// 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 Core.Common.IO.Exceptions; using log4net; using Ringtoets.Common.Data; using Ringtoets.HydraRing.Data; using Ringtoets.HydraRing.IO.HydraulicBoundaryDatabaseContext; using Ringtoets.Integration.Forms.PresentationObjects; using Ringtoets.Integration.Plugin.Properties; namespace Ringtoets.Integration.Plugin.FileImporters { /// /// Imports locations read from an Hydraulic boundary .sqlite file (SqlLite database file) to a /// collection of in a . /// public class HydraulicBoundaryDatabaseImporter : IDisposable { private readonly ILog log = LogManager.GetLogger(typeof(HydraulicBoundaryDatabaseImporter)); private HydraulicBoundarySqLiteDatabaseReader hydraulicBoundaryDatabaseReader; /// /// Validates the file and opens a connection. /// /// The path to the file to read. /// Thrown when the given file at cannot be read. public void ValidateAndConnectTo(string filePath) { hydraulicBoundaryDatabaseReader = new HydraulicBoundarySqLiteDatabaseReader(filePath); } /// /// Gets the version of the database. /// /// The database version. public string GetHydraulicBoundaryDatabaseVersion() { return hydraulicBoundaryDatabaseReader.GetVersion(); } /// /// Based upon the data read from the hydraulic boundary database file located at /// , a new instance of , /// and saved into . /// /// to set the newly /// created . /// The path of the hydraulic boundary database file to open. /// True if the import was successful, false otherwise. public bool Import(HydraulicBoundaryDatabaseContext targetItem, string filePath) { if (hydraulicBoundaryDatabaseReader == null) { throw new InvalidOperationException(Resources.HydraulicBoundaryDatabaseImporter_File_not_opened); } var importResult = GetHydraulicBoundaryDatabase(filePath); if (importResult == null) { return false; } AddImportedDataToModel(targetItem.Parent, importResult); log.Info(Resources.HydraulicBoundaryDatabaseImporter_Import_All_hydraulic_locations_read); return true; } public void Dispose() { if (hydraulicBoundaryDatabaseReader != null) { hydraulicBoundaryDatabaseReader.Dispose(); hydraulicBoundaryDatabaseReader = null; } } private void HandleException(Exception e) { var message = string.Format(Resources.HydraulicBoundaryDatabaseImporter_ErrorMessage_0_file_skipped, e.Message); log.Error(message); } private HydraulicBoundaryDatabase GetHydraulicBoundaryDatabase(string path) { try { var hydraulicBoundaryDatabase = new HydraulicBoundaryDatabase { FilePath = path, Version = hydraulicBoundaryDatabaseReader.GetVersion() }; hydraulicBoundaryDatabaseReader.PrepareReadLocation(); while (hydraulicBoundaryDatabaseReader.HasNext) { try { hydraulicBoundaryDatabase.Locations.Add(hydraulicBoundaryDatabaseReader.ReadLocation()); } catch (CriticalFileReadException e) { var message = string.Format(Resources.HydraulicBoundaryDatabaseImporter_ErrorMessage_0_file_skipped, path); log.Error(message, e); return null; } } return hydraulicBoundaryDatabase; } catch (LineParseException e) { HandleException(e); } return null; } private static void AddImportedDataToModel(AssessmentSectionBase assessmentSection, HydraulicBoundaryDatabase importedData) { assessmentSection.HydraulicBoundaryDatabase = importedData; } } }