DigitalFormDataModel.cs 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. bRequiresLoad = true;
  22. }
  23. public DigitalFormDataModel(TEntity entity, TInstance instance)
  24. {
  25. Entity = entity;
  26. Instance = instance;
  27. bRequiresLoad = false;
  28. }
  29. public Entity Entity { get; set; }
  30. public object? GetEntityValue(string name)
  31. {
  32. try
  33. {
  34. return CoreUtils.GetPropertyValue(Entity, name);
  35. }
  36. catch (Exception e)
  37. {
  38. return null;
  39. }
  40. }
  41. public void SetEntityValue(string name, object? value)
  42. {
  43. try
  44. {
  45. CoreUtils.SetPropertyValue(Entity, name, value);
  46. }
  47. catch (Exception e)
  48. {
  49. Logger.Send(LogType.Error, "", string.Format("*** Unknown Error: {0}\n{1}", e.Message, e.StackTrace));
  50. }
  51. }
  52. public IDigitalFormInstance Instance { get; set; }
  53. public event DigitalFormUpdateHandler OnModelSaved;
  54. public event DigitalFormBeforeUpdateHandler BeforeModelSaved;
  55. public void Load(Action<IDigitalFormDataModel>? callback = null)
  56. {
  57. if (!bRequiresLoad)
  58. {
  59. callback?.Invoke(this);
  60. return;
  61. }
  62. var client = new MultiQuery();
  63. DoAddQueries(client);
  64. if (callback == null)
  65. {
  66. if (client.Count > 0)
  67. {
  68. client.Query();
  69. DoParseQueries(client);
  70. }
  71. }
  72. else
  73. {
  74. if (client.Count > 0)
  75. client.Query(c =>
  76. {
  77. DoParseQueries(client);
  78. callback.Invoke(this);
  79. });
  80. else
  81. callback.Invoke(this);
  82. }
  83. }
  84. public void Update(Action<IDigitalFormDataModel>? callback = null)
  85. {
  86. //this never get used and causes a crash in the app for Job ITP forms - maybe time for a rebuild?
  87. //if (!bRequiresLoad)
  88. //{
  89. // callback.Invoke(this);
  90. // return;
  91. //}
  92. if (callback == null)
  93. DoUpdate();
  94. else
  95. Task.Run(() =>
  96. {
  97. DoUpdate();
  98. callback.Invoke(this);
  99. });
  100. }
  101. public virtual void AddQueries(MultiQuery client)
  102. {
  103. }
  104. private void DoAddQueries(MultiQuery client)
  105. {
  106. if (Entity.ID != Guid.Empty)
  107. client.Add(
  108. new QueryDef<TEntity>(
  109. new Filter<TEntity>(x => x.ID).IsEqualTo(Entity.ID),
  110. new Columns<TEntity>(x => x.ID),
  111. null
  112. ),
  113. typeof(TEntity)
  114. );
  115. if (Instance.ID != Guid.Empty)
  116. client.Add(
  117. new QueryDef<TInstance>(
  118. new Filter<TInstance>(x => x.ID).IsEqualTo(Instance.ID),
  119. new Columns<TInstance>
  120. (
  121. x => x.ID,
  122. x => x.FormData,
  123. x => x.Form.ID,
  124. x => x.FormCompleted,
  125. x => x.FormCompletedBy.ID,
  126. x => x.Created,
  127. x => x.FormOpen,
  128. x => x.BlobData
  129. ),
  130. null
  131. ),
  132. typeof(TInstance)
  133. );
  134. AddQueries(client);
  135. }
  136. public virtual void ParseQueries(MultiQuery client)
  137. {
  138. }
  139. private void DoParseQueries(MultiQuery client)
  140. {
  141. if (client.Contains(typeof(TEntity)))
  142. {
  143. var table = client.Get(typeof(TEntity));
  144. if (table.Rows.Any())
  145. Entity = table.Rows.First().ToObject<TEntity>();
  146. }
  147. if (client.Contains(typeof(TInstance)))
  148. {
  149. var table = client.Get(typeof(TInstance));
  150. if (table.Rows.Any())
  151. Instance = table.Rows.First().ToObject<TInstance>();
  152. }
  153. ParseQueries(client);
  154. }
  155. protected virtual void AddPrimaryUpdates(MultiSave client)
  156. {
  157. }
  158. protected virtual void AddSecondaryUpdates(MultiSave client)
  159. {
  160. }
  161. private void DoUpdate()
  162. {
  163. BeforeModelSaved?.Invoke(this);
  164. var entityaudittrail = "";
  165. var instanceaudittrail = "";
  166. var parents = new MultiSave();
  167. if (Entity.IsChanged())
  168. {
  169. parents.Add(typeof(TEntity), Entity);
  170. entityaudittrail = "Updated from App - Digital Forms Module";
  171. }
  172. else if (Entity.ID == Guid.Empty)
  173. {
  174. parents.Add(typeof(TEntity), Entity);
  175. entityaudittrail = "Created from App - Digital Forms Module";
  176. }
  177. AddPrimaryUpdates(parents);
  178. parents.Save(null, entityaudittrail);
  179. ((IDigitalFormInstance<TEntityLink>)Instance).Parent.ID = Entity.ID; //be careful of bug here in future due to cast
  180. var children = new MultiSave();
  181. if (Instance.IsChanged())
  182. {
  183. children.Add(typeof(TInstance), (TInstance)Instance);
  184. instanceaudittrail = "Updated from App - Digital Forms Module";
  185. }
  186. else if (Entity.ID == Guid.Empty)
  187. {
  188. children.Add(typeof(TInstance), (TInstance)Instance);
  189. instanceaudittrail = "Created from App - Digital Forms Module";
  190. }
  191. AddSecondaryUpdates(children);
  192. children.Save(null, instanceaudittrail);
  193. OnModelSaved?.Invoke(this);
  194. }
  195. }
  196. }