BaseStore.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. using System.Collections.Generic;
  2. using System.Linq;
  3. using System.Text;
  4. using System.Threading.Tasks;
  5. using Comal.Classes;
  6. using InABox.Core;
  7. using InABox.Database;
  8. using System;
  9. using com.sun.org.glassfish.external.probe.provider.annotations;
  10. using PRS.Shared.Events;
  11. namespace Comal.Stores
  12. {
  13. public class UserPlatform
  14. {
  15. public string UserID { get; set; }
  16. public Platform Platform { get; set; }
  17. public string Version { get; set; }
  18. }
  19. public static class PlatformCache
  20. {
  21. static PlatformCache()
  22. {
  23. Platforms = new List<UserPlatform>();
  24. }
  25. public static List<UserPlatform> Platforms { get; }
  26. }
  27. public class BaseStore<T> : Store<T> where T : Entity, new()
  28. {
  29. protected override Filter<T>? PrepareFilter(Filter<T>? filter)
  30. {
  31. CheckPlatformVersion();
  32. return base.PrepareFilter(filter);
  33. }
  34. protected override void BeforeSave(T entity)
  35. {
  36. CheckPlatformVersion();
  37. if (entity is IProblems problems && problems.Problem.Notes?.Length == 1 &&
  38. string.IsNullOrEmpty(problems.Problem.Notes[0]))
  39. problems.Problem.Notes = null;
  40. base.BeforeSave(entity);
  41. }
  42. protected override void AfterSave(T entity)
  43. {
  44. base.AfterSave(entity);
  45. EventUtils.AfterSave(this, entity);
  46. }
  47. protected override void BeforeDelete(T entity)
  48. {
  49. CheckPlatformVersion();
  50. base.BeforeDelete(entity);
  51. }
  52. private void CheckPlatformVersion()
  53. {
  54. if (string.IsNullOrEmpty(UserID) || string.IsNullOrEmpty(Version))
  55. return;
  56. var platform = PlatformCache.Platforms.FirstOrDefault(x => x.UserID.Equals(UserID) && x.Platform.Equals(Platform));
  57. if (platform == null)
  58. {
  59. platform = new UserPlatform { UserID = UserID, Platform = Platform, Version = "???" };
  60. PlatformCache.Platforms.Add(platform);
  61. }
  62. if (!platform.Version.Equals(Version))
  63. {
  64. platform.Version = Version;
  65. if(Platform == Platform.Wpf || Platform == Platform.TimeBench)
  66. {
  67. var user = Provider.Load(new Filter<User>(x => x.UserID).IsEqualTo(UserID)).FirstOrDefault();
  68. if(user is not null)
  69. {
  70. var current = Platform switch
  71. {
  72. Platform.Wpf => user.Platform.DesktopVersion,
  73. Platform.TimeBench => user.Platform.MobileVersion,
  74. _ => ""
  75. };
  76. if (!Version.Equals(current))
  77. {
  78. switch (Platform)
  79. {
  80. case Platform.Wpf:
  81. user.Platform.DesktopVersion = Version;
  82. break;
  83. case Platform.TimeBench:
  84. user.Platform.MobileVersion = Version;
  85. break;
  86. }
  87. Provider.Save(user);
  88. }
  89. }
  90. }
  91. }
  92. }
  93. protected void UnlinkTrackingKanban<TEntityKanban, TEntity, TEntityLink>(TEntity entity)
  94. where TEntityKanban : EntityKanban<TEntity, TEntityLink>, new()
  95. where TEntity : Entity
  96. where TEntityLink : IEntityLink<TEntity>, new()
  97. {
  98. var kanbans = Provider.Query(
  99. new Filter<TEntityKanban>(x => x.Entity.ID).IsEqualTo(entity.ID).And(x => x.Kanban.Locked).IsEqualTo(true),
  100. Columns.Required<TEntityKanban>().Add(
  101. x => x.Entity.ID,
  102. x => x.Kanban.ID,
  103. x => x.Kanban.Locked
  104. )
  105. );
  106. if (!kanbans.Rows.Any())
  107. return;
  108. var kanban = kanbans.Rows.First().ToObject<RequisitionKanban, KanbanLink, Kanban>(x => x.Kanban);
  109. kanban.Locked = false;
  110. Provider.Save(kanban);
  111. }
  112. protected void UpdateTrackingKanban<TEntityKanban, TEntity, TEntityLink>(TEntity entity, Func<TEntity, KanbanStatus> status)
  113. where TEntityKanban : EntityKanban<TEntity, TEntityLink>, new()
  114. where TEntity : Entity
  115. where TEntityLink : IEntityLink<TEntity>, new()
  116. {
  117. var kanbans = Provider.Query(
  118. new Filter<TEntityKanban>(x => x.Entity.ID).IsEqualTo(entity.ID),
  119. Columns.Required<TEntityKanban>().Add(x => x.Entity.ID, x => x.Kanban.ID, x => x.Kanban.Status, x => x.Kanban.Closed, x => x.Kanban.Completed, x => x.Kanban.EmployeeLink.ID, x => x.Kanban.Notes, x => x.Kanban.DueDate, x => x.Kanban.Title, x => x.Kanban.Summary)
  120. );
  121. if (!kanbans.Rows.Any())
  122. return;
  123. var oldcategory = kanbans.Rows.First().Get<TEntityKanban, KanbanStatus>(c => c.Kanban.Status);
  124. var newcategory = status(entity);
  125. if (!Equals(newcategory, oldcategory))
  126. {
  127. var kanban = kanbans.Rows.First().ToObject<RequisitionKanban, KanbanLink, Kanban>(x => x.Kanban);
  128. kanban.Status = newcategory;
  129. var notes = kanban.Notes.ToList();
  130. notes.Add(string.Format("{0:yyyy-MM-dd HH:mm:ss} {1}: {2}", DateTime.Now, UserID, "Task Status updated by Requisition Progress"));
  131. kanban.Notes = notes.ToArray();
  132. kanban.Locked = true;
  133. FindSubStore<Kanban>().Save(kanban, "Updated due to Requisition Status Change");
  134. }
  135. }
  136. protected void NotifyEmployee<TEntity>(
  137. TEntity entity,
  138. Func<TEntity, Guid> employeeid,
  139. Func<TEntity, bool> filter,
  140. Func<TEntity, string> title,
  141. Func<TEntity, string> body
  142. ) where TEntity : Entity
  143. {
  144. if (!filter(entity))
  145. return;
  146. var employees = Provider.Query(
  147. new Filter<Employee>(x => x.ID).IsEqualTo(employeeid(entity)).Or(x => x.UserLink.ID).IsEqualTo(UserGuid),
  148. Columns.None<Employee>().Add(x => x.ID).Add(x => x.UserLink.ID).Add(x => x.Name)
  149. ).Rows.Select(r => new Tuple<Guid, Guid, string>(
  150. r.Get<Employee, Guid>(c => c.ID),
  151. r.Get<Employee, Guid>(c => c.UserLink.ID),
  152. r.Get<Employee, string>(c => c.Name)
  153. )
  154. );
  155. var recipient = employees.FirstOrDefault(x => Equals(x.Item1, employeeid(entity)));
  156. var sender = employees.FirstOrDefault(x => Equals(x.Item2, UserGuid));
  157. if (recipient == null || sender == null)
  158. return;
  159. if (recipient.Item2 == UserGuid)
  160. return;
  161. var notification = new Notification();
  162. notification.Employee.ID = recipient.Item1;
  163. notification.Sender.ID = sender.Item1;
  164. notification.Title = title(entity);
  165. var sb = new StringBuilder();
  166. sb.AppendLine(string.Format("Dear {0},\n", recipient.Item3.Split(' ').First()));
  167. sb.Append(body.Invoke(entity));
  168. if (sender != null)
  169. sb.AppendLine(string.Format("\n\nRegards,\n{0}.", sender.Item3.Split(' ').First()));
  170. notification.Description = sb.ToString();
  171. notification.EntityType = typeof(TEntity).EntityName();
  172. notification.EntityID = entity.ID;
  173. FindSubStore<Notification>().Save(notification, "");
  174. }
  175. protected Guid GetEmployeeIDFromUserGuid()
  176. {
  177. return Provider.Query(
  178. new Filter<Employee>(x => x.UserLink.ID).IsEqualTo(UserGuid),
  179. Columns.None<Employee>().Add(x => x.ID)
  180. ).Rows.FirstOrDefault()?
  181. .Get<Employee, Guid>(x => x.ID) ?? Guid.Empty;
  182. }
  183. }
  184. }