| | | 1 | | using System.Text.Json.Nodes; |
| | | 2 | | using Elsa.Api.Client.Resources.Resilience.Models; |
| | | 3 | | using Elsa.Api.Client.Resources.WorkflowDefinitions.Models; |
| | | 4 | | using Elsa.Api.Client.Shared.Enums; |
| | | 5 | | using Elsa.Api.Client.Shared.Models; |
| | | 6 | | |
| | | 7 | | namespace Elsa.Api.Client.Extensions; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Provides extension methods for <see cref="JsonObject"/>. |
| | | 11 | | /// </summary> |
| | | 12 | | public static class ActivityExtensions |
| | | 13 | | { |
| | | 14 | | /// <summary> |
| | | 15 | | /// Gets the type name of the specified activity. |
| | | 16 | | /// </summary> |
| | 0 | 17 | | public static string GetTypeName(this JsonObject activity) => activity.GetProperty<string>("type")!; |
| | | 18 | | |
| | | 19 | | /// <summary> |
| | | 20 | | /// Gets the type name of the specified activity. |
| | | 21 | | /// </summary> |
| | 0 | 22 | | public static void SetTypeName(this JsonObject activity, string value) => activity.SetProperty(JsonValue.Create(valu |
| | | 23 | | |
| | | 24 | | /// <summary> |
| | | 25 | | /// Gets the version of the specified activity. |
| | | 26 | | /// </summary> |
| | 0 | 27 | | public static int GetVersion(this JsonObject activity) => activity.GetProperty<int>("version"); |
| | | 28 | | |
| | | 29 | | /// <summary> |
| | | 30 | | /// Gets the version of the specified activity. |
| | | 31 | | /// </summary> |
| | 0 | 32 | | public static void SetVersion(this JsonObject activity, int value) => activity.SetProperty(JsonValue.Create(value), |
| | | 33 | | |
| | | 34 | | /// <summary> |
| | | 35 | | /// Gets the ID of the specified activity. |
| | | 36 | | /// </summary> |
| | 0 | 37 | | public static string GetId(this JsonObject activity) => activity.GetProperty<string>("id")!; |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Sets the ID of the specified activity. |
| | | 41 | | /// </summary> |
| | 0 | 42 | | public static void SetId(this JsonObject activity, string value) => activity.SetProperty(JsonValue.Create(value), "i |
| | | 43 | | |
| | | 44 | | /// <summary> |
| | | 45 | | /// Gets the node ID of the specified activity. |
| | | 46 | | /// </summary> |
| | 0 | 47 | | public static string GetNodeId(this JsonObject activity) => activity.GetProperty<string>("nodeId")!; |
| | | 48 | | |
| | | 49 | | /// <summary> |
| | | 50 | | /// Sets the node ID of the specified activity. |
| | | 51 | | /// </summary> |
| | 0 | 52 | | public static void SetNodeId(this JsonObject activity, string value) => activity.SetProperty(JsonValue.Create(value) |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// Gets the name of the specified activity. |
| | | 56 | | /// </summary> |
| | 0 | 57 | | public static string? GetName(this JsonObject activity) => activity.GetProperty<string>("name"); |
| | | 58 | | |
| | | 59 | | /// <summary> |
| | | 60 | | /// Sets the name of the specified activity. |
| | | 61 | | /// </summary> |
| | 0 | 62 | | public static void SetName(this JsonObject activity, string? value) => activity.SetProperty(JsonValue.Create(value), |
| | | 63 | | |
| | | 64 | | /// <summary> |
| | | 65 | | /// Gets the designer metadata for the specified activity. |
| | | 66 | | /// </summary> |
| | 0 | 67 | | public static JsonObject? GetMetadata(this JsonObject activity) => activity.GetProperty("metadata")?.AsObject(); |
| | | 68 | | |
| | | 69 | | /// <summary> |
| | | 70 | | /// Sets the designer metadata for the specified activity. |
| | | 71 | | /// </summary> |
| | 0 | 72 | | public static void SetDesignerMetadata(this JsonObject activity, ActivityDesignerMetadata designerMetadata) => activ |
| | | 73 | | |
| | | 74 | | /// <summary> |
| | | 75 | | /// Gets the designer metadata for the specified activity. |
| | | 76 | | /// </summary> |
| | 0 | 77 | | public static ActivityDesignerMetadata GetDesignerMetadata(this JsonObject activity) => activity.GetProperty<Activit |
| | | 78 | | |
| | | 79 | | /// <summary> |
| | | 80 | | /// Gets the display text for the specified activity. |
| | | 81 | | /// </summary> |
| | 0 | 82 | | public static string? GetDisplayText(this JsonObject activity) => activity.GetProperty("metadata", "displayText")?.G |
| | | 83 | | |
| | | 84 | | /// <summary> |
| | | 85 | | /// Sets the display text for the specified activity. |
| | | 86 | | /// </summary> |
| | | 87 | | public static void SetDisplayText(this JsonObject activity, string? value) |
| | | 88 | | { |
| | 0 | 89 | | activity.SetProperty(JsonValue.Create(value), "metadata", "displayText"); |
| | 0 | 90 | | } |
| | | 91 | | |
| | | 92 | | /// <summary> |
| | | 93 | | /// Gets the description for the specified activity. |
| | | 94 | | /// </summary> |
| | 0 | 95 | | public static string? GetDescription(this JsonObject activity) => activity.GetProperty("metadata", "description")?.G |
| | | 96 | | |
| | | 97 | | /// <summary> |
| | | 98 | | /// Sets the description for the specified activity. |
| | | 99 | | /// </summary> |
| | 0 | 100 | | public static void SetDescription(this JsonObject activity, string value) => activity.SetProperty(JsonValue.Create(v |
| | | 101 | | |
| | | 102 | | /// <summary> |
| | | 103 | | /// Gets a value indicating whether the description for the specified activity should be shown. |
| | | 104 | | /// </summary> |
| | 0 | 105 | | public static bool? GetShowDescription(this JsonObject activity) => activity.GetProperty<bool>("metadata", "showDesc |
| | | 106 | | |
| | | 107 | | /// <summary> |
| | | 108 | | /// Sets a value indicating whether the description for the specified activity should be shown. |
| | | 109 | | /// </summary> |
| | 0 | 110 | | public static void SetShowDescription(this JsonObject activity, bool value) => activity.SetProperty(JsonValue.Create |
| | | 111 | | |
| | | 112 | | /// <summary> |
| | | 113 | | /// Gets a value indicating whether the specified activity can trigger the workflow. |
| | | 114 | | /// </summary> |
| | 0 | 115 | | public static bool? GetCanStartWorkflow(this JsonObject activity) => activity.GetProperty<bool>("customProperties", |
| | | 116 | | |
| | | 117 | | /// <summary> |
| | | 118 | | /// Sets a value indicating whether the specified activity can trigger the workflow. |
| | | 119 | | /// </summary> |
| | 0 | 120 | | public static void SetCanStartWorkflow(this JsonObject activity, bool value) => activity.SetProperty(JsonValue.Creat |
| | | 121 | | |
| | | 122 | | public static MergeMode GetMergeMode(this JsonObject activity) |
| | | 123 | | { |
| | 0 | 124 | | var value = activity.GetProperty<MergeMode?>("customProperties", "mergeMode"); |
| | | 125 | | // Treat MergeMode.None as equivalent to null (no merge mode set), defaulting to Stream |
| | 0 | 126 | | return value == null || value == MergeMode.None ? MergeMode.Stream : value.Value; |
| | | 127 | | } |
| | | 128 | | |
| | | 129 | | public static void SetMergeMode(this JsonObject activity, MergeMode? value) |
| | | 130 | | { |
| | | 131 | | // Treat MergeMode.None as equivalent to null (no merge mode set) |
| | 0 | 132 | | if (value == MergeMode.None) |
| | 0 | 133 | | value = null; |
| | 0 | 134 | | activity.SetProperty(JsonValue.Create(value), "customProperties", "mergeMode"); |
| | 0 | 135 | | } |
| | | 136 | | |
| | | 137 | | /// <summary> |
| | | 138 | | /// Gets the activities in the specified flowchart. |
| | | 139 | | /// </summary> |
| | 0 | 140 | | public static IEnumerable<JsonObject> GetActivities(this JsonObject flowchart) => flowchart.GetProperty("activities" |
| | | 141 | | |
| | | 142 | | /// <summary> |
| | | 143 | | /// Sets the activities in the specified flowchart. |
| | | 144 | | /// </summary> |
| | 0 | 145 | | public static void SetActivities(this JsonObject flowchart, JsonArray activities) => flowchart.SetProperty(activitie |
| | | 146 | | |
| | | 147 | | /// <summary> |
| | | 148 | | /// Gets the connections in the specified flowchart. |
| | | 149 | | /// </summary> |
| | 0 | 150 | | public static IEnumerable<Connection> GetConnections(this JsonObject flowchart) => flowchart.GetProperty<ICollection |
| | | 151 | | |
| | | 152 | | /// <summary> |
| | | 153 | | /// Sets the connections in the specified flowchart. |
| | | 154 | | /// </summary> |
| | 0 | 155 | | public static void SetConnections(this JsonObject flowchart, IEnumerable<Connection> connections) => flowchart.SetPr |
| | | 156 | | |
| | | 157 | | /// <summary> |
| | | 158 | | /// Gets the root activity in the specified activity. |
| | | 159 | | /// </summary> |
| | 0 | 160 | | public static JsonObject? GetRoot(this JsonObject activity) => activity.GetProperty("root") as JsonObject; |
| | | 161 | | |
| | | 162 | | /// <summary> |
| | | 163 | | /// Sets the root activity in the specified activity. |
| | | 164 | | /// </summary> |
| | 0 | 165 | | public static void SetRoot(this JsonObject container, JsonObject root) => container.SetProperty(root, "root"); |
| | | 166 | | |
| | | 167 | | /// <summary> |
| | | 168 | | /// Gets the root activity in the specified activity. |
| | | 169 | | /// </summary> |
| | | 170 | | public static string? GetWorkflowDefinitionId(this JsonObject activity) |
| | | 171 | | { |
| | | 172 | | // Determine if this activity represents a workflow definition activity based on the presence of the following p |
| | 0 | 173 | | var workflowDefinitionIdNode = activity.GetProperty("workflowDefinitionId"); |
| | 0 | 174 | | var workflowDefinitionVersionId = activity.GetProperty("workflowDefinitionVersionId"); |
| | 0 | 175 | | var latestAvailablePublishedVersion = activity.GetProperty("latestAvailablePublishedVersion"); |
| | 0 | 176 | | var latestAvailablePublishedVersionId = activity.GetProperty("latestAvailablePublishedVersionId"); |
| | | 177 | | |
| | 0 | 178 | | if (workflowDefinitionIdNode == null || workflowDefinitionVersionId == null || latestAvailablePublishedVersion = |
| | 0 | 179 | | return null; // Not a workflow definition activity. |
| | | 180 | | |
| | 0 | 181 | | if (workflowDefinitionIdNode is JsonValue value) |
| | 0 | 182 | | return value.ToString(); |
| | | 183 | | |
| | 0 | 184 | | return null; // Not a workflow definition activity. |
| | | 185 | | } |
| | | 186 | | |
| | | 187 | | /// <summary> |
| | | 188 | | /// Determines whether the given activity is a workflow definition activity. |
| | | 189 | | /// </summary> |
| | | 190 | | /// <param name="activity">The JsonObject representing the activity.</param> |
| | | 191 | | /// <returns>Returns true if the activity is a workflow definition activity; otherwise, false.</returns> |
| | 0 | 192 | | public static bool GetIsWorkflowDefinitionActivity(this JsonObject activity) => activity.ContainsKey("workflowDefini |
| | | 193 | | |
| | | 194 | | /// <summary> |
| | | 195 | | /// Gets a value indicating whether the specified activity can trigger the workflow. |
| | | 196 | | /// </summary> |
| | 0 | 197 | | public static bool? GetRunAsynchronously(this JsonObject activity) => activity.GetProperty<bool>("customProperties", |
| | | 198 | | |
| | | 199 | | /// <summary> |
| | | 200 | | /// Sets a value indicating whether the specified activity can trigger the workflow. |
| | | 201 | | /// </summary> |
| | 0 | 202 | | public static void SetRunAsynchronously(this JsonObject activity, bool value) => activity.SetProperty(JsonValue.Crea |
| | | 203 | | |
| | | 204 | | /// <summary> |
| | | 205 | | /// Gets the commit state behavior for the specified activity. |
| | | 206 | | /// </summary> |
| | 0 | 207 | | public static string? GetCommitStrategy(this JsonObject activity) => activity.TryGetProperty<string?>("customPropert |
| | | 208 | | |
| | | 209 | | /// <summary> |
| | | 210 | | /// Sets the commit state behavior for the specified activity. |
| | | 211 | | /// </summary> |
| | 0 | 212 | | public static void SetCommitStrategy(this JsonObject activity, string? name) => activity.SetProperty(JsonValue.Creat |
| | | 213 | | |
| | | 214 | | |
| | | 215 | | /// <summary> |
| | | 216 | | /// Gets the resilience strategy for the specified activity. |
| | | 217 | | /// </summary> |
| | | 218 | | public static ResilienceStrategyConfig? GetResilienceStrategy(this JsonObject activity) |
| | | 219 | | { |
| | 0 | 220 | | var node = activity.GetProperty("customProperties", "resilienceStrategy"); |
| | 0 | 221 | | return ResilienceStrategyConfig.Deserialize(node); |
| | | 222 | | } |
| | | 223 | | |
| | | 224 | | /// <summary> |
| | | 225 | | /// Sets the resilience strategy for the specified activity. |
| | | 226 | | /// </summary> |
| | | 227 | | public static void SetResilienceStrategy(this JsonObject activity, ResilienceStrategyConfig? config) |
| | | 228 | | { |
| | 0 | 229 | | var node = config?.SerializeToNode(); |
| | 0 | 230 | | activity.SetProperty(node, "customProperties", "resilienceStrategy"); |
| | 0 | 231 | | } |
| | | 232 | | } |