CustomImporter.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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 InABox.Scripting;
  50. using Comal.Classes;
  51. using PRSDesktop;
  52. public class Module
  53. {
  54. // Initialise a table with the necessary columns.
  55. public CoreTable? CreateTable(CreateTableArgs args)
  56. {
  57. var table = new CoreTable();
  58. table.Columns.Add(new CoreColumn { ColumnName = ..., DataType = typeof(string) });
  59. return table;
  60. }
  61. public void LoadData(LoadDataArgs args)
  62. {
  63. // Read data from args.Stream;
  64. // Add it to the results with:
  65. //
  66. // var row = args.Table.NewRow();
  67. // args.Table.Rows.Add(row);
  68. //
  69. }
  70. }";
  71. }
  72. }
  73. public class CustomImporter<T> : SettingsImporter<T, CustomImporterSettings>
  74. where T : Entity, IRemotable, IPersistent, new()
  75. {
  76. #region Script
  77. private ScriptDocument? _script;
  78. private ScriptDocument? Script
  79. {
  80. get
  81. {
  82. EnsureScript();
  83. return _script;
  84. }
  85. }
  86. private object? _scriptObject;
  87. private object ScriptObject
  88. {
  89. get
  90. {
  91. EnsureScript();
  92. return _scriptObject;
  93. }
  94. }
  95. [MemberNotNull(nameof(_scriptObject), nameof(_createTableMethod), nameof(_loadDataMethod))]
  96. private void EnsureScript()
  97. {
  98. if(_script is null)
  99. {
  100. if (!Settings.Script.IsNullOrWhiteSpace())
  101. {
  102. _script = new ScriptDocument(Settings.Script);
  103. if (!_script.Compile())
  104. {
  105. throw new Exception("Script in Custom Importer failed to compile!");
  106. }
  107. _scriptObject = _script.GetObject()
  108. ?? throw new Exception("Error in Custom Importer script.");
  109. _createTableMethod = _script.GetMethod(methodName: "CreateTable")
  110. ?? throw new Exception("CreateTable() not provided");
  111. _loadDataMethod = _script.GetMethod(methodName: "LoadData")
  112. ?? throw new Exception("LoadData(LoadDataArgs args) not provided");
  113. }
  114. else
  115. {
  116. throw new Exception("No script provided for custom importer.");
  117. }
  118. }
  119. }
  120. private MethodInfo? _createTableMethod;
  121. private MethodInfo? _loadDataMethod;
  122. private MethodInfo CreateTableMethod
  123. {
  124. get
  125. {
  126. EnsureScript();
  127. return _createTableMethod;
  128. }
  129. }
  130. private MethodInfo LoadDataMethod
  131. {
  132. get
  133. {
  134. EnsureScript();
  135. return _loadDataMethod;
  136. }
  137. }
  138. #endregion
  139. private CoreTable? _table;
  140. private bool _loaded;
  141. private Stream? _stream;
  142. private int rowIdx;
  143. public override void Close()
  144. {
  145. _table = null;
  146. _loaded = false;
  147. _stream = null;
  148. }
  149. public override bool MoveNext()
  150. {
  151. EnsureData();
  152. ++rowIdx;
  153. return rowIdx < _table.Rows.Count;
  154. }
  155. public override bool Open(Stream stream)
  156. {
  157. _stream = stream;
  158. return true;
  159. }
  160. [MemberNotNullWhen(true, nameof(_table))]
  161. public override bool ReadHeader()
  162. {
  163. if (_stream is null)
  164. {
  165. throw new Exception("Cannot read header before Open() is called.");
  166. }
  167. var args = new CreateTableArgs(_stream);
  168. _table = CreateTableMethod.Invoke(ScriptObject, new object[] { args }) as CoreTable;
  169. if(_table is not null)
  170. {
  171. Fields = _table.Columns.Select(x => x.ColumnName).ToArray();
  172. return true;
  173. }
  174. else
  175. {
  176. return false;
  177. }
  178. }
  179. [MemberNotNull(nameof(_table))]
  180. private void EnsureData()
  181. {
  182. if (_table is null)
  183. {
  184. if (!ReadHeader())
  185. {
  186. throw new Exception("Failed to read header.");
  187. }
  188. }
  189. if (!_loaded)
  190. {
  191. if (_stream is null)
  192. {
  193. throw new Exception("Cannot import before Open() is called.");
  194. }
  195. LoadDataMethod.Invoke(ScriptObject, new object?[] { new LoadDataArgs(_stream, _table) });
  196. _loaded = true;
  197. rowIdx = -1;
  198. }
  199. }
  200. public override Dictionary<string, string> ReadLine()
  201. {
  202. EnsureData();
  203. var row = _table.Rows[rowIdx];
  204. return _table.Columns.ToDictionary(
  205. x => x.ColumnName,
  206. x => row[x.ColumnName]?.ToString() ?? "");
  207. }
  208. }
  209. }