// 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 Application.Ringtoets.Storage.DbContext; using Core.Common.Utils; using Ringtoets.ClosingStructures.Data; using Ringtoets.Common.Data.DikeProfiles; using Ringtoets.Common.Data.FailureMechanism; using Ringtoets.Common.Data.Hydraulics; using Ringtoets.Common.Data.Structures; using Ringtoets.DuneErosion.Data; using Ringtoets.GrassCoverErosionInwards.Data; using Ringtoets.HeightStructures.Data; using Ringtoets.Piping.Data; using Ringtoets.Piping.Primitives; using Ringtoets.StabilityPointStructures.Data; namespace Application.Ringtoets.Storage.Create { /// /// This class can be used to keep track of create and update operations on a database. /// This information can be used to reuse objects. When all operations have been performed, /// then the collected information can be used to transfer the ids assigned to the created /// database instances back to the data model or to clean up orphans. /// internal class PersistenceRegistry { private readonly Dictionary failureMechanismSections = CreateDictionary(); private readonly Dictionary dikeProfiles = CreateDictionary(); private readonly Dictionary foreshoreProfiles = CreateDictionary(); private readonly Dictionary grassCoverErosionInwardsCalculations = CreateDictionary(); private readonly Dictionary stochasticSoilModels = CreateDictionary(); private readonly Dictionary stochasticSoilProfiles = CreateDictionary(); private readonly Dictionary soilProfiles = CreateDictionary(); private readonly Dictionary surfaceLines = CreateDictionary(); private readonly Dictionary hydraulicLocations = CreateDictionary(); private readonly Dictionary duneLocations = CreateDictionary(); private readonly Dictionary heightStructures = CreateDictionary(); private readonly Dictionary closingStructures = CreateDictionary(); private readonly Dictionary stabilityPointStructures = CreateDictionary(); private readonly Dictionary> heightStructuresCalculations = CreateDictionary>(); private readonly Dictionary> closingStructuresCalculations = CreateDictionary>(); private readonly Dictionary> stabilityPointStructuresCalculations = CreateDictionary>(); private static Dictionary CreateDictionary() { return new Dictionary(new ReferenceEqualityComparer()); } private bool ContainsValue(Dictionary collection, TModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } return collection.Values.Contains(model, new ReferenceEqualityComparer()); } private void Register(Dictionary collection, TEntity entity, TModel model) { if (entity == null) { throw new ArgumentNullException(nameof(entity)); } if (model == null) { throw new ArgumentNullException(nameof(model)); } collection[entity] = model; } private TEntity Get(Dictionary collection, TModel model) { if (model == null) { throw new ArgumentNullException(nameof(model)); } return collection.Keys.Single(k => ReferenceEquals(collection[k], model)); } #region Register Methods /// /// Registers a create operation for and the /// that was constructed with the information. /// /// The /// to be registered. /// The to /// be registered. /// Thrown when either: /// /// is null /// is null /// internal void Register(GrassCoverErosionInwardsCalculationEntity entity, GrassCoverErosionInwardsCalculation model) { Register(grassCoverErosionInwardsCalculations, entity, model); } /// /// Registers a create operation for and the /// that was constructed with the information. /// /// The to be registered. /// The to be registered. /// Thrown when either: /// /// is null /// is null /// internal void Register(FailureMechanismSectionEntity entity, FailureMechanismSection model) { Register(failureMechanismSections, entity, model); } /// /// Registers a create operation for and the /// that was constructed with the information. /// /// The to be registered. /// The to be registered. /// Thrown when either: /// /// is null /// is null /// internal void Register(DikeProfileEntity entity, DikeProfile model) { Register(dikeProfiles, entity, model); } /// /// Registers a create operation for and the /// that was constructed with the information. /// /// The to be registered. /// The to be registered. /// Thrown when either: /// /// is null /// is null /// internal void Register(ForeshoreProfileEntity entity, ForeshoreProfile model) { Register(foreshoreProfiles, entity, model); } /// /// Registers a create operation for and the /// that was constructed with the information. /// /// The to be registered. /// The to be registered. /// Thrown when either: /// /// is null /// is null /// internal void Register(HydraulicLocationEntity entity, HydraulicBoundaryLocation model) { Register(hydraulicLocations, entity, model); } /// /// Registers a create operation for and the /// that was constructed with the information. /// /// The to be registered. /// The to be registered. /// Thrown when either: /// /// is null /// is null /// internal void Register(GrassCoverErosionOutwardsHydraulicLocationEntity entity, HydraulicBoundaryLocation model) { Register(hydraulicLocations, entity, model); } /// /// Registers a create operation for and the /// that was constructed with the information. /// /// The to be registered. /// The to be registered. /// Thrown when either: /// /// is null /// is null /// internal void Register(DuneLocationEntity entity, DuneLocation model) { Register(duneLocations, entity, model); } /// /// Registers a create operation for and the /// that was constructed with the information. /// /// The to be registered. /// The to be registered. /// Thrown when either: /// /// is null /// is null /// internal void Register(StochasticSoilModelEntity entity, StochasticSoilModel model) { Register(stochasticSoilModels, entity, model); } /// /// Registers a create operation for and the /// that was constructed with the information. /// /// The to be registered. /// The to be registered. /// Thrown when either: /// /// is null /// is null /// internal void Register(StochasticSoilProfileEntity entity, StochasticSoilProfile model) { Register(stochasticSoilProfiles, entity, model); } /// /// Registers a create operation for and the /// that was constructed with the information. /// /// The to be registered. /// The to be registered. /// Thrown when either: /// /// is null /// is null /// internal void Register(SoilProfileEntity entity, PipingSoilProfile model) { Register(soilProfiles, entity, model); } /// /// Registers a create operation for and the /// that was constructed with the information. /// /// The to be registered. /// The to be registered. /// Thrown when either: /// /// is null /// is null /// internal void Register(SurfaceLineEntity entity, PipingSurfaceLine model) { Register(surfaceLines, entity, model); } /// /// Registers a create operation for and the /// that was constructed with the information. /// /// The to be registered. /// The to be registered. /// Thrown when either: /// /// is null /// is null /// internal void Register(HeightStructureEntity entity, HeightStructure model) { Register(heightStructures, entity, model); } /// /// Registers a create operation for and the /// that was constructed with the information. /// /// The to be registered. /// The to be registered. /// Thrown when either: /// /// is null /// is null /// internal void Register(ClosingStructureEntity entity, ClosingStructure model) { Register(closingStructures, entity, model); } /// /// Registers a create operation for and the /// that was constructed with the information. /// /// The to be registered. /// The to be registered. /// Thrown when either: /// /// is null /// is null /// internal void Register(StabilityPointStructureEntity entity, StabilityPointStructure model) { Register(stabilityPointStructures, entity, model); } /// /// Registers a create operation for and the /// that was constructed with the information. /// /// The /// to be registered. /// The to /// be registered. /// Thrown when any input parameter is null. internal void Register(HeightStructuresCalculationEntity entity, StructuresCalculation model) { Register(heightStructuresCalculations, entity, model); } /// /// Registers a create operation for and the /// that was constructed with the information. /// /// The /// to be registered. /// The to /// be registered. /// Thrown when any input parameter is null. internal void Register(ClosingStructuresCalculationEntity entity, StructuresCalculation model) { Register(closingStructuresCalculations, entity, model); } /// /// Registers a create operation for and the /// that was constructed with the information. /// /// The /// to be registered. /// The to /// be registered. /// Thrown when any input parameter is null. internal void Register(StabilityPointStructuresCalculationEntity entity, StructuresCalculation model) { Register(stabilityPointStructuresCalculations, entity, model); } #endregion #region Contains Methods /// /// Checks whether a create operations has been registered for the given . /// /// The to check for. /// true if the was registered before, false otherwise. /// Thrown when is null. internal bool Contains(StochasticSoilModel model) { return ContainsValue(stochasticSoilModels, model); } /// /// Checks whether a create operations has been registered for the given . /// /// The to check for. /// true if the was registered before, false otherwise. /// Thrown when is null. internal bool Contains(StochasticSoilProfile model) { return ContainsValue(stochasticSoilProfiles, model); } /// /// Checks whether a create operations has been registered for the given . /// /// The to check for. /// true if the was registered before, false otherwise. /// Thrown when is null. internal bool Contains(PipingSoilProfile model) { return ContainsValue(soilProfiles, model); } /// /// Checks whether a create operations has been registered for the given . /// /// The to check for. /// true if the was registered before, false otherwise. /// Thrown when is null. internal bool Contains(HydraulicBoundaryLocation model) { return ContainsValue(hydraulicLocations, model); } /// /// Checks whether a create operations has been registered for the given . /// /// The to check for. /// true if the was registered before, false otherwise. /// Thrown when is null. internal bool Contains(DuneLocation model) { return ContainsValue(duneLocations, model); } /// /// Checks whether a create operations has been registered for the given . /// /// The to check for. /// true if the was registered before, false otherwise. /// Thrown when is null. internal bool Contains(PipingSurfaceLine model) { return ContainsValue(surfaceLines, model); } /// /// Checks whether a create operations has been registered for the given . /// /// The to check for. /// true if the was registered before, false otherwise. /// Thrown when is null. internal bool Contains(FailureMechanismSection model) { return ContainsValue(failureMechanismSections, model); } /// /// Checks whether a create operations has been registered for the given . /// /// The to check for. /// true if the was registered before, false otherwise. /// Thrown when is null. internal bool Contains(DikeProfile model) { return ContainsValue(dikeProfiles, model); } /// /// Checks whether a create operations has been registered for the given . /// /// The to check for. /// true if the was registered before, false otherwise. /// Thrown when is null. internal bool Contains(ForeshoreProfile model) { return ContainsValue(foreshoreProfiles, model); } /// /// Checks whether a create operations has been registered for the given . /// /// The to check for. /// true if the was registered before, false otherwise. /// Thrown when is null. internal bool Contains(HeightStructure model) { return ContainsValue(heightStructures, model); } /// /// Checks whether a create operations has been registered for the given . /// /// The to check for. /// true if the was registered before, false otherwise. /// Thrown when is null. internal bool Contains(GrassCoverErosionInwardsCalculation model) { return ContainsValue(grassCoverErosionInwardsCalculations, model); } /// /// Checks whether a create operations has been registered for the given . /// /// The to check for. /// true if the was registered before, false otherwise. /// Thrown when is null. internal bool Contains(ClosingStructure model) { return ContainsValue(closingStructures, model); } /// /// Checks whether a create operations has been registered for the given . /// /// The to check for. /// true if the was registered before, false otherwise. /// Thrown when is null. internal bool Contains(StabilityPointStructure model) { return ContainsValue(stabilityPointStructures, model); } /// /// Checks whether a create operations has been registered for the given . /// /// The to check for. /// true if the was registered before, false otherwise. /// Thrown when is null. internal bool Contains(StructuresCalculation model) { return ContainsValue(heightStructuresCalculations, model); } /// /// Checks whether a create operations has been registered for the given . /// /// The to check for. /// true if the was registered before, false otherwise. /// Thrown when is null. internal bool Contains(StructuresCalculation model) { return ContainsValue(closingStructuresCalculations, model); } /// /// Checks whether a create operations has been registered for the given . /// /// The to check for. /// true if the was registered before, false otherwise. /// Thrown when is null. internal bool Contains(StructuresCalculation model) { return ContainsValue(stabilityPointStructuresCalculations, model); } #endregion #region Get Methods /// /// Obtains the which was registered for the /// given . /// /// The for which a read operation /// has been registered. /// The constructed . /// Thrown when is null. /// Thrown when no create operation /// has been registered for . /// Use to find out whether a create /// operation has been registered for . public ForeshoreProfileEntity Get(ForeshoreProfile model) { return Get(foreshoreProfiles, model); } /// /// Obtains the which was registered for /// the given . /// /// The for which a create /// operation has been registered. /// The created . /// Thrown when is null. /// Thrown when no create operation /// has been registered for . /// Use to find out whether /// a create operation has been registered for . internal StochasticSoilModelEntity Get(StochasticSoilModel model) { return Get(stochasticSoilModels, model); } /// /// Obtains the which was registered for /// the given . /// /// The for which a create /// operation has been registered. /// The created . /// Thrown when is null. /// Thrown when no create operation /// has been registered for . /// Use to find out whether /// a create/create operation has been registered for . internal StochasticSoilProfileEntity Get(StochasticSoilProfile model) { return Get(stochasticSoilProfiles, model); } /// /// Obtains the which was registered for the given /// . /// /// The for which a create /// operation has been registered. /// The constructed . /// Thrown when is null. /// Thrown when no create operation /// has been registered for . /// Use to find out whether a /// create operation has been registered for . internal SoilProfileEntity Get(PipingSoilProfile model) { return Get(soilProfiles, model); } /// /// Obtains the which was registered for the given /// . /// /// The for which a /// read operation has been registered. /// The constructed . /// Thrown when is null. /// Thrown when no create operation /// has been registered for . /// Use to find out /// whether a create operation has been registered for . internal SurfaceLineEntity Get(PipingSurfaceLine model) { return Get(surfaceLines, model); } /// /// Obtains the which was registered for the /// given . /// /// The for which a /// read operation has been registered. /// The constructed . /// Thrown when is null. /// Thrown when no create operation /// has been registered for . /// Use to find out /// whether a create operation has been registered for . internal T Get(HydraulicBoundaryLocation model) where T : class { return Get(hydraulicLocations, model) as T; } /// /// Obtains the which was registered for the /// given . /// /// The for which a /// read operation has been registered. /// The constructed . /// Thrown when is null. /// Thrown when no create operation /// has been registered for . /// Use to find out /// whether a create operation has been registered for . internal DuneLocationEntity Get(DuneLocation model) { return Get(duneLocations, model); } /// /// Obtains the which was registered for the /// given . /// /// The for which a /// read operation has been registered. /// The constructed . /// Thrown when is null. /// Thrown when no create operation /// has been registered for . /// Use to find out /// whether a create operation has been registered for . internal FailureMechanismSectionEntity Get(FailureMechanismSection model) { return Get(failureMechanismSections, model); } /// /// Obtains the which was registered for the /// given . /// /// The for which a read operation /// has been registered. /// The constructed . /// Thrown when is null. /// Thrown when no create operation /// has been registered for . /// Use to find out whether a create /// operation has been registered for . internal DikeProfileEntity Get(DikeProfile model) { return Get(dikeProfiles, model); } /// /// Obtains the which was /// registered for the given . /// /// The for /// which a read operation has been registered. /// The constructed . /// Thrown when is null. /// Thrown when no create operation /// has been registered for . /// Use to find out /// whether a create operation has been registered for . internal GrassCoverErosionInwardsCalculationEntity Get(GrassCoverErosionInwardsCalculation model) { return Get(grassCoverErosionInwardsCalculations, model); } /// /// Obtains the which was registered for the /// given . /// /// The for which a read operation has been registered. /// The constructed . /// Thrown when is null. /// Thrown when no create operation /// has been registered for . /// Use to find out whether a /// create operation has been registered for . internal HeightStructureEntity Get(HeightStructure model) { return Get(heightStructures, model); } /// /// Obtains the which was registered for the /// given . /// /// The for which a read operation has been registered. /// The constructed . /// Thrown when is null. /// Thrown when no create operation /// has been registered for . /// Use to find out whether a /// create operation has been registered for . internal ClosingStructureEntity Get(ClosingStructure model) { return Get(closingStructures, model); } /// /// Obtains the which was registered for the /// given . /// /// The for which a read operation has been registered. /// The constructed . /// Thrown when is null. /// Thrown when no create operation /// has been registered for . /// Use to find out whether a /// create operation has been registered for . internal StabilityPointStructureEntity Get(StabilityPointStructure model) { return Get(stabilityPointStructures, model); } /// /// Obtains the which was /// registered for the given . /// /// The for /// which a read operation has been registered. /// The constructed . /// Thrown when is null. /// Thrown when no create operation /// has been registered for . /// Use to find out /// whether a create operation has been registered for . internal HeightStructuresCalculationEntity Get(StructuresCalculation model) { return Get(heightStructuresCalculations, model); } /// /// Obtains the which was /// registered for the given . /// /// The for /// which a read operation has been registered. /// The constructed . /// Thrown when is null. /// Thrown when no create operation /// has been registered for . /// Use to find out /// whether a create operation has been registered for . internal ClosingStructuresCalculationEntity Get(StructuresCalculation model) { return Get(closingStructuresCalculations, model); } /// /// Obtains the which was /// registered for the given . /// /// The for /// which a read operation has been registered. /// The constructed . /// Thrown when is null. /// Thrown when no create operation /// has been registered for . /// Use to find out /// whether a create operation has been registered for . internal StabilityPointStructuresCalculationEntity Get(StructuresCalculation model) { return Get(stabilityPointStructuresCalculations, model); } #endregion } }