using System; using System.Collections.Generic; using System.Linq; using BO.AppServer.Metadata.Dto; using BO.ProcessServer.Business.Enums; using BO.ProcessServer.Options; using Quadarax.Foundation.Core.Logging; using Quadarax.Foundation.Core.Value.Extensions; namespace BO.ProcessServer.Business.Scenarios { internal class Scenarios { private Configuration _configuration; private ILog _log; private Random _rnd; public Scenarios(Configuration configuration, ILogger logger) { if (logger == null) throw new ArgumentNullException(nameof(logger)); _configuration = configuration ?? throw new ArgumentNullException(nameof(configuration)); _log = logger.GetLogger(GetType()); _rnd = new Random(); } public bool ExistsScenario(DocumentRDto docInfo) { if (docInfo == null) throw new ArgumentNullException(nameof(docInfo)); return _configuration.System.Scenarios.Any(x => MatchScenario(x, docInfo)); } public Configuration.CfgScenario FindScenario(DocumentRDto docInfo) { if (_configuration.System.Mode == AppModeEnum.Proxy || _configuration.System.Mode == AppModeEnum.ProxyProcess) { var succ = _rnd.NextBool(Constants.CS_DEF_PROXY_SCENARIO_SUCC_PCT); return new Configuration.CfgScenario() { Name = succ ? "DummySucc" : "DummyFail", AcceptFilters = new List(), AcceptFiltersIfAll = true, ExitCodeFail = 1, ExitCodeSucc = 0, RunHidden = true, RunWindowsStyleRaw = "Minimized", ShellCommand = succ ? Constants.CS_DEF_SCENARIO_SUCC : Constants.CS_DEF_SCENARIO_FAIL, ShellCommandArguments = Constants.CS_TAG_P_OUT_EXT + " " + Constants.CS_TAG_INPUT_FILES, TimeoutIntervalRaw = "00:00:10" }; } return _configuration.System.Scenarios.FirstOrDefault(x => MatchScenario(x, docInfo)); } private bool MatchScenario(Configuration.CfgScenario scenario, DocumentRDto document) { if (_configuration.System.Mode == AppModeEnum.Proxy || _configuration.System.Mode == AppModeEnum.ProxyProcess) { return true; } var match = scenario.AcceptFiltersIfAll; foreach (var filter in scenario.AcceptFilters) { switch (filter.Code) { case ScenarioAcceptFilterCodeEnum.PresetWatermarkIs: if (scenario.AcceptFiltersIfAll) match = match && bool.Parse(filter.Value) == document.PresetWatermark; else match = bool.Parse(filter.Value) == document.PresetWatermark; break; case ScenarioAcceptFilterCodeEnum.PresetQualityEq: if (scenario.AcceptFiltersIfAll) match = match && int.Parse(filter.Value) == document.PresetCreditCost; else match = int.Parse(filter.Value) == document.PresetCreditCost; break; case ScenarioAcceptFilterCodeEnum.PresetQualityLt: if (scenario.AcceptFiltersIfAll) match = match && int.Parse(filter.Value) > document.PresetCreditCost; else match = int.Parse(filter.Value) > document.PresetCreditCost; break; case ScenarioAcceptFilterCodeEnum.PresetQualityGt: if (scenario.AcceptFiltersIfAll) match = match && int.Parse(filter.Value) < document.PresetCreditCost; else match = int.Parse(filter.Value) < document.PresetCreditCost; break; case ScenarioAcceptFilterCodeEnum.PresetProcessProfileEq: if (scenario.AcceptFiltersIfAll) match = match && document.PresetProcessProfile == filter.Value; else match = document.PresetProcessProfile == filter.Value; break; case ScenarioAcceptFilterCodeEnum.PresetProcessProfileStarts: if (scenario.AcceptFiltersIfAll) match = match && document.PresetProcessProfile.StartsWith(filter.Value); else match = document.PresetProcessProfile.StartsWith(filter.Value); break; case ScenarioAcceptFilterCodeEnum.PresetProcessProfileEnds: if (scenario.AcceptFiltersIfAll) match = match && document.PresetProcessProfile.EndsWith(filter.Value); else match = document.PresetProcessProfile.EndsWith(filter.Value); break; case ScenarioAcceptFilterCodeEnum.PresetProcessProfileContains: if (scenario.AcceptFiltersIfAll) match = match && document.PresetProcessProfile.Contains(filter.Value); else match = document.PresetProcessProfile.Contains(filter.Value); break; case ScenarioAcceptFilterCodeEnum.PresetTestIs: if (scenario.AcceptFiltersIfAll) match = match && bool.Parse(filter.Value) == document.PresetTest; else match = bool.Parse(filter.Value) == document.PresetTest; break; default: throw new NotSupportedException($"Not supported code '{filter.Code}'"); } if (!scenario.AcceptFiltersIfAll && match) break; } return match; } } }