// Copyright (C) Stichting Deltares 2016. 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.ClosingStructures.Data;
using Ringtoets.ClosingStructures.Service;
using Ringtoets.Common.Data.AssessmentSection;
using Ringtoets.Common.Data.DikeProfiles;
using Ringtoets.Common.Data.FailureMechanism;
using Ringtoets.Common.Data.Hydraulics;
using Ringtoets.Common.Data.Structures;
using Ringtoets.Common.Service;
using Ringtoets.GrassCoverErosionInwards.Data;
using Ringtoets.GrassCoverErosionInwards.Service;
using Ringtoets.GrassCoverErosionInwards.Utils;
using Ringtoets.GrassCoverErosionOutwards.Data;
using Ringtoets.GrassCoverErosionOutwards.Service;
using Ringtoets.HeightStructures.Data;
using Ringtoets.HeightStructures.Service;
using Ringtoets.Piping.Data;
using Ringtoets.Piping.Service;
using Ringtoets.Revetment.Data;
using Ringtoets.StabilityPointStructures.Data;
using Ringtoets.StabilityPointStructures.Service;
using Ringtoets.StabilityStoneCover.Data;
using Ringtoets.StabilityStoneCover.Service;
using Ringtoets.WaveImpactAsphaltCover.Data;
using Ringtoets.WaveImpactAsphaltCover.Service;
namespace Ringtoets.Integration.Service
{
///
/// Service for synchronizing ringtoets.
///
public static class RingtoetsDataSynchronizationService
{
///
/// Clears all the output data and hydraulic boundary locations within the .
///
/// The to clear the data for.
/// An of calculations which are affected by
/// removing data.
/// /// Thrown when is null.
public static IEnumerable ClearAllCalculationOutputAndHydraulicBoundaryLocations(IAssessmentSection assessmentSection)
{
if (assessmentSection == null)
{
throw new ArgumentNullException("assessmentSection");
}
var affectedItems = new List();
foreach (var failureMechanism in assessmentSection.GetFailureMechanisms())
{
var pipingFailureMechanism = failureMechanism as PipingFailureMechanism;
var grassCoverErosionInwardsFailureMechanism = failureMechanism as GrassCoverErosionInwardsFailureMechanism;
var stabilityStoneCoverFailureMechanism = failureMechanism as StabilityStoneCoverFailureMechanism;
var heightStructuresFailureMechanism = failureMechanism as HeightStructuresFailureMechanism;
var closingStructuresFailureMechanism = failureMechanism as ClosingStructuresFailureMechanism;
var stabilityPointStructuresFailureMechanism = failureMechanism as StabilityPointStructuresFailureMechanism;
var grassCoverErosionOutwardsFailureMechanism = failureMechanism as GrassCoverErosionOutwardsFailureMechanism;
var waveImpactAsphaltCoverFailureMechanism = failureMechanism as WaveImpactAsphaltCoverFailureMechanism;
if (pipingFailureMechanism != null)
{
affectedItems.AddRange(PipingDataSynchronizationService.ClearAllCalculationOutputAndHydraulicBoundaryLocations(pipingFailureMechanism));
}
if (grassCoverErosionInwardsFailureMechanism != null)
{
affectedItems.AddRange(GrassCoverErosionInwardsDataSynchronizationService.ClearAllCalculationOutputAndHydraulicBoundaryLocations(grassCoverErosionInwardsFailureMechanism));
}
if (stabilityStoneCoverFailureMechanism != null)
{
affectedItems.AddRange(StabilityStoneCoverDataSynchronizationService.ClearAllWaveConditionsCalculationOutputAndHydraulicBoundaryLocations(stabilityStoneCoverFailureMechanism));
}
if (waveImpactAsphaltCoverFailureMechanism != null)
{
affectedItems.AddRange(WaveImpactAsphaltCoverDataSynchronizationService.ClearAllWaveConditionsCalculationOutputAndHydraulicBoundaryLocations(waveImpactAsphaltCoverFailureMechanism));
}
if (grassCoverErosionOutwardsFailureMechanism != null)
{
affectedItems.AddRange(GrassCoverErosionOutwardsDataSynchronizationService.ClearAllWaveConditionsCalculationOutputAndHydraulicBoundaryLocations(grassCoverErosionOutwardsFailureMechanism));
}
if (heightStructuresFailureMechanism != null)
{
affectedItems.AddRange(HeightStructuresDataSynchronizationService.ClearAllCalculationOutputAndHydraulicBoundaryLocations(heightStructuresFailureMechanism));
}
if (closingStructuresFailureMechanism != null)
{
affectedItems.AddRange(ClosingStructuresDataSynchronizationService.ClearAllCalculationOutputAndHydraulicBoundaryLocations(closingStructuresFailureMechanism));
}
if (stabilityPointStructuresFailureMechanism != null)
{
affectedItems.AddRange(StabilityPointStructuresDataSynchronizationService.ClearAllCalculationOutputAndHydraulicBoundaryLocations(stabilityPointStructuresFailureMechanism));
}
}
return affectedItems;
}
///
/// Clears the output of 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 ClearFailureMechanismCalculationOutputs(IAssessmentSection assessmentSection)
{
if (assessmentSection == null)
{
throw new ArgumentNullException("assessmentSection");
}
var affectedItems = new List();
foreach (var failureMechanism in assessmentSection.GetFailureMechanisms())
{
var pipingFailureMechanism = failureMechanism as PipingFailureMechanism;
var grassCoverErosionInwardsFailureMechanism = failureMechanism as GrassCoverErosionInwardsFailureMechanism;
var stabilityStoneCoverFailureMechanism = failureMechanism as StabilityStoneCoverFailureMechanism;
var heightStructuresFailureMechanism = failureMechanism as HeightStructuresFailureMechanism;
var closingStructuresFailureMechanism = failureMechanism as ClosingStructuresFailureMechanism;
var stabilityPointStructuresFailureMechanism = failureMechanism as StabilityPointStructuresFailureMechanism;
var grassCoverErosionOutwardsFailureMechanism = failureMechanism as GrassCoverErosionOutwardsFailureMechanism;
var waveImpactAsphaltCoverFailureMechanism = failureMechanism as WaveImpactAsphaltCoverFailureMechanism;
if (pipingFailureMechanism != null)
{
affectedItems.AddRange(PipingDataSynchronizationService.ClearAllCalculationOutput(pipingFailureMechanism));
}
if (grassCoverErosionInwardsFailureMechanism != null)
{
affectedItems.AddRange(GrassCoverErosionInwardsDataSynchronizationService.ClearAllCalculationOutput(grassCoverErosionInwardsFailureMechanism));
}
if (stabilityStoneCoverFailureMechanism != null)
{
affectedItems.AddRange(StabilityStoneCoverDataSynchronizationService.ClearAllWaveConditionsCalculationOutput(stabilityStoneCoverFailureMechanism));
}
if (waveImpactAsphaltCoverFailureMechanism != null)
{
affectedItems.AddRange(WaveImpactAsphaltCoverDataSynchronizationService.ClearAllWaveConditionsCalculationOutput(waveImpactAsphaltCoverFailureMechanism));
}
if (grassCoverErosionOutwardsFailureMechanism != null)
{
affectedItems.AddRange(GrassCoverErosionOutwardsDataSynchronizationService.ClearAllWaveConditionsCalculationOutput(grassCoverErosionOutwardsFailureMechanism));
}
if (heightStructuresFailureMechanism != null)
{
affectedItems.AddRange(HeightStructuresDataSynchronizationService.ClearAllCalculationOutput(heightStructuresFailureMechanism));
}
if (closingStructuresFailureMechanism != null)
{
affectedItems.AddRange(ClosingStructuresDataSynchronizationService.ClearAllCalculationOutput(closingStructuresFailureMechanism));
}
if (stabilityPointStructuresFailureMechanism != null)
{
affectedItems.AddRange(StabilityPointStructuresDataSynchronizationService.ClearAllCalculationOutput(stabilityPointStructuresFailureMechanism));
}
}
return affectedItems;
}
///
/// Clears the output of the hydraulic boundary locations within the
/// and .
///
/// The wich contains the locations.
/// The which contains the locations.
/// All objects affected by the operation.
/// Thrown when
/// or is null.
public static IEnumerable ClearHydraulicBoundaryLocationOutput(HydraulicBoundaryDatabase hydraulicBoundaryDatabase,
GrassCoverErosionOutwardsFailureMechanism failureMechanism)
{
if (hydraulicBoundaryDatabase == null)
{
throw new ArgumentNullException("hydraulicBoundaryDatabase");
}
if (failureMechanism == null)
{
throw new ArgumentNullException("failureMechanism");
}
return RingtoetsCommonDataSynchronizationService.ClearHydraulicBoundaryLocationOutput(failureMechanism.HydraulicBoundaryLocations)
.Concat(RingtoetsCommonDataSynchronizationService.ClearHydraulicBoundaryLocationOutput(
hydraulicBoundaryDatabase.Locations))
.ToArray();
}
///
/// Clears the reference line and all data that depends on it, either directly or indirectly.
///
/// The assessment section.
/// All observable objects affected by this method.
/// Thrown when
/// is null.
public static IEnumerable ClearReferenceLine(IAssessmentSection assessmentSection)
{
if (assessmentSection == null)
{
throw new ArgumentNullException("assessmentSection");
}
var list = new List();
foreach (IFailureMechanism failureMechanism in assessmentSection.GetFailureMechanisms())
{
var pipingFailureMechanism = failureMechanism as PipingFailureMechanism;
var grassCoverErosionInwardsFailureMechanism = failureMechanism as GrassCoverErosionInwardsFailureMechanism;
var stabilityStoneCoverFailureMechanism = failureMechanism as StabilityStoneCoverFailureMechanism;
var heightStructuresFailureMechanism = failureMechanism as HeightStructuresFailureMechanism;
var closingStructuresFailureMechanism = failureMechanism as ClosingStructuresFailureMechanism;
var stabilityPointStructuresFailureMechanism = failureMechanism as StabilityPointStructuresFailureMechanism;
var grassCoverErosionOutwardsFailureMechanism = failureMechanism as GrassCoverErosionOutwardsFailureMechanism;
var waveImpactAsphaltCoverFailureMechanism = failureMechanism as WaveImpactAsphaltCoverFailureMechanism;
if (pipingFailureMechanism != null)
{
list.AddRange(PipingDataSynchronizationService.ClearReferenceLineDependentData(pipingFailureMechanism));
}
else if (grassCoverErosionInwardsFailureMechanism != null)
{
list.AddRange(GrassCoverErosionInwardsDataSynchronizationService.ClearReferenceLineDependentData(grassCoverErosionInwardsFailureMechanism));
}
else if (stabilityStoneCoverFailureMechanism != null)
{
list.AddRange(StabilityStoneCoverDataSynchronizationService.ClearReferenceLineDependentData(stabilityStoneCoverFailureMechanism));
}
else if (waveImpactAsphaltCoverFailureMechanism != null)
{
list.AddRange(WaveImpactAsphaltCoverDataSynchronizationService.ClearReferenceLineDependentData(waveImpactAsphaltCoverFailureMechanism));
}
else if (grassCoverErosionOutwardsFailureMechanism != null)
{
list.AddRange(GrassCoverErosionOutwardsDataSynchronizationService.ClearReferenceLineDependentData(grassCoverErosionOutwardsFailureMechanism));
}
else if (heightStructuresFailureMechanism != null)
{
list.AddRange(HeightStructuresDataSynchronizationService.ClearReferenceLineDependentData(heightStructuresFailureMechanism));
}
else if (closingStructuresFailureMechanism != null)
{
list.AddRange(ClosingStructuresDataSynchronizationService.ClearReferenceLineDependentData(closingStructuresFailureMechanism));
}
else if (stabilityPointStructuresFailureMechanism != null)
{
list.AddRange(StabilityPointStructuresDataSynchronizationService.ClearReferenceLineDependentData(stabilityPointStructuresFailureMechanism));
}
else
{
list.AddRange(ClearReferenceLineDependentData(failureMechanism));
}
}
assessmentSection.ReferenceLine = null;
list.Add(assessmentSection);
return list;
}
///
/// Removes a given from the
/// and clears all data that depends on it, either directly or indirectly.
///
/// The failure mechanism containing .
/// The profile residing in
/// that should be removed.
/// All observable objects affected by this method.
/// Thrown when
/// or is null.
public static IEnumerable RemoveForeshoreProfile(HeightStructuresFailureMechanism failureMechanism, ForeshoreProfile profile)
{
if (failureMechanism == null)
{
throw new ArgumentNullException("failureMechanism");
}
if (profile == null)
{
throw new ArgumentNullException("profile");
}
var changedObservables = new List();
IEnumerable> calculations = failureMechanism.Calculations.Cast>();
changedObservables.AddRange(RingtoetsCommonDataSynchronizationService.ClearForeshoreProfile(calculations, profile));
failureMechanism.ForeshoreProfiles.Remove(profile);
changedObservables.Add(failureMechanism.ForeshoreProfiles);
return changedObservables;
}
///
/// Removes a given from the
/// and clears all data that depends on it, either directly or indirectly.
///
/// The failure mechanism containing .
/// The profile residing in
/// that should be removed.
/// All observable objects affected by this method.
/// Thrown when
/// or is null.
public static IEnumerable RemoveForeshoreProfile(ClosingStructuresFailureMechanism failureMechanism, ForeshoreProfile profile)
{
if (failureMechanism == null)
{
throw new ArgumentNullException("failureMechanism");
}
if (profile == null)
{
throw new ArgumentNullException("profile");
}
var changedObservables = new List();
IEnumerable> calculations = failureMechanism.Calculations.Cast>();
changedObservables.AddRange(RingtoetsCommonDataSynchronizationService.ClearForeshoreProfile(calculations, profile));
failureMechanism.ForeshoreProfiles.Remove(profile);
changedObservables.Add(failureMechanism.ForeshoreProfiles);
return changedObservables;
}
///
/// Removes a given from the
/// and clears all data that depends on it, either directly or indirectly.
///
/// The failure mechanism containing .
/// The profile residing in
/// that should be removed.
/// All observable objects affected by this method.
/// Thrown when
/// or is null.
public static IEnumerable RemoveForeshoreProfile(StabilityPointStructuresFailureMechanism failureMechanism, ForeshoreProfile profile)
{
if (failureMechanism == null)
{
throw new ArgumentNullException("failureMechanism");
}
if (profile == null)
{
throw new ArgumentNullException("profile");
}
var changedObservables = new List();
IEnumerable> calculations = failureMechanism.Calculations.Cast>();
changedObservables.AddRange(RingtoetsCommonDataSynchronizationService.ClearForeshoreProfile(calculations, profile));
failureMechanism.ForeshoreProfiles.Remove(profile);
changedObservables.Add(failureMechanism.ForeshoreProfiles);
return changedObservables;
}
///
/// Removes a given from the
/// and clears all data that depends on it, either directly or indirectly.
///
/// The failure mechanism containing .
/// The profile residing in
/// that should be removed.
/// All observable objects affected by this method.
/// Thrown when
/// or is null.
public static IEnumerable RemoveForeshoreProfile(StabilityStoneCoverFailureMechanism failureMechanism, ForeshoreProfile profile)
{
if (failureMechanism == null)
{
throw new ArgumentNullException("failureMechanism");
}
if (profile == null)
{
throw new ArgumentNullException("profile");
}
var changedObservables = new List();
WaveConditionsInput[] calculationInputs = failureMechanism.Calculations
.Cast()
.Select(c => c.InputParameters)
.ToArray();
changedObservables.AddRange(OnWaveConditionsInputForeshoreProfileRemoved(profile, calculationInputs));
failureMechanism.ForeshoreProfiles.Remove(profile);
changedObservables.Add(failureMechanism.ForeshoreProfiles);
return changedObservables;
}
///
/// Removes a given from the
/// and clears all data that depends on it, either directly or indirectly.
///
/// The failure mechanism containing .
/// The profile residing in
/// that should be removed.
/// All observable objects affected by this method.
/// Thrown when
/// or is null.
public static IEnumerable RemoveForeshoreProfile(WaveImpactAsphaltCoverFailureMechanism failureMechanism, ForeshoreProfile profile)
{
if (failureMechanism == null)
{
throw new ArgumentNullException("failureMechanism");
}
if (profile == null)
{
throw new ArgumentNullException("profile");
}
var changedObservables = new List();
WaveConditionsInput[] calculationInputs = failureMechanism.Calculations
.Cast()
.Select(c => c.InputParameters)
.ToArray();
changedObservables.AddRange(OnWaveConditionsInputForeshoreProfileRemoved(profile, calculationInputs));
failureMechanism.ForeshoreProfiles.Remove(profile);
changedObservables.Add(failureMechanism.ForeshoreProfiles);
return changedObservables;
}
///
/// Removes a given from the
/// and clears all data that depends on it, either directly or indirectly.
///
/// The failure mechanism containing .
/// The profile residing in
/// that should be removed.
/// All observable objects affected by this method.
/// Thrown when
/// or is null.
public static IEnumerable RemoveForeshoreProfile(GrassCoverErosionOutwardsFailureMechanism failureMechanism, ForeshoreProfile profile)
{
if (failureMechanism == null)
{
throw new ArgumentNullException("failureMechanism");
}
if (profile == null)
{
throw new ArgumentNullException("profile");
}
var changedObservables = new List();
WaveConditionsInput[] calculationInputs = failureMechanism.Calculations
.Cast()
.Select(c => c.InputParameters)
.ToArray();
changedObservables.AddRange(OnWaveConditionsInputForeshoreProfileRemoved(profile, calculationInputs));
failureMechanism.ForeshoreProfiles.Remove(profile);
changedObservables.Add(failureMechanism.ForeshoreProfiles);
return changedObservables;
}
///
/// Removes a given from the
/// and clears all data that depends on it, either directly or indirectly.
///
/// The failure mechanism containing .
/// The profile residing in
/// that should be removed.
/// All observable objects affected by this method.
/// Thrown when
/// or is null.
public static IEnumerable RemoveDikeProfile(GrassCoverErosionInwardsFailureMechanism failureMechanism, DikeProfile profile)
{
if (failureMechanism == null)
{
throw new ArgumentNullException("failureMechanism");
}
if (profile == null)
{
throw new ArgumentNullException("profile");
}
var changedObservables = new HashSet();
GrassCoverErosionInwardsCalculation[] calculations = failureMechanism.Calculations
.Cast()
.ToArray();
GrassCoverErosionInwardsCalculation[] calculationWithRemovedDikeProfile = calculations
.Where(c => ReferenceEquals(c.InputParameters.DikeProfile, profile))
.ToArray();
foreach (GrassCoverErosionInwardsCalculation calculation in calculationWithRemovedDikeProfile)
{
calculation.InputParameters.DikeProfile = null;
IEnumerable changedSectionResults =
GrassCoverErosionInwardsHelper.Delete(failureMechanism.SectionResults, calculation, calculations);
foreach (GrassCoverErosionInwardsFailureMechanismSectionResult result in changedSectionResults)
{
changedObservables.Add(result);
}
changedObservables.Add(calculation.InputParameters);
}
failureMechanism.DikeProfiles.Remove(profile);
changedObservables.Add(failureMechanism.DikeProfiles);
return changedObservables;
}
private static IEnumerable ClearReferenceLineDependentData(IFailureMechanism failureMechanism)
{
failureMechanism.ClearAllSections();
return new[]
{
failureMechanism
};
}
private static IEnumerable OnWaveConditionsInputForeshoreProfileRemoved(ForeshoreProfile profile, WaveConditionsInput[] calculationInputs)
{
var changedObservables = new List();
foreach (WaveConditionsInput input in calculationInputs.Where(input => ReferenceEquals(input.ForeshoreProfile, profile)))
{
input.ForeshoreProfile = null;
changedObservables.Add(input);
}
return changedObservables;
}
}
}