assert numNew >= 1;
assert numOld >= 1;
// Check for the most common case (just one new location and one old location) and use a more optimal algorithm ...
if (numNew == 1 && numOld == 1) {
WorkspaceAndPath newWsAndPath = newWsAndPaths.iterator().next();
WorkspaceAndPath oldWsAndPath = newWsAndPaths.iterator().next();
String newWorkspace = newWsAndPath.getWorkspaceName();
String oldWorkspace = oldWsAndPath.getWorkspaceName();
if (newWorkspace.equals(oldWorkspace)) {
// The workspaces are the same, so this is the case of a simple move
changesFor(newWorkspace).nodeMoved(key, primaryType, mixinTypes, newParentKey, oldParentKey,
newWsAndPath.getPath(), oldWsAndPath.getPath(), queryable);
return;
}
// The workspace names don't match, so treat the old as a NODE_REMOVED ...
changesFor(oldWsAndPath.getWorkspaceName()).nodeRemoved(key, oldParentKey, oldWsAndPath.getPath(), primaryType,
mixinTypes, queryable);
// And the new as NODE_CREATED (in a separate workspace) ...
// Note that we do not know the properties ...
Map<Name, Property> properties = Collections.emptyMap();
changesFor(newWsAndPath.getWorkspaceName()).nodeCreated(key, newParentKey, newWsAndPath.getPath(), primaryType,
mixinTypes, properties, queryable);
return;
}
assert numNew > 1 || numOld > 1;
// Finally the general case. Here, we need to make sure that we don't lose any old locations that did not correspond
// to at least new location. Since this is the last algorithm, we're actually going to remove all elements from
// the 'oldWsAndPaths' collection as soon as we move them. (If multiple new locations map to a single old location,
// then we'll have only one NODE_MOVED and one or more NODE_CREATED.)
for (WorkspaceAndPath wsAndNewPath : newWsAndPaths) {
// Look for the projections of the old external path in the same workspace ...
boolean found = false;
Iterator<WorkspaceAndPath> oldWsAndPathsIter = oldWsAndPaths.iterator();
while (oldWsAndPathsIter.hasNext()) {
WorkspaceAndPath wsAndOldPath = oldWsAndPathsIter.next();
String newWorkspace = wsAndNewPath.getWorkspaceName();
String oldWorkspace = wsAndOldPath.getWorkspaceName();
if (newWorkspace.equals(oldWorkspace)) {
found = true;
changesFor(newWorkspace).nodeMoved(key, primaryType, mixinTypes, newParentKey, oldParentKey,
wsAndNewPath.getPath(), wsAndOldPath.getPath(), queryable);
oldWsAndPathsIter.remove(); // we don't want to deal with this WorkspaceAndPath as the 'from' of another move
}
}
if (!found) {
// The node appeared in one workspace, but it was moved from a node that projected into a different workspace,