using com.sun.net.ssl.@internal.ssl; using Comal.Classes; using Comal.Stores; using InABox.Core; using InABox.Database; using InABox.Scripting; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace PRSStores; public class BillApprovalSetStore : BaseStore { private static Dictionary _scripts = new(); protected override void BeforeSave(BillApprovalSet entity) { base.BeforeSave(entity); if(entity.HasOriginalValue(x => x.ApprovalSetUpdateScript)) { UpdateApprovalSetScript(entity.ID, entity.ApprovalSetUpdateScript); } } private static ScriptDocument? UpdateApprovalSetScript(Guid approvalSetID, string script) { ScriptDocument? document = null; if (!script.IsNullOrWhiteSpace()) { document = new ScriptDocument(script); if (!document.Compile()) { InABox.Core.Logger.Send(LogType.Error, "", $"Bill Approval Script failed to compile: {document.Result}"); document = null; } } lock(_scripts) { _scripts[approvalSetID] = document; } return document; } public static ScriptDocument? GetUpdateApprovalsScript(Guid approvalSetID) { lock (_scripts) { if (_scripts.TryGetValue(approvalSetID, out var script)) return script; } var set = DbFactory.NewProvider(Logger.Main).Query( new Filter(x => x.ID).IsEqualTo(approvalSetID), Columns.None().Add(x => x.ApprovalSetUpdateScript)) .ToObjects().FirstOrDefault(); if(set is not null) { return UpdateApprovalSetScript(approvalSetID, set.ApprovalSetUpdateScript); } else { return null; } } }