// 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 System.Linq; using Core.Common.Base; using Ringtoets.Common.Data.Calculation; using Ringtoets.Common.Data.DikeProfiles; using Ringtoets.Common.Data.Hydraulics; using Ringtoets.Common.Service; using Ringtoets.GrassCoverErosionInwards.Data; using Ringtoets.GrassCoverErosionInwards.Util; namespace Ringtoets.GrassCoverErosionInwards.Service { /// /// Service for synchronizing grass cover erosion inwards data. /// public static class GrassCoverErosionInwardsDataSynchronizationService { /// /// Clears the output for all calculations in the . /// /// The /// which contains the calculations. /// An of calculations which are affected by /// clearing the output. /// Thrown when /// is null. public static IEnumerable ClearAllCalculationOutput(GrassCoverErosionInwardsFailureMechanism failureMechanism) { if (failureMechanism == null) { throw new ArgumentNullException(nameof(failureMechanism)); } return failureMechanism.Calculations .Cast() .SelectMany(ClearCalculationOutput) .ToArray(); } /// /// Clears the output of the given . /// /// The /// to clear the output for. /// Thrown when /// is null. /// All objects that have been changed. public static IEnumerable ClearCalculationOutput(GrassCoverErosionInwardsCalculation calculation) { if (calculation == null) { throw new ArgumentNullException(nameof(calculation)); } if (calculation.HasOutput) { calculation.ClearOutput(); return new[] { calculation }; } return Enumerable.Empty(); } /// /// Clears the and output for all the calculations /// in the . /// /// The /// which contains the calculations. /// An of objects which are affected by removal of data. /// Thrown when /// is null. public static IEnumerable ClearAllCalculationOutputAndHydraulicBoundaryLocations(GrassCoverErosionInwardsFailureMechanism failureMechanism) { if (failureMechanism == null) { throw new ArgumentNullException(nameof(failureMechanism)); } var affectedItems = new List(); foreach (GrassCoverErosionInwardsCalculation calculation in failureMechanism.Calculations.Cast()) { affectedItems.AddRange(ClearCalculationOutput(calculation) .Concat(ClearHydraulicBoundaryLocation(calculation.InputParameters))); } return affectedItems; } /// /// Clears all data dependent, either directly or indirectly, on the parent reference line. /// /// The failure mechanism to be cleared. /// The results of the clear action. /// Thrown when /// is null. public static ClearResults ClearReferenceLineDependentData(GrassCoverErosionInwardsFailureMechanism failureMechanism) { if (failureMechanism == null) { throw new ArgumentNullException(nameof(failureMechanism)); } var changedObjects = new List(); object[] removedObjects = failureMechanism.Sections .OfType() .Concat(failureMechanism.SectionResults) .Concat(failureMechanism.CalculationsGroup.GetAllChildrenRecursive()) .Concat(failureMechanism.DikeProfiles) .ToArray(); failureMechanism.ClearAllSections(); changedObjects.Add(failureMechanism); changedObjects.Add(failureMechanism.SectionResults); failureMechanism.CalculationsGroup.Children.Clear(); changedObjects.Add(failureMechanism.CalculationsGroup); failureMechanism.DikeProfiles.Clear(); changedObjects.Add(failureMechanism.DikeProfiles); return new ClearResults(changedObjects, removedObjects); } /// /// Removes the , unassigns them from the /// and clears all the data that depends on it, either directly or indirectly. /// /// The dike profile to remove. /// The calculations that may have /// assigned. /// The collection of in /// which is contained. /// The section results that may have an assignment to a calculation /// based on the . /// An of all affected objects by this operation. /// Thrown when any input argument is null. public static IEnumerable RemoveDikeProfile(DikeProfile dikeProfileToRemove, IEnumerable calculations, DikeProfileCollection dikeProfiles, IEnumerable sectionResults) { if (dikeProfileToRemove == null) { throw new ArgumentNullException(nameof(dikeProfileToRemove)); } if (calculations == null) { throw new ArgumentNullException(nameof(calculations)); } if (dikeProfiles == null) { throw new ArgumentNullException(nameof(dikeProfiles)); } if (sectionResults == null) { throw new ArgumentNullException(nameof(sectionResults)); } IEnumerable affectedCalculations = calculations.Where(calc => ReferenceEquals(dikeProfileToRemove, calc.InputParameters.DikeProfile)); var affectedObjects = new List { dikeProfiles }; affectedObjects.AddRange(ClearDikeProfileDependentData(sectionResults, affectedCalculations, calculations)); dikeProfiles.Remove(dikeProfileToRemove); return affectedObjects; } /// /// Clears , unassigns the elements from the /// and clears all the data that depends on it, either directly or indirectly. /// /// The calculations that may have /// an assigned element of . /// The collection to be cleared. /// The section results that may have an assignment to a calculation /// based on the elements of . /// An of all affected objects by this operation. /// Thrown when any input argument is null. public static IEnumerable RemoveAllDikeProfiles(IEnumerable calculations, DikeProfileCollection dikeProfiles, IEnumerable sectionResults) { if (calculations == null) { throw new ArgumentNullException(nameof(calculations)); } if (dikeProfiles == null) { throw new ArgumentNullException(nameof(dikeProfiles)); } if (sectionResults == null) { throw new ArgumentNullException(nameof(sectionResults)); } IEnumerable affectedCalculations = calculations.Where(calc => calc.InputParameters.DikeProfile != null) .ToArray(); var affectedObjects = new List { dikeProfiles }; affectedObjects.AddRange(ClearDikeProfileDependentData(sectionResults, affectedCalculations, calculations)); dikeProfiles.Clear(); return affectedObjects; } private static IEnumerable ClearDikeProfileDependentData( IEnumerable sectionResults, IEnumerable calculationsWithRemovedDikeProfile, IEnumerable calculations) { var affectedObjects = new List(); foreach (GrassCoverErosionInwardsCalculation calculation in calculationsWithRemovedDikeProfile) { affectedObjects.AddRange(RingtoetsCommonDataSynchronizationService.ClearCalculationOutput(calculation)); affectedObjects.AddRange(ClearDikeProfile(calculation.InputParameters)); } affectedObjects.AddRange(GrassCoverErosionInwardsHelper.UpdateCalculationToSectionResultAssignments( sectionResults, calculations)); return affectedObjects; } private static IEnumerable ClearDikeProfile(GrassCoverErosionInwardsInput inputParameters) { if (inputParameters.DikeProfile != null) { inputParameters.DikeProfile = null; return new[] { inputParameters }; } return Enumerable.Empty(); } private static IEnumerable ClearHydraulicBoundaryLocation(GrassCoverErosionInwardsInput input) { if (input.HydraulicBoundaryLocation != null) { input.HydraulicBoundaryLocation = null; return new[] { input }; } return Enumerable.Empty(); } } }