// Copyright (C) Stichting Deltares 2023. All rights reserved. // // This file is part of the application DAM - UI. // // DAM - UI 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.Globalization; using System.IO; using System.Linq; using System.Reflection; using System.Resources; namespace Deltares.Dam.Data; /// /// Declares the string resource names /// internal enum StringResourceNames { CsvFileNotFound, CsvFileNotValid, CsvHeaderNullOrEmpty, CsvHeaderNotValid, CsvObjectMaterializerNotValid, CsvSplitterPatternNotValid, DataImportArgumentNull, ImportFileNotExist, ImportFolderNotExist, ImportFolderNullOrEmpty, MStabExecutableFileNameNotFound, SlopeWExecutableFileNameNotFound, OutputFileNotExist, OutputFileHasNoContent, OutputFileNameNotValid, ProjectFileNameNullOrEmpty, ProjectFileNotExist, SegmentIdArgumentNullOrEmpty, SoilProfileIdArgumentNullOrEmpty, SurfaceLineIdArgumentNullOrEmpty, RequiredEntityDoesNotExist, ImportFilesNotValidated, RequiredParameterMissing, SurfacelineIsNotInsideSoilprofile, TopLayerToBeEvaluatedNotInsideSoilprofile, SoilAttachedToLayerNotValid, ImportDataNotValid, ImportedLocationDoesntHaveValidSurfaceLines, ImportedLocationDoesntHaveValidPL1Lines, SurfaceLinePointNotExtists, NonExistentLocation, TwoSandlayersRequiredInSoilProfile, CsvColumnIndexAlreadyExists, WaterLevelInputFileNullOrEmpty, NoSoilProfileDefinedForLocation, LocationHasNoSegment, FileNameNotValid, GaugeIdArgumentNullOrEmpty, LocationIdArgumentNullOrEmpty, ImportedLocationDoesntHaveSoilprofile, SoildatabaseNotFound, SurfaceLineNotAssigned, SoilProfileNotAssigned, SoilListIsNull, PL1NotCreated, NoDikeDefined, NoLocationsDefined, NoCalculationTypeSpecified, NoScenariosDefinedInLocation, NoRiverLevel, NoSurfaceLines, NoSoilsDefinedInProject, NoSoilprofiles1DDefinedInProject } /// /// Defines a helper class for getting string resources /// /// /// The class implements a singleton pattern /// internal class StringResources { /// /// Holds a single reference to this class /// private static StringResources instance; /// /// The sync method for making this class thread safe /// private static readonly object sync = new object(); /// /// The actual resource manager /// private readonly ResourceManager resources; // Methods internal StringResources() { resources = new ResourceManager("Deltares.Dam.Data.Properties.Resources", Assembly.GetExecutingAssembly()); } /// /// Getst the string from the from the resource manager /// /// The name of the resource /// The resource string public static string GetString(string name) { StringResources loader = GetInstance(); if (loader == null) { return null; } return loader.resources.GetString(name, CultureInfo.CurrentCulture); } /// /// Gets the sinlgeton instance /// /// A reference to this class instance private static StringResources GetInstance() { if (instance == null) { lock (sync) { if (instance == null) { instance = new StringResources(); } } } return instance; } } /// /// /// internal static class ThrowHelper { public static void ThrowWhenConditionIsTrue(string message, Func condition) where TException : Exception { var exception = (Exception) Activator.CreateInstance(typeof(TException), message); if (condition()) { throw exception; } } public static void ThrowWhenParameterIsMissing(object owner, string parameterName, object parameterValue) { if (parameterValue == null) { throw new ParameterMissingException( string.Format(GetResourceString(StringResourceNames.RequiredParameterMissing), parameterName, owner.GetType().Name)); } } public static void ThrowWhenParameterIsMissing(object owner, string propertyName) { PropertyInfo pi = owner.GetType().GetProperty(propertyName); object propertyValue = pi.GetValue(owner, null); if (propertyValue == null) { throw new ParameterMissingException( string.Format(GetResourceString(StringResourceNames.RequiredParameterMissing), propertyName, owner.GetType().Name)); } } public static void ThrowWhenRequiredEntityDoesntExistInLookup(object id) where T : new() { throw new RequiredEntityNotExistException( GetResourceString(StringResourceNames.RequiredEntityDoesNotExist), typeof(T), id); } public static T ThrowWhenRequiredEntityDoesntExistInLookup(IEnumerable lookup, object id, Func predicate) where T : new() { T entity = lookup.FirstOrDefault(predicate); if (Equals(entity, default(T))) { throw new RequiredEntityNotExistException( GetResourceString(StringResourceNames.RequiredEntityDoesNotExist), typeof(T), id); } return entity; } internal static string GetResourceString(StringResourceNames resourceNames) { return StringResources.GetString(resourceNames.ToString()); } internal static void ThrowIfArgumentNull(object value, string parameter, StringResourceNames resourceNamesName) { if (value == null) { throw new ArgumentNullException(parameter, GetResourceString(resourceNamesName)); } } internal static void ThrowIfArgumentNull(object value, string parameter, string message) { if (value == null) { throw new ArgumentNullException(parameter, message); } } internal static void ThrowIfStringArgumentNullOrEmpty(string value, StringResourceNames resourceNamesName) { if (string.IsNullOrEmpty(value) || value.Trim() == "") { throw new ArgumentException(GetResourceString(resourceNamesName)); } } internal static void ThrowIfFileNameNullOrEmpty(string fileName) { ThrowIfFileNameNullOrEmpty(fileName, StringResourceNames.FileNameNotValid); } internal static void ThrowIfFileNameNullOrEmpty(string fileName, StringResourceNames resourceName) { ThrowIfFileNameNullOrEmpty(fileName, resourceName); } internal static void ThrowIfFileNameNullOrEmpty(string fileName, StringResourceNames resourceName) where TException : Exception { ThrowWhenConditionIsTrue(resourceName, () => string.IsNullOrEmpty(fileName) || fileName.Trim() == ""); } internal static void ThrowIfFileNotExist(string fileName, StringResourceNames resourceNamesName) { if (!File.Exists(fileName)) { throw new FileNotFoundException(string.Format(GetResourceString(resourceNamesName), $"{(fileName ?? "")} in {DamProject.ProjectWorkingPath}")); } } internal static void ThrowIfDirectoryNotExist(string dirName, StringResourceNames resourceNamesName) { if (!Directory.Exists(dirName)) { throw new DirectoryNotFoundException(string.Format(GetResourceString(resourceNamesName), dirName ?? "supplied")); } } internal static void ThrowWhenConditionIsTrue(Func condition, Func exceptionFactory) where TException : Exception { if (condition()) { throw exceptionFactory(); } } internal static void ThrowWhenConditionIsTrue(StringResourceNames resourceName, Func condition) where TException : Exception { var exception = (Exception) Activator.CreateInstance( typeof(TException), GetResourceString(resourceName)); if (condition()) { throw exception; } } internal static void ThrowWhenConditionIsTrue(TArgument arg, StringResourceNames resourceName, Func condition, Func exceptionFactory) where TException : Exception { TException exception = exceptionFactory(resourceName); if (condition(arg)) { throw exception; } } }