using System.Text.Json.Nodes; using Elsa.Api.Client.Resources.Resilience.Models; using Elsa.Api.Client.Resources.WorkflowDefinitions.Models; using Elsa.Api.Client.Shared.Enums; using Elsa.Api.Client.Shared.Models; namespace Elsa.Api.Client.Extensions; /// /// Provides extension methods for . /// public static class ActivityExtensions { /// /// Gets the type name of the specified activity. /// public static string GetTypeName(this JsonObject activity) => activity.GetProperty("type")!; /// /// Gets the type name of the specified activity. /// public static void SetTypeName(this JsonObject activity, string value) => activity.SetProperty(JsonValue.Create(value), "type"); /// /// Gets the version of the specified activity. /// public static int GetVersion(this JsonObject activity) => activity.GetProperty("version"); /// /// Gets the version of the specified activity. /// public static void SetVersion(this JsonObject activity, int value) => activity.SetProperty(JsonValue.Create(value), "version"); /// /// Gets the ID of the specified activity. /// public static string GetId(this JsonObject activity) => activity.GetProperty("id")!; /// /// Sets the ID of the specified activity. /// public static void SetId(this JsonObject activity, string value) => activity.SetProperty(JsonValue.Create(value), "id"); /// /// Gets the node ID of the specified activity. /// public static string GetNodeId(this JsonObject activity) => activity.GetProperty("nodeId")!; /// /// Sets the node ID of the specified activity. /// public static void SetNodeId(this JsonObject activity, string value) => activity.SetProperty(JsonValue.Create(value), "nodeId"); /// /// Gets the name of the specified activity. /// public static string? GetName(this JsonObject activity) => activity.GetProperty("name"); /// /// Sets the name of the specified activity. /// public static void SetName(this JsonObject activity, string? value) => activity.SetProperty(JsonValue.Create(value), "name"); /// /// Gets the designer metadata for the specified activity. /// public static JsonObject? GetMetadata(this JsonObject activity) => activity.GetProperty("metadata")?.AsObject(); /// /// Sets the designer metadata for the specified activity. /// public static void SetDesignerMetadata(this JsonObject activity, ActivityDesignerMetadata designerMetadata) => activity.SetProperty(designerMetadata.SerializeToNode(), "metadata", "designer"); /// /// Gets the designer metadata for the specified activity. /// public static ActivityDesignerMetadata GetDesignerMetadata(this JsonObject activity) => activity.GetProperty("metadata", "designer") ?? new ActivityDesignerMetadata(); /// /// Gets the display text for the specified activity. /// public static string? GetDisplayText(this JsonObject activity) => activity.GetProperty("metadata", "displayText")?.GetValue(); /// /// Sets the display text for the specified activity. /// public static void SetDisplayText(this JsonObject activity, string? value) { activity.SetProperty(JsonValue.Create(value), "metadata", "displayText"); } /// /// Gets the description for the specified activity. /// public static string? GetDescription(this JsonObject activity) => activity.GetProperty("metadata", "description")?.GetValue(); /// /// Sets the description for the specified activity. /// public static void SetDescription(this JsonObject activity, string value) => activity.SetProperty(JsonValue.Create(value), "metadata", "description"); /// /// Gets a value indicating whether the description for the specified activity should be shown. /// public static bool? GetShowDescription(this JsonObject activity) => activity.GetProperty("metadata", "showDescription"); /// /// Sets a value indicating whether the description for the specified activity should be shown. /// public static void SetShowDescription(this JsonObject activity, bool value) => activity.SetProperty(JsonValue.Create(value), "metadata", "showDescription"); /// /// Gets a value indicating whether the specified activity can trigger the workflow. /// public static bool? GetCanStartWorkflow(this JsonObject activity) => activity.GetProperty("customProperties", "canStartWorkflow"); /// /// Sets a value indicating whether the specified activity can trigger the workflow. /// public static void SetCanStartWorkflow(this JsonObject activity, bool value) => activity.SetProperty(JsonValue.Create(value), "customProperties", "canStartWorkflow"); public static MergeMode GetMergeMode(this JsonObject activity) { return activity.GetProperty("customProperties", "mergeMode") ?? MergeMode.None; } public static void SetMergeMode(this JsonObject activity, MergeMode? value) { activity.SetProperty(JsonValue.Create(value), "customProperties", "mergeMode"); } /// /// Gets the activities in the specified flowchart. /// public static IEnumerable GetActivities(this JsonObject flowchart) => flowchart.GetProperty("activities")?.AsArray().AsEnumerable().Cast() ?? []; /// /// Sets the activities in the specified flowchart. /// public static void SetActivities(this JsonObject flowchart, JsonArray activities) => flowchart.SetProperty(activities, "activities"); /// /// Gets the connections in the specified flowchart. /// public static IEnumerable GetConnections(this JsonObject flowchart) => flowchart.GetProperty>("connections") ?? new List(); /// /// Sets the connections in the specified flowchart. /// public static void SetConnections(this JsonObject flowchart, IEnumerable connections) => flowchart.SetProperty(JsonValue.Create(connections), "connections"); /// /// Gets the root activity in the specified activity. /// public static JsonObject? GetRoot(this JsonObject activity) => activity.GetProperty("root") as JsonObject; /// /// Sets the root activity in the specified activity. /// public static void SetRoot(this JsonObject container, JsonObject root) => container.SetProperty(root, "root"); /// /// Gets the root activity in the specified activity. /// public static string? GetWorkflowDefinitionId(this JsonObject activity) { // Determine if this activity represents a workflow definition activity based on the presence of the following properties. var workflowDefinitionIdNode = activity.GetProperty("workflowDefinitionId"); var workflowDefinitionVersionId = activity.GetProperty("workflowDefinitionVersionId"); var latestAvailablePublishedVersion = activity.GetProperty("latestAvailablePublishedVersion"); var latestAvailablePublishedVersionId = activity.GetProperty("latestAvailablePublishedVersionId"); if (workflowDefinitionIdNode == null || workflowDefinitionVersionId == null || latestAvailablePublishedVersion == null || latestAvailablePublishedVersionId == null) return null; // Not a workflow definition activity. if (workflowDefinitionIdNode is JsonValue value) return value.ToString(); return null; // Not a workflow definition activity. } /// /// Determines whether the given activity is a workflow definition activity. /// /// The JsonObject representing the activity. /// Returns true if the activity is a workflow definition activity; otherwise, false. public static bool GetIsWorkflowDefinitionActivity(this JsonObject activity) => activity.ContainsKey("workflowDefinitionId") && activity.ContainsKey("workflowDefinitionVersionId"); /// /// Gets a value indicating whether the specified activity can trigger the workflow. /// public static bool? GetRunAsynchronously(this JsonObject activity) => activity.GetProperty("customProperties", "runAsynchronously"); /// /// Sets a value indicating whether the specified activity can trigger the workflow. /// public static void SetRunAsynchronously(this JsonObject activity, bool value) => activity.SetProperty(JsonValue.Create(value), "customProperties", "runAsynchronously"); /// /// Gets the commit state behavior for the specified activity. /// public static string? GetCommitStrategy(this JsonObject activity) => activity.TryGetProperty("customProperties", "commitStrategyName"); /// /// Sets the commit state behavior for the specified activity. /// public static void SetCommitStrategy(this JsonObject activity, string? name) => activity.SetProperty(JsonValue.Create(name), "customProperties", "commitStrategyName"); /// /// Gets the resilience strategy for the specified activity. /// public static ResilienceStrategyConfig? GetResilienceStrategy(this JsonObject activity) { var node = activity.GetProperty("customProperties", "resilienceStrategy"); return ResilienceStrategyConfig.Deserialize(node); } /// /// Sets the resilience strategy for the specified activity. /// public static void SetResilienceStrategy(this JsonObject activity, ResilienceStrategyConfig? config) { var node = config?.SerializeToNode(); activity.SetProperty(node, "customProperties", "resilienceStrategy"); } }