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;
}
}
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);
}
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);
}
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];
}
}
}
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());
}
}
Aggregations