Search in sources :

Example 26 with VersionException

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

the class OpvAbortTest method testChildInSubTree.

public void testChildInSubTree() throws Exception {
    Node n = testRootNode.addNode(nodeName1).addNode(nodeName2);
    n.addMixin("OpvAbortTest");
    Node child = n.addNode("child", NodeTypeConstants.NT_OAK_UNSTRUCTURED);
    superuser.save();
    assertEquals(OnParentVersionAction.ABORT, child.getDefinition().getOnParentVersion());
    try {
        vMgr.checkpoint(testRootNode.getPath());
        fail();
    } catch (VersionException e) {
    // success
    } finally {
        superuser.refresh(false);
    }
}
Also used : Node(javax.jcr.Node) VersionException(javax.jcr.version.VersionException)

Example 27 with VersionException

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

the class VersionManagerImpl method restore.

@Override
public void restore(final String absPath, final Version version, final boolean removeExisting) throws RepositoryException {
    final SessionDelegate sessionDelegate = sessionContext.getSessionDelegate();
    sessionDelegate.performVoid(new SessionOperation<Void>("restore", true) {

        @Override
        public void performVoid() throws RepositoryException {
            String oakPath = getOakPathOrThrowNotFound(absPath);
            NodeDelegate nodeDelegate = sessionDelegate.getNode(oakPath);
            if (nodeDelegate != null) {
                throw new VersionException("VersionManager.restore(String, Version, boolean)" + " not allowed on existing nodes; use" + " VersionManager.restore(Version, boolean) instead: " + absPath);
            }
            // check if parent exists
            NodeDelegate parent = ensureParentExists(sessionDelegate, absPath);
            // check for pending changes
            checkPendingChangesForRestore(sessionDelegate);
            // check lock status
            checkNotLocked(parent.getPath());
            // check for existing nodes
            List<NodeDelegate> existing = getExisting(version, Collections.<String>emptySet());
            boolean success = false;
            try {
                if (!existing.isEmpty()) {
                    if (removeExisting) {
                        removeExistingNodes(existing);
                    } else {
                        List<String> paths = new ArrayList<String>();
                        for (NodeDelegate nd : existing) {
                            paths.add(nd.getPath());
                        }
                        throw new ItemExistsException("Unable to restore with " + "removeExisting=false. Existing nodes in " + "workspace: " + paths);
                    }
                }
                // ready for restore
                VersionDelegate vd = versionManagerDelegate.getVersionByIdentifier(version.getIdentifier());
                versionManagerDelegate.restore(parent, PathUtils.getName(oakPath), vd);
                sessionDelegate.commit();
                success = true;
            } catch (CommitFailedException e) {
                throw e.asRepositoryException();
            } finally {
                if (!success) {
                    // refresh if one of the modifying operations fail
                    sessionDelegate.refresh(false);
                }
            }
        }
    });
}
Also used : VersionDelegate(org.apache.jackrabbit.oak.jcr.delegate.VersionDelegate) RepositoryException(javax.jcr.RepositoryException) NodeDelegate(org.apache.jackrabbit.oak.jcr.delegate.NodeDelegate) SessionDelegate(org.apache.jackrabbit.oak.jcr.delegate.SessionDelegate) ItemExistsException(javax.jcr.ItemExistsException) ArrayList(java.util.ArrayList) List(java.util.List) CommitFailedException(org.apache.jackrabbit.oak.api.CommitFailedException) VersionException(javax.jcr.version.VersionException)

Example 28 with VersionException

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

the class VersionManagerImpl method restore.

@Override
public void restore(final Version[] versions, final boolean removeExisting) throws ItemExistsException, UnsupportedRepositoryOperationException, VersionException, LockException, InvalidItemStateException, RepositoryException {
    if (versions.length > 1) {
        throw new UnsupportedRepositoryOperationException("OAK-168: Restore of multiple versions not implemented.");
    }
    final Version version = versions[0];
    VersionHistory history = (VersionHistory) version.getParent();
    final String versionableId = history.getVersionableIdentifier();
    if (history.getRootVersion().isSame(version)) {
        throw new VersionException("Restore of root version not possible");
    }
    final SessionDelegate sessionDelegate = sessionContext.getSessionDelegate();
    sessionDelegate.performVoid(new SessionOperation<Void>("restore", true) {

        @Override
        public void performVoid() throws RepositoryException {
            // check for pending changes
            checkPendingChangesForRestore(sessionDelegate);
            NodeDelegate n = sessionDelegate.getNodeByIdentifier(versionableId);
            if (n == null) {
                throw new VersionException("Unable to restore version. " + "No versionable node with identifier: " + versionableId);
            }
            // check lock status
            checkNotLocked(n.getPath());
            // check for existing nodes
            List<NodeDelegate> existing = getExisting(version, Collections.singleton(n.getPath()));
            boolean success = false;
            try {
                if (!existing.isEmpty()) {
                    if (removeExisting) {
                        removeExistingNodes(existing);
                    } else {
                        List<String> paths = new ArrayList<String>();
                        for (NodeDelegate nd : existing) {
                            paths.add(nd.getPath());
                        }
                        throw new ItemExistsException("Unable to restore with " + "removeExisting=false. Existing nodes in " + "workspace: " + paths);
                    }
                }
                // ready for restore
                VersionDelegate vd = versionManagerDelegate.getVersionByIdentifier(version.getIdentifier());
                versionManagerDelegate.restore(n.getParent(), n.getName(), vd);
                sessionDelegate.commit();
                success = true;
            } catch (CommitFailedException e) {
                throw new RepositoryException(e);
            } finally {
                if (!success) {
                    // refresh if one of the modifying operations fail
                    sessionDelegate.refresh(false);
                }
            }
        }
    });
}
Also used : UnsupportedRepositoryOperationException(javax.jcr.UnsupportedRepositoryOperationException) VersionDelegate(org.apache.jackrabbit.oak.jcr.delegate.VersionDelegate) RepositoryException(javax.jcr.RepositoryException) VersionHistory(javax.jcr.version.VersionHistory) NodeDelegate(org.apache.jackrabbit.oak.jcr.delegate.NodeDelegate) SessionDelegate(org.apache.jackrabbit.oak.jcr.delegate.SessionDelegate) Version(javax.jcr.version.Version) ItemExistsException(javax.jcr.ItemExistsException) ArrayList(java.util.ArrayList) List(java.util.List) CommitFailedException(org.apache.jackrabbit.oak.api.CommitFailedException) VersionException(javax.jcr.version.VersionException)

Example 29 with VersionException

use of javax.jcr.version.VersionException in project jackrabbit by apache.

the class ItemValidator method checkCondition.

private void checkCondition(ItemImpl item, int options, int permissions, boolean isRemoval) throws RepositoryException {
    if ((options & CHECK_PENDING_CHANGES) == CHECK_PENDING_CHANGES) {
        if (item.getSession().hasPendingChanges()) {
            String msg = "Unable to perform operation. Session has pending changes.";
            log.debug(msg);
            throw new InvalidItemStateException(msg);
        }
    }
    if ((options & CHECK_PENDING_CHANGES_ON_NODE) == CHECK_PENDING_CHANGES_ON_NODE) {
        if (item.isNode() && ((NodeImpl) item).hasPendingChanges()) {
            String msg = "Unable to perform operation. Session has pending changes.";
            log.debug(msg);
            throw new InvalidItemStateException(msg);
        }
    }
    if ((options & CHECK_CONSTRAINTS) == CHECK_CONSTRAINTS) {
        if (isProtected(item)) {
            String msg = "Unable to perform operation. Node is protected.";
            log.debug(msg);
            throw new ConstraintViolationException(msg);
        }
    }
    if ((options & CHECK_CHECKED_OUT) == CHECK_CHECKED_OUT) {
        NodeImpl node = (item.isNode()) ? (NodeImpl) item : (NodeImpl) item.getParent();
        if (!node.isCheckedOut()) {
            String msg = "Unable to perform operation. Node is checked-in.";
            log.debug(msg);
            throw new VersionException(msg);
        }
    }
    if ((options & CHECK_LOCK) == CHECK_LOCK) {
        checkLock(item);
    }
    if (permissions > Permission.NONE) {
        Path path = item.getPrimaryPath();
        context.getAccessManager().checkPermission(path, permissions);
    }
    if ((options & CHECK_HOLD) == CHECK_HOLD) {
        if (hasHold(item, isRemoval)) {
            throw new RepositoryException("Unable to perform operation. Node is affected by a hold.");
        }
    }
    if ((options & CHECK_RETENTION) == CHECK_RETENTION) {
        if (hasRetention(item, isRemoval)) {
            throw new RepositoryException("Unable to perform operation. Node is affected by a retention.");
        }
    }
}
Also used : Path(org.apache.jackrabbit.spi.Path) InvalidItemStateException(javax.jcr.InvalidItemStateException) ConstraintViolationException(javax.jcr.nodetype.ConstraintViolationException) RepositoryException(javax.jcr.RepositoryException) VersionException(javax.jcr.version.VersionException)

Example 30 with VersionException

use of javax.jcr.version.VersionException in project jackrabbit by apache.

the class InternalVersionHistoryImpl method setVersionLabel.

/**
     * Sets the version <code>label</code> to the given <code>version</code>.
     * If the label is already assigned to another version, a VersionException is
     * thrown unless <code>move</code> is <code>true</code>. If <code>version</code>
     * is <code>null</code>, the label is removed from the respective version.
     * In either case, the version the label was previously assigned to is returned,
     * or <code>null</code> of the label was not moved.
     *
     * @param versionName the name of the version
     * @param label the label to assign
     * @param move  flag what to do by collisions
     * @return the version that was previously assigned by this label or <code>null</code>.
     * @throws VersionException if the version does not exist or if the label is already defined.
     */
synchronized InternalVersion setVersionLabel(Name versionName, Name label, boolean move) throws VersionException {
    InternalVersion version = (versionName != null) ? getVersion(versionName) : null;
    if (versionName != null && version == null) {
        throw new VersionException("Version " + versionName + " does not exist in this version history.");
    }
    Name prevName = labelCache.get(label);
    InternalVersionImpl prev = null;
    if (prevName == null) {
        if (version == null) {
            return null;
        }
    } else {
        prev = (InternalVersionImpl) getVersion(prevName);
        if (prev.equals(version)) {
            return version;
        } else if (!move) {
            // already defined elsewhere, throw
            throw new VersionException("Version label " + label + " already defined for version " + prev.getName());
        }
    }
    // update persistence
    try {
        if (version == null) {
            labelNode.removeProperty(label);
        } else {
            labelNode.setPropertyValue(label, InternalValue.create(version.getId()));
        }
        labelNode.store();
    } catch (RepositoryException e) {
        throw new VersionException(e);
    }
    // update internal structures
    if (prev != null) {
        prev.internalRemoveLabel(label);
        labelCache.remove(label);
    }
    if (version != null) {
        labelCache.put(label, version.getName());
        ((InternalVersionImpl) version).internalAddLabel(label);
    }
    return prev;
}
Also used : RepositoryException(javax.jcr.RepositoryException) VersionException(javax.jcr.version.VersionException) Name(org.apache.jackrabbit.spi.Name)

Aggregations

VersionException (javax.jcr.version.VersionException)78 Node (javax.jcr.Node)25 Version (javax.jcr.version.Version)25 RepositoryException (javax.jcr.RepositoryException)19 Name (org.apache.jackrabbit.spi.Name)8 NotExecutableException (org.apache.jackrabbit.test.NotExecutableException)7 Property (javax.jcr.Property)6 Session (javax.jcr.Session)6 NameException (org.apache.jackrabbit.spi.commons.conversion.NameException)6 VersionIterator (javax.jcr.version.VersionIterator)5 VersionManager (javax.jcr.version.VersionManager)5 NodeId (org.apache.jackrabbit.core.id.NodeId)5 Nonnull (javax.annotation.Nonnull)4 ItemExistsException (javax.jcr.ItemExistsException)4 UnsupportedRepositoryOperationException (javax.jcr.UnsupportedRepositoryOperationException)4 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)4 RetentionManager (javax.jcr.retention.RetentionManager)4 LabelExistsVersionException (javax.jcr.version.LabelExistsVersionException)4 ItemStateException (org.apache.jackrabbit.core.state.ItemStateException)4 CommitFailedException (org.apache.jackrabbit.oak.api.CommitFailedException)4