Search in sources :

Example 56 with ItemExistsException

use of javax.jcr.ItemExistsException 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 57 with ItemExistsException

use of javax.jcr.ItemExistsException in project jackrabbit by apache.

the class SessionImporter method importProperty.

/**
     *
     * @param pi
     * @param parentState
     * @param resolver
     * @throws RepositoryException
     * @throws ConstraintViolationException
     */
private void importProperty(PropInfo pi, NodeState parentState, NamePathResolver resolver) throws RepositoryException, ConstraintViolationException {
    Name propName = pi.getName();
    TextValue[] tva = pi.getValues();
    int infoType = pi.getType();
    PropertyState propState = null;
    QPropertyDefinition def = null;
    NodeEntry parentEntry = (NodeEntry) parentState.getHierarchyEntry();
    PropertyEntry pEntry = parentEntry.getPropertyEntry(propName);
    if (pEntry != null) {
        // a property with that name already exists...
        try {
            PropertyState existing = pEntry.getPropertyState();
            def = existing.getDefinition();
            if (def.isProtected()) {
                // skip protected property
                log.debug("skipping protected property " + LogUtil.safeGetJCRPath(existing, session.getPathResolver()));
                return;
            }
            if (def.isAutoCreated() && (existing.getType() == infoType || infoType == PropertyType.UNDEFINED) && def.isMultiple() == existing.isMultiValued()) {
                // this property has already been auto-created, no need to create it
                propState = existing;
            } else {
                throw new ItemExistsException(LogUtil.safeGetJCRPath(existing, session.getPathResolver()));
            }
        } catch (ItemNotFoundException e) {
        // property doesn't exist any more
        // -> ignore
        }
    }
    Name[] parentNtNames = parentState.getAllNodeTypeNames();
    if (def == null) {
        // there's no property with that name, find applicable definition
        if (tva.length == 1) {
            // could be single- or multi-valued (n == 1)
            def = session.getItemDefinitionProvider().getQPropertyDefinition(parentNtNames, propName, infoType);
        } else {
            // can only be multi-valued (n == 0 || n > 1)
            def = session.getItemDefinitionProvider().getQPropertyDefinition(parentNtNames, propName, infoType, true);
        }
        if (def.isProtected()) {
            // skip protected property
            log.debug("skipping protected property " + propName);
            return;
        }
    }
    // retrieve the target property type needed for creation of QValue(s)
    // including an eventual conversion. the targetType is then needed for
    // setting/updating the type of the property-state.
    int targetType = def.getRequiredType();
    if (targetType == PropertyType.UNDEFINED) {
        if (infoType == PropertyType.UNDEFINED) {
            targetType = PropertyType.STRING;
        } else {
            targetType = infoType;
        }
    }
    QValue[] values = getPropertyValues(pi, targetType, def.isMultiple(), resolver);
    if (propState == null) {
        // create new property
        Operation ap = AddProperty.create(parentState, propName, targetType, def, values);
        stateMgr.execute(ap);
        propState = parentEntry.getPropertyEntry(propName).getPropertyState();
    } else {
        // modify value of existing property
        Operation sp = SetPropertyValue.create(propState, values, targetType);
        stateMgr.execute(sp);
    }
    // store reference for later resolution
    if (propState.getType() == PropertyType.REFERENCE) {
        refTracker.processedReference(propState);
    }
}
Also used : QValue(org.apache.jackrabbit.spi.QValue) Operation(org.apache.jackrabbit.jcr2spi.operation.Operation) Name(org.apache.jackrabbit.spi.Name) PropertyState(org.apache.jackrabbit.jcr2spi.state.PropertyState) NodeEntry(org.apache.jackrabbit.jcr2spi.hierarchy.NodeEntry) PropertyEntry(org.apache.jackrabbit.jcr2spi.hierarchy.PropertyEntry) ItemExistsException(javax.jcr.ItemExistsException) QPropertyDefinition(org.apache.jackrabbit.spi.QPropertyDefinition) ItemNotFoundException(javax.jcr.ItemNotFoundException)

Example 58 with ItemExistsException

use of javax.jcr.ItemExistsException in project jackrabbit by apache.

the class WorkspaceItemStateFactory method createNodeState.

/**
     * Create the node state with the information from <code>info</code>.
     *
     * @param info the <code>NodeInfo</code> to use to create the <code>NodeState</code>.
     * @param entry  the hierarchy entry for of this state
     * @return the new <code>NodeState</code>.
     * @throws ItemNotFoundException
     * @throws RepositoryException
     */
private NodeState createNodeState(NodeInfo info, NodeEntry entry) throws ItemNotFoundException, RepositoryException {
    // Make sure the entry has the correct ItemId
    // this may not be the case, if the hierarchy has not been completely
    // resolved yet -> if uniqueID is present, set it on this entry or on
    // the appropriate parent entry
    String uniqueID = info.getId().getUniqueID();
    Path path = info.getId().getPath();
    if (path == null) {
        entry.setUniqueID(uniqueID);
    } else if (uniqueID != null) {
        // uniqueID that applies to a parent NodeEntry -> get parentEntry
        NodeEntry parent = getAncestor(entry, path.getLength());
        parent.setUniqueID(uniqueID);
    }
    int previousStatus = entry.getStatus();
    if (Status.isTransient(previousStatus) || Status.isStale(previousStatus)) {
        log.debug("Node has pending changes; omit resetting the state.");
        return entry.getNodeState();
    }
    // update NodeEntry from the information present in the NodeInfo (prop entries)
    List<Name> propNames = new ArrayList<Name>();
    for (Iterator<PropertyId> it = info.getPropertyIds(); it.hasNext(); ) {
        PropertyId pId = it.next();
        Name propertyName = pId.getName();
        propNames.add(propertyName);
    }
    try {
        entry.setPropertyEntries(propNames);
    } catch (ItemExistsException e) {
        // should not get here
        log.error("Internal error", e);
    }
    // unless the child-info are omitted by the SPI impl -> make sure
    // the child entries the node entry are initialized or updated.
    Iterator<ChildInfo> childInfos = info.getChildInfos();
    if (childInfos != null) {
        entry.setNodeEntries(childInfos);
    }
    // now build or update the nodestate itself
    NodeState tmp = new NodeState(entry, info, this, definitionProvider);
    entry.setItemState(tmp);
    NodeState nState = entry.getNodeState();
    if (previousStatus == Status._UNDEFINED_) {
        // tmp state was used as resolution for the given entry i.e. the
        // entry was not available before. otherwise the 2 states were
        // merged. see HierarchyEntryImpl#setItemState
        notifyCreated(nState);
    } else {
        notifyUpdated(nState, previousStatus);
    }
    return nState;
}
Also used : Path(org.apache.jackrabbit.spi.Path) ArrayList(java.util.ArrayList) ChildInfo(org.apache.jackrabbit.spi.ChildInfo) Name(org.apache.jackrabbit.spi.Name) PropertyId(org.apache.jackrabbit.spi.PropertyId) NodeEntry(org.apache.jackrabbit.jcr2spi.hierarchy.NodeEntry) ItemExistsException(javax.jcr.ItemExistsException)

Example 59 with ItemExistsException

use of javax.jcr.ItemExistsException in project jackrabbit-oak by apache.

the class WorkspaceDelegate method copy.

/**
     * Copy a node
     * @param srcPath  oak path to the source node to copy
     * @param destPath  oak path to the destination
     * @throws RepositoryException
     */
public void copy(String srcPath, String destPath) throws RepositoryException {
    SessionDelegate sessionDelegate = context.getSessionDelegate();
    AccessManager accessManager = context.getAccessManager();
    Root root = sessionDelegate.getContentSession().getLatestRoot();
    // check destination
    Tree dest = root.getTree(destPath);
    if (dest.exists()) {
        throw new ItemExistsException(destPath);
    }
    // check parent of destination
    Tree destParent = dest.getParent();
    if (!destParent.exists()) {
        throw new PathNotFoundException(destParent.getPath());
    }
    // check source exists
    Tree src = root.getTree(srcPath);
    if (src.isRoot()) {
        throw new RepositoryException("Cannot copy the root node");
    }
    if (!src.exists()) {
        throw new PathNotFoundException(srcPath);
    }
    accessManager.checkPermissions(destPath, Permissions.getString(Permissions.NODE_TYPE_MANAGEMENT));
    String userId = sessionDelegate.getAuthInfo().getUserID();
    new WorkspaceCopy(src, destParent, Text.getName(destPath)).perform(root, userId);
    sessionDelegate.refresh(true);
}
Also used : AccessManager(org.apache.jackrabbit.oak.jcr.security.AccessManager) Root(org.apache.jackrabbit.oak.api.Root) ItemExistsException(javax.jcr.ItemExistsException) Tree(org.apache.jackrabbit.oak.api.Tree) RepositoryException(javax.jcr.RepositoryException) PathNotFoundException(javax.jcr.PathNotFoundException)

Example 60 with ItemExistsException

use of javax.jcr.ItemExistsException in project jackrabbit-oak by apache.

the class SessionDelegate method move.

/**
     * Move a node
     *
     * @param srcPath  oak path to the source node to copy
     * @param destPath  oak path to the destination
     * @param transientOp  whether or not to perform the move in transient space
     * @throws RepositoryException
     */
public void move(String srcPath, String destPath, boolean transientOp) throws RepositoryException {
    Root moveRoot = transientOp ? root : contentSession.getLatestRoot();
    // check destination
    Tree dest = moveRoot.getTree(destPath);
    if (dest.exists()) {
        throw new ItemExistsException(destPath);
    }
    // check parent of destination
    String destParentPath = PathUtils.getParentPath(destPath);
    Tree destParent = moveRoot.getTree(destParentPath);
    if (!destParent.exists()) {
        throw new PathNotFoundException(PathUtils.getParentPath(destPath));
    }
    // check source exists
    Tree src = moveRoot.getTree(srcPath);
    if (!src.exists()) {
        throw new PathNotFoundException(srcPath);
    }
    try {
        if (!moveRoot.move(srcPath, destPath)) {
            throw new RepositoryException("Cannot move node at " + srcPath + " to " + destPath);
        }
        if (!transientOp) {
            sessionCounters.saveTime = clock.getTime();
            sessionCounters.saveCount++;
            commit(moveRoot);
            refresh(true);
        }
    } catch (CommitFailedException e) {
        throw newRepositoryException(e);
    }
}
Also used : Root(org.apache.jackrabbit.oak.api.Root) PathUtils.denotesRoot(org.apache.jackrabbit.oak.commons.PathUtils.denotesRoot) ItemExistsException(javax.jcr.ItemExistsException) Tree(org.apache.jackrabbit.oak.api.Tree) RepositoryException(javax.jcr.RepositoryException) PathNotFoundException(javax.jcr.PathNotFoundException) CommitFailedException(org.apache.jackrabbit.oak.api.CommitFailedException)

Aggregations

ItemExistsException (javax.jcr.ItemExistsException)63 Node (javax.jcr.Node)25 RepositoryException (javax.jcr.RepositoryException)25 NotExecutableException (org.apache.jackrabbit.test.NotExecutableException)16 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)15 NodeDefinition (javax.jcr.nodetype.NodeDefinition)14 ItemNotFoundException (javax.jcr.ItemNotFoundException)13 Version (javax.jcr.version.Version)13 NodeId (org.apache.jackrabbit.core.id.NodeId)12 Name (org.apache.jackrabbit.spi.Name)12 NodeState (org.apache.jackrabbit.core.state.NodeState)10 ChildNodeEntry (org.apache.jackrabbit.core.state.ChildNodeEntry)9 Path (org.apache.jackrabbit.spi.Path)8 PathNotFoundException (javax.jcr.PathNotFoundException)6 EffectiveNodeType (org.apache.jackrabbit.core.nodetype.EffectiveNodeType)6 QPropertyDefinition (org.apache.jackrabbit.spi.QPropertyDefinition)6 NodeImpl (org.apache.jackrabbit.core.NodeImpl)5 QNodeDefinition (org.apache.jackrabbit.spi.QNodeDefinition)5 ArrayList (java.util.ArrayList)4 AccessDeniedException (javax.jcr.AccessDeniedException)4