// 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 System.Windows.Forms; using Core.Common.Base; using Core.Common.Gui.Commands; using log4net; using Ringtoets.Common.Data.AssessmentSection; using Ringtoets.Common.Data.Calculation; using Ringtoets.Common.Data.FailureMechanism; using Ringtoets.DuneErosion.Data; using Ringtoets.Integration.Forms.PropertyClasses; using Ringtoets.Integration.Plugin.Properties; using Ringtoets.Integration.Service; using Ringtoets.StabilityStoneCover.Data; using Ringtoets.WaveImpactAsphaltCover.Data; using CoreCommonBaseResources = Core.Common.Base.Properties.Resources; namespace Ringtoets.Integration.Plugin.Handlers { /// /// Class responsible for changing the /// value clearing all data dependent on the original composition value. /// public class AssessmentSectionCompositionChangeHandler : IAssessmentSectionCompositionChangeHandler { private readonly IViewCommands viewCommands; private readonly ILog log = LogManager.GetLogger(typeof(AssessmentSectionCompositionChangeHandler)); /// /// Creates a new instance of . /// /// The view commands used to close views for irrelevant /// failure mechanisms. /// Thrown when /// is null. public AssessmentSectionCompositionChangeHandler(IViewCommands viewCommands) { if (viewCommands == null) { throw new ArgumentNullException(nameof(viewCommands)); } this.viewCommands = viewCommands; } public bool ConfirmCompositionChange() { DialogResult result = MessageBox.Show(Resources.AssessmentSectionCompositionChangeHandler_Confirm_change_composition_and_clear_dependent_data, CoreCommonBaseResources.Confirm, MessageBoxButtons.OKCancel); return result == DialogResult.OK; } public IEnumerable ChangeComposition(IAssessmentSection assessmentSection, AssessmentSectionComposition newComposition) { if (assessmentSection == null) { throw new ArgumentNullException(nameof(assessmentSection)); } Dictionary oldFailureMechanismContributions = assessmentSection.GetFailureMechanisms().ToDictionary(f => f, f => f.Contribution); Dictionary oldFailureMechanismRelevancies = assessmentSection.GetFailureMechanisms().ToDictionary(f => f, f => f.IsRelevant); var affectedObjects = new List(); if (assessmentSection.Composition != newComposition) { assessmentSection.ChangeComposition(newComposition); affectedObjects.Add(assessmentSection); IFailureMechanism[] failureMechanismsToClearOutputFor = GetFailureMechanismsToClearOutputFor(assessmentSection, oldFailureMechanismContributions).ToArray(); IObservable[] affectedCalculations = RingtoetsDataSynchronizationService.ClearFailureMechanismCalculationOutputs(failureMechanismsToClearOutputFor).ToArray(); if (affectedCalculations.Length > 0) { affectedObjects.AddRange(affectedCalculations); log.InfoFormat(Resources.ChangeHandler_Results_of_NumberOfCalculations_0_calculations_cleared, affectedObjects.OfType().Count()); } affectedObjects.AddRange(ClearHydraulicBoundaryLocationOutput(failureMechanismsToClearOutputFor)); IFailureMechanism[] failureMechanismsWithRelevancyUpdated = GetFailureMechanismsWithRelevancyUpdated(assessmentSection, oldFailureMechanismRelevancies).ToArray(); affectedObjects.AddRange(failureMechanismsWithRelevancyUpdated); CloseViewsForIrrelevantFailureMechanisms(failureMechanismsWithRelevancyUpdated); } return affectedObjects; } private void CloseViewsForIrrelevantFailureMechanisms(IFailureMechanism[] failureMechanisms) { foreach (IFailureMechanism failureMechanism in failureMechanisms) { if (!failureMechanism.IsRelevant) { viewCommands.RemoveAllViewsForItem(failureMechanism); } } } private static IEnumerable GetFailureMechanismsWithRelevancyUpdated(IAssessmentSection assessmentSection, IDictionary oldFailureMechanismRelevancies) { return assessmentSection.GetFailureMechanisms() .Where(failureMechanism => oldFailureMechanismRelevancies.ContainsKey(failureMechanism) && oldFailureMechanismRelevancies[failureMechanism] != failureMechanism.IsRelevant); } private static IEnumerable GetFailureMechanismsToClearOutputFor(IAssessmentSection assessmentSection, IDictionary oldFailureMechanismContributions) { var failureMechanismsToClearOutputFor = new List(); foreach (IFailureMechanism failureMechanism in assessmentSection.GetFailureMechanisms()) { if (failureMechanism is StabilityStoneCoverFailureMechanism || failureMechanism is WaveImpactAsphaltCoverFailureMechanism) { continue; } if (failureMechanism is DuneErosionFailureMechanism) { failureMechanismsToClearOutputFor.Add(failureMechanism); } if (oldFailureMechanismContributions.ContainsKey(failureMechanism)) { double oldContribution = oldFailureMechanismContributions[failureMechanism]; if (Math.Abs(oldContribution) > 1e-6 && Math.Abs(oldContribution - failureMechanism.Contribution) > 1e-6) { failureMechanismsToClearOutputFor.Add(failureMechanism); } } } return failureMechanismsToClearOutputFor; } private IEnumerable ClearHydraulicBoundaryLocationOutput(IEnumerable failureMechanismsToClearOutputFor) { IEnumerable affectedObjects = RingtoetsDataSynchronizationService.ClearHydraulicBoundaryLocationOutputOfFailureMechanisms(failureMechanismsToClearOutputFor); if (affectedObjects.Any()) { log.Info(Resources.AssessmentSectionCompositionChangeHandler_Waveheight_and_design_water_level_results_cleared); return affectedObjects; } return Enumerable.Empty(); } } }