| | | 1 | | using System.Diagnostics.CodeAnalysis; |
| | | 2 | | using Elsa.Alterations.AlterationTypes; |
| | | 3 | | using Elsa.Alterations.Core.Abstractions; |
| | | 4 | | using Elsa.Alterations.Core.Contexts; |
| | | 5 | | using Elsa.Extensions; |
| | | 6 | | using Elsa.Workflows; |
| | | 7 | | using Elsa.Workflows.Activities; |
| | | 8 | | using Elsa.Workflows.Memory; |
| | | 9 | | using JetBrains.Annotations; |
| | | 10 | | using Microsoft.Extensions.DependencyInjection; |
| | | 11 | | |
| | | 12 | | namespace Elsa.Alterations.AlterationHandlers; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Modifies a workflow variable. |
| | | 16 | | /// </summary> |
| | | 17 | | [UsedImplicitly] |
| | | 18 | | public class ModifyVariableHandler : AlterationHandlerBase<ModifyVariable> |
| | | 19 | | { |
| | | 20 | | /// <inheritdoc /> |
| | | 21 | | [RequiresUnreferencedCode("Calls System.Text.Json.JsonSerializer.Serialize<TValue>(TValue, JsonSerializerOptions)")] |
| | | 22 | | protected override async ValueTask HandleAsync(AlterationContext context, ModifyVariable alteration) |
| | | 23 | | { |
| | 0 | 24 | | var workflow = context.Workflow; |
| | 0 | 25 | | var cancellationToken = context.CancellationToken; |
| | 0 | 26 | | var variable = await FindVariable(context, alteration, workflow, cancellationToken); |
| | | 27 | | |
| | 0 | 28 | | if (variable == null) |
| | | 29 | | { |
| | 0 | 30 | | context.Fail($"Variable with ID {alteration.VariableId} not found"); |
| | 0 | 31 | | return; |
| | | 32 | | } |
| | | 33 | | |
| | 0 | 34 | | var convertedValue = variable.ParseValue(alteration.Value); |
| | 0 | 35 | | UpdateVariable(context, variable, convertedValue); |
| | 0 | 36 | | context.Succeed(); |
| | 0 | 37 | | } |
| | | 38 | | |
| | | 39 | | private void UpdateVariable(AlterationContext context, Variable variable, object? value) |
| | | 40 | | { |
| | 0 | 41 | | var activityExecutionContext = FindActivityExecutionContextContainingVariable(context, variable); |
| | | 42 | | |
| | 0 | 43 | | if (activityExecutionContext == null) |
| | | 44 | | { |
| | 0 | 45 | | context.Fail($"Activity execution context containing variable with ID {variable.Id} not found"); |
| | 0 | 46 | | return; |
| | | 47 | | } |
| | | 48 | | |
| | 0 | 49 | | variable.Set(activityExecutionContext, value); |
| | 0 | 50 | | } |
| | | 51 | | |
| | | 52 | | private ActivityExecutionContext? FindActivityExecutionContextContainingVariable(AlterationContext context, Variable |
| | | 53 | | { |
| | 0 | 54 | | var query = |
| | 0 | 55 | | from activityExecutionContext in context.WorkflowExecutionContext.ActivityExecutionContexts |
| | 0 | 56 | | from var in activityExecutionContext.Variables |
| | 0 | 57 | | where var.Id == variable.Id |
| | 0 | 58 | | select activityExecutionContext; |
| | | 59 | | |
| | 0 | 60 | | return query.FirstOrDefault(); |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | private async Task<Variable?> FindVariable(AlterationContext context, ModifyVariable alteration, Workflow workflow, |
| | | 64 | | { |
| | 0 | 65 | | var activityVisitor = context.ServiceProvider.GetRequiredService<IActivityVisitor>(); |
| | 0 | 66 | | var graph = await activityVisitor.VisitAsync(workflow, cancellationToken); |
| | 0 | 67 | | var flattenedList = graph.Flatten().ToList(); |
| | | 68 | | |
| | 0 | 69 | | return flattenedList |
| | 0 | 70 | | .Where(x => x.Activity is IVariableContainer) |
| | 0 | 71 | | .SelectMany(x => ((IVariableContainer)x.Activity).Variables) |
| | 0 | 72 | | .FirstOrDefault(x => x.Id == alteration.VariableId); |
| | 0 | 73 | | } |
| | | 74 | | } |