| | | 1 | | using System.Diagnostics.CodeAnalysis; |
| | | 2 | | using System.Text.Json.Serialization; |
| | | 3 | | using Elsa.Common.Entities; |
| | | 4 | | using Elsa.Common.Models; |
| | | 5 | | using Elsa.Extensions; |
| | | 6 | | using Elsa.Workflows; |
| | | 7 | | using Elsa.Workflows.Management; |
| | | 8 | | using Elsa.Workflows.Management.Entities; |
| | | 9 | | using Elsa.Workflows.Management.Filters; |
| | | 10 | | using Elsa.Workflows.Management.Models; |
| | | 11 | | using Elsa.Workflows.Memory; |
| | | 12 | | using Elsa.Workflows.Models; |
| | | 13 | | using JetBrains.Annotations; |
| | | 14 | | using Microsoft.EntityFrameworkCore; |
| | | 15 | | using Microsoft.Extensions.Logging; |
| | | 16 | | using Open.Linq.AsyncExtensions; |
| | | 17 | | |
| | | 18 | | namespace Elsa.Persistence.EFCore.Modules.Management; |
| | | 19 | | |
| | | 20 | | /// <inheritdoc /> |
| | | 21 | | [UsedImplicitly] |
| | 324 | 22 | | public class EFCoreWorkflowDefinitionStore(EntityStore<ManagementElsaDbContext, WorkflowDefinition> store, IPayloadSeria |
| | | 23 | | : IWorkflowDefinitionStore |
| | | 24 | | { |
| | | 25 | | /// <inheritdoc /> |
| | | 26 | | public async Task<WorkflowDefinition?> FindAsync(WorkflowDefinitionFilter filter, CancellationToken cancellationToke |
| | | 27 | | { |
| | 1085 | 28 | | var orderBy = new WorkflowDefinitionOrder<DateTimeOffset>(x => x.CreatedAt, OrderDirection.Ascending); |
| | 1085 | 29 | | return await FindAsync(filter, orderBy, cancellationToken); |
| | 1085 | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <inheritdoc /> |
| | | 33 | | public async Task<WorkflowDefinition?> FindAsync<TOrderBy>(WorkflowDefinitionFilter filter, WorkflowDefinitionOrder< |
| | | 34 | | { |
| | 2182 | 35 | | return await store.QueryAsync(queryable => Filter(queryable, filter).OrderBy(order), OnLoadAsync, filter.TenantA |
| | 1091 | 36 | | } |
| | | 37 | | |
| | | 38 | | /// <inheritdoc /> |
| | | 39 | | public async Task<Page<WorkflowDefinition>> FindManyAsync(WorkflowDefinitionFilter filter, PageArgs pageArgs, Cancel |
| | | 40 | | { |
| | 0 | 41 | | var orderBy = new WorkflowDefinitionOrder<DateTimeOffset>(x => x.CreatedAt, OrderDirection.Ascending); |
| | 0 | 42 | | return await FindManyAsync(filter, orderBy, pageArgs, cancellationToken); |
| | 0 | 43 | | } |
| | | 44 | | |
| | | 45 | | /// <inheritdoc /> |
| | | 46 | | public async Task<Page<WorkflowDefinition>> FindManyAsync<TOrderBy>(WorkflowDefinitionFilter filter, WorkflowDefinit |
| | | 47 | | { |
| | 2 | 48 | | var count = await store.QueryAsync(queryable => Filter(queryable, filter), cancellationToken).LongCount(); |
| | 2 | 49 | | var results = await store.QueryAsync(queryable => Filter(queryable, filter).OrderBy(order).Paginate(pageArgs), O |
| | 1 | 50 | | return new(results, count); |
| | 1 | 51 | | } |
| | | 52 | | |
| | | 53 | | /// <inheritdoc /> |
| | | 54 | | public async Task<IEnumerable<WorkflowDefinition>> FindManyAsync(WorkflowDefinitionFilter filter, CancellationToken |
| | | 55 | | { |
| | 1152 | 56 | | var orderBy = new WorkflowDefinitionOrder<DateTimeOffset>(x => x.CreatedAt, OrderDirection.Ascending); |
| | 1152 | 57 | | return await FindManyAsync(filter, orderBy, cancellationToken); |
| | 1152 | 58 | | } |
| | | 59 | | |
| | | 60 | | /// <inheritdoc /> |
| | | 61 | | public async Task<IEnumerable<WorkflowDefinition>> FindManyAsync<TOrderBy>(WorkflowDefinitionFilter filter, Workflow |
| | | 62 | | { |
| | 2304 | 63 | | return await store.QueryAsync(queryable => Filter(queryable, filter).OrderBy(order), OnLoadAsync, filter.TenantA |
| | 1152 | 64 | | } |
| | | 65 | | |
| | | 66 | | /// <inheritdoc /> |
| | | 67 | | [RequiresUnreferencedCode("The method 'FindSummariesAsync' is used for serialization and requires unreferenced code |
| | | 68 | | public async Task<Page<WorkflowDefinitionSummary>> FindSummariesAsync(WorkflowDefinitionFilter filter, PageArgs page |
| | | 69 | | { |
| | 0 | 70 | | var orderBy = new WorkflowDefinitionOrder<DateTimeOffset>(x => x.CreatedAt, OrderDirection.Ascending); |
| | 0 | 71 | | return await FindSummariesAsync(filter, orderBy, pageArgs, cancellationToken); |
| | 0 | 72 | | } |
| | | 73 | | |
| | | 74 | | /// <inheritdoc /> |
| | | 75 | | [RequiresUnreferencedCode("The method 'FindSummariesAsync' is used for serialization and requires unreferenced code |
| | | 76 | | public async Task<Page<WorkflowDefinitionSummary>> FindSummariesAsync<TOrderBy>(WorkflowDefinitionFilter filter, Wor |
| | | 77 | | { |
| | 0 | 78 | | await using var dbContext = await store.CreateDbContextAsync(cancellationToken); |
| | 0 | 79 | | var set = dbContext.WorkflowDefinitions.AsNoTracking(); |
| | 0 | 80 | | var queryable = Filter(set.AsQueryable(), filter).OrderBy(order); |
| | | 81 | | |
| | 0 | 82 | | if (filter.TenantAgnostic) |
| | 0 | 83 | | queryable = queryable.IgnoreQueryFilters(); |
| | | 84 | | |
| | 0 | 85 | | var count = await queryable.LongCountAsync(cancellationToken); |
| | 0 | 86 | | queryable = Paginate(queryable, pageArgs); |
| | 0 | 87 | | var results = await queryable.Select(WorkflowDefinitionSummary.FromDefinitionExpression()).ToListAsync(cancellat |
| | 0 | 88 | | return Page.Of(results, count); |
| | 0 | 89 | | } |
| | | 90 | | |
| | | 91 | | /// <inheritdoc /> |
| | | 92 | | [RequiresUnreferencedCode("The method 'FindSummariesAsync' is used for serialization and requires unreferenced code |
| | | 93 | | public async Task<IEnumerable<WorkflowDefinitionSummary>> FindSummariesAsync(WorkflowDefinitionFilter filter, Cancel |
| | | 94 | | { |
| | 2 | 95 | | var orderBy = new WorkflowDefinitionOrder<DateTimeOffset>(x => x.CreatedAt, OrderDirection.Ascending); |
| | 2 | 96 | | return await FindSummariesAsync(filter, orderBy, cancellationToken); |
| | 2 | 97 | | } |
| | | 98 | | |
| | | 99 | | /// <inheritdoc /> |
| | | 100 | | [RequiresUnreferencedCode("The method 'FindSummariesAsync' is used for serialization and requires unreferenced code |
| | | 101 | | public async Task<IEnumerable<WorkflowDefinitionSummary>> FindSummariesAsync<TOrderBy>(WorkflowDefinitionFilter filt |
| | | 102 | | { |
| | 2 | 103 | | await using var dbContext = await store.CreateDbContextAsync(cancellationToken); |
| | 2 | 104 | | var set = dbContext.WorkflowDefinitions.AsNoTracking(); |
| | 2 | 105 | | var queryable = Filter(set.AsQueryable(), filter).OrderBy(order); |
| | | 106 | | |
| | 2 | 107 | | if (filter.TenantAgnostic) |
| | 0 | 108 | | queryable = queryable.IgnoreQueryFilters(); |
| | | 109 | | |
| | 2 | 110 | | return await queryable.Select(WorkflowDefinitionSummary.FromDefinitionExpression()).ToListAsync(cancellationToke |
| | 2 | 111 | | } |
| | | 112 | | |
| | | 113 | | /// <inheritdoc /> |
| | | 114 | | public async Task<WorkflowDefinition?> FindLastVersionAsync(WorkflowDefinitionFilter filter, CancellationToken cance |
| | | 115 | | { |
| | 14 | 116 | | return await store.QueryAsync(queryable => Filter(queryable, filter).OrderByDescending(x => x.Version), OnLoadAs |
| | 7 | 117 | | } |
| | | 118 | | |
| | | 119 | | /// <inheritdoc /> |
| | | 120 | | public async Task SaveAsync(WorkflowDefinition definition, CancellationToken cancellationToken = default) |
| | | 121 | | { |
| | 11 | 122 | | await store.SaveAsync(definition, OnSaveAsync, cancellationToken); |
| | 11 | 123 | | } |
| | | 124 | | |
| | | 125 | | /// <inheritdoc /> |
| | | 126 | | public async Task SaveManyAsync(IEnumerable<WorkflowDefinition> definitions, CancellationToken cancellationToken = d |
| | | 127 | | { |
| | 574 | 128 | | await store.SaveManyAsync(definitions, OnSaveAsync, cancellationToken); |
| | 574 | 129 | | } |
| | | 130 | | |
| | | 131 | | /// <inheritdoc /> |
| | | 132 | | public async Task<long> DeleteAsync(WorkflowDefinitionFilter filter, CancellationToken cancellationToken = default) |
| | | 133 | | { |
| | 4 | 134 | | await using var dbContext = await store.CreateDbContextAsync(cancellationToken); |
| | 4 | 135 | | var set = dbContext.WorkflowDefinitions; |
| | 4 | 136 | | var queryable = set.AsQueryable(); |
| | | 137 | | |
| | 4 | 138 | | if (filter.TenantAgnostic) |
| | 0 | 139 | | queryable = queryable.IgnoreQueryFilters(); |
| | | 140 | | |
| | 4 | 141 | | var ids = await Filter(queryable, filter).Select(x => x.Id).Distinct().ToListAsync(cancellationToken); |
| | 4 | 142 | | return await store.DeleteWhereAsync(x => ids.Contains(x.Id), cancellationToken); |
| | 4 | 143 | | } |
| | | 144 | | |
| | | 145 | | /// <inheritdoc /> |
| | | 146 | | public async Task<bool> AnyAsync(WorkflowDefinitionFilter filter, CancellationToken cancellationToken = default) |
| | | 147 | | { |
| | 0 | 148 | | return await store.QueryAsync(queryable => Filter(queryable, filter), filter.TenantAgnostic, cancellationToken). |
| | 0 | 149 | | } |
| | | 150 | | |
| | | 151 | | /// <inheritdoc /> |
| | | 152 | | public async Task<long> CountDistinctAsync(CancellationToken cancellationToken = default) |
| | | 153 | | { |
| | 0 | 154 | | return await store.CountAsync(x => true, x => x.DefinitionId, false, cancellationToken); |
| | 0 | 155 | | } |
| | | 156 | | |
| | | 157 | | /// <inheritdoc /> |
| | | 158 | | public async Task<bool> GetIsNameUnique(string name, string? definitionId = null, CancellationToken cancellationToke |
| | | 159 | | { |
| | 0 | 160 | | var exists = await store.AnyAsync(x => x.Name == name && x.DefinitionId != definitionId, false, cancellationToke |
| | 0 | 161 | | return !exists; |
| | 0 | 162 | | } |
| | | 163 | | |
| | | 164 | | private ValueTask OnSaveAsync(ManagementElsaDbContext managementElsaDbContext, WorkflowDefinition entity, Cancellati |
| | | 165 | | { |
| | 588 | 166 | | var data = new WorkflowDefinitionState(entity.Options, entity.Variables, entity.Inputs, entity.Outputs, entity.O |
| | 588 | 167 | | var json = payloadSerializer.Serialize(data); |
| | | 168 | | |
| | 588 | 169 | | managementElsaDbContext.Entry(entity).Property("Data").CurrentValue = json; |
| | 588 | 170 | | managementElsaDbContext.Entry(entity).Property("UsableAsActivity").CurrentValue = data.Options.UsableAsActivity; |
| | 588 | 171 | | return ValueTask.CompletedTask; |
| | | 172 | | } |
| | | 173 | | |
| | | 174 | | private ValueTask OnLoadAsync(ManagementElsaDbContext managementElsaDbContext, WorkflowDefinition? entity, Cancellat |
| | | 175 | | { |
| | 2115 | 176 | | if (entity == null) |
| | 0 | 177 | | return ValueTask.CompletedTask; |
| | | 178 | | |
| | 2115 | 179 | | var data = new WorkflowDefinitionState(entity.Options, entity.Variables, entity.Inputs, entity.Outputs, entity.O |
| | 2115 | 180 | | var json = (string?)managementElsaDbContext.Entry(entity).Property("Data").CurrentValue; |
| | | 181 | | |
| | | 182 | | try |
| | | 183 | | { |
| | 2115 | 184 | | if (!string.IsNullOrWhiteSpace(json)) |
| | 2115 | 185 | | data = payloadSerializer.Deserialize<WorkflowDefinitionState>(json); |
| | 2115 | 186 | | } |
| | 0 | 187 | | catch (Exception exp) |
| | | 188 | | { |
| | 0 | 189 | | logger.LogError(exp, "Could not deserialize workflow definition state: {DefinitionId}. Reverting to default |
| | 0 | 190 | | } |
| | | 191 | | |
| | 2115 | 192 | | entity.Options = data.Options; |
| | 2115 | 193 | | entity.Variables = data.Variables; |
| | 2115 | 194 | | entity.Inputs = data.Inputs; |
| | 2115 | 195 | | entity.Outputs = data.Outputs; |
| | 2115 | 196 | | entity.Outcomes = data.Outcomes; |
| | 2115 | 197 | | entity.CustomProperties = data.CustomProperties; |
| | | 198 | | |
| | 2115 | 199 | | return ValueTask.CompletedTask; |
| | | 200 | | } |
| | | 201 | | |
| | | 202 | | private IQueryable<WorkflowDefinition> Filter(IQueryable<WorkflowDefinition> queryable, WorkflowDefinitionFilter fil |
| | | 203 | | { |
| | 2258 | 204 | | var definitionId = filter.DefinitionId ?? filter.DefinitionHandle?.DefinitionId; |
| | 2258 | 205 | | var versionOptions = filter.VersionOptions ?? filter.DefinitionHandle?.VersionOptions; |
| | 2258 | 206 | | var id = filter.Id ?? filter.DefinitionHandle?.DefinitionVersionId; |
| | | 207 | | |
| | 4177 | 208 | | if (definitionId != null) queryable = queryable.Where(x => x.DefinitionId == definitionId); |
| | 2264 | 209 | | if (filter.DefinitionIds != null) queryable = queryable.Where(x => filter.DefinitionIds.Contains(x.DefinitionId) |
| | 2718 | 210 | | if (id != null) queryable = queryable.Where(x => x.Id == id); |
| | 2258 | 211 | | if (filter.Ids != null) queryable = queryable.Where(x => filter.Ids.Contains(x.Id)); |
| | 4042 | 212 | | if (versionOptions != null) queryable = queryable.WithVersion(versionOptions.Value); |
| | 2258 | 213 | | if (filter.MaterializerName != null) queryable = queryable.Where(x => x.MaterializerName == filter.MaterializerN |
| | 2258 | 214 | | if (filter.Name != null) queryable = queryable.Where(x => x.Name == filter.Name); |
| | 2258 | 215 | | if (filter.Names != null) queryable = queryable.Where(x => filter.Names.Contains(x.Name!)); |
| | 2269 | 216 | | if (filter.UsableAsActivity != null) queryable = queryable.Where(x => EF.Property<bool>(x, "UsableAsActivity") = |
| | 2258 | 217 | | if (!string.IsNullOrWhiteSpace(filter.SearchTerm)) queryable = queryable.Where(x => x.Name!.ToLower().Contains(f |
| | | 218 | | |
| | | 219 | | // TEMP: IsSystem may be null when upgrading from older versions of Elsa to 3.2. See issue #5366. |
| | | 220 | | // In a future version, we should remove this check and simply do queryable.Where(x => x.IsSystem == filter.IsSy |
| | 2258 | 221 | | if (filter.IsSystem != null) |
| | 0 | 222 | | queryable = filter.IsSystem == true |
| | 0 | 223 | | ? queryable.Where(x => x.IsSystem == true) |
| | 0 | 224 | | #pragma warning disable CS0472 // The result of the expression is always the same since a value of this type is never eq |
| | 0 | 225 | | : queryable.Where(x => x.IsSystem == false || x.IsSystem == null!); |
| | | 226 | | #pragma warning restore CS0472 // The result of the expression is always the same since a value of this type is never eq |
| | | 227 | | |
| | 2264 | 228 | | if (filter.IsReadonly != null) queryable = queryable.Where(x => x.IsReadonly == filter.IsReadonly); |
| | 2258 | 229 | | return queryable; |
| | | 230 | | } |
| | | 231 | | |
| | | 232 | | private IQueryable<WorkflowDefinition> Paginate(IQueryable<WorkflowDefinition> queryable, PageArgs? pageArgs) |
| | | 233 | | { |
| | 0 | 234 | | if (pageArgs?.Offset != null) queryable = queryable.Skip(pageArgs.Offset.Value); |
| | 0 | 235 | | if (pageArgs?.Limit != null) queryable = queryable.Take(pageArgs.Limit.Value); |
| | 0 | 236 | | return queryable; |
| | | 237 | | } |
| | | 238 | | |
| | | 239 | | private class WorkflowDefinitionState |
| | | 240 | | { |
| | | 241 | | [JsonConstructor] |
| | 2115 | 242 | | public WorkflowDefinitionState() |
| | | 243 | | { |
| | 2115 | 244 | | } |
| | | 245 | | |
| | 2703 | 246 | | public WorkflowDefinitionState( |
| | 2703 | 247 | | WorkflowOptions options, |
| | 2703 | 248 | | ICollection<Variable> variables, |
| | 2703 | 249 | | ICollection<InputDefinition> inputs, |
| | 2703 | 250 | | ICollection<OutputDefinition> outputs, |
| | 2703 | 251 | | ICollection<string> outcomes, |
| | 2703 | 252 | | IDictionary<string, object> customProperties |
| | 2703 | 253 | | ) |
| | | 254 | | { |
| | 2703 | 255 | | Options = options; |
| | 2703 | 256 | | Variables = variables; |
| | 2703 | 257 | | Inputs = inputs; |
| | 2703 | 258 | | Outputs = outputs; |
| | 2703 | 259 | | Outcomes = outcomes; |
| | 2703 | 260 | | CustomProperties = customProperties; |
| | 2703 | 261 | | } |
| | | 262 | | |
| | 12927 | 263 | | public WorkflowOptions Options { get; set; } = new(); |
| | 12339 | 264 | | public ICollection<Variable> Variables { get; set; } = new List<Variable>(); |
| | 12339 | 265 | | public ICollection<InputDefinition> Inputs { get; set; } = new List<InputDefinition>(); |
| | 12339 | 266 | | public ICollection<OutputDefinition> Outputs { get; set; } = new List<OutputDefinition>(); |
| | 12339 | 267 | | public ICollection<string> Outcomes { get; set; } = new List<string>(); |
| | 12339 | 268 | | public IDictionary<string, object> CustomProperties { get; set; } = new Dictionary<string, object>(); |
| | | 269 | | } |
| | | 270 | | } |