tags:

views:

9

answers:

1

Jackrabbit 2.1 has versioned nodes. We want to support the "undo" of a delete of one of these nodes. "Finding it" seems to be the tricky part.

A: 

Not sure how to iterate over the version tree properly - should be possible I think, by going over /jcr:system/jcr:versionStorage, see JCR 1.0 section 8.2.2.1 and JCR 2.0 section 15.10 - but you can query the version tree with a query like

SELECT * FROM nt:frozenNode WHERE prop = 'value'

(if there is a search index configured for the version workspace in Jackrabbit, which should be by default).

The nodes returned will be the frozen nodes, get the parent node to retrieve the Version:

NodeIterator iter = res.getNodes();
while (iter.hasNext()) {
    Node frozenNode = iter.nextNode();
    Version v = (Version) frozenNode.getParent();
    // ...
}

It makes sense to store the (parent) path of the node as property whenever you create a version in the first place, so that you can query for it and also know where to restore it later (see below).

You know that it is deleted when the jcr:frozenUuid of the frozenNode can't be found in the session:

boolean deleted = false;
try {
    session.getNodeByUUID(
        frozenNode.getProperty(JcrConstants.JCR_FROZENUUID).getString()
    );
} catch (ItemNotFoundException e) {
    deleted = true;
} catch (RepositoryException e) {
    continue;
}

To restore it, take the Version and pass it to the version manager, along with the absolute path to restore it to (which could come from the property saved on the version's frozen node):

VersionManager vMgr = session.getWorkspace().getVersionManager();
vMgr.restore(path, v, true);

If you somehow know it without needing to search for it, you can also get the version by its UUID:

Version v = (Version) session.getNodeByUUID( versionUUID );
Alexander Klimetschek