|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2026 Obeo. |
| 3 | + * This program and the accompanying materials |
| 4 | + * are made available under the terms of the Eclipse Public License v2.0 |
| 5 | + * which accompanies this distribution, and is available at |
| 6 | + * https://www.eclipse.org/legal/epl-2.0/ |
| 7 | + * |
| 8 | + * SPDX-License-Identifier: EPL-2.0 |
| 9 | + * |
| 10 | + * Contributors: |
| 11 | + * Obeo - initial API and implementation |
| 12 | + *******************************************************************************/ |
| 13 | +package org.eclipse.syson.application.expressions.services; |
| 14 | + |
| 15 | +import java.util.Objects; |
| 16 | +import java.util.Optional; |
| 17 | + |
| 18 | +import org.eclipse.sirius.components.collaborative.api.ChangeDescription; |
| 19 | +import org.eclipse.sirius.components.collaborative.api.ChangeKind; |
| 20 | +import org.eclipse.sirius.components.collaborative.api.IEditingContextEventHandler; |
| 21 | +import org.eclipse.sirius.components.collaborative.messages.ICollaborativeMessageService; |
| 22 | +import org.eclipse.sirius.components.core.api.ErrorPayload; |
| 23 | +import org.eclipse.sirius.components.core.api.IEditingContext; |
| 24 | +import org.eclipse.sirius.components.core.api.IInput; |
| 25 | +import org.eclipse.sirius.components.core.api.IObjectSearchService; |
| 26 | +import org.eclipse.sirius.components.core.api.IPayload; |
| 27 | +import org.eclipse.sirius.components.core.api.SuccessPayload; |
| 28 | +import org.eclipse.syson.application.expressions.dto.DeleteExpressionInput; |
| 29 | +import org.eclipse.syson.services.DeleteService; |
| 30 | +import org.eclipse.syson.sysml.Element; |
| 31 | +import org.eclipse.syson.sysml.Expression; |
| 32 | +import org.eclipse.syson.sysml.metamodel.services.MetamodelQueryElementService; |
| 33 | +import org.springframework.stereotype.Service; |
| 34 | + |
| 35 | +import reactor.core.publisher.Sinks; |
| 36 | + |
| 37 | +/** |
| 38 | + * Event handler for the {@code deleteExpression} mutation. |
| 39 | + * |
| 40 | + * @author pcdavid |
| 41 | + */ |
| 42 | +@Service |
| 43 | +public class DeleteExpressionEventHandler implements IEditingContextEventHandler { |
| 44 | + |
| 45 | + private final IObjectSearchService objectSearchService; |
| 46 | + |
| 47 | + private final MetamodelQueryElementService metamodelQueryElementService; |
| 48 | + |
| 49 | + private final ICollaborativeMessageService messageService; |
| 50 | + |
| 51 | + private final DeleteService deleteService; |
| 52 | + |
| 53 | + public DeleteExpressionEventHandler(IObjectSearchService objectSearchService, ICollaborativeMessageService messageService) { |
| 54 | + this.objectSearchService = Objects.requireNonNull(objectSearchService); |
| 55 | + this.metamodelQueryElementService = new MetamodelQueryElementService(); |
| 56 | + this.messageService = Objects.requireNonNull(messageService); |
| 57 | + this.deleteService = new DeleteService(); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public boolean canHandle(IEditingContext editingContext, IInput input) { |
| 62 | + return input instanceof DeleteExpressionInput; |
| 63 | + } |
| 64 | + |
| 65 | + @Override |
| 66 | + public void handle(Sinks.One<IPayload> payloadSink, Sinks.Many<ChangeDescription> changeDescriptionSink, IEditingContext editingContext, IInput input) { |
| 67 | + IPayload payload; |
| 68 | + if (input instanceof DeleteExpressionInput deleteExpressionInput) { |
| 69 | + Optional<Expression> optionalExpression = this.objectSearchService.getObject(editingContext, deleteExpressionInput.parentElementId()) |
| 70 | + .filter(Element.class::isInstance) |
| 71 | + .map(Element.class::cast) |
| 72 | + .flatMap(element -> this.metamodelQueryElementService.findSingleExpressionDefinition(element)); |
| 73 | + if (optionalExpression.isPresent()) { |
| 74 | + this.deleteService.deleteFromModel(optionalExpression.get()); |
| 75 | + var changeDescription = new ChangeDescription(ChangeKind.SEMANTIC_CHANGE, editingContext.getId(), input); |
| 76 | + changeDescriptionSink.tryEmitNext(changeDescription); |
| 77 | + payload = new SuccessPayload(input.id()); |
| 78 | + } else { |
| 79 | + payload = new ErrorPayload(input.id(), this.messageService.notFound()); |
| 80 | + } |
| 81 | + } else { |
| 82 | + payload = new ErrorPayload(input.id(), this.messageService.invalidInput(DeleteExpressionInput.class.getName(), input.getClass().getName())); |
| 83 | + } |
| 84 | + payloadSink.tryEmitValue(payload); |
| 85 | + } |
| 86 | +} |
0 commit comments