// 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.Linq; using log4net; using Ringtoets.Common.Service.Properties; namespace Ringtoets.Common.Service { /// /// This class defines helper methods for performing calculations. /// public static class CalculationServiceHelper { private static readonly ILog log = LogManager.GetLogger(typeof(CalculationServiceHelper)); /// /// Method for performing validation. Error and status information is logged during /// the execution of the operation. /// /// The name of the calculation. /// The method used to perform the validation. /// True if has no validation errors; False otherwise. public static bool PerformValidation(string name, Func validationFunc) { LogValidationBeginTime(name); var inputValidationResults = validationFunc(); if (inputValidationResults.Any()) { LogMessagesAsError(Resources.Error_in_validation_0, inputValidationResults); } LogValidationEndTime(name); return !inputValidationResults.Any(); } /// /// Method for performing calculations. Error and status information is logged during /// the execution of the operation. /// /// The output type. /// The name of the calculation. /// The method used to perform the calculation. /// The error message to show when the output is null. /// on a successful calculation, null otherwise. /// When throws an exception, this will not be catched in this method. public static T PerformCalculation(string name, Func calculationFunc, string outputIsNullErrorMessage) { LogCalculationBeginTime(name); try { var output = calculationFunc(); if (output == null) { LogMessagesAsError(outputIsNullErrorMessage, name); } return output; } finally { LogCalculationEndTime(name); } } /// /// Logs messages as errors. /// /// The format for the message. /// The messages to log. public static void LogMessagesAsError(string format, params string[] errorMessages) { foreach (var errorMessage in errorMessages) { log.ErrorFormat(format, errorMessage); } } /// /// Logs the begin time of the validation. /// /// The name of the object being validated. public static void LogValidationBeginTime(string name) { log.Info(string.Format(Resources.Validation_Subject_0_started_Time_1_, name, DateTimeService.CurrentTimeAsString)); } /// /// Logs the end time of the validation. /// /// The name of the object being validated. public static void LogValidationEndTime(string name) { log.Info(string.Format(Resources.Validation_Subject_0_ended_Time_1_, name, DateTimeService.CurrentTimeAsString)); } /// /// Logs the begin time of the calculation. /// /// The name of the calculation being started. public static void LogCalculationBeginTime(string name) { log.Info(string.Format(Resources.Calculation_Subject_0_started_Time_1_, name, DateTimeService.CurrentTimeAsString)); } /// /// Logs the end time of the calculation. /// /// The name of the calculation that has ended. public static void LogCalculationEndTime(string name) { log.Info(string.Format(Resources.Calculation_Subject_0_ended_Time_1_, name, DateTimeService.CurrentTimeAsString)); } } }