Script.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. using System.Linq;
  2. namespace InABox.Core
  3. {
  4. [UserTracking(false)]
  5. public class Script : Entity, IRemotable, IPersistent, ILicense<CoreLicense>
  6. {
  7. //[ComboLookupEditor(typeof(SectionLookups), Visible = Visible.Default)]
  8. [ComboLookupEditor(typeof(PropertyClassLookups))]
  9. [EditorSequence(1)]
  10. public string Section { get; set; }
  11. //private class SectionLookups : LookupGenerator
  12. //{
  13. // public SectionLookups() : base()
  14. // {
  15. // foreach (var entity in CoreUtils.Entities)
  16. // this[entity.EntityName()()] = entity.EntityName()();
  17. // }
  18. //}
  19. [EnumLookupEditor(typeof(ScriptType), Visible = Visible.Default)]
  20. [EditorSequence(2)]
  21. public ScriptType ScriptType { get; set; } = ScriptType.None;
  22. //[TextBoxEditor]
  23. //public String Name { get; set; }
  24. [ScriptEditor]
  25. [EditorSequence(3)]
  26. public string Code { get; set; } = "";
  27. private string NameSpace()
  28. {
  29. return string.IsNullOrWhiteSpace(Section) ? "" : string.Join(".", Section.Split('.').Reverse().Skip(1).Reverse());
  30. }
  31. private string EntityName()
  32. {
  33. return string.IsNullOrWhiteSpace(Section) ? "" : Section.Split('.').Last();
  34. }
  35. private string RowStyleTemplate()
  36. {
  37. return
  38. "using System;\r\n" +
  39. "using System.Collections.Generic;\r\n" +
  40. "using System.Linq;\r\n" +
  41. "using System.Runtime;\r\n" +
  42. "using System.Windows;\r\n" +
  43. "using System.Windows.Media;\r\n" +
  44. "using InABox.Core;\r\n" +
  45. "using Comal.Classes;\r\n" +
  46. "\r\n" +
  47. "public class Module\r\n" +
  48. "{\r\n" +
  49. "\r\n" +
  50. " public CoreRow Row { get; set; }\r\n" +
  51. " public String Column { get; set; }\r\n" +
  52. "\r\n" +
  53. " public Brush Background { get; set; }\r\n" +
  54. " public Brush Foreground { get; set; }\r\n" +
  55. " public FontStyle Style { get; set; }\r\n" +
  56. " public FontWeight Weight { get; set; }\r\n" +
  57. "\r\n" +
  58. " public bool Execute()\r\n" +
  59. " {\r\n" +
  60. " return true;\r\n" +
  61. " }\r\n" +
  62. "\r\n" +
  63. "}";
  64. }
  65. private string FilterTemplate()
  66. {
  67. return
  68. "using System;\r\n" +
  69. "using System.Collections.Generic;\r\n" +
  70. "using System.Linq;\r\n" +
  71. "using System.Runtime;\r\n" +
  72. "using InABox.Core;\r\n" +
  73. "using InABox.Database;\r\n" +
  74. "using InABox.Logging;\r\n" +
  75. "using " + NameSpace() + ";\r\n" +
  76. "\r\n" +
  77. "public class Module\r\n" +
  78. "{\r\n" +
  79. "\r\n" +
  80. " public Store<" + EntityName() + "> Store { get; set; }\r\n" +
  81. " public Filter<" + EntityName() + "> Filter { get; set; }\r\n" +
  82. "\r\n" +
  83. " public bool Execute()\r\n" +
  84. " {\r\n" +
  85. " return true;\r\n" +
  86. " }\r\n" +
  87. "\r\n" +
  88. "}";
  89. }
  90. private string CoreTableTemplate()
  91. {
  92. return
  93. "using System;\r\n" +
  94. "using System.Collections.Generic;\r\n" +
  95. "using System.Linq;\r\n" +
  96. "using System.Runtime;\r\n" +
  97. "using InABox.Core;\r\n" +
  98. "using InABox.Database;\r\n" +
  99. "using InABox.Logging;\r\n" +
  100. "using " + NameSpace() + ";\r\n" +
  101. "\r\n" +
  102. "public class Module\r\n" +
  103. "{\r\n" +
  104. "\r\n" +
  105. " public Store<" + EntityName() + "> Store { get; set; }\r\n" +
  106. " public CoreTable " + EntityName() + "s { get; set; }\r\n" +
  107. "\r\n" +
  108. " public bool Execute()\r\n" +
  109. " {\r\n" +
  110. " foreach (CoreRow " + EntityName() + " in " + EntityName() + "s.Rows)\r\n" +
  111. " {\r\n" +
  112. " }\r\n" +
  113. " return true;\r\n" +
  114. " }\r\n" +
  115. "\r\n" +
  116. "}";
  117. }
  118. private string EntityTemplate()
  119. {
  120. return
  121. "using System;\r\n" +
  122. "using System.Collections.Generic;\r\n" +
  123. "using System.Linq;\r\n" +
  124. "using System.Runtime;\r\n" +
  125. "using InABox.Core;\r\n" +
  126. "using InABox.Database;\r\n" +
  127. "using InABox.Logging;\r\n" +
  128. "using " + NameSpace() + ";\r\n" +
  129. "\r\n" +
  130. "public class Module\r\n" +
  131. "{\r\n" +
  132. "\r\n" +
  133. " public Store<" + EntityName() + "> Store { get; set; }\r\n" +
  134. " public IEnumerable<" + EntityName() + "> " + EntityName() + "s { get; set; }\r\n" +
  135. "\r\n" +
  136. " public bool Execute()\r\n" +
  137. " {\r\n" +
  138. " foreach (" + EntityName() + " " + EntityName() + " in " + EntityName() + "s)\r\n" +
  139. " {\r\n" +
  140. " }\r\n" +
  141. " return true;\r\n" +
  142. " }\r\n" +
  143. "\r\n" +
  144. "}";
  145. }
  146. private string GeneralTemplate()
  147. {
  148. return
  149. "using System;\r\n" +
  150. "using System.Collections.Generic;\r\n" +
  151. "using System.Linq;\r\n" +
  152. "using System.Runtime;\r\n" +
  153. "using InABox.Core;\r\n" +
  154. "using InABox.Database;\r\n" +
  155. "using InABox.Logging;" +
  156. "using " + NameSpace() + ";\r\n" +
  157. "\r\n" +
  158. "public class Module\r\n" +
  159. "{\r\n" +
  160. "\r\n" +
  161. " public Object Data { get; set; }\r\n" +
  162. "\r\n" +
  163. " public bool Execute()\r\n" +
  164. " {\r\n" +
  165. " throw new Exception(\"Not Implemented\");\r\n" +
  166. " return true;\r\n" +
  167. " }\r\n" +
  168. "\r\n" +
  169. "}";
  170. }
  171. private bool IsDefault(ScriptType type)
  172. {
  173. if (type.Equals(ScriptType.None))
  174. return string.IsNullOrWhiteSpace(Code);
  175. if (type.Equals(ScriptType.RowStyle))
  176. return Code.Equals(RowStyleTemplate());
  177. if (type.Equals(ScriptType.BeforeQuery))
  178. return Code.Equals(FilterTemplate());
  179. if (type.Equals(ScriptType.AfterQuery))
  180. return Code.Equals(CoreTableTemplate());
  181. if (type.Equals(ScriptType.AfterLoad))
  182. return Code.Equals(EntityTemplate());
  183. if (type.Equals(ScriptType.BeforeSave))
  184. return Code.Equals(EntityTemplate());
  185. if (type.Equals(ScriptType.AfterSave))
  186. return Code.Equals(EntityTemplate());
  187. if (type.Equals(ScriptType.BeforeDelete))
  188. return Code.Equals(EntityTemplate());
  189. if (type.Equals(ScriptType.AfterDelete))
  190. return Code.Equals(EntityTemplate());
  191. return Code.Equals(GeneralTemplate());
  192. }
  193. protected override void DoPropertyChanged(string name, object? before, object? after)
  194. {
  195. base.DoPropertyChanged(name, before, after);
  196. if (name.Equals("ScriptType") && IsDefault((ScriptType)before))
  197. {
  198. if (after == null || after.Equals(ScriptType.None))
  199. Code = "";
  200. else if (after.Equals(ScriptType.RowStyle))
  201. Code = RowStyleTemplate();
  202. else if (after.Equals(ScriptType.BeforeQuery))
  203. Code = FilterTemplate();
  204. else if (after.Equals(ScriptType.AfterQuery))
  205. Code = CoreTableTemplate();
  206. else if (after.Equals(ScriptType.AfterLoad)
  207. || after.Equals(ScriptType.BeforeSave)
  208. || after.Equals(ScriptType.AfterSave)
  209. || after.Equals(ScriptType.BeforeDelete)
  210. || after.Equals(ScriptType.AfterDelete))
  211. Code = EntityTemplate();
  212. else
  213. Code = GeneralTemplate();
  214. }
  215. }
  216. }
  217. }