Search in sources :

Example 1 with UploadDocumentInput

use of org.eclipse.sirius.web.services.api.document.UploadDocumentInput in project sirius-web by eclipse-sirius.

the class MutationUploadDocumentDataFetcher method get.

@Override
public CompletableFuture<IPayload> get(DataFetchingEnvironment environment) throws Exception {
    Map<Object, Object> inputArgument = environment.getArgument(MutationTypeProvider.INPUT_ARGUMENT);
    // We cannot use directly UploadDocumentInput, the objectMapper cannot handle the file stream.
    // @formatter:off
    UUID id = Optional.of(inputArgument.get(ID)).filter(String.class::isInstance).map(String.class::cast).flatMap(new IDParser()::parse).orElse(null);
    String editingContextId = Optional.of(inputArgument.get(EDITING_CONTEXT_ID)).filter(String.class::isInstance).map(String.class::cast).orElse(null);
    UploadFile file = Optional.of(inputArgument.get(FILE)).filter(UploadFile.class::isInstance).map(UploadFile.class::cast).orElse(null);
    // @formatter:on
    UploadDocumentInput input = new UploadDocumentInput(id, editingContextId, file);
    // @formatter:off
    return this.editingContextEventProcessorRegistry.dispatchEvent(input.getEditingContextId(), input).defaultIfEmpty(new ErrorPayload(input.getId(), this.messageService.unexpectedError())).toFuture();
// @formatter:on
}
Also used : UploadDocumentInput(org.eclipse.sirius.web.services.api.document.UploadDocumentInput) ErrorPayload(org.eclipse.sirius.components.core.api.ErrorPayload) UploadFile(org.eclipse.sirius.components.graphql.api.UploadFile) IDParser(org.eclipse.sirius.web.services.api.id.IDParser) UUID(java.util.UUID)

Example 2 with UploadDocumentInput

use of org.eclipse.sirius.web.services.api.document.UploadDocumentInput in project sirius-web by eclipse-sirius.

the class UploadDocumentEventHandler method handle.

@Override
public void handle(One<IPayload> payloadSink, Many<ChangeDescription> changeDescriptionSink, IEditingContext editingContext, IInput input) {
    this.counter.increment();
    IPayload payload = new ErrorPayload(input.getId(), this.messageService.unexpectedError());
    ChangeDescription changeDescription = new ChangeDescription(ChangeKind.NOTHING, editingContext.getId(), input);
    if (input instanceof UploadDocumentInput) {
        UploadDocumentInput uploadDocumentInput = (UploadDocumentInput) input;
        String projectId = uploadDocumentInput.getEditingContextId();
        UploadFile file = uploadDocumentInput.getFile();
        // @formatter:off
        Optional<AdapterFactoryEditingDomain> optionalEditingDomain = Optional.of(editingContext).filter(EditingContext.class::isInstance).map(EditingContext.class::cast).map(EditingContext::getDomain);
        // @formatter:on
        String name = file.getName().trim();
        if (optionalEditingDomain.isPresent()) {
            AdapterFactoryEditingDomain adapterFactoryEditingDomain = optionalEditingDomain.get();
            String content = this.getContent(adapterFactoryEditingDomain.getResourceSet().getPackageRegistry(), file);
            var optionalDocument = this.documentService.createDocument(projectId, name, content);
            if (optionalDocument.isPresent()) {
                Document document = optionalDocument.get();
                ResourceSet resourceSet = adapterFactoryEditingDomain.getResourceSet();
                URI uri = URI.createURI(document.getId().toString());
                if (resourceSet.getResource(uri, false) == null) {
                    ResourceSet loadingResourceSet = new ResourceSetImpl();
                    loadingResourceSet.setPackageRegistry(resourceSet.getPackageRegistry());
                    JsonResource resource = new SiriusWebJSONResourceFactoryImpl().createResource(uri);
                    loadingResourceSet.getResources().add(resource);
                    try (var inputStream = new ByteArrayInputStream(document.getContent().getBytes())) {
                        resource.load(inputStream, null);
                    } catch (IOException exception) {
                        this.logger.warn(exception.getMessage(), exception);
                    }
                    resource.eAdapters().add(new DocumentMetadataAdapter(name));
                    resourceSet.getResources().add(resource);
                    payload = new UploadDocumentSuccessPayload(input.getId(), document);
                    changeDescription = new ChangeDescription(ChangeKind.SEMANTIC_CHANGE, editingContext.getId(), input);
                }
            }
        }
    }
    payloadSink.tryEmitValue(payload);
    changeDescriptionSink.tryEmitNext(changeDescription);
}
Also used : AdapterFactoryEditingDomain(org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain) ResourceSetImpl(org.eclipse.emf.ecore.resource.impl.ResourceSetImpl) JsonResource(org.eclipse.sirius.emfjson.resource.JsonResource) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) IOException(java.io.IOException) Document(org.eclipse.sirius.web.services.api.document.Document) URI(org.eclipse.emf.common.util.URI) IPayload(org.eclipse.sirius.components.core.api.IPayload) ErrorPayload(org.eclipse.sirius.components.core.api.ErrorPayload) UploadDocumentInput(org.eclipse.sirius.web.services.api.document.UploadDocumentInput) UploadFile(org.eclipse.sirius.components.graphql.api.UploadFile) IEditingContext(org.eclipse.sirius.components.core.api.IEditingContext) EditingContext(org.eclipse.sirius.components.emf.services.EditingContext) ByteArrayInputStream(java.io.ByteArrayInputStream) ChangeDescription(org.eclipse.sirius.components.collaborative.api.ChangeDescription) UploadDocumentSuccessPayload(org.eclipse.sirius.web.services.api.document.UploadDocumentSuccessPayload) SiriusWebJSONResourceFactoryImpl(org.eclipse.sirius.components.emf.services.SiriusWebJSONResourceFactoryImpl)

Example 3 with UploadDocumentInput

use of org.eclipse.sirius.web.services.api.document.UploadDocumentInput in project sirius-web by eclipse-sirius.

the class ProjectImporter method createDocuments.

/**
 * Creates all documents in the project thanks to the {@link IEditingContextEventProcessor} and the
 * {@link CreateDocumentFromUploadEvent}. If at least one document has not been created it will return
 * <code>false</code>.
 *
 * @param inputId
 *
 * @return <code>true</code> whether all documents has been created, <code>false</code> otherwise
 */
private boolean createDocuments(UUID inputId) {
    for (Entry<String, UploadFile> entry : this.documents.entrySet()) {
        String oldDocumentId = entry.getKey();
        UploadFile uploadFile = entry.getValue();
        UploadDocumentInput input = new UploadDocumentInput(inputId, this.projectId, uploadFile);
        // @formatter:off
        Document document = this.editingContextEventProcessor.handle(input).filter(UploadDocumentSuccessPayload.class::isInstance).map(UploadDocumentSuccessPayload.class::cast).map(UploadDocumentSuccessPayload::getDocument).blockOptional().orElse(null);
        // @formatter:on
        if (document == null) {
            // $NON-NLS-1$
            this.logger.warn("The document {} has not been created", this.projectManifest.getDocumentIdsToName().get(oldDocumentId));
        }
        this.oldDocumentIdToNewDocument.put(oldDocumentId, document);
    }
    return this.oldDocumentIdToNewDocument.values().stream().allMatch(Objects::nonNull);
}
Also used : UploadDocumentInput(org.eclipse.sirius.web.services.api.document.UploadDocumentInput) UploadFile(org.eclipse.sirius.components.graphql.api.UploadFile) UploadDocumentSuccessPayload(org.eclipse.sirius.web.services.api.document.UploadDocumentSuccessPayload) Objects(java.util.Objects) Document(org.eclipse.sirius.web.services.api.document.Document)

Example 4 with UploadDocumentInput

use of org.eclipse.sirius.web.services.api.document.UploadDocumentInput in project sirius-web by eclipse-sirius.

the class UploadDocumentEventHandlerTests method simulatesDocumentUpload.

/**
 * Simulates a document upload with fake services. The uploaded resource will be put in the given editing domain
 * with the documentId as URI.
 *
 * @param editingDomain
 *            The editing domain where the uploaded resource will be put
 * @param documentId
 *            The id of the document to upload
 * @param resourceBytes
 *            The content of the document to upload
 */
private void simulatesDocumentUpload(AdapterFactoryEditingDomain editingDomain, UUID documentId, byte[] resourceBytes) {
    IDocumentService documentService = new IDocumentService.NoOp() {

        @Override
        public Optional<Document> createDocument(String projectId, String name, String content) {
            // $NON-NLS-1$ //$NON-NLS-2$
            return Optional.of(new Document(documentId, new Project(UUID.fromString(projectId), "", new Profile(UUID.randomUUID(), "username"), Visibility.PUBLIC), name, content));
        }
    };
    IServicesMessageService messageService = new NoOpServicesMessageService();
    UploadDocumentEventHandler handler = new UploadDocumentEventHandler(documentService, messageService, new SimpleMeterRegistry());
    UploadFile file = new UploadFile(FILE_NAME, new ByteArrayInputStream(resourceBytes));
    var input = new UploadDocumentInput(UUID.randomUUID(), UUID.randomUUID().toString(), file);
    IEditingContext editingContext = new EditingContext(UUID.randomUUID().toString(), editingDomain);
    Many<ChangeDescription> changeDescriptionSink = Sinks.many().unicast().onBackpressureBuffer();
    One<IPayload> payloadSink = Sinks.one();
    assertThat(handler.canHandle(editingContext, input)).isTrue();
    handler.handle(payloadSink, changeDescriptionSink, editingContext, input);
    ChangeDescription changeDescription = changeDescriptionSink.asFlux().blockFirst();
    assertThat(changeDescription.getKind()).isEqualTo(ChangeKind.SEMANTIC_CHANGE);
    IPayload payload = payloadSink.asMono().block();
    assertThat(payload).isInstanceOf(UploadDocumentSuccessPayload.class);
}
Also used : IServicesMessageService(org.eclipse.sirius.web.services.messages.IServicesMessageService) NoOpServicesMessageService(org.eclipse.sirius.web.services.projects.NoOpServicesMessageService) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) Document(org.eclipse.sirius.web.services.api.document.Document) Profile(org.eclipse.sirius.web.services.api.accounts.Profile) IPayload(org.eclipse.sirius.components.core.api.IPayload) Project(org.eclipse.sirius.web.services.api.projects.Project) UploadDocumentInput(org.eclipse.sirius.web.services.api.document.UploadDocumentInput) UploadFile(org.eclipse.sirius.components.graphql.api.UploadFile) IEditingContext(org.eclipse.sirius.components.core.api.IEditingContext) IEditingContext(org.eclipse.sirius.components.core.api.IEditingContext) EditingContext(org.eclipse.sirius.components.emf.services.EditingContext) ByteArrayInputStream(java.io.ByteArrayInputStream) ChangeDescription(org.eclipse.sirius.components.collaborative.api.ChangeDescription) IDocumentService(org.eclipse.sirius.web.services.api.document.IDocumentService)

Example 5 with UploadDocumentInput

use of org.eclipse.sirius.web.services.api.document.UploadDocumentInput in project sirius-web by eclipse-sirius.

the class UploadDocumentEventHandlerTests method uploadDocument.

private EditingDomain uploadDocument(InputStream inputstream) {
    IDocumentService documentService = new IDocumentService.NoOp() {

        @Override
        public Optional<Document> createDocument(String projectId, String name, String content) {
            // $NON-NLS-1$ //$NON-NLS-2$
            return Optional.of(new Document(UUID.randomUUID(), new Project(UUID.fromString(projectId), "", new Profile(UUID.randomUUID(), "username"), Visibility.PUBLIC), name, content));
        }
    };
    IServicesMessageService messageService = new NoOpServicesMessageService();
    UploadDocumentEventHandler handler = new UploadDocumentEventHandler(documentService, messageService, new SimpleMeterRegistry());
    UploadFile file = new UploadFile(FILE_NAME, inputstream);
    var input = new UploadDocumentInput(UUID.randomUUID(), UUID.randomUUID().toString(), file);
    AdapterFactoryEditingDomain editingDomain = new EditingDomainFactory().create();
    IEditingContext editingContext = new EditingContext(UUID.randomUUID().toString(), editingDomain);
    Many<ChangeDescription> changeDescriptionSink = Sinks.many().unicast().onBackpressureBuffer();
    One<IPayload> payloadSink = Sinks.one();
    assertThat(handler.canHandle(editingContext, input)).isTrue();
    handler.handle(payloadSink, changeDescriptionSink, editingContext, input);
    ChangeDescription changeDescription = changeDescriptionSink.asFlux().blockFirst();
    assertThat(changeDescription.getKind()).isEqualTo(ChangeKind.SEMANTIC_CHANGE);
    IPayload payload = payloadSink.asMono().block();
    assertThat(payload).isInstanceOf(UploadDocumentSuccessPayload.class);
    return editingDomain;
}
Also used : AdapterFactoryEditingDomain(org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain) IServicesMessageService(org.eclipse.sirius.web.services.messages.IServicesMessageService) NoOpServicesMessageService(org.eclipse.sirius.web.services.projects.NoOpServicesMessageService) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) Document(org.eclipse.sirius.web.services.api.document.Document) Profile(org.eclipse.sirius.web.services.api.accounts.Profile) IPayload(org.eclipse.sirius.components.core.api.IPayload) Project(org.eclipse.sirius.web.services.api.projects.Project) UploadDocumentInput(org.eclipse.sirius.web.services.api.document.UploadDocumentInput) UploadFile(org.eclipse.sirius.components.graphql.api.UploadFile) IEditingContext(org.eclipse.sirius.components.core.api.IEditingContext) IEditingContext(org.eclipse.sirius.components.core.api.IEditingContext) EditingContext(org.eclipse.sirius.components.emf.services.EditingContext) ChangeDescription(org.eclipse.sirius.components.collaborative.api.ChangeDescription) IDocumentService(org.eclipse.sirius.web.services.api.document.IDocumentService)

Aggregations

UploadFile (org.eclipse.sirius.components.graphql.api.UploadFile)5 UploadDocumentInput (org.eclipse.sirius.web.services.api.document.UploadDocumentInput)5 Document (org.eclipse.sirius.web.services.api.document.Document)4 ChangeDescription (org.eclipse.sirius.components.collaborative.api.ChangeDescription)3 IEditingContext (org.eclipse.sirius.components.core.api.IEditingContext)3 IPayload (org.eclipse.sirius.components.core.api.IPayload)3 EditingContext (org.eclipse.sirius.components.emf.services.EditingContext)3 SimpleMeterRegistry (io.micrometer.core.instrument.simple.SimpleMeterRegistry)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 AdapterFactoryEditingDomain (org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain)2 ErrorPayload (org.eclipse.sirius.components.core.api.ErrorPayload)2 Profile (org.eclipse.sirius.web.services.api.accounts.Profile)2 IDocumentService (org.eclipse.sirius.web.services.api.document.IDocumentService)2 UploadDocumentSuccessPayload (org.eclipse.sirius.web.services.api.document.UploadDocumentSuccessPayload)2 Project (org.eclipse.sirius.web.services.api.projects.Project)2 IServicesMessageService (org.eclipse.sirius.web.services.messages.IServicesMessageService)2 NoOpServicesMessageService (org.eclipse.sirius.web.services.projects.NoOpServicesMessageService)2 IOException (java.io.IOException)1 Objects (java.util.Objects)1 UUID (java.util.UUID)1