Name resolvedPrimaryTypeName = null;
AbstractJcrNode sourceChildNode = null;
AbstractJcrNode targetChildNode = null;
Property frozenPrimaryType = sourceChild.getProperty(JcrLexicon.FROZEN_PRIMARY_TYPE, cache);
Name sourceFrozenPrimaryType = frozenPrimaryType != null ? name(frozenPrimaryType.getFirstValue()) : null;
boolean isShared = ModeShapeLexicon.SHARE.equals(sourceFrozenPrimaryType);
boolean shouldRestore = !versionedChildrenThatShouldNotBeRestored.contains(targetChild);
boolean shouldRestoreMixinsAndUuid = false;
Path targetPath = target.getPath(cache);
boolean restoreTargetUnderSamePath = targetChild != null
&& targetChild.getPath(cache).getParent().isSameAs(targetPath);
if (targetChild != null) {
resolvedChild = resolveSourceNode(sourceChild, checkinTime, cache);
resolvedPrimaryTypeName = name(resolvedChild.getPrimaryType(cache));
sourceChildNode = session.node(resolvedChild, (Type)null);
targetChildNode = session.node(targetChild, (Type)null);
if (isShared && !restoreTargetUnderSamePath) {
// This is a shared node that already exists in the workspace ...
restoredSharedChild(target, sourceChild, targetChildNode);
continue;
}
}
if (!restoreTargetUnderSamePath) {
if (targetChild != null) {
if (!cache.isDestroyed(targetChild.getKey())) {
// the target child exists but is under a different path in the source than the target
// so we need to remove it from its parent in the target to avoid the case when later on, it might be
// destroyed
MutableCachedNode targetChildParent = cache.mutable(targetChild.getParentKey(cache));
targetChildParent.removeChild(cache, targetChild.getKey());
}
resolvedChild = resolveSourceNode(sourceChild, checkinTime, cache);
} else {
// Pull the resolved node
resolvedChild = inSourceOnly.get(sourceChild);
}
resolvedPrimaryTypeName = name(resolvedChild.getPrimaryType(cache));
sourceChildNode = session.node(resolvedChild, (Type)null);
shouldRestoreMixinsAndUuid = true;
Name primaryTypeName = null;
NodeKey desiredKey = null;
Name desiredName = null;
if (isShared && sourceChildNode != null) {
// This is a shared node that already exists in the workspace ...
AbstractJcrNode resolvedChildNode = session.node(resolvedChild, (Type)null);
restoredSharedChild(target, sourceChild, resolvedChildNode);
continue;
}
if (JcrNtLexicon.FROZEN_NODE.equals(resolvedPrimaryTypeName)) {
primaryTypeName = name(resolvedChild.getProperty(JcrLexicon.FROZEN_PRIMARY_TYPE, cache).getFirstValue());
Property idProp = resolvedChild.getProperty(JcrLexicon.FROZEN_UUID, cache);
String frozenUuid = string(idProp.getFirstValue());
desiredKey = target.getKey().withId(frozenUuid);
// the name should be that of the versioned child
desiredName = session.node(sourceChild, (Type)null).name();
} else {
primaryTypeName = resolvedChild.getPrimaryType(cache);
Property idProp = resolvedChild.getProperty(JcrLexicon.UUID, cache);
if (idProp == null || idProp.isEmpty()) {
desiredKey = target.getKey().withRandomId();
} else {
String uuid = string(idProp.getFirstValue());
desiredKey = target.getKey().withId(uuid);
}
assert sourceChildNode != null;
desiredName = sourceChildNode.name();
}
Property primaryType = propFactory.create(JcrLexicon.PRIMARY_TYPE, primaryTypeName);
targetChild = target.createChild(cache, desiredKey, desiredName, primaryType);
targetChildNode = session.node(targetChild, (Type)null);
assert shouldRestore;
}