// Copyright (C) Stichting Deltares 2016. All rights reserved. // // This file is part of Ringtoets. // // Ringtoets is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program. If not, see . // // All names, logos, and references to "Deltares" are registered trademarks of // Stichting Deltares and remain full property of Stichting Deltares at all times. // All rights reserved. using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Security; using Core.Common.IO.Exceptions; using Core.Common.Utils.Builders; using Core.Common.Utils.Properties; using log4net; using Ringtoets.Common.Data.AssessmentSection; using Ringtoets.Common.IO.Exceptions; using RingtoetsCommonIOResources = Ringtoets.Common.IO.Properties.Resources; using CoreCommonBaseResources = Core.Common.Base.Properties.Resources; namespace Ringtoets.Common.IO { /// /// Imports a and stores in on a , /// taking data from a shape file containing a poly lines. /// public class ReferenceLineMetaImporter { private static readonly ILog log = LogManager.GetLogger(typeof(ReferenceLineMetaImporter)); private string shapeFilePath; /// /// Initializes a new instance of the class. /// /// The path to the folder where a shape file should be read. /// /// 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); } /// /// Reads and validates the objects from the shape file. /// /// The read objects. /// Thrown when the shape file does not contain poly lines. /// 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. /// public IEnumerable GetReferenceLineMetas() { ICollection referenceLineMetas = ReferenceLinesMetaReader.ReadReferenceLinesMetas(shapeFilePath); ValidateReferenceLineMetas(referenceLineMetas); return referenceLineMetas; } private void ValidateAndConnectTo(string folderpath) { var files = GetShapeFilesInFolder(folderpath); if (files.Length == 0) { var message = string.Format(RingtoetsCommonIOResources.ReferenceLineMetaImporter_ValidateAndConnectTo_No_shape_file_found_in_folder_0, folderpath); throw new CriticalFileReadException(message); } shapeFilePath = files.First(); if (files.Length > 1) { log.Warn(string.Format(RingtoetsCommonIOResources.ReferenceLineMetaImporter_ValidateAndConnectTo_Multiple_shape_files_found_FilePath_0_SelectedFilePath_1, Path.GetDirectoryName(shapeFilePath), Path.GetFileName(shapeFilePath))); } } private static string[] GetShapeFilesInFolder(string path) { if (string.IsNullOrWhiteSpace(path)) { var message = new FileReaderErrorMessageBuilder(path).Build(Resources.Error_Path_must_be_specified); throw new ArgumentException(message); } try { return Directory.GetFiles(path, "*.shp"); } catch (ArgumentException e) { var message = new FileReaderErrorMessageBuilder(path) .Build(string.Format(Resources.Error_Path_cannot_contain_Characters_0_, string.Join(", ", Path.GetInvalidFileNameChars()))); throw new ArgumentException(message, e); } catch (Exception e) { if (e is IOException || e is SecurityException) { var message = string.Format(RingtoetsCommonIOResources.ReferenceLineMetaImporter_ValidateDirectory_Directory_Invalid, path); throw new CriticalFileReadException(message, e); } throw; } } private void ValidateReferenceLineMetas(ICollection referenceLineMetas) { if (referenceLineMetas.Any(rlm => string.IsNullOrEmpty(rlm.AssessmentSectionId))) { var message = new FileReaderErrorMessageBuilder(shapeFilePath) .Build(RingtoetsCommonIOResources.ReferenceLineMetaImporter_ValidateReferenceLineMetas_Missing_AssessmentSection_Ids); throw new CriticalFileValidationException(message); } var referenceLineMetasCount = referenceLineMetas.Count; var referenceLineMetasDistinctCount = referenceLineMetas.Select(rlm => rlm.AssessmentSectionId).Distinct().Count(); if (referenceLineMetasCount != referenceLineMetasDistinctCount) { var message = new FileReaderErrorMessageBuilder(shapeFilePath) .Build(RingtoetsCommonIOResources.ReferenceLineMetaImporter_ValidateReferenceLineMetas_AssessmentSection_Ids_Not_Unique); throw new CriticalFileValidationException(message); } } } }