Index: src/Plugins/Wti/Wti.Plugin/FileImporter/PipingSurfaceLinesCsvImporter.cs =================================================================== diff -u -rab8c3b732c3aaadc8e55be98e132bdaf41110169 -rcc2b9aecfd7eb25353fbf015f23c9f0f69178f3f --- src/Plugins/Wti/Wti.Plugin/FileImporter/PipingSurfaceLinesCsvImporter.cs (.../PipingSurfaceLinesCsvImporter.cs) (revision ab8c3b732c3aaadc8e55be98e132bdaf41110169) +++ src/Plugins/Wti/Wti.Plugin/FileImporter/PipingSurfaceLinesCsvImporter.cs (.../PipingSurfaceLinesCsvImporter.cs) (revision cc2b9aecfd7eb25353fbf015f23c9f0f69178f3f) @@ -1,9 +1,12 @@ using System; using System.Collections.Generic; using System.Drawing; +using System.IO; using DelftTools.Shell.Core; +using log4net; + using Wti.Data; using Wti.IO; @@ -90,18 +93,54 @@ public object ImportItem(string path, object target = null) { + var readSurfaceLines = ReadPipingSurfaceLines(path); + + if (!ShouldCancel) + { + AddImportedDataToModel(target, readSurfaceLines); + } + else + { + HandleUserCancellingImport(); + } + + return target; + } + + private void NotifyProgress(string currentStepName, int currentStep, int totalNumberOfSteps) + { + if (ProgressChanged != null) + { + ProgressChanged(currentStepName, currentStep, totalNumberOfSteps); + } + } + + private List ReadPipingSurfaceLines(string path) + { + var stepName = String.Format(ApplicationResources.PipingSurfaceLinesCsvImporter_ReadPipingSurfaceLines_0_, Path.GetFileName(path)); + List readSurfaceLines; using (var reader = new PipingSurfaceLinesCsvReader(path)) { var itemCount = reader.GetSurfaceLinesCount(); + + NotifyProgress(stepName, 0, itemCount); + readSurfaceLines = new List(itemCount); - for (int i = 0; i < itemCount; i++) + for (int i = 0; i < itemCount && !ShouldCancel; i++) { - // TODO: Check ShouldCancel for early abort readSurfaceLines.Add(reader.ReadLine()); + + NotifyProgress(stepName, i+1, itemCount); } } - + return readSurfaceLines; + } + + private void AddImportedDataToModel(object target, List readSurfaceLines) + { + NotifyProgress(ApplicationResources.PipingSurfaceLinesCsvImporter_AddingImportedDataToModel, readSurfaceLines.Count, readSurfaceLines.Count); + var targetCollection = (ICollection)target; foreach (var readSurfaceLine in readSurfaceLines) { @@ -113,8 +152,11 @@ { observableTarget.NotifyObservers(); } + } - return target; + private void HandleUserCancellingImport() + { + LogManager.GetLogger(GetType()).Info(ApplicationResources.PipingSurfaceLinesCsvImporter_ImportItem_ImportCancelled); } } } \ No newline at end of file Index: src/Plugins/Wti/Wti.Plugin/Properties/Resources.Designer.cs =================================================================== diff -u -ra4e086ea5fbb916dd649712efd1fe6a195efc8b3 -rcc2b9aecfd7eb25353fbf015f23c9f0f69178f3f --- src/Plugins/Wti/Wti.Plugin/Properties/Resources.Designer.cs (.../Resources.Designer.cs) (revision a4e086ea5fbb916dd649712efd1fe6a195efc8b3) +++ src/Plugins/Wti/Wti.Plugin/Properties/Resources.Designer.cs (.../Resources.Designer.cs) (revision cc2b9aecfd7eb25353fbf015f23c9f0f69178f3f) @@ -1,7 +1,7 @@ //------------------------------------------------------------------------------ // // This code was generated by a tool. -// Runtime Version:4.0.30319.34209 +// Runtime Version:4.0.30319.18444 // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. @@ -70,6 +70,33 @@ } /// + /// Looks up a localized string similar to Geïmporteerde data toevoegen aan faalmechanisme.... + /// + public static string PipingSurfaceLinesCsvImporter_AddingImportedDataToModel { + get { + return ResourceManager.GetString("PipingSurfaceLinesCsvImporter_AddingImportedDataToModel", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Dwarsdoorsneden importeren afgebroken; Geen data ingelezen.. + /// + public static string PipingSurfaceLinesCsvImporter_ImportItem_ImportCancelled { + get { + return ResourceManager.GetString("PipingSurfaceLinesCsvImporter_ImportItem_ImportCancelled", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Inlezen {0}.... + /// + public static string PipingSurfaceLinesCsvImporter_ReadPipingSurfaceLines_0_ { + get { + return ResourceManager.GetString("PipingSurfaceLinesCsvImporter_ReadPipingSurfaceLines_0_", resourceCulture); + } + } + + /// /// Looks up a localized string similar to DSoilModel bestand. /// public static string SoilFileName { Index: src/Plugins/Wti/Wti.Plugin/Properties/Resources.resx =================================================================== diff -u -ra4e086ea5fbb916dd649712efd1fe6a195efc8b3 -rcc2b9aecfd7eb25353fbf015f23c9f0f69178f3f --- src/Plugins/Wti/Wti.Plugin/Properties/Resources.resx (.../Resources.resx) (revision a4e086ea5fbb916dd649712efd1fe6a195efc8b3) +++ src/Plugins/Wti/Wti.Plugin/Properties/Resources.resx (.../Resources.resx) (revision cc2b9aecfd7eb25353fbf015f23c9f0f69178f3f) @@ -141,4 +141,13 @@ DSoilModel bestand + + Geïmporteerde data toevoegen aan faalmechanisme... + + + Dwarsdoorsneden importeren afgebroken; Geen data ingelezen. + + + Inlezen {0}... + \ No newline at end of file Index: src/Plugins/Wti/Wti.Plugin/Wti.Plugin.csproj =================================================================== diff -u -ra4e086ea5fbb916dd649712efd1fe6a195efc8b3 -rcc2b9aecfd7eb25353fbf015f23c9f0f69178f3f --- src/Plugins/Wti/Wti.Plugin/Wti.Plugin.csproj (.../Wti.Plugin.csproj) (revision a4e086ea5fbb916dd649712efd1fe6a195efc8b3) +++ src/Plugins/Wti/Wti.Plugin/Wti.Plugin.csproj (.../Wti.Plugin.csproj) (revision cc2b9aecfd7eb25353fbf015f23c9f0f69178f3f) @@ -41,6 +41,10 @@ ..\..\..\..\packages\Fluent.Ribbon.3.4.0\lib\net40\Fluent.dll True + + False + ..\..\..\..\lib\log4net.dll + ..\..\..\..\packages\Fluent.Ribbon.3.4.0\lib\net40\Microsoft.Windows.Shell.dll True Index: test/Plugins/Wti/Wti.Plugin.Test/FileImporter/PipingSurfaceLineCsvImporterTest.cs =================================================================== diff -u -rab8c3b732c3aaadc8e55be98e132bdaf41110169 -rcc2b9aecfd7eb25353fbf015f23c9f0f69178f3f --- test/Plugins/Wti/Wti.Plugin.Test/FileImporter/PipingSurfaceLineCsvImporterTest.cs (.../PipingSurfaceLineCsvImporterTest.cs) (revision ab8c3b732c3aaadc8e55be98e132bdaf41110169) +++ test/Plugins/Wti/Wti.Plugin.Test/FileImporter/PipingSurfaceLineCsvImporterTest.cs (.../PipingSurfaceLineCsvImporterTest.cs) (revision cc2b9aecfd7eb25353fbf015f23c9f0f69178f3f) @@ -85,7 +85,9 @@ public void ImportItem_ImportingToValidTargetWithValidFile_ImportSurfaceLinesToCollection() { // Setup - string validFilePath = Path.Combine(testDataPath, "TwoValidSurfaceLines.csv"); + const int expectedNumberOfSurfaceLines = 2; + var twovalidsurfacelinesCsv = "TwoValidSurfaceLines.csv"; + string validFilePath = Path.Combine(testDataPath, twovalidsurfacelinesCsv); var mocks = new MockRepository(); var observer = mocks.StrictMock(); @@ -96,7 +98,26 @@ observableSurfaceLinesList.Attach(observer); var importer = new PipingSurfaceLinesCsvImporter(); + int callCount = 0; + importer.ProgressChanged += delegate(string currentStepName, int currentStep, int totalNumberOfSteps) + { + if (callCount <= expectedNumberOfSurfaceLines) + { + Assert.AreEqual(String.Format(ApplicationResources.PipingSurfaceLinesCsvImporter_ReadPipingSurfaceLines_0_, twovalidsurfacelinesCsv), currentStepName); + } + else if (callCount == expectedNumberOfSurfaceLines+1) + { + Assert.AreEqual(ApplicationResources.PipingSurfaceLinesCsvImporter_AddingImportedDataToModel, currentStepName); + } + else + { + Assert.Fail("Not expecting progress: \"{0}: {1} out of {2}\".", currentStepName, currentStep, totalNumberOfSteps); + } + Assert.AreEqual(expectedNumberOfSurfaceLines, totalNumberOfSteps); + callCount++; + }; + // Precondition CollectionAssert.IsEmpty(observableSurfaceLinesList); Assert.IsTrue(importer.CanImportOn(observableSurfaceLinesList)); @@ -108,7 +129,7 @@ // Assert Assert.AreSame(observableSurfaceLinesList, importedItem); var importTargetArray = observableSurfaceLinesList.ToArray(); - Assert.AreEqual(2, importTargetArray.Length); + Assert.AreEqual(expectedNumberOfSurfaceLines, importTargetArray.Length); // Sample some of the imported data: var firstSurfaceLine = importTargetArray[0]; @@ -121,7 +142,44 @@ Assert.AreEqual(3, secondSurfaceLine.Points.Count()); Assert.AreEqual(4.4, secondSurfaceLine.EndingWorldPoint.X); + Assert.AreEqual(4, callCount); + mocks.VerifyAll(); } + + [Test] + public void ImportItem_ImportingToValidTargetWithValidFileWhileShouldCancelTrue_CancelImportAndLog() + { + // Setup + string validFilePath = Path.Combine(testDataPath, "TwoValidSurfaceLines.csv"); + + var mocks = new MockRepository(); + var observer = mocks.StrictMock(); + mocks.ReplayAll(); + + var observableSurfaceLinesList = new ObservableList(); + observableSurfaceLinesList.Attach(observer); + + var importer = new PipingSurfaceLinesCsvImporter(); + + // Precondition + CollectionAssert.IsEmpty(observableSurfaceLinesList); + Assert.IsTrue(importer.CanImportOn(observableSurfaceLinesList)); + Assert.IsTrue(File.Exists(validFilePath)); + + importer.ShouldCancel = true; + + object importedItem = null; + + // Call + Action call = () => importedItem = importer.ImportItem(validFilePath, observableSurfaceLinesList); + + // Assert + TestHelper.AssertLogMessageIsGenerated(call, ApplicationResources.PipingSurfaceLinesCsvImporter_ImportItem_ImportCancelled, 1); + Assert.AreSame(observableSurfaceLinesList, importedItem); + CollectionAssert.IsEmpty(observableSurfaceLinesList); + + mocks.VerifyAll(); // 'observer' should not be notified + } } } \ No newline at end of file