| | | 1 | | using System.Collections; |
| | | 2 | | using System.Text; |
| | | 3 | | using Elsa.Workflows.Runtime.Entities; |
| | | 4 | | |
| | | 5 | | namespace Elsa.Workflows.Runtime.Filters; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// A filter for bookmarks. |
| | | 9 | | /// </summary> |
| | | 10 | | public class BookmarkFilter |
| | | 11 | | { |
| | | 12 | | // Cache the properties of BookmarkFilter for performance. |
| | 2 | 13 | | private static readonly System.Reflection.PropertyInfo[] CachedProperties = typeof(BookmarkFilter).GetProperties(); |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Gets or sets the ID of the bookmark. |
| | | 17 | | /// </summary> |
| | 3513 | 18 | | public string? BookmarkId { get; set; } |
| | | 19 | | |
| | | 20 | | /// <summary> |
| | | 21 | | /// Gets or sets the IDs of the bookmark. |
| | | 22 | | /// </summary> |
| | 2653 | 23 | | public ICollection<string>? BookmarkIds { get; set; } |
| | | 24 | | |
| | | 25 | | /// <summary> |
| | | 26 | | /// Gets or sets the IDs of the workflow instance. |
| | | 27 | | /// </summary> |
| | 4718 | 28 | | public string? WorkflowInstanceId { get; set; } |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Gets or sets the IDs of the workflow instances. |
| | | 32 | | /// </summary> |
| | 2442 | 33 | | public ICollection<string>? WorkflowInstanceIds { get; set; } |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Gets or sets the hash of the bookmark to find. |
| | | 37 | | /// </summary> |
| | 4617 | 38 | | public string? Hash { get; set; } |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Gets or sets the hashes of the bookmarks to find. |
| | | 42 | | /// </summary> |
| | 2403 | 43 | | public ICollection<string>? Hashes { get; set; } |
| | | 44 | | |
| | | 45 | | /// <summary> |
| | | 46 | | /// Gets or sets the correlation ID of the bookmark to find. |
| | | 47 | | /// </summary> |
| | 3590 | 48 | | public string? CorrelationId { get; set; } |
| | | 49 | | |
| | | 50 | | /// <summary> |
| | | 51 | | /// Gets or sets the name of the bookmark to find. |
| | | 52 | | /// </summary> |
| | 4528 | 53 | | public string? Name { get; set; } |
| | | 54 | | |
| | | 55 | | /// <summary> |
| | | 56 | | /// Gets or sets the names of the bookmarks to find. |
| | | 57 | | /// </summary> |
| | 2407 | 58 | | public ICollection<string>? Names { get; set; } |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// Gets or sets the activity instance ID of the bookmark to find. |
| | | 62 | | /// </summary> |
| | 3509 | 63 | | public string? ActivityInstanceId { get; set; } |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// Get or sets if the triggers to find is a tenant agnostic search |
| | | 67 | | /// </summary> |
| | 2315 | 68 | | public bool TenantAgnostic { get; set; } |
| | | 69 | | |
| | | 70 | | /// <summary> |
| | | 71 | | /// Applies the filter to the specified query. |
| | | 72 | | /// </summary> |
| | | 73 | | public IQueryable<StoredBookmark> Apply(IQueryable<StoredBookmark> query) |
| | | 74 | | { |
| | 1297 | 75 | | var filter = this; |
| | 1300 | 76 | | if (filter.BookmarkId != null) query = query.Where(x => x.Id == filter.BookmarkId); |
| | 1433 | 77 | | if (filter.BookmarkIds != null) query = query.Where(x => filter.BookmarkIds.Contains(x.Id)); |
| | 1375 | 78 | | if (filter.CorrelationId != null) query = query.Where(x => x.CorrelationId == filter.CorrelationId); |
| | 2403 | 79 | | if (filter.Hash != null) query = query.Where(x => x.Hash == filter.Hash); |
| | 1297 | 80 | | if (filter.Hashes != null) query = query.Where(x => filter.Hashes.Contains(x.Hash)); |
| | 2432 | 81 | | if (filter.WorkflowInstanceId != null) query = query.Where(x => x.WorkflowInstanceId == filter.WorkflowInstanceI |
| | 1323 | 82 | | if (filter.WorkflowInstanceIds != null) query = query.Where(x => filter.WorkflowInstanceIds.Contains(x.WorkflowI |
| | 2321 | 83 | | if (filter.Name != null) query = query.Where(x => x.Name == filter.Name); |
| | 1299 | 84 | | if (filter.Names != null) query = query.Where(x => filter.Names.Contains(x.Name!)); |
| | 1297 | 85 | | if (filter.ActivityInstanceId != null) query = query.Where(x => x.ActivityInstanceId == filter.ActivityInstanceI |
| | | 86 | | |
| | 1297 | 87 | | return query; |
| | | 88 | | } |
| | | 89 | | |
| | 1 | 90 | | public static BookmarkFilter ByActivityTypeNames(IEnumerable<string> activityTypeNames) => new() |
| | 1 | 91 | | { |
| | 1 | 92 | | Names = activityTypeNames.ToList() |
| | 1 | 93 | | }; |
| | | 94 | | |
| | | 95 | | public string GetHashableString() |
| | | 96 | | { |
| | | 97 | | // Return a hashable string representation of the filter, excluding null values. |
| | 1106 | 98 | | var sb = new StringBuilder(); |
| | 26544 | 99 | | foreach (var prop in CachedProperties) |
| | | 100 | | { |
| | 12166 | 101 | | var value = prop.GetValue(this); |
| | 12166 | 102 | | if (value == null) |
| | | 103 | | continue; |
| | | 104 | | |
| | | 105 | | string valueString; |
| | | 106 | | // Handle collections (excluding string) |
| | 4339 | 107 | | if (value is IEnumerable enumerable and not string) |
| | | 108 | | { |
| | 0 | 109 | | var items = new List<string>(); |
| | 0 | 110 | | foreach (var item in enumerable) |
| | | 111 | | { |
| | 0 | 112 | | if (item != null) |
| | 0 | 113 | | items.Add(item.ToString()!); |
| | | 114 | | } |
| | 0 | 115 | | items.Sort(StringComparer.Ordinal); |
| | 0 | 116 | | valueString = string.Join(",", items); |
| | | 117 | | } |
| | | 118 | | else |
| | | 119 | | { |
| | 4339 | 120 | | var toStringResult = value.ToString(); |
| | 4339 | 121 | | if (toStringResult == null) |
| | | 122 | | continue; |
| | 4339 | 123 | | valueString = toStringResult; |
| | | 124 | | } |
| | 4339 | 125 | | sb.Append($"{prop.Name}:{valueString};"); |
| | | 126 | | } |
| | | 127 | | |
| | 1106 | 128 | | return sb.ToString(); |
| | | 129 | | } |
| | | 130 | | } |