// it may happen during an import, that a node with version history & base version is assigned a new key and
// therefore
// the base version points to an existing version while no version history is found initially
boolean shouldCreateNewVersionHistory = true;
if (baseVersionKey != null) {
CachedNode baseVersionNode = systemCache.getNode(baseVersionKey);
if (baseVersionNode != null) {
historyKey = baseVersionNode.getParentKey(systemCache);
shouldCreateNewVersionHistory = (historyKey == null);
}
}
if (shouldCreateNewVersionHistory) {
// a new version history should be initialized
assert historyKey != null;
if (baseVersionKey == null) baseVersionKey = historyKey.withRandomId();
NodeKey originalVersionKey = originalVersionKeys != null ? originalVersionKeys.get(versionableKey) : null;
Path versionHistoryPath = versionManager.versionHistoryPathFor(versionableKey);
systemContent.initializeVersionStorage(versionableKey, historyKey, baseVersionKey, primaryType,
mixinTypes, versionHistoryPath, originalVersionKey,
context.getTime());
}
// Now update the node as if it's checked in (with the exception of the predecessors...)
Reference historyRef = referenceFactory.create(historyKey, true);
Reference baseVersionRef = referenceFactory.create(baseVersionKey, true);
node.setProperty(cache, propertyFactory.create(JcrLexicon.IS_CHECKED_OUT, Boolean.TRUE));
node.setReference(cache, propertyFactory.create(JcrLexicon.VERSION_HISTORY, historyRef), systemCache);
node.setReference(cache, propertyFactory.create(JcrLexicon.BASE_VERSION, baseVersionRef), systemCache);
// JSR 283 - 15.1
node.setReference(cache, propertyFactory.create(JcrLexicon.PREDECESSORS, new Object[] {baseVersionRef}),
systemCache);
} else {
// we're dealing with node which has a version history, check if there any versionable properties present
boolean hasVersioningProperties = node.hasProperty(JcrLexicon.IS_CHECKED_OUT, cache)
|| node.hasProperty(JcrLexicon.VERSION_HISTORY, cache)
|| node.hasProperty(JcrLexicon.BASE_VERSION, cache)
|| node.hasProperty(JcrLexicon.PREDECESSORS, cache);
if (!hasVersioningProperties) {
// the node doesn't have any versionable properties, so this is a case of mix:versionable removed at some
// point and then re-added. If it had any versioning properties, we might've been dealing with something
// else
// e.g. a restore
// Re-link the versionable properties, based on the existing version history
node.setProperty(cache, propertyFactory.create(JcrLexicon.IS_CHECKED_OUT, Boolean.TRUE));
JcrVersionHistoryNode versionHistoryNode = versionManager().getVersionHistory(node(node.getKey(), null));
Reference historyRef = referenceFactory.create(versionHistoryNode.key(), true);
node.setReference(cache, propertyFactory.create(JcrLexicon.VERSION_HISTORY, historyRef), systemCache);
// set the base version to the last existing version
JcrVersionNode baseVersion = null;
for (VersionIterator versionIterator = versionHistoryNode.getAllVersions(); versionIterator.hasNext();) {
JcrVersionNode version = (JcrVersionNode)versionIterator.nextVersion();
if (baseVersion == null || version.isLinearSuccessorOf(baseVersion)) {
baseVersion = version;
}
}
assert baseVersion != null;
Reference baseVersionRef = referenceFactory.create(baseVersion.key(), true);
node.setReference(cache, propertyFactory.create(JcrLexicon.BASE_VERSION, baseVersionRef), systemCache);
// set the predecessors to the same list as the base version's predecessors
Version[] baseVersionPredecessors = baseVersion.getPredecessors();
Reference[] predecessors = new Reference[baseVersionPredecessors.length];
for (int i = 0; i < baseVersionPredecessors.length; i++) {
predecessors[i] = referenceFactory.create(((JcrVersionNode)baseVersionPredecessors[i]).key(), true);
}
node.setReference(cache, propertyFactory.create(JcrLexicon.PREDECESSORS, predecessors), systemCache);
}
}
}
// -----------
// nt:resource
// -----------
if (nodeTypeCapabilities.isNtResource(primaryType)) {
// If there is no "jcr:mimeType" property ...
if (!node.hasProperty(JcrLexicon.MIMETYPE, cache)) {
// Try to get the MIME type for the binary value ...
org.modeshape.jcr.value.Property dataProp = node.getProperty(JcrLexicon.DATA, cache);
if (dataProp != null) {
Object dataValue = dataProp.getFirstValue();
if (dataValue instanceof Binary) {
Binary binaryValue = (Binary)dataValue;
// Get the name of this node's parent ...
String fileName = null;
NodeKey parentKey = node.getParentKey(cache);
if (parentKey != null) {
CachedNode parent = cache.getNode(parentKey);
Name parentName = parent.getName(cache);
fileName = stringFactory().create(parentName);
}
String mimeType = binaryValue.getMimeType(fileName);
if (mimeType != null) {
node.setProperty(cache, propertyFactory.create(JcrLexicon.MIMETYPE, mimeType));