123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- using System.Collections.Generic;
- using System.Linq;
- using System.Threading.Tasks;
- using Comal.Classes;
- using InABox.Core;
- using System;
- 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, TEntity>, 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,
- Columns.None<EmployeeFormSubscription>().Add(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)
- {
- if (entity is IDigitalFormInstance instance
- && entity.HasOriginalValue(x=>x.FormCompleted)
- && entity.GetOriginalValue(x=>x.FormCompleted) == DateTime.MinValue
- )
- {
- var updates = new List<Notification>();
- CheckFormSubscriptionCache();
- var formid = entity.Form.ID; //(Guid)CoreUtils.GetPropertyValue(entity, "Form.ID");
- var formname = entity.Form.Description; //(string)CoreUtils.GetPropertyValue(entity, "Form.Description");
- var userid = entity.FormCompletedBy.ID; //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),
- Columns.None<Employee>().Add(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);
- CheckParentStatus(entity);
- }
- private void CheckParentStatus(TEntity entity)
- {
- if (entity.HasOriginalValue(x=>x.FormCompleted) || entity.HasOriginalValue(x=>x.FormCancelled))
- {
- bool isFinal = false;
- TParent? parent = null;
- Task[] tasks = new Task[]
- {
- Task.Run(() =>
- {
- isFinal = Provider.Query(
- new Filter<DigitalForm>(x => x.ID).IsEqualTo(entity.Form.ID),
- Columns.None<DigitalForm>().Add(x => x.Final)
- ).Rows.FirstOrDefault()?.Get<DigitalForm, bool>(x => x.Final) ?? false;
- }),
- Task.Run(() =>
- {
- parent = Provider.Query(
- new Filter<TParent>(x=>x.ID).IsEqualTo(entity.ParentID()),
- LookupFactory.RequiredColumns<TParent>()
- ).Rows
- .FirstOrDefault()?
- .ToObject<TParent>();
- })
- };
- Task.WaitAll(tasks);
- if (isFinal && (parent != null))
- {
- UpdateParentStatus(entity, parent);
- if (parent.IsChanged())
- FindSubStore<TParent>().Save(parent, $"{typeof(TParent).EntityName().Split('.').Last()} status updated by {entity.Form.Description}");
- }
-
- }
- }
- protected virtual void UpdateParentStatus(IDigitalFormInstance form, TParent parent)
- {
-
- }
-
- }
- }
|