Skip to content

Commit

Permalink
[4050] Give a chance to report an error on document upload
Browse files Browse the repository at this point in the history
Bug: eclipse-sirius#4050
Signed-off-by: Arthur Daussy <arthur.daussy@obeo.fr>
Signed-off-by: Florian ROUËNÉ <florian.rouene@obeosoft.com>
  • Loading branch information
adaussy authored and frouene committed Oct 1, 2024
1 parent 065fa30 commit cfceaaa
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 29 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@ Both `IInput` and `IDomainEvent` implement `ICause` and will thus be used to ind
Previously, the value false was equivalent to NEVER, and true to IF-CHILDREN.
The new option ALWAYS allows the separator to be displayed in every case.
- https://github.com/eclipse-sirius/sirius-web/issues/3678[#3678] [core] Remove `IRepresentationMetadataSearchService#findByRepresentation`, use `IRepresentationMetadataSearchService#findByRepresentationId` instead.
- https://github.com/eclipse-sirius/sirius-web/issues/4050[#4050] [sirius-web] `IUploadFileLoader` must now return an `IResult`.
This allows an error to be displayed when there is a problem during uploading


=== Dependency update

Expand All @@ -48,6 +51,7 @@ The new option ALWAYS allows the separator to be displayed in every case.
- https://github.com/eclipse-sirius/sirius-web/issues/4008[#4008] [diagram] Prevent `detailsEvent` to be triggered twice when selecting a multi-represented element in the diagram
- https://github.com/eclipse-sirius/sirius-web/issues/4032[#4032] [diagram] Fix an issue where separator list compartment is missing
- https://github.com/eclipse-sirius/sirius-web/issues/4028[#4028] [trees] Fixed an issue where special characters (e.g. #) could not be used in the treeId due to URL parsing limitations.
- https://github.com/eclipse-sirius/sirius-web/issues/4050[#4050] [sirius-web] Display error message on failed upload document action

=== New Features

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
*******************************************************************************/
package org.eclipse.sirius.web.application.document.services;

import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
Expand All @@ -39,6 +38,8 @@
import org.eclipse.sirius.web.application.document.services.api.IUploadDocumentReportProvider;
import org.eclipse.sirius.web.application.document.services.api.IUploadFileLoader;
import org.eclipse.sirius.web.application.views.explorer.services.ExplorerDescriptionProvider;
import org.eclipse.sirius.web.domain.services.Failure;
import org.eclipse.sirius.web.domain.services.Success;
import org.eclipse.sirius.web.domain.services.api.IMessageService;
import org.springframework.stereotype.Service;

Expand All @@ -64,7 +65,9 @@ public class UploadDocumentEventHandler implements IEditingContextEventHandler {

private final Counter counter;

public UploadDocumentEventHandler(IEditingContextSearchService editingContextSearchService, List<IUploadDocumentReportProvider> uploadDocumentReportProviders, IMessageService messageService, IUploadFileLoader uploadDocumentLoader, MeterRegistry meterRegistry) {

public UploadDocumentEventHandler(IEditingContextSearchService editingContextSearchService, List<IUploadDocumentReportProvider> uploadDocumentReportProviders, IMessageService messageService,
IUploadFileLoader uploadDocumentLoader, MeterRegistry meterRegistry) {
this.editingContextSearchService = Objects.requireNonNull(editingContextSearchService);
this.uploadDocumentReportProviders = Objects.requireNonNull(uploadDocumentReportProviders);
this.messageService = Objects.requireNonNull(messageService);
Expand All @@ -90,26 +93,29 @@ public void handle(Sinks.One<IPayload> payloadSink, Sinks.Many<ChangeDescription
if (input instanceof UploadDocumentInput uploadDocumentInput && editingContext instanceof IEMFEditingContext emfEditingContext && optionalResourceSet.isPresent()) {
var resourceSet = optionalResourceSet.get();

var newResource = this.uploadDocumentLoader.load(resourceSet, emfEditingContext, uploadDocumentInput.file());

var optionalId = newResource.map(Resource::getURI)
.map(uri -> uri.path().substring(1))
.flatMap(new UUIDParser()::parse);

var optionalName = newResource.map(Resource::eAdapters).stream()
.flatMap(Collection::stream)
.filter(ResourceMetadataAdapter.class::isInstance)
.map(ResourceMetadataAdapter.class::cast)
.findFirst()
.map(ResourceMetadataAdapter::getName);
if (newResource.isPresent() && optionalId.isPresent() && optionalName.isPresent()) {
var uploadedResource = newResource.get();
var id = optionalId.get();
var name = optionalName.get();

String report = this.getReport(uploadedResource);
payload = new UploadDocumentSuccessPayload(input.id(), new DocumentDTO(id, name, ExplorerDescriptionProvider.DOCUMENT_KIND), report);
changeDescription = new ChangeDescription(ChangeKind.SEMANTIC_CHANGE, editingContext.getId(), input);
var result = this.uploadDocumentLoader.load(resourceSet, emfEditingContext, uploadDocumentInput.file());
if (result instanceof Success<Resource> success) {
var newResource = success.data();

var optionalId = new UUIDParser().parse(newResource.getURI().path().substring(1));

var optionalName = newResource.eAdapters().stream()
.filter(ResourceMetadataAdapter.class::isInstance)
.map(ResourceMetadataAdapter.class::cast)
.findFirst()
.map(ResourceMetadataAdapter::getName);

if (optionalId.isPresent() && optionalName.isPresent()) {
var id = optionalId.get();
var name = optionalName.get();

String report = this.getReport(newResource);
payload = new UploadDocumentSuccessPayload(input.id(), new DocumentDTO(id, name, ExplorerDescriptionProvider.DOCUMENT_KIND), report);
changeDescription = new ChangeDescription(ChangeKind.SEMANTIC_CHANGE, editingContext.getId(), input);
}

} else if (result instanceof Failure<Resource> failure) {
payload = new ErrorPayload(input.id(), failure.message());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@
import org.eclipse.sirius.web.application.document.services.api.IUploadFileLoader;
import org.eclipse.sirius.web.application.editingcontext.services.api.IEditingContextMigrationParticipantPredicate;
import org.eclipse.sirius.web.application.editingcontext.services.api.IResourceLoader;
import org.eclipse.sirius.web.domain.services.Failure;
import org.eclipse.sirius.web.domain.services.IResult;
import org.eclipse.sirius.web.domain.services.Success;
import org.eclipse.sirius.web.domain.services.api.IMessageService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
Expand All @@ -43,6 +47,8 @@ public class UploadFileLoader implements IUploadFileLoader {

private final Logger logger = LoggerFactory.getLogger(UploadFileLoader.class);

private final IMessageService messageService;

private final IProxyValidator proxyValidator;

private final IResourceLoader resourceLoader;
Expand All @@ -51,15 +57,16 @@ public class UploadFileLoader implements IUploadFileLoader {

private final List<IEditingContextMigrationParticipantPredicate> migrationParticipantPredicates;

public UploadFileLoader(IProxyValidator proxyValidator, IResourceLoader resourceLoader, IDocumentSanitizedJsonContentProvider documentSanitizedJsonContentProvider, List<IEditingContextMigrationParticipantPredicate> migrationParticipantPredicates) {
public UploadFileLoader(IMessageService messageService, IProxyValidator proxyValidator, IResourceLoader resourceLoader, IDocumentSanitizedJsonContentProvider documentSanitizedJsonContentProvider, List<IEditingContextMigrationParticipantPredicate> migrationParticipantPredicates) {
this.messageService = Objects.requireNonNull(messageService);
this.proxyValidator = Objects.requireNonNull(proxyValidator);
this.resourceLoader = Objects.requireNonNull(resourceLoader);
this.documentSanitizedJsonContentProvider = Objects.requireNonNull(documentSanitizedJsonContentProvider);
this.migrationParticipantPredicates = Objects.requireNonNull(migrationParticipantPredicates);
}

@Override
public Optional<Resource> load(ResourceSet resourceSet, IEMFEditingContext emfEditingContext, UploadFile file) {
public IResult<Resource> load(ResourceSet resourceSet, IEMFEditingContext emfEditingContext, UploadFile file) {
var fileName = file.getName();
var applyMigrationParticipants = this.migrationParticipantPredicates.stream().anyMatch(predicate -> predicate.test(emfEditingContext));
var optionalContent = this.getContent(resourceSet, file, applyMigrationParticipants);
Expand All @@ -73,11 +80,14 @@ public Optional<Resource> load(ResourceSet resourceSet, IEMFEditingContext emfEd
if (hasProxies) {
this.logger.warn("The resource {} contains unresolvable proxies and will not be uploaded.", file.getName());
} else {
return this.resourceLoader.toResource(emfEditingContext.getDomain().getResourceSet(), UUID.randomUUID()
var optionalRessource = this.resourceLoader.toResource(emfEditingContext.getDomain().getResourceSet(), UUID.randomUUID()
.toString(), fileName, content, applyMigrationParticipants);
if (optionalRessource.isPresent()) {
return new Success<>(optionalRessource.get());
}
}
}
return Optional.empty();
return new Failure<>(this.messageService.unexpectedError());
}

private Optional<String> getContent(ResourceSet resourceSet, UploadFile file, boolean applyMigrationParticipants) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@
*******************************************************************************/
package org.eclipse.sirius.web.application.document.services.api;

import java.util.Optional;

import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.sirius.components.emf.services.api.IEMFEditingContext;
import org.eclipse.sirius.components.graphql.api.UploadFile;
import org.eclipse.sirius.web.domain.services.IResult;

/**
* Use to load a file receive in an upload document event.
Expand All @@ -26,6 +25,6 @@
*/
public interface IUploadFileLoader {

Optional<Resource> load(ResourceSet resourceSet, IEMFEditingContext emfEditingContext, UploadFile file);
IResult<Resource> load(ResourceSet resourceSet, IEMFEditingContext emfEditingContext, UploadFile file);

}

0 comments on commit cfceaaa

Please sign in to comment.