2021-03-04 15:11:18 +00:00
|
|
|
using System;
|
|
|
|
|
using System.Globalization;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using Elsa.Scripting.JavaScript.Messages;
|
2021-04-26 10:32:16 +00:00
|
|
|
using Elsa.Services;
|
|
|
|
|
using Elsa.Services.Models;
|
2021-03-04 15:11:18 +00:00
|
|
|
using Jint;
|
|
|
|
|
using Jint.Runtime.Interop;
|
|
|
|
|
using MediatR;
|
2021-04-13 11:17:24 +00:00
|
|
|
using Microsoft.Extensions.Configuration;
|
2021-03-04 15:11:18 +00:00
|
|
|
using NodaTime;
|
|
|
|
|
|
|
|
|
|
namespace Elsa.Scripting.JavaScript.Handlers
|
|
|
|
|
{
|
|
|
|
|
public class ConfigureJavaScriptEngine : INotificationHandler<EvaluatingJavaScriptExpression>
|
|
|
|
|
{
|
2021-04-13 11:17:24 +00:00
|
|
|
private readonly IConfiguration _configuration;
|
|
|
|
|
|
|
|
|
|
public ConfigureJavaScriptEngine(IConfiguration configuration)
|
|
|
|
|
{
|
|
|
|
|
_configuration = configuration;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-04 15:11:18 +00:00
|
|
|
public Task Handle(EvaluatingJavaScriptExpression notification, CancellationToken cancellationToken)
|
|
|
|
|
{
|
2021-05-13 10:15:31 +00:00
|
|
|
var activityExecutionContext = notification.ActivityExecutionContext;
|
|
|
|
|
var workflowExecutionContext = activityExecutionContext.WorkflowExecutionContext;
|
2021-03-04 15:11:18 +00:00
|
|
|
var workflowBlueprint = workflowExecutionContext.WorkflowBlueprint;
|
|
|
|
|
var workflowInstance = workflowExecutionContext.WorkflowInstance;
|
|
|
|
|
var engine = notification.Engine;
|
|
|
|
|
|
|
|
|
|
// Global functions.
|
|
|
|
|
engine.SetValue("guid", (Func<string>) (() => Guid.NewGuid().ToString()));
|
2021-05-25 11:24:01 +00:00
|
|
|
engine.SetValue("parseGuid", (Func<string, Guid>) (Guid.Parse));
|
2021-05-13 10:15:31 +00:00
|
|
|
engine.SetValue("setVariable", (Action<string, object>) ((name, value) => activityExecutionContext.SetVariable(name, value)));
|
|
|
|
|
engine.SetValue("getVariable", (Func<string, object?>) (name => activityExecutionContext.GetVariable(name)));
|
2021-04-13 11:17:24 +00:00
|
|
|
engine.SetValue("getConfig", (Func<string, object?>) (name => _configuration.GetSection(name).Value));
|
2021-04-15 14:08:17 +00:00
|
|
|
engine.SetValue("isNullOrWhiteSpace", (Func<string, bool>) (string.IsNullOrWhiteSpace));
|
|
|
|
|
engine.SetValue("isNullOrEmpty", (Func<string, bool>) (string.IsNullOrEmpty));
|
2021-05-13 10:15:31 +00:00
|
|
|
engine.SetValue("getWorkflowDefinitionIdByName", (Func<string, string?>) (name => GetWorkflowDefinitionIdByName(activityExecutionContext, name)));
|
|
|
|
|
engine.SetValue("getWorkflowDefinitionIdByTag", (Func<string, string?>) (tag => GetWorkflowDefinitionIdByTag(activityExecutionContext, tag)));
|
2021-03-11 21:50:18 +00:00
|
|
|
|
2021-03-04 15:11:18 +00:00
|
|
|
// Global variables.
|
2021-05-13 10:15:31 +00:00
|
|
|
engine.SetValue("activityExecutionContext", activityExecutionContext);
|
|
|
|
|
engine.SetValue("workflowExecutionContext", workflowExecutionContext);
|
|
|
|
|
engine.SetValue("workflowInstance", workflowInstance);
|
|
|
|
|
engine.SetValue("input", activityExecutionContext.Input);
|
|
|
|
|
engine.SetValue("workflowInstanceId", workflowInstance.Id);
|
|
|
|
|
engine.SetValue("workflowDefinitionId", workflowInstance.DefinitionId);
|
|
|
|
|
engine.SetValue("workflowDefinitionVersion", workflowInstance.Version);
|
2021-03-04 15:11:18 +00:00
|
|
|
engine.SetValue("correlationId", workflowInstance.CorrelationId);
|
|
|
|
|
engine.SetValue("currentCulture", CultureInfo.InvariantCulture);
|
2021-05-13 10:15:31 +00:00
|
|
|
engine.SetValue("workflowContext", activityExecutionContext.GetWorkflowContext());
|
2021-03-11 21:50:18 +00:00
|
|
|
|
2021-03-04 15:11:18 +00:00
|
|
|
// NodaTime types.
|
|
|
|
|
RegisterType<Instant>(engine);
|
|
|
|
|
RegisterType<Duration>(engine);
|
|
|
|
|
RegisterType<Period>(engine);
|
|
|
|
|
RegisterType<LocalDate>(engine);
|
|
|
|
|
RegisterType<LocalTime>(engine);
|
|
|
|
|
RegisterType<LocalDateTime>(engine);
|
|
|
|
|
|
2021-04-04 13:19:54 +00:00
|
|
|
var variables = workflowExecutionContext.GetMergedVariables();
|
2021-03-04 15:11:18 +00:00
|
|
|
|
|
|
|
|
// Add workflow variables.
|
|
|
|
|
foreach (var variable in variables.Data)
|
|
|
|
|
engine.SetValue(variable.Key, variable.Value);
|
|
|
|
|
|
|
|
|
|
// Add activity outputs.
|
2021-03-11 21:50:18 +00:00
|
|
|
foreach (var activity in workflowBlueprint.Activities.Where(x => x.Name is not null and not "" && workflowInstance.ActivityOutput.ContainsKey(x.Id)))
|
|
|
|
|
{
|
|
|
|
|
var output = new { Output = workflowInstance.ActivityOutput[activity.Id!] };
|
|
|
|
|
engine.SetValue(activity.Name, output);
|
|
|
|
|
}
|
2021-03-04 15:11:18 +00:00
|
|
|
|
|
|
|
|
return Task.CompletedTask;
|
|
|
|
|
}
|
2021-03-11 21:50:18 +00:00
|
|
|
|
2021-04-26 10:32:16 +00:00
|
|
|
private string? GetWorkflowDefinitionIdByTag(ActivityExecutionContext activityExecutionContext, string tag) => GetWorkflowDefinitionId(activityExecutionContext, x => string.Equals(x.Tag, tag, StringComparison.OrdinalIgnoreCase));
|
|
|
|
|
private string? GetWorkflowDefinitionIdByName(ActivityExecutionContext activityExecutionContext, string name) => GetWorkflowDefinitionId(activityExecutionContext, x => string.Equals(x.Name, name, StringComparison.OrdinalIgnoreCase));
|
|
|
|
|
|
|
|
|
|
private string? GetWorkflowDefinitionId(ActivityExecutionContext activityExecutionContext, Func<IWorkflowBlueprint, bool> filter)
|
|
|
|
|
{
|
|
|
|
|
var workflowRegistry = activityExecutionContext.GetService<IWorkflowRegistry>();
|
|
|
|
|
var workflowBlueprint = workflowRegistry.FindAsync(filter).Result;
|
|
|
|
|
return workflowBlueprint?.Id;
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-04 15:11:18 +00:00
|
|
|
private void RegisterType<T>(Engine engine) => engine.SetValue(typeof(T).Name, TypeReference.CreateTypeReference(engine, typeof(T)));
|
|
|
|
|
}
|
|
|
|
|
}
|