Index: Riskeer/Common/src/Riskeer.Common.IO/HydraRing/HydraulicBoundaryDataHelper.cs =================================================================== diff -u --- Riskeer/Common/src/Riskeer.Common.IO/HydraRing/HydraulicBoundaryDataHelper.cs (revision 0) +++ Riskeer/Common/src/Riskeer.Common.IO/HydraRing/HydraulicBoundaryDataHelper.cs (revision f2a9aa153c00f7d7e86167f62ae1351ecc0a2fb3) @@ -0,0 +1,216 @@ +// Copyright (C) Stichting Deltares 2022. All rights reserved. +// +// This file is part of Riskeer. +// +// Riskeer 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.Util; +using Core.Common.Util.Builders; +using Riskeer.Common.Data.Hydraulics; +using Riskeer.Common.IO.Properties; +using Riskeer.HydraRing.IO.HydraulicBoundaryDatabase; +using Riskeer.HydraRing.IO.HydraulicLocationConfigurationDatabase; +using HydraRingResources = Riskeer.HydraRing.IO.Properties.Resources; +using CoreCommonUtilResources = Core.Common.Util.Properties.Resources; + +namespace Riskeer.Common.IO.HydraRing +{ + /// + /// This class defines helper methods for handling and using hydraulic boundary data. + /// + public static class HydraulicBoundaryDataHelper + { + private const string hydraRingConfigurationDatabaseExtension = "config.sqlite"; + private const string preprocessorClosureFileName = "preprocClosure.sqlite"; + + /// + /// Attempts to connect to the as if it is a HRD file with a HLCD file and settings file next + /// to it. + /// + /// The path of the HRD file. + /// The path of the HLCD file. + /// The preprocessor directory. + /// Indicator whether the preprocessor closure is used in a calculation. + /// A describing the problem when trying to connect to the + /// or null if a connection could be correctly made. + public static string ValidateFilesForCalculation(string filePath, string hlcdFilePath, string preprocessorDirectory, bool usePreprocessorClosure) + { + try + { + IOUtils.ValidateFilePath(filePath); + } + catch (ArgumentException e) + { + return e.Message; + } + + try + { + Path.GetDirectoryName(filePath); + } + catch (PathTooLongException) + { + return string.Format(CultureInfo.CurrentCulture, HydraRingResources.HydraulicBoundaryDatabaseHelper_ValidatePathForCalculation_Invalid_path_0_, + filePath); + } + + string settingsDatabaseFileName = GetHydraulicBoundarySettingsDatabase(filePath); + try + { + using (new HydraulicBoundaryDatabaseReader(filePath)) + { + // Used on purpose to check the filePath + } + + using (new HydraulicLocationConfigurationDatabaseReader(hlcdFilePath)) + { + // Used on purpose to check the 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; + } + + if (usePreprocessorClosure) + { + string preprocessorClosureFilePath = GetPreprocessorClosureFilePath(hlcdFilePath); + if (!File.Exists(preprocessorClosureFilePath)) + { + return new FileReaderErrorMessageBuilder(preprocessorClosureFilePath).Build(CoreCommonUtilResources.Error_File_does_not_exist); + } + } + + return null; + } + + /// + /// Checks whether the version of a instance matches the version of a hydraulic + /// boundary database at the given . + /// + /// The hydraulic boundary data to compare the version of. + /// The path to the hydraulic boundary database to compare the version of. + /// true if equals the version of the hydraulic boundary 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(HydraulicBoundaryData hydraulicBoundaryData, string hydraulicBoundaryDatabaseFilePath) + { + if (hydraulicBoundaryData == null) + { + throw new ArgumentNullException(nameof(hydraulicBoundaryData)); + } + + if (hydraulicBoundaryDatabaseFilePath == null) + { + throw new ArgumentNullException(nameof(hydraulicBoundaryDatabaseFilePath)); + } + + return hydraulicBoundaryData.Version == GetVersion(hydraulicBoundaryDatabaseFilePath); + } + + /// + /// 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; + } + + /// + /// Gets the file path of the preprocessor closure database. + /// + /// The file path of the hydraulic location configuration database. + /// The file path of the preprocessor closure database. + /// Thrown when is null. + public static string GetPreprocessorClosureFilePath(string hlcdFilePath) + { + if (hlcdFilePath == null) + { + throw new ArgumentNullException(nameof(hlcdFilePath)); + } + + string directory = Path.GetDirectoryName(hlcdFilePath); + string hlcdFileName = Path.GetFileNameWithoutExtension(hlcdFilePath); + return Path.Combine(directory, $"{hlcdFileName}_{preprocessorClosureFileName}"); + } + + /// + /// Returns the version from the hydraulic boundary database pointed at by the . + /// + /// The location of the hydraulic boundary database. + /// The version from the hydraulic boundary 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 HydraulicBoundaryDatabaseReader(filePath)) + { + return db.ReadVersion(); + } + } + } +} \ No newline at end of file Fisheye: Tag f2a9aa153c00f7d7e86167f62ae1351ecc0a2fb3 refers to a dead (removed) revision in file `Riskeer/Common/src/Riskeer.Common.IO/HydraRing/HydraulicBoundaryDatabaseHelper.cs'. Fisheye: No comparison available. Pass `N' to diff?