From 2e8c7835c99f036a22b75795eb6b7d6a0943f782 Mon Sep 17 00:00:00 2001 From: Arthur Daussy Date: Mon, 22 Jul 2024 15:53:23 +0200 Subject: [PATCH] [3801] Add IViewFormDescriptionSearchService service MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Bug: https://github.com/eclipse-sirius/sirius-web/issues/3801 Signed-off-by: Arthur Daussy Signed-off-by: Florian ROUËNÉ --- CHANGELOG.adoc | 1 + .../ViewFormDescriptionSearchService.java | 99 +++++++++++++++++++ .../IViewFormDescriptionSearchService.java | 50 ++++++++++ 3 files changed, 150 insertions(+) create mode 100644 packages/view/backend/sirius-components-view-emf/src/main/java/org/eclipse/sirius/components/view/emf/form/ViewFormDescriptionSearchService.java create mode 100644 packages/view/backend/sirius-components-view-emf/src/main/java/org/eclipse/sirius/components/view/emf/form/api/IViewFormDescriptionSearchService.java diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index 5321924c8ac..0b3252aed09 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -82,6 +82,7 @@ Thanks to this change, the various form based representations (details, related A migration participant has been added to automatically keep compatible all diagram descriptions created before 2024.9.0. - https://github.com/eclipse-sirius/sirius-web/issues/3774[#3774] [sirius-web] Add an extension point to contribute custom tools to the diagram palette - https://github.com/eclipse-sirius/sirius-web/issues/3785[#3785] [sirius-web] Add an extension point to contribute custom tree item menu entry +- https://github.com/eclipse-sirius/sirius-web/issues/3801[#3801] Add an equivalent of IViewDiagramDescriptionSearchService for Form descriptions == v2024.7.0 diff --git a/packages/view/backend/sirius-components-view-emf/src/main/java/org/eclipse/sirius/components/view/emf/form/ViewFormDescriptionSearchService.java b/packages/view/backend/sirius-components-view-emf/src/main/java/org/eclipse/sirius/components/view/emf/form/ViewFormDescriptionSearchService.java new file mode 100644 index 00000000000..33e5c87366b --- /dev/null +++ b/packages/view/backend/sirius-components-view-emf/src/main/java/org/eclipse/sirius/components/view/emf/form/ViewFormDescriptionSearchService.java @@ -0,0 +1,99 @@ +/******************************************************************************* + * Copyright (c) 2024 Obeo. + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Obeo - initial API and implementation + *******************************************************************************/ +package org.eclipse.sirius.components.view.emf.form; + +import java.util.List; +import java.util.Objects; +import java.util.Optional; + +import org.eclipse.emf.common.util.TreeIterator; +import org.eclipse.emf.ecore.EObject; +import org.eclipse.sirius.components.core.api.IEditingContext; +import org.eclipse.sirius.components.core.api.IIdentityService; +import org.eclipse.sirius.components.core.api.IURLParser; +import org.eclipse.sirius.components.view.emf.IRepresentationDescriptionIdProvider; +import org.eclipse.sirius.components.view.emf.IViewRepresentationDescriptionSearchService; +import org.eclipse.sirius.components.view.emf.form.api.IViewFormDescriptionSearchService; +import org.eclipse.sirius.components.view.form.FormDescription; +import org.eclipse.sirius.components.view.form.FormElementDescription; +import org.springframework.stereotype.Service; + +/** + * Used to find information from view based form descriptions. + * + * @author Arthur Daussy + */ +@Service +public class ViewFormDescriptionSearchService implements IViewFormDescriptionSearchService { + + private final IIdentityService identityService; + + private final IURLParser urlParser; + + private final IViewRepresentationDescriptionSearchService viewRepresentationDescriptionSearchService; + + public ViewFormDescriptionSearchService(IIdentityService identityService, IURLParser urlParser, IViewRepresentationDescriptionSearchService viewRepresentationDescriptionSearchService) { + this.identityService = Objects.requireNonNull(identityService); + this.urlParser = Objects.requireNonNull(urlParser); + this.viewRepresentationDescriptionSearchService = Objects.requireNonNull(viewRepresentationDescriptionSearchService); + } + + @Override + public Optional findById(IEditingContext editingContext, String formDescriptionId) { + return this.viewRepresentationDescriptionSearchService.findById(editingContext, formDescriptionId).filter(FormDescription.class::isInstance).map(FormDescription.class::cast); + } + + @Override + public Optional findFormElementDescriptionById(IEditingContext editingContext, String formElementDescriptionId) { + Optional optionalSourceElementId = this.getSourceElementId(formElementDescriptionId); + Optional optionalSourceId = this.getSourceId(formElementDescriptionId); + if (optionalSourceElementId.isPresent() && optionalSourceId.isPresent()) { + var sourceElementId = optionalSourceElementId.get(); + var sourceId = optionalSourceId.get(); + + var views = this.viewRepresentationDescriptionSearchService.findViewsBySourceId(editingContext, sourceId); + + return views.stream() + .flatMap(view -> view.getDescriptions().stream()) + .filter(FormDescription.class::isInstance) + .map(FormDescription.class::cast) + .map(viewFormDescription -> this.findFormElementDescriptionById(viewFormDescription, sourceElementId)) + .flatMap(Optional::stream) + .findFirst(); + } + return Optional.empty(); + } + + private Optional findFormElementDescriptionById(FormDescription viewFormDescription, String sourceElementId) { + + TreeIterator contentIterator = viewFormDescription.eAllContents(); + while (contentIterator.hasNext()) { + EObject next = contentIterator.next(); + if (next instanceof FormElementDescription desc && sourceElementId.equals(this.identityService.getId(desc))) { + return Optional.of(desc); + } + } + return Optional.empty(); + } + + private Optional getSourceElementId(String descriptionId) { + var parameters = this.urlParser.getParameterValues(descriptionId); + return Optional.ofNullable(parameters.get(IRepresentationDescriptionIdProvider.SOURCE_ELEMENT_ID)).orElse(List.of()).stream().findFirst(); + } + + private Optional getSourceId(String descriptionId) { + var parameters = this.urlParser.getParameterValues(descriptionId); + return Optional.ofNullable(parameters.get(IRepresentationDescriptionIdProvider.SOURCE_ID)).orElse(List.of()).stream().findFirst(); + } + +} diff --git a/packages/view/backend/sirius-components-view-emf/src/main/java/org/eclipse/sirius/components/view/emf/form/api/IViewFormDescriptionSearchService.java b/packages/view/backend/sirius-components-view-emf/src/main/java/org/eclipse/sirius/components/view/emf/form/api/IViewFormDescriptionSearchService.java new file mode 100644 index 00000000000..8837b77d559 --- /dev/null +++ b/packages/view/backend/sirius-components-view-emf/src/main/java/org/eclipse/sirius/components/view/emf/form/api/IViewFormDescriptionSearchService.java @@ -0,0 +1,50 @@ +/******************************************************************************* + * Copyright (c) 2024 Obeo. + * This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v2.0 + * which accompanies this distribution, and is available at + * https://www.eclipse.org/legal/epl-2.0/ + * + * SPDX-License-Identifier: EPL-2.0 + * + * Contributors: + * Obeo - initial API and implementation + *******************************************************************************/ +package org.eclipse.sirius.components.view.emf.form.api; + +import java.util.Optional; + +import org.eclipse.sirius.components.core.api.IEditingContext; +import org.eclipse.sirius.components.view.form.FormDescription; +import org.eclipse.sirius.components.view.form.FormElementDescription; + +/** + * Used to find information from a view based form descriptions. + * + * @author Arthur Daussy + */ +public interface IViewFormDescriptionSearchService { + + Optional findById(IEditingContext editingContext, String formDescriptionId); + + Optional findFormElementDescriptionById(IEditingContext editingContext, String formElementDescriptionId); + + /** + * No operation implementation. + * + * @author Arthur Daussy + */ + class NoOp implements IViewFormDescriptionSearchService { + + @Override + public Optional findById(IEditingContext editingContext, String formDescriptionId) { + return Optional.empty(); + } + + @Override + public Optional findFormElementDescriptionById(IEditingContext editingContext, String formElementDescriptionId) { + return Optional.empty(); + } + + } +}