Search in sources :

Example 1 with RenameDocumentInput

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

the class RenameDocumentEventHandlerTests method testRenameDocument.

@Test
public void testRenameDocument() {
    IDocumentService noOpDocumentService = new IDocumentService.NoOp() {

        @Override
        public Optional<Document> rename(UUID documentId, String newName) {
            // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            return Optional.of(new Document(documentId, new Project(UUID.randomUUID(), "", new Profile(UUID.randomUUID(), "username"), Visibility.PUBLIC), newName, "noContent"));
        }
    };
    RenameDocumentEventHandler handler = new RenameDocumentEventHandler(noOpDocumentService, new NoOpServicesMessageService(), new SimpleMeterRegistry());
    UUID documentId = UUID.randomUUID();
    IInput input = new RenameDocumentInput(UUID.randomUUID(), documentId, NEW_NAME);
    AdapterFactoryEditingDomain editingDomain = new EditingDomainFactory().create();
    DocumentMetadataAdapter adapter = new DocumentMetadataAdapter(OLD_NAME);
    Resource resource = new SiriusWebJSONResourceFactoryImpl().createResource(URI.createURI(documentId.toString()));
    resource.eAdapters().add(adapter);
    editingDomain.getResourceSet().getResources().add(resource);
    assertThat(editingDomain.getResourceSet().getResources().size()).isEqualTo(1);
    assertThat(adapter.getName()).isEqualTo(OLD_NAME);
    IEditingContext 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(1);
    assertThat(adapter.getName()).isEqualTo(NEW_NAME);
    ChangeDescription changeDescription = changeDescriptionSink.asFlux().blockFirst();
    assertThat(changeDescription.getKind()).isEqualTo(ChangeKind.SEMANTIC_CHANGE);
    IPayload payload = payloadSink.asMono().block();
    assertThat(payload).isInstanceOf(RenameDocumentSuccessPayload.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) RenameDocumentInput(org.eclipse.sirius.components.collaborative.dto.RenameDocumentInput) Resource(org.eclipse.emf.ecore.resource.Resource) IInput(org.eclipse.sirius.components.core.api.IInput) 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) IEditingContext(org.eclipse.sirius.components.core.api.IEditingContext) IEditingContext(org.eclipse.sirius.components.core.api.IEditingContext) EditingContext(org.eclipse.sirius.components.emf.services.EditingContext) ChangeDescription(org.eclipse.sirius.components.collaborative.api.ChangeDescription) SiriusWebJSONResourceFactoryImpl(org.eclipse.sirius.components.emf.services.SiriusWebJSONResourceFactoryImpl) IDocumentService(org.eclipse.sirius.web.services.api.document.IDocumentService) UUID(java.util.UUID) Test(org.junit.jupiter.api.Test)

Example 2 with RenameDocumentInput

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

the class RenameDocumentEventHandler 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(), RenameDocumentInput.class.getSimpleName());
    IPayload payload = new ErrorPayload(input.getId(), message);
    ChangeDescription changeDescription = new ChangeDescription(ChangeKind.NOTHING, editingContext.getId(), input);
    // @formatter:off
    Optional<AdapterFactoryEditingDomain> optionalEditingDomain = Optional.of(editingContext).filter(EditingContext.class::isInstance).map(EditingContext.class::cast).map(EditingContext::getDomain);
    if (input instanceof RenameDocumentInput) {
        RenameDocumentInput renameDocumentInput = (RenameDocumentInput) input;
        UUID documentId = renameDocumentInput.getDocumentId();
        String newName = renameDocumentInput.getNewName();
        Optional<Document> optionalDocument = this.documentService.rename(documentId, newName);
        if (optionalEditingDomain.isPresent() && optionalDocument.isPresent()) {
            Document document = optionalDocument.get();
            AdapterFactoryEditingDomain adapterFactoryEditingDomain = optionalEditingDomain.get();
            ResourceSet resourceSet = adapterFactoryEditingDomain.getResourceSet();
            // @formatter:off
            resourceSet.getResources().stream().filter(resource -> document.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(newName));
            });
            // @formatter:on
            payload = new RenameDocumentSuccessPayload(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) RenameDocumentSuccessPayload(org.eclipse.sirius.components.collaborative.dto.RenameDocumentSuccessPayload) RenameDocumentInput(org.eclipse.sirius.components.collaborative.dto.RenameDocumentInput) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) Document(org.eclipse.sirius.web.services.api.document.Document) 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) UUID(java.util.UUID)

Aggregations

UUID (java.util.UUID)2 AdapterFactoryEditingDomain (org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain)2 ChangeDescription (org.eclipse.sirius.components.collaborative.api.ChangeDescription)2 RenameDocumentInput (org.eclipse.sirius.components.collaborative.dto.RenameDocumentInput)2 IEditingContext (org.eclipse.sirius.components.core.api.IEditingContext)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 SimpleMeterRegistry (io.micrometer.core.instrument.simple.SimpleMeterRegistry)1 Resource (org.eclipse.emf.ecore.resource.Resource)1 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)1 RenameDocumentSuccessPayload (org.eclipse.sirius.components.collaborative.dto.RenameDocumentSuccessPayload)1 ErrorPayload (org.eclipse.sirius.components.core.api.ErrorPayload)1 IInput (org.eclipse.sirius.components.core.api.IInput)1 SiriusWebJSONResourceFactoryImpl (org.eclipse.sirius.components.emf.services.SiriusWebJSONResourceFactoryImpl)1 Profile (org.eclipse.sirius.web.services.api.accounts.Profile)1 IDocumentService (org.eclipse.sirius.web.services.api.document.IDocumentService)1 Project (org.eclipse.sirius.web.services.api.projects.Project)1 NoOpServicesMessageService (org.eclipse.sirius.web.services.projects.NoOpServicesMessageService)1 Test (org.junit.jupiter.api.Test)1