// Copyright (C) Stichting Deltares 2017. 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 Core.Common.Base; using Ringtoets.Common.Data.Exceptions; using Ringtoets.Common.Data.FailureMechanism; namespace Ringtoets.Common.Data.UpdateDataStrategies { /// /// Base class of the algorithm that replaces data from a /// target collection with the data that was imported. /// /// The target data type. /// The failure mechanism in which the target collection should be updated. public abstract class ReplaceDataStrategyBase where TTargetData : class where TFailureMechanism : IFailureMechanism { private readonly TFailureMechanism failureMechanism; /// /// Initializes a /// /// The failure mechanism in which the target collection should be updated. /// Thrown when is null protected ReplaceDataStrategyBase(TFailureMechanism failureMechanism) { if (failureMechanism == null) { throw new ArgumentNullException(nameof(failureMechanism)); } this.failureMechanism = failureMechanism; } /// /// Clears all the dependent data of the target items that are contained within /// the . /// /// The failure mechanism in which the target collection resides. /// A with all the items that are affected within the failure mechanism /// after clearing all the within the target collection. protected abstract IEnumerable ClearData(TFailureMechanism failureMechanism); /// /// Replaces the data of the with the imported data in . /// /// The collection that needs to be updated. /// The data that was imported. /// The source file path where the imported data comes from. /// An with affected objects. /// Thrown when any of the input parameters are null. /// Thrown when replacing the data has failed. protected IEnumerable ReplaceTargetCollectionWithImportedData( ObservableUniqueItemCollectionWithSourcePath targetDataCollection, IEnumerable importedDataCollection, string sourceFilePath) { if (targetDataCollection == null) { throw new ArgumentNullException(nameof(targetDataCollection)); } if (importedDataCollection == null) { throw new ArgumentNullException(nameof(importedDataCollection)); } if (sourceFilePath == null) { throw new ArgumentNullException(nameof(sourceFilePath)); } var affectedObjects = new List(); affectedObjects.AddRange(ClearData(failureMechanism)); AddData(targetDataCollection, importedDataCollection, sourceFilePath); return affectedObjects; } /// /// Adds read data from the to the . /// /// The target collection which needs to be updated. /// The data that was imported. /// The source file path where the imported data comes from. /// Thrown when an error occurs while /// adding data to the . private static void AddData(ObservableUniqueItemCollectionWithSourcePath targetCollection, IEnumerable importedDataCollection, string sourceFilePath) { try { targetCollection.AddRange(importedDataCollection, sourceFilePath); } catch (ArgumentException e) { throw new UpdateDataException(e.Message, e); } } } }