using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Comal.Classes; using InABox.Core; using InABox.Database; namespace Comal.Stores { public class UserPlatform { public string UserID { get; set; } public string Platform { get; set; } public string Version { get; set; } } public static class PlatformCache { static PlatformCache() { Platforms = new List(); } public static List Platforms { get; } } public class BaseStore : Store where T : Entity, new() { protected override Filter PrepareFilter(Filter filter) { CheckPlatformVersion(); return base.PrepareFilter(filter); } protected override void BeforeSave(T entity) { CheckPlatformVersion(); base.BeforeSave(entity); } protected override void BeforeDelete(T entity) { CheckPlatformVersion(); base.BeforeDelete(entity); } private void CheckPlatformVersion() { if (string.IsNullOrEmpty(UserID) || string.IsNullOrEmpty(Platform) || string.IsNullOrEmpty(Version)) return; var platform = PlatformCache.Platforms.FirstOrDefault(x => x.UserID.Equals(UserID) && x.Platform.Equals(Platform)); if (platform == null) { platform = new UserPlatform { UserID = UserID, Platform = Platform, Version = "???" }; PlatformCache.Platforms.Add(platform); } if (!platform.Version.Equals(Version)) { platform.Version = Version; var prop = DatabaseSchema.Property(typeof(User), Platform); if (prop != null && prop.Getter() != null && prop.Setter() != null) { var user = Provider.Load(new Filter(x => x.UserID).IsEqualTo(UserID)).FirstOrDefault(); if (user != null) { var value = prop.Getter().Invoke(user); if (!Version.Equals(value)) { prop.Setter().Invoke(user, Version); Provider.Save(user); } } } } } protected void UnlinkTrackingKanban(TEntity entity) where TEntityKanban : EntityKanban, new() where TEntity : Entity where TEntityLink : IEntityLink, new() { var kanbans = Provider.Query( new Filter(x => x.Entity.ID).IsEqualTo(entity.ID).And(x => x.Kanban.Locked).IsEqualTo(true), new Columns( x => x.Entity.ID, x => x.Kanban.ID, x => x.Kanban.Locked ) ); if (!kanbans.Rows.Any()) return; var kanban = kanbans.Rows.First().ToObject(x => x.Kanban); kanban.Locked = false; Provider.Save(kanban); } protected void UpdateTrackingKanban(TEntity entity, Func category) where TEntityKanban : EntityKanban, new() where TEntity : Entity where TEntityLink : IEntityLink, new() { var kanbans = Provider.Query( new Filter(x => x.Entity.ID).IsEqualTo(entity.ID), new Columns( x => x.Entity.ID, x => x.Kanban.ID, x => x.Kanban.Category, 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 ) ); if (!kanbans.Rows.Any()) return; var oldcategory = Kanban.StringToCategory(kanbans.Rows.First().Get(c => c.Kanban.Category)); var newcategory = category(entity); if (!Equals(newcategory, oldcategory)) { var kanban = kanbans.Rows.First().ToObject(x => x.Kanban); kanban.Category = Kanban.CategoryToString(newcategory); var notes = kanban.Notes.ToList(); notes.Add(string.Format("{0:yyyy-MM-dd HH:mm:ss} {1}: {2}", DateTime.Now, UserID, "Task Status updated by Requisition Progress")); kanban.Notes = notes.ToArray(); kanban.Locked = true; FindSubStore().Save(kanban, "Updated due to Requisition Status Change"); } } protected void NotifyEmployee( TEntity entity, Func employeeid, Func filter, Func title, Func body ) where TEntity : Entity { if (!filter(entity)) return; var employees = Provider.Query( new Filter(x => x.ID).IsEqualTo(employeeid(entity)).Or(x => x.UserLink.ID).IsEqualTo(UserGuid), new Columns(x => x.ID).Add(x => x.UserLink.ID).Add(x => x.Name) ).Rows.Select(r => new Tuple( r.Get(c => c.ID), r.Get(c => c.UserLink.ID), r.Get(c => c.Name) ) ); var recipient = employees.FirstOrDefault(x => Equals(x.Item1, employeeid(entity))); var sender = employees.FirstOrDefault(x => Equals(x.Item2, UserGuid)); if (recipient == null || sender == null) return; if (recipient.Item2 == UserGuid) return; var notification = new Notification(); notification.Employee.ID = recipient.Item1; notification.Sender.ID = sender.Item1; notification.Title = title(entity); var sb = new StringBuilder(); sb.AppendLine(string.Format("Dear {0},\n", recipient.Item3.Split(' ').First())); sb.Append(body.Invoke(entity)); if (sender != null) sb.AppendLine(string.Format("\n\nRegards,\n{0}.", sender.Item3.Split(' ').First())); notification.Description = sb.ToString(); notification.EntityType = typeof(TEntity).EntityName(); notification.EntityID = entity.ID; FindSubStore().Save(notification, ""); } } }