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);
}
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);
}
Aggregations