Search in sources :

Example 31 with NodeId

use of org.apache.jackrabbit.core.id.NodeId in project jackrabbit by apache.

the class HierarchyManagerImpl method resolvePath.

//-------------------------------------------------------< implementation >
/**
     * Internal implementation that iteratively resolves a path into an item.
     *
     * @param elements path elements
     * @param next index of next item in <code>elements</code> to inspect
     * @param id id of item at path <code>elements[0]</code>..<code>elements[next - 1]</code>
     * @param typesAllowed one of <code>RETURN_ANY</code>, <code>RETURN_NODE</code>
     *                     or <code>RETURN_PROPERTY</code>
     * @return id or <code>null</code>
     * @throws ItemStateException if an intermediate item state is not found
     * @throws MalformedPathException if building an intermediate path fails
     */
protected ItemId resolvePath(Path.Element[] elements, int next, ItemId id, int typesAllowed) throws ItemStateException, MalformedPathException {
    PathBuilder builder = new PathBuilder();
    for (int i = 0; i < next; i++) {
        builder.addLast(elements[i]);
    }
    for (int i = next; i < elements.length; i++) {
        Path.Element elem = elements[i];
        NodeId parentId = (NodeId) id;
        id = null;
        Name name = elem.getName();
        int index = elem.getIndex();
        if (index == 0) {
            index = 1;
        }
        int typeExpected = typesAllowed;
        if (i < elements.length - 1) {
            // intermediate items must always be nodes
            typeExpected = RETURN_NODE;
        }
        NodeState parentState = (NodeState) getItemState(parentId);
        if ((typeExpected & RETURN_NODE) != 0) {
            ChildNodeEntry nodeEntry = getChildNodeEntry(parentState, name, index);
            if (nodeEntry != null) {
                id = nodeEntry.getId();
            }
        }
        if (id == null && (typeExpected & RETURN_PROPERTY) != 0) {
            if (parentState.hasPropertyName(name) && (index <= 1)) {
                // property
                id = new PropertyId(parentState.getNodeId(), name);
            }
        }
        if (id == null) {
            break;
        }
        builder.addLast(elements[i]);
        pathResolved(id, builder);
    }
    return id;
}
Also used : Path(org.apache.jackrabbit.spi.Path) PathBuilder(org.apache.jackrabbit.spi.commons.name.PathBuilder) NodeState(org.apache.jackrabbit.core.state.NodeState) ChildNodeEntry(org.apache.jackrabbit.core.state.ChildNodeEntry) NodeId(org.apache.jackrabbit.core.id.NodeId) Name(org.apache.jackrabbit.spi.Name) PropertyId(org.apache.jackrabbit.core.id.PropertyId)

Example 32 with NodeId

use of org.apache.jackrabbit.core.id.NodeId in project jackrabbit by apache.

the class HierarchyManagerImpl method isShareAncestor.

/**
     * {@inheritDoc}
     */
public boolean isShareAncestor(NodeId ancestor, NodeId descendant) throws ItemNotFoundException, RepositoryException {
    if (ancestor.equals(descendant)) {
        // can't be ancestor of self
        return false;
    }
    try {
        ItemState state = getItemState(descendant);
        Set<NodeId> parentIds = getParentIds(state, false);
        while (parentIds.size() > 0) {
            if (parentIds.contains(ancestor)) {
                return true;
            }
            Set<NodeId> grandparentIds = new LinkedHashSet<NodeId>();
            for (NodeId parentId : parentIds) {
                grandparentIds.addAll(getParentIds(getItemState(parentId), false));
            }
            parentIds = grandparentIds;
        }
        // not an ancestor
        return false;
    } catch (NoSuchItemStateException nsise) {
        String msg = "failed to determine degree of relationship of " + ancestor + " and " + descendant;
        log.debug(msg);
        throw new ItemNotFoundException(msg, nsise);
    } catch (ItemStateException ise) {
        String msg = "failed to determine degree of relationship of " + ancestor + " and " + descendant;
        log.debug(msg);
        throw new RepositoryException(msg, ise);
    }
}
Also used : LinkedHashSet(java.util.LinkedHashSet) NoSuchItemStateException(org.apache.jackrabbit.core.state.NoSuchItemStateException) ItemState(org.apache.jackrabbit.core.state.ItemState) NodeId(org.apache.jackrabbit.core.id.NodeId) RepositoryException(javax.jcr.RepositoryException) ItemNotFoundException(javax.jcr.ItemNotFoundException) NoSuchItemStateException(org.apache.jackrabbit.core.state.NoSuchItemStateException) InvalidItemStateException(javax.jcr.InvalidItemStateException) ItemStateException(org.apache.jackrabbit.core.state.ItemStateException)

Example 33 with NodeId

use of org.apache.jackrabbit.core.id.NodeId in project jackrabbit by apache.

the class HierarchyManagerImpl method getParentIds.

/**
     * Return all parents of a node. A shareable node has possibly more than
     * one parent.
     *
     * @param state item state
     * @param useOverlayed whether to use overlayed state for shareable nodes
     * @return set of parent <code>NodeId</code>s. If state has no parent,
     *         array has length <code>0</code>.
     */
protected Set<NodeId> getParentIds(ItemState state, boolean useOverlayed) {
    if (state.isNode()) {
        // if this is a node, quickly check whether it is shareable and
        // whether it contains more than one parent
        NodeState ns = (NodeState) state;
        if (ns.isShareable() && useOverlayed && ns.hasOverlayedState()) {
            ns = (NodeState) ns.getOverlayedState();
        }
        Set<NodeId> s = ns.getSharedSet();
        if (s.size() > 1) {
            return s;
        }
    }
    NodeId parentId = getParentId(state);
    if (parentId != null) {
        LinkedHashSet<NodeId> s = new LinkedHashSet<NodeId>();
        s.add(parentId);
        return s;
    }
    return Collections.emptySet();
}
Also used : LinkedHashSet(java.util.LinkedHashSet) NodeState(org.apache.jackrabbit.core.state.NodeState) NodeId(org.apache.jackrabbit.core.id.NodeId)

Example 34 with NodeId

use of org.apache.jackrabbit.core.id.NodeId in project jackrabbit by apache.

the class HierarchyManagerImpl method getDepth.

/**
     * {@inheritDoc}
     */
public int getDepth(ItemId id) throws ItemNotFoundException, RepositoryException {
    // shortcut
    if (id.equals(rootNodeId)) {
        return 0;
    }
    try {
        ItemState state = getItemState(id);
        NodeId parentId = getParentId(state);
        int depth = 0;
        while (parentId != null) {
            depth++;
            state = getItemState(parentId);
            parentId = getParentId(state);
        }
        return depth;
    } catch (NoSuchItemStateException nsise) {
        String msg = "failed to determine depth of " + id;
        log.debug(msg);
        throw new ItemNotFoundException(msg, nsise);
    } catch (ItemStateException ise) {
        String msg = "failed to determine depth of " + id;
        log.debug(msg);
        throw new RepositoryException(msg, ise);
    }
}
Also used : NoSuchItemStateException(org.apache.jackrabbit.core.state.NoSuchItemStateException) ItemState(org.apache.jackrabbit.core.state.ItemState) NodeId(org.apache.jackrabbit.core.id.NodeId) RepositoryException(javax.jcr.RepositoryException) ItemNotFoundException(javax.jcr.ItemNotFoundException) NoSuchItemStateException(org.apache.jackrabbit.core.state.NoSuchItemStateException) InvalidItemStateException(javax.jcr.InvalidItemStateException) ItemStateException(org.apache.jackrabbit.core.state.ItemStateException)

Example 35 with NodeId

use of org.apache.jackrabbit.core.id.NodeId in project jackrabbit by apache.

the class HierarchyManagerImpl method getRelativeDepth.

/**
     * {@inheritDoc}
     */
public int getRelativeDepth(NodeId ancestorId, ItemId descendantId) throws ItemNotFoundException, RepositoryException {
    if (ancestorId.equals(descendantId)) {
        return 0;
    }
    int depth = 1;
    try {
        ItemState state = getItemState(descendantId);
        NodeId parentId = getParentId(state);
        while (parentId != null) {
            if (parentId.equals(ancestorId)) {
                return depth;
            }
            depth++;
            state = getItemState(parentId);
            parentId = getParentId(state);
        }
        // not an ancestor
        return -1;
    } catch (NoSuchItemStateException nsise) {
        String msg = "failed to determine depth of " + descendantId + " relative to " + ancestorId;
        log.debug(msg);
        throw new ItemNotFoundException(msg, nsise);
    } catch (ItemStateException ise) {
        String msg = "failed to determine depth of " + descendantId + " relative to " + ancestorId;
        log.debug(msg);
        throw new RepositoryException(msg, ise);
    }
}
Also used : NoSuchItemStateException(org.apache.jackrabbit.core.state.NoSuchItemStateException) ItemState(org.apache.jackrabbit.core.state.ItemState) NodeId(org.apache.jackrabbit.core.id.NodeId) RepositoryException(javax.jcr.RepositoryException) ItemNotFoundException(javax.jcr.ItemNotFoundException) NoSuchItemStateException(org.apache.jackrabbit.core.state.NoSuchItemStateException) InvalidItemStateException(javax.jcr.InvalidItemStateException) ItemStateException(org.apache.jackrabbit.core.state.ItemStateException)

Aggregations

NodeId (org.apache.jackrabbit.core.id.NodeId)203 RepositoryException (javax.jcr.RepositoryException)68 NodeState (org.apache.jackrabbit.core.state.NodeState)52 ItemStateException (org.apache.jackrabbit.core.state.ItemStateException)48 Name (org.apache.jackrabbit.spi.Name)37 NoSuchItemStateException (org.apache.jackrabbit.core.state.NoSuchItemStateException)31 ChildNodeEntry (org.apache.jackrabbit.core.state.ChildNodeEntry)23 Path (org.apache.jackrabbit.spi.Path)23 ItemNotFoundException (javax.jcr.ItemNotFoundException)22 NodeImpl (org.apache.jackrabbit.core.NodeImpl)20 InternalValue (org.apache.jackrabbit.core.value.InternalValue)20 ArrayList (java.util.ArrayList)19 PropertyId (org.apache.jackrabbit.core.id.PropertyId)16 HashSet (java.util.HashSet)15 InvalidItemStateException (javax.jcr.InvalidItemStateException)14 NodePropBundle (org.apache.jackrabbit.core.persistence.util.NodePropBundle)14 PropertyState (org.apache.jackrabbit.core.state.PropertyState)14 Session (javax.jcr.Session)13 HashMap (java.util.HashMap)12 ItemExistsException (javax.jcr.ItemExistsException)12