Search in sources :

Example 1 with CreateDocumentSuccessPayload

use of org.eclipse.sirius.components.collaborative.dto.CreateDocumentSuccessPayload in project sirius-web by eclipse-sirius.

the class CreateDocumentEventHandler method createDocument.

private void createDocument(One<IPayload> payloadSink, Many<ChangeDescription> changeDescriptionSink, IInput input, IEditingContext editingContext, String editingContextId, String name, StereotypeDescription stereotypeDescription) {
    IPayload payload = new ErrorPayload(input.getId(), this.messageService.unexpectedError());
    ChangeDescription changeDescription = new ChangeDescription(ChangeKind.NOTHING, editingContext.getId(), input);
    // @formatter:off
    Optional<AdapterFactoryEditingDomain> optionalEditingDomain = Optional.of(editingContext).filter(EditingContext.class::isInstance).map(EditingContext.class::cast).map(EditingContext::getDomain);
    if (optionalEditingDomain.isPresent()) {
        AdapterFactoryEditingDomain adapterFactoryEditingDomain = optionalEditingDomain.get();
        ResourceSet resourceSet = adapterFactoryEditingDomain.getResourceSet();
        var optionalDocument = this.documentService.createDocument(editingContextId, name, stereotypeDescription.getContent());
        if (optionalDocument.isPresent()) {
            Document document = optionalDocument.get();
            URI uri = URI.createURI(document.getId().toString());
            JsonResource resource = new SiriusWebJSONResourceFactoryImpl().createResource(uri);
            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 CreateDocumentSuccessPayload(input.getId());
            changeDescription = new ChangeDescription(ChangeKind.SEMANTIC_CHANGE, editingContext.getId(), input);
        }
    }
    payloadSink.tryEmitValue(payload);
    changeDescriptionSink.tryEmitNext(changeDescription);
}
Also used : AdapterFactoryEditingDomain(org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain) JsonResource(org.eclipse.sirius.emfjson.resource.JsonResource) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) IOException(java.io.IOException) CreateDocumentSuccessPayload(org.eclipse.sirius.components.collaborative.dto.CreateDocumentSuccessPayload) 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) 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) SiriusWebJSONResourceFactoryImpl(org.eclipse.sirius.components.emf.services.SiriusWebJSONResourceFactoryImpl)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 URI (org.eclipse.emf.common.util.URI)1 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)1 AdapterFactoryEditingDomain (org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain)1 ChangeDescription (org.eclipse.sirius.components.collaborative.api.ChangeDescription)1 CreateDocumentSuccessPayload (org.eclipse.sirius.components.collaborative.dto.CreateDocumentSuccessPayload)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 Document (org.eclipse.sirius.web.services.api.document.Document)1