Scenarios.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using BO.AppServer.Metadata.Dto;
  5. using BO.ProcessServer.Business.Enums;
  6. using BO.ProcessServer.Options;
  7. using Quadarax.Foundation.Core.Logging;
  8. using Quadarax.Foundation.Core.Value.Extensions;
  9. namespace BO.ProcessServer.Business.Scenarios
  10. {
  11. internal class Scenarios
  12. {
  13. private Configuration _configuration;
  14. private ILog _log;
  15. private Random _rnd;
  16. public Scenarios(Configuration configuration, ILogger logger)
  17. {
  18. if (logger == null) throw new ArgumentNullException(nameof(logger));
  19. _configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
  20. _log = logger.GetLogger(GetType());
  21. _rnd = new Random();
  22. }
  23. public bool ExistsScenario(DocumentRDto docInfo)
  24. {
  25. if (docInfo == null) throw new ArgumentNullException(nameof(docInfo));
  26. return _configuration.System.Scenarios.Any(x => MatchScenario(x, docInfo));
  27. }
  28. public Configuration.CfgScenario FindScenario(DocumentRDto docInfo)
  29. {
  30. if (_configuration.System.Mode == AppModeEnum.Proxy ||
  31. _configuration.System.Mode == AppModeEnum.ProxyProcess)
  32. {
  33. var succ = _rnd.NextBool(Constants.CS_DEF_PROXY_SCENARIO_SUCC_PCT);
  34. return new Configuration.CfgScenario()
  35. {
  36. Name = succ ? "DummySucc" : "DummyFail",
  37. AcceptFilters = new List<Configuration.CfgScenarioAcceptFilter>(),
  38. AcceptFiltersIfAll = true,
  39. ExitCodeFail = 1,
  40. ExitCodeSucc = 0,
  41. RunHidden = true,
  42. RunWindowsStyleRaw = "Minimized",
  43. ShellCommand = succ ? Constants.CS_DEF_SCENARIO_SUCC : Constants.CS_DEF_SCENARIO_FAIL,
  44. ShellCommandArguments = Constants.CS_TAG_P_OUT_EXT + " " + Constants.CS_TAG_INPUT_FILES,
  45. TimeoutIntervalRaw = "00:00:10"
  46. };
  47. }
  48. return _configuration.System.Scenarios.FirstOrDefault(x => MatchScenario(x, docInfo));
  49. }
  50. private bool MatchScenario(Configuration.CfgScenario scenario, DocumentRDto document)
  51. {
  52. if (_configuration.System.Mode == AppModeEnum.Proxy ||
  53. _configuration.System.Mode == AppModeEnum.ProxyProcess)
  54. {
  55. return true;
  56. }
  57. var match = scenario.AcceptFiltersIfAll;
  58. foreach (var filter in scenario.AcceptFilters)
  59. {
  60. switch (filter.Code)
  61. {
  62. case ScenarioAcceptFilterCodeEnum.PresetWatermarkIs:
  63. if (scenario.AcceptFiltersIfAll)
  64. match = match && bool.Parse(filter.Value) == document.PresetWatermark;
  65. else
  66. match = bool.Parse(filter.Value) == document.PresetWatermark;
  67. break;
  68. case ScenarioAcceptFilterCodeEnum.PresetQualityEq:
  69. if (scenario.AcceptFiltersIfAll)
  70. match = match && int.Parse(filter.Value) == document.PresetCreditCost;
  71. else
  72. match = int.Parse(filter.Value) == document.PresetCreditCost;
  73. break;
  74. case ScenarioAcceptFilterCodeEnum.PresetQualityLt:
  75. if (scenario.AcceptFiltersIfAll)
  76. match = match && int.Parse(filter.Value) > document.PresetCreditCost;
  77. else
  78. match = int.Parse(filter.Value) > document.PresetCreditCost;
  79. break;
  80. case ScenarioAcceptFilterCodeEnum.PresetQualityGt:
  81. if (scenario.AcceptFiltersIfAll)
  82. match = match && int.Parse(filter.Value) < document.PresetCreditCost;
  83. else
  84. match = int.Parse(filter.Value) < document.PresetCreditCost;
  85. break;
  86. case ScenarioAcceptFilterCodeEnum.PresetProcessProfileEq:
  87. if (scenario.AcceptFiltersIfAll)
  88. match = match && document.PresetProcessProfile == filter.Value;
  89. else
  90. match = document.PresetProcessProfile == filter.Value;
  91. break;
  92. case ScenarioAcceptFilterCodeEnum.PresetProcessProfileStarts:
  93. if (scenario.AcceptFiltersIfAll)
  94. match = match && document.PresetProcessProfile.StartsWith(filter.Value);
  95. else
  96. match = document.PresetProcessProfile.StartsWith(filter.Value);
  97. break;
  98. case ScenarioAcceptFilterCodeEnum.PresetProcessProfileEnds:
  99. if (scenario.AcceptFiltersIfAll)
  100. match = match && document.PresetProcessProfile.EndsWith(filter.Value);
  101. else
  102. match = document.PresetProcessProfile.EndsWith(filter.Value);
  103. break;
  104. case ScenarioAcceptFilterCodeEnum.PresetProcessProfileContains:
  105. if (scenario.AcceptFiltersIfAll)
  106. match = match && document.PresetProcessProfile.Contains(filter.Value);
  107. else
  108. match = document.PresetProcessProfile.Contains(filter.Value);
  109. break;
  110. case ScenarioAcceptFilterCodeEnum.PresetTestIs:
  111. if (scenario.AcceptFiltersIfAll)
  112. match = match && bool.Parse(filter.Value) == document.PresetTest;
  113. else
  114. match = bool.Parse(filter.Value) == document.PresetTest;
  115. break;
  116. default:
  117. throw new NotSupportedException($"Not supported code '{filter.Code}'");
  118. }
  119. if (!scenario.AcceptFiltersIfAll && match)
  120. break;
  121. }
  122. return match;
  123. }
  124. }
  125. }