// 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.Create; using Application.Ringtoets.Storage.DbContext; using Core.Common.Utils; namespace Application.Ringtoets.Storage.Update { /// /// Class that can be used to keep track of data model objects which were potentially created or updated during an update /// operation. Can be used to reuse objects when assigning an already created domain model object or to remove entities /// from the database which were not potentialy modified. /// public class UpdateConversionCollector : CreateConversionCollector { private readonly HashSet projects = new HashSet(new ReferenceEqualityComparer()); private readonly HashSet assessmentSections = new HashSet(new ReferenceEqualityComparer()); private readonly HashSet failureMechanisms = new HashSet(new ReferenceEqualityComparer()); private readonly HashSet failureMechanismSections = new HashSet(new ReferenceEqualityComparer()); private readonly HashSet hydraulicLocations = new HashSet(new ReferenceEqualityComparer()); private readonly HashSet stochasticSoilModels = new HashSet(new ReferenceEqualityComparer()); private readonly HashSet stochasticSoilProfiles = new HashSet(new ReferenceEqualityComparer()); private readonly HashSet soilProfiles = new HashSet(new ReferenceEqualityComparer()); private readonly HashSet soilLayers = new HashSet(new ReferenceEqualityComparer()); /// /// Registers an update operation for . /// /// The that was updated. /// Thrown when is null internal void Update(ProjectEntity entity) { Update(entity, projects); } /// /// Registers an update operation for . /// /// The that was updated. /// Thrown when is null internal void Update(AssessmentSectionEntity entity) { Update(entity, assessmentSections); } /// /// Registers an update operation for . /// /// The that was updated. /// Thrown when is null internal void Update(FailureMechanismEntity entity) { Update(entity, failureMechanisms); } /// /// Registers an update operation for . /// /// The that was updated. /// Thrown when is null public void Update(FailureMechanismSectionEntity entity) { Update(entity, failureMechanismSections); } /// /// Registers an update operation for . /// /// The that was updated. /// Thrown when is null internal void Update(HydraulicLocationEntity entity) { Update(entity, hydraulicLocations); } /// /// Registers an update operation for . /// /// The that was updated. /// Thrown when is null internal void Update(StochasticSoilModelEntity entity) { Update(entity, stochasticSoilModels); } /// /// Registers an update operation for . /// /// The that was updated. /// Thrown when is null internal void Update(StochasticSoilProfileEntity entity) { Update(entity, stochasticSoilProfiles); } /// /// Registers an update operation for . /// /// The that was updated. /// Thrown when is null internal void Update(SoilProfileEntity entity) { Update(entity, soilProfiles); } /// /// Registers an update operation for . /// /// The that was updated. /// Thrown when is null internal void Update(SoilLayerEntity entity) { Update(entity, soilLayers); } /// /// Removes all the entities for which no update operation was registered from the . /// /// The from which to remove the entities. internal void RemoveUntouched(IRingtoetsEntities dbContext) { var projectEntities = dbContext.ProjectEntities; var projectEntitiesToRemove = projectEntities .Local .Where(entity => entity.ProjectEntityId > 0) .Except(projects); projectEntities.RemoveRange(projectEntitiesToRemove); var assessmentSectionEntities = dbContext.AssessmentSectionEntities; var assessmentSectionEntitiesToRemove = assessmentSectionEntities .Local .Where(entity => entity.AssessmentSectionEntityId > 0) .Except(assessmentSections); assessmentSectionEntities.RemoveRange(assessmentSectionEntitiesToRemove); var failureMechanismEntities = dbContext.FailureMechanismEntities; var failureMechanismEntitiesToRemove = failureMechanismEntities .Local .Where(entity => entity.FailureMechanismEntityId > 0) .Except(failureMechanisms); failureMechanismEntities.RemoveRange(failureMechanismEntitiesToRemove); var failureMechanismSectionEntities = dbContext.FailureMechanismSectionEntities; var failureMechanismSectionEntitiesToRemove = failureMechanismSectionEntities .Local .Where(entity => entity.FailureMechanismSectionEntityId > 0) .Except(failureMechanismSections); failureMechanismSectionEntities.RemoveRange(failureMechanismSectionEntitiesToRemove); var hydraulicLocationEntities = dbContext.HydraulicLocationEntities; var hydraulicLocationEntitiesToRemove = hydraulicLocationEntities .Local .Where(entity => entity.HydraulicLocationEntityId > 0) .Except(hydraulicLocations); hydraulicLocationEntities.RemoveRange(hydraulicLocationEntitiesToRemove); var stochasticSoilModelEntities = dbContext.StochasticSoilModelEntities; var stochasticSoilModelEntitiesToRemove = stochasticSoilModelEntities .Local .Where(entity => entity.StochasticSoilModelEntityId > 0) .Except(stochasticSoilModels); stochasticSoilModelEntities.RemoveRange(stochasticSoilModelEntitiesToRemove); var stochasticSoilProfileEntities = dbContext.StochasticSoilProfileEntities; var stochasticSoilProfileEntitiesToRemove = stochasticSoilProfileEntities .Local .Where(entity => entity.StochasticSoilProfileEntityId > 0) .Except(stochasticSoilProfiles); stochasticSoilProfileEntities.RemoveRange(stochasticSoilProfileEntitiesToRemove); var soilProfileEntities = dbContext.SoilProfileEntities; var soilProfileEntitiesToRemove = soilProfileEntities .Local .Where(entity => entity.SoilProfileEntityId > 0) .Except(soilProfiles); soilProfileEntities.RemoveRange(soilProfileEntitiesToRemove); var soilLayerEntities = dbContext.SoilLayerEntities; var soilLayerEntitiesToRemove = soilLayerEntities .Local .Where(entity => entity.SoilLayerEntityId > 0) .Except(soilLayers); soilLayerEntities.RemoveRange(soilLayerEntitiesToRemove); } private void Update(T entity, HashSet collection) { if (entity == null) { throw new ArgumentNullException("entity"); } collection.Add(entity); } } }