using System; using System.Collections.Generic; using System.Linq; using Comal.Classes; using InABox.Core; namespace Comal.Stores { public class FormSubscription { public Guid EmployeeID { get; set; } public Guid FormID { get; set; } } public static class FormSubscriptionCache { static FormSubscriptionCache() { Subscriptions = null; } public static FormSubscription[] Subscriptions { get; set; } } public abstract class EntityFormStore : BaseStore where TEntity : EntityForm, new() where TParent : Entity, new() where TParentLink : IEntityLink, new() { private void CheckFormSubscriptionCache() { if (FormSubscriptionCache.Subscriptions == null) { Logger.Send(LogType.Information, UserID, "Refreshing Form Subscription Cache"); var subs = Provider.Query( null, new Columns(x => x.Employee.ID, x => x.Form.ID) ); FormSubscriptionCache.Subscriptions = subs.Rows.Select(r => new FormSubscription { EmployeeID = r.Get(c => c.Employee.ID), FormID = r.Get(c => c.Form.ID) } ).ToArray(); } } private void CheckFormSubscriptions(TEntity entity) { var instance = entity as IDigitalFormInstance; if (instance != null && entity.HasOriginalValue("FormCompleted") && entity.GetOriginalValue("FormCompleted") == DateTime.MinValue ) { var updates = new List(); CheckFormSubscriptionCache(); var formid = (Guid)CoreUtils.GetPropertyValue(entity, "Form.ID"); var formname = (string)CoreUtils.GetPropertyValue(entity, "Form.Description"); var userid = (Guid)CoreUtils.GetPropertyValue(entity, "FormCompletedBy.ID"); if (userid == Guid.Empty) userid = UserGuid; var emprow = userid != Guid.Empty ? Provider.Query( new Filter(x => x.UserLink.ID).IsEqualTo(userid), new Columns(x => x.ID, x => x.Name) ).Rows.FirstOrDefault() : null; var empid = emprow != null ? emprow.Get(x => x.ID) : Guid.Empty; var empname = emprow != null ? emprow.Get(x => x.Name) : UserID; var subscriptions = FormSubscriptionCache.Subscriptions.Where(x => x.FormID == formid).ToArray(); foreach (var subscription in subscriptions) { var notification = new Notification(); notification.Employee.ID = subscription.EmployeeID; notification.Sender.ID = empid; notification.Title = "Form Completed: " + formname; notification.Description = string.Format("{0} has completed a {1} digital form.", empname, formname); notification.EntityType = entity.GetType().EntityName(); notification.EntityID = entity.ID; updates.Add(notification); } if (updates.Any()) FindSubStore().Save(updates, ""); } } protected override void AfterSave(TEntity entity) { base.AfterSave(entity); CheckFormSubscriptions(entity); } } public class AssignmentFormStore : EntityFormStore { } public class LeaveRequestFormStore : EntityFormStore { } public class JobITPFormStore : EntityFormStore { } public class JobFormStore : EntityFormStore { } public class EmployeeFormStore : EntityFormStore { } public class TimeSheetFormStore : EntityFormStore { } public class KanbanFormStore : EntityFormStore { } }