| | | 1 | | using System.ComponentModel; |
| | | 2 | | using System.Runtime.CompilerServices; |
| | | 3 | | using Elsa.Expressions.Models; |
| | | 4 | | using Elsa.Extensions; |
| | | 5 | | using Elsa.Workflows.Attributes; |
| | | 6 | | using Elsa.Workflows.Memory; |
| | | 7 | | using Elsa.Workflows.Models; |
| | | 8 | | using JetBrains.Annotations; |
| | | 9 | | |
| | | 10 | | namespace Elsa.Workflows.Activities; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Assign a workflow variable a value. |
| | | 14 | | /// </summary> |
| | | 15 | | [Browsable(false)] |
| | | 16 | | [Activity("Elsa", "Primitives", "Assign value to a workflow variable.")] |
| | | 17 | | [PublicAPI] |
| | | 18 | | public class SetVariable<T> : CodeActivity |
| | | 19 | | { |
| | | 20 | | /// <inheritdoc /> |
| | 6 | 21 | | public SetVariable([CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : base(source, line) |
| | | 22 | | { |
| | 6 | 23 | | } |
| | | 24 | | |
| | | 25 | | /// <inheritdoc /> |
| | 6 | 26 | | public SetVariable(Variable<T> variable, Input<T> value, [CallerFilePath] string? source = null, [CallerLineNumber] |
| | | 27 | | { |
| | 6 | 28 | | Variable = variable; |
| | 6 | 29 | | Value = value; |
| | 6 | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <inheritdoc /> |
| | | 33 | | public SetVariable(Variable<T> variable, Variable<T> value, [CallerFilePath] string? source = null, [CallerLineNumbe |
| | 0 | 34 | | : this(variable, new Input<T>(value), source, line) |
| | | 35 | | { |
| | 0 | 36 | | } |
| | | 37 | | |
| | | 38 | | /// <inheritdoc /> |
| | | 39 | | public SetVariable(Variable<T> variable, Func<ExpressionExecutionContext, T> value, [CallerFilePath] string? source |
| | 0 | 40 | | : this(variable, new Input<T>(value), source, line) |
| | | 41 | | { |
| | 0 | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <inheritdoc /> |
| | | 45 | | public SetVariable(Variable<T> variable, Func<T> value, [CallerFilePath] string? source = null, [CallerLineNumber] i |
| | 0 | 46 | | : this(variable, new Input<T>(value), source, line) |
| | | 47 | | { |
| | 0 | 48 | | } |
| | | 49 | | |
| | | 50 | | /// <inheritdoc /> |
| | | 51 | | public SetVariable(Variable<T> variable, T value, [CallerFilePath] string? source = null, [CallerLineNumber] int? li |
| | 0 | 52 | | : this(variable, new Input<T>(value), source, line) |
| | | 53 | | { |
| | 0 | 54 | | } |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// The variable to assign the value to. |
| | | 58 | | /// </summary> |
| | | 59 | | [Input(Description = "The variable to assign the value to.")] |
| | 15 | 60 | | public Variable<T> Variable { get; set; } = null!; |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// The value to assign. |
| | | 64 | | /// </summary> |
| | | 65 | | [Input(Description = "The value to assign.")] |
| | 21 | 66 | | public Input<T> Value { get; set; } = new(new Literal<T>()); |
| | | 67 | | |
| | | 68 | | /// <inheritdoc /> |
| | | 69 | | protected override void Execute(ActivityExecutionContext context) |
| | | 70 | | { |
| | 3 | 71 | | var value = context.Get(Value); |
| | 3 | 72 | | Variable.Set(context, value); |
| | 2 | 73 | | } |
| | | 74 | | } |
| | | 75 | | |
| | | 76 | | /// <summary> |
| | | 77 | | /// Assign a workflow variable a value. |
| | | 78 | | /// </summary> |
| | | 79 | | [Activity("Elsa", "Primitives", "Assign a value to a workflow variable.")] |
| | | 80 | | [PublicAPI] |
| | | 81 | | public class SetVariable : CodeActivity |
| | | 82 | | { |
| | | 83 | | /// <inheritdoc /> |
| | | 84 | | public SetVariable([CallerFilePath] string? source = null, [CallerLineNumber] int? line = null) : base(source, line) |
| | | 85 | | { |
| | | 86 | | } |
| | | 87 | | |
| | | 88 | | /// <summary> |
| | | 89 | | /// The variable to assign the value to. |
| | | 90 | | /// </summary> |
| | | 91 | | [Input(Description = "The variable to assign the value to.")] |
| | | 92 | | public Variable? Variable { get; set; } |
| | | 93 | | |
| | | 94 | | /// <summary> |
| | | 95 | | /// The value to assign. |
| | | 96 | | /// </summary> |
| | | 97 | | [Input(Description = "The value to assign.")] |
| | | 98 | | public Input<object?> Value { get; set; } = new(default(object)); |
| | | 99 | | |
| | | 100 | | /// <inheritdoc /> |
| | | 101 | | protected override void Execute(ActivityExecutionContext context) |
| | | 102 | | { |
| | | 103 | | // Always refer to the variable by ID to ensure that the variable is resolved from the correct scope. |
| | | 104 | | var variableId = Variable?.Id; |
| | | 105 | | var variable = context.ExpressionExecutionContext.EnumerateVariablesInScope().FirstOrDefault(x => x.Id == variab |
| | | 106 | | |
| | | 107 | | if (variable == null) |
| | | 108 | | throw new InvalidOperationException($"Variable '{variableId}' not found."); |
| | | 109 | | |
| | | 110 | | var value = context.Get(Value); |
| | | 111 | | variable.Set(context, value); |
| | | 112 | | } |
| | | 113 | | } |