Index: Application/Ringtoets/src/Application.Ringtoets.Storage/Persistors/ReferenceLinePersistor.cs =================================================================== diff -u -r98df6e59fb589e5326b8f904dac98d402cb35b9c -rf8ff9c1004791467d458ed2818f03be0b9e86552 --- Application/Ringtoets/src/Application.Ringtoets.Storage/Persistors/ReferenceLinePersistor.cs (.../ReferenceLinePersistor.cs) (revision 98df6e59fb589e5326b8f904dac98d402cb35b9c) +++ Application/Ringtoets/src/Application.Ringtoets.Storage/Persistors/ReferenceLinePersistor.cs (.../ReferenceLinePersistor.cs) (revision f8ff9c1004791467d458ed2818f03be0b9e86552) @@ -4,6 +4,7 @@ using System.Linq; using Application.Ringtoets.Storage.Converters; using Application.Ringtoets.Storage.DbContext; +using Core.Common.Base.Geometry; using Ringtoets.Common.Data; namespace Application.Ringtoets.Storage.Persistors @@ -43,16 +44,50 @@ throw new ArgumentNullException("entityCollection"); } - if (entityCollection.Any()) + if (HasChanges(entityCollection, referenceLine)) { - referenceLineEntities.RemoveRange(entityCollection); - entityCollection.Clear(); + if (entityCollection.Any()) + { + referenceLineEntities.RemoveRange(entityCollection); + entityCollection.Clear(); + } + + if (referenceLine != null) + { + converter.ConvertModelToEntity(referenceLine, entityCollection); + } } + } - if (referenceLine != null) + private bool HasChanges(ICollection entityCollection, ReferenceLine otherLine) + { + var existingLine = converter.ConvertEntityToModel(entityCollection); + + if (existingLine == null) { - converter.ConvertModelToEntity(referenceLine, entityCollection); + 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-8; + var isYAlmostEqual = Math.Abs(pointsArray[i].Y - otherPointsArray[i].Y) < 1e-8; + if (!isXAlmostEqual || !isYAlmostEqual) + { + return true; + } + } + return false; } /// Index: Application/Ringtoets/src/Application.Ringtoets.Storage/StorageSqLite.cs =================================================================== diff -u -ra6371ac68897b69e2efd537fa29bb6564f50fdcf -rf8ff9c1004791467d458ed2818f03be0b9e86552 --- Application/Ringtoets/src/Application.Ringtoets.Storage/StorageSqLite.cs (.../StorageSqLite.cs) (revision a6371ac68897b69e2efd537fa29bb6564f50fdcf) +++ Application/Ringtoets/src/Application.Ringtoets.Storage/StorageSqLite.cs (.../StorageSqLite.cs) (revision f8ff9c1004791467d458ed2818f03be0b9e86552) @@ -20,11 +20,9 @@ // All rights reserved. using System; -using System.Collections.Generic; using System.Data; using System.Data.Entity; using System.IO; -using System.Linq; using Application.Ringtoets.Storage.DbContext; using Application.Ringtoets.Storage.Exceptions; using Application.Ringtoets.Storage.Persistors; @@ -130,8 +128,6 @@ var projectEntityPersistor = new ProjectEntityPersistor(dbContext); try { - var projectEntities = dbContext.ProjectEntities; - projectEntityPersistor.UpdateModel(project); projectEntityPersistor.RemoveUnModifiedEntries(); var changes = dbContext.SaveChanges(); @@ -208,8 +204,6 @@ try { - var projectEntities = dbContext.ProjectEntities; - projectEntityPersistor.UpdateModel(project); projectEntityPersistor.RemoveUnModifiedEntries(); return dbContext.ChangeTracker.HasChanges(); Index: Application/Ringtoets/test/Application.Ringtoets.Storage.Test/DbContext/ProjectEntityTest.cs =================================================================== diff -u -r290ba0afb9b78a0823f1fa7b4fa01c1011952df8 -rf8ff9c1004791467d458ed2818f03be0b9e86552 --- Application/Ringtoets/test/Application.Ringtoets.Storage.Test/DbContext/ProjectEntityTest.cs (.../ProjectEntityTest.cs) (revision 290ba0afb9b78a0823f1fa7b4fa01c1011952df8) +++ Application/Ringtoets/test/Application.Ringtoets.Storage.Test/DbContext/ProjectEntityTest.cs (.../ProjectEntityTest.cs) (revision f8ff9c1004791467d458ed2818f03be0b9e86552) @@ -22,7 +22,6 @@ // Setup const long expectedId = 1024L; const string someDescription = ""; - const long someTimestamp = 123456789L; var projectEntity = new ProjectEntity { Index: Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Persistors/ReferenceLinePersistorTest.cs =================================================================== diff -u -r98df6e59fb589e5326b8f904dac98d402cb35b9c -rf8ff9c1004791467d458ed2818f03be0b9e86552 --- Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Persistors/ReferenceLinePersistorTest.cs (.../ReferenceLinePersistorTest.cs) (revision 98df6e59fb589e5326b8f904dac98d402cb35b9c) +++ Application/Ringtoets/test/Application.Ringtoets.Storage.Test/Persistors/ReferenceLinePersistorTest.cs (.../ReferenceLinePersistorTest.cs) (revision f8ff9c1004791467d458ed2818f03be0b9e86552) @@ -146,6 +146,118 @@ } [Test] + public void InsertModel_EmptyCollectionNullReferenceLine_ShouldNotClearCollection() + { + // Setup + var backingList = new List(); + + var mocks = new MockRepository(); + var context = RingtoetsEntitiesHelper.Create(mocks); + var entities = mocks.StrictMock>(); + entities.Expect(e => e.GetEnumerator()).Return(backingList.GetEnumerator()); + mocks.ReplayAll(); + + var persistor = new ReferenceLinePersistor(context); + + // Call + persistor.InsertModel(entities, null); + + // Assert + mocks.VerifyAll(); + } + + [Test] + public void InsertModel_EmptyCollectionReferenceLineWithPoint_ShouldAddPointsToCollection() + { + // Setup + var entities = new List(); + + var referenceLine = new ReferenceLine(); + Point2D point = new Point2D(1.2,3.5); + referenceLine.SetGeometry(new [] + { + point + }); + + var mocks = new MockRepository(); + var context = RingtoetsEntitiesHelper.Create(mocks); + mocks.ReplayAll(); + + var persistor = new ReferenceLinePersistor(context); + + // Call + persistor.InsertModel(entities, referenceLine); + + // Assert + Assert.AreEqual(1, entities.Count); + Assert.AreEqual(point.X, entities.First().X); + Assert.AreEqual(point.Y, entities.First().Y); + Assert.AreEqual(0, entities.First().Order); + mocks.VerifyAll(); + } + + [Test] + public void InsertModel_CollectionWithPointReferenceLineWithPoint_DoesNotChangeCollection() + { + // Setup + var referenceLinePointEntity = new ReferenceLinePointEntity + { + X = 0, Y = 0, Order = 0 + }; + var entities = new List(new [] + { + referenceLinePointEntity + }); + var referenceLine = new ReferenceLine(); + Point2D point = new Point2D(0,0); + referenceLine.SetGeometry(new [] + { + point + }); + + var mocks = new MockRepository(); + var context = RingtoetsEntitiesHelper.Create(mocks); + mocks.ReplayAll(); + + var persistor = new ReferenceLinePersistor(context); + + // Call + persistor.InsertModel(entities, referenceLine); + + // Assert + Assert.AreEqual(new[] { referenceLinePointEntity }, entities); + mocks.VerifyAll(); + } + [Test] + public void InsertModel_CollectionWithPointReferenceLineNull_ClearsCollection() + { + // Setup + var referenceLinePointEntity = new ReferenceLinePointEntity + { + X = 0, + Y = 0, + Order = 0 + }; + var entities = new List(new[] + { + referenceLinePointEntity + }); + + var mocks = new MockRepository(); + var context = RingtoetsEntitiesHelper.Create(mocks); + mocks.ReplayAll(); + + var persistor = new ReferenceLinePersistor(context); + + // Call + persistor.InsertModel(entities, null); + + // Assert + Assert.AreEqual(0, entities.Count); + mocks.VerifyAll(); + } + + [Test] public void LoadModel_WithoutEntityCollection_ThrowsArgumentException() { // Setup