NodeBuilder getOrCreateVersionHistory(@Nonnull NodeBuilder versionable)
throws CommitFailedException {
checkNotNull(versionable);
String vUUID = uuidFromNode(versionable);
String relPath = getVersionHistoryPath(vUUID);
NodeBuilder node = versionStorageNode;
for (Iterator<String> it = PathUtils.elements(relPath).iterator(); it.hasNext(); ) {
String name = it.next();
node = node.child(name);
if (!node.hasProperty(JCR_PRIMARYTYPE)) {
String nt;
if (it.hasNext()) {
nt = REP_VERSIONSTORAGE;
} else {
// last path element denotes nt:versionHistory node
nt = NT_VERSIONHISTORY;
}
node.setProperty(JCR_PRIMARYTYPE, nt, Type.NAME);
}
}
// use jcr:rootVersion node to detect if we need to initialize the
// version history
if (!node.hasChildNode(JCR_ROOTVERSION)) {
// jcr:versionableUuid property
node.setProperty(JCR_VERSIONABLEUUID, vUUID, Type.STRING);
node.setProperty(JCR_UUID,
IdentifierManager.generateUUID(), Type.STRING);
// jcr:versionLabels child node
NodeBuilder vLabels = node.child(JCR_VERSIONLABELS);
vLabels.setProperty(JCR_PRIMARYTYPE, NT_VERSIONLABELS, Type.NAME);
// jcr:rootVersion child node
createVersion(node, versionable);
}
return node;