using System; using System.Collections.Generic; using System.Linq; using Application.Ringtoets.Storage.DbContext; using Core.Common.Base.Geometry; using Ringtoets.Common.Data; using Ringtoets.Common.Data.AssessmentSection; namespace Application.Ringtoets.Storage.Converters { /// /// This class is able to convert a model to a /// entity and back. /// public class ReferenceLineConverter : IEntityConverter> { /// /// Creates a new from the . /// /// Collection of containing /// the geometry of the . /// A new instance with its geometry taken from the /// . public ReferenceLine ConvertEntityToModel(ICollection entityCollection) { if (entityCollection == null) { throw new ArgumentNullException("entityCollection"); } if (!entityCollection.Any()) { return null; } var line = new ReferenceLine(); var geometry = new Point2D[entityCollection.Count]; foreach (var entity in entityCollection) { var point = new Point2D(decimal.ToDouble(entity.X), decimal.ToDouble(entity.Y)); geometry[entity.Order] = point; } line.SetGeometry(geometry); return line; } /// /// Updates the by adding the geometry from to it. /// /// The used as source. /// The target collection. public void ConvertModelToEntity(ReferenceLine modelObject, ICollection entityCollection) { if (modelObject == null) { throw new ArgumentNullException("modelObject"); } if (entityCollection == null) { throw new ArgumentNullException("entityCollection"); } foreach (Point2D point in modelObject.Points) { entityCollection.Add(new ReferenceLinePointEntity { X = Convert.ToDecimal(point.X), Y = Convert.ToDecimal(point.Y), Order = entityCollection.Count }); } } } }