123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- 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<TEntity, TParent, TParentLink> : BaseStore<TEntity>
- where TEntity : EntityForm<TParent, TParentLink>, new()
- where TParent : Entity, new()
- where TParentLink : IEntityLink<TParent>, new()
- {
- private void CheckFormSubscriptionCache()
- {
- if (FormSubscriptionCache.Subscriptions == null)
- {
- Logger.Send(LogType.Information, UserID, "Refreshing Form Subscription Cache");
- var subs = Provider.Query(
- null,
- new Columns<EmployeeFormSubscription>(x => x.Employee.ID, x => x.Form.ID)
- );
- FormSubscriptionCache.Subscriptions = subs.Rows.Select(r =>
- new FormSubscription
- {
- EmployeeID = r.Get<EmployeeFormSubscription, Guid>(c => c.Employee.ID),
- FormID = r.Get<EmployeeFormSubscription, Guid>(c => c.Form.ID)
- }
- ).ToArray();
- }
- }
- private void CheckFormSubscriptions(TEntity entity)
- {
- var instance = entity as IDigitalFormInstance;
- if (instance != null
- && entity.HasOriginalValue("FormCompleted")
- && entity.GetOriginalValue<TEntity, DateTime>("FormCompleted") == DateTime.MinValue
- )
- {
- var updates = new List<Notification>();
- 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<Employee>(x => x.UserLink.ID).IsEqualTo(userid),
- new Columns<Employee>(x => x.ID, x => x.Name)
- ).Rows.FirstOrDefault()
- : null;
- var empid = emprow != null ? emprow.Get<Employee, Guid>(x => x.ID) : Guid.Empty;
- var empname = emprow != null ? emprow.Get<Employee, string>(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("<html><body>{0} has completed a <u><b>{1}</b></u> digital form.</body></html>", empname,
- formname);
- notification.EntityType = entity.GetType().EntityName();
- notification.EntityID = entity.ID;
- updates.Add(notification);
- }
- if (updates.Any())
- FindSubStore<Notification>().Save(updates, "");
- }
- }
- protected override void AfterSave(TEntity entity)
- {
- base.AfterSave(entity);
- CheckFormSubscriptions(entity);
- }
- }
- public class AssignmentFormStore : EntityFormStore<AssignmentForm, Assignment, AssignmentLink>
- {
- }
- public class LeaveRequestFormStore : EntityFormStore<LeaveRequestForm, LeaveRequest, LeaveRequestLink>
- {
- }
- public class JobITPFormStore : EntityFormStore<JobITPForm, JobITP, JobITPLink>
- {
- }
- public class JobFormStore : EntityFormStore<JobForm, Job, JobLink>
- {
- }
- public class EmployeeFormStore : EntityFormStore<EmployeeForm, Employee, EmployeeLink>
- {
- }
- public class TimeSheetFormStore : EntityFormStore<TimeSheetForm, TimeSheet, TimeSheetLink>
- {
- }
- public class KanbanFormStore : EntityFormStore<KanbanForm, Kanban, KanbanLink>
- {
- }
- }
|