| | | 1 | | using Elsa.Common.Models; |
| | | 2 | | using Elsa.Common.Multitenancy; |
| | | 3 | | using Elsa.Workflows.Management.Entities; |
| | | 4 | | using Elsa.Workflows.Management.Filters; |
| | | 5 | | using Elsa.Workflows.Models; |
| | | 6 | | |
| | | 7 | | namespace Elsa.Workflows.Management.Activities.WorkflowDefinitionActivity; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Provides activity descriptors based on <see cref="WorkflowDefinition"/>s stored in the database. |
| | | 11 | | /// </summary> |
| | | 12 | | public class WorkflowDefinitionActivityProvider : IActivityProvider |
| | | 13 | | { |
| | | 14 | | private readonly IWorkflowDefinitionStore _store; |
| | | 15 | | private readonly WorkflowDefinitionActivityDescriptorFactory _workflowDefinitionActivityDescriptorFactory; |
| | | 16 | | private readonly ITenantAccessor? _tenantAccessor; |
| | | 17 | | |
| | 421 | 18 | | public WorkflowDefinitionActivityProvider(IWorkflowDefinitionStore store, WorkflowDefinitionActivityDescriptorFactor |
| | | 19 | | { |
| | 421 | 20 | | _store = store; |
| | 421 | 21 | | _workflowDefinitionActivityDescriptorFactory = workflowDefinitionActivityDescriptorFactory; |
| | 421 | 22 | | } |
| | | 23 | | |
| | 421 | 24 | | public WorkflowDefinitionActivityProvider(IWorkflowDefinitionStore store, WorkflowDefinitionActivityDescriptorFactor |
| | | 25 | | { |
| | 421 | 26 | | _tenantAccessor = tenantAccessor; |
| | 421 | 27 | | } |
| | | 28 | | |
| | | 29 | | /// <inheritdoc /> |
| | | 30 | | public async ValueTask<IEnumerable<ActivityDescriptor>> GetDescriptorsAsync(CancellationToken cancellationToken = de |
| | | 31 | | { |
| | 163 | 32 | | var filter = new WorkflowDefinitionFilter |
| | 163 | 33 | | { |
| | 163 | 34 | | UsableAsActivity = true, |
| | 163 | 35 | | VersionOptions = VersionOptions.All |
| | 163 | 36 | | }; |
| | | 37 | | |
| | 163 | 38 | | var definitions = (await _store.FindManyAsync(filter, cancellationToken)).ToList(); |
| | | 39 | | |
| | 163 | 40 | | if (_tenantAccessor != null) |
| | | 41 | | { |
| | 163 | 42 | | var currentTenantId = _tenantAccessor.TenantId; |
| | 1877 | 43 | | definitions = definitions.Where(x => x.TenantId == Tenant.AgnosticTenantId || x.TenantId.NormalizeTenantId() |
| | | 44 | | } |
| | | 45 | | |
| | 163 | 46 | | return CreateDescriptors(definitions).ToList(); |
| | 163 | 47 | | } |
| | | 48 | | |
| | | 49 | | private IEnumerable<ActivityDescriptor> CreateDescriptors(ICollection<WorkflowDefinition> definitions) |
| | | 50 | | { |
| | 1877 | 51 | | return definitions.Select(x => CreateDescriptor(x, definitions)); |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | private ActivityDescriptor CreateDescriptor(WorkflowDefinition definition, ICollection<WorkflowDefinition> allDefini |
| | | 55 | | { |
| | 1714 | 56 | | var latestPublishedVersion = allDefinitions |
| | 21838 | 57 | | .Where(x => x.DefinitionId == definition.DefinitionId && x.IsPublished) |
| | 3210 | 58 | | .MaxBy(x => x.Version); |
| | 1714 | 59 | | return _workflowDefinitionActivityDescriptorFactory.CreateDescriptor(definition, latestPublishedVersion); |
| | | 60 | | } |
| | | 61 | | } |