Search in sources :

Example 6 with EffectiveNodeType

use of org.apache.jackrabbit.jcr2spi.nodetype.EffectiveNodeType in project jackrabbit by apache.

the class ItemStateValidator method checkAddNode.

/**
 * Checks if adding a child node called <code>nodeName</code> of node type
 * <code>nodeTypeName</code> to the given parent node is allowed in the
 * current context.
 *
 * @param parentState
 * @param nodeName
 * @param nodeTypeName
 * @param options      bit-wise OR'ed flags specifying the checks that should be
 *                     performed; any combination of the following constants:
 * <ul>
 * <li><code>{@link #CHECK_ACCESS}</code>: make sure current session is
 * granted read access on parent node and can add a child node with the
 * given name.</li>
 * <li><code>{@link #CHECK_LOCK}</code>: make sure there's no foreign lock
 * on parent node</li>
 * <li><code>{@link #CHECK_VERSIONING}</code>: make sure parent node is
 * checked-out</li>
 * <li><code>{@link #CHECK_CONSTRAINTS}</code>: make sure no node type
 * constraints would be violated</li>
 * <li><code>{@link #CHECK_COLLISION}</code>: check for collision with
 * existing properties or nodes</li>
 * </ul>
 *
 * @throws ConstraintViolationException
 * @throws AccessDeniedException
 * @throws VersionException
 * @throws LockException
 * @throws ItemNotFoundException
 * @throws ItemExistsException
 * @throws RepositoryException
 */
public void checkAddNode(NodeState parentState, Name nodeName, Name nodeTypeName, int options) throws ConstraintViolationException, AccessDeniedException, VersionException, LockException, ItemNotFoundException, ItemExistsException, RepositoryException {
    checkIsWritable(parentState, options);
    // access restrictions on new node
    if ((options & CHECK_ACCESS) == CHECK_ACCESS) {
        // make sure current session is granted write access on parent node
        Path relPath = pathFactory.create(nodeName);
        if (!mgrProvider.getAccessManager().isGranted(parentState, relPath, new String[] { AccessManager.ADD_NODE_ACTION })) {
            throw new AccessDeniedException(safeGetJCRPath(parentState) + ": not allowed to add child node '" + nodeName + "'");
        }
    }
    // node type constraints
    if ((options & CHECK_CONSTRAINTS) == CHECK_CONSTRAINTS) {
        // make sure there's an applicable definition for new child node
        Name[] ntNames = parentState.getAllNodeTypeNames();
        EffectiveNodeType entParent = mgrProvider.getEffectiveNodeTypeProvider().getEffectiveNodeType(ntNames);
        QNodeTypeDefinition def = mgrProvider.getNodeTypeDefinitionProvider().getNodeTypeDefinition(nodeTypeName);
        entParent.checkAddNodeConstraints(nodeName, def, mgrProvider.getItemDefinitionProvider());
    }
    // collisions
    if ((options & CHECK_COLLISION) == CHECK_COLLISION) {
        checkCollision(parentState, nodeName, nodeTypeName);
    }
}
Also used : Path(org.apache.jackrabbit.spi.Path) QNodeTypeDefinition(org.apache.jackrabbit.spi.QNodeTypeDefinition) AccessDeniedException(javax.jcr.AccessDeniedException) EffectiveNodeType(org.apache.jackrabbit.jcr2spi.nodetype.EffectiveNodeType) Name(org.apache.jackrabbit.spi.Name)

Example 7 with EffectiveNodeType

use of org.apache.jackrabbit.jcr2spi.nodetype.EffectiveNodeType in project jackrabbit by apache.

the class ItemStateValidator method validate.

/**
 * Checks whether the given node state satisfies the constraints specified
 * by its primary and mixin node types. The following validations/checks are
 * performed:
 * <ul>
 * <li>check if its node type satisfies the 'required node types' constraint
 * specified in its definition</li>
 * <li>check if all 'mandatory' child items exist</li>
 * <li>for every property: check if the property value satisfies the
 * value constraints specified in the property's definition</li>
 * </ul>
 *
 * @param nodeState state of node to be validated
 * @throws ConstraintViolationException if any of the validations fail
 * @throws RepositoryException          if another error occurs
 */
public void validate(NodeState nodeState) throws ConstraintViolationException, RepositoryException {
    // effective primary node type
    EffectiveNodeType entPrimary = mgrProvider.getEffectiveNodeTypeProvider().getEffectiveNodeType(nodeState.getNodeTypeName());
    QNodeDefinition def = nodeState.getDefinition();
    // check if primary type satisfies the 'required node types' constraint
    Name[] requiredPrimaryTypes = def.getRequiredPrimaryTypes();
    for (int i = 0; i < requiredPrimaryTypes.length; i++) {
        if (!entPrimary.includesNodeType(requiredPrimaryTypes[i])) {
            String msg = safeGetJCRPath(nodeState) + ": missing required primary type " + requiredPrimaryTypes[i];
            log.debug(msg);
            throw new ConstraintViolationException(msg);
        }
    }
    // mandatory properties
    // effective node type (primary type incl. mixins)
    Name[] ntNames = nodeState.getAllNodeTypeNames();
    EffectiveNodeType entPrimaryAndMixins = mgrProvider.getEffectiveNodeTypeProvider().getEffectiveNodeType(ntNames);
    QPropertyDefinition[] pda = entPrimaryAndMixins.getMandatoryQPropertyDefinitions();
    for (int i = 0; i < pda.length; i++) {
        QPropertyDefinition pd = pda[i];
        if (!nodeState.hasPropertyName(pd.getName())) {
            String msg = safeGetJCRPath(nodeState) + ": mandatory property " + pd.getName() + " does not exist";
            log.debug(msg);
            throw new ConstraintViolationException(msg);
        }
    }
    // mandatory child nodes
    QNodeDefinition[] cnda = entPrimaryAndMixins.getMandatoryQNodeDefinitions();
    for (int i = 0; i < cnda.length; i++) {
        QNodeDefinition cnd = cnda[i];
        if (!nodeState.getNodeEntry().hasNodeEntry(cnd.getName())) {
            String msg = safeGetJCRPath(nodeState) + ": mandatory child node " + cnd.getName() + " does not exist";
            log.debug(msg);
            throw new ConstraintViolationException(msg);
        }
    }
}
Also used : EffectiveNodeType(org.apache.jackrabbit.jcr2spi.nodetype.EffectiveNodeType) QPropertyDefinition(org.apache.jackrabbit.spi.QPropertyDefinition) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) QNodeDefinition(org.apache.jackrabbit.spi.QNodeDefinition) Name(org.apache.jackrabbit.spi.Name)

Aggregations

EffectiveNodeType (org.apache.jackrabbit.jcr2spi.nodetype.EffectiveNodeType)7 Name (org.apache.jackrabbit.spi.Name)6 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)4 QNodeDefinition (org.apache.jackrabbit.spi.QNodeDefinition)3 ArrayList (java.util.ArrayList)2 QPropertyDefinition (org.apache.jackrabbit.spi.QPropertyDefinition)2 AccessDeniedException (javax.jcr.AccessDeniedException)1 ItemExistsException (javax.jcr.ItemExistsException)1 ItemNotFoundException (javax.jcr.ItemNotFoundException)1 PropertyIterator (javax.jcr.PropertyIterator)1 NoSuchNodeTypeException (javax.jcr.nodetype.NoSuchNodeTypeException)1 NodeType (javax.jcr.nodetype.NodeType)1 NodeEntry (org.apache.jackrabbit.jcr2spi.hierarchy.NodeEntry)1 EffectiveNodeTypeProvider (org.apache.jackrabbit.jcr2spi.nodetype.EffectiveNodeTypeProvider)1 ItemDefinitionProvider (org.apache.jackrabbit.jcr2spi.nodetype.ItemDefinitionProvider)1 NodeTypeImpl (org.apache.jackrabbit.jcr2spi.nodetype.NodeTypeImpl)1 NodeTypeManagerImpl (org.apache.jackrabbit.jcr2spi.nodetype.NodeTypeManagerImpl)1 Operation (org.apache.jackrabbit.jcr2spi.operation.Operation)1 NodeState (org.apache.jackrabbit.jcr2spi.state.NodeState)1 NodeId (org.apache.jackrabbit.spi.NodeId)1