Search in sources :

Example 1 with AddNode

use of org.apache.jackrabbit.jcr2spi.operation.AddNode in project jackrabbit by apache.

the class AccessControlManagerImpl method addNode.

private NodeState addNode(SetTree treeOperation, NodeState parent, Name nodeName, String uuid, Name nodeTypeName) throws RepositoryException {
    Operation sp = treeOperation.addChildNode(parent, nodeName, nodeTypeName, uuid);
    itemStateMgr.execute(sp);
    return (NodeState) ((AddNode) sp).getAddedStates().get(0);
}
Also used : NodeState(org.apache.jackrabbit.jcr2spi.state.NodeState) Operation(org.apache.jackrabbit.jcr2spi.operation.Operation) AddNode(org.apache.jackrabbit.jcr2spi.operation.AddNode)

Example 2 with AddNode

use of org.apache.jackrabbit.jcr2spi.operation.AddNode in project jackrabbit by apache.

the class NodeImpl method createNode.

//---------------------------------------------< private implementation >---
/**
     * Create a new <code>NodeState</code> and subsequently retrieves the
     * corresponding <code>Node</code> object.
     *
     * @param nodeName     name of the new node
     * @param nodeTypeName name of the new node's node type or <code>null</code>
     *                     if it should be determined automatically
     * @return the newly added node
     * @throws ItemExistsException
     * @throws NoSuchNodeTypeException
     * @throws VersionException
     * @throws ConstraintViolationException
     * @throws LockException
     * @throws RepositoryException
     */
private synchronized Node createNode(Name nodeName, Name nodeTypeName) throws ItemExistsException, NoSuchNodeTypeException, VersionException, ConstraintViolationException, LockException, RepositoryException {
    QNodeDefinition definition = session.getItemDefinitionProvider().getQNodeDefinition(getNodeState().getAllNodeTypeNames(), nodeName, nodeTypeName);
    if (nodeTypeName == null) {
        // use default node type
        nodeTypeName = definition.getDefaultPrimaryType();
    }
    // validation check are performed by item state manager
    // NOTE: uuid is generated while creating new state.
    Operation an = AddNode.create(getNodeState(), nodeName, nodeTypeName, null);
    session.getSessionItemStateManager().execute(an);
    // finally retrieve the new node
    List<ItemState> addedStates = ((AddNode) an).getAddedStates();
    ItemState nState = addedStates.get(0);
    return (Node) getItemManager().getItem(nState.getHierarchyEntry());
}
Also used : ItemState(org.apache.jackrabbit.jcr2spi.state.ItemState) AddNode(org.apache.jackrabbit.jcr2spi.operation.AddNode) Node(javax.jcr.Node) Operation(org.apache.jackrabbit.jcr2spi.operation.Operation) QNodeDefinition(org.apache.jackrabbit.spi.QNodeDefinition) AddNode(org.apache.jackrabbit.jcr2spi.operation.AddNode)

Example 3 with AddNode

use of org.apache.jackrabbit.jcr2spi.operation.AddNode in project jackrabbit by apache.

the class SessionImporter method importNode.

/**
     *
     * @param nodeInfo
     * @param parent
     * @return
     * @throws ConstraintViolationException
     * @throws ItemNotFoundException
     * @throws RepositoryException
     */
private NodeState importNode(NodeInfo nodeInfo, NodeState parent) throws ConstraintViolationException, ItemNotFoundException, RepositoryException {
    Name[] parentNtNames = parent.getAllNodeTypeNames();
    if (parent.hasPropertyName(nodeInfo.getName())) {
        /**
             * a property with the same name already exists; if this property
             * has been imported as well (e.g. through document view import
             * where an element can have the same name as one of the attributes
             * of its parent element) we have to rename the conflicting property;
             *
             * see http://issues.apache.org/jira/browse/JCR-61
             */
        PropertyState conflicting = parent.getPropertyState(nodeInfo.getName());
        if (conflicting.getStatus() == Status.NEW) {
            // assume this property has been imported as well;
            // rename conflicting property
            // TODO: use better reversible escaping scheme to create unique name
            Name newName = session.getNameFactory().create(nodeInfo.getName().getNamespaceURI(), nodeInfo.getName().getLocalName() + "_");
            if (parent.hasPropertyName(newName)) {
                newName = session.getNameFactory().create(newName.getNamespaceURI(), newName.getLocalName() + "_");
            }
            // since name changes, need to find new applicable definition
            QPropertyDefinition propDef;
            if (conflicting.getValues().length == 1) {
                // could be single- or multi-valued (n == 1)
                try {
                    // try single-valued
                    propDef = session.getItemDefinitionProvider().getQPropertyDefinition(parentNtNames, newName, conflicting.getType(), false);
                } catch (ConstraintViolationException cve) {
                    // try multi-valued
                    propDef = session.getItemDefinitionProvider().getQPropertyDefinition(parentNtNames, newName, conflicting.getType(), true);
                }
            } else {
                // can only be multi-valued (n == 0 || n > 1)
                propDef = session.getItemDefinitionProvider().getQPropertyDefinition(parentNtNames, newName, conflicting.getType(), true);
            }
            Operation ap = AddProperty.create(parent, newName, conflicting.getType(), propDef, conflicting.getValues());
            stateMgr.execute(ap);
            Operation rm = Remove.create(conflicting);
            stateMgr.execute(rm);
        }
    }
    // do create new nodeState
    QNodeDefinition def = session.getItemDefinitionProvider().getQNodeDefinition(parentNtNames, nodeInfo.getName(), nodeInfo.getNodeTypeName());
    if (def.isProtected()) {
        log.debug("Skipping protected nodeState (" + nodeInfo.getName() + ")");
        return null;
    } else {
        Name ntName = nodeInfo.getNodeTypeName();
        if (ntName == null) {
            // use default node type
            ntName = def.getDefaultPrimaryType();
        }
        Operation an = AddNode.create(parent, nodeInfo.getName(), ntName, nodeInfo.getUUID());
        stateMgr.execute(an);
        // retrieve id of state that has been created during execution of AddNode
        NodeState childState = (NodeState) ((AddNode) an).getAddedStates().get(0);
        // and set mixin types
        Name[] mixinNames = nodeInfo.getMixinNames();
        if (mixinNames != null && mixinNames.length > 0) {
            Operation sm = SetMixin.create(childState, nodeInfo.getMixinNames());
            stateMgr.execute(sm);
        }
        return childState;
    }
}
Also used : NodeState(org.apache.jackrabbit.jcr2spi.state.NodeState) QPropertyDefinition(org.apache.jackrabbit.spi.QPropertyDefinition) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) Operation(org.apache.jackrabbit.jcr2spi.operation.Operation) QNodeDefinition(org.apache.jackrabbit.spi.QNodeDefinition) AddNode(org.apache.jackrabbit.jcr2spi.operation.AddNode) Name(org.apache.jackrabbit.spi.Name) PropertyState(org.apache.jackrabbit.jcr2spi.state.PropertyState)

Aggregations

AddNode (org.apache.jackrabbit.jcr2spi.operation.AddNode)3 Operation (org.apache.jackrabbit.jcr2spi.operation.Operation)3 NodeState (org.apache.jackrabbit.jcr2spi.state.NodeState)2 QNodeDefinition (org.apache.jackrabbit.spi.QNodeDefinition)2 Node (javax.jcr.Node)1 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)1 ItemState (org.apache.jackrabbit.jcr2spi.state.ItemState)1 PropertyState (org.apache.jackrabbit.jcr2spi.state.PropertyState)1 Name (org.apache.jackrabbit.spi.Name)1 QPropertyDefinition (org.apache.jackrabbit.spi.QPropertyDefinition)1