Search in sources :

Example 1 with UploadDocumentSuccessPayload

use of org.eclipse.sirius.web.services.api.document.UploadDocumentSuccessPayload 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 2 with UploadDocumentSuccessPayload

use of org.eclipse.sirius.web.services.api.document.UploadDocumentSuccessPayload 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)

Aggregations

UploadFile (org.eclipse.sirius.components.graphql.api.UploadFile)2 Document (org.eclipse.sirius.web.services.api.document.Document)2 UploadDocumentInput (org.eclipse.sirius.web.services.api.document.UploadDocumentInput)2 UploadDocumentSuccessPayload (org.eclipse.sirius.web.services.api.document.UploadDocumentSuccessPayload)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 Objects (java.util.Objects)1 URI (org.eclipse.emf.common.util.URI)1 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)1 ResourceSetImpl (org.eclipse.emf.ecore.resource.impl.ResourceSetImpl)1 AdapterFactoryEditingDomain (org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain)1 ChangeDescription (org.eclipse.sirius.components.collaborative.api.ChangeDescription)1 ErrorPayload (org.eclipse.sirius.components.core.api.ErrorPayload)1 IEditingContext (org.eclipse.sirius.components.core.api.IEditingContext)1 IPayload (org.eclipse.sirius.components.core.api.IPayload)1 EditingContext (org.eclipse.sirius.components.emf.services.EditingContext)1 SiriusWebJSONResourceFactoryImpl (org.eclipse.sirius.components.emf.services.SiriusWebJSONResourceFactoryImpl)1 JsonResource (org.eclipse.sirius.emfjson.resource.JsonResource)1