Search in sources :

Example 1 with DocumentEntity

use of org.eclipse.sirius.web.persistence.entities.DocumentEntity in project sirius-web by eclipse-sirius.

the class EditingContextEPackageService method findDynamicEPackages.

/**
 * Returns all the EPackages defined by a Domain definition.
 */
private Stream<EPackage> findDynamicEPackages(Function<Domain, Optional<EPackage>> domainConverter) {
    ResourceSet resourceSet = new ResourceSetImpl();
    EPackageRegistryImpl ePackageRegistry = new EPackageRegistryImpl();
    this.globalEPackageRegistry.forEach(ePackageRegistry::put);
    resourceSet.setPackageRegistry(ePackageRegistry);
    var domainDocumentEntities = this.documentRepository.findAllByType(DomainPackage.eNAME, DomainPackage.eNS_URI);
    for (DocumentEntity domainDocumentEntity : domainDocumentEntities) {
        this.loadDomainDefinitions(resourceSet, domainDocumentEntity);
    }
    return resourceSet.getResources().stream().flatMap(res -> this.convertDomains(res, domainConverter));
}
Also used : ResourceSetImpl(org.eclipse.emf.ecore.resource.impl.ResourceSetImpl) DocumentEntity(org.eclipse.sirius.web.persistence.entities.DocumentEntity) EPackageRegistryImpl(org.eclipse.emf.ecore.impl.EPackageRegistryImpl) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet)

Example 2 with DocumentEntity

use of org.eclipse.sirius.web.persistence.entities.DocumentEntity 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 3 with DocumentEntity

use of org.eclipse.sirius.web.persistence.entities.DocumentEntity 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 4 with DocumentEntity

use of org.eclipse.sirius.web.persistence.entities.DocumentEntity in project sirius-web by eclipse-sirius.

the class DeleteDocumentTreeItemEventHandler method handle.

@Override
public IStatus handle(IEditingContext editingContext, TreeItem treeItem) {
    // @formatter:off
    var optionalEditingDomain = Optional.of(editingContext).filter(EditingContext.class::isInstance).map(EditingContext.class::cast).map(EditingContext::getDomain);
    // @formatter:on
    var optionalDocumentEntity = new IDParser().parse(treeItem.getId()).flatMap(this.documentRepository::findById);
    if (optionalEditingDomain.isPresent() && optionalDocumentEntity.isPresent()) {
        AdapterFactoryEditingDomain editingDomain = optionalEditingDomain.get();
        DocumentEntity documentEntity = optionalDocumentEntity.get();
        ResourceSet resourceSet = editingDomain.getResourceSet();
        URI uri = URI.createURI(documentEntity.getId().toString());
        // @formatter:off
        List<Resource> resourcesToDelete = resourceSet.getResources().stream().filter(resource -> resource.getURI().equals(uri)).collect(Collectors.toUnmodifiableList());
        resourcesToDelete.stream().forEach(resourceSet.getResources()::remove);
        // @formatter:on
        this.documentRepository.delete(documentEntity);
        return new Success(ChangeKind.SEMANTIC_CHANGE, Map.of());
    }
    // $NON-NLS-1$
    return new Failure("");
}
Also used : AdapterFactoryEditingDomain(org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain) DocumentEntity(org.eclipse.sirius.web.persistence.entities.DocumentEntity) ChangeKind(org.eclipse.sirius.components.collaborative.api.ChangeKind) IEditingContext(org.eclipse.sirius.components.core.api.IEditingContext) URI(org.eclipse.emf.common.util.URI) Failure(org.eclipse.sirius.components.representations.Failure) IDParser(org.eclipse.sirius.web.services.api.id.IDParser) Success(org.eclipse.sirius.components.representations.Success) TreeItem(org.eclipse.sirius.components.trees.TreeItem) Collectors(java.util.stream.Collectors) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) Objects(java.util.Objects) List(java.util.List) EditingContext(org.eclipse.sirius.components.emf.services.EditingContext) IStatus(org.eclipse.sirius.components.representations.IStatus) IDocumentRepository(org.eclipse.sirius.web.persistence.repositories.IDocumentRepository) IDeleteTreeItemHandler(org.eclipse.sirius.web.services.explorer.api.IDeleteTreeItemHandler) Service(org.springframework.stereotype.Service) Map(java.util.Map) Resource(org.eclipse.emf.ecore.resource.Resource) Optional(java.util.Optional) AdapterFactoryEditingDomain(org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain) DocumentEntity(org.eclipse.sirius.web.persistence.entities.DocumentEntity) IEditingContext(org.eclipse.sirius.components.core.api.IEditingContext) EditingContext(org.eclipse.sirius.components.emf.services.EditingContext) IDParser(org.eclipse.sirius.web.services.api.id.IDParser) Resource(org.eclipse.emf.ecore.resource.Resource) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) URI(org.eclipse.emf.common.util.URI) Success(org.eclipse.sirius.components.representations.Success) Failure(org.eclipse.sirius.components.representations.Failure)

Example 5 with DocumentEntity

use of org.eclipse.sirius.web.persistence.entities.DocumentEntity in project sirius-web by eclipse-sirius.

the class RenameDocumentTreeItemHandler method handle.

@Override
public IStatus handle(IEditingContext editingContext, TreeItem treeItem, String newLabel) {
    // @formatter:off
    Optional<AdapterFactoryEditingDomain> optionalEditingDomain = Optional.of(editingContext).filter(EditingContext.class::isInstance).map(EditingContext.class::cast).map(EditingContext::getDomain);
    // @formatter:on
    var optionalDocumentEntity = new IDParser().parse(treeItem.getId()).flatMap(this.documentRepository::findById);
    if (optionalEditingDomain.isPresent() && optionalDocumentEntity.isPresent()) {
        DocumentEntity documentEntity = optionalDocumentEntity.get();
        documentEntity.setName(newLabel);
        this.documentRepository.save(documentEntity);
        AdapterFactoryEditingDomain adapterFactoryEditingDomain = optionalEditingDomain.get();
        ResourceSet resourceSet = adapterFactoryEditingDomain.getResourceSet();
        // @formatter:off
        resourceSet.getResources().stream().filter(resource -> documentEntity.getId().toString().equals(resource.getURI().toString())).findFirst().ifPresent(resource -> {
            resource.eAdapters().stream().filter(DocumentMetadataAdapter.class::isInstance).map(DocumentMetadataAdapter.class::cast).findFirst().ifPresent(adapter -> adapter.setName(documentEntity.getName()));
        });
        // @formatter:on
        return new Success(ChangeKind.SEMANTIC_CHANGE, Map.of());
    }
    // $NON-NLS-1$
    return new Failure("");
}
Also used : AdapterFactoryEditingDomain(org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain) DocumentMetadataAdapter(org.eclipse.sirius.web.services.documents.DocumentMetadataAdapter) DocumentEntity(org.eclipse.sirius.web.persistence.entities.DocumentEntity) IEditingContext(org.eclipse.sirius.components.core.api.IEditingContext) EditingContext(org.eclipse.sirius.components.emf.services.EditingContext) IDParser(org.eclipse.sirius.web.services.api.id.IDParser) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) Success(org.eclipse.sirius.components.representations.Success) Failure(org.eclipse.sirius.components.representations.Failure)

Aggregations

DocumentEntity (org.eclipse.sirius.web.persistence.entities.DocumentEntity)18 ProjectEntity (org.eclipse.sirius.web.persistence.entities.ProjectEntity)8 Test (org.junit.jupiter.api.Test)8 IEditingContext (org.eclipse.sirius.components.core.api.IEditingContext)7 EditingContext (org.eclipse.sirius.components.emf.services.EditingContext)7 IDParser (org.eclipse.sirius.web.services.api.id.IDParser)6 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)6 Transactional (org.springframework.transaction.annotation.Transactional)6 List (java.util.List)5 Resource (org.eclipse.emf.ecore.resource.Resource)5 JsonResource (org.eclipse.sirius.emfjson.resource.JsonResource)5 IDocumentRepository (org.eclipse.sirius.web.persistence.repositories.IDocumentRepository)5 ArrayList (java.util.ArrayList)4 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)4 AdapterFactoryEditingDomain (org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain)4 DocumentMetadataAdapter (org.eclipse.sirius.web.services.documents.DocumentMetadataAdapter)4 IOException (java.io.IOException)3 Objects (java.util.Objects)3 Optional (java.util.Optional)3 UUID (java.util.UUID)3