// 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 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 either:
///
/// - is null
///
internal void Update(ProjectEntity entity)
{
Update(entity, projects);
}
///
/// Registers an update operation for .
///
/// The that was updated.
/// Thrown when either:
///
/// - is null
///
internal void Update(AssessmentSectionEntity entity)
{
Update(entity, assessmentSections);
}
///
/// Registers an update operation for .
///
/// The that was updated.
/// Thrown when either:
///
/// - is null
///
internal void Update(FailureMechanismEntity entity)
{
Update(entity, failureMechanisms);
}
///
/// Registers an update operation for .
///
/// The that was updated.
/// Thrown when either:
///
/// - is null
///
internal void Update(HydraulicLocationEntity entity)
{
hydraulicLocations.Add(entity);
}
///
/// Registers an update operation for .
///
/// The that was updated.
/// Thrown when either:
///
/// - is null
///
internal void Update(StochasticSoilModelEntity entity)
{
Update(entity, stochasticSoilModels);
}
///
/// Registers an update operation for .
///
/// The that was updated.
/// Thrown when either:
///
/// - is null
///
internal void Update(StochasticSoilProfileEntity entity)
{
Update(entity, stochasticSoilProfiles);
}
///
/// Registers an update operation for .
///
/// The that was updated.
/// Thrown when either:
///
/// - is null
///
internal void Update(SoilProfileEntity entity)
{
Update(entity, soilProfiles);
}
///
/// Registers an update operation for .
///
/// The that was updated.
/// Thrown when either:
///
/// - 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;
projectEntities.RemoveRange(projectEntities.Local.Except(projects));
var assessmentSectionEntities = dbContext.AssessmentSectionEntities;
assessmentSectionEntities.RemoveRange(assessmentSectionEntities.Local.Except(assessmentSections));
var failureMechanismEntities = dbContext.FailureMechanismEntities;
failureMechanismEntities.RemoveRange(failureMechanismEntities.Local.Except(failureMechanisms));
var hydraulicLocationEntities = dbContext.HydraulicLocationEntities;
hydraulicLocationEntities.RemoveRange(hydraulicLocationEntities.Local.Except(hydraulicLocations));
var stochasticSoilModelEntities = dbContext.StochasticSoilModelEntities;
stochasticSoilModelEntities.RemoveRange(stochasticSoilModelEntities.Local.Except(stochasticSoilModels));
var stochasticSoilProfileEntities = dbContext.StochasticSoilProfileEntities;
stochasticSoilProfileEntities.RemoveRange(stochasticSoilProfileEntities.Local.Except(stochasticSoilProfiles));
var soilProfileEntities = dbContext.SoilProfileEntities;
soilProfileEntities.RemoveRange(soilProfileEntities.Local.Except(soilProfiles));
var soilLayerEntities = dbContext.SoilLayerEntities;
soilLayerEntities.RemoveRange(soilLayerEntities.Local.Except(soilLayers));
}
private void Update(T entity, HashSet collection)
{
if (entity == null)
{
throw new ArgumentNullException("entity");
}
collection.Add(entity);
}
}
}