SaveEvent.cs 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. using Comal.Classes;
  2. using InABox.Core;
  3. using InABox.Database;
  4. using InABox.Scripting;
  5. using Inflector;
  6. using System;
  7. using System.Collections.Generic;
  8. using System.Diagnostics.CodeAnalysis;
  9. using System.Linq;
  10. using System.Text;
  11. using System.Text.Json.Serialization;
  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. [JsonInclude]
  150. [JsonPropertyName("TriggerProperty")]
  151. private string? _property
  152. {
  153. get => TriggerProperty?.Name;
  154. set
  155. {
  156. TriggerProperty = value is null ? null : DatabaseSchema.PropertyStrict(typeof(T), value);
  157. }
  158. }
  159. public string Description => TriggerProperty is null
  160. ? $"{typeof(T).GetCaption()} changed"
  161. : $"{typeof(T).GetCaption()}.{TriggerProperty.Name} changed";
  162. public IEnumerable<string> ReferencedVariables => [];
  163. public bool Check(SaveEventDataModel<T> dataModel)
  164. {
  165. if(TriggerProperty is null)
  166. {
  167. return false;
  168. }
  169. if (!dataModel.Entity.HasOriginalValue(TriggerProperty.Name))
  170. {
  171. return false;
  172. }
  173. return true;
  174. }
  175. }
  176. [Caption("Filter")]
  177. public class FilterSaveEventTrigger<T> : IEventTrigger<SaveEvent<T>, SaveEventDataModel<T>>
  178. where T : Entity, new()
  179. {
  180. public Filter<T>? Filter { get; set; }
  181. public IEnumerable<string> ReferencedVariables => GetReferencedProperties(Filter).Select(x => $"{typeof(T).Name}.{x}");
  182. public string Description => Filter?.ToString() ?? "Blank Filter";
  183. public bool Check(SaveEventDataModel<T> dataModel)
  184. {
  185. return Filter.Match(dataModel.Entity, dataModel.Store.GetQueryProviderFactory());
  186. }
  187. private IEnumerable<string> GetReferencedProperties(Filter<T>? filter)
  188. {
  189. if (filter is null) yield break;
  190. if(filter.Operator != Operator.All && filter.Operator != Operator.None
  191. && !filter.Property.IsNullOrWhiteSpace())
  192. {
  193. yield return filter.Property;
  194. }
  195. foreach(var prop in filter.Ands.Concat(filter.Ors).SelectMany(GetReferencedProperties))
  196. {
  197. yield return prop;
  198. }
  199. }
  200. }
  201. [Caption("Custom Script")]
  202. public class ScriptSaveEventTrigger<T> : IEventTrigger<SaveEvent<T>, SaveEventDataModel<T>>
  203. where T : Entity, new()
  204. {
  205. public string Description => "Custom Script";
  206. private ScriptDocument? _scriptDocument;
  207. private ScriptDocument? ScriptDocument
  208. {
  209. get
  210. {
  211. if(_scriptDocument is null && Script is not null)
  212. {
  213. _scriptDocument = new(Script);
  214. _scriptDocument.Compile();
  215. }
  216. return _scriptDocument;
  217. }
  218. }
  219. private string? _script;
  220. public string? Script
  221. {
  222. get => _script;
  223. set
  224. {
  225. if(_script != value)
  226. {
  227. _script = value;
  228. _scriptDocument = null;
  229. }
  230. }
  231. }
  232. public IEnumerable<string> ReferencedVariables
  233. {
  234. get
  235. {
  236. var method = ScriptDocument?.GetMethod(methodName: "RequiredColumns");
  237. if(method is not null)
  238. {
  239. var cols = Columns.None<T>();
  240. method.Invoke(ScriptDocument!.GetObject(), [cols]);
  241. return cols.ColumnNames().Select(x => $"{typeof(T).Name}.{x}");
  242. }
  243. else
  244. {
  245. return [];
  246. }
  247. }
  248. }
  249. public string DefaultScript()
  250. {
  251. return @"using PRS.Shared.Events;
  252. public class Module
  253. {
  254. public void RequiredColumns(Columns<" + typeof(T).Name + @"> columns)
  255. {
  256. // Modify 'columns' as required to get the required columns for the 'model.Entity'. If you don't provide these,
  257. // the data you require in 'Execute' may not be present. Example:
  258. // columns.Add(x => x.ID);
  259. }
  260. public bool Check(SaveEventDataModel<" + typeof(T).Name + @"> model)
  261. {
  262. // Return true if model.Entity meets the requirements for this event trigger.
  263. return true;
  264. }
  265. }";
  266. }
  267. public bool Check(SaveEventDataModel<T> dataModel)
  268. {
  269. if (ScriptDocument is null) return false;
  270. return ScriptDocument.Execute(methodname: "Check", parameters: [dataModel]);
  271. }
  272. }
  273. #endregion
  274. #region Actions
  275. [Caption("Custom Script")]
  276. public class ScriptSaveEventAction<T> : IEventAction<SaveEvent<T>>
  277. where T : Entity, new()
  278. {
  279. private ScriptDocument? _scriptDocument;
  280. private ScriptDocument? ScriptDocument
  281. {
  282. get
  283. {
  284. if(_scriptDocument is null && Script is not null)
  285. {
  286. _scriptDocument = new(Script);
  287. _scriptDocument.SetValue("Result", null);
  288. _scriptDocument.Compile();
  289. }
  290. return _scriptDocument;
  291. }
  292. }
  293. private string? _script;
  294. public string? Script
  295. {
  296. get => _script;
  297. set
  298. {
  299. if(_script != value)
  300. {
  301. _script = value;
  302. _scriptDocument = null;
  303. }
  304. }
  305. }
  306. public IEnumerable<string> ReferencedVariables
  307. {
  308. get
  309. {
  310. var method = ScriptDocument?.GetMethod(methodName: "RequiredColumns");
  311. if(method is not null)
  312. {
  313. var cols = Columns.None<T>();
  314. method.Invoke(ScriptDocument!.GetObject(), [cols]);
  315. return cols.ColumnNames().Select(x => $"{typeof(T).Name}.{x}");
  316. }
  317. else
  318. {
  319. return [];
  320. }
  321. }
  322. }
  323. public string Description => "Custom Script";
  324. public string DefaultScript()
  325. {
  326. return @"using PRS.Shared.Events;
  327. public class Module
  328. {
  329. public object? Result { get; set; }
  330. public void RequiredColumns(Columns<" + typeof(T).Name + @"> columns)
  331. {
  332. // Modify 'columns' as required to get the required columns for the 'model.Entity'. If you don't provide these,
  333. // the data you require in 'Execute' may not be present. Example:
  334. // columns.Add(x => x.ID);
  335. }
  336. public bool Execute(SaveEventDataModel<" + typeof(T).Name + @"> model)
  337. {
  338. // 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.
  339. return true;
  340. }
  341. }";
  342. }
  343. public object? Execute(IEventDataModel dataModel)
  344. {
  345. if (ScriptDocument is null) return null;
  346. var model = dataModel.RootModel<SaveEventDataModel<T>>();
  347. if(ScriptDocument.Execute(methodname: "Execute", parameters: [model]))
  348. {
  349. return ScriptDocument.GetValue("Result");
  350. }
  351. else
  352. {
  353. return null;
  354. }
  355. }
  356. }
  357. [Caption("Create Entity")]
  358. public class CreateEntitySaveEventAction<T> : IEventAction<SaveEvent<T>>
  359. where T : Entity, new()
  360. {
  361. [JsonPropertyOrder(1)]
  362. public Type? EntityType { get; set; }
  363. [JsonIgnore]
  364. public List<PropertyInitializer> Initializers { get; set; } = new List<PropertyInitializer>();
  365. private ScriptDocument? _scriptDocument;
  366. private ScriptDocument? ScriptDocument
  367. {
  368. get
  369. {
  370. if(_scriptDocument is null && Script is not null)
  371. {
  372. _scriptDocument = new(Script);
  373. _scriptDocument.Compile();
  374. }
  375. return _scriptDocument;
  376. }
  377. }
  378. private string? _script;
  379. public string? Script
  380. {
  381. get => _script;
  382. set
  383. {
  384. if(_script != value)
  385. {
  386. _script = value;
  387. _scriptDocument = null;
  388. }
  389. }
  390. }
  391. private IEnumerable<string> ScriptReferencedVariables
  392. {
  393. get
  394. {
  395. var method = ScriptDocument?.GetMethod(methodName: "RequiredColumns");
  396. if(method is not null)
  397. {
  398. var cols = Columns.None<T>();
  399. method.Invoke(ScriptDocument!.GetObject(), [cols]);
  400. return cols.ColumnNames().Select(x => $"{typeof(T).Name}.{x}");
  401. }
  402. else
  403. {
  404. return [];
  405. }
  406. }
  407. }
  408. public string DefaultScript()
  409. {
  410. if (EntityType is null) return "Please select an entity type first.";
  411. return @"using PRS.Shared.Events;
  412. public class Module
  413. {
  414. public void RequiredColumns(Columns<" + typeof(T).Name + @"> columns)
  415. {
  416. // Modify 'columns' as required to get the required columns for the 'model.Entity'. If you don't provide these,
  417. // the data you require in 'Execute' may not be present. Example:
  418. // columns.Add(x => x.ID);
  419. }
  420. public void Execute(SaveEventDataModel<" + typeof(T).Name + @"> model, " + EntityType.Name + " new" + EntityType.Name + @")
  421. {
  422. // Modify new" + EntityType.Name + @" as you wish, using model.Entity to get the required data. This method runs after
  423. // the property initializers (in the previous screen) for the Create Entity action have run.
  424. }
  425. }";
  426. }
  427. public string Description => $"Create New {EntityType?.Name ?? "Entity"}";
  428. public IEnumerable<string> ReferencedVariables => Initializers.SelectMany(x => x.ReferencedVariables).Concat(ScriptReferencedVariables);
  429. public object? Execute(IEventDataModel dataModel)
  430. {
  431. if(EntityType is null)
  432. {
  433. return null;
  434. }
  435. var entity = (Activator.CreateInstance(EntityType) as Entity)!;
  436. foreach(var initializer in Initializers)
  437. {
  438. initializer.Execute(entity, dataModel);
  439. }
  440. if(ScriptDocument is not null)
  441. {
  442. var model = dataModel.RootModel<SaveEventDataModel<T>>();
  443. ScriptDocument.Execute(methodname: "Execute", parameters: [model, entity]);
  444. }
  445. DbFactory.FindStore(EntityType, Guid.Empty, "", default, "", Logger.Main).Save(entity, "");
  446. return entity;
  447. }
  448. #region Serialization Stuff
  449. [JsonPropertyOrder(2)]
  450. [JsonInclude]
  451. [JsonPropertyName("Initializers")]
  452. private PropertyInitializerSerializationItem[] _initializers
  453. {
  454. get => Initializers.ToArray(x => new PropertyInitializerSerializationItem { Property = x.Property.Name, Value = x.Value });
  455. set
  456. {
  457. Initializers.Clear();
  458. Initializers.AddRange(value.Select(x => new PropertyInitializer(EntityType!, x)));
  459. }
  460. }
  461. #endregion
  462. }
  463. internal class PropertyInitializerSerializationItem
  464. {
  465. public string Property { get; set; }
  466. public string Value { get; set; }
  467. }
  468. public class PropertyInitializer
  469. {
  470. public IProperty Property { get; set; }
  471. private CoreExpression? _valueExpression;
  472. private CoreExpression ValueExpression
  473. {
  474. get
  475. {
  476. _valueExpression ??= new CoreExpression(Value, Property.PropertyType);
  477. return _valueExpression;
  478. }
  479. }
  480. private string _value;
  481. public string Value
  482. {
  483. get => _value;
  484. [MemberNotNull(nameof(_value))]
  485. set
  486. {
  487. if(value != _value)
  488. {
  489. _value = value;
  490. _valueExpression = null;
  491. }
  492. }
  493. }
  494. public IEnumerable<string> ReferencedVariables => ValueExpression.ReferencedVariables;
  495. internal PropertyInitializer(Type entityType, PropertyInitializerSerializationItem item)
  496. {
  497. Value = item.Value;
  498. Property = DatabaseSchema.PropertyStrict(entityType, item.Property);
  499. }
  500. public PropertyInitializer(IProperty property, string value)
  501. {
  502. Property = property;
  503. Value = value;
  504. }
  505. public void Execute(Entity entity, IEventDataModel dataModel)
  506. {
  507. Property.Setter()(entity, ValueExpression.Evaluate(dataModel));
  508. }
  509. }
  510. #endregion