// Copyright (C) Stichting Deltares 2016. All rights reserved. // // This file is part of Ringtoets. // // Ringtoets is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . // // All names, logos, and references to "Deltares" are registered trademarks of // Stichting Deltares and remain full property of Stichting Deltares at all times. // All rights reserved. using System; using System.Collections.Generic; using System.Linq; using Core.Common.Base; using Core.Common.Utils; using Ringtoets.Piping.Data; using Ringtoets.Piping.IO.Importers; using Ringtoets.Piping.Plugin.Properties; using Ringtoets.Piping.Primitives; using Ringtoets.Piping.Service; namespace Ringtoets.Piping.Plugin.FileImporter { /// /// Strategy for updating the current stochastic soil models with the imported stochastic soil models. /// /// Adds stochastic soil models that are imported and are not part of current stochastic soil model collection. /// Removes stochastic soil models that are part of the current stochastic soil model collection, but were not /// amongst the imported stochastic soil models. /// /// Updates stochastic soil models that are part of the current stochastic soil model collection and are also /// imported. /// /// public class StochasticSoilModelUpdateDataStrategy : IStochasticSoilModelUpdateModelStrategy { private readonly PipingFailureMechanism failureMechanism; /// /// Creates a new instance of . /// /// The failure mechanism in which the models are updated. public StochasticSoilModelUpdateDataStrategy(PipingFailureMechanism failureMechanism) { if (failureMechanism == null) { throw new ArgumentNullException(nameof(failureMechanism)); } this.failureMechanism = failureMechanism; } public IEnumerable UpdateModelWithImportedData(StochasticSoilModelCollection targetCollection, IEnumerable readStochasticSoilModels, string sourceFilePath) { if (readStochasticSoilModels == null) { throw new ArgumentNullException(nameof(readStochasticSoilModels)); } if (targetCollection == null) { throw new ArgumentNullException(nameof(targetCollection)); } if (sourceFilePath == null) { throw new ArgumentNullException(nameof(sourceFilePath)); } try { return ModifyModelCollection(readStochasticSoilModels, targetCollection, sourceFilePath); } catch (InvalidOperationException e) { var message = Resources.StochasticSoilModelUpdateDataStrategy_UpdateModelWithImportedData_Update_of_StochasticSoilModel_failed; throw new StochasticSoilModelUpdateException(message, e); } } private IEnumerable ModifyModelCollection(IEnumerable readStochasticSoilModels, StochasticSoilModelCollection targetCollection, string sourceFilePath) { 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()) { affectedObjects.Add(targetCollection); } affectedObjects.AddRange(UpdateModels(updatedModels, readModelList)); affectedObjects.AddRange(RemoveModels(removedModels)); targetCollection.Clear(); try { targetCollection.AddRange(addedModels.Union(updatedModels), sourceFilePath); } 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; } } } private static IEnumerable GetUpdatedExistingModels(IEnumerable existingCollection, IEnumerable readStochasticSoilModels) { 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) { 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)); 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 .StochasticSoilProfiles .ToDictionary(ssp => ssp, ssp => ssp.SoilProfile, new ReferenceEqualityComparer()); StochasticSoilModelProfileDifference difference = existingModel.Update(readModel); var affectedObjects = new List(); foreach (StochasticSoilProfile removedProfile in difference.RemovedProfiles) { affectedObjects.AddRange(PipingDataSynchronizationService.RemoveStochasticSoilProfileFromInput(failureMechanism, removedProfile)); } foreach (StochasticSoilProfile updatedProfile in difference.UpdatedProfiles) { if (!oldProfiles[updatedProfile].Equals(updatedProfile.SoilProfile)) { affectedObjects.AddRange(PipingDataSynchronizationService.ClearStochasticSoilProfileDependentData(failureMechanism, updatedProfile)); } } return affectedObjects; } /// /// Class for comparing by just the name. /// private class SoilModelNameEqualityComparer : IEqualityComparer { public bool Equals(StochasticSoilModel x, StochasticSoilModel y) { return x.Name == y.Name; } public int GetHashCode(StochasticSoilModel obj) { return obj.Name.GetHashCode(); } } } }