2025-02-11 23:12:06 +00:00
|
|
|
|
using System.Text.Json;
|
2025-01-07 09:05:25 +00:00
|
|
|
|
using Elsa.Connections.Attributes;
|
|
|
|
|
|
using Elsa.Connections.Models;
|
2025-02-11 23:12:06 +00:00
|
|
|
|
using Elsa.Extensions;
|
2025-01-07 09:05:25 +00:00
|
|
|
|
using Elsa.Workflows;
|
|
|
|
|
|
using Elsa.Workflows.Pipelines.ActivityExecution;
|
|
|
|
|
|
using JetBrains.Annotations;
|
2025-02-11 23:12:06 +00:00
|
|
|
|
using Microsoft.Extensions.Logging;
|
|
|
|
|
|
using Elsa.Connections.Core.Extensions;
|
|
|
|
|
|
using Elsa.Connections.Converters;
|
|
|
|
|
|
using Elsa.Connections.Persistence.Contracts;
|
2025-01-07 09:05:25 +00:00
|
|
|
|
|
|
|
|
|
|
namespace Elsa.Connections.Middleware;
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
2025-02-11 23:12:06 +00:00
|
|
|
|
/// An activity execution middleware component that inject connection property from Connection Name.
|
2025-01-07 09:05:25 +00:00
|
|
|
|
/// </summary>
|
|
|
|
|
|
[UsedImplicitly]
|
2025-02-11 23:12:06 +00:00
|
|
|
|
public class ConnectionMiddleware(ActivityMiddlewareDelegate next
|
|
|
|
|
|
, IConnectionStore connectionStore
|
|
|
|
|
|
,ILogger<ConnectionMiddleware> logger)
|
|
|
|
|
|
: IActivityExecutionMiddleware
|
2025-01-07 09:05:25 +00:00
|
|
|
|
{
|
2025-02-11 23:12:06 +00:00
|
|
|
|
private static JsonSerializerOptions? _serializerOptions;
|
|
|
|
|
|
private readonly ILogger<ConnectionMiddleware> _logger = logger;
|
|
|
|
|
|
|
|
|
|
|
|
private static JsonSerializerOptions SerializerOptions =>
|
|
|
|
|
|
_serializerOptions ??= new JsonSerializerOptions
|
|
|
|
|
|
{
|
|
|
|
|
|
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
|
|
|
|
|
|
}.WithConverters(new Int32Converter());
|
|
|
|
|
|
|
2025-01-07 09:05:25 +00:00
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
|
public async ValueTask InvokeAsync(ActivityExecutionContext context)
|
|
|
|
|
|
{
|
|
|
|
|
|
var activityDescriptor = context.ActivityDescriptor;
|
|
|
|
|
|
|
|
|
|
|
|
if (activityDescriptor.Attributes.Any(attr => attr.GetType() == typeof(ConnectionActivityAttribute)))
|
|
|
|
|
|
{
|
|
|
|
|
|
var inputDescriptors = activityDescriptor.Inputs.Where(x => x?.PropertyInfo.PropertyType.GetGenericTypeDefinition() == typeof(ConnectionProperties<>));
|
|
|
|
|
|
|
|
|
|
|
|
if (inputDescriptors != null && inputDescriptors.Any())
|
|
|
|
|
|
{
|
|
|
|
|
|
var input = inputDescriptors.First();
|
|
|
|
|
|
var propertyType = input?.PropertyInfo?.PropertyType.GetGenericArguments()[0];
|
|
|
|
|
|
|
|
|
|
|
|
dynamic inputValue = input.ValueGetter(context.Activity);
|
|
|
|
|
|
|
|
|
|
|
|
var connectionName = (string)inputValue?.ConnectionName;
|
|
|
|
|
|
|
2025-02-11 23:12:06 +00:00
|
|
|
|
if (connectionName == null)
|
|
|
|
|
|
LogConnectionExtensions.LogConnectionIsNull(_logger);
|
|
|
|
|
|
else
|
2025-01-07 09:05:25 +00:00
|
|
|
|
{
|
2025-02-11 23:12:06 +00:00
|
|
|
|
//Get connection from store, if exist,
|
|
|
|
|
|
var connectionConfiguration = await connectionStore.FindAsync(new Persistence.Filters.ConnectionDefinitionFilter() { Name = connectionName });
|
|
|
|
|
|
if (connectionConfiguration != null)
|
2025-01-07 09:05:25 +00:00
|
|
|
|
{
|
2025-02-11 23:12:06 +00:00
|
|
|
|
dynamic deserializedjson =
|
|
|
|
|
|
JsonSerializer.Deserialize(connectionConfiguration.ConnectionConfiguration, propertyType, SerializerOptions);
|
2025-01-07 09:05:25 +00:00
|
|
|
|
|
2025-02-11 23:12:06 +00:00
|
|
|
|
inputValue.Properties = deserializedjson;
|
2025-01-07 09:05:25 +00:00
|
|
|
|
|
2025-02-11 23:12:06 +00:00
|
|
|
|
input.ValueSetter(context.Activity, inputValue);
|
|
|
|
|
|
}
|
|
|
|
|
|
else
|
|
|
|
|
|
LogConnectionExtensions.LogConnectionNotFound(_logger, connectionName);
|
2025-01-07 09:05:25 +00:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
await next(context);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private static bool IsActivityBookmarked(ActivityExecutionContext context)
|
|
|
|
|
|
{
|
|
|
|
|
|
return context.WorkflowExecutionContext.Bookmarks.Any(b => b.ActivityNodeId.Equals(context.ActivityNode.NodeId));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|