public override IAuditWorkUnit Dispatch(IWorkUnitMergeVisitor first) { if (first is PersistentCollectionChangeWorkUnit) { PersistentCollectionChangeWorkUnit original = (PersistentCollectionChangeWorkUnit) first; // Merging the collection changes in both work units. // First building a map from the ids of the collection-entry-entities from the "second" collection changes, // to the PCCD objects. That way, we will be later able to check if an "original" collection change // should be added, or if it is overshadowed by a new one. IDictionary<Object, PersistentCollectionChangeData> newChangesIdMap = new Dictionary<Object, PersistentCollectionChangeData>(); foreach (PersistentCollectionChangeData persistentCollectionChangeData in getCollectionChanges()) { newChangesIdMap.Add( getOriginalId(persistentCollectionChangeData), persistentCollectionChangeData); } // This will be the list with the resulting (merged) changes. List<PersistentCollectionChangeData> mergedChanges = new List<PersistentCollectionChangeData>(); // Including only those original changes, which are not overshadowed by new ones. foreach (PersistentCollectionChangeData originalCollectionChangeData in original.getCollectionChanges()) { if (!newChangesIdMap.ContainsKey(getOriginalId(originalCollectionChangeData))) { mergedChanges.Add(originalCollectionChangeData); } } // Finally adding all of the new changes to the end of the list mergedChanges = (List<PersistentCollectionChangeData>)mergedChanges.Concat(getCollectionChanges()); return new PersistentCollectionChangeWorkUnit(sessionImplementor, EntityName, verCfg, EntityId, mergedChanges, ReferencingPropertyName); } else { throw new Exception("Trying to merge a " + first + " with a PersitentCollectionChangeWorkUnit. " + "This is not really possible."); } }
public override IAuditWorkUnit Dispatch(IWorkUnitMergeVisitor first) { var original = first as PersistentCollectionChangeWorkUnit; if (original != null) { // Merging the collection changes in both work units. // First building a map from the ids of the collection-entry-entities from the "second" collection changes, // to the PCCD objects. That way, we will be later able to check if an "original" collection change // should be added, or if it is overshadowed by a new one. var newChangesIdMap = new Dictionary<IDictionary<string, object>, PersistentCollectionChangeData>(new DictionaryComparer<string, object>()); foreach (var persistentCollectionChangeData in CollectionChanges) { newChangesIdMap.Add( OriginalId(persistentCollectionChangeData), persistentCollectionChangeData); } // This will be the list with the resulting (merged) changes. var mergedChanges = new List<PersistentCollectionChangeData>(); // Including only those original changes, which are not overshadowed by new ones. foreach (var originalCollectionChangeData in original.CollectionChanges) { var originalOriginalId = OriginalId(originalCollectionChangeData); if (!newChangesIdMap.ContainsKey(originalOriginalId)) { mergedChanges.Add(originalCollectionChangeData); } else { // If the changes collide, checking if the first one isn't a DEL, and the second a subsequent ADD // If so, removing the change alltogether. var revTypePropName = VerCfg.AuditEntCfg.RevisionTypePropName; if((RevisionType)newChangesIdMap[originalOriginalId].Data[revTypePropName] == RevisionType.Added && (RevisionType)originalCollectionChangeData.Data[revTypePropName] == RevisionType.Deleted) { newChangesIdMap.Remove(originalOriginalId); } } } // Finally adding all of the new changes to the end of the list // (the map values may differ from CollectionChanges because of the last operation above) mergedChanges = mergedChanges.Concat(newChangesIdMap.Values).ToList(); return new PersistentCollectionChangeWorkUnit(SessionImplementor, EntityName, VerCfg, EntityId, mergedChanges, referencingPropertyName); } throw new Exception("Trying to merge a " + first + " with a PersitentCollectionChangeWorkUnit. " + "This is not really possible."); }