EntityFormStore.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Threading.Tasks;
  4. using Comal.Classes;
  5. using InABox.Core;
  6. using System;
  7. namespace Comal.Stores
  8. {
  9. public class FormSubscription
  10. {
  11. public Guid EmployeeID { get; set; }
  12. public Guid FormID { get; set; }
  13. }
  14. public static class FormSubscriptionCache
  15. {
  16. static FormSubscriptionCache()
  17. {
  18. Subscriptions = null;
  19. }
  20. public static FormSubscription[] Subscriptions { get; set; }
  21. }
  22. public abstract class EntityFormStore<TEntity, TParent, TParentLink> : BaseStore<TEntity>
  23. where TEntity : EntityForm<TParent, TParentLink, TEntity>, new()
  24. where TParent : Entity, new()
  25. where TParentLink : IEntityLink<TParent>, new()
  26. {
  27. private void CheckFormSubscriptionCache()
  28. {
  29. if (FormSubscriptionCache.Subscriptions == null)
  30. {
  31. Logger.Send(LogType.Information, UserID, "Refreshing Form Subscription Cache");
  32. var subs = Provider.Query(
  33. null,
  34. Columns.None<EmployeeFormSubscription>().Add(x => x.Employee.ID, x => x.Form.ID)
  35. );
  36. FormSubscriptionCache.Subscriptions = subs.Rows.Select(r =>
  37. new FormSubscription
  38. {
  39. EmployeeID = r.Get<EmployeeFormSubscription, Guid>(c => c.Employee.ID),
  40. FormID = r.Get<EmployeeFormSubscription, Guid>(c => c.Form.ID)
  41. }
  42. ).ToArray();
  43. }
  44. }
  45. private void CheckFormSubscriptions(TEntity entity)
  46. {
  47. if (entity is IDigitalFormInstance instance
  48. && entity.HasOriginalValue(x=>x.FormCompleted)
  49. && entity.GetOriginalValue(x=>x.FormCompleted) == DateTime.MinValue
  50. )
  51. {
  52. var updates = new List<Notification>();
  53. CheckFormSubscriptionCache();
  54. var formid = entity.Form.ID; //(Guid)CoreUtils.GetPropertyValue(entity, "Form.ID");
  55. var formname = entity.Form.Description; //(string)CoreUtils.GetPropertyValue(entity, "Form.Description");
  56. var userid = entity.FormCompletedBy.ID; //CoreUtils.GetPropertyValue(entity, "FormCompletedBy.ID");
  57. if (userid == Guid.Empty)
  58. userid = UserGuid;
  59. var emprow = userid != Guid.Empty
  60. ? Provider.Query(
  61. new Filter<Employee>(x => x.UserLink.ID).IsEqualTo(userid),
  62. Columns.None<Employee>().Add(x => x.ID, x => x.Name)
  63. ).Rows.FirstOrDefault()
  64. : null;
  65. var empid = emprow != null ? emprow.Get<Employee, Guid>(x => x.ID) : Guid.Empty;
  66. var empname = emprow != null ? emprow.Get<Employee, string>(x => x.Name) : UserID;
  67. var subscriptions = FormSubscriptionCache.Subscriptions.Where(x => x.FormID == formid).ToArray();
  68. foreach (var subscription in subscriptions)
  69. {
  70. var notification = new Notification();
  71. notification.Employee.ID = subscription.EmployeeID;
  72. notification.Sender.ID = empid;
  73. notification.Title = "Form Completed: " + formname;
  74. notification.Description = string.Format("<html><body>{0} has completed a <u><b>{1}</b></u> digital form.</body></html>", empname,
  75. formname);
  76. notification.EntityType = entity.GetType().EntityName();
  77. notification.EntityID = entity.ID;
  78. updates.Add(notification);
  79. }
  80. if (updates.Any())
  81. FindSubStore<Notification>().Save(updates, "");
  82. }
  83. }
  84. protected override void AfterSave(TEntity entity)
  85. {
  86. base.AfterSave(entity);
  87. CheckFormSubscriptions(entity);
  88. CheckParentStatus(entity);
  89. }
  90. private void CheckParentStatus(TEntity entity)
  91. {
  92. if (entity.HasOriginalValue(x=>x.FormCompleted) || entity.HasOriginalValue(x=>x.FormCancelled))
  93. {
  94. bool isFinal = false;
  95. TParent? parent = null;
  96. Task[] tasks = new Task[]
  97. {
  98. Task.Run(() =>
  99. {
  100. isFinal = Provider.Query(
  101. new Filter<DigitalForm>(x => x.ID).IsEqualTo(entity.Form.ID),
  102. Columns.None<DigitalForm>().Add(x => x.Final)
  103. ).Rows.FirstOrDefault()?.Get<DigitalForm, bool>(x => x.Final) ?? false;
  104. }),
  105. Task.Run(() =>
  106. {
  107. parent = Provider.Query(
  108. new Filter<TParent>(x=>x.ID).IsEqualTo(entity.ParentID()),
  109. LookupFactory.RequiredColumns<TParent>()
  110. ).Rows
  111. .FirstOrDefault()?
  112. .ToObject<TParent>();
  113. })
  114. };
  115. Task.WaitAll(tasks);
  116. if (isFinal && (parent != null))
  117. {
  118. UpdateParentStatus(entity, parent);
  119. if (parent.IsChanged())
  120. FindSubStore<TParent>().Save(parent, $"{typeof(TParent).EntityName().Split('.').Last()} status updated by {entity.Form.Description}");
  121. }
  122. }
  123. }
  124. protected virtual void UpdateParentStatus(IDigitalFormInstance form, TParent parent)
  125. {
  126. }
  127. }
  128. }