// 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 Ringtoets.Common.Data;
namespace Application.Ringtoets.Storage.Persistors
{
///
/// Persistor for .
///
public class ReferenceLinePersistor
{
private readonly ReferenceLineConverter converter;
private readonly DbSet referenceLineEntities;
///
/// Creates a new instance of .
///
/// The storage context.
/// Thrown when is null.
public ReferenceLinePersistor(IRingtoetsEntities ringtoetsEntities)
{
if (ringtoetsEntities == null)
{
throw new ArgumentNullException("ringtoetsEntities");
}
referenceLineEntities = ringtoetsEntities.ReferenceLinePointEntities;
converter = new ReferenceLineConverter();
}
///
/// Inserts the as points into the .
///
/// The collection where the entities are added.
/// The reference line which will be added tot the
/// as entities.
/// is null.
public void InsertModel(ICollection entityCollection, ReferenceLine referenceLine)
{
if (entityCollection == null)
{
throw new ArgumentNullException("entityCollection");
}
if (HasChanges(entityCollection, referenceLine))
{
if (entityCollection.Any())
{
referenceLineEntities.RemoveRange(entityCollection);
entityCollection.Clear();
}
if (referenceLine != null)
{
converter.ConvertModelToEntity(referenceLine, entityCollection);
}
}
}
///
/// Creates a new based on the information in .
///
/// The database entity containing the information to set on the new model.
/// A new with properties set from the database.
public ReferenceLine LoadModel(ICollection entityCollection)
{
if (entityCollection == null)
{
throw new ArgumentNullException("entityCollection");
}
return converter.ConvertEntityToModel(entityCollection);
}
private bool HasChanges(ICollection entityCollection, ReferenceLine otherLine)
{
var existingLine = converter.ConvertEntityToModel(entityCollection);
if (existingLine == null)
{
return otherLine != null;
}
if (otherLine == null)
{
return true;
}
var pointsArray = existingLine.Points.ToArray();
var otherPointsArray = otherLine.Points.ToArray();
if (pointsArray.Length != otherPointsArray.Length)
{
return true;
}
for (int i = 0; i < pointsArray.Length; i++)
{
var isXAlmostEqual = Math.Abs(pointsArray[i].X - otherPointsArray[i].X) < 1e-6;
var isYAlmostEqual = Math.Abs(pointsArray[i].Y - otherPointsArray[i].Y) < 1e-6;
if (!isXAlmostEqual || !isYAlmostEqual)
{
return true;
}
}
return false;
}
}
}