/** * Recursively merge changes in the object dependent on the merge policy. * The hastable is used to resolv recursion. */ public Object mergeChanges(Object object, ObjectChangeSet objectChangeSet) throws ValidationException { if (object == null) { return object; } // Do not merged read-only objects in a unit of work. if (getSession().isClassReadOnly(object.getClass())) { return object; } // Means that object is either already merged or in the process of being merged. if (getObjectsAlreadyMerged().containsKey(object)) { return object; } // Put the object to be merged in the set. getObjectsAlreadyMerged().put(object, object); Object mergedObject; if (shouldMergeWorkingCopyIntoOriginal()) { mergedObject = mergeChangesOfWorkingCopyIntoOriginal(object, objectChangeSet); } else if (shouldMergeCloneIntoWorkingCopy() || shouldMergeCloneWithReferencesIntoWorkingCopy()) { mergedObject = mergeChangesOfCloneIntoWorkingCopy(object); } else if (shouldMergeOriginalIntoWorkingCopy()) { mergedObject = mergeChangesOfOriginalIntoWorkingCopy(object); } else { throw ValidationException.invalidMergePolicy(); } return mergedObject; }
/** * Return the coresponding value that should be assigned to the target object for the source object. * This value must be local to the targets object space. */ public Object getTargetVersionOfSourceObject(Object source) { if (shouldMergeWorkingCopyIntoOriginal() || shouldMergeWorkingCopyIntoRemote()) { // Target is in uow parent, or original instance for new object. return ((UnitOfWorkImpl)getSession()).getOriginalVersionOfObject(source); } else if (shouldMergeCloneIntoWorkingCopy() || shouldMergeOriginalIntoWorkingCopy() || shouldMergeCloneWithReferencesIntoWorkingCopy()) { // Target is clone from uow. //make sure we use the register for merge //bug 3584343 return registerObjectForMergeCloneIntoWorkingCopy(source); } else if (shouldRefreshRemoteObject()) { // Target is in session's cache. ClassDescriptor descriptor = getSession().getDescriptor(source); Vector primaryKey = descriptor.getObjectBuilder().extractPrimaryKeyFromObject(source, getSession()); return getSession().getIdentityMapAccessorInstance().getFromIdentityMap(primaryKey, source.getClass(), descriptor); } throw ValidationException.invalidMergePolicy(); }