Skip to content

Instantly share code, notes, and snippets.

@fazy
Last active December 17, 2015 06:49
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fazy/5568416 to your computer and use it in GitHub Desktop.
Save fazy/5568416 to your computer and use it in GitHub Desktop.
Java-based example demonstrating the behaviour of versionable child nodes after an update operation. The expected behaviour is that cloning a node will clone its child, even if the child is versionable, while updating a node will only update the node itself, and not the versionable child.
import javax.jcr.Credentials;
import javax.jcr.Node;
import javax.jcr.Repository;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.Workspace;
import javax.jcr.SimpleCredentials;
import javax.jcr.version.*;
import ch.liip.jcr.davex.DavexClient;
public class Client
{
public static void main(String[] args)
{
try {
// Config
String url = "http://localhost:8080/jackrabbit/server/";
String sourceWorkspaceName = "test-preview";
String destWorkspaceName = "test-published";
// Setup
DavexClient Client = new DavexClient(url);
Repository repo = Client.getRepository();
Credentials sc = new SimpleCredentials("admin", "admin".toCharArray());
Session sourceSession = repo.login(sc, sourceWorkspaceName);
Session destSession = repo.login(sc, destWorkspaceName);
Workspace destWorkspace = destSession.getWorkspace();
// Clean-up from any previous run
if (sourceSession.nodeExists("/fooBar")) {
Node existingSourceNode = sourceSession.getNode("/fooBar");
sourceSession.removeItem("/fooBar");
sourceSession.save();
}
if (destSession.nodeExists("/fooBar")) {
Node existingDestNode = destSession.getNode("/fooBar");
destSession.removeItem("/fooBar");
destSession.save();
}
// Create a node and child node in the source workspace
Node newNode = sourceSession.getRootNode().addNode("fooBar");
newNode.addMixin("mix:versionable"); // referenceable or versionable give same result here
newNode.setProperty("test", "initial value");
Node newChildNode = newNode.addNode("fooBarChild");
newChildNode.addMixin("mix:versionable");
newChildNode.setProperty("test", "initial child value");
sourceSession.save();
// Clone the node for the first time; the child is also cloned as expected
destWorkspace.clone(sourceWorkspaceName, "/fooBar", "/fooBar", true);
// Update the properties of both nodes in the source workspace
newNode.setProperty("test", "updated value");
newChildNode.setProperty("test", "updated child value");
sourceSession.save();
// Update the node again; this should update the corresponding node,
// but not the corresponding child node.
Node destNode = destSession.getNode("/fooBar");
destNode.update(sourceWorkspaceName);
// Now output the destination node properties...
// Gives "updated value" as expected:
System.out.println(destNode.getProperty("test").getString());
// Gives "updated child value", but expected "initial child value":
Node destChildNode = destSession.getNode("/fooBar/fooBarChild");
System.out.println(destChildNode.getProperty("test").getString());
} catch (RepositoryException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment