Search in sources :

Example 46 with ConstraintViolationException

use of javax.jcr.nodetype.ConstraintViolationException in project jackrabbit by apache.

the class BatchedItemOperations method createNodeState.

/**
 * Creates a new node based on the given definition.
 * <p>
 * Note that access rights are <b><i>not</i></b> enforced!
 * <p>
 * <b>Precondition:</b> the state manager needs to be in edit mode.
 *
 * @param parent
 * @param nodeName
 * @param nodeTypeName
 * @param mixinNames
 * @param id
 * @param def
 * @return
 * @throws ItemExistsException
 * @throws ConstraintViolationException
 * @throws RepositoryException
 * @throws IllegalStateException
 */
public NodeState createNodeState(NodeState parent, Name nodeName, Name nodeTypeName, Name[] mixinNames, NodeId id, QNodeDefinition def) throws ItemExistsException, ConstraintViolationException, RepositoryException, IllegalStateException {
    // check for name collisions with existing nodes
    if (!def.allowsSameNameSiblings() && parent.hasChildNodeEntry(nodeName)) {
        NodeId errorId = parent.getChildNodeEntry(nodeName, 1).getId();
        throw new ItemExistsException(safeGetJCRPath(errorId));
    }
    if (nodeTypeName == null) {
        // no primary node type specified,
        // try default primary type from definition
        nodeTypeName = def.getDefaultPrimaryType();
        if (nodeTypeName == null) {
            String msg = "an applicable node type could not be determined for " + nodeName;
            log.debug(msg);
            throw new ConstraintViolationException(msg);
        }
    }
    NodeState node = stateMgr.createNew(id, nodeTypeName, parent.getNodeId());
    if (mixinNames != null && mixinNames.length > 0) {
        node.setMixinTypeNames(new HashSet<Name>(Arrays.asList(mixinNames)));
    }
    // now add new child node entry to parent
    parent.addChildNodeEntry(nodeName, node.getNodeId());
    EffectiveNodeType ent = getEffectiveNodeType(node);
    // check shareable
    if (ent.includesNodeType(NameConstants.MIX_SHAREABLE)) {
        node.addShare(parent.getNodeId());
    }
    if (!node.getMixinTypeNames().isEmpty()) {
        // create jcr:mixinTypes property
        QPropertyDefinition pd = ent.getApplicablePropertyDef(NameConstants.JCR_MIXINTYPES, PropertyType.NAME, true);
        createPropertyState(node, pd.getName(), pd.getRequiredType(), pd);
    }
    // add 'auto-create' properties defined in node type
    for (QPropertyDefinition pd : ent.getAutoCreatePropDefs()) {
        createPropertyState(node, pd.getName(), pd.getRequiredType(), pd);
    }
    // recursively add 'auto-create' child nodes defined in node type
    for (QNodeDefinition nd : ent.getAutoCreateNodeDefs()) {
        createNodeState(node, nd.getName(), nd.getDefaultPrimaryType(), null, null, nd);
    }
    // store node
    stateMgr.store(node);
    // store parent
    stateMgr.store(parent);
    return node;
}
Also used : EffectiveNodeType(org.apache.jackrabbit.core.nodetype.EffectiveNodeType) NodeState(org.apache.jackrabbit.core.state.NodeState) ItemExistsException(javax.jcr.ItemExistsException) QPropertyDefinition(org.apache.jackrabbit.spi.QPropertyDefinition) NodeId(org.apache.jackrabbit.core.id.NodeId) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) QNodeDefinition(org.apache.jackrabbit.spi.QNodeDefinition) Name(org.apache.jackrabbit.spi.Name)

Example 47 with ConstraintViolationException

use of javax.jcr.nodetype.ConstraintViolationException in project jackrabbit by apache.

the class AddMixinOperation method perform.

public Object perform(SessionContext context) throws RepositoryException {
    int permissions = Permission.NODE_TYPE_MNGMT;
    // is required in addition.
    if (MIX_VERSIONABLE.equals(mixinName) || MIX_SIMPLE_VERSIONABLE.equals(mixinName)) {
        permissions |= Permission.VERSION_MNGMT;
    }
    context.getItemValidator().checkModify(node, CHECK_LOCK | CHECK_CHECKED_OUT | CHECK_CONSTRAINTS | CHECK_HOLD, permissions);
    NodeTypeManagerImpl ntMgr = context.getNodeTypeManager();
    NodeTypeImpl mixin = ntMgr.getNodeType(mixinName);
    if (!mixin.isMixin()) {
        throw new RepositoryException(context.getJCRName(mixinName) + " is not a mixin node type");
    }
    Name primaryTypeName = node.getNodeState().getNodeTypeName();
    NodeTypeImpl primaryType = ntMgr.getNodeType(primaryTypeName);
    if (primaryType.isDerivedFrom(mixinName)) {
        // new mixin is already included in primary type
        return this;
    }
    // build effective node type of mixin's & primary type in order
    // to detect conflicts
    NodeTypeRegistry ntReg = context.getNodeTypeRegistry();
    EffectiveNodeType entExisting;
    try {
        // existing mixin's
        Set<Name> mixins = new HashSet<Name>(node.getNodeState().getMixinTypeNames());
        // build effective node type representing primary type including
        // existing mixin's
        entExisting = ntReg.getEffectiveNodeType(primaryTypeName, mixins);
        if (entExisting.includesNodeType(mixinName)) {
            // new mixin is already included in existing mixin type(s)
            return this;
        }
        // add new mixin
        mixins.add(mixinName);
        // try to build new effective node type (will throw in case
        // of conflicts)
        ntReg.getEffectiveNodeType(primaryTypeName, mixins);
    } catch (NodeTypeConflictException e) {
        throw new ConstraintViolationException(e.getMessage(), e);
    }
    // try to revert the changes in case an exception occurs
    try {
        // modify the state of this node
        NodeState thisState = (NodeState) node.getOrCreateTransientItemState();
        // add mixin name
        Set<Name> mixins = new HashSet<Name>(thisState.getMixinTypeNames());
        mixins.add(mixinName);
        thisState.setMixinTypeNames(mixins);
        // set jcr:mixinTypes property
        node.setMixinTypesProperty(mixins);
        // add 'auto-create' properties defined in mixin type
        for (PropertyDefinition aPda : mixin.getAutoCreatedPropertyDefinitions()) {
            PropertyDefinitionImpl pd = (PropertyDefinitionImpl) aPda;
            // make sure that the property is not already defined
            // by primary type or existing mixin's
            NodeTypeImpl declaringNT = (NodeTypeImpl) pd.getDeclaringNodeType();
            if (!entExisting.includesNodeType(declaringNT.getQName())) {
                node.createChildProperty(pd.unwrap().getName(), pd.getRequiredType(), pd);
            }
        }
        // recursively add 'auto-create' child nodes defined in mixin type
        for (NodeDefinition aNda : mixin.getAutoCreatedNodeDefinitions()) {
            NodeDefinitionImpl nd = (NodeDefinitionImpl) aNda;
            // make sure that the child node is not already defined
            // by primary type or existing mixin's
            NodeTypeImpl declaringNT = (NodeTypeImpl) nd.getDeclaringNodeType();
            if (!entExisting.includesNodeType(declaringNT.getQName())) {
                node.createChildNode(nd.unwrap().getName(), (NodeTypeImpl) nd.getDefaultPrimaryType(), null);
            }
        }
    } catch (RepositoryException re) {
        // try to undo the modifications by removing the mixin
        try {
            node.removeMixin(mixinName);
        } catch (RepositoryException re1) {
        // silently ignore & fall through
        }
        throw re;
    }
    return this;
}
Also used : NodeTypeImpl(org.apache.jackrabbit.core.nodetype.NodeTypeImpl) NodeState(org.apache.jackrabbit.core.state.NodeState) NodeTypeConflictException(org.apache.jackrabbit.core.nodetype.NodeTypeConflictException) NodeDefinition(javax.jcr.nodetype.NodeDefinition) PropertyDefinitionImpl(org.apache.jackrabbit.spi.commons.nodetype.PropertyDefinitionImpl) RepositoryException(javax.jcr.RepositoryException) PropertyDefinition(javax.jcr.nodetype.PropertyDefinition) Name(org.apache.jackrabbit.spi.Name) EffectiveNodeType(org.apache.jackrabbit.core.nodetype.EffectiveNodeType) NodeDefinitionImpl(org.apache.jackrabbit.spi.commons.nodetype.NodeDefinitionImpl) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) NodeTypeRegistry(org.apache.jackrabbit.core.nodetype.NodeTypeRegistry) NodeTypeManagerImpl(org.apache.jackrabbit.core.nodetype.NodeTypeManagerImpl) HashSet(java.util.HashSet)

Example 48 with ConstraintViolationException

use of javax.jcr.nodetype.ConstraintViolationException in project jackrabbit by apache.

the class AbstractWriteTest method testACItemsAreProtected.

public void testACItemsAreProtected() throws NotExecutableException, RepositoryException {
    // search for a rep:policy node
    Node policyNode = findPolicyNode(superuser.getRootNode());
    if (policyNode == null) {
        throw new NotExecutableException("no policy node found.");
    }
    assertTrue("The rep:Policy node must be protected", policyNode.getDefinition().isProtected());
    try {
        policyNode.remove();
        fail("rep:Policy node must be protected.");
    } catch (ConstraintViolationException e) {
    // success
    }
    for (NodeIterator it = policyNode.getNodes(); it.hasNext(); ) {
        Node n = it.nextNode();
        if (n.isNodeType("rep:ACE")) {
            try {
                n.remove();
                fail("ACE node must be protected.");
            } catch (ConstraintViolationException e) {
            // success
            }
            break;
        }
    }
    try {
        policyNode.setProperty("test", "anyvalue");
        fail("rep:policy node must be protected.");
    } catch (ConstraintViolationException e) {
    // success
    }
    try {
        policyNode.addNode("test", "rep:ACE");
        fail("rep:policy node must be protected.");
    } catch (ConstraintViolationException e) {
    // success
    }
}
Also used : NodeIterator(javax.jcr.NodeIterator) NotExecutableException(org.apache.jackrabbit.test.NotExecutableException) JackrabbitNode(org.apache.jackrabbit.api.JackrabbitNode) Node(javax.jcr.Node) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException)

Example 49 with ConstraintViolationException

use of javax.jcr.nodetype.ConstraintViolationException in project jackrabbit by apache.

the class AddNodeTest method testMixinNodeType.

/**
 * Tests if addNode() throws a ConstraintViolationException in case
 * of an mixin node type.
 */
public void testMixinNodeType() throws RepositoryException, NotExecutableException {
    NodeTypeManager ntMgr = superuser.getWorkspace().getNodeTypeManager();
    NodeTypeIterator nts = ntMgr.getMixinNodeTypes();
    if (nts.hasNext()) {
        try {
            testRootNode.addNode(nodeName1, nts.nextNodeType().getName());
            superuser.save();
            fail("Expected ConstraintViolationException.");
        } catch (ConstraintViolationException e) {
        // correct.
        }
    } else {
        throw new NotExecutableException("no mixins.");
    }
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) NotExecutableException(org.apache.jackrabbit.test.NotExecutableException) NodeTypeIterator(javax.jcr.nodetype.NodeTypeIterator) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException)

Example 50 with ConstraintViolationException

use of javax.jcr.nodetype.ConstraintViolationException in project jackrabbit by apache.

the class AddNodeTest method internalTestSimilarNodeNames.

/**
 * Tests behavior for creation of "similarly" named nodes
 * @throws RepositoryException
 */
private void internalTestSimilarNodeNames(String name1, String name2) throws RepositoryException {
    Node n1 = null, n2 = null;
    Session s = testRootNode.getSession();
    try {
        n1 = testRootNode.addNode(name1);
        assertEquals(name1, n1.getName());
        s.save();
        assertFalse(testRootNode.hasNode(name2));
    } catch (ConstraintViolationException e) {
    // accepted
    }
    try {
        n2 = testRootNode.addNode(name2);
        assertEquals(name2, n2.getName());
        s.save();
    } catch (ConstraintViolationException e) {
    // accepted
    }
    // If both nodes have been created, do further checks
    if (n1 != null && n2 != null) {
        assertFalse(n1.isSame(n2));
        assertFalse(n1.getIdentifier().equals(n2.getIdentifier()));
        String n2path = n2.getPath();
        n1.remove();
        s.save();
        Node n3 = s.getNode(n2path);
        assertTrue(n3.isSame(n2));
    }
}
Also used : Node(javax.jcr.Node) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) Session(javax.jcr.Session)

Aggregations

ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)180 Node (javax.jcr.Node)74 RepositoryException (javax.jcr.RepositoryException)40 Name (org.apache.jackrabbit.spi.Name)33 Value (javax.jcr.Value)31 NotExecutableException (org.apache.jackrabbit.test.NotExecutableException)28 Test (org.junit.Test)27 PropertyDefinition (javax.jcr.nodetype.PropertyDefinition)23 QPropertyDefinition (org.apache.jackrabbit.spi.QPropertyDefinition)23 Session (javax.jcr.Session)18 ItemExistsException (javax.jcr.ItemExistsException)17 NodeState (org.apache.jackrabbit.core.state.NodeState)16 QNodeDefinition (org.apache.jackrabbit.spi.QNodeDefinition)16 Property (javax.jcr.Property)14 NodeTypeManager (javax.jcr.nodetype.NodeTypeManager)14 ArrayList (java.util.ArrayList)13 NodeId (org.apache.jackrabbit.core.id.NodeId)13 EffectiveNodeType (org.apache.jackrabbit.core.nodetype.EffectiveNodeType)13 NodeType (javax.jcr.nodetype.NodeType)12 ChildNodeEntry (org.apache.jackrabbit.core.state.ChildNodeEntry)12