forked from eclipse-sirius/sirius-web
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[3801] Add IViewFormDescriptionSearchService service
Bug: eclipse-sirius#3801 Signed-off-by: Arthur Daussy <arthur.daussy@obeo.fr> Signed-off-by: Florian ROUËNÉ <florian.rouene@obeosoft.com>
- Loading branch information
Showing
3 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
...in/java/org/eclipse/sirius/components/view/emf/form/ViewFormDescriptionSearchService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<FormDescription> findById(IEditingContext editingContext, String formDescriptionId) { | ||
return this.viewRepresentationDescriptionSearchService.findById(editingContext, formDescriptionId).filter(FormDescription.class::isInstance).map(FormDescription.class::cast); | ||
} | ||
|
||
@Override | ||
public Optional<FormElementDescription> findFormElementDescriptionById(IEditingContext editingContext, String formElementDescriptionId) { | ||
Optional<String> optionalSourceElementId = this.getSourceElementId(formElementDescriptionId); | ||
Optional<String> 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<FormElementDescription> findFormElementDescriptionById(FormDescription viewFormDescription, String sourceElementId) { | ||
|
||
TreeIterator<EObject> 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<String> 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<String> getSourceId(String descriptionId) { | ||
var parameters = this.urlParser.getParameterValues(descriptionId); | ||
return Optional.ofNullable(parameters.get(IRepresentationDescriptionIdProvider.SOURCE_ID)).orElse(List.of()).stream().findFirst(); | ||
} | ||
|
||
} |
50 changes: 50 additions & 0 deletions
50
...va/org/eclipse/sirius/components/view/emf/form/api/IViewFormDescriptionSearchService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<FormDescription> findById(IEditingContext editingContext, String formDescriptionId); | ||
|
||
Optional<FormElementDescription> findFormElementDescriptionById(IEditingContext editingContext, String formElementDescriptionId); | ||
|
||
/** | ||
* No operation implementation. | ||
* | ||
* @author Arthur Daussy | ||
*/ | ||
class NoOp implements IViewFormDescriptionSearchService { | ||
|
||
@Override | ||
public Optional<FormDescription> findById(IEditingContext editingContext, String formDescriptionId) { | ||
return Optional.empty(); | ||
} | ||
|
||
@Override | ||
public Optional<FormElementDescription> findFormElementDescriptionById(IEditingContext editingContext, String formElementDescriptionId) { | ||
return Optional.empty(); | ||
} | ||
|
||
} | ||
} |