DigitalFormDataModel.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 (Exception e)
  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.FormCompleted,
  130. x => x.FormCompletedBy.ID,
  131. x => x.Created,
  132. x => x.FormOpen,
  133. x => x.BlobData
  134. ),
  135. null
  136. ),
  137. typeof(TInstance)
  138. );
  139. AddQueries(client);
  140. }
  141. public virtual void ParseQueries(MultiQuery client)
  142. {
  143. }
  144. private void DoParseQueries(MultiQuery client)
  145. {
  146. if (client.Contains(typeof(TEntity)))
  147. {
  148. var table = client.Get(typeof(TEntity));
  149. if (table.Rows.Any())
  150. Entity = table.Rows.First().ToObject<TEntity>();
  151. }
  152. if (client.Contains(typeof(TInstance)))
  153. {
  154. var table = client.Get(typeof(TInstance));
  155. if (table.Rows.Any())
  156. Instance = table.Rows.First().ToObject<TInstance>();
  157. }
  158. ParseQueries(client);
  159. }
  160. protected virtual void AddPrimaryUpdates(MultiSave client)
  161. {
  162. }
  163. protected virtual void AddSecondaryUpdates(MultiSave client)
  164. {
  165. }
  166. private void DoUpdate()
  167. {
  168. BeforeModelSaved?.Invoke(this);
  169. var entityaudittrail = "";
  170. var instanceaudittrail = "";
  171. var parents = new MultiSave();
  172. if (Entity.IsChanged())
  173. {
  174. parents.Add(typeof(TEntity), Entity);
  175. entityaudittrail = "Updated from App - Digital Forms Module";
  176. }
  177. else if (Entity.ID == Guid.Empty)
  178. {
  179. parents.Add(typeof(TEntity), Entity);
  180. entityaudittrail = "Created from App - Digital Forms Module";
  181. }
  182. AddPrimaryUpdates(parents);
  183. parents.Save(null, entityaudittrail);
  184. ((IDigitalFormInstance<TEntityLink>)Instance).Parent.ID = Entity.ID; //be careful of bug here in future due to cast
  185. var children = new MultiSave();
  186. if (Instance.IsChanged())
  187. {
  188. children.Add(typeof(TInstance), (TInstance)Instance);
  189. instanceaudittrail = "Updated from App - Digital Forms Module";
  190. }
  191. else if (Entity.ID == Guid.Empty)
  192. {
  193. children.Add(typeof(TInstance), (TInstance)Instance);
  194. instanceaudittrail = "Created from App - Digital Forms Module";
  195. }
  196. AddSecondaryUpdates(children);
  197. children.Save(null, instanceaudittrail);
  198. OnModelSaved?.Invoke(this);
  199. }
  200. }
  201. }