Search in sources :

Example 6 with NodeDefinitionImpl

use of org.apache.jackrabbit.spi.commons.nodetype.NodeDefinitionImpl in project jackrabbit by apache.

the class NodeImpl method createChildNode.

protected synchronized NodeImpl createChildNode(Name name, NodeTypeImpl nodeType, NodeId id) throws RepositoryException {
    // create a new node state
    NodeState nodeState = stateMgr.createTransientNodeState(id, nodeType.getQName(), getNodeId(), ItemState.STATUS_NEW);
    // create Node instance wrapping new node state
    NodeImpl node;
    try {
        // NOTE: since the node is not yet connected to its parent, avoid
        // calling ItemManager#getItem(ItemId) which may include a permission
        // check (with subsequent usage of the hierarachy-mgr -> error).
        // just let the mgr create the new node that is known to exist and
        // which has not been accessed before.
        node = (NodeImpl) itemMgr.createItemInstance(nodeState);
    } catch (RepositoryException re) {
        // something went wrong
        stateMgr.disposeTransientItemState(nodeState);
        // re-throw
        throw re;
    }
    // modify the state of 'this', i.e. the parent node
    NodeState thisState = (NodeState) getOrCreateTransientItemState();
    // add new child node entry
    thisState.addChildNodeEntry(name, nodeState.getNodeId());
    // add 'auto-create' properties defined in node type
    for (PropertyDefinition aPda : nodeType.getAutoCreatedPropertyDefinitions()) {
        PropertyDefinitionImpl pd = (PropertyDefinitionImpl) aPda;
        node.createChildProperty(pd.unwrap().getName(), pd.getRequiredType(), pd);
    }
    // recursively add 'auto-create' child nodes defined in node type
    for (NodeDefinition aNda : nodeType.getAutoCreatedNodeDefinitions()) {
        NodeDefinitionImpl nd = (NodeDefinitionImpl) aNda;
        node.createChildNode(nd.unwrap().getName(), (NodeTypeImpl) nd.getDefaultPrimaryType(), null);
    }
    return node;
}
Also used : NodeDefinitionImpl(org.apache.jackrabbit.spi.commons.nodetype.NodeDefinitionImpl) NodeState(org.apache.jackrabbit.core.state.NodeState) QNodeDefinition(org.apache.jackrabbit.spi.QNodeDefinition) NodeDefinition(javax.jcr.nodetype.NodeDefinition) PropertyDefinitionImpl(org.apache.jackrabbit.spi.commons.nodetype.PropertyDefinitionImpl) RepositoryException(javax.jcr.RepositoryException) QPropertyDefinition(org.apache.jackrabbit.spi.QPropertyDefinition) PropertyDefinition(javax.jcr.nodetype.PropertyDefinition)

Example 7 with NodeDefinitionImpl

use of org.apache.jackrabbit.spi.commons.nodetype.NodeDefinitionImpl 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 8 with NodeDefinitionImpl

use of org.apache.jackrabbit.spi.commons.nodetype.NodeDefinitionImpl in project jackrabbit by apache.

the class NodeImpl method clone.

/**
     * Create a child node that is a clone of a shareable node.
     *
     * @param src shareable source node
     * @param name name of new node
     * @return child node
     * @throws ItemExistsException if there already is a child node with the
     *             name given and the definition does not allow creating another one
     * @throws VersionException if this node is not checked out
     * @throws ConstraintViolationException if no definition is found in this
     *             node that would allow creating the child node
     * @throws LockException if this node is locked
     * @throws RepositoryException if some other error occurs
     */
public synchronized NodeImpl clone(NodeImpl src, Name name) throws ItemExistsException, VersionException, ConstraintViolationException, LockException, RepositoryException {
    Path nodePath;
    try {
        nodePath = PathFactoryImpl.getInstance().create(getPrimaryPath(), name, true);
    } catch (MalformedPathException e) {
        // should never happen
        String msg = "internal error: invalid path " + this;
        log.debug(msg);
        throw new RepositoryException(msg, e);
    }
    // (1) make sure that parent node is checked-out
    // (2) check lock status
    // (3) check protected flag of parent (i.e. this) node
    int options = ItemValidator.CHECK_LOCK | ItemValidator.CHECK_CHECKED_OUT | ItemValidator.CHECK_CONSTRAINTS;
    sessionContext.getItemValidator().checkModify(this, options, Permission.NONE);
    // (4) check for name collisions
    NodeDefinitionImpl def;
    try {
        def = getApplicableChildNodeDefinition(name, null);
    } catch (RepositoryException re) {
        String msg = "no definition found in parent node's node type for new node";
        log.debug(msg);
        throw new ConstraintViolationException(msg, re);
    }
    NodeState thisState = data.getNodeState();
    ChildNodeEntry cne = thisState.getChildNodeEntry(name, 1);
    if (cne != null) {
        // check same-name sibling setting of new node
        if (!def.allowsSameNameSiblings()) {
            throw new ItemExistsException(itemMgr.safeGetJCRPath(nodePath));
        }
        // check same-name sibling setting of existing node
        NodeId newId = cne.getId();
        if (!((NodeImpl) itemMgr.getItem(newId)).getDefinition().allowsSameNameSiblings()) {
            throw new ItemExistsException(itemMgr.safeGetJCRPath(nodePath));
        }
    }
    // (5) do clone operation
    NodeId parentId = getNodeId();
    src.addShareParent(parentId);
    // (6) modify the state of 'this', i.e. the parent node
    NodeId srcId = src.getNodeId();
    thisState = (NodeState) getOrCreateTransientItemState();
    // add new child node entry
    thisState.addChildNodeEntry(name, srcId);
    return itemMgr.getNode(srcId, parentId);
}
Also used : Path(org.apache.jackrabbit.spi.Path) NodeDefinitionImpl(org.apache.jackrabbit.spi.commons.nodetype.NodeDefinitionImpl) NodeState(org.apache.jackrabbit.core.state.NodeState) ItemExistsException(javax.jcr.ItemExistsException) ChildNodeEntry(org.apache.jackrabbit.core.state.ChildNodeEntry) MalformedPathException(org.apache.jackrabbit.spi.commons.conversion.MalformedPathException) NodeId(org.apache.jackrabbit.core.id.NodeId) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) RepositoryException(javax.jcr.RepositoryException)

Example 9 with NodeDefinitionImpl

use of org.apache.jackrabbit.spi.commons.nodetype.NodeDefinitionImpl in project jackrabbit by apache.

the class NodeTypeDefinitionImpl method getDeclaredChildNodeDefinitions.

/**
     * {@inheritDoc}
     */
public NodeDefinition[] getDeclaredChildNodeDefinitions() {
    QItemDefinition[] cnda = ntd.getChildNodeDefs();
    NodeDefinition[] nodeDefs = new NodeDefinition[cnda.length];
    for (int i = 0; i < cnda.length; i++) {
        nodeDefs[i] = new NodeDefinitionImpl(cnda[i], null, resolver);
    }
    return nodeDefs;
}
Also used : NodeDefinitionImpl(org.apache.jackrabbit.spi.commons.nodetype.NodeDefinitionImpl) NodeDefinition(javax.jcr.nodetype.NodeDefinition) QItemDefinition(org.apache.jackrabbit.spi.QItemDefinition)

Example 10 with NodeDefinitionImpl

use of org.apache.jackrabbit.spi.commons.nodetype.NodeDefinitionImpl in project jackrabbit by apache.

the class NodeImpl method onRedefine.

protected void onRedefine(QNodeDefinition def) throws RepositoryException {
    NodeDefinitionImpl newDef = sessionContext.getNodeTypeManager().getNodeDefinition(def);
    // modify the state of 'this', i.e. the target node
    getOrCreateTransientItemState();
    // set new definition
    data.setDefinition(newDef);
}
Also used : NodeDefinitionImpl(org.apache.jackrabbit.spi.commons.nodetype.NodeDefinitionImpl)

Aggregations

NodeDefinitionImpl (org.apache.jackrabbit.spi.commons.nodetype.NodeDefinitionImpl)11 RepositoryException (javax.jcr.RepositoryException)7 NodeState (org.apache.jackrabbit.core.state.NodeState)7 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)6 NodeDefinition (javax.jcr.nodetype.NodeDefinition)5 ChildNodeEntry (org.apache.jackrabbit.core.state.ChildNodeEntry)5 PropertyDefinitionImpl (org.apache.jackrabbit.spi.commons.nodetype.PropertyDefinitionImpl)5 HashSet (java.util.HashSet)4 EffectiveNodeType (org.apache.jackrabbit.core.nodetype.EffectiveNodeType)4 NodeTypeConflictException (org.apache.jackrabbit.core.nodetype.NodeTypeConflictException)4 NodeTypeManagerImpl (org.apache.jackrabbit.core.nodetype.NodeTypeManagerImpl)4 NodeTypeRegistry (org.apache.jackrabbit.core.nodetype.NodeTypeRegistry)4 Name (org.apache.jackrabbit.spi.Name)4 Value (javax.jcr.Value)3 ValueFormatException (javax.jcr.ValueFormatException)3 PropertyDefinition (javax.jcr.nodetype.PropertyDefinition)3 PropertyId (org.apache.jackrabbit.core.id.PropertyId)3 NodeTypeImpl (org.apache.jackrabbit.core.nodetype.NodeTypeImpl)3 ItemStateException (org.apache.jackrabbit.core.state.ItemStateException)3 PropertyState (org.apache.jackrabbit.core.state.PropertyState)3