// 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 Application.Ringtoets.Storage.DbContext; using Core.Common.Base.Data; using Core.Common.Utils; using Ringtoets.Common.Data.FailureMechanism; using Ringtoets.HydraRing.Data; using Ringtoets.Integration.Data; using Ringtoets.Piping.Data; using Ringtoets.Piping.Primitives; namespace Application.Ringtoets.Storage.Create { /// /// This class can be used to keep track of create operations on a database. This information can used to reuse previously created /// 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. /// public class CreateConversionCollector { private readonly Dictionary projects = new Dictionary(new ReferenceEqualityComparer()); private readonly Dictionary assessmentSections = new Dictionary(new ReferenceEqualityComparer()); private readonly Dictionary failureMechanisms = new Dictionary(new ReferenceEqualityComparer()); private readonly Dictionary failureMechanismSections = new Dictionary(); private readonly Dictionary hydraulicLocations = new Dictionary(new ReferenceEqualityComparer()); private readonly Dictionary stochasticSoilModels = new Dictionary(new ReferenceEqualityComparer()); private readonly Dictionary stochasticSoilProfiles = new Dictionary(new ReferenceEqualityComparer()); private readonly Dictionary soilProfiles = new Dictionary(new ReferenceEqualityComparer()); private readonly Dictionary soilLayers = new Dictionary(new ReferenceEqualityComparer()); /// /// Registers a create operation for and the that /// was constructed with the information. /// /// The that was constructed. /// The new which needed to be created. /// Thrown when either: /// /// is null /// is null /// internal void Create(ProjectEntity entity, Project model) { Create(projects, entity, model); } /// /// Registers a create operation for and the that /// was constructed with the information. /// /// The that was constructed. /// The new which needed to be created. /// Thrown when either: /// /// is null /// is null /// internal void Create(AssessmentSectionEntity entity, AssessmentSection model) { Create(assessmentSections, entity, model); } /// /// Registers a create operation for and the that /// was constructed with the information. /// /// The that was constructed. /// The new which needed to be created. /// Thrown when either: /// /// is null /// is null /// internal void Create(HydraulicLocationEntity entity, HydraulicBoundaryLocation model) { Create(hydraulicLocations, entity, model); } /// /// Registers a create operation for and the that /// was constructed with the information. /// /// The that was constructed. /// The new which needed to be created. /// Thrown when either: /// /// is null /// is null /// internal void Create(FailureMechanismEntity entity, FailureMechanismBase model) { Create(failureMechanisms, entity, model); } /// /// Registers a create operation for and the that /// was constructed with the information. /// /// The that was constructed. /// The new which needed to be created. /// Thrown when either: /// /// is null /// is null /// public void Create(FailureMechanismSectionEntity entity, FailureMechanismSection model) { Create(failureMechanismSections, entity, model); } /// /// Registers a create operation for and the that /// was constructed with the information. /// /// The that was constructed. /// The new which needed to be created. /// Thrown when either: /// /// is null /// is null /// internal void Create(StochasticSoilModelEntity entity, StochasticSoilModel model) { Create(stochasticSoilModels, entity, model); } /// /// Registers a create operation for and the that /// was constructed with the information. /// /// The that was constructed. /// The new which needed to be created. /// Thrown when either: /// /// is null /// is null /// internal void Create(StochasticSoilProfileEntity entity, StochasticSoilProfile model) { Create(stochasticSoilProfiles, entity, model); } /// /// Registers a create operation for and the that /// was constructed with the information. /// /// The that was constructed. /// The new which needed to be created. /// Thrown when either: /// /// is null /// is null /// internal void Create(SoilProfileEntity entity, PipingSoilProfile model) { Create(soilProfiles, entity, model); } /// /// Registers a create operation for and the that /// was constructed with the information. /// /// The that was constructed. /// The new which needed to be created. /// Thrown when either: /// /// is null /// is null /// internal void Create(SoilLayerEntity entity, PipingSoilLayer model) { Create(soilLayers, entity, model); } /// /// Checks whether a create operations has been registered for the given . /// /// The to check for. /// true if the was created before, false otherwise. /// Thrown when is null. internal bool Contains(PipingSoilProfile model) { return ContainsValue(soilProfiles, model); } /// /// Obtains the which was created for the given . /// /// The for which a read operation has been registerd. /// 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); } /// /// Transfer ids from the created entities to the domain model objects' property. /// internal void TransferIds() { foreach (var entity in projects.Keys) { projects[entity].StorageId = entity.ProjectEntityId; } foreach (var entity in failureMechanisms.Keys) { failureMechanisms[entity].StorageId = entity.FailureMechanismEntityId; } foreach (var entity in failureMechanismSections.Keys) { failureMechanismSections[entity].StorageId = entity.FailureMechanismSectionEntityId; } foreach (var entity in assessmentSections.Keys) { assessmentSections[entity].StorageId = entity.AssessmentSectionEntityId; } foreach (var entity in hydraulicLocations.Keys) { hydraulicLocations[entity].StorageId = entity.HydraulicLocationEntityId; } foreach (var entity in stochasticSoilModels.Keys) { stochasticSoilModels[entity].StorageId = entity.StochasticSoilModelEntityId; } foreach (var entity in stochasticSoilProfiles.Keys) { stochasticSoilProfiles[entity].StorageId = entity.StochasticSoilProfileEntityId; } foreach (var entity in soilProfiles.Keys) { soilProfiles[entity].StorageId = entity.SoilProfileEntityId; } foreach (var entity in soilLayers.Keys) { soilLayers[entity].StorageId = entity.SoilLayerEntityId; } } private bool ContainsValue(Dictionary collection, U model) { if (model == null) { throw new ArgumentNullException("model"); } return collection.ContainsValue(model); } private void Create(Dictionary collection, T entity, U model) { if (entity == null) { throw new ArgumentNullException("entity"); } if (model == null) { throw new ArgumentNullException("model"); } collection[entity] = model; } private T Get(Dictionary collection, U model) { if (model == null) { throw new ArgumentNullException("model"); } return collection.Keys.Single(k => ReferenceEquals(collection[k], model)); } } }