DigitalFormDataModel.cs 8.9 KB

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