*/
private NodeBuilder createVersion(@Nonnull NodeBuilder vHistory,
@Nonnull NodeBuilder versionable)
throws IllegalArgumentException, CommitFailedException {
List<String> predecessors;
NodeBuilder version;
boolean isRootVersion;
if (!vHistory.hasChildNode(JCR_ROOTVERSION)) {
// create root version
isRootVersion = true;
predecessors = Collections.emptyList();
version = vHistory.child(JCR_ROOTVERSION);
} else {
isRootVersion = false;
checkState(versionable.hasProperty(JCR_PREDECESSORS));
PropertyState state = versionable.getProperty(JCR_PREDECESSORS);
predecessors = ImmutableList.copyOf(state.getValue(Type.REFERENCES));
version = vHistory.child(calculateVersion(vHistory, versionable));
}
String versionUUID = IdentifierManager.generateUUID();
version.setProperty(JCR_UUID, versionUUID, Type.STRING);
version.setProperty(JCR_PRIMARYTYPE, NT_VERSION, Type.NAME);
version.setProperty(JCR_CREATED, ISO8601.format(Calendar.getInstance()), Type.DATE);
version.setProperty(JCR_PREDECESSORS, predecessors, Type.REFERENCES);
version.setProperty(JCR_SUCCESSORS, Collections.<String>emptyList(), Type.REFERENCES);
// update successors of versions identified by predecessors
for (String id : predecessors) {
String name = PathUtils.getName(getIdentifierManager().getPath(id));
NodeBuilder predecessor = vHistory.getChildNode(name);
PropertyState state = predecessor.getProperty(JCR_SUCCESSORS);
if (state == null) {
throw new IllegalStateException("Missing " + JCR_SUCCESSORS +
" property on " + predecessor);
}
Set<String> refs = Sets.newHashSet(state.getValue(Type.REFERENCES));
refs.add(versionUUID);
predecessor.setProperty(JCR_SUCCESSORS, refs, Type.REFERENCES);
}
// jcr:frozenNode of created version
VersionableState versionableState = VersionableState.fromVersion(
version, vHistory, versionable, this, ntMgr);