Search in sources :

Example 41 with ItemExistsException

use of javax.jcr.ItemExistsException in project jackrabbit by apache.

the class AddNodeOperation method perform.

public Node perform(SessionContext context) throws RepositoryException {
    ItemManager itemMgr = context.getItemManager();
    // Get the canonical path of the new node
    Path path;
    try {
        path = PathFactoryImpl.getInstance().create(node.getPrimaryPath(), context.getQPath(relPath), true);
    } catch (NameException e) {
        throw new RepositoryException("Failed to resolve path " + relPath + " relative to " + node, e);
    }
    // Check that the last path element is a simple name
    if (!path.denotesName() || path.getIndex() != Path.INDEX_UNDEFINED) {
        throw new RepositoryException("Invalid last path element for adding node " + relPath + " relative to " + node);
    }
    // Get the parent node instance
    NodeImpl parentNode;
    Path parentPath = path.getAncestor(1);
    try {
        parentNode = itemMgr.getNode(parentPath);
    } catch (PathNotFoundException e) {
        if (itemMgr.propertyExists(parentPath)) {
            throw new ConstraintViolationException("Unable to add a child node to property " + context.getJCRPath(parentPath));
        }
        throw e;
    } catch (AccessDeniedException ade) {
        throw new PathNotFoundException("Failed to resolve path " + relPath + " relative to " + node);
    }
    // Resolve node type name (if any)
    Name typeName = null;
    if (nodeTypeName != null) {
        typeName = context.getQName(nodeTypeName);
    }
    // Check that the given UUID (if any) does not already exist
    NodeId id = null;
    if (uuid != null) {
        id = new NodeId(uuid);
        if (itemMgr.itemExists(id)) {
            throw new ItemExistsException("A node with this UUID already exists: " + uuid);
        }
    }
    return parentNode.addNode(path.getName(), typeName, id);
}
Also used : Path(org.apache.jackrabbit.spi.Path) AccessDeniedException(javax.jcr.AccessDeniedException) NameException(org.apache.jackrabbit.spi.commons.conversion.NameException) NodeImpl(org.apache.jackrabbit.core.NodeImpl) ItemManager(org.apache.jackrabbit.core.ItemManager) ItemExistsException(javax.jcr.ItemExistsException) NodeId(org.apache.jackrabbit.core.id.NodeId) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) RepositoryException(javax.jcr.RepositoryException) PathNotFoundException(javax.jcr.PathNotFoundException) Name(org.apache.jackrabbit.spi.Name)

Example 42 with ItemExistsException

use of javax.jcr.ItemExistsException in project jackrabbit by apache.

the class AddNodeTest method testSameNameSiblings.

/**
     * Tests if same name siblings have equal names or if same name
     * siblings are not supported a ItemExistsException is thrown.
     */
public void testSameNameSiblings() throws RepositoryException {
    if (testRootNode.getDefinition().allowsSameNameSiblings()) {
        Node n1 = testRootNode.addNode(nodeName1, testNodeType);
        Node n2 = testRootNode.addNode(nodeName1, testNodeType);
        testRootNode.getSession().save();
        assertEquals("Names of same name siblings are not equal.", n1.getName(), n2.getName());
    } else {
        testRootNode.addNode(nodeName1, testNodeType);
        try {
            testRootNode.addNode(nodeName1, testNodeType);
            fail("Expected ItemExistsException.");
        } catch (ItemExistsException e) {
        // correct
        }
    }
}
Also used : ItemExistsException(javax.jcr.ItemExistsException) Node(javax.jcr.Node)

Example 43 with ItemExistsException

use of javax.jcr.ItemExistsException in project jackrabbit by apache.

the class SaveTest method testItemExistsException.

/**
     * Tests if an {@link javax.jcr.ItemExistsException} is thrown when a query
     * is stored on an existing node and same name siblings are not allowed.
     * @throws NotExecutableException if nt:query is not supported.
     */
public void testItemExistsException() throws RepositoryException, NotExecutableException {
    checkNtQuery();
    Query query = superuser.getWorkspace().getQueryManager().createQuery(statement, Query.XPATH);
    Node qNode = query.storeAsNode(testRoot + "/" + nodeName1);
    // create another one
    query = superuser.getWorkspace().getQueryManager().createQuery(statement, Query.XPATH);
    try {
        query.storeAsNode(testRoot + "/" + nodeName1);
        if (!qNode.getDefinition().allowsSameNameSiblings()) {
            // must throw if same name siblings are not allowed
            fail("Query.storeAsNode() did not throw ItemExistsException");
        }
    } catch (ItemExistsException e) {
        if (qNode.getDefinition().allowsSameNameSiblings()) {
            fail("Query.storeAsNode() must not throw ItemExistsException " + "when same name siblings are allowed");
        } else {
        // expected behaviour
        }
    }
}
Also used : Query(javax.jcr.query.Query) ItemExistsException(javax.jcr.ItemExistsException) Node(javax.jcr.Node)

Example 44 with ItemExistsException

use of javax.jcr.ItemExistsException in project jackrabbit by apache.

the class RestoreTest method testRestoreWithUUIDConflict.

/**
     * Tests if restoring the <code>Version</code> of an existing node throws an
     * <code>ItemExistsException</code> if removeExisting is set to FALSE.
     */
@SuppressWarnings("deprecation")
public void testRestoreWithUUIDConflict() throws RepositoryException, NotExecutableException {
    try {
        Node naa = createVersionableNode(versionableNode, nodeName4, versionableNodeType);
        // Verify that nodes used for the test have proper opv behaviour
        NodeDefinition nd = naa.getDefinition();
        if (nd.getOnParentVersion() != OnParentVersionAction.COPY && nd.getOnParentVersion() != OnParentVersionAction.VERSION) {
            throw new NotExecutableException("Child nodes must have OPV COPY or VERSION in order to be able to test Node.restore with uuid conflict.");
        }
        Version v = versionableNode.checkin();
        versionableNode.checkout();
        superuser.move(naa.getPath(), versionableNode2.getPath() + "/" + naa.getName());
        superuser.save();
        versionableNode.restore(v, false);
        fail("Node.restore( Version, boolean ): An ItemExistsException must be thrown if the node to be restored already exsits and removeExisting was set to false.");
    } catch (ItemExistsException e) {
    // success
    }
}
Also used : NotExecutableException(org.apache.jackrabbit.test.NotExecutableException) Version(javax.jcr.version.Version) ItemExistsException(javax.jcr.ItemExistsException) Node(javax.jcr.Node) NodeDefinition(javax.jcr.nodetype.NodeDefinition)

Example 45 with ItemExistsException

use of javax.jcr.ItemExistsException in project jackrabbit by apache.

the class RestoreTest method testRestoreWithUUIDConflictJcr2.

/**
     * Tests if restoring the <code>Version</code> of an existing node throws an
     * <code>ItemExistsException</code> if removeExisting is set to FALSE.
     */
public void testRestoreWithUUIDConflictJcr2() throws RepositoryException, NotExecutableException {
    try {
        Node naa = createVersionableNode(versionableNode, nodeName4, versionableNodeType);
        // Verify that nodes used for the test have proper opv behaviour
        NodeDefinition nd = naa.getDefinition();
        if (nd.getOnParentVersion() != OnParentVersionAction.COPY && nd.getOnParentVersion() != OnParentVersionAction.VERSION) {
            throw new NotExecutableException("Child nodes must have OPV COPY or VERSION in order to be able to test Node.restore with uuid conflict.");
        }
        Version v = versionManager.checkin(versionableNode.getPath());
        versionManager.checkout(versionableNode.getPath());
        superuser.move(naa.getPath(), versionableNode2.getPath() + "/" + naa.getName());
        superuser.save();
        versionManager.restore(v, false);
        fail("Node.restore( Version, boolean ): An ItemExistsException must be thrown if the node to be restored already exsits and removeExisting was set to false.");
    } catch (ItemExistsException e) {
    // success
    }
}
Also used : NotExecutableException(org.apache.jackrabbit.test.NotExecutableException) Version(javax.jcr.version.Version) ItemExistsException(javax.jcr.ItemExistsException) Node(javax.jcr.Node) NodeDefinition(javax.jcr.nodetype.NodeDefinition)

Aggregations

ItemExistsException (javax.jcr.ItemExistsException)63 Node (javax.jcr.Node)25 RepositoryException (javax.jcr.RepositoryException)25 NotExecutableException (org.apache.jackrabbit.test.NotExecutableException)16 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)15 NodeDefinition (javax.jcr.nodetype.NodeDefinition)14 ItemNotFoundException (javax.jcr.ItemNotFoundException)13 Version (javax.jcr.version.Version)13 NodeId (org.apache.jackrabbit.core.id.NodeId)12 Name (org.apache.jackrabbit.spi.Name)12 NodeState (org.apache.jackrabbit.core.state.NodeState)10 ChildNodeEntry (org.apache.jackrabbit.core.state.ChildNodeEntry)9 Path (org.apache.jackrabbit.spi.Path)8 PathNotFoundException (javax.jcr.PathNotFoundException)6 EffectiveNodeType (org.apache.jackrabbit.core.nodetype.EffectiveNodeType)6 QPropertyDefinition (org.apache.jackrabbit.spi.QPropertyDefinition)6 NodeImpl (org.apache.jackrabbit.core.NodeImpl)5 QNodeDefinition (org.apache.jackrabbit.spi.QNodeDefinition)5 ArrayList (java.util.ArrayList)4 AccessDeniedException (javax.jcr.AccessDeniedException)4