// Copyright (C) Stichting Deltares 2018. 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 log4net;
using Ringtoets.Common.Data.AssessmentSection;
using Ringtoets.Common.Data.Calculation;
using Ringtoets.Common.Data.Contribution;
using Ringtoets.Common.Forms.PropertyClasses;
using Riskeer.Integration.Plugin.Properties;
using Riskeer.Integration.Service;
using CoreCommonBaseResources = Core.Common.Base.Properties.Resources;
namespace Riskeer.Integration.Plugin.Handlers
{
///
/// Class responsible for changing the
/// value of the of an
/// and clearing all data dependent on the original norm value.
///
public class FailureMechanismContributionNormChangeHandler : IObservablePropertyChangeHandler
{
private readonly IAssessmentSection assessmentSection;
private readonly ILog log = LogManager.GetLogger(typeof(FailureMechanismContributionNormChangeHandler));
///
/// Creates a new instance of .
///
/// The assessment section to change the contribution norm for.
/// Thrown when
/// is null.
public FailureMechanismContributionNormChangeHandler(IAssessmentSection assessmentSection)
{
if (assessmentSection == null)
{
throw new ArgumentNullException(nameof(assessmentSection));
}
this.assessmentSection = assessmentSection;
}
public IEnumerable SetPropertyValueAfterConfirmation(SetObservablePropertyValueDelegate setValue)
{
if (setValue == null)
{
throw new ArgumentNullException(nameof(setValue));
}
var affectedObjects = new List();
if (ConfirmPropertyChange())
{
setValue();
affectedObjects.AddRange(ClearAllNormDependentCalculationOutput());
affectedObjects.Add(assessmentSection.FailureMechanismContribution);
affectedObjects.AddRange(assessmentSection.GetFailureMechanisms());
}
return affectedObjects;
}
private static bool ConfirmPropertyChange()
{
DialogResult result = MessageBox.Show(Resources.FailureMechanismContributionNormChangeHandler_Confirm_change_norm_and_clear_dependent_data,
CoreCommonBaseResources.Confirm,
MessageBoxButtons.OKCancel);
return result == DialogResult.OK;
}
private IEnumerable ClearAllNormDependentCalculationOutput()
{
List affectedObjects = RingtoetsDataSynchronizationService.ClearFailureMechanismCalculationOutputs(assessmentSection).ToList();
if (affectedObjects.Count > 0)
{
log.InfoFormat(Resources.ChangeHandler_Results_of_NumberOfCalculations_0_calculations_cleared,
affectedObjects.OfType().Count());
}
affectedObjects.AddRange(ClearAllHydraulicBoundaryLocationCalculationOutput());
return affectedObjects;
}
private IEnumerable ClearAllHydraulicBoundaryLocationCalculationOutput()
{
IEnumerable affectedObjects = RingtoetsDataSynchronizationService.ClearHydraulicBoundaryLocationCalculationOutput(assessmentSection);
if (affectedObjects.Any())
{
log.Info(Resources.FailureMechanismContributionNormChangeHandler_Waveheight_and_design_water_level_results_cleared);
return affectedObjects;
}
return Enumerable.Empty();
}
}
}