CustomImporter.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. using com.sun.xml.@internal.rngom.ast.builder;
  2. using InABox.Core;
  3. using InABox.Scripting;
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Diagnostics.CodeAnalysis;
  7. using System.IO;
  8. using System.Linq;
  9. using System.Reflection;
  10. using System.Text;
  11. namespace PRSDesktop
  12. {
  13. public class CreateTableArgs
  14. {
  15. public Stream Stream { get; set; }
  16. public CreateTableArgs(Stream stream)
  17. {
  18. Stream = stream;
  19. }
  20. }
  21. public class LoadDataArgs
  22. {
  23. public Stream Stream { get; set; }
  24. public CoreTable Table { get; set; }
  25. public LoadDataArgs(Stream stream, CoreTable table)
  26. {
  27. Stream = stream;
  28. Table = table;
  29. }
  30. }
  31. public class CustomImporterSettings : BaseObject
  32. {
  33. [ScriptEditor]
  34. public string Script { get; set; }
  35. public CustomImporterSettings()
  36. {
  37. Script = "";
  38. }
  39. public string DefaultScript()
  40. {
  41. return
  42. @"using System;
  43. using System.Collections.Generic;
  44. using System.Linq;
  45. using System.Runtime;
  46. using System.Windows;
  47. using System.Windows.Media;
  48. using InABox.Core;
  49. using Comal.Classes;
  50. using PRSDesktop;
  51. public class Module
  52. {
  53. // Initialise a table with the necessary columns.
  54. public CoreTable CreateTable(CreateTableArgs args)
  55. {
  56. var table = new CoreTable();
  57. table.Columns.Add(new CoreColumn { ColumnName = ..., DataType = typeof(string) });
  58. return table;
  59. }
  60. public void LoadData(LoadDataArgs args)
  61. {
  62. // Read data from args.Stream;
  63. // Add it to the results with:
  64. //
  65. // var row = args.Table.NewRow();
  66. // args.Table.Rows.Add(row);
  67. //
  68. }
  69. }";
  70. }
  71. }
  72. public class CustomImporter<T> : SettingsImporter<T, CustomImporterSettings>
  73. where T : Entity, IRemotable, IPersistent, new()
  74. {
  75. #region Script
  76. private ScriptDocument? _script;
  77. private ScriptDocument? Script
  78. {
  79. get
  80. {
  81. EnsureScript();
  82. return _script;
  83. }
  84. }
  85. private object? _scriptObject;
  86. private object ScriptObject
  87. {
  88. get
  89. {
  90. EnsureScript();
  91. return _scriptObject;
  92. }
  93. }
  94. [MemberNotNull(nameof(_scriptObject), nameof(_createTableMethod), nameof(_loadDataMethod))]
  95. private void EnsureScript()
  96. {
  97. if(_script is null)
  98. {
  99. if (!Settings.Script.IsNullOrWhiteSpace())
  100. {
  101. _script = new ScriptDocument(Settings.Script);
  102. if (!_script.Compile())
  103. {
  104. throw new Exception("Script in Custom Importer failed to compile!");
  105. }
  106. _scriptObject = _script.GetObject()
  107. ?? throw new Exception("Error in Custom Importer script.");
  108. _createTableMethod = _script.GetMethod(methodName: "CreateTable")
  109. ?? throw new Exception("CreateTable() not provided");
  110. _loadDataMethod = _script.GetMethod(methodName: "LoadData")
  111. ?? throw new Exception("LoadData(LoadDataArgs args) not provided");
  112. }
  113. else
  114. {
  115. throw new Exception("No script provided for custom importer.");
  116. }
  117. }
  118. }
  119. private MethodInfo? _createTableMethod;
  120. private MethodInfo? _loadDataMethod;
  121. private MethodInfo CreateTableMethod
  122. {
  123. get
  124. {
  125. EnsureScript();
  126. return _createTableMethod;
  127. }
  128. }
  129. private MethodInfo LoadDataMethod
  130. {
  131. get
  132. {
  133. EnsureScript();
  134. return _loadDataMethod;
  135. }
  136. }
  137. #endregion
  138. private CoreTable? _table;
  139. private bool _loaded;
  140. private Stream? _stream;
  141. private int rowIdx;
  142. public override void Close()
  143. {
  144. _table = null;
  145. _loaded = false;
  146. _stream = null;
  147. }
  148. public override bool MoveNext()
  149. {
  150. EnsureData();
  151. ++rowIdx;
  152. return rowIdx < _table.Rows.Count;
  153. }
  154. public override bool Open(Stream stream)
  155. {
  156. _stream = stream;
  157. return true;
  158. }
  159. [MemberNotNullWhen(true, nameof(_table))]
  160. public override bool ReadHeader()
  161. {
  162. if (_stream is null)
  163. {
  164. throw new Exception("Cannot read header before Open() is called.");
  165. }
  166. var args = new CreateTableArgs(_stream);
  167. _table = CreateTableMethod.Invoke(ScriptObject, new object[] { args }) as CoreTable;
  168. if(_table is not null)
  169. {
  170. Fields = _table.Columns.Select(x => x.ColumnName).ToArray();
  171. return true;
  172. }
  173. else
  174. {
  175. return false;
  176. }
  177. }
  178. [MemberNotNull(nameof(_table))]
  179. private void EnsureData()
  180. {
  181. if (_table is null)
  182. {
  183. if (!ReadHeader())
  184. {
  185. throw new Exception("Failed to read header.");
  186. }
  187. }
  188. if (!_loaded)
  189. {
  190. if (_stream is null)
  191. {
  192. throw new Exception("Cannot import before Open() is called.");
  193. }
  194. LoadDataMethod.Invoke(ScriptObject, new object?[] { new LoadDataArgs(_stream, _table) });
  195. _loaded = true;
  196. rowIdx = -1;
  197. }
  198. }
  199. public override Dictionary<string, string> ReadLine()
  200. {
  201. EnsureData();
  202. var row = _table.Rows[rowIdx];
  203. return _table.Columns.ToDictionary(
  204. x => x.ColumnName,
  205. x => row[x.ColumnName]?.ToString() ?? "");
  206. }
  207. }
  208. }