Index: Ringtoets/Integration/src/Ringtoets.Integration.Plugin/Merge/AssessmentSectionMergeHandler.cs =================================================================== diff -u -r93522ef41779575693bb5692c5be649017570b30 -rbbc8a9be41850f8de8086c8bdda711f52a6767e9 --- Ringtoets/Integration/src/Ringtoets.Integration.Plugin/Merge/AssessmentSectionMergeHandler.cs (.../AssessmentSectionMergeHandler.cs) (revision 93522ef41779575693bb5692c5be649017570b30) +++ Ringtoets/Integration/src/Ringtoets.Integration.Plugin/Merge/AssessmentSectionMergeHandler.cs (.../AssessmentSectionMergeHandler.cs) (revision bbc8a9be41850f8de8086c8bdda711f52a6767e9) @@ -19,9 +19,29 @@ // 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.Gui.Commands; +using log4net; +using Ringtoets.ClosingStructures.Data; +using Ringtoets.Common.Data.Calculation; using Ringtoets.Common.Data.FailureMechanism; +using Ringtoets.Common.Data.Hydraulics; +using Ringtoets.Common.Data.Structures; +using Ringtoets.GrassCoverErosionInwards.Data; +using Ringtoets.HeightStructures.Data; +using Ringtoets.HydraRing.Calculation.Data.Input.WaveConditions; using Ringtoets.Integration.Data; +using Ringtoets.Integration.Data.StandAlone; +using Ringtoets.Integration.Plugin.Properties; +using Ringtoets.MacroStabilityInwards.Data; +using Ringtoets.Piping.Data; +using Ringtoets.Revetment.Data; +using Ringtoets.StabilityPointStructures.Data; +using Ringtoets.StabilityStoneCover.Data; +using Ringtoets.WaveImpactAsphaltCover.Data; namespace Ringtoets.Integration.Plugin.Merge { @@ -30,7 +50,289 @@ /// public class AssessmentSectionMergeHandler : IAssessmentSectionMergeHandler { + private static readonly ILog log = LogManager.GetLogger(typeof(AssessmentSectionMergeHandler)); + + private readonly IViewCommands viewCommands; + + /// + /// Creates a new instance of . + /// + /// The view commands used to close views for the target + /// . + /// Thrown when + /// is null. + public AssessmentSectionMergeHandler(IViewCommands viewCommands) + { + if (viewCommands == null) + { + throw new ArgumentNullException(nameof(viewCommands)); + } + + this.viewCommands = viewCommands; + } + public void PerformMerge(AssessmentSection targetAssessmentSection, AssessmentSection sourceAssessmentSection, - IEnumerable failureMechanismsToMerge) {} + IEnumerable failureMechanismsToMerge) + { + if (targetAssessmentSection == null) + { + throw new ArgumentNullException(nameof(targetAssessmentSection)); + } + + if (sourceAssessmentSection == null) + { + throw new ArgumentNullException(nameof(sourceAssessmentSection)); + } + + if (failureMechanismsToMerge == null) + { + throw new ArgumentNullException(nameof(failureMechanismsToMerge)); + } + + BeforeMerge(targetAssessmentSection); + MergeHydraulicBoundaryLocations(targetAssessmentSection, sourceAssessmentSection); + MergeFailureMechanisms(targetAssessmentSection, failureMechanismsToMerge); + AfterMerge(targetAssessmentSection); + } + + private void BeforeMerge(AssessmentSection assessmentSection) + { + viewCommands.RemoveAllViewsForItem(assessmentSection); + } + + private static void AfterMerge(AssessmentSection targetAssessmentSection) + { + targetAssessmentSection.NotifyObservers(); + } + + #region HydraulicBoundaryLocationCalculations + + private static void MergeHydraulicBoundaryLocations(AssessmentSection targetAssessmentSection, AssessmentSection sourceAssessmentSection) + { + MergeHydraulicBoundaryLocationCalculations(targetAssessmentSection.WaterLevelCalculationsForFactorizedSignalingNorm, + sourceAssessmentSection.WaterLevelCalculationsForFactorizedSignalingNorm); + MergeHydraulicBoundaryLocationCalculations(targetAssessmentSection.WaterLevelCalculationsForSignalingNorm, + sourceAssessmentSection.WaterLevelCalculationsForSignalingNorm); + MergeHydraulicBoundaryLocationCalculations(targetAssessmentSection.WaterLevelCalculationsForLowerLimitNorm, + sourceAssessmentSection.WaterLevelCalculationsForLowerLimitNorm); + MergeHydraulicBoundaryLocationCalculations(targetAssessmentSection.WaterLevelCalculationsForFactorizedLowerLimitNorm, + sourceAssessmentSection.WaterLevelCalculationsForFactorizedLowerLimitNorm); + + MergeHydraulicBoundaryLocationCalculations(targetAssessmentSection.WaveHeightCalculationsForFactorizedSignalingNorm, + sourceAssessmentSection.WaveHeightCalculationsForFactorizedSignalingNorm); + MergeHydraulicBoundaryLocationCalculations(targetAssessmentSection.WaveHeightCalculationsForSignalingNorm, + sourceAssessmentSection.WaveHeightCalculationsForSignalingNorm); + MergeHydraulicBoundaryLocationCalculations(targetAssessmentSection.WaveHeightCalculationsForLowerLimitNorm, + sourceAssessmentSection.WaveHeightCalculationsForLowerLimitNorm); + MergeHydraulicBoundaryLocationCalculations(targetAssessmentSection.WaveHeightCalculationsForFactorizedLowerLimitNorm, + sourceAssessmentSection.WaveHeightCalculationsForFactorizedLowerLimitNorm); + + log.Info(Resources.AssessmentSectionMergeHandler_MergeHydraulicBoundaryLocations_HydraulicBoundaryLocations_merged); + } + + private static void MergeHydraulicBoundaryLocationCalculations(IEnumerable targetCalculations, + IEnumerable sourceCalculations) + { + for (var i = 0; i < targetCalculations.Count(); i++) + { + HydraulicBoundaryLocationCalculation targetCalculation = targetCalculations.ElementAt(i); + HydraulicBoundaryLocationCalculation sourceCalculation = sourceCalculations.ElementAt(i); + + if (ShouldMerge(targetCalculation, sourceCalculation)) + { + targetCalculation.InputParameters.ShouldIllustrationPointsBeCalculated = sourceCalculation.InputParameters.ShouldIllustrationPointsBeCalculated; + targetCalculation.Output = sourceCalculation.Output; + } + } + } + + private static bool ShouldMerge(HydraulicBoundaryLocationCalculation targetCalculation, HydraulicBoundaryLocationCalculation sourceCalculation) + { + bool targetCalculationHasOutput = targetCalculation.HasOutput; + bool sourceCalculationHasOutput = sourceCalculation.HasOutput; + + if (!targetCalculationHasOutput && !sourceCalculationHasOutput + || targetCalculationHasOutput && !sourceCalculationHasOutput + || targetCalculationHasOutput && targetCalculation.Output.HasGeneralResult && !sourceCalculation.Output.HasGeneralResult) + { + return false; + } + + return true; + } + + #endregion + + #region FailureMechanisms + + private static void MergeFailureMechanisms(AssessmentSection targetAssessmentSection, IEnumerable failureMechanismsToMerge) + { + ObservableList hydraulicBoundaryLocations = targetAssessmentSection.HydraulicBoundaryDatabase.Locations; + foreach (IFailureMechanism failureMechanism in failureMechanismsToMerge) + { + if (TryMergeFailureMechanism( + targetAssessmentSection, failureMechanism, + (section, mechanism) => section.Piping = mechanism)) + { + UpdateHydraulicBoundaryLocationReferences( + targetAssessmentSection.Piping, hydraulicBoundaryLocations); + continue; + } + + if (TryMergeFailureMechanism( + targetAssessmentSection, failureMechanism, + (section, mechanism) => section.GrassCoverErosionInwards = mechanism)) + { + UpdateHydraulicBoundaryLocationReferences( + targetAssessmentSection.GrassCoverErosionInwards, hydraulicBoundaryLocations); + continue; + } + + if (TryMergeFailureMechanism( + targetAssessmentSection, failureMechanism, + (section, mechanism) => section.MacroStabilityInwards = mechanism)) + { + UpdateHydraulicBoundaryLocationReferences( + targetAssessmentSection.MacroStabilityInwards, hydraulicBoundaryLocations); + continue; + } + + if (TryMergeFailureMechanism( + targetAssessmentSection, failureMechanism, + (section, mechanism) => section.MacroStabilityOutwards = mechanism)) + { + continue; + } + + if (TryMergeFailureMechanism( + targetAssessmentSection, failureMechanism, + (section, mechanism) => section.Microstability = mechanism)) + { + continue; + } + + if (TryMergeFailureMechanism( + targetAssessmentSection, failureMechanism, + (section, mechanism) => section.StabilityStoneCover = mechanism)) + { + UpdateHydraulicBoundaryLocationReferences( + targetAssessmentSection.StabilityStoneCover, hydraulicBoundaryLocations); + continue; + } + + if (TryMergeFailureMechanism( + targetAssessmentSection, failureMechanism, + (section, mechanism) => section.WaveImpactAsphaltCover = mechanism)) + { + UpdateHydraulicBoundaryLocationReferences( + targetAssessmentSection.WaveImpactAsphaltCover, hydraulicBoundaryLocations); + continue; + } + + if (TryMergeFailureMechanism( + targetAssessmentSection, failureMechanism, + (section, mechanism) => section.WaterPressureAsphaltCover = mechanism)) + { + continue; + } + + if (TryMergeFailureMechanism( + targetAssessmentSection, failureMechanism, + (section, mechanism) => section.GrassCoverSlipOffOutwards = mechanism)) + { + continue; + } + + if (TryMergeFailureMechanism( + targetAssessmentSection, failureMechanism, + (section, mechanism) => section.GrassCoverSlipOffInwards = mechanism)) + { + continue; + } + + if (TryMergeFailureMechanism( + targetAssessmentSection, failureMechanism, + (section, mechanism) => section.HeightStructures = mechanism)) + { + UpdateHydraulicBoundaryLocationReferences, HeightStructuresInput>( + targetAssessmentSection.HeightStructures, hydraulicBoundaryLocations); + continue; + } + + if (TryMergeFailureMechanism( + targetAssessmentSection, failureMechanism, + (section, mechanism) => section.ClosingStructures = mechanism)) + { + UpdateHydraulicBoundaryLocationReferences, ClosingStructuresInput>( + targetAssessmentSection.ClosingStructures, hydraulicBoundaryLocations); + continue; + } + + if (TryMergeFailureMechanism( + targetAssessmentSection, failureMechanism, + (section, mechanism) => section.PipingStructure = mechanism)) + { + continue; + } + + if (TryMergeFailureMechanism( + targetAssessmentSection, failureMechanism, + (section, mechanism) => section.StabilityPointStructures = mechanism)) + { + UpdateHydraulicBoundaryLocationReferences, StabilityPointStructuresInput>( + targetAssessmentSection.StabilityPointStructures, hydraulicBoundaryLocations); + continue; + } + + if (TryMergeFailureMechanism( + targetAssessmentSection, failureMechanism, + (section, mechanism) => section.StrengthStabilityLengthwiseConstruction = mechanism)) + { + continue; + } + + TryMergeFailureMechanism( + targetAssessmentSection, failureMechanism, + (section, mechanism) => section.TechnicalInnovation = mechanism); + } + } + + private static bool TryMergeFailureMechanism(AssessmentSection targetAssessmentSection, IFailureMechanism failureMechanismToMerge, + Action mergeFailureMechanismAction) + where TFailureMechanism : class, IFailureMechanism + { + var failureMechanism = failureMechanismToMerge as TFailureMechanism; + if (failureMechanism != null) + { + mergeFailureMechanismAction(targetAssessmentSection, failureMechanism); + log.InfoFormat(Resources.AssessmentSectionMergeHandler_TryMergeFailureMechanism_FailureMechanism_0_replaced, failureMechanism.Name); + return true; + } + + return false; + } + + private static void UpdateHydraulicBoundaryLocationReferences( + TFailureMechanism failureMechanism, IEnumerable locations) + where TFailureMechanism : IFailureMechanism + where TCalculation : ICalculation + where TCalculationInput : class, ICalculationInputWithLocation + { + foreach (TCalculation calculation in failureMechanism.Calculations.Cast()) + { + if (calculation.InputParameters.HydraulicBoundaryLocation != null) + { + calculation.InputParameters.HydraulicBoundaryLocation = GetHydraulicBoundaryLocation(calculation.InputParameters.HydraulicBoundaryLocation, + locations); + } + } + } + + private static HydraulicBoundaryLocation GetHydraulicBoundaryLocation(HydraulicBoundaryLocation location, IEnumerable locations) + { + return locations.Single(l => l.Name == location.Name && l.Id == location.Id && l.Location.Equals(location.Location)); + } + + #endregion } } \ No newline at end of file