// Copyright (C) Stichting Deltares 2017. 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.Globalization; using System.IO; using Core.Common.Base.IO; using Core.Common.Utils; using Ringtoets.Common.Data.Hydraulics; using Ringtoets.Common.IO.Properties; using Ringtoets.HydraRing.IO.HydraulicBoundaryDatabaseContext; using Ringtoets.HydraRing.IO.HydraulicLocationConfigurationDatabaseContext; using HydraRingResources = Ringtoets.HydraRing.IO.Properties.Resources; namespace Ringtoets.Common.IO.HydraRing { /// /// This class defines helper methods for obtaining meta data from hyraulic boundary databases. /// public static class HydraulicBoundaryDatabaseHelper { private const string hydraRingConfigurationDatabaseExtension = "config.sqlite"; private const string hlcdFileName = "HLCD.sqlite"; /// /// Attempts to connect to the as if it is a Hydraulic Boundary Locations /// database with a Hydraulic Location Configurations database next to it. /// /// The path of the Hydraulic Boundary Locations database file. /// The preprocessor directory. /// A describing the problem when trying to connect to the /// or null if a connection could be correctly made. public static string ValidatePathForCalculation(string filePath, string preprocessorDirectory) { try { IOUtils.ValidateFilePath(filePath); } catch (ArgumentException e) { return e.Message; } string directoryName; try { directoryName = Path.GetDirectoryName(filePath); } catch (PathTooLongException) { return string.Format(CultureInfo.CurrentCulture, HydraRingResources.HydraulicBoundaryDatabaseHelper_ValidatePathForCalculation_Invalid_path_0_, filePath); } string settingsDatabaseFileName = GetHydraulicBoundarySettingsDatabase(filePath); try { using (var db = new HydraulicBoundarySqLiteDatabaseReader(filePath)) { db.GetVersion(); } string hlcdFilePath = Path.Combine(directoryName, hlcdFileName); using (new HydraulicLocationConfigurationSqLiteDatabaseReader(hlcdFilePath)) {} using (var validator = new HydraRingSettingsDatabaseValidator(settingsDatabaseFileName, preprocessorDirectory)) { if (!validator.ValidateSchema()) { return Resources.HydraRingSettingsDatabase_Hydraulic_calculation_settings_database_has_invalid_schema; } } } catch (CriticalFileReadException e) { return e.Message; } return null; } /// /// Checks whether the version of a matches the version /// of a database at the given . /// /// The database to compare the version of. /// The path to the database to compare the version of. /// true if equals the version of the database at /// , false otherwise. /// Thrown when no connection with the hydraulic /// boundary database could be created using . /// Thrown when: /// /// is null /// is null /// public static bool HaveEqualVersion(HydraulicBoundaryDatabase database, string pathToDatabase) { if (database == null) { throw new ArgumentNullException(nameof(database)); } if (pathToDatabase == null) { throw new ArgumentNullException(nameof(pathToDatabase)); } return database.Version == GetVersion(pathToDatabase); } /// /// Gets the file path of the hydraulic boundary database settings file. /// /// The file path of the corresponding hydraulic boundary database file. /// The file path of the hydraulic boundary settings database file. public static string GetHydraulicBoundarySettingsDatabase(string hydraulicBoundaryDatabaseFilePath) { return Path.ChangeExtension(hydraulicBoundaryDatabaseFilePath, hydraRingConfigurationDatabaseExtension); } /// /// Checks for being a valid folder path. /// /// The preprocessor directory to validate. /// A describing the problem with /// or null when is a valid directory path. public static string ValidatePreprocessorDirectory(string preprocessorDirectory) { if (preprocessorDirectory != string.Empty) { try { IOUtils.GetFullPath(preprocessorDirectory); } catch (ArgumentException exception) { return $"{Resources.HydraulicBoundaryDatabaseHelper_ValidatePreprocessorDirectory_Invalid_path} {exception.Message}"; } if (!Directory.Exists(preprocessorDirectory)) { return $"{Resources.HydraulicBoundaryDatabaseHelper_ValidatePreprocessorDirectory_Invalid_path} {Resources.HydraulicBoundaryDatabaseHelper_ValidatePreprocessorDirectory_Path_does_not_exist}"; } } return null; } /// /// Returns the version from the database pointed at by the . /// /// The location of the database. /// The version from the database as a . /// Thrown when no connection with the hydraulic /// boundary database could be created. private static string GetVersion(string filePath) { using (var db = new HydraulicBoundarySqLiteDatabaseReader(filePath)) { return db.GetVersion(); } } } }