* Merge changes from the source to the target object. Because this is a
* collection mapping, values are added to or removed from the collection
* based on the change set.
*/
public void mergeChanges(CollectionChangeRecord changeRecord, Object valueOfTarget, boolean shouldMergeCascadeParts, MergeManager mergeManager, AbstractSession parentSession) {
ObjectChangeSet objectChanges;
synchronized (valueOfTarget) {
// Step 1 - iterate over the removed changes and remove them from the container.
Vector removedIndices = changeRecord.getOrderedRemoveObjectIndices();
if (removedIndices.isEmpty()) {
// Check if we have removed objects via a
// simpleRemoveFromCollectionChangeRecord API call.
Enumeration removedObjects = changeRecord.getRemoveObjectList().keys();
while (removedObjects.hasMoreElements()) {
objectChanges = (ObjectChangeSet) removedObjects.nextElement();
removeFrom(objectChanges.getOldKey(), objectChanges.getTargetVersionOfSourceObject(mergeManager.getSession()), valueOfTarget, parentSession);
registerRemoveNewObjectIfRequired(objectChanges, mergeManager);
}
} else {
for (int i = removedIndices.size() - 1; i >= 0; i--) {
Integer index = ((Integer) removedIndices.elementAt(i)).intValue();
objectChanges = (ObjectChangeSet) changeRecord.getOrderedRemoveObject(index);;
removeFromAtIndex(index, valueOfTarget);
// The object was actually removed and not moved.
if (changeRecord.getRemoveObjectList().containsKey(objectChanges)) {
registerRemoveNewObjectIfRequired(objectChanges, mergeManager);
}
}
}
// Step 2 - iterate over the added changes and add them to the container.
Enumeration addObjects = changeRecord.getOrderedAddObjects().elements();
while (addObjects.hasMoreElements()) {
objectChanges = (ObjectChangeSet) addObjects.nextElement();
boolean objectAdded = changeRecord.getAddObjectList().containsKey(objectChanges);
Object object = null;
// The object was actually added and not moved.
if (objectAdded && shouldMergeCascadeParts) {
object = mergeCascadeParts(objectChanges, mergeManager, parentSession);
}
if (object == null) {
// Retrieve the object to be added to the collection.
object = objectChanges.getTargetVersionOfSourceObject(mergeManager.getSession());
}
// Assume at this point the above merge will have created a new
// object if required and that the object was actually added and
// not moved.