Feedback Form

BLOG

Implementing versioning of node in Java content Repository

By poulomi - JCRDev | Posted on 2009-12-29 04:26

In any Java Content Repository based Content Management System, maintaining of version of data is an important requirement. This versioning of data is actually maintaining version of the node which holds that data. JSR-170 provides an easy mechanism for such versioning.

Every node has one and only one primary node type. A primary node type defines the characteristics of the node, such as the properties and child nodes that the node is allowed to have. In addition to the primary node type, a node may also have one or more mixin types. A mixin type acts a lot like a decorator, providing extra characteristics to a node.In order to make a node versionable we add mixin type mix:versionable .When one node has a mixin of type "mix:versionable" , it automatically has the mixin type mix:referenceable which provides an auto-created jcr:uuid property that gives the node a unique, referenceable identifier.

Node appNode = rootNode.addNode("appNode");
appNode.addMixin("mix:versionable");
jcrsession.save();

The appNode is now a versionable node. When any changes are made to this node , the changes will be saved under the current version or the base version. In order to lock a particular version or in other words in order to create a new version the node has to be checked in before the session is saved.Suppose we add a property to the above node and now want to create a version of the node with the newly added property.

appNode.setProperty("description","this is a versionable node");
appNode.checkin();
jcrsession.save();

The above code snippet will freeze the current node and create a new version of the node with version id 1.0. When a versionable node is checked in , no further changes can be made on the node unless and until it is checked out.

appNode.checkout();

When a node is checked out, we get the node data and information in accordance with the latest node version.In other words , the current Node is equivalent to the latest version of node that has been last checked in.

One point to note , a new version of the node will not be created until and unless , the node is checked in after changes are made.If the following code snippet is executed:

appNode.checkout();
appNode.setProperty("description","this is a versionable node");
jcrsession.save();

In this case , the changes are saved in the base node or current node and no new version is created. A new version will be created freezing the base node or current node only when the node is checked in, till then all the changes will be saved under current node.Each time a node is checked in, a new version will be created with id 1.0,1.1,1.2 and so on.

Thus, one can easily implement versioning of nodes in java content repository.

.

Rated 7.0 by 1 user.

Previous comments on this post

By  shankha - JCRDev | Posted on 2009-12-29 04:38

I would further like to add that, when a node is added with a mixing node type of mix:versionable, that node becomes automatically a node with the "mix:referenceable". So, after a node can be referenced using Node

appNode = Session.getNodeByUUID(String uuidOfRequiredAppNode).

Thanks for the post.

 

 

JCR Developer Community Blog

BLOG STATS

5625 visitors have read this Blog

BLOG CATEGORIES

JCR: 18
Jackrabbit: 8
Sling: 24
CMS: 23
Future Trends: 7
Other: 6

JCR DEVELOPERS (level, posts)

Sujoy Bhattacharjee (bhatasuj)
JCRDev
Blog Posts 5 | Forum Posts 5
Romit Bose (romitbose)
JCRDev
Blog Posts 0 | Forum Posts 0
Ruben Reusser (reusr1)
JCRDev
Blog Posts 0 | Forum Posts 0
Wayne Lund (wxlund)
JCRDev
Blog Posts 0 | Forum Posts 0
Luis Fernandez (guisho)
JCRDev
Blog Posts 0 | Forum Posts 0
Jong Tenerife (jongdakkel)
JCRDev
Blog Posts 0 | Forum Posts 2
Jay Kerger (jkerger)
JCRDev
Blog Posts 2 | Forum Posts 2
Subhasis Chakraborty (subhasis)
JCRDev
Blog Posts 4 | Forum Posts 13
Nitesh Ambuj (nambuj)
JCRLead
Blog Posts 5 | Forum Posts 18
Amitava Chakraborty (meamitava)
JCRLead
Blog Posts 8 | Forum Posts 14