SaveEvent.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591
  1. using Comal.Classes;
  2. using InABox.Core;
  3. using InABox.Database;
  4. using InABox.Scripting;
  5. using Inflector;
  6. using Newtonsoft.Json;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Diagnostics.CodeAnalysis;
  10. using System.Linq;
  11. using System.Text;
  12. namespace PRS.Shared.Events;
  13. public class SaveEvent<T> : IEvent<SaveEventDataModel<T>>, IEntityEvent<T>
  14. where T : Entity, new()
  15. {
  16. public Type Entity => typeof(T);
  17. public IEventDataModelDefinition DataModelDefinition()
  18. {
  19. return new SaveEventDataModelDefinition<T>(this);
  20. }
  21. public Notification GenerateNotification(SaveEventDataModel<T> model)
  22. {
  23. var notification = new Notification();
  24. notification.Title = $"Updated {typeof(T).Name}";
  25. notification.Description = $"Updated {typeof(T).Name}";
  26. if(model.Entity.ID != Guid.Empty)
  27. {
  28. notification.EntityType = CoreUtils.EntityName(model.EntityType);
  29. notification.EntityID = model.Entity.ID;
  30. }
  31. return notification;
  32. }
  33. public void Init(IStore store, Event ev, IEventData evData, SaveEventDataModel<T> model)
  34. {
  35. if (model.Entity.ID != Guid.Empty)
  36. {
  37. var loadCols = Columns.None<T>();
  38. IEnumerable<string> refVars = evData.ReferencedVariables;
  39. if (!ev.NotificationExpression.IsNullOrWhiteSpace())
  40. {
  41. var notificationExpression = new CoreExpression(ev.NotificationExpression);
  42. refVars = refVars.Concat(notificationExpression.ReferencedVariables);
  43. }
  44. var prefix = $"{typeof(T).Name}.";
  45. foreach (var variable in refVars)
  46. {
  47. if (variable.StartsWith(prefix))
  48. {
  49. var varName = variable[prefix.Length..];
  50. if (!model.Entity.HasOriginalValue(varName))
  51. {
  52. loadCols.Add(varName);
  53. }
  54. }
  55. }
  56. var data = store.Provider.Query(
  57. new Filter<T>(x => x.ID).IsEqualTo(model.Entity.ID),
  58. loadCols);
  59. if(data.Rows.Count > 0)
  60. {
  61. data.Rows[0].FillObject(model.Entity);
  62. }
  63. }
  64. }
  65. }
  66. public class SaveEventDataModelDefinition<T>(SaveEvent<T> ev) : IEventDataModelDefinition
  67. where T : Entity, new()
  68. {
  69. private IEventVariable[]? variables;
  70. public IEnumerable<IEventVariable> GetVariables()
  71. {
  72. if(variables is null)
  73. {
  74. variables = DatabaseSchema.AllProperties(ev.Entity).Select(x => new StandardEventVariable($"{typeof(T).Name}.{x.Name}", x.PropertyType)).ToArray();
  75. variables.SortBy(x => x.Name);
  76. }
  77. return variables;
  78. }
  79. public IEventVariable? GetVariable(string name)
  80. {
  81. if(variables is null)
  82. {
  83. var prefix = $"{typeof(T).Name}.";
  84. if (name.StartsWith(prefix))
  85. {
  86. name = name[prefix.Length..];
  87. var prop = DatabaseSchema.Property(ev.Entity, name);
  88. if(prop is null)
  89. {
  90. return null;
  91. }
  92. else
  93. {
  94. return new StandardEventVariable(prop.Name, prop.PropertyType);
  95. }
  96. }
  97. else
  98. {
  99. return null;
  100. }
  101. }
  102. else
  103. {
  104. return variables.FirstOrDefault(x => x.Name == name);
  105. }
  106. }
  107. }
  108. public class SaveEventDataModel<T>(T entity, IStore store) : IEventDataModel, ITypedEventDataModel
  109. where T : Entity, new()
  110. {
  111. public T Entity { get; set; } = entity;
  112. public Type EntityType => typeof(T);
  113. public IStore Store { get; } = store;
  114. public bool TryGetVariable(string name, out object? value)
  115. {
  116. var prefix = $"{typeof(T).Name}.";
  117. if (name.StartsWith(prefix))
  118. {
  119. name = name[prefix.Length..];
  120. var prop = DatabaseSchema.Property(typeof(T), name);
  121. if(prop != null)
  122. {
  123. value = prop.Getter()(Entity);
  124. return true;
  125. }
  126. }
  127. value = null;
  128. return false;
  129. }
  130. }
  131. #region Triggers
  132. [Caption("New Record")]
  133. public class CreatedSaveEventTrigger<T> : IEventTrigger<SaveEvent<T>, SaveEventDataModel<T>>
  134. where T : Entity, new()
  135. {
  136. public string Description => "New Record";
  137. public IEnumerable<string> ReferencedVariables => [];
  138. public bool Check(SaveEventDataModel<T> dataModel)
  139. {
  140. return dataModel.Entity.HasOriginalValue(x => x.ID);
  141. }
  142. }
  143. [Caption("Property Changed")]
  144. public class PropertyChangedSaveEventTrigger<T> : IEventTrigger<SaveEvent<T>, SaveEventDataModel<T>>
  145. where T : Entity, new()
  146. {
  147. [JsonIgnore]
  148. public IProperty? TriggerProperty { get; set; }
  149. [JsonProperty(PropertyName = "TriggerProperty")]
  150. private string? _property
  151. {
  152. get => TriggerProperty?.Name;
  153. set
  154. {
  155. TriggerProperty = value is null ? null : DatabaseSchema.PropertyStrict(typeof(T), value);
  156. }
  157. }
  158. public string Description => TriggerProperty is null
  159. ? $"{typeof(T).GetCaption()} changed"
  160. : $"{typeof(T).GetCaption()}.{TriggerProperty.Name} changed";
  161. public IEnumerable<string> ReferencedVariables => [];
  162. public bool Check(SaveEventDataModel<T> dataModel)
  163. {
  164. if(TriggerProperty is null)
  165. {
  166. return false;
  167. }
  168. if (!dataModel.Entity.HasOriginalValue(TriggerProperty.Name))
  169. {
  170. return false;
  171. }
  172. return true;
  173. }
  174. }
  175. [Caption("Filter")]
  176. public class FilterSaveEventTrigger<T> : IEventTrigger<SaveEvent<T>, SaveEventDataModel<T>>
  177. where T : Entity, new()
  178. {
  179. public Filter<T>? Filter { get; set; }
  180. public IEnumerable<string> ReferencedVariables => GetReferencedProperties(Filter).Select(x => $"{typeof(T).Name}.{x}");
  181. public string Description => Filter?.ToString() ?? "Blank Filter";
  182. public bool Check(SaveEventDataModel<T> dataModel)
  183. {
  184. return Filter.Match(dataModel.Entity, dataModel.Store.GetQueryProviderFactory());
  185. }
  186. private IEnumerable<string> GetReferencedProperties(Filter<T>? filter)
  187. {
  188. if (filter is null) yield break;
  189. if(filter.Operator != Operator.All && filter.Operator != Operator.None
  190. && !filter.Property.IsNullOrWhiteSpace())
  191. {
  192. yield return filter.Property;
  193. }
  194. foreach(var prop in filter.Ands.Concat(filter.Ors).SelectMany(GetReferencedProperties))
  195. {
  196. yield return prop;
  197. }
  198. }
  199. }
  200. [Caption("Custom Script")]
  201. public class ScriptSaveEventTrigger<T> : IEventTrigger<SaveEvent<T>, SaveEventDataModel<T>>
  202. where T : Entity, new()
  203. {
  204. public string Description => "Custom Script";
  205. private ScriptDocument? _scriptDocument;
  206. private ScriptDocument? ScriptDocument
  207. {
  208. get
  209. {
  210. if(_scriptDocument is null && Script is not null)
  211. {
  212. _scriptDocument = new(Script);
  213. _scriptDocument.Compile();
  214. }
  215. return _scriptDocument;
  216. }
  217. }
  218. private string? _script;
  219. public string? Script
  220. {
  221. get => _script;
  222. set
  223. {
  224. if(_script != value)
  225. {
  226. _script = value;
  227. _scriptDocument = null;
  228. }
  229. }
  230. }
  231. public IEnumerable<string> ReferencedVariables
  232. {
  233. get
  234. {
  235. var method = ScriptDocument?.GetMethod(methodName: "RequiredColumns");
  236. if(method is not null)
  237. {
  238. var cols = Columns.None<T>();
  239. method.Invoke(ScriptDocument!.GetObject(), [cols]);
  240. return cols.ColumnNames().Select(x => $"{typeof(T).Name}.{x}");
  241. }
  242. else
  243. {
  244. return [];
  245. }
  246. }
  247. }
  248. public string DefaultScript()
  249. {
  250. return @"using PRS.Shared.Events;
  251. public class Module
  252. {
  253. public void RequiredColumns(Columns<" + typeof(T).Name + @"> columns)
  254. {
  255. // Modify 'columns' as required to get the required columns for the 'model.Entity'. If you don't provide these,
  256. // the data you require in 'Execute' may not be present. Example:
  257. // columns.Add(x => x.ID);
  258. }
  259. public bool Check(SaveEventDataModel<" + typeof(T).Name + @"> model)
  260. {
  261. // Return true if model.Entity meets the requirements for this event trigger.
  262. return true;
  263. }
  264. }";
  265. }
  266. public bool Check(SaveEventDataModel<T> dataModel)
  267. {
  268. if (ScriptDocument is null) return false;
  269. return ScriptDocument.Execute(methodname: "Check", parameters: [dataModel]);
  270. }
  271. }
  272. #endregion
  273. #region Actions
  274. [Caption("Custom Script")]
  275. public class ScriptSaveEventAction<T> : IEventAction<SaveEvent<T>>
  276. where T : Entity, new()
  277. {
  278. private ScriptDocument? _scriptDocument;
  279. private ScriptDocument? ScriptDocument
  280. {
  281. get
  282. {
  283. if(_scriptDocument is null && Script is not null)
  284. {
  285. _scriptDocument = new(Script);
  286. _scriptDocument.SetValue("Result", null);
  287. _scriptDocument.Compile();
  288. }
  289. return _scriptDocument;
  290. }
  291. }
  292. private string? _script;
  293. public string? Script
  294. {
  295. get => _script;
  296. set
  297. {
  298. if(_script != value)
  299. {
  300. _script = value;
  301. _scriptDocument = null;
  302. }
  303. }
  304. }
  305. public IEnumerable<string> ReferencedVariables
  306. {
  307. get
  308. {
  309. var method = ScriptDocument?.GetMethod(methodName: "RequiredColumns");
  310. if(method is not null)
  311. {
  312. var cols = Columns.None<T>();
  313. method.Invoke(ScriptDocument!.GetObject(), [cols]);
  314. return cols.ColumnNames().Select(x => $"{typeof(T).Name}.{x}");
  315. }
  316. else
  317. {
  318. return [];
  319. }
  320. }
  321. }
  322. public string Description => "Custom Script";
  323. public string DefaultScript()
  324. {
  325. return @"using PRS.Shared.Events;
  326. public class Module
  327. {
  328. public object? Result { get; set; }
  329. public void RequiredColumns(Columns<" + typeof(T).Name + @"> columns)
  330. {
  331. // Modify 'columns' as required to get the required columns for the 'model.Entity'. If you don't provide these,
  332. // the data you require in 'Execute' may not be present. Example:
  333. // columns.Add(x => x.ID);
  334. }
  335. public bool Execute(SaveEventDataModel<" + typeof(T).Name + @"> model)
  336. {
  337. // Do anything you want with model.Entity, and then save return-value to 'Result', or leave it as 'null' if no return value is needed.
  338. return true;
  339. }
  340. }";
  341. }
  342. public object? Execute(IEventDataModel dataModel)
  343. {
  344. if (ScriptDocument is null) return null;
  345. var model = dataModel.RootModel<SaveEventDataModel<T>>();
  346. if(ScriptDocument.Execute(methodname: "Execute", parameters: [model]))
  347. {
  348. return ScriptDocument.GetValue("Result");
  349. }
  350. else
  351. {
  352. return null;
  353. }
  354. }
  355. }
  356. [Caption("Create Entity")]
  357. public class CreateEntitySaveEventAction<T> : IEventAction<SaveEvent<T>>
  358. where T : Entity, new()
  359. {
  360. [JsonProperty(Order = 1)]
  361. public Type? EntityType { get; set; }
  362. [JsonIgnore]
  363. public List<PropertyInitializer> Initializers { get; set; } = new List<PropertyInitializer>();
  364. private ScriptDocument? _scriptDocument;
  365. private ScriptDocument? ScriptDocument
  366. {
  367. get
  368. {
  369. if(_scriptDocument is null && Script is not null)
  370. {
  371. _scriptDocument = new(Script);
  372. _scriptDocument.Compile();
  373. }
  374. return _scriptDocument;
  375. }
  376. }
  377. private string? _script;
  378. public string? Script
  379. {
  380. get => _script;
  381. set
  382. {
  383. if(_script != value)
  384. {
  385. _script = value;
  386. _scriptDocument = null;
  387. }
  388. }
  389. }
  390. private IEnumerable<string> ScriptReferencedVariables
  391. {
  392. get
  393. {
  394. var method = ScriptDocument?.GetMethod(methodName: "RequiredColumns");
  395. if(method is not null)
  396. {
  397. var cols = Columns.None<T>();
  398. method.Invoke(ScriptDocument!.GetObject(), [cols]);
  399. return cols.ColumnNames().Select(x => $"{typeof(T).Name}.{x}");
  400. }
  401. else
  402. {
  403. return [];
  404. }
  405. }
  406. }
  407. public string DefaultScript()
  408. {
  409. if (EntityType is null) return "Please select an entity type first.";
  410. return @"using PRS.Shared.Events;
  411. public class Module
  412. {
  413. public void RequiredColumns(Columns<" + typeof(T).Name + @"> columns)
  414. {
  415. // Modify 'columns' as required to get the required columns for the 'model.Entity'. If you don't provide these,
  416. // the data you require in 'Execute' may not be present. Example:
  417. // columns.Add(x => x.ID);
  418. }
  419. public void Execute(SaveEventDataModel<" + typeof(T).Name + @"> model, " + EntityType.Name + " new" + EntityType.Name + @")
  420. {
  421. // Modify new" + EntityType.Name + @" as you wish, using model.Entity to get the required data. This method runs after
  422. // the property initializers (in the previous screen) for the Create Entity action have run.
  423. }
  424. }";
  425. }
  426. public string Description => $"Create New {EntityType?.Name ?? "Entity"}";
  427. public IEnumerable<string> ReferencedVariables => Initializers.SelectMany(x => x.ReferencedVariables).Concat(ScriptReferencedVariables);
  428. public object? Execute(IEventDataModel dataModel)
  429. {
  430. if(EntityType is null)
  431. {
  432. return null;
  433. }
  434. var entity = (Activator.CreateInstance(EntityType) as Entity)!;
  435. foreach(var initializer in Initializers)
  436. {
  437. initializer.Execute(entity, dataModel);
  438. }
  439. if(ScriptDocument is not null)
  440. {
  441. var model = dataModel.RootModel<SaveEventDataModel<T>>();
  442. ScriptDocument.Execute(methodname: "Execute", parameters: [model, entity]);
  443. }
  444. DbFactory.FindStore(EntityType, Guid.Empty, "", default, "", Logger.Main).Save(entity, "");
  445. return entity;
  446. }
  447. #region Serialization Stuff
  448. [JsonProperty(Order = 2, PropertyName = "Initializers")]
  449. private PropertyInitializerSerializationItem[] _initializers
  450. {
  451. get => Initializers.ToArray(x => new PropertyInitializerSerializationItem { Property = x.Property.Name, Value = x.Value });
  452. set
  453. {
  454. Initializers.Clear();
  455. Initializers.AddRange(value.Select(x => new PropertyInitializer(EntityType!, x)));
  456. }
  457. }
  458. #endregion
  459. }
  460. internal class PropertyInitializerSerializationItem
  461. {
  462. public string Property { get; set; }
  463. public string Value { get; set; }
  464. }
  465. public class PropertyInitializer
  466. {
  467. public IProperty Property { get; set; }
  468. private CoreExpression? _valueExpression;
  469. private CoreExpression ValueExpression
  470. {
  471. get
  472. {
  473. _valueExpression ??= new CoreExpression(Value, Property.PropertyType);
  474. return _valueExpression;
  475. }
  476. }
  477. private string _value;
  478. public string Value
  479. {
  480. get => _value;
  481. [MemberNotNull(nameof(_value))]
  482. set
  483. {
  484. if(value != _value)
  485. {
  486. _value = value;
  487. _valueExpression = null;
  488. }
  489. }
  490. }
  491. public IEnumerable<string> ReferencedVariables => ValueExpression.ReferencedVariables;
  492. internal PropertyInitializer(Type entityType, PropertyInitializerSerializationItem item)
  493. {
  494. Value = item.Value;
  495. Property = DatabaseSchema.PropertyStrict(entityType, item.Property);
  496. }
  497. public PropertyInitializer(IProperty property, string value)
  498. {
  499. Property = property;
  500. Value = value;
  501. }
  502. public void Execute(Entity entity, IEventDataModel dataModel)
  503. {
  504. Property.Setter()(entity, ValueExpression.Evaluate(dataModel));
  505. }
  506. }
  507. #endregion