EntityFormStore.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using Comal.Classes;
  5. using InABox.Core;
  6. namespace Comal.Stores
  7. {
  8. public class FormSubscription
  9. {
  10. public Guid EmployeeID { get; set; }
  11. public Guid FormID { get; set; }
  12. }
  13. public static class FormSubscriptionCache
  14. {
  15. static FormSubscriptionCache()
  16. {
  17. Subscriptions = null;
  18. }
  19. public static FormSubscription[] Subscriptions { get; set; }
  20. }
  21. public abstract class EntityFormStore<TEntity, TParent, TParentLink> : BaseStore<TEntity>
  22. where TEntity : EntityForm<TParent, TParentLink>, new()
  23. where TParent : Entity, new()
  24. where TParentLink : IEntityLink<TParent>, new()
  25. {
  26. private void CheckFormSubscriptionCache()
  27. {
  28. if (FormSubscriptionCache.Subscriptions == null)
  29. {
  30. Logger.Send(LogType.Information, UserID, "Refreshing Form Subscription Cache");
  31. var subs = Provider.Query(
  32. null,
  33. new Columns<EmployeeFormSubscription>(x => x.Employee.ID, x => x.Form.ID)
  34. );
  35. FormSubscriptionCache.Subscriptions = subs.Rows.Select(r =>
  36. new FormSubscription
  37. {
  38. EmployeeID = r.Get<EmployeeFormSubscription, Guid>(c => c.Employee.ID),
  39. FormID = r.Get<EmployeeFormSubscription, Guid>(c => c.Form.ID)
  40. }
  41. ).ToArray();
  42. }
  43. }
  44. private void CheckFormSubscriptions(TEntity entity)
  45. {
  46. var instance = entity as IDigitalFormInstance;
  47. if (instance != null
  48. && entity.HasOriginalValue("FormCompleted")
  49. && entity.GetOriginalValue<TEntity, DateTime>("FormCompleted") == DateTime.MinValue
  50. )
  51. {
  52. var updates = new List<Notification>();
  53. CheckFormSubscriptionCache();
  54. var formid = (Guid)CoreUtils.GetPropertyValue(entity, "Form.ID");
  55. var formname = (string)CoreUtils.GetPropertyValue(entity, "Form.Description");
  56. var userid = (Guid)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. new Columns<Employee>(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. }
  89. }
  90. public class AssignmentFormStore : EntityFormStore<AssignmentForm, Assignment, AssignmentLink>
  91. {
  92. }
  93. public class LeaveRequestFormStore : EntityFormStore<LeaveRequestForm, LeaveRequest, LeaveRequestLink>
  94. {
  95. }
  96. public class JobITPFormStore : EntityFormStore<JobITPForm, JobITP, JobITPLink>
  97. {
  98. }
  99. public class JobFormStore : EntityFormStore<JobForm, Job, JobLink>
  100. {
  101. }
  102. public class EmployeeFormStore : EntityFormStore<EmployeeForm, Employee, EmployeeLink>
  103. {
  104. }
  105. public class TimeSheetFormStore : EntityFormStore<TimeSheetForm, TimeSheet, TimeSheetLink>
  106. {
  107. }
  108. public class KanbanFormStore : EntityFormStore<KanbanForm, Kanban, KanbanLink>
  109. {
  110. }
  111. }