BaseStore.cs 7.5 KB

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