| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111 |
- using System;
- using System.Diagnostics;
- using System.Linq;
- using BO.AppServer.Metadata.Dto;
- using BO.ProcessServer.Business.Enums;
- using BO.ProcessServer.Options;
- using Quadarax.Foundation.Core.Logging;
- namespace BO.ProcessServer.Business.Scenarios
- {
- internal class Scenarios
- {
- private Configuration _configuration;
- private ILog _log;
- 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());
- }
-
- 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)
- {
- return _configuration.System.Scenarios.FirstOrDefault(x => MatchScenario(x, docInfo));
- }
- private bool MatchScenario(Configuration.CfgScenario scenario, DocumentRDto document)
- {
- 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;
- }
- }
- }
|