CSVPosterEngine.cs 3.9 KB

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