| | | 1 | | using Bpmn.Interchange; |
| | | 2 | | using Elsa.Abstractions; |
| | | 3 | | using Elsa.Bpmn.Interchange.Exceptions; |
| | | 4 | | using Elsa.Bpmn.Interchange.Services; |
| | | 5 | | using Elsa.Common.Models; |
| | | 6 | | using Elsa.Workflows.Management; |
| | | 7 | | using Elsa.Workflows.Models; |
| | | 8 | | using JetBrains.Annotations; |
| | | 9 | | using Microsoft.AspNetCore.Http; |
| | | 10 | | |
| | | 11 | | namespace Elsa.Bpmn.Interchange.Endpoints.Bpmn.Export; |
| | | 12 | | |
| | | 13 | | /// <summary>Writes a stored workflow definition back out as BPMN 2.0 XML.</summary> |
| | | 14 | | /// <remarks> |
| | | 15 | | /// A thin wrapper over <see cref="BpmnInterchangeDocumentService.Export(Elsa.Workflows.Management.Entities.WorkflowDefi |
| | | 16 | | /// See that type's remarks for why this re-reads the original document rather than reconstructing one from the Elsa |
| | | 17 | | /// activity graph the definition runs, and for why a missing or stale source is refused rather than exported anyway. |
| | | 18 | | /// </remarks> |
| | | 19 | | [UsedImplicitly] |
| | 15 | 20 | | internal sealed class Export(IWorkflowDefinitionStore store, BpmnInterchangeDocumentService documentService) : ElsaEndpo |
| | | 21 | | { |
| | | 22 | | /// <inheritdoc /> |
| | | 23 | | public override void Configure() |
| | | 24 | | { |
| | 11 | 25 | | Get("bpmn/definitions/{definitionId}/export"); |
| | 11 | 26 | | ConfigurePermissions("read:workflow-definitions"); |
| | 11 | 27 | | } |
| | | 28 | | |
| | | 29 | | /// <inheritdoc /> |
| | | 30 | | public override async Task HandleAsync(Request request, CancellationToken cancellationToken) |
| | | 31 | | { |
| | | 32 | | VersionOptions versionOptions; |
| | | 33 | | |
| | 4 | 34 | | if (string.IsNullOrWhiteSpace(request.VersionOptions)) |
| | | 35 | | { |
| | 3 | 36 | | versionOptions = VersionOptions.Latest; |
| | | 37 | | } |
| | | 38 | | else |
| | | 39 | | { |
| | | 40 | | try |
| | | 41 | | { |
| | | 42 | | // VersionOptions.TryParse always returns true and still throws through FromString for a value it |
| | | 43 | | // cannot make sense of, so it buys nothing over calling FromString directly — catch what it can |
| | | 44 | | // actually throw instead: a non-numeric or out-of-range value that is not one of its named options. |
| | 1 | 45 | | versionOptions = VersionOptions.FromString(request.VersionOptions); |
| | 0 | 46 | | } |
| | 1 | 47 | | catch (Exception exception) when (exception is FormatException or OverflowException) |
| | | 48 | | { |
| | 1 | 49 | | AddError( |
| | 1 | 50 | | $"'{request.VersionOptions}' is not a valid version. Specify a whole number, or one of: AllVersions, |
| | 1 | 51 | | + "LatestOrPublished, LatestAndPublished."); |
| | 1 | 52 | | await Send.ErrorsAsync(StatusCodes.Status400BadRequest, cancellationToken); |
| | 1 | 53 | | return; |
| | | 54 | | } |
| | | 55 | | } |
| | | 56 | | |
| | 3 | 57 | | var filter = WorkflowDefinitionHandle.ByDefinitionId(request.DefinitionId, versionOptions).ToFilter(); |
| | 3 | 58 | | var definition = await store.FindAsync(filter, cancellationToken); |
| | | 59 | | |
| | 3 | 60 | | if (definition == null) |
| | | 61 | | { |
| | 1 | 62 | | await Send.NotFoundAsync(cancellationToken); |
| | 1 | 63 | | return; |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | try |
| | | 67 | | { |
| | 2 | 68 | | var bytes = documentService.Export(definition); |
| | 1 | 69 | | await Send.BytesAsync(bytes, $"{request.DefinitionId}.bpmn", "application/xml", cancellation: cancellationTo |
| | 1 | 70 | | } |
| | 1 | 71 | | catch (BpmnExportUnavailableException exception) |
| | | 72 | | { |
| | 1 | 73 | | AddError(exception.Message); |
| | 1 | 74 | | await Send.ErrorsAsync(StatusCodes.Status422UnprocessableEntity, cancellationToken); |
| | 1 | 75 | | } |
| | 0 | 76 | | catch (BpmnInterchangeException exception) |
| | | 77 | | { |
| | 0 | 78 | | AddError(exception.Message); |
| | 0 | 79 | | await Send.ErrorsAsync(StatusCodes.Status400BadRequest, cancellationToken); |
| | | 80 | | } |
| | 4 | 81 | | } |
| | | 82 | | } |