| | | 1 | | using Elsa.Common.Models; |
| | | 2 | | using Elsa.Workflows.Activities; |
| | | 3 | | using Elsa.Workflows.Management.Mappers; |
| | | 4 | | using Elsa.Workflows.Management.Materializers; |
| | | 5 | | using Elsa.Workflows.Management.Models; |
| | | 6 | | using Elsa.Workflows.Models; |
| | | 7 | | |
| | | 8 | | namespace Elsa.Workflows.Management.Services |
| | | 9 | | { |
| | | 10 | | /// <inheritdoc /> |
| | | 11 | | public class WorkflowDefinitionImporter : IWorkflowDefinitionImporter |
| | | 12 | | { |
| | | 13 | | private readonly IActivitySerializer _serializer; |
| | | 14 | | private readonly IWorkflowDefinitionPublisher _workflowDefinitionPublisher; |
| | | 15 | | private readonly VariableDefinitionMapper _variableDefinitionMapper; |
| | | 16 | | |
| | | 17 | | /// <summary> |
| | | 18 | | /// Initializes a new instance of the <see cref="WorkflowDefinitionImporter"/> class. |
| | | 19 | | /// </summary> |
| | 5 | 20 | | public WorkflowDefinitionImporter( |
| | 5 | 21 | | IActivitySerializer serializer, |
| | 5 | 22 | | IWorkflowDefinitionPublisher workflowDefinitionPublisher, |
| | 5 | 23 | | VariableDefinitionMapper variableDefinitionMapper) |
| | | 24 | | { |
| | 5 | 25 | | _serializer = serializer; |
| | 5 | 26 | | _workflowDefinitionPublisher = workflowDefinitionPublisher; |
| | 5 | 27 | | _variableDefinitionMapper = variableDefinitionMapper; |
| | 5 | 28 | | } |
| | | 29 | | |
| | | 30 | | /// <inheritdoc /> |
| | | 31 | | public async Task<ImportWorkflowResult> ImportAsync(SaveWorkflowDefinitionRequest request, CancellationToken can |
| | | 32 | | { |
| | 4 | 33 | | var model = request.Model; |
| | 4 | 34 | | var definitionId = model.DefinitionId; |
| | | 35 | | |
| | | 36 | | // Get a workflow draft version. |
| | 4 | 37 | | var draft = !string.IsNullOrWhiteSpace(definitionId) |
| | 4 | 38 | | ? await _workflowDefinitionPublisher.GetDraftAsync(definitionId, VersionOptions.Latest, cancellationToke |
| | 4 | 39 | | : null; |
| | | 40 | | |
| | 4 | 41 | | var isNew = draft == null; |
| | | 42 | | |
| | | 43 | | // Create a new workflow in case no existing definition was found. |
| | 4 | 44 | | if (isNew) |
| | | 45 | | { |
| | 4 | 46 | | draft = await _workflowDefinitionPublisher.NewAsync(cancellationToken: cancellationToken); |
| | | 47 | | |
| | 4 | 48 | | if (!string.IsNullOrWhiteSpace(definitionId)) |
| | 4 | 49 | | draft.DefinitionId = definitionId; |
| | | 50 | | |
| | 4 | 51 | | if (!string.IsNullOrWhiteSpace(model.Id)) |
| | 0 | 52 | | draft.Id = model.Id; |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | // Update the draft with the received model. |
| | 4 | 56 | | var root = model.Root ?? new Sequence(); |
| | 4 | 57 | | var stringData = _serializer.Serialize(root); |
| | 4 | 58 | | var variables = _variableDefinitionMapper.Map(model.Variables).ToList(); |
| | | 59 | | |
| | 4 | 60 | | draft!.StringData = stringData; |
| | 4 | 61 | | draft.MaterializerName = JsonWorkflowMaterializer.MaterializerName; |
| | 4 | 62 | | draft.Name = model.Name?.Trim(); |
| | 4 | 63 | | draft.Description = model.Description?.Trim(); |
| | 4 | 64 | | draft.CustomProperties = model.CustomProperties ?? new Dictionary<string, object>(); |
| | 4 | 65 | | draft.Variables = variables; |
| | 4 | 66 | | draft.Inputs = model.Inputs ?? new List<InputDefinition>(); |
| | 4 | 67 | | draft.Outputs = model.Outputs ?? new List<OutputDefinition>(); |
| | 4 | 68 | | draft.Outcomes = model.Outcomes ?? new List<string>(); |
| | 4 | 69 | | draft.IsReadonly = model.IsReadonly; |
| | 4 | 70 | | draft.Options = model.Options ?? new WorkflowOptions(); |
| | 4 | 71 | | draft.ToolVersion = model.ToolVersion; |
| | | 72 | | |
| | 4 | 73 | | if (request.Publish ?? model.IsPublished) |
| | | 74 | | { |
| | 2 | 75 | | var result = await _workflowDefinitionPublisher.PublishAsync(draft, cancellationToken); |
| | 2 | 76 | | return new ImportWorkflowResult(result.Succeeded, draft, result.ValidationErrors); |
| | | 77 | | } |
| | | 78 | | |
| | 2 | 79 | | await _workflowDefinitionPublisher.SaveDraftAsync(draft, cancellationToken); |
| | 2 | 80 | | return new ImportWorkflowResult(true, draft, new List<WorkflowValidationError>()); |
| | 4 | 81 | | } |
| | | 82 | | } |
| | | 83 | | } |