// 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.Gui.Commands;
using log4net;
using Ringtoets.Common.Data.FailureMechanism;
using Ringtoets.Common.Data.Hydraulics;
using Ringtoets.Integration.Data;
using Ringtoets.Integration.Plugin.Properties;
namespace Ringtoets.Integration.Plugin.Merge
{
///
/// Class responsible for handling the merge of data.
///
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)
{
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);
}
private void BeforeMerge(AssessmentSection assessmentSection)
{
viewCommands.RemoveAllViewsForItem(assessmentSection);
}
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;
}
}
}