Search in sources :

Example 1 with SiriusWebJSONResourceFactoryImpl

use of org.eclipse.sirius.components.emf.services.SiriusWebJSONResourceFactoryImpl in project sirius-web by eclipse-sirius.

the class EditingContextEPackageService method loadDomainDefinitions.

private void loadDomainDefinitions(ResourceSet resourceSet, DocumentEntity domainDocument) {
    URI uri = URI.createURI(domainDocument.getId().toString());
    JsonResource resource = new SiriusWebJSONResourceFactoryImpl().createResource(uri);
    try (var inputStream = new ByteArrayInputStream(domainDocument.getContent().getBytes())) {
        resourceSet.getResources().add(resource);
        resource.load(inputStream, null);
    } catch (IOException | IllegalArgumentException exception) {
        this.logger.warn(exception.getMessage(), exception);
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) SiriusWebJSONResourceFactoryImpl(org.eclipse.sirius.components.emf.services.SiriusWebJSONResourceFactoryImpl) JsonResource(org.eclipse.sirius.emfjson.resource.JsonResource) IOException(java.io.IOException) URI(org.eclipse.emf.common.util.URI)

Example 2 with SiriusWebJSONResourceFactoryImpl

use of org.eclipse.sirius.components.emf.services.SiriusWebJSONResourceFactoryImpl in project sirius-web by eclipse-sirius.

the class EditingContextSearchService method findById.

@Override
public Optional<IEditingContext> findById(String editingContextId) {
    long start = System.currentTimeMillis();
    // $NON-NLS-1$
    this.logger.debug("Loading the editing context {}", editingContextId);
    AdapterFactoryEditingDomain editingDomain = new AdapterFactoryEditingDomain(this.composedAdapterFactory, new BasicCommandStack());
    ResourceSet resourceSet = editingDomain.getResourceSet();
    resourceSet.eAdapters().add(new ECrossReferenceAdapter());
    EPackageRegistryImpl ePackageRegistry = new EPackageRegistryImpl();
    this.globalEPackageRegistry.forEach(ePackageRegistry::put);
    List<EPackage> additionalEPackages = this.editingContextEPackageService.getEPackages(editingContextId);
    additionalEPackages.forEach(ePackage -> ePackageRegistry.put(ePackage.getNsURI(), ePackage));
    resourceSet.setPackageRegistry(ePackageRegistry);
    List<DocumentEntity> documentEntities = new IDParser().parse(editingContextId).map(this.documentRepository::findAllByProjectId).orElseGet(List::of);
    for (DocumentEntity documentEntity : documentEntities) {
        URI uri = URI.createURI(documentEntity.getId().toString());
        JsonResource resource = new SiriusWebJSONResourceFactoryImpl().createResource(uri);
        try (var inputStream = new ByteArrayInputStream(documentEntity.getContent().getBytes())) {
            resourceSet.getResources().add(resource);
            resource.load(inputStream, null);
            resource.eAdapters().add(new DocumentMetadataAdapter(documentEntity.getName()));
        } catch (IOException | IllegalArgumentException exception) {
            // $NON-NLS-1$
            this.logger.warn("An error occured while loading document {}: {}.", documentEntity.getId(), exception.getMessage());
            resourceSet.getResources().remove(resource);
        }
    }
    // $NON-NLS-1$
    this.logger.debug("{} documents loaded for the editing context {}", resourceSet.getResources().size(), editingContextId);
    long end = System.currentTimeMillis();
    this.timer.record(end - start, TimeUnit.MILLISECONDS);
    return Optional.of(new EditingContext(editingContextId, editingDomain));
}
Also used : AdapterFactoryEditingDomain(org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain) DocumentMetadataAdapter(org.eclipse.sirius.web.services.documents.DocumentMetadataAdapter) JsonResource(org.eclipse.sirius.emfjson.resource.JsonResource) BasicCommandStack(org.eclipse.emf.common.command.BasicCommandStack) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) IOException(java.io.IOException) URI(org.eclipse.emf.common.util.URI) EPackage(org.eclipse.emf.ecore.EPackage) ECrossReferenceAdapter(org.eclipse.emf.ecore.util.ECrossReferenceAdapter) DocumentEntity(org.eclipse.sirius.web.persistence.entities.DocumentEntity) IEditingContext(org.eclipse.sirius.components.core.api.IEditingContext) EditingContext(org.eclipse.sirius.components.emf.services.EditingContext) EPackageRegistryImpl(org.eclipse.emf.ecore.impl.EPackageRegistryImpl) ByteArrayInputStream(java.io.ByteArrayInputStream) IDParser(org.eclipse.sirius.web.services.api.id.IDParser) SiriusWebJSONResourceFactoryImpl(org.eclipse.sirius.components.emf.services.SiriusWebJSONResourceFactoryImpl) List(java.util.List)

Example 3 with SiriusWebJSONResourceFactoryImpl

use of org.eclipse.sirius.components.emf.services.SiriusWebJSONResourceFactoryImpl in project sirius-web by eclipse-sirius.

the class RenameDocumentEventHandlerTests method testRenameDocument.

@Test
public void testRenameDocument() {
    IDocumentService noOpDocumentService = new IDocumentService.NoOp() {

        @Override
        public Optional<Document> rename(UUID documentId, String newName) {
            // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            return Optional.of(new Document(documentId, new Project(UUID.randomUUID(), "", new Profile(UUID.randomUUID(), "username"), Visibility.PUBLIC), newName, "noContent"));
        }
    };
    RenameDocumentEventHandler handler = new RenameDocumentEventHandler(noOpDocumentService, new NoOpServicesMessageService(), new SimpleMeterRegistry());
    UUID documentId = UUID.randomUUID();
    IInput input = new RenameDocumentInput(UUID.randomUUID(), documentId, NEW_NAME);
    AdapterFactoryEditingDomain editingDomain = new EditingDomainFactory().create();
    DocumentMetadataAdapter adapter = new DocumentMetadataAdapter(OLD_NAME);
    Resource resource = new SiriusWebJSONResourceFactoryImpl().createResource(URI.createURI(documentId.toString()));
    resource.eAdapters().add(adapter);
    editingDomain.getResourceSet().getResources().add(resource);
    assertThat(editingDomain.getResourceSet().getResources().size()).isEqualTo(1);
    assertThat(adapter.getName()).isEqualTo(OLD_NAME);
    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);
    assertThat(editingDomain.getResourceSet().getResources().size()).isEqualTo(1);
    assertThat(adapter.getName()).isEqualTo(NEW_NAME);
    ChangeDescription changeDescription = changeDescriptionSink.asFlux().blockFirst();
    assertThat(changeDescription.getKind()).isEqualTo(ChangeKind.SEMANTIC_CHANGE);
    IPayload payload = payloadSink.asMono().block();
    assertThat(payload).isInstanceOf(RenameDocumentSuccessPayload.class);
}
Also used : AdapterFactoryEditingDomain(org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain) NoOpServicesMessageService(org.eclipse.sirius.web.services.projects.NoOpServicesMessageService) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) RenameDocumentInput(org.eclipse.sirius.components.collaborative.dto.RenameDocumentInput) Resource(org.eclipse.emf.ecore.resource.Resource) IInput(org.eclipse.sirius.components.core.api.IInput) 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) 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) SiriusWebJSONResourceFactoryImpl(org.eclipse.sirius.components.emf.services.SiriusWebJSONResourceFactoryImpl) IDocumentService(org.eclipse.sirius.web.services.api.document.IDocumentService) UUID(java.util.UUID) Test(org.junit.jupiter.api.Test)

Example 4 with SiriusWebJSONResourceFactoryImpl

use of org.eclipse.sirius.components.emf.services.SiriusWebJSONResourceFactoryImpl in project sirius-web by eclipse-sirius.

the class StereotypeBuilder method getStereotypeBody.

public String getStereotypeBody(EObject rootEObject) {
    // $NON-NLS-1$
    JsonResource resource = new SiriusWebJSONResourceFactoryImpl().createResource(URI.createURI("inmemory"));
    if (rootEObject != null) {
        resource.getContents().add(rootEObject);
    }
    // $NON-NLS-1$
    String content = "";
    try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
        Map<String, Object> options = new HashMap<>();
        options.put(JsonResource.OPTION_ENCODING, JsonResource.ENCODING_UTF_8);
        options.put(JsonResource.OPTION_SCHEMA_LOCATION, Boolean.TRUE);
        resource.save(outputStream, options);
        content = outputStream.toString();
    } catch (IOException exception) {
        this.logger.error(exception.getMessage(), exception);
    }
    return content;
}
Also used : HashMap(java.util.HashMap) SiriusWebJSONResourceFactoryImpl(org.eclipse.sirius.components.emf.services.SiriusWebJSONResourceFactoryImpl) JsonResource(org.eclipse.sirius.emfjson.resource.JsonResource) EObject(org.eclipse.emf.ecore.EObject) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 5 with SiriusWebJSONResourceFactoryImpl

use of org.eclipse.sirius.components.emf.services.SiriusWebJSONResourceFactoryImpl 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

SiriusWebJSONResourceFactoryImpl (org.eclipse.sirius.components.emf.services.SiriusWebJSONResourceFactoryImpl)15 JsonResource (org.eclipse.sirius.emfjson.resource.JsonResource)13 IOException (java.io.IOException)11 ByteArrayInputStream (java.io.ByteArrayInputStream)8 URI (org.eclipse.emf.common.util.URI)8 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)7 AdapterFactoryEditingDomain (org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain)6 EditingContext (org.eclipse.sirius.components.emf.services.EditingContext)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 HashMap (java.util.HashMap)5 Resource (org.eclipse.emf.ecore.resource.Resource)5 ResourceSetImpl (org.eclipse.emf.ecore.resource.impl.ResourceSetImpl)5 IEditingContext (org.eclipse.sirius.components.core.api.IEditingContext)5 Document (org.eclipse.sirius.web.services.api.document.Document)5 ChangeDescription (org.eclipse.sirius.components.collaborative.api.ChangeDescription)4 IPayload (org.eclipse.sirius.components.core.api.IPayload)4 SimpleMeterRegistry (io.micrometer.core.instrument.simple.SimpleMeterRegistry)3 UUID (java.util.UUID)3 EObject (org.eclipse.emf.ecore.EObject)3 EPackageRegistryImpl (org.eclipse.emf.ecore.impl.EPackageRegistryImpl)3