Script.cs 8.9 KB

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