Search in sources :

Example 1 with DeleteDocumentInput

use of org.eclipse.sirius.components.collaborative.dto.DeleteDocumentInput in project sirius-web by eclipse-sirius.

the class DeleteDocumentEventHandler method handle.

@Override
public void handle(One<IPayload> payloadSink, Many<ChangeDescription> changeDescriptionSink, IEditingContext editingContext, IInput input) {
    this.counter.increment();
    String message = this.messageService.invalidInput(input.getClass().getSimpleName(), DeleteDocumentInput.class.getSimpleName());
    IPayload payload = new ErrorPayload(input.getId(), message);
    ChangeDescription changeDescription = new ChangeDescription(ChangeKind.NOTHING, editingContext.getId(), input);
    // @formatter:off
    var optionalEditingDomain = Optional.of(editingContext).filter(EditingContext.class::isInstance).map(EditingContext.class::cast).map(EditingContext::getDomain);
    if (input instanceof DeleteDocumentInput) {
        DeleteDocumentInput deleteDocumentInput = (DeleteDocumentInput) input;
        var optionalDocument = this.documentService.getDocument(deleteDocumentInput.getDocumentId());
        if (optionalEditingDomain.isPresent() && optionalDocument.isPresent()) {
            AdapterFactoryEditingDomain editingDomain = optionalEditingDomain.get();
            Document document = optionalDocument.get();
            ResourceSet resourceSet = editingDomain.getResourceSet();
            URI uri = URI.createURI(document.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.documentService.delete(document.getId());
            payload = new DeleteDocumentSuccessPayload(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) ErrorPayload(org.eclipse.sirius.components.core.api.ErrorPayload) IEditingContext(org.eclipse.sirius.components.core.api.IEditingContext) URI(org.eclipse.emf.common.util.URI) One(reactor.core.publisher.Sinks.One) IInput(org.eclipse.sirius.components.core.api.IInput) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) EditingContext(org.eclipse.sirius.components.emf.services.EditingContext) Document(org.eclipse.sirius.web.services.api.document.Document) Monitoring(org.eclipse.sirius.components.collaborative.api.Monitoring) Service(org.springframework.stereotype.Service) DeleteDocumentInput(org.eclipse.sirius.components.collaborative.dto.DeleteDocumentInput) Counter(io.micrometer.core.instrument.Counter) ChangeKind(org.eclipse.sirius.components.collaborative.api.ChangeKind) IDocumentService(org.eclipse.sirius.web.services.api.document.IDocumentService) ChangeDescription(org.eclipse.sirius.components.collaborative.api.ChangeDescription) Collectors(java.util.stream.Collectors) DeleteDocumentSuccessPayload(org.eclipse.sirius.components.collaborative.dto.DeleteDocumentSuccessPayload) Objects(java.util.Objects) List(java.util.List) IPayload(org.eclipse.sirius.components.core.api.IPayload) MeterRegistry(io.micrometer.core.instrument.MeterRegistry) Resource(org.eclipse.emf.ecore.resource.Resource) IServicesMessageService(org.eclipse.sirius.web.services.messages.IServicesMessageService) Many(reactor.core.publisher.Sinks.Many) Optional(java.util.Optional) IEditingContextEventHandler(org.eclipse.sirius.components.collaborative.api.IEditingContextEventHandler) AdapterFactoryEditingDomain(org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain) Resource(org.eclipse.emf.ecore.resource.Resource) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) 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) ChangeDescription(org.eclipse.sirius.components.collaborative.api.ChangeDescription) DeleteDocumentInput(org.eclipse.sirius.components.collaborative.dto.DeleteDocumentInput) DeleteDocumentSuccessPayload(org.eclipse.sirius.components.collaborative.dto.DeleteDocumentSuccessPayload)

Example 2 with DeleteDocumentInput

use of org.eclipse.sirius.components.collaborative.dto.DeleteDocumentInput in project sirius-web by eclipse-sirius.

the class DeleteDocumentEventHandlerTests method testDeleteDocument.

@Test
public void testDeleteDocument() {
    UUID projectId = UUID.randomUUID();
    // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
    Document document = new Document(UUID.randomUUID(), new Project(projectId, "", new Profile(UUID.randomUUID(), "username"), Visibility.PUBLIC), "name", "content");
    IDocumentService documentService = new IDocumentService.NoOp() {

        @Override
        public Optional<Document> getDocument(UUID documentId) {
            return Optional.of(document);
        }
    };
    DeleteDocumentEventHandler handler = new DeleteDocumentEventHandler(documentService, new NoOpServicesMessageService(), new SimpleMeterRegistry());
    var input = new DeleteDocumentInput(UUID.randomUUID(), document.getId());
    AdapterFactoryEditingDomain editingDomain = new EditingDomainFactory().create();
    Resource resource = new SiriusWebJSONResourceFactoryImpl().createResource(URI.createURI(document.getId().toString()));
    editingDomain.getResourceSet().getResources().add(resource);
    EditingContext 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(0);
    ChangeDescription changeDescription = changeDescriptionSink.asFlux().blockFirst();
    assertThat(changeDescription.getKind()).isEqualTo(ChangeKind.SEMANTIC_CHANGE);
    IPayload payload = payloadSink.asMono().block();
    assertThat(payload).isInstanceOf(DeleteDocumentSuccessPayload.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) Resource(org.eclipse.emf.ecore.resource.Resource) 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) EditingContext(org.eclipse.sirius.components.emf.services.EditingContext) ChangeDescription(org.eclipse.sirius.components.collaborative.api.ChangeDescription) SiriusWebJSONResourceFactoryImpl(org.eclipse.sirius.components.emf.services.SiriusWebJSONResourceFactoryImpl) UUID(java.util.UUID) IDocumentService(org.eclipse.sirius.web.services.api.document.IDocumentService) DeleteDocumentInput(org.eclipse.sirius.components.collaborative.dto.DeleteDocumentInput) Test(org.junit.jupiter.api.Test)

Aggregations

Resource (org.eclipse.emf.ecore.resource.Resource)2 AdapterFactoryEditingDomain (org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain)2 ChangeDescription (org.eclipse.sirius.components.collaborative.api.ChangeDescription)2 DeleteDocumentInput (org.eclipse.sirius.components.collaborative.dto.DeleteDocumentInput)2 IPayload (org.eclipse.sirius.components.core.api.IPayload)2 EditingContext (org.eclipse.sirius.components.emf.services.EditingContext)2 Document (org.eclipse.sirius.web.services.api.document.Document)2 IDocumentService (org.eclipse.sirius.web.services.api.document.IDocumentService)2 Counter (io.micrometer.core.instrument.Counter)1 MeterRegistry (io.micrometer.core.instrument.MeterRegistry)1 SimpleMeterRegistry (io.micrometer.core.instrument.simple.SimpleMeterRegistry)1 List (java.util.List)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 UUID (java.util.UUID)1 Collectors (java.util.stream.Collectors)1 URI (org.eclipse.emf.common.util.URI)1 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)1 ChangeKind (org.eclipse.sirius.components.collaborative.api.ChangeKind)1 IEditingContextEventHandler (org.eclipse.sirius.components.collaborative.api.IEditingContextEventHandler)1