DigitalFormDataModel.cs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Threading.Tasks;
  5. using InABox.Clients;
  6. namespace InABox.Core
  7. {
  8. public class DigitalFormDataModel<TEntity, TEntityLink, TInstance> : IDigitalFormDataModel
  9. where TEntity : Entity, IRemotable, IPersistent, new()
  10. where TEntityLink : EntityLink<TEntity>
  11. where TInstance : Entity, IRemotable, IPersistent, IDigitalFormInstance<TEntityLink>, new()
  12. {
  13. private readonly bool bRequiresLoad = true;
  14. public DigitalFormDataModel(Guid entityid, Guid instanceid)
  15. {
  16. Entity = new TEntity();
  17. Entity.ID = entityid;
  18. Entity.CommitChanges();
  19. Instance = new TInstance();
  20. Instance.ID = instanceid;
  21. Variables = new DigitalFormVariable[] { };
  22. bRequiresLoad = true;
  23. }
  24. public DigitalFormDataModel(TEntity entity, TInstance instance)
  25. {
  26. Entity = entity;
  27. Instance = instance;
  28. Variables = new DigitalFormVariable[] { };
  29. bRequiresLoad = false;
  30. }
  31. public Entity Entity { get; set; }
  32. public object? GetEntityValue(string name)
  33. {
  34. try
  35. {
  36. return CoreUtils.GetPropertyValue(Entity, name);
  37. }
  38. catch
  39. {
  40. return null;
  41. }
  42. }
  43. public void SetEntityValue(string name, object? value)
  44. {
  45. try
  46. {
  47. CoreUtils.SetPropertyValue(Entity, name, value);
  48. }
  49. catch (Exception e)
  50. {
  51. Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));
  52. }
  53. }
  54. IDigitalFormInstance IDigitalFormDataModel.Instance
  55. {
  56. get => Instance;
  57. set
  58. {
  59. Instance = (TInstance)value;
  60. }
  61. }
  62. public TInstance Instance { get; set; }
  63. public DigitalFormVariable[] Variables { get; set; }
  64. public event DigitalFormUpdateHandler OnModelSaved;
  65. public event DigitalFormBeforeUpdateHandler BeforeModelSaved;
  66. public void Load(Action<IDigitalFormDataModel>? callback = null)
  67. {
  68. if (!bRequiresLoad)
  69. {
  70. callback?.Invoke(this);
  71. return;
  72. }
  73. var client = new MultiQuery();
  74. DoAddQueries(client);
  75. if (callback == null)
  76. {
  77. if (client.Count > 0)
  78. {
  79. client.Query();
  80. DoParseQueries(client);
  81. }
  82. }
  83. else
  84. {
  85. if (client.Count > 0)
  86. client.Query(c =>
  87. {
  88. DoParseQueries(client);
  89. callback.Invoke(this);
  90. });
  91. else
  92. callback.Invoke(this);
  93. }
  94. }
  95. public void Update(Action<IDigitalFormDataModel>? callback = null)
  96. {
  97. //this never get used and causes a crash in the app for Job ITP forms - maybe time for a rebuild?
  98. //if (!bRequiresLoad)
  99. //{
  100. // callback.Invoke(this);
  101. // return;
  102. //}
  103. if (callback == null)
  104. DoUpdate();
  105. else
  106. Task.Run(() =>
  107. {
  108. DoUpdate();
  109. callback.Invoke(this);
  110. });
  111. }
  112. public virtual void AddQueries(MultiQuery client)
  113. {
  114. }
  115. private void DoAddQueries(MultiQuery client)
  116. {
  117. if (Entity.ID != Guid.Empty)
  118. client.Add(
  119. new QueryDef<TEntity>(
  120. new Filter<TEntity>(x => x.ID).IsEqualTo(Entity.ID),
  121. Columns.None<TEntity>().Add(x => x.ID),
  122. null
  123. ),
  124. typeof(TEntity)
  125. );
  126. if (Instance.ID != Guid.Empty)
  127. client.Add(
  128. new QueryDef<TInstance>(
  129. new Filter<TInstance>(x => x.ID).IsEqualTo(Instance.ID),
  130. Columns.None<TInstance>().Add
  131. (
  132. x => x.ID,
  133. x => x.Number,
  134. x => x.FormData,
  135. x => x.Form.ID,
  136. x => x.Form.Description,
  137. x => x.Form.DescriptionExpression,
  138. x => x.FormCompleted,
  139. x => x.FormCompletedBy.ID,
  140. x => x.Created,
  141. x => x.FormOpen,
  142. x => x.BlobData
  143. ),
  144. null
  145. ),
  146. typeof(TInstance)
  147. );
  148. AddQueries(client);
  149. }
  150. public virtual void ParseQueries(MultiQuery client)
  151. {
  152. }
  153. private void DoParseQueries(MultiQuery client)
  154. {
  155. if (client.Contains(typeof(TEntity)))
  156. {
  157. var table = client.Get(typeof(TEntity));
  158. if (table.Rows.Any())
  159. Entity = table.Rows.First().ToObject<TEntity>();
  160. }
  161. if (client.Contains(typeof(TInstance)))
  162. {
  163. var table = client.Get(typeof(TInstance));
  164. if (table.Rows.Any())
  165. Instance = table.Rows.First().ToObject<TInstance>();
  166. }
  167. ParseQueries(client);
  168. }
  169. protected virtual void AddPrimaryUpdates(MultiSave client)
  170. {
  171. }
  172. protected virtual void AddSecondaryUpdates(MultiSave client)
  173. {
  174. }
  175. public void DuplicateInstance()
  176. {
  177. var formid = Instance.Form.ID;
  178. Instance = new TInstance();
  179. Instance.Parent.ID = Entity.ID;
  180. Instance.Form.ID = formid;
  181. }
  182. public string? EvaluateExpression(string expression)
  183. {
  184. if (string.IsNullOrWhiteSpace(expression))
  185. return null;
  186. Dictionary<string, object?> variables = new Dictionary<string, object?>();
  187. var formData = DigitalForm.DeserializeFormData(Instance);
  188. if(formData != null)
  189. {
  190. foreach (var item in formData.Items())
  191. variables[$"Data.{item.Key}"] = item.Value;
  192. }
  193. foreach (var property in DatabaseSchema.Properties(Instance.GetType()).Where(x => !x.Name.StartsWith("Parent.")))
  194. variables[$"Form.{property.Name}"] = property.Getter()(Instance);
  195. foreach (var property in DatabaseSchema.Properties(Entity.GetType()))
  196. variables[$"{typeof(TEntity).Name}.{property.Name}"] = property.Getter()(Entity);
  197. var exp = new CoreExpression(expression);
  198. return exp.Evaluate(variables)?.ToString();
  199. }
  200. private void DoUpdate()
  201. {
  202. var result = EvaluateExpression(Instance.Form.DescriptionExpression);
  203. Instance.Description = !String.IsNullOrWhiteSpace(result)
  204. ? result
  205. : Instance.Form.Description;
  206. BeforeModelSaved?.Invoke(this);
  207. var entityaudittrail = "";
  208. var instanceaudittrail = "";
  209. var parents = new MultiSave();
  210. if (Entity.IsChanged())
  211. {
  212. parents.Add(typeof(TEntity), Entity);
  213. entityaudittrail = "Updated from App - Digital Forms Module";
  214. }
  215. else if (Entity.ID == Guid.Empty)
  216. {
  217. parents.Add(typeof(TEntity), Entity);
  218. entityaudittrail = "Created from App - Digital Forms Module";
  219. }
  220. AddPrimaryUpdates(parents);
  221. parents.Save(null, entityaudittrail);
  222. ((IDigitalFormInstance<TEntityLink>)Instance).Parent.ID = Entity.ID; //be careful of bug here in future due to cast
  223. var children = new MultiSave();
  224. if (Instance.IsChanged())
  225. {
  226. children.Add(typeof(TInstance), (TInstance)Instance);
  227. instanceaudittrail = "Updated from App - Digital Forms Module";
  228. }
  229. else if (Entity.ID == Guid.Empty)
  230. {
  231. children.Add(typeof(TInstance), (TInstance)Instance);
  232. instanceaudittrail = "Created from App - Digital Forms Module";
  233. }
  234. AddSecondaryUpdates(children);
  235. children.Save(null, instanceaudittrail);
  236. OnModelSaved?.Invoke(this);
  237. }
  238. }
  239. }