2
votes

I want to know if I can tag a particular version. I want to use JCR in my project and we have many hierarchical type tree made of different type of nodes and we need to save a version after bunch of changes for staging or production. I want to know how can I tag the particular version so that all the changes made for particular node and its children will be saved.

Thanks!!!

1

1 Answers

3
votes

Yes, you can tag the versions in a version history with strings of your choosing. But JCR 2.0 calls them "labels".

First, you need to find the VersionHistory for a particular node that's been checked in:

String path = ... // the path to the versioned node
VersionManager versionManager = session.getWorkspace().getVersionManager();
VersionHistory history = versionManager.getVersionHistory(path);

Then you can find the specific Version that you want to label, either by iterating over all the versions, by getting the "root version", or getting the base version. I won't show this since there's so many ways to do this.

// Find the version ...
Version versionToBeLabeled = ...

Then you can add a label to this version. Note that a label can only be used once within the version history of a single node, so when adding a label you can choose whether to move an existing label (if there is one) or throw an exception. Here's code that moves it if it already is used:

// Add a user-defined label to this version ...
String versionName = versionToBeLabeled.getName();
String versionLabel = "MyLabel";
boolean moveLabel = true;
VersionHistory.addVersionLabel(versionName, versionLabel, moveLabel);

Note that the version names are determined by the JCR implementation, whereas labels are user-defined. This is why it's often very convenient to add your own labels and then find particular Version instances by label rather than by some implementation-determined name:

Version foundVersion = versionHistory.getVersionByLabel(versionLabel);

And of course you can find out whether there's any Version in the history with a particular label:

if ( versionHistory.hasVersionLabel(versionLabel) ) {
    // do something
}

or if a specific version has a label:

Version version = ...
if ( versionHistory.hasVersionLabel(version,versionLabel) {
    // do something
}

and even remove a particular label:

versionHistory.removeLabel(versionLabel);

For more information, see Section 15.4 of the JCR 2.0 (JSR-283) specification.