BaseStore.cs 7.9 KB

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