Search in sources :

Example 31 with VersionException

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

the class InternalVersionHistoryImpl method removeVersion.

/**
     * Removes the indicated version from this VersionHistory. If the specified
     * vesion does not exist, if it specifies the root version or if it is
     * referenced by any node e.g. as base version, a VersionException is thrown.
     * <p>
     * all successors of the removed version become successors of the
     * predecessors of the removed version and vice versa. then, the entire
     * version node and all its subnodes are removed.
     *
     * @param versionName name of the version to remove
     * @throws VersionException if removal is not possible
     */
synchronized void removeVersion(Name versionName) throws RepositoryException {
    InternalVersionImpl v = (InternalVersionImpl) getVersion(versionName);
    if (v.equals(rootVersion)) {
        String msg = "Removal of " + versionName + " not allowed.";
        log.debug(msg);
        throw new VersionException(msg);
    }
    // check if any references (from outside the version storage) exist on this version
    if (vMgr.hasItemReferences(v.getId())) {
        throw new ReferentialIntegrityException("Unable to remove version. At least once referenced.");
    }
    // unregister from labels
    Name[] labels = v.internalGetLabels();
    for (Name label : labels) {
        v.internalRemoveLabel(label);
        labelNode.removeProperty(label);
    }
    // detach from the version graph
    v.internalDetach();
    // check if referenced by an activity
    InternalActivityImpl activity = v.getActivity();
    if (activity != null) {
        activity.removeVersion(v);
    }
    // remove from persistence state
    node.removeNode(v.getName());
    // and remove from history
    versionCache.remove(v.getId());
    nameCache.remove(versionName);
    vMgr.versionDestroyed(v);
    // Check if this was the last version in addition to the root version
    if (!vMgr.hasItemReferences(node.getNodeId())) {
        log.debug("Current version history has no references");
        NodeStateEx[] childNodes = node.getChildNodes();
        // Check if there is only root version and version labels nodes
        if (childNodes.length == 2) {
            log.debug("Removing orphan version history as it contains only two children");
            NodeStateEx parentNode = node.getParent();
            // Remove version history node
            parentNode.removeNode(node.getName());
            // store changes for this node and his children
            parentNode.store();
        } else {
            node.store();
        }
    } else {
        log.debug("Current version history has at least one reference");
        // store changes
        node.store();
    }
    // now also remove from labelCache
    for (Name label : labels) {
        labelCache.remove(label);
    }
}
Also used : ReferentialIntegrityException(javax.jcr.ReferentialIntegrityException) VersionException(javax.jcr.version.VersionException) Name(org.apache.jackrabbit.spi.Name)

Example 32 with VersionException

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

the class NodeRemoveMixinTest method testCheckedIn.

/**
     * Tests if <code>Node.removeMixin(String mixinName)</code> throws a
     * <code>VersionException</code> if <code>Node</code> is checked-in
     * <p>
     * The test creates a node <code>nodeName1</code> of type
     * <code>testNodeType</code> under <code>testRoot</code>, adds a mixin and
     * then checks it in. Then the test tries to remove the added.
     */
public void testCheckedIn() throws ConstraintViolationException, NotExecutableException, RepositoryException {
    Session session = testRootNode.getSession();
    if (!isSupported(Repository.OPTION_VERSIONING_SUPPORTED)) {
        throw new NotExecutableException("Versioning is not supported.");
    }
    // create a node that is versionable
    Node node = testRootNode.addNode(nodeName1, testNodeType);
    // or try to make it versionable if it is not
    ensureMixinType(node, mixVersionable);
    testRootNode.getSession().save();
    String mixinName = NodeMixinUtil.getAddableMixinName(session, node);
    if (mixinName == null || node.isNodeType(mixinName)) {
        throw new NotExecutableException("No testable mixin node type found");
    }
    node.addMixin(mixinName);
    testRootNode.getSession().save();
    node.checkin();
    try {
        node.removeMixin(mixinName);
        fail("Node.removeMixin(String mixinName) must throw a " + "VersionException if the node is checked-in.");
    } catch (VersionException e) {
    // success
    }
}
Also used : NotExecutableException(org.apache.jackrabbit.test.NotExecutableException) Node(javax.jcr.Node) Session(javax.jcr.Session) VersionException(javax.jcr.version.VersionException)

Example 33 with VersionException

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

the class SetValueVersionExceptionTest method testNode.

/**
     * Tests if setValue(Node) throws a VersionException immediately
     * or on save if the parent node of this property is checked-in.
     * <ul>
     * <li>{@code nodetype2} name of a node type with a reference property
     * <li>{@code propertyname3} name of a single value reference property
     *   declared in nodetype2
     * </ul>
     */
public void testNode() throws NotExecutableException, RepositoryException {
    String nodeType3 = getProperty("nodetype3");
    // create a referenceable node
    Node referenceableNode = (nodeType3 == null) ? testRootNode.addNode(nodeName3) : testRootNode.addNode(nodeName3, nodeType3);
    // try to make it referenceable if it is not
    ensureMixinType(referenceableNode, mixReferenceable);
    // implementation specific if mixin takes effect immediately or upon save
    testRootNode.getSession().save();
    String refPropName = getProperty("propertyname3");
    String nodeType = getProperty("nodetype2");
    Node node = testRootNode.addNode(nodeName4, nodeType);
    // try to make it versionable if it is not
    ensureMixinType(node, mixVersionable);
    // fail early when reference properties are not suppoerted
    ensureCanSetProperty(node, refPropName, node.getSession().getValueFactory().createValue(referenceableNode));
    Property property = node.setProperty(refPropName, referenceableNode);
    testRootNode.getSession().save();
    node.checkin();
    try {
        property.setValue(node);
        node.save();
        fail("Property.setValue(Node) must throw a VersionException " + "immediately or on save if the parent node of this property " + "is checked-in.");
    } catch (VersionException e) {
    // success
    }
    superuser.refresh(false);
    node.checkout();
}
Also used : Node(javax.jcr.Node) Property(javax.jcr.Property) VersionException(javax.jcr.version.VersionException)

Example 34 with VersionException

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

the class VersionManagerImplRestore method internalRestore.

/**
     * Internal method to restore a version.
     *
     * @param state the state to restore
     * @param version version to restore
     * @param vsel the version selector that will select the correct version for
     * OPV=Version child nodes.
     * @param removeExisting remove existing flag
     * @return set of restored versions
     * @throws RepositoryException if an error occurs
     * @throws ItemStateException if an error occurs
     */
protected Set<InternalVersion> internalRestore(NodeStateEx state, InternalVersion version, VersionSelector vsel, boolean removeExisting) throws RepositoryException, ItemStateException {
    // fail if root version
    if (version.isRootVersion()) {
        String msg = "Restore of root version not allowed.";
        log.error(msg);
        throw new VersionException(msg);
    }
    boolean isFull = checkVersionable(state);
    // check permission
    Path path = hierMgr.getPath(state.getNodeId());
    session.getAccessManager().checkPermission(path, Permission.VERSION_MNGMT);
    // 1. The child node and properties of N will be changed, removed or
    //    added to, depending on their corresponding copies in V and their
    //    own OnParentVersion attributes (see 7.2.8, below, for details).
    Set<InternalVersion> restored = new HashSet<InternalVersion>();
    internalRestoreFrozen(state, version.getFrozenNode(), vsel, restored, removeExisting, false);
    restored.add(version);
    if (isFull) {
        // 2. N's jcr:baseVersion property will be changed to point to V.
        state.setPropertyValue(NameConstants.JCR_BASEVERSION, InternalValue.create(version.getId()));
        // 4. N's jcr:predecessor property is set to null
        state.setPropertyValues(NameConstants.JCR_PREDECESSORS, PropertyType.REFERENCE, InternalValue.EMPTY_ARRAY);
        // set version history
        state.setPropertyValue(NameConstants.JCR_VERSIONHISTORY, InternalValue.create(version.getVersionHistory().getId()));
        // also clear mergeFailed
        state.removeProperty(NameConstants.JCR_MERGEFAILED);
    } else {
        // with simple versioning, the node is checked in automatically,
        // thus not allowing any branches
        vMgr.checkin(session, state, null);
    }
    // 3. N's jcr:isCheckedOut property is set to false.
    state.setPropertyValue(NameConstants.JCR_ISCHECKEDOUT, InternalValue.create(false));
    state.store();
    // check if a baseline is restored
    if (version instanceof InternalBaseline) {
        // just restore all base versions
        InternalBaseline baseline = (InternalBaseline) version;
        internalRestore(baseline.getBaseVersions(), true);
        // ensure that the restored root node has a jcr:configuration property
        // since it might not have been recorded by the initial checkin of the
        // configuration
        NodeId configId = baseline.getConfigurationId();
        NodeId rootId = baseline.getConfigurationRootId();
        NodeStateEx rootNode = state.getNode(rootId);
        rootNode.setPropertyValue(NameConstants.JCR_CONFIGURATION, InternalValue.create(configId));
        rootNode.store();
    }
    return restored;
}
Also used : Path(org.apache.jackrabbit.spi.Path) NodeId(org.apache.jackrabbit.core.id.NodeId) VersionException(javax.jcr.version.VersionException)

Example 35 with VersionException

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

the class VersionManagerImplMerge method finishMerge.

/**
     * Perform {@link Node#cancelMerge(Version)} or {@link Node#doneMerge(Version)}
     * depending on the value of <code>cancel</code>.
     * @param state state to finish
     * @param version version
     * @param cancel flag inidicates if this is a cancel operation
     * @throws RepositoryException if an error occurs
     */
protected void finishMerge(NodeStateEx state, Version version, boolean cancel) throws RepositoryException {
    // check versionable
    if (!checkVersionable(state)) {
        throw new UnsupportedRepositoryOperationException("Node not full versionable: " + safeGetJCRPath(state));
    }
    // check if version is in mergeFailed list
    Set<NodeId> failed = getMergeFailed(state);
    NodeId versionId = ((VersionImpl) version).getNodeId();
    if (!failed.remove(versionId)) {
        String msg = "Unable to finish merge. Specified version is not in" + " jcr:mergeFailed property: " + safeGetJCRPath(state);
        log.error(msg);
        throw new VersionException(msg);
    }
    WriteOperation ops = startWriteOperation();
    try {
        // remove version from mergeFailed list
        setMergeFailed(state, failed);
        if (!cancel) {
            // add version to jcr:predecessors list
            InternalValue[] vals = state.getPropertyValues(NameConstants.JCR_PREDECESSORS);
            InternalValue[] v = new InternalValue[vals.length + 1];
            for (int i = 0; i < vals.length; i++) {
                v[i] = InternalValue.create(vals[i].getNodeId());
            }
            v[vals.length] = InternalValue.create(versionId);
            state.setPropertyValues(NameConstants.JCR_PREDECESSORS, PropertyType.REFERENCE, v, true);
        }
        state.store();
        ops.save();
    } catch (ItemStateException e) {
        throw new RepositoryException(e);
    } finally {
        ops.close();
    }
}
Also used : UnsupportedRepositoryOperationException(javax.jcr.UnsupportedRepositoryOperationException) NodeId(org.apache.jackrabbit.core.id.NodeId) RepositoryException(javax.jcr.RepositoryException) InternalValue(org.apache.jackrabbit.core.value.InternalValue) VersionException(javax.jcr.version.VersionException) ItemStateException(org.apache.jackrabbit.core.state.ItemStateException)

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