Search in sources :

Example 1 with RenameDocumentSuccessPayload

use of org.eclipse.sirius.components.collaborative.dto.RenameDocumentSuccessPayload 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)1 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)1 AdapterFactoryEditingDomain (org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain)1 ChangeDescription (org.eclipse.sirius.components.collaborative.api.ChangeDescription)1 RenameDocumentInput (org.eclipse.sirius.components.collaborative.dto.RenameDocumentInput)1 RenameDocumentSuccessPayload (org.eclipse.sirius.components.collaborative.dto.RenameDocumentSuccessPayload)1 ErrorPayload (org.eclipse.sirius.components.core.api.ErrorPayload)1 IEditingContext (org.eclipse.sirius.components.core.api.IEditingContext)1 IPayload (org.eclipse.sirius.components.core.api.IPayload)1 EditingContext (org.eclipse.sirius.components.emf.services.EditingContext)1 Document (org.eclipse.sirius.web.services.api.document.Document)1