| | | 1 | | using Elsa.Workflows; |
| | | 2 | | |
| | | 3 | | // ReSharper disable once CheckNamespace |
| | | 4 | | namespace Elsa.Extensions; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Provides extension methods for <see cref="IActivity"/>. |
| | | 8 | | /// </summary> |
| | | 9 | | public static class ActivityPropertyExtensions |
| | | 10 | | { |
| | 5 | 11 | | private static readonly string[] CanStartWorkflowPropertyName = ["canStartWorkflow", "CanStartWorkflow"]; |
| | 5 | 12 | | private static readonly string[] RunAsynchronouslyPropertyName = ["runAsynchronously", "RunAsynchronously"]; |
| | 5 | 13 | | private static readonly string[] SourcePropertyName = ["source", "Source"]; |
| | 5 | 14 | | private static readonly string[] CommitStrategyName = ["commitStrategyName", "CommitStrategyName"]; |
| | | 15 | | |
| | | 16 | | extension(IActivity activity) |
| | | 17 | | { |
| | | 18 | | /// <summary> |
| | | 19 | | /// Gets a flag indicating whether this activity can be used for starting a workflow. |
| | | 20 | | /// Usually used for triggers, but also used to disambiguate between two or more starting activities and no star |
| | | 21 | | /// </summary> |
| | 3732 | 22 | | public bool GetCanStartWorkflow() => activity.CustomProperties.GetValueOrDefault(CanStartWorkflowPropertyName, ( |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Sets a flag indicating whether this activity can be used for starting a workflow. |
| | | 26 | | /// </summary> |
| | 2670 | 27 | | public void SetCanStartWorkflow(bool value) => activity.CustomProperties[CanStartWorkflowPropertyName[0]] = valu |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Gets a flag indicating if this activity should execute synchronously or asynchronously. |
| | | 31 | | /// By default, activities with an <see cref="ActivityKind"/> of <see cref="Action"/>, <see cref="Task"/> or <se |
| | | 32 | | /// will execute synchronously, while activities of the <see cref="ActivityKind.Job"/> kind will execute asynchr |
| | | 33 | | /// </summary> |
| | | 34 | | public bool? GetRunAsynchronously() |
| | | 35 | | { |
| | 48 | 36 | | return activity.CustomProperties.GetValueOrDefault<bool?>(RunAsynchronouslyPropertyName, defaultValueFactory |
| | | 37 | | } |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Sets a flag indicating if this activity should execute synchronously or asynchronously. |
| | | 41 | | /// By default, activities with an <see cref="ActivityKind"/> of <see cref="Action"/>, <see cref="Task"/> or <se |
| | | 42 | | /// will execute synchronously, while activities of the <see cref="ActivityKind.Job"/> kind will execute asynchr |
| | | 43 | | /// </summary> |
| | 3663 | 44 | | public void SetRunAsynchronously(bool? value) => activity.CustomProperties[RunAsynchronouslyPropertyName[0]] = v |
| | | 45 | | |
| | | 46 | | /// <summary> |
| | | 47 | | /// Gets the source file and line number where this activity was instantiated, if any. |
| | | 48 | | /// </summary> |
| | 9586 | 49 | | public string? GetSource() => activity.CustomProperties.GetValueOrDefault(SourcePropertyName, () => default(stri |
| | | 50 | | |
| | | 51 | | /// <summary> |
| | | 52 | | /// Sets the source file and line number where this activity was instantiated, if any. |
| | | 53 | | /// </summary> |
| | 8108 | 54 | | public void SetSource(string value) => activity.CustomProperties[SourcePropertyName[0]] = value; |
| | | 55 | | |
| | | 56 | | /// <summary> |
| | | 57 | | /// Gets the commit state behavior for the specified activity. |
| | | 58 | | /// </summary> |
| | 12062 | 59 | | public string? GetCommitStrategy() => activity.CustomProperties.GetValueOrDefault<string?>(CommitStrategyName, ( |
| | | 60 | | } |
| | | 61 | | |
| | | 62 | | /// <summary> |
| | | 63 | | /// Sets the commit state behavior for the specified activity. |
| | | 64 | | /// </summary> |
| | | 65 | | public static TActivity SetCommitStrategy<TActivity>(this TActivity activity, string? name) where TActivity : IActiv |
| | | 66 | | { |
| | 0 | 67 | | if (string.IsNullOrWhiteSpace(name)) |
| | 0 | 68 | | activity.CustomProperties.Remove(CommitStrategyName[0]); |
| | | 69 | | else |
| | 0 | 70 | | activity.CustomProperties[CommitStrategyName[0]] = name; |
| | 0 | 71 | | return activity; |
| | | 72 | | } |
| | | 73 | | |
| | | 74 | | extension(IActivity activity) |
| | | 75 | | { |
| | | 76 | | /// <summary> |
| | | 77 | | /// Sets the source file and line number where this activity was instantiated, if any. |
| | | 78 | | /// </summary> |
| | | 79 | | public void SetSource(string? sourceFile, int? lineNumber) |
| | | 80 | | { |
| | 12972 | 81 | | if (sourceFile == null || lineNumber == null) |
| | 4864 | 82 | | return; |
| | | 83 | | |
| | 8108 | 84 | | var source = $"{Path.GetFileName(sourceFile)}:{lineNumber}"; |
| | 8108 | 85 | | activity.SetSource(source); |
| | 8108 | 86 | | } |
| | | 87 | | |
| | | 88 | | /// <summary> |
| | | 89 | | /// Gets the display text for the specified activity. |
| | | 90 | | /// </summary> |
| | 0 | 91 | | public string? GetDisplayText() => activity.Metadata.TryGetValue("displayText", out var value) ? value.ToString( |
| | | 92 | | |
| | | 93 | | /// <summary> |
| | | 94 | | /// Sets the display text for the specified activity. |
| | | 95 | | /// </summary> |
| | 7 | 96 | | public void SetDisplayText(string value) => activity.Metadata["displayText"] = value; |
| | | 97 | | |
| | | 98 | | /// <summary> |
| | | 99 | | /// Gets the description for the specified activity. |
| | | 100 | | /// </summary> |
| | 0 | 101 | | public string? GetDescription() => activity.Metadata.TryGetValue("description", out var value) ? value.ToString( |
| | | 102 | | |
| | | 103 | | /// <summary> |
| | | 104 | | /// Sets the description for the specified activity. |
| | | 105 | | /// </summary> |
| | 7 | 106 | | public void SetDescription(string value) => activity.Metadata["description"] = value; |
| | | 107 | | } |
| | | 108 | | } |