DigitalFormDataModel.cs 6.3 KB

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