Index: Ringtoets/Piping/src/Ringtoets.Piping.Plugin/FileImporter/RingtoetsPipingSurfaceLineUpdateStrategy.cs =================================================================== diff -u -r2939615955f7dc0d299fd1baa7b2c7dafcca3db2 -r501642521ffd0016f8f8c1d1b618395c4898612b --- Ringtoets/Piping/src/Ringtoets.Piping.Plugin/FileImporter/RingtoetsPipingSurfaceLineUpdateStrategy.cs (.../RingtoetsPipingSurfaceLineUpdateStrategy.cs) (revision 2939615955f7dc0d299fd1baa7b2c7dafcca3db2) +++ Ringtoets/Piping/src/Ringtoets.Piping.Plugin/FileImporter/RingtoetsPipingSurfaceLineUpdateStrategy.cs (.../RingtoetsPipingSurfaceLineUpdateStrategy.cs) (revision 501642521ffd0016f8f8c1d1b618395c4898612b) @@ -23,7 +23,7 @@ using System.Collections.Generic; using System.Linq; using Core.Common.Base; -using Core.Common.Utils; +using Ringtoets.Common.Data.UpdateDataStrategies; using Ringtoets.Piping.Data; using Ringtoets.Piping.Forms; using Ringtoets.Piping.IO.Importers; @@ -41,131 +41,66 @@ /// Updates the surface lines that are part of the current collection and are part of the imported surface line collection. /// /// - public class RingtoetsPipingSurfaceLineUpdateDataStrategy : ISurfaceLineUpdateDataStrategy + public class RingtoetsPipingSurfaceLineUpdateDataStrategy : UpdateDataStrategyBase, + ISurfaceLineUpdateDataStrategy { - private readonly PipingFailureMechanism failureMechanism; - /// /// Creates a new instance of . /// /// The failure mechanism in which the surface lines are updated. + /// Thrown when is null. public RingtoetsPipingSurfaceLineUpdateDataStrategy(PipingFailureMechanism failureMechanism) - { - if (failureMechanism == null) - { - throw new ArgumentNullException(nameof(failureMechanism)); - } + : base(failureMechanism, new RingtoetsPipingSurfaceLineNameEqualityComparer()) {} - this.failureMechanism = failureMechanism; - } - public IEnumerable UpdateSurfaceLinesWithImportedData(RingtoetsPipingSurfaceLineCollection targetCollection, IEnumerable readRingtoetsPipingSurfaceLines, string sourceFilePath) { - if (targetCollection == null) + try { - throw new ArgumentNullException(nameof(targetCollection)); + return UpdateTargetCollectionData(targetCollection, readRingtoetsPipingSurfaceLines, sourceFilePath); } - if (readRingtoetsPipingSurfaceLines == null) + catch (ArgumentNullException) { - throw new ArgumentNullException(nameof(readRingtoetsPipingSurfaceLines)); + throw; } - if (sourceFilePath == null) + catch (ArgumentException e) { - throw new ArgumentNullException(nameof(sourceFilePath)); + throw new RingtoetsPipingSurfaceLineUpdateException(e.Message, e); } - - try - { - return ModifySurfaceLineCollection(targetCollection, readRingtoetsPipingSurfaceLines, sourceFilePath); - } catch (InvalidOperationException e) { - var message = Resources.RingtoetsPipingSurfaceLineUpdateDataStrategy_UpdateSurfaceLinesWithImportedData_Update_of_RingtoetsPipingSurfaceLine_has_failed; + string message = Resources.RingtoetsPipingSurfaceLineUpdateDataStrategy_UpdateSurfaceLinesWithImportedData_Update_of_RingtoetsPipingSurfaceLine_has_failed; throw new RingtoetsPipingSurfaceLineUpdateException(message, e); } } - private IEnumerable ModifySurfaceLineCollection(RingtoetsPipingSurfaceLineCollection existingCollection, - IEnumerable readSurfaceLines, - string sourceFilePath) + /// + /// Class for comparing by only the name. + /// + private class RingtoetsPipingSurfaceLineNameEqualityComparer : IEqualityComparer { - List readSurfaceLineList = readSurfaceLines.ToList(); - List addedSurfaceLines = GetAddedReadSurfaceLines(existingCollection, readSurfaceLineList).ToList(); - List updatedSurfaceLines = GetUpdatedSurfaceLines(existingCollection, readSurfaceLineList).ToList(); - List removedSurfaceLines = GetRemovedSurfaceLines(existingCollection, readSurfaceLineList).ToList(); - - var affectedObjects = new List(); - if (addedSurfaceLines.Any()) + public bool Equals(RingtoetsPipingSurfaceLine x, RingtoetsPipingSurfaceLine y) { - affectedObjects.Add(existingCollection); + return x.Name == y.Name; } - affectedObjects.AddRange(UpdateSurfaceLines(updatedSurfaceLines, readSurfaceLineList)); - affectedObjects.AddRange(RemoveSurfaceLines(removedSurfaceLines)); - existingCollection.Clear(); - - try + public int GetHashCode(RingtoetsPipingSurfaceLine obj) { - existingCollection.AddRange(addedSurfaceLines.Union(updatedSurfaceLines), sourceFilePath); + return obj.Name.GetHashCode(); } - catch (ArgumentException e) - { - throw new RingtoetsPipingSurfaceLineUpdateException(e.Message, e); - } - - return affectedObjects.Distinct(new ReferenceEqualityComparer()); } - private static IEnumerable GetRemovedSurfaceLines(IEnumerable existingCollection, - IEnumerable readSurfaceLine) - { - return existingCollection.Except(readSurfaceLine, new RingtoetsPipingSurfaceLineNameEqualityComparer()); - } + #region Updating Data Functions - private static IEnumerable GetUpdatedSurfaceLines(IEnumerable existingCollection, - IEnumerable readSurfaceLines) + protected override IEnumerable UpdateData(IEnumerable objectsToUpdate, + IEnumerable importedDataCollection) { - return existingCollection.Intersect(readSurfaceLines, new RingtoetsPipingSurfaceLineNameEqualityComparer()); - } - - private static IEnumerable GetAddedReadSurfaceLines(IEnumerable existingCollection, - IEnumerable readSurfaceLines) - { - return readSurfaceLines.Except(existingCollection, new RingtoetsPipingSurfaceLineNameEqualityComparer()); - } - - #region Removing surface line helpers - - private IEnumerable RemoveSurfaceLines(IEnumerable removedSurfaceLines) - { var affectedObjects = new List(); - foreach (RingtoetsPipingSurfaceLine surfaceLine in removedSurfaceLines) + foreach (RingtoetsPipingSurfaceLine updatedSurfaceLine in objectsToUpdate) { - affectedObjects.AddRange(ClearSurfaceLineDependentData(surfaceLine)); - } - return affectedObjects; - } - - private IEnumerable ClearSurfaceLineDependentData(RingtoetsPipingSurfaceLine surfaceLine) - { - return PipingDataSynchronizationService.RemoveSurfaceLine(failureMechanism, surfaceLine); - } - - #endregion - - #region Updating surface line helper - - private IEnumerable UpdateSurfaceLines(IEnumerable updatedSurfaceLines, - IList readSurfaceLines) - { - var affectedObjects = new List(); - - foreach (RingtoetsPipingSurfaceLine updatedSurfaceLine in updatedSurfaceLines) - { - RingtoetsPipingSurfaceLine matchingSurfaceLine = readSurfaceLines.Single(sl => sl.Name == updatedSurfaceLine.Name); + RingtoetsPipingSurfaceLine matchingSurfaceLine = importedDataCollection.Single(sl => sl.Name == updatedSurfaceLine.Name); updatedSurfaceLine.Update(matchingSurfaceLine); affectedObjects.Add(updatedSurfaceLine); @@ -201,20 +136,24 @@ #endregion - /// - /// Class for comparing by only the name. - /// - private class RingtoetsPipingSurfaceLineNameEqualityComparer : IEqualityComparer + #region Removing Data Functions + + protected override IEnumerable RemoveData(IEnumerable removedObjects) { - public bool Equals(RingtoetsPipingSurfaceLine x, RingtoetsPipingSurfaceLine y) - { - return x.Name == y.Name; - } + var affectedObjects = new List(); - public int GetHashCode(RingtoetsPipingSurfaceLine obj) + foreach (RingtoetsPipingSurfaceLine surfaceLine in removedObjects) { - return obj.Name.GetHashCode(); + affectedObjects.AddRange(ClearSurfaceLineDependentData(surfaceLine)); } + return affectedObjects; } + + private IEnumerable ClearSurfaceLineDependentData(RingtoetsPipingSurfaceLine surfaceLine) + { + return PipingDataSynchronizationService.RemoveSurfaceLine(failureMechanism, surfaceLine); + } + + #endregion } } \ No newline at end of file