DigitalFormDataModel.cs 7.8 KB

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