| | | 1 | | using Bpmn.Interchange; |
| | | 2 | | using Bpmn.Semantics; |
| | | 3 | | using Elsa.Abstractions; |
| | | 4 | | using Elsa.Bpmn.Interchange.Exceptions; |
| | | 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] |
| | 14 | 21 | | internal sealed class Import(BpmnInterchangeDocumentService documentService) : ElsaEndpoint<Request> |
| | | 22 | | { |
| | | 23 | | /// <inheritdoc /> |
| | | 24 | | public override void Configure() |
| | | 25 | | { |
| | 11 | 26 | | Post("bpmn/import"); |
| | 11 | 27 | | AllowFileUploads(); |
| | 11 | 28 | | ConfigurePermissions("write:workflow-definitions"); |
| | 11 | 29 | | } |
| | | 30 | | |
| | | 31 | | /// <inheritdoc /> |
| | | 32 | | public override async Task HandleAsync(Request request, CancellationToken cancellationToken) |
| | | 33 | | { |
| | 3 | 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 | | |
| | 3 | 41 | | var xml = await BpmnUploadedFileReader.ReadTextAsync(Files[0], cancellationToken); |
| | | 42 | | |
| | | 43 | | BpmnDocumentImportResult result; |
| | | 44 | | |
| | | 45 | | try |
| | | 46 | | { |
| | 3 | 47 | | result = await documentService.ImportAsync(xml, request.DefinitionId, request.Name, request.ProcessId, cance |
| | 2 | 48 | | } |
| | 0 | 49 | | catch (BpmnInterchangeException exception) |
| | | 50 | | { |
| | 0 | 51 | | AddError(exception.Message); |
| | 0 | 52 | | await Send.ErrorsAsync(StatusCodes.Status400BadRequest, cancellationToken); |
| | 0 | 53 | | return; |
| | | 54 | | } |
| | 1 | 55 | | catch (BpmnBindingException exception) |
| | | 56 | | { |
| | 1 | 57 | | AddError(exception.Message); |
| | 1 | 58 | | await Send.ErrorsAsync(StatusCodes.Status422UnprocessableEntity, cancellationToken); |
| | 1 | 59 | | return; |
| | | 60 | | } |
| | 0 | 61 | | catch (BpmnCapabilityException exception) |
| | | 62 | | { |
| | 0 | 63 | | AddCapabilityErrors(exception); |
| | 0 | 64 | | await Send.ErrorsAsync(StatusCodes.Status422UnprocessableEntity, cancellationToken); |
| | 0 | 65 | | return; |
| | | 66 | | } |
| | | 67 | | |
| | 2 | 68 | | if (!result.ImportResult.Succeeded) |
| | | 69 | | { |
| | 0 | 70 | | foreach (var validationError in result.ImportResult.ValidationErrors) |
| | 0 | 71 | | AddError(validationError.Message); |
| | | 72 | | |
| | 0 | 73 | | await Send.ErrorsAsync(StatusCodes.Status400BadRequest, cancellationToken); |
| | 0 | 74 | | return; |
| | | 75 | | } |
| | | 76 | | |
| | 2 | 77 | | var definition = result.ImportResult.WorkflowDefinition; |
| | | 78 | | |
| | 2 | 79 | | await Send.OkAsync(new Response |
| | 2 | 80 | | { |
| | 2 | 81 | | Id = definition.Id, |
| | 2 | 82 | | DefinitionId = definition.DefinitionId, |
| | 2 | 83 | | Version = definition.Version, |
| | 2 | 84 | | Analysis = BpmnImportAnalysisModel.From(result.Analysis) |
| | 2 | 85 | | }, cancellationToken); |
| | 3 | 86 | | } |
| | | 87 | | |
| | | 88 | | /// <remarks> |
| | | 89 | | /// <see cref="BpmnCapabilityException"/> carries <see cref="BpmnCapabilityException.DrivingElementIds"/> as a |
| | | 90 | | /// single flat list, already unioned across every missing capability — it does not say which element drove which |
| | | 91 | | /// capability (unlike <c>BpmnCapabilityRequirements.DrivingElementIds</c>, which is per-capability, but that type |
| | | 92 | | /// is gone by the time this catch clause sees the exception). Attributing the full, unioned list to each |
| | | 93 | | /// capability individually would put elements next to a capability they may have nothing to do with, so this |
| | | 94 | | /// reports the missing capabilities together with the combined element list once, rather than repeating it. |
| | | 95 | | /// </remarks> |
| | | 96 | | private void AddCapabilityErrors(BpmnCapabilityException exception) |
| | | 97 | | { |
| | 0 | 98 | | var missingCapabilities = string.Join(", ", BpmnInterchangeDocumentService.IndividualCapabilities.Where(capabili |
| | 0 | 99 | | var elementIds = string.Join(", ", exception.DrivingElementIds); |
| | | 100 | | |
| | 0 | 101 | | AddError( |
| | 0 | 102 | | $"This deployment does not declare the following BPMN host capabilities the document requires: {missingCapab |
| | 0 | 103 | | + $"Offending elements (combined across all missing capabilities above, not attributable to any one of them) |
| | 0 | 104 | | } |
| | | 105 | | } |