123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591 |
- using Comal.Classes;
- using InABox.Core;
- using InABox.Database;
- using InABox.Scripting;
- using Inflector;
- using Newtonsoft.Json;
- using System;
- using System.Collections.Generic;
- using System.Diagnostics.CodeAnalysis;
- using System.Linq;
- using System.Text;
- namespace PRS.Shared.Events;
- public class SaveEvent<T> : IEvent<SaveEventDataModel<T>>, IEntityEvent<T>
- where T : Entity, new()
- {
- public Type Entity => typeof(T);
- public IEventDataModelDefinition DataModelDefinition()
- {
- return new SaveEventDataModelDefinition<T>(this);
- }
- public Notification GenerateNotification(SaveEventDataModel<T> model)
- {
- var notification = new Notification();
- notification.Title = $"Updated {typeof(T).Name}";
- notification.Description = $"Updated {typeof(T).Name}";
- if(model.Entity.ID != Guid.Empty)
- {
- notification.EntityType = CoreUtils.EntityName(model.EntityType);
- notification.EntityID = model.Entity.ID;
- }
- return notification;
- }
- public void Init(IStore store, Event ev, IEventData evData, SaveEventDataModel<T> model)
- {
- if (model.Entity.ID != Guid.Empty)
- {
- var loadCols = Columns.None<T>();
- IEnumerable<string> refVars = evData.ReferencedVariables;
- if (!ev.NotificationExpression.IsNullOrWhiteSpace())
- {
- var notificationExpression = new CoreExpression(ev.NotificationExpression);
- refVars = refVars.Concat(notificationExpression.ReferencedVariables);
- }
- var prefix = $"{typeof(T).Name}.";
- foreach (var variable in refVars)
- {
- if (variable.StartsWith(prefix))
- {
- var varName = variable[prefix.Length..];
- if (!model.Entity.HasOriginalValue(varName))
- {
- loadCols.Add(varName);
- }
- }
- }
- var data = store.Provider.Query(
- new Filter<T>(x => x.ID).IsEqualTo(model.Entity.ID),
- loadCols);
- if(data.Rows.Count > 0)
- {
- data.Rows[0].FillObject(model.Entity);
- }
- }
- }
- }
- public class SaveEventDataModelDefinition<T>(SaveEvent<T> ev) : IEventDataModelDefinition
- where T : Entity, new()
- {
- private IEventVariable[]? variables;
- public IEnumerable<IEventVariable> GetVariables()
- {
- if(variables is null)
- {
- variables = DatabaseSchema.AllProperties(ev.Entity).Select(x => new StandardEventVariable($"{typeof(T).Name}.{x.Name}", x.PropertyType)).ToArray();
- variables.SortBy(x => x.Name);
- }
- return variables;
- }
- public IEventVariable? GetVariable(string name)
- {
- if(variables is null)
- {
- var prefix = $"{typeof(T).Name}.";
- if (name.StartsWith(prefix))
- {
- name = name[prefix.Length..];
- var prop = DatabaseSchema.Property(ev.Entity, name);
- if(prop is null)
- {
- return null;
- }
- else
- {
- return new StandardEventVariable(prop.Name, prop.PropertyType);
- }
- }
- else
- {
- return null;
- }
- }
- else
- {
- return variables.FirstOrDefault(x => x.Name == name);
- }
- }
- }
- public class SaveEventDataModel<T>(T entity, IStore store) : IEventDataModel, ITypedEventDataModel
- where T : Entity, new()
- {
- public T Entity { get; set; } = entity;
- public Type EntityType => typeof(T);
- public IStore Store { get; } = store;
- public bool TryGetVariable(string name, out object? value)
- {
- var prefix = $"{typeof(T).Name}.";
- if (name.StartsWith(prefix))
- {
- name = name[prefix.Length..];
- var prop = DatabaseSchema.Property(typeof(T), name);
- if(prop != null)
- {
- value = prop.Getter()(Entity);
- return true;
- }
- }
- value = null;
- return false;
- }
- }
- #region Triggers
- [Caption("New Record")]
- public class CreatedSaveEventTrigger<T> : IEventTrigger<SaveEvent<T>, SaveEventDataModel<T>>
- where T : Entity, new()
- {
- public string Description => "New Record";
- public IEnumerable<string> ReferencedVariables => [];
- public bool Check(SaveEventDataModel<T> dataModel)
- {
- return dataModel.Entity.HasOriginalValue(x => x.ID);
- }
- }
- [Caption("Property Changed")]
- public class PropertyChangedSaveEventTrigger<T> : IEventTrigger<SaveEvent<T>, SaveEventDataModel<T>>
- where T : Entity, new()
- {
- [JsonIgnore]
- public IProperty? TriggerProperty { get; set; }
- [JsonProperty(PropertyName = "TriggerProperty")]
- private string? _property
- {
- get => TriggerProperty?.Name;
- set
- {
- TriggerProperty = value is null ? null : DatabaseSchema.PropertyStrict(typeof(T), value);
- }
- }
- public string Description => TriggerProperty is null
- ? $"{typeof(T).GetCaption()} changed"
- : $"{typeof(T).GetCaption()}.{TriggerProperty.Name} changed";
- public IEnumerable<string> ReferencedVariables => [];
- public bool Check(SaveEventDataModel<T> dataModel)
- {
- if(TriggerProperty is null)
- {
- return false;
- }
- if (!dataModel.Entity.HasOriginalValue(TriggerProperty.Name))
- {
- return false;
- }
- return true;
- }
- }
- [Caption("Filter")]
- public class FilterSaveEventTrigger<T> : IEventTrigger<SaveEvent<T>, SaveEventDataModel<T>>
- where T : Entity, new()
- {
- public Filter<T>? Filter { get; set; }
- public IEnumerable<string> ReferencedVariables => GetReferencedProperties(Filter).Select(x => $"{typeof(T).Name}.{x}");
- public string Description => Filter?.ToString() ?? "Blank Filter";
- public bool Check(SaveEventDataModel<T> dataModel)
- {
- return Filter.Match(dataModel.Entity, dataModel.Store.GetQueryProviderFactory());
- }
- private IEnumerable<string> GetReferencedProperties(Filter<T>? filter)
- {
- if (filter is null) yield break;
- if(filter.Operator != Operator.All && filter.Operator != Operator.None
- && !filter.Property.IsNullOrWhiteSpace())
- {
- yield return filter.Property;
- }
- foreach(var prop in filter.Ands.Concat(filter.Ors).SelectMany(GetReferencedProperties))
- {
- yield return prop;
- }
- }
- }
- [Caption("Custom Script")]
- public class ScriptSaveEventTrigger<T> : IEventTrigger<SaveEvent<T>, SaveEventDataModel<T>>
- where T : Entity, new()
- {
- public string Description => "Custom Script";
- private ScriptDocument? _scriptDocument;
- private ScriptDocument? ScriptDocument
- {
- get
- {
- if(_scriptDocument is null && Script is not null)
- {
- _scriptDocument = new(Script);
- _scriptDocument.Compile();
- }
- return _scriptDocument;
- }
- }
- private string? _script;
- public string? Script
- {
- get => _script;
- set
- {
- if(_script != value)
- {
- _script = value;
- _scriptDocument = null;
- }
- }
- }
- public IEnumerable<string> ReferencedVariables
- {
- get
- {
- var method = ScriptDocument?.GetMethod(methodName: "RequiredColumns");
- if(method is not null)
- {
- var cols = Columns.None<T>();
- method.Invoke(ScriptDocument!.GetObject(), [cols]);
- return cols.ColumnNames().Select(x => $"{typeof(T).Name}.{x}");
- }
- else
- {
- return [];
- }
- }
- }
- public string DefaultScript()
- {
- return @"using PRS.Shared.Events;
- public class Module
- {
- public void RequiredColumns(Columns<" + typeof(T).Name + @"> columns)
- {
- // Modify 'columns' as required to get the required columns for the 'model.Entity'. If you don't provide these,
- // the data you require in 'Execute' may not be present. Example:
- // columns.Add(x => x.ID);
- }
- public bool Check(SaveEventDataModel<" + typeof(T).Name + @"> model)
- {
- // Return true if model.Entity meets the requirements for this event trigger.
- return true;
- }
- }";
- }
- public bool Check(SaveEventDataModel<T> dataModel)
- {
- if (ScriptDocument is null) return false;
- return ScriptDocument.Execute(methodname: "Check", parameters: [dataModel]);
- }
- }
- #endregion
- #region Actions
- [Caption("Custom Script")]
- public class ScriptSaveEventAction<T> : IEventAction<SaveEvent<T>>
- where T : Entity, new()
- {
- private ScriptDocument? _scriptDocument;
- private ScriptDocument? ScriptDocument
- {
- get
- {
- if(_scriptDocument is null && Script is not null)
- {
- _scriptDocument = new(Script);
- _scriptDocument.SetValue("Result", null);
- _scriptDocument.Compile();
- }
- return _scriptDocument;
- }
- }
- private string? _script;
- public string? Script
- {
- get => _script;
- set
- {
- if(_script != value)
- {
- _script = value;
- _scriptDocument = null;
- }
- }
- }
- public IEnumerable<string> ReferencedVariables
- {
- get
- {
- var method = ScriptDocument?.GetMethod(methodName: "RequiredColumns");
- if(method is not null)
- {
- var cols = Columns.None<T>();
- method.Invoke(ScriptDocument!.GetObject(), [cols]);
- return cols.ColumnNames().Select(x => $"{typeof(T).Name}.{x}");
- }
- else
- {
- return [];
- }
- }
- }
- public string Description => "Custom Script";
- public string DefaultScript()
- {
- return @"using PRS.Shared.Events;
- public class Module
- {
- public object? Result { get; set; }
- public void RequiredColumns(Columns<" + typeof(T).Name + @"> columns)
- {
- // Modify 'columns' as required to get the required columns for the 'model.Entity'. If you don't provide these,
- // the data you require in 'Execute' may not be present. Example:
- // columns.Add(x => x.ID);
- }
- public bool Execute(SaveEventDataModel<" + typeof(T).Name + @"> model)
- {
- // Do anything you want with model.Entity, and then save return-value to 'Result', or leave it as 'null' if no return value is needed.
- return true;
- }
- }";
- }
- public object? Execute(IEventDataModel dataModel)
- {
- if (ScriptDocument is null) return null;
- var model = dataModel.RootModel<SaveEventDataModel<T>>();
- if(ScriptDocument.Execute(methodname: "Execute", parameters: [model]))
- {
- return ScriptDocument.GetValue("Result");
- }
- else
- {
- return null;
- }
- }
- }
- [Caption("Create Entity")]
- public class CreateEntitySaveEventAction<T> : IEventAction<SaveEvent<T>>
- where T : Entity, new()
- {
- [JsonProperty(Order = 1)]
- public Type? EntityType { get; set; }
- [JsonIgnore]
- public List<PropertyInitializer> Initializers { get; set; } = new List<PropertyInitializer>();
- private ScriptDocument? _scriptDocument;
- private ScriptDocument? ScriptDocument
- {
- get
- {
- if(_scriptDocument is null && Script is not null)
- {
- _scriptDocument = new(Script);
- _scriptDocument.Compile();
- }
- return _scriptDocument;
- }
- }
- private string? _script;
- public string? Script
- {
- get => _script;
- set
- {
- if(_script != value)
- {
- _script = value;
- _scriptDocument = null;
- }
- }
- }
- private IEnumerable<string> ScriptReferencedVariables
- {
- get
- {
- var method = ScriptDocument?.GetMethod(methodName: "RequiredColumns");
- if(method is not null)
- {
- var cols = Columns.None<T>();
- method.Invoke(ScriptDocument!.GetObject(), [cols]);
- return cols.ColumnNames().Select(x => $"{typeof(T).Name}.{x}");
- }
- else
- {
- return [];
- }
- }
- }
- public string DefaultScript()
- {
- if (EntityType is null) return "Please select an entity type first.";
- return @"using PRS.Shared.Events;
- public class Module
- {
- public void RequiredColumns(Columns<" + typeof(T).Name + @"> columns)
- {
- // Modify 'columns' as required to get the required columns for the 'model.Entity'. If you don't provide these,
- // the data you require in 'Execute' may not be present. Example:
- // columns.Add(x => x.ID);
- }
- public void Execute(SaveEventDataModel<" + typeof(T).Name + @"> model, " + EntityType.Name + " new" + EntityType.Name + @")
- {
- // Modify new" + EntityType.Name + @" as you wish, using model.Entity to get the required data. This method runs after
- // the property initializers (in the previous screen) for the Create Entity action have run.
- }
- }";
- }
- public string Description => $"Create New {EntityType?.Name ?? "Entity"}";
- public IEnumerable<string> ReferencedVariables => Initializers.SelectMany(x => x.ReferencedVariables).Concat(ScriptReferencedVariables);
- public object? Execute(IEventDataModel dataModel)
- {
- if(EntityType is null)
- {
- return null;
- }
- var entity = (Activator.CreateInstance(EntityType) as Entity)!;
- foreach(var initializer in Initializers)
- {
- initializer.Execute(entity, dataModel);
- }
- if(ScriptDocument is not null)
- {
- var model = dataModel.RootModel<SaveEventDataModel<T>>();
- ScriptDocument.Execute(methodname: "Execute", parameters: [model, entity]);
- }
- DbFactory.FindStore(EntityType, Guid.Empty, "", default, "", Logger.Main).Save(entity, "");
- return entity;
- }
- #region Serialization Stuff
- [JsonProperty(Order = 2, PropertyName = "Initializers")]
- private PropertyInitializerSerializationItem[] _initializers
- {
- get => Initializers.ToArray(x => new PropertyInitializerSerializationItem { Property = x.Property.Name, Value = x.Value });
- set
- {
- Initializers.Clear();
- Initializers.AddRange(value.Select(x => new PropertyInitializer(EntityType!, x)));
- }
- }
- #endregion
- }
- internal class PropertyInitializerSerializationItem
- {
- public string Property { get; set; }
- public string Value { get; set; }
- }
- public class PropertyInitializer
- {
- public IProperty Property { get; set; }
- private CoreExpression? _valueExpression;
- private CoreExpression ValueExpression
- {
- get
- {
- _valueExpression ??= new CoreExpression(Value, Property.PropertyType);
- return _valueExpression;
- }
- }
- private string _value;
- public string Value
- {
- get => _value;
- [MemberNotNull(nameof(_value))]
- set
- {
- if(value != _value)
- {
- _value = value;
- _valueExpression = null;
- }
- }
- }
- public IEnumerable<string> ReferencedVariables => ValueExpression.ReferencedVariables;
- internal PropertyInitializer(Type entityType, PropertyInitializerSerializationItem item)
- {
- Value = item.Value;
- Property = DatabaseSchema.PropertyStrict(entityType, item.Property);
- }
- public PropertyInitializer(IProperty property, string value)
- {
- Property = property;
- Value = value;
- }
- public void Execute(Entity entity, IEventDataModel dataModel)
- {
- Property.Setter()(entity, ValueExpression.Evaluate(dataModel));
- }
- }
- #endregion
|