Index: Ringtoets/Piping/src/Ringtoets.Piping.Plugin/FileImporter/StochasticSoilModelUpdateDataStrategy.cs =================================================================== diff -u -r92549be285a5082435e9625732cf33ff50cd60b9 -r501642521ffd0016f8f8c1d1b618395c4898612b --- Ringtoets/Piping/src/Ringtoets.Piping.Plugin/FileImporter/StochasticSoilModelUpdateDataStrategy.cs (.../StochasticSoilModelUpdateDataStrategy.cs) (revision 92549be285a5082435e9625732cf33ff50cd60b9) +++ Ringtoets/Piping/src/Ringtoets.Piping.Plugin/FileImporter/StochasticSoilModelUpdateDataStrategy.cs (.../StochasticSoilModelUpdateDataStrategy.cs) (revision 501642521ffd0016f8f8c1d1b618395c4898612b) @@ -24,6 +24,7 @@ using System.Linq; using Core.Common.Base; using Core.Common.Utils; +using Ringtoets.Common.Data.UpdateDataStrategies; using Ringtoets.Piping.Data; using Ringtoets.Piping.IO.Importers; using Ringtoets.Piping.Plugin.Properties; @@ -43,132 +44,71 @@ /// imported. /// /// - public class StochasticSoilModelUpdateDataStrategy : IStochasticSoilModelUpdateModelStrategy + public class StochasticSoilModelUpdateDataStrategy : UpdateDataStrategyBase, + IStochasticSoilModelUpdateModelStrategy { - private readonly PipingFailureMechanism failureMechanism; - /// /// Creates a new instance of . /// /// The failure mechanism in which the models are updated. + /// Thrown when is null. public StochasticSoilModelUpdateDataStrategy(PipingFailureMechanism failureMechanism) - { - if (failureMechanism == null) - { - throw new ArgumentNullException(nameof(failureMechanism)); - } - this.failureMechanism = failureMechanism; - } - + : base(failureMechanism, new SoilModelNameEqualityComparer()) {} + public IEnumerable UpdateModelWithImportedData(StochasticSoilModelCollection targetCollection, IEnumerable readStochasticSoilModels, string sourceFilePath) { - if (readStochasticSoilModels == null) + try { - throw new ArgumentNullException(nameof(readStochasticSoilModels)); + return UpdateTargetCollectionData(targetCollection, readStochasticSoilModels, sourceFilePath); } - if (targetCollection == null) + catch (ArgumentNullException) { - throw new ArgumentNullException(nameof(targetCollection)); + throw; } - if (sourceFilePath == null) + catch (ArgumentException e) { - throw new ArgumentNullException(nameof(sourceFilePath)); + throw new StochasticSoilModelUpdateException(e.Message, e); } - - try - { - return ModifyModelCollection(readStochasticSoilModels, targetCollection, sourceFilePath); - } catch (InvalidOperationException e) { - var message = Resources.StochasticSoilModelUpdateDataStrategy_UpdateModelWithImportedData_Update_of_StochasticSoilModel_failed; + string message = Resources.StochasticSoilModelUpdateDataStrategy_UpdateModelWithImportedData_Update_of_StochasticSoilModel_failed; throw new StochasticSoilModelUpdateException(message, e); } } - private IEnumerable ModifyModelCollection(IEnumerable readStochasticSoilModels, - StochasticSoilModelCollection targetCollection, - string sourceFilePath) + /// + /// Class for comparing by just the name. + /// + private class SoilModelNameEqualityComparer : IEqualityComparer { - List readModelList = readStochasticSoilModels.ToList(); - List addedModels = GetAddedReadModels(targetCollection, readModelList).ToList(); - List updatedModels = GetUpdatedExistingModels(targetCollection, readModelList).ToList(); - List removedModels = GetRemovedExistingModels(targetCollection, readModelList).ToList(); - - var affectedObjects = new List(); - if (addedModels.Any()) + public bool Equals(StochasticSoilModel x, StochasticSoilModel y) { - affectedObjects.Add(targetCollection); + return x.Name == y.Name; } - affectedObjects.AddRange(UpdateModels(updatedModels, readModelList)); - affectedObjects.AddRange(RemoveModels(removedModels)); - targetCollection.Clear(); - - try + public int GetHashCode(StochasticSoilModel obj) { - targetCollection.AddRange(addedModels.Union(updatedModels), sourceFilePath); + return obj.Name.GetHashCode(); } - catch (ArgumentException e) - { - throw new StochasticSoilModelUpdateException(e.Message, e); - } - - return affectedObjects.Distinct(new ReferenceEqualityComparer()); } - private static IEnumerable GetAddedReadModels(IEnumerable existingCollection, IEnumerable readStochasticSoilModels) - { - var comparer = new SoilModelNameEqualityComparer(); - foreach (StochasticSoilModel source in readStochasticSoilModels) - { - if (!existingCollection.Contains(source, comparer)) - { - yield return source; - } - } - } + #region Update Data Functions - private static IEnumerable GetUpdatedExistingModels(IEnumerable existingCollection, IEnumerable readStochasticSoilModels) + protected override IEnumerable UpdateData(IEnumerable objectsToUpdate, + IEnumerable importedDataCollection) { - return existingCollection.Intersect(readStochasticSoilModels, new SoilModelNameEqualityComparer()); - } - - private static IEnumerable GetRemovedExistingModels(IEnumerable existingCollection, IEnumerable readStochasticSoilModels) - { - return existingCollection.Except(readStochasticSoilModels, new SoilModelNameEqualityComparer()); - } - - private IEnumerable RemoveModels(IEnumerable removedModels) - { var affectedObjects = new List(); - - foreach (var model in removedModels) + foreach (StochasticSoilModel updatedModel in objectsToUpdate) { - affectedObjects.AddRange(ClearStochasticSoilModelDependentData(model)); - } - return affectedObjects; - } - - private IEnumerable UpdateModels(IList updatedModels, IList readModels) - { - var affectedObjects = new List(); - foreach (StochasticSoilModel updatedModel in updatedModels) - { affectedObjects.Add(updatedModel); - StochasticSoilModel readModel = readModels.Single(r => r.Name.Equals(updatedModel.Name)); + StochasticSoilModel readModel = importedDataCollection.Single(r => r.Name.Equals(updatedModel.Name)); affectedObjects.AddRange(UpdateStochasticSoilModel(updatedModel, readModel)); } return affectedObjects; } - private IEnumerable ClearStochasticSoilModelDependentData(StochasticSoilModel removedModel) - { - return PipingDataSynchronizationService.RemoveStochasticSoilModel(failureMechanism, removedModel); - } - private IEnumerable UpdateStochasticSoilModel(StochasticSoilModel existingModel, StochasticSoilModel readModel) { Dictionary oldProfiles = existingModel @@ -192,20 +132,26 @@ return affectedObjects; } - /// - /// Class for comparing by just the name. - /// - private class SoilModelNameEqualityComparer : IEqualityComparer + #endregion + + #region Remove Data Functions + + protected override IEnumerable RemoveData(IEnumerable removedObjects) { - public bool Equals(StochasticSoilModel x, StochasticSoilModel y) - { - return x.Name == y.Name; - } + var affectedObjects = new List(); - public int GetHashCode(StochasticSoilModel obj) + foreach (StochasticSoilModel model in removedObjects) { - return obj.Name.GetHashCode(); + affectedObjects.AddRange(ClearStochasticSoilModelDependentData(model)); } + return affectedObjects; } + + private IEnumerable ClearStochasticSoilModelDependentData(StochasticSoilModel removedModel) + { + return PipingDataSynchronizationService.RemoveStochasticSoilModel(failureMechanism, removedModel); + } + + #endregion } } \ No newline at end of file