Search in sources :

Example 1 with DocumentMetadataAdapter

use of org.eclipse.sirius.web.services.documents.DocumentMetadataAdapter 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 2 with DocumentMetadataAdapter

use of org.eclipse.sirius.web.services.documents.DocumentMetadataAdapter in project sirius-web by eclipse-sirius.

the class ExplorerDescriptionProvider method getLabel.

private String getLabel(VariableManager variableManager) {
    Object self = variableManager.getVariables().get(VariableManager.SELF);
    // $NON-NLS-1$
    String label = "";
    if (self instanceof RepresentationMetadata) {
        label = ((RepresentationMetadata) self).getLabel();
    } else if (self instanceof Resource) {
        Resource resource = (Resource) self;
        // @formatter:off
        label = resource.eAdapters().stream().filter(DocumentMetadataAdapter.class::isInstance).map(DocumentMetadataAdapter.class::cast).findFirst().map(DocumentMetadataAdapter::getName).orElse(resource.getURI().lastSegment());
    // @formatter:on
    } else if (self instanceof EObject) {
        label = this.objectService.getLabel(self);
        if (label.isBlank()) {
            var kind = this.objectService.getKind(self);
            label = this.kindParser.getParameterValues(kind).get(SemanticKindConstants.ENTITY_ARGUMENT).get(0);
        }
    }
    return label;
}
Also used : DocumentMetadataAdapter(org.eclipse.sirius.web.services.documents.DocumentMetadataAdapter) EObject(org.eclipse.emf.ecore.EObject) Resource(org.eclipse.emf.ecore.resource.Resource) RepresentationMetadata(org.eclipse.sirius.components.core.RepresentationMetadata) EObject(org.eclipse.emf.ecore.EObject)

Example 3 with DocumentMetadataAdapter

use of org.eclipse.sirius.web.services.documents.DocumentMetadataAdapter in project sirius-web by eclipse-sirius.

the class EditingContextPersistenceServiceTests method testDocumentPersistence.

@Test
public void testDocumentPersistence() {
    UUID projectId = UUID.randomUUID();
    // $NON-NLS-1$
    String name = "New Document";
    UUID id = UUID.randomUUID();
    JsonResource resource = new SiriusWebJSONResourceFactoryImpl().createResource(URI.createURI(id.toString()));
    resource.eAdapters().add(new DocumentMetadataAdapter(name));
    EClass eClass = EcoreFactory.eINSTANCE.createEClass();
    // $NON-NLS-1$
    eClass.setName("Concept");
    resource.getContents().add(eClass);
    AdapterFactoryEditingDomain editingDomain = new EditingDomainFactory().create();
    editingDomain.getResourceSet().getResources().add(resource);
    ProjectEntity projectEntity = new ProjectEntity();
    projectEntity.setId(projectId);
    // $NON-NLS-1$
    projectEntity.setName("");
    AccountEntity owner = new AccountEntity();
    owner.setId(UUID.randomUUID());
    // $NON-NLS-1$
    owner.setUsername("jdoe");
    projectEntity.setOwner(owner);
    DocumentEntity existingEntity = new DocumentEntity();
    existingEntity.setId(id);
    existingEntity.setProject(projectEntity);
    existingEntity.setName(name);
    // $NON-NLS-1$
    existingEntity.setContent("");
    List<DocumentEntity> entities = new ArrayList<>();
    IDocumentRepository documentRepository = new NoOpDocumentRepository() {

        @Override
        public <S extends DocumentEntity> S save(S entity) {
            entities.add(entity);
            return entity;
        }

        @Override
        public Optional<DocumentEntity> findById(UUID id) {
            return Optional.of(existingEntity);
        }
    };
    IEditingContextPersistenceService editingContextPersistenceService = new EditingContextPersistenceService(documentRepository, new NoOpApplicationEventPublisher(), new SimpleMeterRegistry());
    assertThat(entities).hasSize(0);
    IEditingContext editingContext = new EditingContext(UUID.randomUUID().toString(), editingDomain);
    editingContextPersistenceService.persist(editingContext);
    assertThat(entities).hasSize(1);
    DocumentEntity documentEntity = entities.get(0);
    assertThat(documentEntity.getId()).isEqualTo(id);
    assertThat(documentEntity.getName()).isEqualTo(name);
    assertThat(documentEntity.getProject().getId()).isEqualTo(projectId);
}
Also used : AdapterFactoryEditingDomain(org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain) DocumentMetadataAdapter(org.eclipse.sirius.web.services.documents.DocumentMetadataAdapter) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) ArrayList(java.util.ArrayList) JsonResource(org.eclipse.sirius.emfjson.resource.JsonResource) IEditingContextPersistenceService(org.eclipse.sirius.components.core.api.IEditingContextPersistenceService) AccountEntity(org.eclipse.sirius.web.persistence.entities.AccountEntity) EClass(org.eclipse.emf.ecore.EClass) IDocumentRepository(org.eclipse.sirius.web.persistence.repositories.IDocumentRepository) DocumentEntity(org.eclipse.sirius.web.persistence.entities.DocumentEntity) IEditingContextPersistenceService(org.eclipse.sirius.components.core.api.IEditingContextPersistenceService) IEditingContext(org.eclipse.sirius.components.core.api.IEditingContext) IEditingContext(org.eclipse.sirius.components.core.api.IEditingContext) EditingContext(org.eclipse.sirius.components.emf.services.EditingContext) ProjectEntity(org.eclipse.sirius.web.persistence.entities.ProjectEntity) SiriusWebJSONResourceFactoryImpl(org.eclipse.sirius.components.emf.services.SiriusWebJSONResourceFactoryImpl) UUID(java.util.UUID) EditingDomainFactory(org.eclipse.sirius.web.services.documents.EditingDomainFactory) Test(org.junit.jupiter.api.Test)

Aggregations

DocumentMetadataAdapter (org.eclipse.sirius.web.services.documents.DocumentMetadataAdapter)3 AdapterFactoryEditingDomain (org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain)2 IEditingContext (org.eclipse.sirius.components.core.api.IEditingContext)2 EditingContext (org.eclipse.sirius.components.emf.services.EditingContext)2 SiriusWebJSONResourceFactoryImpl (org.eclipse.sirius.components.emf.services.SiriusWebJSONResourceFactoryImpl)2 JsonResource (org.eclipse.sirius.emfjson.resource.JsonResource)2 DocumentEntity (org.eclipse.sirius.web.persistence.entities.DocumentEntity)2 SimpleMeterRegistry (io.micrometer.core.instrument.simple.SimpleMeterRegistry)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 UUID (java.util.UUID)1 BasicCommandStack (org.eclipse.emf.common.command.BasicCommandStack)1 URI (org.eclipse.emf.common.util.URI)1 EClass (org.eclipse.emf.ecore.EClass)1 EObject (org.eclipse.emf.ecore.EObject)1 EPackage (org.eclipse.emf.ecore.EPackage)1 EPackageRegistryImpl (org.eclipse.emf.ecore.impl.EPackageRegistryImpl)1 Resource (org.eclipse.emf.ecore.resource.Resource)1