if (existingParent == null) {
if (parent != null) {
if (additionalParents == null || additionalParents.isEmpty()) {
document.setString(PARENT, parent.toString());
} else {
EditableArray parents = Schematic.newArray(additionalParents.additionCount() + 1);
parents.add(parent.toString());
for (NodeKey added : additionalParents.getAdditions()) {
parents.add(added.toString());
}
document.set(PARENT, parents);
}
} else if (additionalParents != null && !additionalParents.isEmpty()) {
EditableArray parents = Schematic.newArray(additionalParents.additionCount());
for (NodeKey added : additionalParents.getAdditions()) {
parents.add(added.toString());
}
document.set(PARENT, parents);
}
return;
}
if (existingParent instanceof List<?>) {
// Remove the old parent and add the new parent ...
EditableArray parents = document.getArray(PARENT);
if (parent != null && !parent.equals(oldParent)) {
parents.addStringIfAbsent(parent.toString());
if (oldParent != null) {
parents.remove((Object)oldParent.toString());
}
}
if (additionalParents != null) {
for (NodeKey removedParent : additionalParents.getRemovals()) {
// When the primary parent is removed and changed to one of the additional parents, then the additional
// parent is removed. Therefore, we only want to remove it if it does not equal the new parent ...
if (!removedParent.equals(parent)) {
parents.remove((Object)removedParent.toString()); // remove by value (not by name)
}
}
for (NodeKey added : additionalParents.getAdditions()) {
parents.addStringIfAbsent(added.toString());
}
}
} else if (existingParent instanceof String) {
String existing = (String)existingParent;
if (parent != null && (additionalParents == null || additionalParents.isEmpty())) {
String oldParentStr = oldParent != null ? oldParent.toString() : null;
if (existing.equals(oldParentStr)) {
// Just replace the
document.set(PARENT, parent.toString());
} else {
// Add it ...
EditableArray parents = Schematic.newArray(2);
parents.add(existing);
parents.add(parent.toString());
document.set(PARENT, parents);
}
} else {
// Just replace the existing 'parent' field ...
int totalNumber = additionalParents.additionCount() - additionalParents.removalCount() + 1;
assert totalNumber >= 0;
EditableArray parents = Schematic.newArray(totalNumber);
if (parent != null && !existingParent.equals(parent.toString())) {
parents.add(parent.toString());
} else {
parents.add(existingParent);
}
for (NodeKey removed : additionalParents.getRemovals()) {
parents.remove((Object)removed.toString());
}
for (NodeKey added : additionalParents.getAdditions()) {
parents.add(added.toString());
}
document.set(PARENT, parents);
}
}
}