// 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.Data.Entity;
using System.Linq;
using Application.Ringtoets.Storage.Converters;
using Application.Ringtoets.Storage.DbContext;
using Application.Ringtoets.Storage.Exceptions;
using Application.Ringtoets.Storage.Properties;
using Ringtoets.Common.Data.FailureMechanism;
namespace Application.Ringtoets.Storage.Persistors
{
///
/// Persistor for classes which implement .
///
public abstract class FailureMechanismPersistorBase where T : IFailureMechanism
{
private readonly DbSet failureMechanismSet;
private readonly Dictionary insertedList = new Dictionary();
private readonly ICollection modifiedList = new List();
private readonly IEntityConverter converter;
///
/// New instance of .
///
/// The storage context.
/// An implementation of the to use in the persistor.
/// Thrown when is null.
protected FailureMechanismPersistorBase(IRingtoetsEntities ringtoetsContext, IEntityConverter converter)
{
if (ringtoetsContext == null)
{
throw new ArgumentNullException("ringtoetsContext");
}
failureMechanismSet = ringtoetsContext.FailureMechanismEntities;
this.converter = converter;
}
///
/// Loads the as .
///
/// to load from.
/// The to load data in.
/// Thrown when:
/// - is null.
/// - is null.
///
public void LoadModel(FailureMechanismEntity entity, T failureMechanism)
{
if (entity == null)
{
throw new ArgumentNullException("entity");
}
if (failureMechanism == null)
{
throw new ArgumentNullException("failureMechanism");
}
var model = converter.ConvertEntityToModel(entity);
failureMechanism.StorageId = model.StorageId;
LoadChildren(failureMechanism, entity);
}
///
/// Implement to provide a way to load the children of the as children of .
///
/// The to load into.
/// The to load from.
protected abstract void LoadChildren(T model, FailureMechanismEntity entity);
///
/// Ensures that the is set as in the .
///
/// Collection where objects can be searched and added. Usually, this collection is a navigation property of a .
/// to be saved in the storage.
/// Thrown when:
/// - is null.
/// - is null.
///
/// Thrown when the is read-only.
/// Thrown when the storageId of > 0 and:
/// - More than one element found in that should have been unique.
/// - No such element exists in .
///
public void UpdateModel(ICollection parentNavigationProperty, T model)
{
if (model == null)
{
throw new ArgumentNullException("model");
}
if (parentNavigationProperty == null)
{
throw new ArgumentNullException("parentNavigationProperty");
}
if (model.StorageId < 1)
{
InsertModel(parentNavigationProperty, model);
return;
}
FailureMechanismEntity entity;
try
{
entity = parentNavigationProperty.SingleOrDefault(db => db.FailureMechanismEntityId == model.StorageId);
}
catch (InvalidOperationException exception)
{
throw new EntityNotFoundException(String.Format(Resources.Error_Entity_Not_Found_0_1, "FailureMechanismEntity", model.StorageId), exception);
}
if (entity == null)
{
throw new EntityNotFoundException(String.Format(Resources.Error_Entity_Not_Found_0_1, "FailureMechanismEntity", model.StorageId));
}
modifiedList.Add(entity);
converter.ConvertModelToEntity(model, entity);
UpdateChildren(model, entity);
}
///
/// Implement to provide a way to update the children of the with data from .
///
/// The for which to use the data to update the .
/// The to update.
protected abstract void UpdateChildren(T model, FailureMechanismEntity entity);
///
/// Ensures that the model is added as in the .
///
/// Collection where objects can be added. Usually, this collection is a navigation property of a .
/// to be saved in the storage.
/// Thrown when:
/// - is null.
///
public void InsertModel(ICollection parentNavigationProperty, T model)
{
if (parentNavigationProperty == null)
{
throw new ArgumentNullException("parentNavigationProperty");
}
var entity = new FailureMechanismEntity();
parentNavigationProperty.Add(entity);
insertedList.Add(entity, model);
converter.ConvertModelToEntity(model, entity);
if (model.StorageId > 0)
{
modifiedList.Add(entity);
}
InsertChildren(model, entity);
}
///
/// Implement to provide a way to insert the children of the into the .
///
/// The for which to use the data to update the .
/// The to update.
protected abstract void InsertChildren(T model, FailureMechanismEntity entity);
///
/// All unmodified in will be removed.
///
/// List where objects can be searched. Usually, this collection is a navigation property of a .
/// Thrown when the is read-only.
public void RemoveUnModifiedEntries(ICollection parentNavigationProperty)
{
var untouchedModifiedList = parentNavigationProperty.Where(e => e.FailureMechanismEntityId > 0 && !modifiedList.Contains(e));
failureMechanismSet.RemoveRange(untouchedModifiedList);
modifiedList.Clear();
}
///
/// Perform actions that can only be executed after has been called.
///
public void PerformPostSaveActions()
{
foreach (var entry in insertedList)
{
entry.Value.StorageId = entry.Key.FailureMechanismEntityId;
}
insertedList.Clear();
PerformChildPostSaveAction();
}
///
/// Implement to provide a way to perform post save actions on the children of the .
///
protected abstract void PerformChildPostSaveAction();
}
}