// Copyright (C) Stichting Deltares 2019. All rights reserved. // // This file is part of the application DAM - Clients Library. // // 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.IO; using System.Linq; using System.Resources; using System.Reflection; using System.Globalization; namespace Deltares.Dam.Data { /// /// Declares the string resource names /// internal enum StringResourceNames { CsvFileNotFound, CsvFileNotValid, CsvHeaderNullOrEmpty, CsvHeaderNotValid, CsvObjectMaterializerNotValid, CsvSplitterPatternNotValid, DataImportArgumentNull, EntityAlreadyExist, EntityFactoryArgumentNull, 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() { this.resources = new ResourceManager("Deltares.Dam.Data.Properties.Resources", Assembly.GetExecutingAssembly()); } /// /// 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; } /// /// Getst the string from the from the resource manager /// /// The name of the resource /// The resource string public static string GetString(string name) { var loader = GetInstance(); if (loader == null) { return null; } return loader.resources.GetString(name, CultureInfo.CurrentCulture); } } /// /// /// internal static class ThrowHelper { 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), String.Format("{0} in {1}", (fileName ?? ""), 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(); } public static void ThrowWhenConditionIsTrue(string message, Func condition) where TException : Exception { var exception = (Exception)Activator.CreateInstance(typeof(TException), new object[] { message }); if (condition()) throw exception; } internal static void ThrowWhenConditionIsTrue(StringResourceNames resourceName, Func condition) where TException : Exception { var exception = (Exception)Activator.CreateInstance( typeof(TException), new object[] { GetResourceString(resourceName) }); if (condition()) throw exception; } internal static void ThrowWhenConditionIsTrue(TArgument arg, StringResourceNames resourceName, Func condition, Func exceptionFactory) where TException : Exception { var exception = exceptionFactory(resourceName); if (condition(arg)) 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) { var pi = owner.GetType().GetProperty(propertyName); var 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() { var entity = lookup.FirstOrDefault(predicate); if (Equals(entity, default(T))) { throw new RequiredEntityNotExistException( GetResourceString(StringResourceNames.RequiredEntityDoesNotExist), typeof(T), id); } return entity; } } }