Scenarios.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. using System;
  2. using System.Diagnostics;
  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. namespace BO.ProcessServer.Business.Scenarios
  9. {
  10. internal class Scenarios
  11. {
  12. private Configuration _configuration;
  13. private ILog _log;
  14. public Scenarios(Configuration configuration, ILogger logger)
  15. {
  16. if (logger == null) throw new ArgumentNullException(nameof(logger));
  17. _configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
  18. _log = logger.GetLogger(GetType());
  19. }
  20. public bool ExistsScenario(DocumentRDto docInfo)
  21. {
  22. if (docInfo == null) throw new ArgumentNullException(nameof(docInfo));
  23. return _configuration.System.Scenarios.Any(x => MatchScenario(x, docInfo));
  24. }
  25. public Configuration.CfgScenario FindScenario(DocumentRDto docInfo)
  26. {
  27. return _configuration.System.Scenarios.FirstOrDefault(x => MatchScenario(x, docInfo));
  28. }
  29. private bool MatchScenario(Configuration.CfgScenario scenario, DocumentRDto document)
  30. {
  31. var match = scenario.AcceptFiltersIfAll;
  32. foreach (var filter in scenario.AcceptFilters)
  33. {
  34. switch (filter.Code)
  35. {
  36. case ScenarioAcceptFilterCodeEnum.PresetWatermarkIs:
  37. if (scenario.AcceptFiltersIfAll)
  38. match = match && bool.Parse(filter.Value) == document.PresetWatermark;
  39. else
  40. match = bool.Parse(filter.Value) == document.PresetWatermark;
  41. break;
  42. case ScenarioAcceptFilterCodeEnum.PresetQualityEq:
  43. if (scenario.AcceptFiltersIfAll)
  44. match = match && int.Parse(filter.Value) == document.PresetCreditCost;
  45. else
  46. match = int.Parse(filter.Value) == document.PresetCreditCost;
  47. break;
  48. case ScenarioAcceptFilterCodeEnum.PresetQualityLt:
  49. if (scenario.AcceptFiltersIfAll)
  50. match = match && int.Parse(filter.Value) > document.PresetCreditCost;
  51. else
  52. match = int.Parse(filter.Value) > document.PresetCreditCost;
  53. break;
  54. case ScenarioAcceptFilterCodeEnum.PresetQualityGt:
  55. if (scenario.AcceptFiltersIfAll)
  56. match = match && int.Parse(filter.Value) < document.PresetCreditCost;
  57. else
  58. match = int.Parse(filter.Value) < document.PresetCreditCost;
  59. break;
  60. case ScenarioAcceptFilterCodeEnum.PresetProcessProfileEq:
  61. if (scenario.AcceptFiltersIfAll)
  62. match = match && document.PresetProcessProfile == filter.Value;
  63. else
  64. match = document.PresetProcessProfile == filter.Value;
  65. break;
  66. case ScenarioAcceptFilterCodeEnum.PresetProcessProfileStarts:
  67. if (scenario.AcceptFiltersIfAll)
  68. match = match && document.PresetProcessProfile.StartsWith(filter.Value);
  69. else
  70. match = document.PresetProcessProfile.StartsWith(filter.Value);
  71. break;
  72. case ScenarioAcceptFilterCodeEnum.PresetProcessProfileEnds:
  73. if (scenario.AcceptFiltersIfAll)
  74. match = match && document.PresetProcessProfile.EndsWith(filter.Value);
  75. else
  76. match = document.PresetProcessProfile.EndsWith(filter.Value);
  77. break;
  78. case ScenarioAcceptFilterCodeEnum.PresetProcessProfileContains:
  79. if (scenario.AcceptFiltersIfAll)
  80. match = match && document.PresetProcessProfile.Contains(filter.Value);
  81. else
  82. match = document.PresetProcessProfile.Contains(filter.Value);
  83. break;
  84. case ScenarioAcceptFilterCodeEnum.PresetTestIs:
  85. if (scenario.AcceptFiltersIfAll)
  86. match = match && bool.Parse(filter.Value) == document.PresetTest;
  87. else
  88. match = bool.Parse(filter.Value) == document.PresetTest;
  89. break;
  90. default:
  91. throw new NotSupportedException($"Not supported code '{filter.Code}'");
  92. }
  93. if (!scenario.AcceptFiltersIfAll && match)
  94. break;
  95. }
  96. return match;
  97. }
  98. }
  99. }