|
@@ -2,6 +2,7 @@
|
|
|
using InABox.Core;
|
|
|
using InABox.Database;
|
|
|
using InABox.Scripting;
|
|
|
+using Inflector;
|
|
|
using Newtonsoft.Json;
|
|
|
using System;
|
|
|
using System.Collections.Generic;
|
|
@@ -283,7 +284,10 @@ public class Module
|
|
|
{
|
|
|
public void RequiredColumns(Columns<" + typeof(T).Name + @"> columns)
|
|
|
{
|
|
|
- // Modify 'columns' as required.
|
|
|
+ // 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)
|
|
@@ -369,7 +373,10 @@ public class Module
|
|
|
|
|
|
public void RequiredColumns(Columns<" + typeof(T).Name + @"> columns)
|
|
|
{
|
|
|
- // Modify 'columns' as required.
|
|
|
+ // 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)
|
|
@@ -406,9 +413,79 @@ public class CreateEntitySaveEventAction<T> : IEventAction<SaveEvent<T>>
|
|
|
[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);
|
|
|
+ public IEnumerable<string> ReferencedVariables => Initializers.SelectMany(x => x.ReferencedVariables).Concat(ScriptReferencedVariables);
|
|
|
|
|
|
public object? Execute(IEventDataModel dataModel)
|
|
|
{
|
|
@@ -421,6 +498,14 @@ public class CreateEntitySaveEventAction<T> : IEventAction<SaveEvent<T>>
|
|
|
{
|
|
|
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;
|
|
|
}
|