| | | 1 | | using Elsa.Workflows.Activities; |
| | | 2 | | using Elsa.Workflows.Memory; |
| | | 3 | | using Elsa.Workflows.Models; |
| | | 4 | | |
| | | 5 | | namespace Elsa.Workflows; |
| | | 6 | | |
| | | 7 | | /// <inheritdoc /> |
| | | 8 | | public class OutputBindingDestinationResolver : IOutputBindingDestinationResolver |
| | | 9 | | { |
| | | 10 | | /// <inheritdoc /> |
| | | 11 | | public OutputBindingDestination? Resolve(ActivityExecutionContext activityExecutionContext, Output output) |
| | | 12 | | { |
| | 10 | 13 | | var reference = output.MemoryBlockReference(); |
| | | 14 | | |
| | 10 | 15 | | if (reference.Id == null) |
| | 0 | 16 | | return null; |
| | | 17 | | |
| | 10 | 18 | | if (activityExecutionContext.ExpressionExecutionContext.TryGetBlock(reference, out var block) && |
| | 10 | 19 | | block.Metadata is VariableBlockMetadata variableMetadata) |
| | | 20 | | { |
| | 7 | 21 | | return CreateVariableDestination(variableMetadata.Variable); |
| | | 22 | | } |
| | | 23 | | |
| | 3 | 24 | | return CreateWorkflowOutputDestination(activityExecutionContext.WorkflowExecutionContext.Workflow, reference.Id) |
| | | 25 | | } |
| | | 26 | | |
| | | 27 | | /// <inheritdoc /> |
| | | 28 | | public OutputBindingDestination? Resolve(WorkflowGraph workflowGraph, ActivityNode activityNode, Output output) |
| | | 29 | | { |
| | 6 | 30 | | var referenceId = output.MemoryBlockReference().Id; |
| | | 31 | | |
| | 6 | 32 | | if (referenceId == null) |
| | 0 | 33 | | return null; |
| | | 34 | | |
| | 6 | 35 | | var variable = new[] { activityNode } |
| | 6 | 36 | | .Concat(activityNode.Ancestors()) |
| | 7 | 37 | | .Select(x => x.Activity) |
| | 6 | 38 | | .OfType<IVariableContainer>() |
| | 2 | 39 | | .SelectMany(x => x.Variables) |
| | 8 | 40 | | .FirstOrDefault(x => x.Id == referenceId); |
| | | 41 | | |
| | 6 | 42 | | if (variable != null) |
| | 2 | 43 | | return CreateVariableDestination(variable); |
| | | 44 | | |
| | 4 | 45 | | return CreateWorkflowOutputDestination(workflowGraph.Workflow, referenceId); |
| | | 46 | | } |
| | | 47 | | |
| | | 48 | | private static OutputBindingDestination? CreateVariableDestination(Variable variable) |
| | | 49 | | { |
| | 9 | 50 | | var variableType = variable.GetType().GenericTypeArguments.FirstOrDefault(); |
| | 9 | 51 | | return variableType == null |
| | 9 | 52 | | ? null |
| | 9 | 53 | | : new(variable.Id, variableType, AllowsNull(variableType), OutputBindingDestinationKind.Variable); |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | private static OutputBindingDestination? CreateWorkflowOutputDestination(Workflow workflow, string referenceId) |
| | | 57 | | { |
| | 13 | 58 | | var output = workflow.Outputs.FirstOrDefault(x => x.Name == referenceId); |
| | 7 | 59 | | return output == null |
| | 7 | 60 | | ? null |
| | 7 | 61 | | : new(output.Name, output.Type, AllowsNull(output.Type), OutputBindingDestinationKind.WorkflowOutput); |
| | | 62 | | } |
| | | 63 | | |
| | 15 | 64 | | private static bool AllowsNull(Type type) => !type.IsValueType || Nullable.GetUnderlyingType(type) != null; |
| | | 65 | | } |