DigitalFormDataModel.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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. public IDigitalFormInstance Instance { get; set; }
  55. public DigitalFormVariable[] Variables { get; set; }
  56. public event DigitalFormUpdateHandler OnModelSaved;
  57. public event DigitalFormBeforeUpdateHandler BeforeModelSaved;
  58. public void Load(Action<IDigitalFormDataModel>? callback = null)
  59. {
  60. if (!bRequiresLoad)
  61. {
  62. callback?.Invoke(this);
  63. return;
  64. }
  65. var client = new MultiQuery();
  66. DoAddQueries(client);
  67. if (callback == null)
  68. {
  69. if (client.Count > 0)
  70. {
  71. client.Query();
  72. DoParseQueries(client);
  73. }
  74. }
  75. else
  76. {
  77. if (client.Count > 0)
  78. client.Query(c =>
  79. {
  80. DoParseQueries(client);
  81. callback.Invoke(this);
  82. });
  83. else
  84. callback.Invoke(this);
  85. }
  86. }
  87. public void Update(Action<IDigitalFormDataModel>? callback = null)
  88. {
  89. //this never get used and causes a crash in the app for Job ITP forms - maybe time for a rebuild?
  90. //if (!bRequiresLoad)
  91. //{
  92. // callback.Invoke(this);
  93. // return;
  94. //}
  95. if (callback == null)
  96. DoUpdate();
  97. else
  98. Task.Run(() =>
  99. {
  100. DoUpdate();
  101. callback.Invoke(this);
  102. });
  103. }
  104. public virtual void AddQueries(MultiQuery client)
  105. {
  106. }
  107. private void DoAddQueries(MultiQuery client)
  108. {
  109. if (Entity.ID != Guid.Empty)
  110. client.Add(
  111. new QueryDef<TEntity>(
  112. new Filter<TEntity>(x => x.ID).IsEqualTo(Entity.ID),
  113. new Columns<TEntity>(x => x.ID),
  114. null
  115. ),
  116. typeof(TEntity)
  117. );
  118. if (Instance.ID != Guid.Empty)
  119. client.Add(
  120. new QueryDef<TInstance>(
  121. new Filter<TInstance>(x => x.ID).IsEqualTo(Instance.ID),
  122. new Columns<TInstance>
  123. (
  124. x => x.ID,
  125. x => x.Number,
  126. x => x.FormData,
  127. x => x.Form.ID,
  128. x => x.Form.Description,
  129. x => x.Form.DescriptionExpression,
  130. x => x.FormCompleted,
  131. x => x.FormCompletedBy.ID,
  132. x => x.Created,
  133. x => x.FormOpen,
  134. x => x.BlobData
  135. ),
  136. null
  137. ),
  138. typeof(TInstance)
  139. );
  140. AddQueries(client);
  141. }
  142. public virtual void ParseQueries(MultiQuery client)
  143. {
  144. }
  145. private void DoParseQueries(MultiQuery client)
  146. {
  147. if (client.Contains(typeof(TEntity)))
  148. {
  149. var table = client.Get(typeof(TEntity));
  150. if (table.Rows.Any())
  151. Entity = table.Rows.First().ToObject<TEntity>();
  152. }
  153. if (client.Contains(typeof(TInstance)))
  154. {
  155. var table = client.Get(typeof(TInstance));
  156. if (table.Rows.Any())
  157. Instance = table.Rows.First().ToObject<TInstance>();
  158. }
  159. ParseQueries(client);
  160. }
  161. protected virtual void AddPrimaryUpdates(MultiSave client)
  162. {
  163. }
  164. protected virtual void AddSecondaryUpdates(MultiSave client)
  165. {
  166. }
  167. public void DuplicateInstance()
  168. {
  169. var formid = Instance.Form.ID;
  170. Instance = new TInstance();
  171. Instance.Parent.ID = Entity.ID;
  172. Instance.Form.ID = formid;
  173. }
  174. private void DoUpdate()
  175. {
  176. if (!String.IsNullOrWhiteSpace(this.Instance.Form.DescriptionExpression))
  177. {
  178. Dictionary<string, object?> variables = new Dictionary<string, object?>();
  179. var formData = DigitalForm.DeserializeFormData(Instance);
  180. foreach (var item in formData.Items())
  181. variables[$"Data.{item.Key}"] = item.Value;
  182. var instancedata = Entity.GetValues(true);
  183. foreach (var item in instancedata)
  184. variables[$"Form.{item.Key}"] = item.Value;
  185. var entitydata = Entity.GetValues(true);
  186. foreach (var item in entitydata)
  187. variables[$"{typeof(TEntity).EntityName().Split('.').Last()}.{item.Key}"] = item.Value;
  188. var expression = new CoreExpression(Instance.Form.DescriptionExpression);
  189. var result = expression.Evaluate(variables)?.ToString();
  190. Instance.Description = !String.IsNullOrWhiteSpace(result)
  191. ? result
  192. : Instance.Form.Description;
  193. }
  194. BeforeModelSaved?.Invoke(this);
  195. var entityaudittrail = "";
  196. var instanceaudittrail = "";
  197. var parents = new MultiSave();
  198. if (Entity.IsChanged())
  199. {
  200. parents.Add(typeof(TEntity), Entity);
  201. entityaudittrail = "Updated from App - Digital Forms Module";
  202. }
  203. else if (Entity.ID == Guid.Empty)
  204. {
  205. parents.Add(typeof(TEntity), Entity);
  206. entityaudittrail = "Created from App - Digital Forms Module";
  207. }
  208. AddPrimaryUpdates(parents);
  209. parents.Save(null, entityaudittrail);
  210. ((IDigitalFormInstance<TEntityLink>)Instance).Parent.ID = Entity.ID; //be careful of bug here in future due to cast
  211. var children = new MultiSave();
  212. if (Instance.IsChanged())
  213. {
  214. children.Add(typeof(TInstance), (TInstance)Instance);
  215. instanceaudittrail = "Updated from App - Digital Forms Module";
  216. }
  217. else if (Entity.ID == Guid.Empty)
  218. {
  219. children.Add(typeof(TInstance), (TInstance)Instance);
  220. instanceaudittrail = "Created from App - Digital Forms Module";
  221. }
  222. AddSecondaryUpdates(children);
  223. children.Save(null, instanceaudittrail);
  224. OnModelSaved?.Invoke(this);
  225. }
  226. }
  227. }