// 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.Utils;
using Ringtoets.Common.Data.DikeProfiles;
using Ringtoets.Common.Data.FailureMechanism;
using Ringtoets.GrassCoverErosionInwards.Data;
using Ringtoets.HydraRing.Data;
using Ringtoets.Piping.Data;
using Ringtoets.Piping.Primitives;
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 grassCoverErosionInwardsCalculations = CreateDictionary();
private readonly Dictionary hydraulicLocations = CreateDictionary();
private readonly Dictionary stochasticSoilModels = CreateDictionary();
private readonly Dictionary stochasticSoilProfiles = CreateDictionary();
private readonly Dictionary soilProfiles = CreateDictionary();
private readonly Dictionary surfaceLines = CreateDictionary();
///
/// Registers a create or update 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 or update 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 or update 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 or update 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 or update 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 or update 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 or update 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 or update 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, RingtoetsPipingSurfaceLine model)
{
Register(surfaceLines, entity, model);
}
///
/// Checks whether a create or update 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 or update 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 or update 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 or update 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 or update 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(RingtoetsPipingSurfaceLine model)
{
return ContainsValue(surfaceLines, model);
}
///
/// Checks whether a create or update 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 or update 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 or update 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);
}
///
/// Obtains the which was registered for
/// the given .
///
/// The for which a create/update
/// operation has been registered.
/// The created .
/// Thrown when is null.
/// Thrown when no create/update operation
/// has been registered for .
/// Use to find out whether
/// a create/update 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/update
/// operation has been registered.
/// The created .
/// Thrown when is null.
/// Thrown when no create/update 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/update
/// operation has been registered.
/// The constructed .
/// Thrown when is null.
/// Thrown when no create/update operation
/// has been registered for .
/// Use to find out whether a
/// create/update 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/update operation has been registered.
/// The constructed .
/// Thrown when is null.
/// Thrown when no create/update operation
/// has been registered for .
/// Use to find out
/// whether a create/update operation has been registered for .
internal SurfaceLineEntity Get(RingtoetsPipingSurfaceLine model)
{
return Get(surfaceLines, model);
}
///
/// Obtains the which was registered for the
/// given .
///
/// The for which a
/// read/update operation has been registered.
/// The constructed .
/// Thrown when is null.
/// Thrown when no create/update operation
/// has been registered for .
/// Use to find out
/// whether a create/update operation has been registered for .
internal HydraulicLocationEntity Get(HydraulicBoundaryLocation model)
{
return Get(hydraulicLocations, model);
}
///
/// Obtains the which was registered for the
/// given .
///
/// The for which a
/// read/update operation has been registered.
/// The constructed .
/// Thrown when is null.
/// Thrown when no create/update operation
/// has been registered for .
/// Use to find out
/// whether a create/update 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/update operation has been registered.
/// The constructed .
/// Thrown when is null.
/// Thrown when no create/update operation
/// has been registered for .
/// Use to find out
/// whether a create/update 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/update operation has been registered.
/// The constructed .
/// Thrown when is null.
/// Thrown when no create/update operation
/// has been registered for .
/// Use to find out
/// whether a create/update operation has been registered for .
internal GrassCoverErosionInwardsCalculationEntity Get(GrassCoverErosionInwardsCalculation model)
{
return Get(grassCoverErosionInwardsCalculations, model);
}
private static Dictionary CreateDictionary()
{
return new Dictionary(new ReferenceEqualityComparer());
}
private bool ContainsValue(Dictionary collection, TModel model)
{
if (model == null)
{
throw new ArgumentNullException("model");
}
return collection.ContainsValue(model);
}
private void Register(Dictionary collection, TEntity entity, TModel model)
{
if (entity == null)
{
throw new ArgumentNullException("entity");
}
if (model == null)
{
throw new ArgumentNullException("model");
}
collection[entity] = model;
}
private TEntity Get(Dictionary collection, TModel model)
{
if (model == null)
{
throw new ArgumentNullException("model");
}
return collection.Keys.Single(k => ReferenceEquals(collection[k], model));
}
}
}