Index: Ringtoets/Piping/src/Ringtoets.Piping.Plugin/FileImporter/PipingSoilProfilesImporter.cs =================================================================== diff -u -r10779bb6a6db2d00f4627b2bc190e7e35e1fee3e -r619f3fd6896edb6d7f6ac160fcfc66054883dce4 --- Ringtoets/Piping/src/Ringtoets.Piping.Plugin/FileImporter/PipingSoilProfilesImporter.cs (.../PipingSoilProfilesImporter.cs) (revision 10779bb6a6db2d00f4627b2bc190e7e35e1fee3e) +++ Ringtoets/Piping/src/Ringtoets.Piping.Plugin/FileImporter/PipingSoilProfilesImporter.cs (.../PipingSoilProfilesImporter.cs) (revision 619f3fd6896edb6d7f6ac160fcfc66054883dce4) @@ -27,12 +27,13 @@ using Core.Common.IO.Exceptions; using Core.Common.IO.Readers; using log4net; +using Ringtoets.Piping.Data; using Ringtoets.Piping.IO.Exceptions; using Ringtoets.Piping.IO.SoilProfile; using Ringtoets.Piping.Primitives; using PipingFormsResources = Ringtoets.Piping.Forms.Properties.Resources; using RingtoetsFormsResources = Ringtoets.Common.Forms.Properties.Resources; -using ApplicationResources = Ringtoets.Piping.Plugin.Properties.Resources; +using RingtoetsPluginResources = Ringtoets.Piping.Plugin.Properties.Resources; namespace Ringtoets.Piping.Plugin.FileImporter { @@ -72,37 +73,124 @@ get { return String.Format("{0} {1} (*.soil)|*.soil", - PipingFormsResources.PipingSoilProfilesCollection_DisplayName, ApplicationResources.Soil_file_name); + PipingFormsResources.PipingSoilProfilesCollection_DisplayName, RingtoetsPluginResources.Soil_file_name); } } public override ProgressChangedDelegate ProgressChanged { protected get; set; } public override bool Import(object targetItem, string filePath) { - var importResult = ReadSoilProfiles(filePath); + if (!IsReferenceLineAvailable(targetItem)) + { + var message = String.Format(RingtoetsPluginResources.PipingSoilProfilesImporter_ReadSoilProfiles_ParseErrorMessage_0_SoilProfile_skipped, + RingtoetsPluginResources.PipingSurfaceLinesCsvImporter_Import_Required_referenceline_missing); + log.Error(message); + return false; + } - if (!importResult.CriticalErrorOccurred) + var importSoilProfileResult = ReadSoilProfiles(filePath); + if (importSoilProfileResult.CriticalErrorOccurred) { - if (!ImportIsCancelled) - { - AddImportedDataToModel(targetItem, importResult); + return false; + } - return true; - } - + if (ImportIsCancelled) + { HandleUserCancellingImport(); + return false; } - return false; + AddImportedDataToModel(targetItem, importSoilProfileResult); + return true; } - private ReadResult ReadSoilProfiles(string path) + private static bool IsReferenceLineAvailable(object targetItem) { - NotifyProgress(ApplicationResources.PipingSoilProfilesImporter_Reading_database, 1, 1); + //return ((RingtoetsPipingSurfaceLinesContext) targetItem).AssessmentSection.ReferenceLine != null; + return true; + } + private void HandleException(string path, Exception e) + { + var message = string.Format(RingtoetsPluginResources.PipingSoilProfilesImporter_CriticalErrorMessage_0_File_Skipped, + e.Message); + log.Error(message); + } + + private void HandleUserCancellingImport() + { + log.Info(RingtoetsPluginResources.PipingSoilProfilesImporter_Import_Import_cancelled); + + ImportIsCancelled = false; + } + + #region read stochastic soil models + + private ReadResult ReadStochasticSoilModels(string path) + { + NotifyProgress(RingtoetsPluginResources.PipingSoilProfilesImporter_Reading_database, 1, 1); try { + using (var stochasticSoilModelReader = new StochasticSoilModelReader(path)) + { + return GetStochasticSoilModelReadResult(path, stochasticSoilModelReader); + } + } + catch (CriticalFileReadException e) + { + HandleException(path, e); + } + return new ReadResult(true); + } + + private ReadResult GetStochasticSoilModelReadResult(string path, StochasticSoilModelReader stochasticSoilModelReader) + { + var totalNumberOfSteps = stochasticSoilModelReader.PipingStochasticSoilModelCount; + var currentStep = 1; + + var soilModels = new Collection(); + while (stochasticSoilModelReader.HasNext) + { + if (ImportIsCancelled) + { + return new ReadResult(false); + } + try + { + NotifyProgress("Inlezen van de ondergrondsmodellen uit de D-Soil Model database.", currentStep++, totalNumberOfSteps); + soilModels.Add(stochasticSoilModelReader.ReadStochasticSoilModel()); + } + catch (PipingSoilProfileReadException e) + { + var message = string.Format("{0} " + + "Dit ondergrondsmodel wordt overgeslagen.", + e.Message); + log.Error(message); + } + catch (CriticalFileReadException e) + { + var message = string.Format(RingtoetsPluginResources.PipingSoilProfilesImporter_CriticalErrorMessage_0_File_Skipped, + path, e.Message); + log.Error(message); + return new ReadResult(true); + } + } + return new ReadResult(false) + { + ImportedItems = soilModels + }; + } + + #endregion + + #region read soil profiles + + private ReadResult ReadSoilProfiles(string path) + { + NotifyProgress(RingtoetsPluginResources.PipingSoilProfilesImporter_Reading_database, 1, 1); + try + { using (var soilProfileReader = new PipingSoilProfileReader(path)) { return GetProfileReadResult(path, soilProfileReader); @@ -115,11 +203,16 @@ return new ReadResult(true); } - private void HandleException(string path, Exception e) + private void AddImportedDataToModel(object target, ReadResult imported) { - var message = string.Format(ApplicationResources.PipingSoilProfilesImporter_CriticalErrorMessage_0_File_Skipped, - e.Message); - log.Error(message); + var targetCollection = (ICollection) target; + + int totalProfileCount = imported.ImportedItems.Count; + NotifyProgress(RingtoetsPluginResources.PipingSoilProfilesImporter_Adding_imported_data_to_model, totalProfileCount, totalProfileCount); + foreach (var item in imported.ImportedItems) + { + targetCollection.Add(item); + } } private ReadResult GetProfileReadResult(string path, PipingSoilProfileReader soilProfileReader) @@ -136,18 +229,18 @@ } try { - NotifyProgress(ApplicationResources.PipingSoilProfilesImporter_ReadingSoilProfiles, currentStep++, totalNumberOfSteps); + NotifyProgress(RingtoetsPluginResources.PipingSoilProfilesImporter_ReadingSoilProfiles, currentStep++, totalNumberOfSteps); profiles.Add(soilProfileReader.ReadProfile()); } catch (PipingSoilProfileReadException e) { - var message = string.Format(ApplicationResources.PipingSoilProfilesImporter_ReadSoilProfiles_ParseErrorMessage_0_SoilProfile_skipped, + var message = string.Format(RingtoetsPluginResources.PipingSoilProfilesImporter_ReadSoilProfiles_ParseErrorMessage_0_SoilProfile_skipped, e.Message); log.Error(message); } catch (CriticalFileReadException e) { - var message = string.Format(ApplicationResources.PipingSoilProfilesImporter_CriticalErrorMessage_0_File_Skipped, + var message = string.Format(RingtoetsPluginResources.PipingSoilProfilesImporter_CriticalErrorMessage_0_File_Skipped, path, e.Message); log.Error(message); return new ReadResult(true); @@ -159,24 +252,6 @@ }; } - private void AddImportedDataToModel(object target, ReadResult imported) - { - var targetCollection = (ICollection) target; - - int totalProfileCount = imported.ImportedItems.Count; - NotifyProgress(ApplicationResources.PipingSoilProfilesImporter_Adding_imported_data_to_model, totalProfileCount, totalProfileCount); - - foreach (var item in imported.ImportedItems) - { - targetCollection.Add(item); - } - } - - private void HandleUserCancellingImport() - { - log.Info(ApplicationResources.PipingSoilProfilesImporter_Import_Import_cancelled); - - ImportIsCancelled = false; - } + #endregion } } \ No newline at end of file