AbstractPostCommand.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.IO.Abstractions;
  5. using System.Linq;
  6. using System.Threading.Tasks;
  7. using Quadarax.Foundation.Core.Data.Interface.Entity;
  8. using Quadarax.Foundation.Core.Json;
  9. using Quadarax.Foundation.Core.QConsole;
  10. using Quadarax.Foundation.Core.QConsole.Argument;
  11. using Quadarax.Foundation.Core.QConsole.Value;
  12. using Quadarax.Foundation.Core.Value;
  13. namespace BO.Console.Commands.Base
  14. {
  15. internal abstract class AbstractPostCommand<TData,TIterationResult> : BaseCommand
  16. where TData : IModifyDto
  17. where TIterationResult : IReadDto, new()
  18. {
  19. #region *** Constants ***
  20. private const string CS_ARG_NAME_IN = "in";
  21. private const string CS_ARG_HINT_IN = "<input_json_file>";
  22. private const string CS_ARG_DESC_IN = "Input .json file for entity {0}";
  23. private const string CS_ARG_NAME_LIST = "lst";
  24. private const string CS_ARG_HINT_LIST = "<show_list>";
  25. private const string CS_ARG_DESC_LIST = "Shows parsed deserialized input list";
  26. #endregion
  27. #region *** Properties ***
  28. protected IList<TData> Data { get; private set; }
  29. #endregion
  30. #region *** Constructors ***
  31. protected AbstractPostCommand(Engine engine) : base(engine)
  32. {
  33. }
  34. #endregion
  35. #region *** Protected Overrides ***
  36. protected override void BeginExecute()
  37. {
  38. base.BeginExecute();
  39. if (WasArgumentSpecified(CS_ARG_NAME_LIST))
  40. {
  41. WriteInfo("Parsed input data list:");
  42. if (Data == null)
  43. {
  44. WriteWarning("No data.");
  45. return;
  46. }
  47. var cnt = 0;
  48. foreach (var item in Data)
  49. {
  50. WriteCaption($"[{cnt}]");
  51. WriteDtoToConsole(item);
  52. cnt++;
  53. }
  54. WriteCaption(string.Empty);
  55. WriteCaption($"Total {cnt} items.");
  56. }
  57. }
  58. protected override Result OnExecute()
  59. {
  60. if (Data == null)
  61. return new Result(new Exception("Nothing to do."));
  62. if (Data.Count == 0)
  63. return new Result(new Exception("Nothing to do."));
  64. var tasks = new List<Task<TIterationResult>>();
  65. var errors = new List<Exception>();
  66. foreach (var data in Data)
  67. {
  68. var task = new Task<TIterationResult>(() =>
  69. {
  70. try
  71. {
  72. return IterationExecute(data);
  73. }
  74. catch (Exception e)
  75. {
  76. if (e is AggregateException exception)
  77. errors.AddRange(exception.InnerExceptions);
  78. else
  79. errors.Add(e);
  80. WriteError(e);
  81. return new TIterationResult();
  82. }
  83. });
  84. task.Start();
  85. tasks.Add(task);
  86. }
  87. Task.WaitAll(tasks.ToArray());
  88. return errors.Count == 0 ? new Result() : new Result(new Exception("Operation failed!"));
  89. }
  90. protected abstract TIterationResult IterationExecute(TData data);
  91. protected override void OnValidateArguments()
  92. {
  93. base.OnValidateArguments();
  94. if (!FileSystem.File.Exists(GetArgumentValueOrDefault<string>(CS_ARG_NAME_IN)))
  95. throw new FileNotFoundException($"Cannot find input file '{GetArgumentValueOrDefault<string>(CS_ARG_NAME_IN)}'");
  96. var binder = new Binder(FileSystem);
  97. Data = binder.Load<List<TData>>(GetArgumentValueOrDefault<string>(CS_ARG_NAME_IN));
  98. }
  99. protected override IEnumerable<AbstractArgument> OnSetupArguments()
  100. {
  101. var args = base.OnSetupArguments().ToList();
  102. args.Add(new NamedArgument(CS_ARG_NAME_IN , string.Format(CS_ARG_DESC_IN, typeof(TData).Name), CS_ARG_HINT_IN, TypeValuesEnum.String, string.Empty,true));
  103. args.Add(new FlagArgument(CS_ARG_NAME_LIST , CS_ARG_DESC_LIST, CS_ARG_HINT_LIST,false));
  104. return args;
  105. }
  106. #endregion
  107. }
  108. }