| | | 1 | | using Elsa.Workflows.Activities; |
| | | 2 | | using Elsa.Workflows.Management.Entities; |
| | | 3 | | using Elsa.Workflows.Management.Materializers; |
| | | 4 | | using Elsa.Workflows.Management.Models; |
| | | 5 | | using Elsa.Workflows.Models; |
| | | 6 | | |
| | | 7 | | namespace Elsa.Workflows.Management.Mappers; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Maps <see cref="WorkflowDefinition"/> to and from <see cref="Workflow"/> and <see cref="WorkflowDefinitionModel"/>. |
| | | 11 | | /// </summary> |
| | | 12 | | public class WorkflowDefinitionMapper |
| | | 13 | | { |
| | | 14 | | private readonly IActivitySerializer _activitySerializer; |
| | | 15 | | private readonly IWorkflowDefinitionService _workflowDefinitionService; |
| | | 16 | | private readonly VariableDefinitionMapper _variableDefinitionMapper; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Initializes a new instance of the <see cref="WorkflowDefinitionMapper"/> class. |
| | | 20 | | /// </summary> |
| | 267 | 21 | | public WorkflowDefinitionMapper(IActivitySerializer activitySerializer, IWorkflowDefinitionService workflowDefinitio |
| | | 22 | | { |
| | 267 | 23 | | _activitySerializer = activitySerializer; |
| | 267 | 24 | | _workflowDefinitionService = workflowDefinitionService; |
| | 267 | 25 | | _variableDefinitionMapper = variableDefinitionMapper; |
| | 267 | 26 | | } |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Maps a <see cref="WorkflowDefinition"/> to a <see cref="Workflow"/>. |
| | | 30 | | /// </summary> |
| | | 31 | | /// <param name="source">The source <see cref="WorkflowDefinition"/>.</param> |
| | | 32 | | /// <returns>The mapped <see cref="Workflow"/>.</returns> |
| | | 33 | | public Workflow Map(WorkflowDefinition source) |
| | | 34 | | { |
| | | 35 | | // NEW WAY: If OriginalSource is present, deserialize the full WorkflowDefinitionModel |
| | | 36 | | // This enables symmetric round-trip without re-serialization |
| | 901 | 37 | | if (!string.IsNullOrEmpty(source.OriginalSource)) |
| | | 38 | | { |
| | 893 | 39 | | var model = _activitySerializer.Deserialize<WorkflowDefinitionModel>(source.OriginalSource); |
| | 893 | 40 | | return MapFromModelWithIdentity(model, source); |
| | | 41 | | } |
| | | 42 | | |
| | | 43 | | // OLD WAY: Deserialize from StringData (backwards compatibility) |
| | 8 | 44 | | var root = _activitySerializer.Deserialize(source.StringData!); |
| | | 45 | | |
| | 8 | 46 | | return new( |
| | 8 | 47 | | new(source.DefinitionId, source.Version, source.Id, source.TenantId), |
| | 8 | 48 | | new(source.IsLatest, source.IsPublished), |
| | 8 | 49 | | new(source.Name, source.Description, source.CreatedAt, source.ToolVersion), |
| | 8 | 50 | | source.Options, |
| | 8 | 51 | | root, |
| | 8 | 52 | | source.Variables, |
| | 8 | 53 | | source.Inputs, |
| | 8 | 54 | | source.Outputs, |
| | 8 | 55 | | source.Outcomes, |
| | 8 | 56 | | source.CustomProperties, |
| | 8 | 57 | | source.IsReadonly, |
| | 8 | 58 | | source.IsSystem); |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | /// <summary> |
| | | 62 | | /// Maps a <see cref="WorkflowDefinitionModel"/> to a <see cref="Workflow"/>, using identity from stored definition. |
| | | 63 | | /// </summary> |
| | | 64 | | private Workflow MapFromModelWithIdentity(WorkflowDefinitionModel model, WorkflowDefinition storedDefinition) |
| | | 65 | | { |
| | 893 | 66 | | var root = model.Root!; |
| | 893 | 67 | | var variables = _variableDefinitionMapper.Map(model.Variables).ToList(); |
| | 893 | 68 | | var options = model.Options ?? new WorkflowOptions(); |
| | | 69 | | |
| | 893 | 70 | | return new( |
| | 893 | 71 | | new(storedDefinition.DefinitionId, storedDefinition.Version, storedDefinition.Id, storedDefinition.TenantId) |
| | 893 | 72 | | new(storedDefinition.IsLatest, storedDefinition.IsPublished), |
| | 893 | 73 | | new(model.Name, model.Description, model.CreatedAt, model.ToolVersion), |
| | 893 | 74 | | options, |
| | 893 | 75 | | root, |
| | 893 | 76 | | variables, |
| | 893 | 77 | | model.Inputs ?? new List<InputDefinition>(), |
| | 893 | 78 | | model.Outputs ?? new List<OutputDefinition>(), |
| | 893 | 79 | | model.Outcomes ?? new List<string>(), |
| | 893 | 80 | | model.CustomProperties ?? new Dictionary<string, object>(), |
| | 893 | 81 | | storedDefinition.IsReadonly, |
| | 893 | 82 | | storedDefinition.IsSystem); |
| | | 83 | | } |
| | | 84 | | |
| | | 85 | | /// <summary> |
| | | 86 | | /// Maps a <see cref="WorkflowDefinitionModel"/> to a <see cref="Workflow"/>. |
| | | 87 | | /// </summary> |
| | | 88 | | /// <param name="source">The source <see cref="WorkflowDefinitionModel"/>.</param> |
| | | 89 | | /// <returns>The mapped <see cref="Workflow"/>.</returns> |
| | | 90 | | public Workflow Map(WorkflowDefinitionModel source) |
| | | 91 | | { |
| | 168 | 92 | | var root = source.Root!; |
| | 168 | 93 | | var variables = _variableDefinitionMapper.Map(source.Variables).ToList(); |
| | 168 | 94 | | var options = source.Options ?? new WorkflowOptions(); |
| | | 95 | | |
| | | 96 | | // TODO: Remove this in the future when users have migrated workflows to use the new UsableAsActivity options pr |
| | | 97 | | |
| | | 98 | | #pragma warning disable CS0618 |
| | 168 | 99 | | options.UsableAsActivity ??= source.UsableAsActivity ?? false; |
| | | 100 | | #pragma warning restore CS0618 |
| | | 101 | | |
| | 168 | 102 | | return new( |
| | 168 | 103 | | new(source.DefinitionId, source.Version, source.Id, source.TenantId), |
| | 168 | 104 | | new(source.IsLatest, source.IsPublished), |
| | 168 | 105 | | new(source.Name, source.Description, source.CreatedAt, source.ToolVersion), |
| | 168 | 106 | | options, |
| | 168 | 107 | | root, |
| | 168 | 108 | | variables, |
| | 168 | 109 | | source.Inputs ?? new List<InputDefinition>(), |
| | 168 | 110 | | source.Outputs ?? new List<OutputDefinition>(), |
| | 168 | 111 | | source.Outcomes ?? new List<string>(), |
| | 168 | 112 | | source.CustomProperties ?? new Dictionary<string, object>(), |
| | 168 | 113 | | source.IsReadonly, |
| | 168 | 114 | | source.IsSystem); |
| | | 115 | | } |
| | | 116 | | |
| | | 117 | | public WorkflowDefinition MapToWorkflowDefinition(WorkflowDefinitionModel source) |
| | | 118 | | { |
| | 0 | 119 | | var root = source.Root!; |
| | 0 | 120 | | var variables = _variableDefinitionMapper.Map(source.Variables).ToList(); |
| | 0 | 121 | | var options = source.Options ?? new WorkflowOptions(); |
| | 0 | 122 | | var stringData = _activitySerializer.Serialize(root); |
| | | 123 | | |
| | 0 | 124 | | return new() |
| | 0 | 125 | | { |
| | 0 | 126 | | IsPublished = source.IsPublished, |
| | 0 | 127 | | Description = source.Description, |
| | 0 | 128 | | Id = source.Id, |
| | 0 | 129 | | Inputs = source.Inputs ?? [], |
| | 0 | 130 | | Name = source.Name, |
| | 0 | 131 | | Options = options, |
| | 0 | 132 | | Outcomes = source.Outcomes ?? [], |
| | 0 | 133 | | Outputs = source.Outputs ?? [], |
| | 0 | 134 | | Variables = variables, |
| | 0 | 135 | | Version = source.Version, |
| | 0 | 136 | | CreatedAt = source.CreatedAt, |
| | 0 | 137 | | CustomProperties = source.CustomProperties ?? new Dictionary<string, object>(), |
| | 0 | 138 | | DefinitionId = source.DefinitionId, |
| | 0 | 139 | | IsLatest = source.IsLatest, |
| | 0 | 140 | | IsReadonly = source.IsReadonly, |
| | 0 | 141 | | IsSystem = source.IsSystem, |
| | 0 | 142 | | TenantId = source.TenantId, |
| | 0 | 143 | | ToolVersion = source.ToolVersion, |
| | 0 | 144 | | StringData = stringData, |
| | 0 | 145 | | MaterializerName = JsonWorkflowMaterializer.MaterializerName |
| | 0 | 146 | | }; |
| | | 147 | | } |
| | | 148 | | |
| | | 149 | | /// <summary> |
| | | 150 | | /// Maps many <see cref="WorkflowDefinition"/>s to many <see cref="WorkflowDefinitionModel"/>s. |
| | | 151 | | /// </summary> |
| | | 152 | | /// <param name="source">The source <see cref="WorkflowDefinition"/>s.</param> |
| | | 153 | | /// <param name="cancellationToken">An optional cancellation token.</param> |
| | | 154 | | /// <returns>The mapped <see cref="WorkflowDefinitionModel"/>s.</returns> |
| | | 155 | | public async Task<IEnumerable<WorkflowDefinitionModel>> MapAsync(IEnumerable<WorkflowDefinition> source, Cancellatio |
| | | 156 | | { |
| | 0 | 157 | | return await Task.WhenAll(source.Select(async x => await MapAsync(x, cancellationToken))); |
| | 0 | 158 | | } |
| | | 159 | | |
| | | 160 | | /// <summary> |
| | | 161 | | /// Maps a <see cref="WorkflowDefinition"/> to a <see cref="Workflow"/>. |
| | | 162 | | /// </summary> |
| | | 163 | | /// <param name="workflowDefinition">The source <see cref="WorkflowDefinition"/>.</param> |
| | | 164 | | /// <param name="cancellationToken">The cancellation token.</param> |
| | | 165 | | /// <returns>The mapped <see cref="Workflow"/>.</returns> |
| | | 166 | | public async Task<WorkflowDefinitionModel> MapAsync(WorkflowDefinition workflowDefinition, CancellationToken cancell |
| | | 167 | | { |
| | 1 | 168 | | var workflowGraph = await _workflowDefinitionService.MaterializeWorkflowAsync(workflowDefinition, cancellationTo |
| | 1 | 169 | | var workflow = workflowGraph.Workflow; |
| | 1 | 170 | | var variables = _variableDefinitionMapper.Map(workflow.Variables).ToList(); |
| | | 171 | | |
| | 1 | 172 | | return new( |
| | 1 | 173 | | workflowDefinition.Id, |
| | 1 | 174 | | workflowDefinition.DefinitionId, |
| | 1 | 175 | | workflowDefinition.TenantId, |
| | 1 | 176 | | workflowDefinition.Name, |
| | 1 | 177 | | workflowDefinition.Description, |
| | 1 | 178 | | workflowDefinition.CreatedAt, |
| | 1 | 179 | | workflowDefinition.Version, |
| | 1 | 180 | | workflowDefinition.ToolVersion, |
| | 1 | 181 | | variables, |
| | 1 | 182 | | workflowDefinition.Inputs, |
| | 1 | 183 | | workflowDefinition.Outputs, |
| | 1 | 184 | | workflowDefinition.Outcomes, |
| | 1 | 185 | | workflowDefinition.CustomProperties, |
| | 1 | 186 | | workflowDefinition.IsReadonly, |
| | 1 | 187 | | workflowDefinition.IsSystem, |
| | 1 | 188 | | workflowDefinition.IsLatest, |
| | 1 | 189 | | workflowDefinition.IsPublished, |
| | 1 | 190 | | workflow.Options, |
| | 1 | 191 | | null, |
| | 1 | 192 | | workflow.Root); |
| | 1 | 193 | | } |
| | | 194 | | |
| | | 195 | | /// <summary> |
| | | 196 | | /// Maps a <see cref="Workflow"/> to a <see cref="WorkflowDefinitionModel"/>. |
| | | 197 | | /// </summary> |
| | | 198 | | /// <param name="workflow">The source <see cref="WorkflowDefinition"/>.</param> |
| | | 199 | | /// <returns>The mapped <see cref="WorkflowDefinitionModel"/>.</returns> |
| | | 200 | | public WorkflowDefinitionModel Map(Workflow workflow) |
| | | 201 | | { |
| | 0 | 202 | | var variables = _variableDefinitionMapper.Map(workflow.Variables).ToList(); |
| | | 203 | | |
| | 0 | 204 | | return new( |
| | 0 | 205 | | workflow.Identity.Id, |
| | 0 | 206 | | workflow.Identity.DefinitionId, |
| | 0 | 207 | | workflow.Identity.TenantId, |
| | 0 | 208 | | workflow.WorkflowMetadata.Name, |
| | 0 | 209 | | workflow.WorkflowMetadata.Description, |
| | 0 | 210 | | workflow.WorkflowMetadata.CreatedAt, |
| | 0 | 211 | | workflow.Identity.Version, |
| | 0 | 212 | | workflow.WorkflowMetadata.ToolVersion, |
| | 0 | 213 | | variables, |
| | 0 | 214 | | workflow.Inputs, |
| | 0 | 215 | | workflow.Outputs, |
| | 0 | 216 | | workflow.Outcomes, |
| | 0 | 217 | | workflow.CustomProperties, |
| | 0 | 218 | | workflow.IsReadonly, |
| | 0 | 219 | | workflow.IsSystem, |
| | 0 | 220 | | workflow.Publication.IsLatest, |
| | 0 | 221 | | workflow.Publication.IsPublished, |
| | 0 | 222 | | workflow.Options, |
| | 0 | 223 | | null, |
| | 0 | 224 | | workflow.Root); |
| | | 225 | | } |
| | | 226 | | } |