| | | 1 | | using System.Runtime.CompilerServices; |
| | | 2 | | using Elsa.Extensions; |
| | | 3 | | using Elsa.Workflows.Attributes; |
| | | 4 | | using Elsa.Workflows.UIHints; |
| | | 5 | | using Elsa.Workflows.Models; |
| | | 6 | | using JetBrains.Annotations; |
| | | 7 | | |
| | | 8 | | namespace Elsa.Workflows.Management.Activities.SetOutput; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Assigns a given value to a workflow definition's output. |
| | | 12 | | /// </summary> |
| | | 13 | | [Activity("Elsa", "Composition", "Assigns a given value to a the container's output.", Kind = ActivityKind.Action)] |
| | | 14 | | [PublicAPI] |
| | | 15 | | public class SetOutput : CodeActivity |
| | | 16 | | { |
| | | 17 | | /// <inheritdoc /> |
| | 136 | 18 | | public SetOutput([CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : base(source, line) |
| | | 19 | | { |
| | 136 | 20 | | } |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// The name of the output to assign. |
| | | 24 | | /// </summary> |
| | | 25 | | [Input( |
| | | 26 | | DisplayName = "Output", |
| | | 27 | | Description = "The output to assign.", |
| | | 28 | | UIHint = InputUIHints.OutputPicker |
| | | 29 | | )] |
| | 434 | 30 | | public Input<string> OutputName { get; set; } = null!; |
| | | 31 | | |
| | | 32 | | /// <summary> |
| | | 33 | | /// The value to assign to the output. |
| | | 34 | | /// </summary> |
| | | 35 | | [Input(Description = "The value to assign.")] |
| | 431 | 36 | | public Input<object?> OutputValue { get; set; } = null!; |
| | | 37 | | |
| | | 38 | | /// <inheritdoc /> |
| | | 39 | | protected override void Execute(ActivityExecutionContext context) |
| | | 40 | | { |
| | 18 | 41 | | var outputName = OutputName.Get(context); |
| | 18 | 42 | | var outputValue = OutputValue.GetOrDefault(context); |
| | 37 | 43 | | var ancestorContext = context.ParentActivityExecutionContext?.GetAncestors().FirstOrDefault(x => x.ActivityDescr |
| | | 44 | | |
| | 18 | 45 | | if (ancestorContext != null) |
| | 3 | 46 | | SetAncestorActivityOutput(context, ancestorContext, outputValue); |
| | | 47 | | else |
| | 15 | 48 | | context.WorkflowExecutionContext.Output[outputName] = outputValue!; |
| | 15 | 49 | | } |
| | | 50 | | |
| | | 51 | | private void SetAncestorActivityOutput(ActivityExecutionContext context, ActivityExecutionContext ancestorContext, o |
| | | 52 | | { |
| | 3 | 53 | | var ancestorActivity = ancestorContext.Activity; |
| | 3 | 54 | | var ancestorActivityDescriptor = ancestorContext.ActivityDescriptor; |
| | 3 | 55 | | var outputName = OutputName.Get(context); |
| | 7 | 56 | | var ancestorOutputDescriptor = ancestorActivityDescriptor.Outputs.FirstOrDefault(x => x.Name == outputName); |
| | | 57 | | |
| | 3 | 58 | | if (ancestorOutputDescriptor == null) |
| | 0 | 59 | | return; |
| | | 60 | | |
| | 3 | 61 | | var ancestorOutput = (Output)ancestorOutputDescriptor.ValueGetter(ancestorActivity)!; |
| | 3 | 62 | | ancestorContext.Set(ancestorOutput, outputValue, outputName); |
| | | 63 | | |
| | | 64 | | // If the ancestor activity is the root workflow, we need to update the workflow execution context's output coll |
| | 3 | 65 | | if (ancestorContext.ParentActivityExecutionContext == null) |
| | 0 | 66 | | context.WorkflowExecutionContext.Output[outputName] = outputValue!; |
| | | 67 | | else |
| | | 68 | | { |
| | | 69 | | // If this activity executes in a composite activity, we need to update the composite activity's output vari |
| | 3 | 70 | | var variable = context.ExpressionExecutionContext.GetVariable(outputName); |
| | | 71 | | |
| | 3 | 72 | | if (variable != null) |
| | 3 | 73 | | context.Set(variable, outputValue); |
| | | 74 | | |
| | | 75 | | // Also set the output on the composite activity's output property. |
| | 3 | 76 | | var compositeActivityContext = ancestorContext; |
| | 3 | 77 | | var compositeActivity = compositeActivityContext.Activity; |
| | 3 | 78 | | var compositeActivityDescriptor = compositeActivityContext.ActivityDescriptor; |
| | 7 | 79 | | var compositeOutputDescriptor = compositeActivityDescriptor.Outputs.FirstOrDefault(x => x.Name == outputName |
| | 3 | 80 | | var compositeOutput = (Output?)compositeOutputDescriptor?.ValueGetter(compositeActivity); |
| | | 81 | | |
| | 3 | 82 | | if(compositeOutput != null) |
| | 1 | 83 | | ancestorContext.ParentActivityExecutionContext.Set(compositeOutput, outputValue); |
| | | 84 | | } |
| | 3 | 85 | | } |
| | | 86 | | } |