Skip to content

Commit

Permalink
Add WorkflowManagerExtensions.
Browse files Browse the repository at this point in the history
  • Loading branch information
sarahelsaig committed Aug 6, 2023
1 parent 8e53e98 commit d6f0688
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Lombiq.HelpfulLibraries.OrchardCore/Docs/Workflow.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@

- `WorkflowManagerExtensions`: Adds `IWorkflowManager` extension methods for specific workflow events and `IEnumerable<IWorkflowManager>` extension methods for triggering workflow events only if there is a workflow manager.

## Activities

- `SimpleEventActivity`: A base class for a simple workflow event that only has a `Done` result.
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
using OrchardCore.ContentManagement;
using OrchardCore.Workflows.Activities;
using OrchardCore.Workflows.Services;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Lombiq.HelpfulLibraries.OrchardCore.Workflow;

public static class WorkflowManagerExtensions
{
/// <summary>
/// Triggers an event by passing <paramref name="content"/>'s <see cref="ContentItem"/>.
/// </summary>
/// <typeparam name="T">
/// The type of the activity to trigger. This will only work when it's the same as the event's type name which is
/// customary in most events and enforced in <see cref="SimpleEventActivity"/> events.
/// </typeparam>
public static Task TriggerContentItemEventAsync<T>(this IWorkflowManager workflowManager, IContent content)
where T : EventActivity
{
var contentItem = content.ContentItem;
return workflowManager.TriggerEventAsync(
typeof(T).Name,
contentItem,
$"{contentItem.ContentType}-{contentItem.ContentItemId}");
}

/// <inheritdoc cref="TriggerContentItemEventAsync{T}(IWorkflowManager, IContent)"/>
/// <remarks><para>Executes on the first item of <paramref name="workflowManagers"/> if any.</para></remarks>
public static Task TriggerContentItemEventAsync<T>(this IEnumerable<IWorkflowManager> workflowManagers, IContent content)
where T : EventActivity =>
workflowManagers.FirstOrDefault() is { } manager
? manager.TriggerContentItemEventAsync<T>(content)
: Task.CompletedTask;

/// <summary>
/// Triggers the <see cref="IEvent"/> identified by <paramref name="name"/>.
/// </summary>
/// <remarks><para>Executes on the first item of <paramref name="workflowManagers"/> if any.</para></remarks>
public static Task TriggerEventAsync(
this IEnumerable<IWorkflowManager> workflowManagers,
string name,
object input = null,
string correlationId = null) =>
workflowManagers.FirstOrDefault() is { } workflowManager
? workflowManager.TriggerEventAsync(name, input, correlationId)
: Task.CompletedTask;
}

0 comments on commit d6f0688

Please sign in to comment.