Search in sources :

Example 81 with Version

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

the class VersionOperation method getRandomVersion.

/**
     * Returns a randomly chosen version for the current node or
     * <code>null</code> if the current node only has a root version.
     *
     * @param excludeReferenced exclude versions that are still referenced.
     * @return randomly chosen version or <code>null</code>.
     * @throws RepositoryException if an error occurs while reading from the
     *                             repository.
     */
protected Version getRandomVersion(boolean excludeReferenced) throws RepositoryException {
    List allVersions = new ArrayList();
    Node n = getNode();
    for (VersionIterator it = n.getVersionHistory().getAllVersions(); it.hasNext(); ) {
        Version v = it.nextVersion();
        if (excludeReferenced) {
            // quick check if it is the base version
            if (n.getBaseVersion().isSame(v)) {
                continue;
            }
        }
        if (v.getPredecessors().length > 0) {
            if (!excludeReferenced || !v.getReferences().hasNext()) {
                allVersions.add(v);
            }
        }
    }
    if (allVersions.size() > 0) {
        return (Version) allVersions.get(getRandom().nextInt(allVersions.size()));
    } else {
        return null;
    }
}
Also used : Version(javax.jcr.version.Version) Node(javax.jcr.Node) ArrayList(java.util.ArrayList) VersionIterator(javax.jcr.version.VersionIterator) List(java.util.List) ArrayList(java.util.ArrayList)

Example 82 with Version

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

the class Restore method execute.

public NodeIterator execute() throws Exception {
    Node n = getNode();
    Version v = getRandomVersion(false);
    if (v != null) {
        log.info(n.getPath() + ":" + v.getName());
        n.restore(v, true);
    }
    return wrapWithIterator(n);
}
Also used : Version(javax.jcr.version.Version) Node(javax.jcr.Node)

Example 83 with Version

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

the class ShareableNodeTest method testRestoreRemoveExisting.

/**
     * Restore a shareable node that automatically removes an existing shareable
     * node (6.13.19). In this case the particular shared node is removed but
     * its descendants continue to exist below the remaining members of the
     * shared set.
     */
public void testRestoreRemoveExisting() throws Exception {
    // setup parent nodes and first child
    Node a1 = testRootNode.addNode("a1");
    Node a2 = testRootNode.addNode("a2");
    Node b1 = a1.addNode("b1");
    testRootNode.getSession().save();
    // make b1 shareable
    ensureMixinType(b1, mixShareable);
    b1.save();
    // clone
    Workspace workspace = b1.getSession().getWorkspace();
    workspace.clone(workspace.getName(), b1.getPath(), a2.getPath() + "/b2", false);
    // add child c
    b1.addNode("c");
    b1.save();
    // make a2 versionable
    ensureMixinType(a2, mixVersionable);
    a2.save();
    // check in version and check out again
    Version v = a2.checkin();
    a2.checkout();
    // delete b2 and save
    a2.getNode("b2").remove();
    a2.save();
    // verify shareable set contains one elements only
    Node[] shared = getSharedSet(b1);
    assertEquals(1, shared.length);
    // restore version and remove existing (i.e. b1)
    a2.restore(v, true);
    // verify shareable set contains still one element
    shared = getSharedSet(a2.getNode("b2"));
    assertEquals(1, shared.length);
    // verify child c still exists
    Node[] children = toArray(a2.getNode("b2").getNodes());
    assertEquals(1, children.length);
}
Also used : Version(javax.jcr.version.Version) Node(javax.jcr.Node) Workspace(javax.jcr.Workspace)

Example 84 with Version

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

the class ReadVersionsWhileModified method testVersionHistory.

public void testVersionHistory() throws RepositoryException {
    final Node n = testRootNode.addNode(nodeName1);
    n.addMixin(mixVersionable);
    testRootNode.save();
    final Session s = getHelper().getSuperuserSession();
    Thread t = new Thread(new Runnable() {

        public void run() {
            long end = System.currentTimeMillis() + RUN_NUM_SECONDS * 1000;
            try {
                Node vn = (Node) s.getItem(n.getPath());
                while (end > System.currentTimeMillis()) {
                    vn.checkout();
                    vn.checkin();
                }
            } catch (RepositoryException e) {
                e.printStackTrace();
            } finally {
                s.logout();
            }
        }
    });
    t.start();
    VersionHistory vh = n.getVersionHistory();
    while (t.isAlive()) {
        // walk version history
        Version v = vh.getRootVersion();
        while (v.getSuccessors().length > 0) {
            v = v.getSuccessors()[0];
        }
    }
}
Also used : Version(javax.jcr.version.Version) Node(javax.jcr.Node) RepositoryException(javax.jcr.RepositoryException) VersionHistory(javax.jcr.version.VersionHistory) Session(javax.jcr.Session)

Example 85 with Version

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

the class AbstractVersionManagementTest method testRemoveVersion2.

public void testRemoveVersion2() throws RepositoryException, NotExecutableException {
    Node trn = getTestNode();
    Node n = createVersionableNode(testRootNode);
    modifyPrivileges(trn.getPath(), Privilege.JCR_VERSION_MANAGEMENT, true);
    Node testNode = trn.getNode(nodeName1);
    Version v = testNode.checkin();
    testNode.checkout();
    testNode.checkin();
    // -> VersionHistory.removeVersion must not be allowed.
    try {
        testNode.getVersionHistory().removeVersion(v.getName());
        fail("TestSession without remove privilege on the v-storage must not be able to remove a version.");
    } catch (AccessDeniedException e) {
        // success
        log.debug(e.getMessage());
    }
}
Also used : AccessDeniedException(javax.jcr.AccessDeniedException) Version(javax.jcr.version.Version) Node(javax.jcr.Node)

Aggregations

Version (javax.jcr.version.Version)265 Node (javax.jcr.Node)155 VersionManager (javax.jcr.version.VersionManager)65 RepositoryException (javax.jcr.RepositoryException)45 VersionHistory (javax.jcr.version.VersionHistory)29 VersionException (javax.jcr.version.VersionException)25 NodeIterator (javax.jcr.NodeIterator)23 Session (javax.jcr.Session)19 Test (org.junit.Test)19 Property (javax.jcr.Property)16 VersionIterator (javax.jcr.version.VersionIterator)16 Value (javax.jcr.Value)14 NotExecutableException (org.apache.jackrabbit.test.NotExecutableException)14 ItemExistsException (javax.jcr.ItemExistsException)13 HashSet (java.util.HashSet)12 NodeDefinition (javax.jcr.nodetype.NodeDefinition)12 ArrayList (java.util.ArrayList)11 Set (java.util.Set)7 DavException (org.apache.jackrabbit.webdav.DavException)7 UserTransaction (javax.transaction.UserTransaction)6