Added EntityChanged Trigger (#512)

Co-authored-by: Rafael Carnucci <rafael.carnucci@icn.de>
This commit is contained in:
Rafael Carnucci 2020-12-14 19:57:33 +01:00 committed by GitHub
parent 6671095efe
commit b22a9f8f8c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
16 changed files with 315 additions and 0 deletions

View file

@ -170,6 +170,10 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ElsaDashboard.Backend", "sr
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ElsaDashboard.WebAssembly", "src\dashboards\blazor\ElsaDashboard.WebAssembly\ElsaDashboard.WebAssembly.csproj", "{0AA2A003-C79E-4B43-803E-E1150254D2B0}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Elsa.Activities.Entity", "src\activities\Elsa.Activities.Entity\Elsa.Activities.Entity.csproj", "{A0A8431F-3A2F-433A-BB0D-EB9627DFA739}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Elsa.Samples.EntityChanged", "src\samples\console\Elsa.Samples.EntityChanged\Elsa.Samples.EntityChanged.csproj", "{A3149FE4-FD39-4DC2-AA9F-208C7216BC03}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
@ -408,6 +412,14 @@ Global
{0AA2A003-C79E-4B43-803E-E1150254D2B0}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0AA2A003-C79E-4B43-803E-E1150254D2B0}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0AA2A003-C79E-4B43-803E-E1150254D2B0}.Release|Any CPU.Build.0 = Release|Any CPU
{A0A8431F-3A2F-433A-BB0D-EB9627DFA739}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A0A8431F-3A2F-433A-BB0D-EB9627DFA739}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A0A8431F-3A2F-433A-BB0D-EB9627DFA739}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A0A8431F-3A2F-433A-BB0D-EB9627DFA739}.Release|Any CPU.Build.0 = Release|Any CPU
{A3149FE4-FD39-4DC2-AA9F-208C7216BC03}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A3149FE4-FD39-4DC2-AA9F-208C7216BC03}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A3149FE4-FD39-4DC2-AA9F-208C7216BC03}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A3149FE4-FD39-4DC2-AA9F-208C7216BC03}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
@ -488,6 +500,8 @@ Global
{CCF8D59E-3672-4238-A900-CE879ABF02D2} = {E02BD53A-5B08-47A0-920E-DD537A6AFAAA}
{B5EAB378-0002-47FD-A0C1-8931DB577006} = {E02BD53A-5B08-47A0-920E-DD537A6AFAAA}
{0AA2A003-C79E-4B43-803E-E1150254D2B0} = {E02BD53A-5B08-47A0-920E-DD537A6AFAAA}
{A0A8431F-3A2F-433A-BB0D-EB9627DFA739} = {B43B546E-23F3-46E8-ACB7-D04F05CDA180}
{A3149FE4-FD39-4DC2-AA9F-208C7216BC03} = {FC9F520F-BA51-4AD2-BFEE-EF787798E734}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {8B0975FD-7050-48B0-88C5-48C33378E158}

View file

@ -0,0 +1,28 @@
using Elsa.Activities.Entity.Models;
using Elsa.ActivityResults;
using Elsa.Attributes;
using Elsa.Design;
using Elsa.Services;
// ReSharper disable once CheckNamespace
namespace Elsa.Activities.Entity
{
[Trigger(
Category = "Entity",
DisplayName = "Entity Changed",
Description = "Trigger when Entity Changed",
Outcomes = new[] { OutcomeNames.Done }
)]
public class EntityChanged : Activity
{
[ActivityProperty(Type = ActivityPropertyTypes.Text, Hint = "Entity Name")]
public string? EntityName { get; set; }
[ActivityProperty(Type = ActivityPropertyTypes.Text, Hint = "Action")]
[SelectOptions("Added", "Updated", "Deleted")]
public EntityChangedAction? Action { get; set; }
protected override IActivityExecutionResult OnExecute() => Done(new EntityChangedDto(EntityName, Action));
}
}

View file

@ -0,0 +1,10 @@
// ReSharper disable once CheckNamespace
namespace Elsa.Activities.Entity
{
public enum EntityChangedAction
{
Added,
Updated,
Deleted
}
}

View file

@ -0,0 +1,14 @@
using System;
using Elsa.Builders;
// ReSharper disable once CheckNamespace
namespace Elsa.Activities.Entity
{
public static class EntityChangedBuilderExtensions
{
private static IActivityBuilder EntityChanged(this IBuilder builder, Action<ISetupActivity<EntityChanged>> setup) => builder.Then(setup);
public static IActivityBuilder EntityChanged(this IBuilder builder, Type entity, EntityChangedAction action) => builder.EntityChanged(setup => setup.WithEntity(entity).WithAction(action));
public static IActivityBuilder EntityChanged<TEntity>(this IBuilder builder, EntityChangedAction action) => builder.EntityChanged(setup => setup.WithEntity(typeof(TEntity)).WithAction(action));
}
}

View file

@ -0,0 +1,15 @@
// ReSharper disable once CheckNamespace
using System;
using Elsa.Activities.Entity.Extensions;
using Elsa.Builders;
namespace Elsa.Activities.Entity
{
public static class EntityChangedExtensions
{
public static ISetupActivity<EntityChanged> WithEntity<T>(this ISetupActivity<EntityChanged> activity) => activity.WithEntity(typeof(T));
public static ISetupActivity<EntityChanged> WithEntity(this ISetupActivity<EntityChanged> activity, Type type) => activity.Set(x => x.EntityName, type.GetEntityName(false));
public static ISetupActivity<EntityChanged> WithAction(this ISetupActivity<EntityChanged> activity, EntityChangedAction action) => activity.Set(x => x.Action, action);
}
}

View file

@ -0,0 +1,15 @@
using System;
namespace Elsa.Activities.Entity.Attributes
{
[AttributeUsage(AttributeTargets.Class)]
public class EntityNameAttribute : Attribute
{
public string Name { get; }
public EntityNameAttribute(string name)
{
Name = name;
}
}
}

View file

@ -0,0 +1,35 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<PackageVersion>1.0.0</PackageVersion>
<LangVersion>latest</LangVersion>
<Authors>Elsa Contributors</Authors>
<Description>
Elsa is a set of workflow libraries and tools that enable lean and mean workflowing capabilities in any .NET Core application.
This package provides the following Entity activities:
* EntityChanged
</Description>
<Copyright>2020</Copyright>
<PackageProjectUrl>https://github.com/elsa-workflows/elsa-core</PackageProjectUrl>
<RepositoryUrl>https://github.com/elsa-workflows/elsa-core</RepositoryUrl>
<RepositoryType>GitHub</RepositoryType>
<PackageTags>elsa, workflows, entity</PackageTags>
<PackageIcon>icon.png</PackageIcon>
<Nullable>enable</Nullable>
</PropertyGroup>
<ItemGroup>
<None Include="icon.png">
<Pack>True</Pack>
<PackagePath />
</None>
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\core\Elsa.Core\Elsa.Core.csproj" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,14 @@
using Elsa.Activities.Entity;
using Elsa.Activities.Entity.Triggers;
// ReSharper disable once CheckNamespace
namespace Microsoft.Extensions.DependencyInjection
{
public static class ServiceCollectionExtensions
{
public static IServiceCollection AddEntityActivities(this IServiceCollection services) =>
services
.AddActivity<EntityChanged>()
.AddTriggerProvider<EntityChangedTriggerProvider>();
}
}

View file

@ -0,0 +1,17 @@
using System;
using System.Reflection;
using Elsa.Activities.Entity.Attributes;
namespace Elsa.Activities.Entity.Extensions
{
public static class TypeExtensions
{
public static string GetEntityName(this Type type, bool inherit = false)
{
var attribute = type.GetCustomAttribute<EntityNameAttribute>(inherit);
return attribute?.Name ?? type.FullName;
}
}
}

View file

@ -0,0 +1,20 @@
namespace Elsa.Activities.Entity.Models
{
public class EntityChangedDto
{
public string? EntityName { get; set; }
public EntityChangedAction? Action { get; set; }
public EntityChangedDto()
{
}
public EntityChangedDto(string? entityName, EntityChangedAction? action)
{
EntityName = entityName;
Action = action;
}
}
}

View file

@ -0,0 +1,43 @@
using System.Threading;
using System.Threading.Tasks;
using Elsa.Triggers;
namespace Elsa.Activities.Entity.Triggers
{
public class EntityChangedTrigger : Trigger
{
public string? EntityName { get; set; }
public EntityChangedAction? Action { get; set; }
public string? ContextId { get; set; }
public string? CorrelationId { get; set; }
public EntityChangedTrigger()
{
}
public EntityChangedTrigger(string? entityName, EntityChangedAction? action, string? contextId, string? correlationId)
{
EntityName = entityName;
Action = action;
ContextId = contextId;
CorrelationId = correlationId;
}
}
public class EntityChangedTriggerProvider : TriggerProvider<EntityChangedTrigger, EntityChanged>
{
public override async ValueTask<ITrigger> GetTriggerAsync(TriggerProviderContext<EntityChanged> context, CancellationToken cancellationToken) =>
new EntityChangedTrigger(
entityName: await context.Activity.GetPropertyValueAsync(x => x.EntityName, cancellationToken),
action: await context.Activity.GetPropertyValueAsync(x => x.Action, cancellationToken),
contextId: context.ActivityExecutionContext.WorkflowExecutionContext.WorkflowInstance.ContextId,
correlationId: context.ActivityExecutionContext.WorkflowExecutionContext.WorkflowInstance.CorrelationId
);
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.6 KiB

View file

@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<IsPackable>false</IsPackable>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="5.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\..\..\activities\Elsa.Activities.Entity\Elsa.Activities.Entity.csproj" />
<ProjectReference Include="..\..\..\core\Elsa\Elsa.csproj" />
</ItemGroup>
</Project>

View file

@ -0,0 +1,9 @@
using Elsa.Activities.Entity.Attributes;
namespace Elsa.Samples.EntityChanged
{
[EntityName("Test-Entity")]
public class Entity
{
}
}

View file

@ -0,0 +1,17 @@
using Elsa.Activities.Entity;
using Elsa.Activities.Console;
using Elsa.Builders;
using Elsa.Activities.Entity.Models;
namespace Elsa.Samples.EntityChanged
{
public class EntityChangedWorkflow : IWorkflow
{
public void Build(IWorkflowBuilder workflow)
{
workflow
.EntityChanged<Entity>(EntityChangedAction.Added)
.WriteLine(context => $"Entity '{((EntityChangedDto)context.Input).EntityName}' was '{((EntityChangedDto)context.Input).Action}'");
}
}
}

View file

@ -0,0 +1,46 @@
using System.Threading.Tasks;
using Elsa.Activities.Entity;
using Elsa.Activities.Entity.Triggers;
using Elsa.Activities.Entity.Extensions;
using Elsa.Services;
using Elsa.Triggers;
using Microsoft.Extensions.DependencyInjection;
using System.Linq;
namespace Elsa.Samples.EntityChanged
{
/// <summary>
/// Demonstrates the use of declarative composite activities.
/// </summary>
class Program
{
static async Task Main()
{
// Create a service container with Elsa services.
var services = new ServiceCollection()
.AddElsa()
.AddConsoleActivities()
.AddEntityActivities()
.AddWorkflow<EntityChangedWorkflow>()
.BuildServiceProvider();
// Run startup actions (not needed when registering Elsa with a Host).
var startupRunner = services.GetRequiredService<IStartupRunner>();
await startupRunner.StartupAsync();
var workflowSelector = services.GetRequiredService<IWorkflowSelector>();
var workflowSelectionResult = (await workflowSelector.SelectWorkflowsAsync<EntityChangedTrigger>(evalute =>
(evalute.Action.HasValue == false || evalute.Action == EntityChangedAction.Added)
&& (evalute.EntityName == null || evalute.EntityName == typeof(Entity).GetEntityName()))).First();
var workflowRunner = services.GetRequiredService<IWorkflowRunner>();
await workflowRunner.RunWorkflowAsync(workflowSelectionResult.WorkflowBlueprint, workflowSelectionResult.ActivityId);
}
}
}