Search in sources :

Example 1 with IDParser

use of org.eclipse.sirius.web.services.api.id.IDParser 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 IDParser

use of org.eclipse.sirius.web.services.api.id.IDParser 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 IDParser

use of org.eclipse.sirius.web.services.api.id.IDParser 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 4 with IDParser

use of org.eclipse.sirius.web.services.api.id.IDParser in project sirius-web by eclipse-sirius.

the class RepresentationService method save.

@Override
public void save(IEditingContext editingContext, ISemanticRepresentation representation) {
    long start = System.currentTimeMillis();
    var editingContextId = new IDParser().parse(editingContext.getId());
    var representationId = new IDParser().parse(representation.getId());
    if (editingContextId.isPresent() && representationId.isPresent()) {
        UUID editingContextUUID = editingContextId.get();
        UUID representationUUID = representationId.get();
        var optionalProjectEntity = this.projectRepository.findById(editingContextUUID);
        if (optionalProjectEntity.isPresent()) {
            ProjectEntity projectEntity = optionalProjectEntity.get();
            RepresentationEntity representationEntity = this.toEntity(projectEntity, representationUUID, representation);
            this.representationRepository.save(representationEntity);
        }
    }
    long end = System.currentTimeMillis();
    this.timer.record(end - start, TimeUnit.MILLISECONDS);
}
Also used : RepresentationEntity(org.eclipse.sirius.web.persistence.entities.RepresentationEntity) ProjectEntity(org.eclipse.sirius.web.persistence.entities.ProjectEntity) IDParser(org.eclipse.sirius.web.services.api.id.IDParser) UUID(java.util.UUID)

Example 5 with IDParser

use of org.eclipse.sirius.web.services.api.id.IDParser in project sirius-web by eclipse-sirius.

the class RepresentationService method getRepresentationDescriptorForProjectId.

@Override
public Optional<RepresentationDescriptor> getRepresentationDescriptorForProjectId(String projectId, String representationId) {
    var projectUUID = new IDParser().parse(projectId);
    var representationUUID = new IDParser().parse(representationId);
    if (projectUUID.isPresent() && representationUUID.isPresent()) {
        return this.representationRepository.findByIdAndProjectId(representationUUID.get(), projectUUID.get()).map(new RepresentationMapper(this.objectMapper)::toDTO);
    }
    return Optional.empty();
}
Also used : IDParser(org.eclipse.sirius.web.services.api.id.IDParser)

Aggregations

IDParser (org.eclipse.sirius.web.services.api.id.IDParser)13 DocumentEntity (org.eclipse.sirius.web.persistence.entities.DocumentEntity)6 List (java.util.List)5 Objects (java.util.Objects)5 Optional (java.util.Optional)5 IEditingContext (org.eclipse.sirius.components.core.api.IEditingContext)5 EditingContext (org.eclipse.sirius.components.emf.services.EditingContext)5 Collectors (java.util.stream.Collectors)4 MeterRegistry (io.micrometer.core.instrument.MeterRegistry)3 Timer (io.micrometer.core.instrument.Timer)3 IOException (java.io.IOException)3 UUID (java.util.UUID)3 TimeUnit (java.util.concurrent.TimeUnit)3 Resource (org.eclipse.emf.ecore.resource.Resource)3 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)3 AdapterFactoryEditingDomain (org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain)3 ErrorPayload (org.eclipse.sirius.components.core.api.ErrorPayload)3 JsonResource (org.eclipse.sirius.emfjson.resource.JsonResource)3 IDocumentRepository (org.eclipse.sirius.web.persistence.repositories.IDocumentRepository)3 Document (org.eclipse.sirius.web.services.api.document.Document)3