Search in sources :

Example 31 with QNodeDefinition

use of org.apache.jackrabbit.spi.QNodeDefinition in project jackrabbit by apache.

the class SessionImporter method startNode.

/**
 * {@inheritDoc}
 */
public void startNode(NodeInfo nodeInfo, List<PropInfo> propInfos, NamePathResolver resolver) throws RepositoryException {
    if (isClosed()) {
        // workspace-importer only: ignore if import has been aborted before.
        return;
    }
    checkSession();
    NodeState parent = parents.peek();
    if (parent == null) {
        // parent node was skipped, skip this child node also
        // push null onto stack for skipped node
        parents.push(null);
        log.debug("Skipping node '" + nodeInfo.getName() + "'.");
        return;
    }
    NodeEntry parentEntry = (NodeEntry) parent.getHierarchyEntry();
    NodeState nodeState = null;
    if (parentEntry.hasNodeEntry(nodeInfo.getName())) {
        try {
            // a valid child node with that name already exists
            NodeEntry entry = parentEntry.getNodeEntry(nodeInfo.getName(), Path.INDEX_DEFAULT);
            NodeState existing = entry.getNodeState();
            QNodeDefinition def = existing.getDefinition();
            if (!def.allowsSameNameSiblings()) {
                // existing doesn't allow same-name siblings, check for conflicts
                EffectiveNodeTypeProvider provider = session.getEffectiveNodeTypeProvider();
                Name[] ntNames = existing.getAllNodeTypeNames();
                EffectiveNodeType entExisting = provider.getEffectiveNodeType(ntNames);
                if (def.isProtected() && entExisting.includesNodeType(nodeInfo.getNodeTypeName())) {
                    // skip protected node
                    // push null onto stack for skipped node
                    parents.push(null);
                    log.debug("skipping protected node " + LogUtil.safeGetJCRPath(existing, session.getPathResolver()));
                    return;
                }
                if (def.isAutoCreated() && entExisting.includesNodeType(nodeInfo.getNodeTypeName())) {
                    // this node has already been auto-created, no need to create it
                    nodeState = existing;
                } else {
                    throw new ItemExistsException(LogUtil.safeGetJCRPath(existing, session.getPathResolver()));
                }
            }
        } catch (ItemNotFoundException e) {
        // 'existing' doesn't exist any more -> ignore
        }
    }
    if (nodeState == null) {
        // node does not exist -> create new one
        if (nodeInfo.getUUID() == null) {
            // no potential uuid conflict, add new node from given info
            nodeState = importNode(nodeInfo, parent);
        } else {
            // make sure the import does not define a uuid without having
            // a primaryType or mixin that makes the new node referenceable
            checkIncludesMixReferenceable(nodeInfo);
            // potential uuid conflict
            try {
                NodeId conflictingId = session.getIdFactory().createNodeId(nodeInfo.getUUID());
                NodeEntry conflicting = session.getHierarchyManager().getNodeEntry(conflictingId);
                // assert that the entry is available
                conflicting.getItemState();
                nodeState = resolveUUIDConflict(parent, conflicting, nodeInfo);
            } catch (ItemNotFoundException e) {
                // no conflict: create new with given uuid
                nodeState = importNode(nodeInfo, parent);
            }
        }
    }
    // node state may be 'null' if applicable def is protected
    if (nodeState != null) {
        // process properties
        for (PropInfo pi : propInfos) {
            importProperty(pi, nodeState, resolver);
        }
    }
    // push current nodeState onto stack of parents
    parents.push(nodeState);
}
Also used : EffectiveNodeTypeProvider(org.apache.jackrabbit.jcr2spi.nodetype.EffectiveNodeTypeProvider) EffectiveNodeType(org.apache.jackrabbit.jcr2spi.nodetype.EffectiveNodeType) NodeState(org.apache.jackrabbit.jcr2spi.state.NodeState) NodeEntry(org.apache.jackrabbit.jcr2spi.hierarchy.NodeEntry) ItemExistsException(javax.jcr.ItemExistsException) NodeId(org.apache.jackrabbit.spi.NodeId) QNodeDefinition(org.apache.jackrabbit.spi.QNodeDefinition) Name(org.apache.jackrabbit.spi.Name) ItemNotFoundException(javax.jcr.ItemNotFoundException)

Example 32 with QNodeDefinition

use of org.apache.jackrabbit.spi.QNodeDefinition 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)

Example 33 with QNodeDefinition

use of org.apache.jackrabbit.spi.QNodeDefinition in project jackrabbit by apache.

the class TestAll method testDefaultTypeNode.

/**
 * Test for the <code>defaultPrimaryType</code> child node attribute.
 */
public void testDefaultTypeNode() {
    QNodeDefinition def = getChildNode("childNodeType", "defaultTypeNode");
    assertEquals("defaultTypeNode defaultPrimaryType", FACTORY.create(Name.NS_NT_URI, "base"), def.getDefaultPrimaryType());
}
Also used : QNodeDefinition(org.apache.jackrabbit.spi.QNodeDefinition)

Example 34 with QNodeDefinition

use of org.apache.jackrabbit.spi.QNodeDefinition in project jackrabbit by apache.

the class ItemDefinitionProviderImpl method getQNodeDefinition.

public QNodeDefinition getQNodeDefinition(Name[] parentNodeTypeNames, Name nodeName, Name ntName, NodeId nodeId) throws RepositoryException {
    if (parentNodeTypeNames == null) {
        return getRootNodeDefinition();
    }
    QNodeDefinition definition;
    try {
        EffectiveNodeType ent = entProvider.getEffectiveNodeType(parentNodeTypeNames);
        EffectiveNodeType entTarget = getEffectiveNodeType(ntName);
        definition = getQNodeDefinition(ent, entTarget, nodeName);
    } catch (RepositoryException e) {
        log.debug("Cannot determine effective node type of {}: {}", nodeId, e);
        definition = getNodeDefinition(service, sessionInfo, nodeId);
    }
    return definition;
}
Also used : RepositoryException(javax.jcr.RepositoryException) QNodeDefinition(org.apache.jackrabbit.spi.QNodeDefinition)

Example 35 with QNodeDefinition

use of org.apache.jackrabbit.spi.QNodeDefinition in project jackrabbit by apache.

the class NodeTypeImpl method getChildNodeDefinitions.

/**
 * @see javax.jcr.nodetype.NodeType#getChildNodeDefinitions()
 */
public NodeDefinition[] getChildNodeDefinitions() {
    QNodeDefinition[] cnda = ent.getAllQNodeDefinitions();
    NodeDefinition[] nodeDefs = new NodeDefinition[cnda.length];
    for (int i = 0; i < cnda.length; i++) {
        nodeDefs[i] = ntMgr.getNodeDefinition(cnda[i]);
    }
    return nodeDefs;
}
Also used : NodeDefinition(javax.jcr.nodetype.NodeDefinition) QNodeDefinition(org.apache.jackrabbit.spi.QNodeDefinition) QNodeDefinition(org.apache.jackrabbit.spi.QNodeDefinition) ValueConstraint(org.apache.jackrabbit.spi.commons.nodetype.constraint.ValueConstraint)

Aggregations

QNodeDefinition (org.apache.jackrabbit.spi.QNodeDefinition)45 Name (org.apache.jackrabbit.spi.Name)25 QPropertyDefinition (org.apache.jackrabbit.spi.QPropertyDefinition)19 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)16 QItemDefinition (org.apache.jackrabbit.spi.QItemDefinition)9 QValueConstraint (org.apache.jackrabbit.spi.QValueConstraint)9 ArrayList (java.util.ArrayList)7 RepositoryException (javax.jcr.RepositoryException)7 EffectiveNodeType (org.apache.jackrabbit.core.nodetype.EffectiveNodeType)7 NodeId (org.apache.jackrabbit.core.id.NodeId)6 NodeState (org.apache.jackrabbit.core.state.NodeState)6 ItemExistsException (javax.jcr.ItemExistsException)5 ChildNodeEntry (org.apache.jackrabbit.core.state.ChildNodeEntry)5 HashSet (java.util.HashSet)4 ItemNotFoundException (javax.jcr.ItemNotFoundException)4 NoSuchNodeTypeException (javax.jcr.nodetype.NoSuchNodeTypeException)4 ItemStateException (org.apache.jackrabbit.core.state.ItemStateException)4 QValue (org.apache.jackrabbit.spi.QValue)4 ValueConstraint (org.apache.jackrabbit.spi.commons.nodetype.constraint.ValueConstraint)4 NodeDefinition (javax.jcr.nodetype.NodeDefinition)3