Search in sources :

Example 1 with EObjectIDManager

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

the class EditingContextPersistenceService method save.

private Optional<DocumentEntity> save(Resource resource) {
    Optional<DocumentEntity> result = Optional.empty();
    HashMap<Object, Object> options = new HashMap<>();
    options.put(JsonResource.OPTION_ID_MANAGER, new EObjectIDManager());
    try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
        resource.save(outputStream, options);
        for (Resource.Diagnostic warning : resource.getWarnings()) {
            this.logger.warn(warning.getMessage());
        }
        for (Resource.Diagnostic error : resource.getErrors()) {
            this.logger.warn(error.getMessage());
        }
        byte[] bytes = outputStream.toByteArray();
        String content = new String(bytes);
        // @formatter:off
        result = new IDParser().parse(resource.getURI().toString()).flatMap(this.documentRepository::findById).map(entity -> {
            entity.setContent(content);
            return this.documentRepository.save(entity);
        });
    // @formatter:on
    } catch (IllegalArgumentException | IOException exception) {
        this.logger.warn(exception.getMessage(), exception);
    }
    return result;
}
Also used : DocumentEntity(org.eclipse.sirius.web.persistence.entities.DocumentEntity) IEditingContext(org.eclipse.sirius.components.core.api.IEditingContext) ByteArrayOutputStream(java.io.ByteArrayOutputStream) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) DocumentsModifiedEvent(org.eclipse.sirius.web.services.api.events.DocumentsModifiedEvent) ArrayList(java.util.ArrayList) EditingDomain(org.eclipse.emf.edit.domain.EditingDomain) EditingContext(org.eclipse.sirius.components.emf.services.EditingContext) Document(org.eclipse.sirius.web.services.api.document.Document) Timer(io.micrometer.core.instrument.Timer) IEditingContextPersistenceService(org.eclipse.sirius.components.core.api.IEditingContextPersistenceService) Service(org.springframework.stereotype.Service) DocumentMapper(org.eclipse.sirius.web.services.documents.DocumentMapper) ApplicationEventPublisher(org.springframework.context.ApplicationEventPublisher) EObjectIDManager(org.eclipse.sirius.components.emf.services.EObjectIDManager) Logger(org.slf4j.Logger) IDParser(org.eclipse.sirius.web.services.api.id.IDParser) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) JsonResource(org.eclipse.sirius.emfjson.resource.JsonResource) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) IDocumentRepository(org.eclipse.sirius.web.persistence.repositories.IDocumentRepository) MeterRegistry(io.micrometer.core.instrument.MeterRegistry) Resource(org.eclipse.emf.ecore.resource.Resource) Optional(java.util.Optional) HashMap(java.util.HashMap) JsonResource(org.eclipse.sirius.emfjson.resource.JsonResource) Resource(org.eclipse.emf.ecore.resource.Resource) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) DocumentEntity(org.eclipse.sirius.web.persistence.entities.DocumentEntity) IDParser(org.eclipse.sirius.web.services.api.id.IDParser) EObjectIDManager(org.eclipse.sirius.components.emf.services.EObjectIDManager)

Example 2 with EObjectIDManager

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

the class UploadDocumentEventHandlerTests method getEObjectUriToId.

/**
 * Loads a json resource with the {@link #JSON_CONTENT} to map the uriFragment of eObjects to the id supplied by
 * {@link EObjectIDManager}, then returns the map.
 */
private Map<String, String> getEObjectUriToId(EditingDomain editingDomain, byte[] resourceBytes) {
    Map<String, String> eObjectUriToId = new HashMap<>();
    // $NON-NLS-1$
    JsonResource jsonResource = new SiriusWebJSONResourceFactoryImpl().createResource(URI.createURI("json.flow"));
    editingDomain.getResourceSet().getResources().add(jsonResource);
    Map<String, Object> options = new HashMap<>();
    options.put(JsonResource.OPTION_ID_MANAGER, new EObjectIDManager());
    try {
        // Load the resource to attach the contained element to the resource.
        ByteArrayInputStream inputStream = new ByteArrayInputStream(resourceBytes);
        jsonResource.load(inputStream, options);
        /*
             * Once the resource is loaded we cannot load it anymore, so we use the save to associate, in a map, the uri
             * fragment of eObject to theirs IDs that have been computed by the IDSupplier during the previous load.
             */
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        jsonResource.save(outputStream, Collections.singletonMap(JsonResource.OPTION_ID_MANAGER, new EObjectIDManager() {

            @Override
            public String getOrCreateId(EObject eObject) {
                String id = super.getOrCreateId(eObject);
                String uriFragment = jsonResource.getURIFragment(eObject);
                eObjectUriToId.put(uriFragment, id);
                return id;
            }
        }));
    } catch (IOException exception) {
        fail(exception.getMessage());
    }
    return eObjectUriToId;
}
Also used : HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) EObject(org.eclipse.emf.ecore.EObject) 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) EObjectIDManager(org.eclipse.sirius.components.emf.services.EObjectIDManager)

Example 3 with EObjectIDManager

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

the class UploadDocumentEventHandlerTests method testEObjectIDGenerationForUpload.

/**
 * This method test the {@link UploadDocumentEventHandler} generates new IDs for objects loaded from
 * {@link UploadFile} to ensure there will be no ID conflict with existing elements.
 */
@Test
public void testEObjectIDGenerationForUpload() {
    AdapterFactoryEditingDomain editingDomain = new EditingDomainFactory().create();
    byte[] resourceBytes = JSON_CONTENT.getBytes();
    Map<String, String> eObjectUriToId = this.getEObjectUriToId(editingDomain, resourceBytes);
    final UUID documentId = UUID.randomUUID();
    this.simulatesDocumentUpload(editingDomain, documentId, resourceBytes);
    Resource resource = editingDomain.getResourceSet().getResource(URI.createURI(documentId.toString()), false);
    // Use an output stream to prevent the resource to be written on file system.
    try (var outputStream = new ByteArrayOutputStream()) {
        resource.save(outputStream, Collections.singletonMap(JsonResource.OPTION_ID_MANAGER, new EObjectIDManager() {

            @Override
            public String getOrCreateId(EObject eObject) {
                // Should get the Id produced by the EObjectRandomIDSupplier during upload
                String id = super.getOrCreateId(eObject);
                String uriFragment = resource.getURIFragment(eObject);
                // Use the map to get and check if, the id generate from the first load and the id generated during
                // upload are different
                String idBeforeUpload = eObjectUriToId.get(uriFragment);
                assertThat(idBeforeUpload).isNotEqualTo(id);
                return id;
            }
        }));
    } catch (IOException exception) {
        fail(exception.getMessage());
    }
}
Also used : AdapterFactoryEditingDomain(org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain) EObject(org.eclipse.emf.ecore.EObject) JsonResource(org.eclipse.sirius.emfjson.resource.JsonResource) Resource(org.eclipse.emf.ecore.resource.Resource) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) UUID(java.util.UUID) EObjectIDManager(org.eclipse.sirius.components.emf.services.EObjectIDManager) Test(org.junit.jupiter.api.Test)

Aggregations

ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 IOException (java.io.IOException)3 EObjectIDManager (org.eclipse.sirius.components.emf.services.EObjectIDManager)3 JsonResource (org.eclipse.sirius.emfjson.resource.JsonResource)3 HashMap (java.util.HashMap)2 EObject (org.eclipse.emf.ecore.EObject)2 Resource (org.eclipse.emf.ecore.resource.Resource)2 MeterRegistry (io.micrometer.core.instrument.MeterRegistry)1 Timer (io.micrometer.core.instrument.Timer)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 UUID (java.util.UUID)1 TimeUnit (java.util.concurrent.TimeUnit)1 Collectors (java.util.stream.Collectors)1 AdapterFactoryEditingDomain (org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain)1 EditingDomain (org.eclipse.emf.edit.domain.EditingDomain)1 IEditingContext (org.eclipse.sirius.components.core.api.IEditingContext)1