Package org.jboss.dna.connector.store.jpa.model.common

Examples of org.jboss.dna.connector.store.jpa.model.common.NamespaceEntity


                                    Name childName,
                                    boolean allowSameNameChildrenInNewNode ) {
        int nextSnsIndex = 1; // SNS index is 1-based
        int nextIndexInParent = 0; // index-in-parent is 0-based
        String childNsUri = childName.getNamespaceUri();
        NamespaceEntity ns = namespaces.get(childNsUri, true);
        assert ns != null;

        // If the parent is null, the create a root node ...
        Path parentPath = null;
        String parentUuid = null;
        ChildEntity parentEntity = null;
        if (parent == null) {
            return Location.create(pathFactory.createRootPath(), UUID.fromString(childUuid));
        }
        parentPath = parent.location.getPath();
        parentUuid = parent.uuid;
        parentEntity = parent.childEntity; // may be null

        assert workspaceId != null;

        ChildId id = new ChildId(workspaceId, parentUuid, childUuid);
        ChildEntity entity = null;

        // Look in the cache for the children of the parent node.
        LinkedList<Location> childrenOfParent = cache.getAllChildren(workspaceId, parentPath);

        // Now create the entity ...
        if (parentEntity == null || parentEntity.getAllowsSameNameChildren()) {
            // The parent DOES allow same-name-siblings, so we need to find the SNS index ...

            if (childrenOfParent != null) {
                // The cache had the complete list of children for the parent node, which means
                // we know about all of the children and can walk the children to figure out the next indexes.
                nextIndexInParent = childrenOfParent.size();
                if (nextIndexInParent > 1) {
                    // Since we want the last indexes, process the list backwards ...
                    ListIterator<Location> iter = childrenOfParent.listIterator(childrenOfParent.size());
                    while (iter.hasPrevious()) {
                        Location existing = iter.previous();
                        Path.Segment segment = existing.getPath().getLastSegment();
                        if (!segment.getName().equals(childName)) continue;
                        // Otherwise the name matched, so get the indexes ...
                        nextSnsIndex = segment.getIndex() + 1;
                    }
                }
            } else {
                // The cache did not have the complete list of children for the parent node,
                // so we need to look the values up by querying the database ...

                // Find the largest SNS index in the existing ChildEntity objects with the same name ...
                String childLocalName = childName.getLocalName();
                Query query = entities.createNamedQuery("ChildEntity.findMaximumSnsIndex");
                query.setParameter("workspaceId", workspaceId);
                query.setParameter("parentUuid", parentUuid);
                query.setParameter("ns", ns.getId());
                query.setParameter("childName", childLocalName);
                try {
                    Integer result = (Integer)query.getSingleResult();
                    nextSnsIndex = result != null ? result + 1 : 1; // SNS index is 1-based
                } catch (NoResultException e) {
View Full Code Here


                    final int oldIndex = fromEntity.getIndexInParent();

                    // Make sure the child name is set correctly ...
                    String childOldLocalName = fromEntity.getChildName();
                    String childLocalName = null;
                    NamespaceEntity ns = null;
                    Name childName = request.desiredName();
                    if (childName != null) {
                        childLocalName = request.desiredName().getLocalName();
                        String childNsUri = childName.getNamespaceUri();
                        ns = namespaces.get(childNsUri, true);
                    } else {
                        childName = oldPath.getLastSegment().getName();
                        childLocalName = fromEntity.getChildName();
                        ns = fromEntity.getChildNamespace();
                    }

                    // Find the largest SNS index in the existing ChildEntity objects with the same name ...
                    Query query = entities.createNamedQuery("ChildEntity.findMaximumSnsIndex");
                    query.setParameter("workspaceId", workspaceId);
                    query.setParameter("parentUuid", toUuidString);
                    query.setParameter("ns", ns.getId());
                    query.setParameter("childName", childLocalName);
                    int nextSnsIndex = 1;
                    try {
                        Integer index = (Integer)query.getSingleResult();
                        if (index != null) nextSnsIndex = index.intValue() + 1;
                    } catch (NoResultException e) {
                    }

                    // Find the largest child index in the existing ChildEntity objects ...
                    query = entities.createNamedQuery("ChildEntity.findMaximumChildIndex");
                    query.setParameter("workspaceId", workspaceId);
                    query.setParameter("parentUuid", toUuidString);
                    int nextIndexInParent = 1;
                    try {
                        Integer index = (Integer)query.getSingleResult();
                        if (index != null) nextIndexInParent = index + 1;
                    } catch (NoResultException e) {
                    }

                    ChildId movedId = new ChildId(workspaceId, toUuidString, fromUuidString);
                    if (fromEntity.getId().equals(movedId)) {
                        // The node is being renamed, but not moved ...
                        fromEntity.setChildName(childLocalName);
                        fromEntity.setChildNamespace(ns);
                        fromEntity.setIndexInParent(nextIndexInParent);
                        fromEntity.setSameNameSiblingIndex(nextSnsIndex);
                    } else {
                        // We won't be able to move the entity to a different parent, because that would involve
                        // changing the PK for the entity, which is not possible. Instead, we have to create a
                        // new entity with the same identity information, then delete 'fromEntity'
                        ChildEntity movedEntity = new ChildEntity(movedId, nextIndexInParent, ns, childLocalName, nextSnsIndex);
                        movedEntity.setAllowsSameNameChildren(fromEntity.getAllowsSameNameChildren());
                        entities.persist(movedEntity);
                        entities.remove(fromEntity);
                    }

                    // Flush the entities to the database ...
                    entities.flush();

                    // Determine the new location ...
                    Path newParentPath = actualIntoLocation.location.getPath();
                    Path newPath = pathFactory.create(newParentPath, childName, nextSnsIndex);
                    actualNewLocation = actualOldLocation.with(newPath);

                    // And adjust the SNS index and indexes ...
                    ChildEntity.adjustSnsIndexesAndIndexesAfterRemoving(entities,
                                                                        workspaceId,
                                                                        oldParentUuid,
                                                                        childOldLocalName,
                                                                        ns.getId(),
                                                                        oldIndex);

                    // Update the cache ...
                    cache.moveNode(workspaceId, actualOldLocation, oldIndex, actualNewLocation);
                }
View Full Code Here

        assert pathSegment != null;
        assert workspaceId != null;
        Name name = pathSegment.getName();
        String localName = name.getLocalName();
        String nsUri = name.getNamespaceUri();
        NamespaceEntity ns = namespaces.get(nsUri, false);
        if (ns == null) {
            // The namespace can't be found, then certainly the node won't be found ...
            return null;
        }
        int snsIndex = pathSegment.hasIndex() ? pathSegment.getIndex() : 1;
        Query query = entities.createNamedQuery("ChildEntity.findByPathSegment");
        query.setParameter("workspaceId", workspaceId);
        query.setParameter("parentUuidString", parentUuid);
        query.setParameter("ns", ns.getId());
        query.setParameter("childName", localName);
        query.setParameter("sns", snsIndex);
        try {
            return (ChildEntity)query.getSingleResult();
        } catch (NoResultException e) {
View Full Code Here

        this.entityManager = manager;
    }

    public NamespaceEntity get( String namespaceUri,
                                boolean createIfRequired ) {
        NamespaceEntity entity = cache.get(namespaceUri);
        if (entity == null) {
            entity = NamespaceEntity.findByUri(entityManager, namespaceUri, createIfRequired);
            if (entity != null) {
                cache.put(namespaceUri, entity);
            }
View Full Code Here

            createProperties(workspaceId, uuidString, request.properties());

            // Find or create the namespace for the child ...
            Name childName = request.named();
            String childNsUri = childName.getNamespaceUri();
            NamespaceEntity ns = namespaces.get(childNsUri, true);
            assert ns != null;
            final Path parentPath = actual.location.getPath();
            assert parentPath != null;

            // Figure out the next SNS index and index-in-parent for this new child ...
View Full Code Here

        factory = configurator.buildEntityManagerFactory();
        manager = factory.createEntityManager();

        // Always create a bunch of nodes in a workspace that is not used, so we're sure that these
        // tests work only on the workspace used in the test
        NamespaceEntity ns = NamespaceEntity.findByUri(manager, "http://www.example.com");
        Long workspaceId = 1202L;
        for (int i = 0; i != 10; ++i) {
            createChildren(workspaceId, UUID.randomUUID(), ns, 1, 10, "child", false);
        }
    }
View Full Code Here

    @Test
    public void shouldCreateChildrenWithDifferentNames() {
        Long workspaceId = 1L;
        UUID parentUuid = UUID.randomUUID();
        NamespaceEntity ns = NamespaceEntity.findByUri(manager, "http://www.example.com");
        ChildId[] ids = createChildren(workspaceId, parentUuid, ns, 1, 10, "child", false);

        // Look up the object ...
        manager.getTransaction().begin();
        try {
View Full Code Here

    @Test
    public void shouldCreateChildrenWithSameNameSiblingIndex() {
        Long workspaceId = 1L;
        UUID parentUuid = UUID.randomUUID();
        NamespaceEntity ns = NamespaceEntity.findByUri(manager, "http://www.example.com");
        ChildId[] ids = createChildren(workspaceId, parentUuid, ns, 1, 10, "child", true);

        // Look up the object ...
        manager.getTransaction().begin();
        try {
View Full Code Here

    @SuppressWarnings( "unchecked" )
    @Test
    public void shouldCreateMixtureOfChildrenWithDifferentNamesAndSameNameSiblingIndexes() {
        Long workspaceId = 1L;
        UUID parentUuid = UUID.randomUUID();
        NamespaceEntity ns = NamespaceEntity.findByUri(manager, "http://www.example.com");
        createChildren(workspaceId, parentUuid, ns, 1, 10, "child", false);
        createChildren(workspaceId, parentUuid, ns, 11, 10, "childWithSameName", true);
        createChildren(workspaceId, parentUuid, ns, 21, 1, "anotherChild", false);
        createChildren(workspaceId, parentUuid, ns, 22, 1, "nextToLastChild", false);
        createChildren(workspaceId, parentUuid, ns, 23, 1, "lastChild", false);
View Full Code Here

    @SuppressWarnings( "unchecked" )
    @Test
    public void shouldCreateMixtureOfChildrenWithDifferentNamesAndSameNameSiblingIndexesMethod2() {
        Long workspaceId = 1L;
        UUID parentUuid = UUID.randomUUID();
        NamespaceEntity ns = NamespaceEntity.findByUri(manager, "http://www.example.com");
        ChildId[] ids = createMixtureOfChildren(workspaceId, parentUuid, ns);
        assertThat(ids.length, is(23));

        // Look up the object ...
        manager.getTransaction().begin();
View Full Code Here

TOP

Related Classes of org.jboss.dna.connector.store.jpa.model.common.NamespaceEntity

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.