BaseStore.cs 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 string 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(Platform) || 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. var prop = DatabaseSchema.Property(typeof(User), Platform);
  56. if (prop != null && prop.Getter() != null && prop.Setter() != null)
  57. {
  58. var user = Provider.Load(new Filter<User>(x => x.UserID).IsEqualTo(UserID)).FirstOrDefault();
  59. if (user != null)
  60. {
  61. var value = prop.Getter().Invoke(user);
  62. if (!Version.Equals(value))
  63. {
  64. prop.Setter().Invoke(user, Version);
  65. Provider.Save(user);
  66. }
  67. }
  68. }
  69. }
  70. }
  71. protected void UnlinkTrackingKanban<TEntityKanban, TEntity, TEntityLink>(TEntity entity)
  72. where TEntityKanban : EntityKanban<TEntity, TEntityLink>, new()
  73. where TEntity : Entity
  74. where TEntityLink : IEntityLink<TEntity>, new()
  75. {
  76. var kanbans = Provider.Query(
  77. new Filter<TEntityKanban>(x => x.Entity.ID).IsEqualTo(entity.ID).And(x => x.Kanban.Locked).IsEqualTo(true),
  78. new Columns<TEntityKanban>(
  79. x => x.Entity.ID,
  80. x => x.Kanban.ID,
  81. x => x.Kanban.Locked
  82. )
  83. );
  84. if (!kanbans.Rows.Any())
  85. return;
  86. var kanban = kanbans.Rows.First().ToObject<RequisitionKanban, KanbanLink, Kanban>(x => x.Kanban);
  87. kanban.Locked = false;
  88. Provider.Save(kanban);
  89. }
  90. protected void UpdateTrackingKanban<TEntityKanban, TEntity, TEntityLink>(TEntity entity, Func<TEntity, KanbanCategory> category)
  91. where TEntityKanban : EntityKanban<TEntity, TEntityLink>, new()
  92. where TEntity : Entity
  93. where TEntityLink : IEntityLink<TEntity>, new()
  94. {
  95. var kanbans = Provider.Query(
  96. new Filter<TEntityKanban>(x => x.Entity.ID).IsEqualTo(entity.ID),
  97. new Columns<TEntityKanban>(
  98. x => x.Entity.ID,
  99. x => x.Kanban.ID,
  100. x => x.Kanban.Category,
  101. x => x.Kanban.Closed,
  102. x => x.Kanban.Completed,
  103. x => x.Kanban.EmployeeLink.ID,
  104. x => x.Kanban.Notes,
  105. x => x.Kanban.DueDate,
  106. x => x.Kanban.Title,
  107. x => x.Kanban.Summary
  108. )
  109. );
  110. if (!kanbans.Rows.Any())
  111. return;
  112. var oldcategory = Kanban.StringToCategory(kanbans.Rows.First().Get<TEntityKanban, string>(c => c.Kanban.Category));
  113. var newcategory = category(entity);
  114. if (!Equals(newcategory, oldcategory))
  115. {
  116. var kanban = kanbans.Rows.First().ToObject<RequisitionKanban, KanbanLink, Kanban>(x => x.Kanban);
  117. kanban.Category = Kanban.CategoryToString(newcategory);
  118. var notes = kanban.Notes.ToList();
  119. notes.Add(string.Format("{0:yyyy-MM-dd HH:mm:ss} {1}: {2}", DateTime.Now, UserID, "Task Status updated by Requisition Progress"));
  120. kanban.Notes = notes.ToArray();
  121. kanban.Locked = true;
  122. FindSubStore<Kanban>().Save(kanban, "Updated due to Requisition Status Change");
  123. }
  124. }
  125. protected void NotifyEmployee<TEntity>(
  126. TEntity entity,
  127. Func<TEntity, Guid> employeeid,
  128. Func<TEntity, bool> filter,
  129. Func<TEntity, string> title,
  130. Func<TEntity, string> body
  131. ) where TEntity : Entity
  132. {
  133. if (!filter(entity))
  134. return;
  135. var employees = Provider.Query(
  136. new Filter<Employee>(x => x.ID).IsEqualTo(employeeid(entity)).Or(x => x.UserLink.ID).IsEqualTo(UserGuid),
  137. new Columns<Employee>(x => x.ID).Add(x => x.UserLink.ID).Add(x => x.Name)
  138. ).Rows.Select(r => new Tuple<Guid, Guid, string>(
  139. r.Get<Employee, Guid>(c => c.ID),
  140. r.Get<Employee, Guid>(c => c.UserLink.ID),
  141. r.Get<Employee, string>(c => c.Name)
  142. )
  143. );
  144. var recipient = employees.FirstOrDefault(x => Equals(x.Item1, employeeid(entity)));
  145. var sender = employees.FirstOrDefault(x => Equals(x.Item2, UserGuid));
  146. if (recipient == null || sender == null)
  147. return;
  148. if (recipient.Item2 == UserGuid)
  149. return;
  150. var notification = new Notification();
  151. notification.Employee.ID = recipient.Item1;
  152. notification.Sender.ID = sender.Item1;
  153. notification.Title = title(entity);
  154. var sb = new StringBuilder();
  155. sb.AppendLine(string.Format("Dear {0},\n", recipient.Item3.Split(' ').First()));
  156. sb.Append(body.Invoke(entity));
  157. if (sender != null)
  158. sb.AppendLine(string.Format("\n\nRegards,\n{0}.", sender.Item3.Split(' ').First()));
  159. notification.Description = sb.ToString();
  160. notification.EntityType = typeof(TEntity).EntityName();
  161. notification.EntityID = entity.ID;
  162. FindSubStore<Notification>().Save(notification, "");
  163. }
  164. }
  165. }