Search in sources :

Example 11 with NodeDelegate

use of org.apache.jackrabbit.oak.jcr.delegate.NodeDelegate in project jackrabbit-oak by apache.

the class NodeImpl method addNode.

@Override
@Nonnull
public Node addNode(final String relPath, String primaryNodeTypeName) throws RepositoryException {
    final String oakPath = getOakPathOrThrowNotFound(relPath);
    final String oakTypeName;
    if (primaryNodeTypeName != null) {
        oakTypeName = getOakName(primaryNodeTypeName);
    } else {
        oakTypeName = null;
    }
    checkIndexOnName(relPath);
    return perform(new ItemWriteOperation<Node>("addNode") {

        @Nonnull
        @Override
        public Node perform() throws RepositoryException {
            String oakName = PathUtils.getName(oakPath);
            String parentPath = PathUtils.getParentPath(oakPath);
            NodeDelegate parent = dlg.getChild(parentPath);
            if (parent == null) {
                // is it a property?
                String grandParentPath = PathUtils.getParentPath(parentPath);
                NodeDelegate grandParent = dlg.getChild(grandParentPath);
                if (grandParent != null) {
                    String propName = PathUtils.getName(parentPath);
                    if (grandParent.getPropertyOrNull(propName) != null) {
                        throw new ConstraintViolationException("Can't add new node to property.");
                    }
                }
                throw new PathNotFoundException(relPath);
            }
            if (parent.getChild(oakName) != null) {
                throw new ItemExistsException(relPath);
            }
            // modification of that property in the PermissionValidator
            if (oakTypeName != null) {
                PropertyState prop = PropertyStates.createProperty(JCR_PRIMARYTYPE, oakTypeName, NAME);
                sessionContext.getAccessManager().checkPermissions(parent.getTree(), prop, Permissions.NODE_TYPE_MANAGEMENT);
            }
            NodeDelegate added = parent.addChild(oakName, oakTypeName);
            if (added == null) {
                throw new ItemExistsException(format("Node [%s/%s] exists", getNodePath(), oakName));
            }
            return createNode(added, sessionContext);
        }

        @Override
        public String toString() {
            return format("Adding node [%s/%s]", dlg.getPath(), relPath);
        }
    });
}
Also used : Nonnull(javax.annotation.Nonnull) ItemExistsException(javax.jcr.ItemExistsException) JackrabbitNode(org.apache.jackrabbit.api.JackrabbitNode) Node(javax.jcr.Node) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) RepositoryException(javax.jcr.RepositoryException) PathNotFoundException(javax.jcr.PathNotFoundException) NodeDelegate(org.apache.jackrabbit.oak.jcr.delegate.NodeDelegate) PropertyState(org.apache.jackrabbit.oak.api.PropertyState) Nonnull(javax.annotation.Nonnull)

Example 12 with NodeDelegate

use of org.apache.jackrabbit.oak.jcr.delegate.NodeDelegate in project jackrabbit-oak by apache.

the class NodeImpl method createNode.

@Nonnull
public static NodeImpl<? extends NodeDelegate> createNode(@Nonnull NodeDelegate delegate, @Nonnull SessionContext context) throws RepositoryException {
    PropertyDelegate pd = delegate.getPropertyOrNull(JCR_PRIMARYTYPE);
    String type = pd != null ? pd.getString() : null;
    if (JcrConstants.NT_VERSION.equals(type)) {
        VersionManagerDelegate vmd = VersionManagerDelegate.create(context.getSessionDelegate());
        return new VersionImpl(vmd.createVersion(delegate), context);
    } else if (JcrConstants.NT_VERSIONHISTORY.equals(type)) {
        VersionManagerDelegate vmd = VersionManagerDelegate.create(context.getSessionDelegate());
        return new VersionHistoryImpl(vmd.createVersionHistory(delegate), context);
    } else {
        return new NodeImpl<NodeDelegate>(delegate, context);
    }
}
Also used : VersionHistoryImpl(org.apache.jackrabbit.oak.jcr.version.VersionHistoryImpl) VersionManagerDelegate(org.apache.jackrabbit.oak.jcr.delegate.VersionManagerDelegate) NodeDelegate(org.apache.jackrabbit.oak.jcr.delegate.NodeDelegate) PropertyDelegate(org.apache.jackrabbit.oak.jcr.delegate.PropertyDelegate) VersionImpl(org.apache.jackrabbit.oak.jcr.version.VersionImpl) Nonnull(javax.annotation.Nonnull)

Example 13 with NodeDelegate

use of org.apache.jackrabbit.oak.jcr.delegate.NodeDelegate in project jackrabbit-oak by apache.

the class VersionManagerImpl method getExisting.

/**
 * Returns referenceable nodes outside of the versionable sub-graphs
 * identified by <code>versionablePaths</code>, which are also present
 * in the versionable state captured by <code>version</code>.
 *
 * @param version the version.
 * @param versionablePaths identifies the starting points of the versionable
 *                         sub-graphs.
 * @return existing nodes in this workspace.
 */
private List<NodeDelegate> getExisting(@Nonnull Version version, @Nonnull Set<String> versionablePaths) throws RepositoryException {
    // collect uuids
    final List<String> uuids = new ArrayList<String>();
    version.getFrozenNode().accept(new TraversingItemVisitor.Default() {

        @Override
        protected void entering(Node node, int level) throws RepositoryException {
            if (node.isNodeType(NodeType.NT_FROZEN_NODE)) {
                String id = node.getProperty(Property.JCR_FROZEN_UUID).getString();
                if (id.length() > 0) {
                    uuids.add(id);
                }
            } else if (node.isNodeType(NodeType.NT_VERSIONED_CHILD)) {
                Node history = node.getProperty(Property.JCR_CHILD_VERSION_HISTORY).getNode();
                uuids.add(history.getProperty(Property.JCR_VERSIONABLE_UUID).getString());
            // TODO: further traverse versioned children with some selector (date?)
            }
        }
    });
    SessionDelegate delegate = sessionContext.getSessionDelegate();
    if (uuids.isEmpty()) {
        return Collections.emptyList();
    }
    List<NodeDelegate> existing = new ArrayList<NodeDelegate>();
    for (String uuid : uuids) {
        NodeDelegate node = delegate.getNodeByIdentifier(uuid);
        if (node != null) {
            boolean inSubGraph = false;
            for (String versionablePath : versionablePaths) {
                if (node.getPath().startsWith(versionablePath)) {
                    inSubGraph = true;
                    break;
                }
            }
            if (!inSubGraph) {
                existing.add(node);
            }
        }
    }
    return existing;
}
Also used : Node(javax.jcr.Node) ArrayList(java.util.ArrayList) RepositoryException(javax.jcr.RepositoryException) TraversingItemVisitor(javax.jcr.util.TraversingItemVisitor) NodeDelegate(org.apache.jackrabbit.oak.jcr.delegate.NodeDelegate) SessionDelegate(org.apache.jackrabbit.oak.jcr.delegate.SessionDelegate)

Example 14 with NodeDelegate

use of org.apache.jackrabbit.oak.jcr.delegate.NodeDelegate in project jackrabbit-oak by apache.

the class VersionManagerImpl method ensureParentExists.

/**
 * Returns the parent for the given <code>absPath</code> or throws a
 * {@link PathNotFoundException} if it doesn't exist.
 *
 * @param sessionDelegate session delegate.
 * @param absPath an absolute path
 * @return the parent for the given <code>absPath</code>.
 * @throws PathNotFoundException if the node does not exist.
 */
@Nonnull
private NodeDelegate ensureParentExists(@Nonnull SessionDelegate sessionDelegate, @Nonnull String absPath) throws PathNotFoundException {
    String oakParentPath = getOakPathOrThrowNotFound(PathUtils.getParentPath(checkNotNull(absPath)));
    NodeDelegate parent = checkNotNull(sessionDelegate).getNode(oakParentPath);
    if (parent == null) {
        throw new PathNotFoundException(PathUtils.getParentPath(absPath));
    }
    return parent;
}
Also used : PathNotFoundException(javax.jcr.PathNotFoundException) NodeDelegate(org.apache.jackrabbit.oak.jcr.delegate.NodeDelegate) Nonnull(javax.annotation.Nonnull)

Aggregations

NodeDelegate (org.apache.jackrabbit.oak.jcr.delegate.NodeDelegate)14 PathNotFoundException (javax.jcr.PathNotFoundException)7 RepositoryException (javax.jcr.RepositoryException)6 Nonnull (javax.annotation.Nonnull)5 SessionDelegate (org.apache.jackrabbit.oak.jcr.delegate.SessionDelegate)5 Node (javax.jcr.Node)4 ArrayList (java.util.ArrayList)3 ItemExistsException (javax.jcr.ItemExistsException)3 VersionException (javax.jcr.version.VersionException)3 PropertyDelegate (org.apache.jackrabbit.oak.jcr.delegate.PropertyDelegate)3 List (java.util.List)2 CommitFailedException (org.apache.jackrabbit.oak.api.CommitFailedException)2 Tree (org.apache.jackrabbit.oak.api.Tree)2 ItemDelegate (org.apache.jackrabbit.oak.jcr.delegate.ItemDelegate)2 VersionDelegate (org.apache.jackrabbit.oak.jcr.delegate.VersionDelegate)2 Iterator (java.util.Iterator)1 NoSuchElementException (java.util.NoSuchElementException)1 CheckForNull (javax.annotation.CheckForNull)1 NodeIterator (javax.jcr.NodeIterator)1 UnsupportedRepositoryOperationException (javax.jcr.UnsupportedRepositoryOperationException)1