| | | 1 | | using Elsa.Authorization; |
| | | 2 | | using Bpmn.Semantics; |
| | | 3 | | using Elsa.Abstractions; |
| | | 4 | | using Elsa.Bpmn.Interchange.Endpoints.Bpmn; |
| | | 5 | | using Elsa.Bpmn.Interchange.Services; |
| | | 6 | | using JetBrains.Annotations; |
| | | 7 | | using Microsoft.AspNetCore.Http; |
| | | 8 | | |
| | | 9 | | namespace Elsa.Bpmn.Interchange.Endpoints.Bpmn.Import; |
| | | 10 | | |
| | | 11 | | /// <summary> |
| | | 12 | | /// Reads a <c>.bpmn</c> document and persists it as a workflow definition whose root is the <see cref="Elsa.Bpmn.Activi |
| | | 13 | | /// scope <c>BpmnWorkBinder</c> builds for it. |
| | | 14 | | /// </summary> |
| | | 15 | | /// <remarks> |
| | | 16 | | /// A thin wrapper over <see cref="BpmnInterchangeDocumentService.ImportAsync"/>. Its remarks explain why capability |
| | | 17 | | /// refusal is surfaced here rather than left to first execution, and why nothing beyond the source XML is persisted |
| | | 18 | | /// alongside the bound activity graph. |
| | | 19 | | /// </remarks> |
| | | 20 | | [UsedImplicitly] |
| | 80 | 21 | | internal sealed class Import(BpmnInterchangeDocumentService documentService) : ElsaEndpoint<Request> |
| | | 22 | | { |
| | | 23 | | /// <inheritdoc /> |
| | | 24 | | public override void Configure() |
| | | 25 | | { |
| | 46 | 26 | | Post("bpmn/import"); |
| | 46 | 27 | | AllowFileUploads(); |
| | 46 | 28 | | RequirePermission(Elsa.Bpmn.Interchange.Permissions.BpmnPermissions.Definitions, CoreVerbs.Write); |
| | 46 | 29 | | } |
| | | 30 | | |
| | | 31 | | /// <inheritdoc /> |
| | | 32 | | public override async Task HandleAsync(Request request, CancellationToken cancellationToken) |
| | | 33 | | { |
| | 34 | 34 | | if (Files.Count != 1) |
| | | 35 | | { |
| | 0 | 36 | | AddError("Upload exactly one .bpmn file."); |
| | 0 | 37 | | await Send.ErrorsAsync(StatusCodes.Status400BadRequest, cancellationToken); |
| | 0 | 38 | | return; |
| | | 39 | | } |
| | | 40 | | |
| | 34 | 41 | | var xml = await BpmnUploadedFileReader.ReadTextAsync(Files[0], cancellationToken); |
| | | 42 | | |
| | 34 | 43 | | var result = await BpmnImportErrorResponses.RunAsync( |
| | 34 | 44 | | () => documentService.ImportAsync(xml, request.DefinitionId, request.Name, request.ProcessId, cancellationTo |
| | 34 | 45 | | HttpContext.Response, |
| | 0 | 46 | | message => AddError(message), |
| | 34 | 47 | | Send.ErrorsAsync, |
| | 34 | 48 | | cancellationToken); |
| | | 49 | | |
| | 34 | 50 | | if (result is null) |
| | 4 | 51 | | return; |
| | | 52 | | |
| | 30 | 53 | | var definition = result.ImportResult.WorkflowDefinition; |
| | | 54 | | |
| | 30 | 55 | | await Send.OkAsync(new Response |
| | 30 | 56 | | { |
| | 30 | 57 | | Id = definition.Id, |
| | 30 | 58 | | DefinitionId = definition.DefinitionId, |
| | 30 | 59 | | Version = definition.Version, |
| | 30 | 60 | | Analysis = BpmnImportAnalysisModel.From(result.Analysis) |
| | 30 | 61 | | }, cancellationToken); |
| | 34 | 62 | | } |
| | | 63 | | } |