CSVPosterEngine.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. using CsvHelper;
  2. using InABox.Core;
  3. using InABox.Core.Postable;
  4. using InABox.Scripting;
  5. using Microsoft.Win32;
  6. using System.Collections.Generic;
  7. using System.Globalization;
  8. using System.IO;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Threading.Tasks;
  12. namespace InABox.Poster.CSV
  13. {
  14. public class CSVPosterEngine<TPostable> : PosterEngine<TPostable, ICSVPoster<TPostable>, CSVPosterSettings>
  15. where TPostable : Entity, IPostable, IRemotable, IPersistent, new()
  16. {
  17. private ScriptDocument? _script;
  18. private bool _hasCheckedScript;
  19. private ScriptDocument? GetScriptDocument()
  20. {
  21. if (_hasCheckedScript)
  22. {
  23. return _script;
  24. }
  25. var settings = GetSettings();
  26. if (settings.ScriptEnabled && !string.IsNullOrWhiteSpace(settings.Script))
  27. {
  28. var document = new ScriptDocument(settings.Script);
  29. document.Properties.Add(new ScriptProperty("Results", null));
  30. if (!document.Compile())
  31. {
  32. throw new Exception("Script failed to compile!");
  33. }
  34. _script = document;
  35. }
  36. else
  37. {
  38. _script = null;
  39. }
  40. _hasCheckedScript = true;
  41. return _script;
  42. }
  43. public override bool BeforePost(IDataModel<TPostable> model)
  44. {
  45. if(GetScriptDocument() is ScriptDocument script)
  46. {
  47. return script.Execute(methodname: "BeforePost", parameters: new object[] { model });
  48. }
  49. else
  50. {
  51. return Poster.BeforePost(model);
  52. }
  53. }
  54. protected override IPostResult<TPostable> DoProcess(IDataModel<TPostable> model)
  55. {
  56. var settings = GetSettings();
  57. ICSVExport<TPostable> results;
  58. if (GetScriptDocument() is ScriptDocument script)
  59. {
  60. if (!script.Execute(methodname: "Process", parameters: new object[] { model }))
  61. {
  62. throw new Exception("Post Failed.");
  63. }
  64. var resultsObject = script.GetValue("Results");
  65. results = (resultsObject as ICSVExport<TPostable>)
  66. ?? throw new Exception($"Script 'Results' property expected to be ICSVExport<{typeof(TPostable)}>, got {resultsObject}");
  67. }
  68. else
  69. {
  70. results = Poster.Process(model);
  71. }
  72. var dlg = new SaveFileDialog()
  73. {
  74. FileName = settings.DefaultOutputFile,
  75. Filter = "CSV Files (*.csv)|*.csv"
  76. };
  77. if(dlg.ShowDialog() == true)
  78. {
  79. using var writer = new StreamWriter(dlg.FileName);
  80. using var csv = new CsvWriter(writer, CultureInfo.InvariantCulture);
  81. csv.Context.RegisterClassMap(results.ClassMap.ClassMap);
  82. var method = typeof(CsvWriter).GetMethods()
  83. .Where(x => x.Name == nameof(CsvWriter.WriteRecords) && x.GetGenericArguments().Length == 1).First()
  84. .MakeGenericMethod(results.Type);
  85. method.Invoke(csv, new object?[] { results.Records });
  86. return results;
  87. }
  88. else
  89. {
  90. throw new PostCancelledException();
  91. }
  92. }
  93. public override void AfterPost(IDataModel<TPostable> model, IPostResult<TPostable> result)
  94. {
  95. if (GetScriptDocument() is ScriptDocument script)
  96. {
  97. // Ignoring 'result', because this is actually the 'Results' field of the script class.
  98. script.Execute(methodname: "AfterPost", parameters: new object[] { model });
  99. }
  100. else
  101. {
  102. Poster.AfterPost(model, result);
  103. }
  104. }
  105. }
  106. }