Index: Core/Common/src/Core.Common.Base/IO/FileImporterBase.cs =================================================================== diff -u -rdcd6469f6000957bc1604da8e92bd5ea09e43769 -r9a426b227a1f3902ae05dea3c2b0e94f4b4551cb --- Core/Common/src/Core.Common.Base/IO/FileImporterBase.cs (.../FileImporterBase.cs) (revision dcd6469f6000957bc1604da8e92bd5ea09e43769) +++ Core/Common/src/Core.Common.Base/IO/FileImporterBase.cs (.../FileImporterBase.cs) (revision 9a426b227a1f3902ae05dea3c2b0e94f4b4551cb) @@ -13,6 +13,13 @@ /// public abstract class FileImporterBase : IFileImporter { + /// + /// Indicates if a cancel request has been made. When true, no changes should be + /// made to the data model unless the importer is already in progress of changing + /// the data model. + /// + protected bool ImportIsCancelled; + public abstract string Name { get; } public abstract string Category { get; } public abstract Bitmap Image { get; } @@ -22,10 +29,18 @@ public abstract bool Import(object targetItem, string filePath); - public abstract void Cancel(); + public void Cancel() + { + ImportIsCancelled = true; + } public void DoPostImportUpdates(object targetItem) { + if (ImportIsCancelled) + { + return; + } + var observableTarget = targetItem as IObservable; if (observableTarget != null) { Index: Core/Common/test/Core.Common.Base.Test/IO/FileImporterBaseTest.cs =================================================================== diff -u -rdcd6469f6000957bc1604da8e92bd5ea09e43769 -r9a426b227a1f3902ae05dea3c2b0e94f4b4551cb --- Core/Common/test/Core.Common.Base.Test/IO/FileImporterBaseTest.cs (.../FileImporterBaseTest.cs) (revision dcd6469f6000957bc1604da8e92bd5ea09e43769) +++ Core/Common/test/Core.Common.Base.Test/IO/FileImporterBaseTest.cs (.../FileImporterBaseTest.cs) (revision 9a426b227a1f3902ae05dea3c2b0e94f4b4551cb) @@ -65,6 +65,30 @@ mocks.VerifyAll(); // Assert NotifyObservers is called } + [Test] + public void DoPostImportUpdates_ImportCancelled_NoNotifyObserversCalled() + { + // Setup + var mocks = new MockRepository(); + var observableInstance = mocks.StrictMock(); + + var observableTarget = mocks.StrictMock(); + mocks.ReplayAll(); + + var simpleImporter = new SimpleFileImporter(); + simpleImporter.GetAffectedNonTargetObservableInstancesOverride = new[] + { + observableInstance + }; + simpleImporter.Cancel(); + + // Call + simpleImporter.DoPostImportUpdates(observableTarget); + + // Assert + mocks.VerifyAll(); // Assert no NotifyObservers were called + } + private class SimpleFileImporter : FileImporterBase { public override string Name @@ -119,11 +143,6 @@ { throw new NotImplementedException(); } - - public override void Cancel() - { - throw new NotImplementedException(); - } } } } \ No newline at end of file Index: Core/Common/test/Core.Common.Base.Test/Service/FileImportActivityTest.cs =================================================================== diff -u -rdcd6469f6000957bc1604da8e92bd5ea09e43769 -r9a426b227a1f3902ae05dea3c2b0e94f4b4551cb --- Core/Common/test/Core.Common.Base.Test/Service/FileImportActivityTest.cs (.../FileImportActivityTest.cs) (revision dcd6469f6000957bc1604da8e92bd5ea09e43769) +++ Core/Common/test/Core.Common.Base.Test/Service/FileImportActivityTest.cs (.../FileImportActivityTest.cs) (revision 9a426b227a1f3902ae05dea3c2b0e94f4b4551cb) @@ -213,11 +213,6 @@ return true; } - - public override void Cancel() - { - - } } } } Index: Ringtoets/HydraRing/src/Ringtoets.HydraRing.Plugin/HydraulicBoundaryLocationsImporter.cs =================================================================== diff -u -rdcd6469f6000957bc1604da8e92bd5ea09e43769 -r9a426b227a1f3902ae05dea3c2b0e94f4b4551cb --- Ringtoets/HydraRing/src/Ringtoets.HydraRing.Plugin/HydraulicBoundaryLocationsImporter.cs (.../HydraulicBoundaryLocationsImporter.cs) (revision dcd6469f6000957bc1604da8e92bd5ea09e43769) +++ Ringtoets/HydraRing/src/Ringtoets.HydraRing.Plugin/HydraulicBoundaryLocationsImporter.cs (.../HydraulicBoundaryLocationsImporter.cs) (revision 9a426b227a1f3902ae05dea3c2b0e94f4b4551cb) @@ -43,7 +43,6 @@ public class HydraulicBoundaryLocationsImporter : FileImporterBase { private readonly ILog log = LogManager.GetLogger(typeof(HydraulicBoundaryLocationsImporter)); - private bool shouldCancel; /// /// Gets the version of the used Hydraulic Boundary Database. @@ -86,7 +85,7 @@ /// /// Gets the of the item supported by the . - /// public override Type SupportedItemType { get @@ -142,28 +141,20 @@ if (!importResult.CriticalErrorOccurred) { - if (!shouldCancel) + if (!ImportIsCancelled) { AddImportedDataToModel(targetItem, importResult); log.Info(ApplicationResources.HydraulicBoundaryLocationsImporter_Import_Import_successful); return true; } log.Info(ApplicationResources.HydraulicBoundaryLocationsImporter_Import_cancelled); - shouldCancel = false; + ImportIsCancelled = false; } return false; } - /// - /// This method cancels an import. - /// - public override void Cancel() - { - shouldCancel = true; - } - private ReadResult ReadHydraulicBoundaryLocations(string path) { NotifyProgress(ApplicationResources.HydraulicBoundaryLocationsImporter_ReadHydraulicBoundaryLocations, 1, 1); @@ -196,7 +187,7 @@ var locations = new Collection(); while (hydraulicBoundarySqLiteDatabaseReader.HasNext) { - if (shouldCancel) + if (ImportIsCancelled) { return new ReadResult(false); } Index: Ringtoets/Integration/src/Ringtoets.Integration.Plugin/FileImporters/ReferenceLineImporter.cs =================================================================== diff -u -rdcd6469f6000957bc1604da8e92bd5ea09e43769 -r9a426b227a1f3902ae05dea3c2b0e94f4b4551cb --- Ringtoets/Integration/src/Ringtoets.Integration.Plugin/FileImporters/ReferenceLineImporter.cs (.../ReferenceLineImporter.cs) (revision dcd6469f6000957bc1604da8e92bd5ea09e43769) +++ Ringtoets/Integration/src/Ringtoets.Integration.Plugin/FileImporters/ReferenceLineImporter.cs (.../ReferenceLineImporter.cs) (revision 9a426b227a1f3902ae05dea3c2b0e94f4b4551cb) @@ -114,6 +114,11 @@ } } + if (ImportIsCancelled) + { + return false; + } + ReferenceLine importedReferenceLine; try { @@ -130,12 +135,15 @@ return false; } + if (ImportIsCancelled) + { + return false; + } + AddReferenceLineToDataModel(importTarget.Parent, importedReferenceLine, clearReferenceLineDependentData); return true; } - public override void Cancel() {} - protected override IEnumerable GetAffectedNonTargetObservableInstances() { return changedObservables; Index: Ringtoets/Integration/test/Ringtoets.Integration.Plugin.Test/FileImporters/ReferenceLineImporterTest.cs =================================================================== diff -u -rdcd6469f6000957bc1604da8e92bd5ea09e43769 -r9a426b227a1f3902ae05dea3c2b0e94f4b4551cb --- Ringtoets/Integration/test/Ringtoets.Integration.Plugin.Test/FileImporters/ReferenceLineImporterTest.cs (.../ReferenceLineImporterTest.cs) (revision dcd6469f6000957bc1604da8e92bd5ea09e43769) +++ Ringtoets/Integration/test/Ringtoets.Integration.Plugin.Test/FileImporters/ReferenceLineImporterTest.cs (.../ReferenceLineImporterTest.cs) (revision 9a426b227a1f3902ae05dea3c2b0e94f4b4551cb) @@ -332,7 +332,90 @@ mocks.VerifyAll(); // Expect NotifyObservers on cleared calculations and context } - // TODO: Cancel + [Test] + public void Import_CancellingImport_ReturnFalseAndNoChanges() + { + // Setup + var originalReferenceLine = new ReferenceLine(); + + var mocks = new MockRepository(); + var assessmentSection = mocks.Stub(); + assessmentSection.ReferenceLine = originalReferenceLine; + mocks.ReplayAll(); + + var path = TestHelper.GetTestDataPath(TestDataPath.Ringtoets.Common.IO, "traject_10-2.shp"); + + var referenceLineContext = new ReferenceLineContext(assessmentSection); + + var importer = new ReferenceLineImporter(); + + DialogBoxHandler = (name, wnd) => + { + importer.Cancel(); + + var messageBoxTester = new MessageBoxTester(wnd); + messageBoxTester.ClickOk(); + }; + + // Call + bool importSuccesful = importer.Import(referenceLineContext, path); + + // Assert + Assert.IsFalse(importSuccesful); + Assert.AreSame(originalReferenceLine, assessmentSection.ReferenceLine); + mocks.VerifyAll(); + } + + [Test] + public void DoPostImportUpdates_CancellingImport_DoNotNotifyObservers() + { + // Setup + var originalReferenceLine = new ReferenceLine(); + + var mocks = new MockRepository(); + var calculation1 = mocks.StrictMock(); + + var failureMechanism1 = mocks.Stub(); + failureMechanism1.Stub(fm => fm.CalculationItems).Return(new[] + { + calculation1 + }); + + var assessmentSection = mocks.Stub(); + assessmentSection.ReferenceLine = originalReferenceLine; + assessmentSection.Stub(a => a.GetFailureMechanisms()).Return(new[] + { + failureMechanism1 + }); + + var contextObserver = mocks.StrictMock(); + mocks.ReplayAll(); + + var path = TestHelper.GetTestDataPath(TestDataPath.Ringtoets.Common.IO, "traject_10-2.shp"); + + var referenceLineContext = new ReferenceLineContext(assessmentSection); + referenceLineContext.Attach(contextObserver); + + var importer = new ReferenceLineImporter(); + + DialogBoxHandler = (name, wnd) => + { + importer.Cancel(); + + var messageBoxTester = new MessageBoxTester(wnd); + messageBoxTester.ClickOk(); + }; + + // Precondition + Assert.IsFalse(importer.Import(referenceLineContext, path)); + + // Call + importer.DoPostImportUpdates(referenceLineContext); + + // Assert + mocks.VerifyAll(); // Expect no NotifyObserver calls + } + // TODO: Progress reporting // TODO: Instance reuse } Index: Ringtoets/Piping/src/Ringtoets.Piping.Plugin/FileImporter/PipingSoilProfilesImporter.cs =================================================================== diff -u -rdcd6469f6000957bc1604da8e92bd5ea09e43769 -r9a426b227a1f3902ae05dea3c2b0e94f4b4551cb --- Ringtoets/Piping/src/Ringtoets.Piping.Plugin/FileImporter/PipingSoilProfilesImporter.cs (.../PipingSoilProfilesImporter.cs) (revision dcd6469f6000957bc1604da8e92bd5ea09e43769) +++ Ringtoets/Piping/src/Ringtoets.Piping.Plugin/FileImporter/PipingSoilProfilesImporter.cs (.../PipingSoilProfilesImporter.cs) (revision 9a426b227a1f3902ae05dea3c2b0e94f4b4551cb) @@ -42,7 +42,6 @@ public class PipingSoilProfilesImporter : FileImporterBase { private readonly ILog log = LogManager.GetLogger(typeof(PipingSoilProfilesImporter)); - private bool shouldCancel; public override string Name { @@ -87,18 +86,13 @@ public override ProgressChangedDelegate ProgressChanged { protected get; set; } - public override void Cancel() - { - shouldCancel = true; - } - public override bool Import(object targetItem, string filePath) { var importResult = ReadSoilProfiles(filePath); if (!importResult.CriticalErrorOccurred) { - if (!shouldCancel) + if (!ImportIsCancelled) { AddImportedDataToModel(targetItem, importResult); @@ -144,7 +138,7 @@ var profiles = new Collection(); while (soilProfileReader.HasNext) { - if (shouldCancel) + if (ImportIsCancelled) { return new ReadResult(false); } @@ -198,7 +192,7 @@ { log.Info(ApplicationResources.PipingSoilProfilesImporter_Import_Import_cancelled); - shouldCancel = false; + ImportIsCancelled = false; } } } \ No newline at end of file Index: Ringtoets/Piping/src/Ringtoets.Piping.Plugin/FileImporter/PipingSurfaceLinesCsvImporter.cs =================================================================== diff -u -rdcd6469f6000957bc1604da8e92bd5ea09e43769 -r9a426b227a1f3902ae05dea3c2b0e94f4b4551cb --- Ringtoets/Piping/src/Ringtoets.Piping.Plugin/FileImporter/PipingSurfaceLinesCsvImporter.cs (.../PipingSurfaceLinesCsvImporter.cs) (revision dcd6469f6000957bc1604da8e92bd5ea09e43769) +++ Ringtoets/Piping/src/Ringtoets.Piping.Plugin/FileImporter/PipingSurfaceLinesCsvImporter.cs (.../PipingSurfaceLinesCsvImporter.cs) (revision 9a426b227a1f3902ae05dea3c2b0e94f4b4551cb) @@ -47,7 +47,6 @@ public class PipingSurfaceLinesCsvImporter : FileImporterBase { private readonly ILog log; - private bool shouldCancel; private const string characteristicPointsFileSubExtension = ".krp"; public PipingSurfaceLinesCsvImporter() @@ -98,11 +97,6 @@ public override ProgressChangedDelegate ProgressChanged { protected get; set; } - public override void Cancel() - { - shouldCancel = true; - } - public override bool Import(object targetItem, string filePath) { var importSurfaceLinesResult = ReadPipingSurfaceLines(filePath); @@ -111,7 +105,7 @@ return false; } - if (shouldCancel) + if (ImportIsCancelled) { HandleUserCancellingImport(); return false; @@ -124,7 +118,7 @@ return false; } - if (shouldCancel) + if (ImportIsCancelled) { HandleUserCancellingImport(); return false; @@ -172,7 +166,7 @@ var readSurfaceLines = new List(itemCount); var readSurfaceLineIdentifiers = new HashSet(); - for (int i = 0; i < itemCount && !shouldCancel; i++) + for (int i = 0; i < itemCount && !ImportIsCancelled; i++) { try { @@ -250,7 +244,7 @@ var readCharacteristicPointsLocations = new List(itemCount); var readCharacteristicPointsLocationIdentifiers = new HashSet(); - for (int i = 0; i < itemCount && !shouldCancel; i++) + for (int i = 0; i < itemCount && !ImportIsCancelled; i++) { try { @@ -408,7 +402,7 @@ { log.Info(RingtoetsPluginResources.PipingSurfaceLinesCsvImporter_Import_Import_cancelled); - shouldCancel = false; + ImportIsCancelled = false; } } } \ No newline at end of file