// There is at least one persisted child with the same name, and we're adding a new child
// or renaming an existing child to this name. Therefore, we have to find a child node definition
// that allows SNS. Look for one ignoring the child node type (this is faster than finding the
// child node primary types) ...
NodeDefinitionSet childDefns = nodeTypeCapabilities.findChildNodeDefinitions(primaryType, mixinTypes);
JcrNodeDefinition childNodeDefinition = childDefns.findBestDefinitionForChild(childName, null, true,
siblingCounter);
if (childNodeDefinition != null) {
// found the one child node definition that applies, so it's okay ...
continue;
}
// We were NOT able to find a definition that allows SNS for this name, but we need to make sure that
// the node that already exists (persisted) isn't the one that's being changed
NodeKey persistedChildKey = persistedChildReferences.getChild(childName).getKey();
if (appendedChildren.containsKey(persistedChildKey) || renamedChildren.containsKey(persistedChildKey)) {
// The persisted node is being changed, so it's okay ...
continue;
}
// We still were NOT able to find a definition that allows SNS for this name WITHOUT considering the
// specific child node type. This likely means there is either 0 or more than 1 (possibly residual)
// child node definitions. We need to find all of the added/renamed child nodes and use their specific
// primary types. The first to fail will result in an exception ...
final SessionCache session = cache();
for (NodeKey appendedOrRenamedKey : appendedOrRenamedChildrenByName.get(childName)) {
MutableCachedNode appendedOrRenamedChild = session.mutable(appendedOrRenamedKey);
if (appendedOrRenamedChild == null) continue;
Name childPrimaryType = appendedOrRenamedChild.getPrimaryType(session);
childDefns = nodeTypeCapabilities.findChildNodeDefinitions(primaryType, mixinTypes);
childNodeDefinition = childDefns.findBestDefinitionForChild(childName, childPrimaryType, true,
siblingCounter);
if (childNodeDefinition == null) {
// Could not find a valid child node definition that allows SNS given the child's primary type and
// name plus the parent's primary type and mixin types.
throw new ItemExistsException(JcrI18n.noSnsDefinitionForNode.text(childName, workspaceName()));