Index: Ringtoets/Common/src/Ringtoets.Common.IO/ReferenceLineMetaImporter.cs =================================================================== diff -u -r7d38bc43c1be859f9a410ed99c6dacb2a6f58126 -r1de82b61e03283a14d380a48cd718ec65a98432d --- Ringtoets/Common/src/Ringtoets.Common.IO/ReferenceLineMetaImporter.cs (.../ReferenceLineMetaImporter.cs) (revision 7d38bc43c1be859f9a410ed99c6dacb2a6f58126) +++ Ringtoets/Common/src/Ringtoets.Common.IO/ReferenceLineMetaImporter.cs (.../ReferenceLineMetaImporter.cs) (revision 1de82b61e03283a14d380a48cd718ec65a98432d) @@ -30,7 +30,8 @@ using Core.Common.Utils.Properties; using log4net; using Ringtoets.Common.Data.AssessmentSection; -using Ringtoets.Common.Forms.PresentationObjects; +using RingtoetsCommonIOResources = Ringtoets.Common.IO.Properties.Resources; +using CoreCommonBaseResources = Core.Common.Base.Properties.Resources; namespace Ringtoets.Common.IO { @@ -41,58 +42,46 @@ public class ReferenceLineMetaImporter { private static readonly ILog log = LogManager.GetLogger(typeof(ReferenceLineMetaImporter)); - private readonly List referenceLineMetas = new List(); private string shapeFilePath; /// - /// Initializes a new instance of the class and reads the file. + /// Initializes a new instance of the class. /// /// The path to the folder where a shape file should be read. /// - /// The is usually + /// The is typically /// Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.CommonDocuments), "WTI", "NBPW");. /// + /// Thrown when: + /// + /// points to an invalid directory. + /// The path does not contain any shape files. + /// public ReferenceLineMetaImporter(string folderpath) { ValidateAndConnectTo(folderpath); - - ReadReferenceLineMetas(); } /// - /// This method imports the data to an item from a file at the given location. + /// Reads and validates the objects from the shape file. /// - /// The item to perform the import on. - /// - /// - public bool Import(ReferenceLineContext targetItem, string assessmentSectionId) + /// The read objects. + /// Thrown when: + /// + /// The shape file does not contain the required attributes. + /// The assessment section ids in the shape file are not unique or are missing. + /// The shape file does not contain poly lines. + /// The shape file contains multiple poly lines. + /// + public IEnumerable GetReferenceLineMetas() { - var selectedReferenceLineMeta = referenceLineMetas.FirstOrDefault(rlm => rlm.AssessmentSectionId == assessmentSectionId); - if (selectedReferenceLineMeta == null) - { - var message = new FileReaderErrorMessageBuilder(shapeFilePath) - .Build(string.Format("De geselecteerde referentielijn '{0}' is niet gevonden.", assessmentSectionId)); - log.Error(message); - return false; - } + var referenceLineMetas = ReadReferenceLineMetas(); - targetItem.WrappedData.Id = assessmentSectionId; - targetItem.WrappedData.ReferenceLine = selectedReferenceLineMeta; + ValidateReferenceLineMetas(referenceLineMetas); - targetItem.WrappedData.NotifyObservers(); - - return true; + return referenceLineMetas; } - /// - /// Gets all the assessment section ids from the shape file. - /// - /// A list of all assessment section ids read. - public IEnumerable GetAssessmentSectionIds() - { - return referenceLineMetas.Select(rlm => rlm.AssessmentSectionId); - } - private void ValidateAndConnectTo(string folderpath) { ValidateDirectory(folderpath); @@ -102,14 +91,14 @@ { var message = new FileReaderErrorMessageBuilder( Path.Combine(folderpath, "*.shp")) - .Build(@"Er is geen shape file gevonden."); + .Build(RingtoetsCommonIOResources.ReferenceLineMetaImporter_ValidateAndConnectTo_No_shape_file_found); throw new CriticalFileReadException(message); } shapeFilePath = files.First(); if (files.Length > 1) { - log.Warn(string.Format(@"Er zijn meerdere shape files gevonden in '{0}'. '{1}' is gekozen.", + log.Warn(string.Format(RingtoetsCommonIOResources.ReferenceLineMetaImporter_ValidateAndConnectTo_Multiple_shape_files_found_0_Selected_1, Path.GetDirectoryName(shapeFilePath), Path.GetFileName(shapeFilePath))); } } @@ -137,23 +126,16 @@ { if (e is IOException || e is SecurityException) { - HandleException(e); var message = new FileReaderErrorMessageBuilder(path) - .Build("Ongeldig pad."); + .Build(RingtoetsCommonIOResources.ReferenceLineMetaImporter_ValidateDirectory_Directory_Invalid); throw new CriticalFileReadException(message, e); } throw; } } - private static void HandleException(Exception e) + private IEnumerable ReadReferenceLineMetas() { - var message = string.Format("{0} Het bestand wordt overgeslagen.", e.Message); - log.Error(message); - } - - private void ReadReferenceLineMetas() - { using (var reader = new ReferenceLinesMetaReader(shapeFilePath)) { ReferenceLineMeta referenceLinesMeta; @@ -162,36 +144,35 @@ referenceLinesMeta = reader.ReadReferenceLinesMeta(); if (referenceLinesMeta != null) { - referenceLineMetas.Add(referenceLinesMeta); + yield return referenceLinesMeta; } } while (referenceLinesMeta != null); } - - ValidateReferenceLineMetas(); } - private void ValidateReferenceLineMetas() + private void ValidateReferenceLineMetas(IEnumerable referenceLineMetas) { var referenceLineMetasCount = referenceLineMetas.Select(rlm => rlm.AssessmentSectionId).Count(); var referenceLineMetasDistinctCount = referenceLineMetas.Select(rlm => rlm.AssessmentSectionId).Distinct().Count(); if (referenceLineMetasCount != referenceLineMetasDistinctCount) { var message = new FileReaderErrorMessageBuilder(shapeFilePath) - .Build("De trajectid's niet uniek."); + .Build(RingtoetsCommonIOResources.ReferenceLineMetaImporter_ValidateReferenceLineMetas_AssessmentSection_Ids_Not_Unique); log.Warn(message); - MessageBox.Show(message, "Fout", MessageBoxButtons.OK, MessageBoxIcon.Error); - return; + MessageBox.Show(message, CoreCommonBaseResources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error); + throw new CriticalFileReadException(message); } - if (referenceLineMetas.Any(rlm => String.IsNullOrEmpty(rlm.AssessmentSectionId))) + if (referenceLineMetas.Any(rlm => string.IsNullOrEmpty(rlm.AssessmentSectionId))) { var message = new FileReaderErrorMessageBuilder(shapeFilePath) - .Build("De trajectid's zijn niet allemaal ingevuld."); + .Build(RingtoetsCommonIOResources.ReferenceLineMetaImporter_ValidateReferenceLineMetas_Missing_AssessmentSection_Ids); log.Warn(message); - MessageBox.Show(message, "Fout", MessageBoxButtons.OK, MessageBoxIcon.Error); + MessageBox.Show(message, CoreCommonBaseResources.Error, MessageBoxButtons.OK, MessageBoxIcon.Error); + throw new CriticalFileReadException(message); } } }