// 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.IO; using Core.Common.IO.Exceptions; using Core.Common.Utils.Builders; using log4net; using Ringtoets.Common.Data.AssessmentSection; using Ringtoets.HydraRing.Data; using Ringtoets.HydraRing.IO.HydraulicBoundaryDatabaseContext; using Ringtoets.HydraRing.IO.HydraulicLocationConfigurationDatabaseContext; 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; private HydraulicLocationConfigurationSqLiteDatabaseReader hydraulicLocationConfigurationDatabaseReader; /// /// Creates a new instance of , based upon the data read from /// the hydraulic boundary database file, and saved into . /// /// to set the newly /// created . /// The path of the hydraulic boundary database file to import from. /// True if the import was successful, false otherwise. /// Thrown when: /// /// The given file at cannot be read. /// The file 'HLCD.sqlite' in the same folder as cannot be read. /// /// public bool Import(IAssessmentSection targetItem, string filePath) { ValidateAndConnectTo(filePath); var hydraulicBoundaryDatabase = targetItem.HydraulicBoundaryDatabase; if (!IsImportRequired(hydraulicBoundaryDatabase)) { hydraulicBoundaryDatabase.FilePath = filePath; } else { var importResult = GetHydraulicBoundaryDatabase(); if (importResult == null) { return false; } AddImportedDataToModel(targetItem, importResult); log.Info(Resources.HydraulicBoundaryDatabaseImporter_Import_All_hydraulic_locations_read); } return true; } public void Dispose() { if (hydraulicBoundaryDatabaseReader != null) { hydraulicBoundaryDatabaseReader.Dispose(); hydraulicBoundaryDatabaseReader = null; } if (hydraulicLocationConfigurationDatabaseReader != null) { hydraulicLocationConfigurationDatabaseReader.Dispose(); hydraulicLocationConfigurationDatabaseReader = null; } } /// /// Validates the file and opens a connection. /// /// The path to the file to read. /// Thrown when: /// /// The given file at cannot be read. /// The file 'HLCD.sqlite' in the same folder as cannot be read. /// /// private void ValidateAndConnectTo(string filePath) { hydraulicBoundaryDatabaseReader = new HydraulicBoundarySqLiteDatabaseReader(filePath); string hlcdFilePath = Path.Combine(Path.GetDirectoryName(filePath), "hlcd.sqlite"); try { hydraulicLocationConfigurationDatabaseReader = new HydraulicLocationConfigurationSqLiteDatabaseReader(hlcdFilePath); } catch (CriticalFileReadException) { var message = new FileReaderErrorMessageBuilder(filePath).Build(Resources.HydraulicBoundaryDatabaseImporter_HLCD_sqlite_Not_Found); throw new CriticalFileReadException(message); } } private bool IsImportRequired(HydraulicBoundaryDatabase hydraulicBoundaryDatabase) { return hydraulicBoundaryDatabase == null || hydraulicBoundaryDatabaseReader.GetVersion() != hydraulicBoundaryDatabase.Version; } private void HandleException(Exception e) { var message = string.Format(Resources.HydraulicBoundaryDatabaseImporter_ErrorMessage_0_file_skipped, e.Message); log.Error(message); } private HydraulicBoundaryDatabase GetHydraulicBoundaryDatabase() { var trackId = GetTrackId(); if (trackId == 0) { return null; } try { var hydraulicBoundaryDatabase = new HydraulicBoundaryDatabase { FilePath = hydraulicBoundaryDatabaseReader.Path, Version = hydraulicBoundaryDatabaseReader.GetVersion() }; // Locations directory of HLCD location ids and HRD location ids var locationidsDictionary = hydraulicLocationConfigurationDatabaseReader.GetLocationsIdByTrackId(trackId); var filter = new HydraulicBoundaryLocationFilter(); // Prepare query to fetch hrd locations hydraulicBoundaryDatabaseReader.PrepareReadLocation(); while (hydraulicBoundaryDatabaseReader.HasNext) { HrdLocation hrdLocation = hydraulicBoundaryDatabaseReader.ReadLocation(); long locationId; locationidsDictionary.TryGetValue(hrdLocation.HrdLocationId, out locationId); if (filter.ShouldInclude(locationId)) { var hydraulicBoundaryLocation = new HydraulicBoundaryLocation( locationId, hrdLocation.Name, hrdLocation.LocationX, hrdLocation.LocationY); hydraulicBoundaryDatabase.Locations.Add(hydraulicBoundaryLocation); } } return hydraulicBoundaryDatabase; } catch (Exception e) { if (e is LineParseException || e is CriticalFileReadException) { HandleException(e); return null; } throw; } } private long GetTrackId() { try { return hydraulicBoundaryDatabaseReader.GetTrackId(); } catch (Exception e) { if (e is LineParseException || e is CriticalFileReadException) { HandleException(e); return 0; } throw; } } private static void AddImportedDataToModel(IAssessmentSection assessmentSection, HydraulicBoundaryDatabase importedData) { assessmentSection.HydraulicBoundaryDatabase = importedData; assessmentSection.NotifyObservers(); } } }