forked from eclipse-sirius/sirius-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FormDescriptionAggregator.java
78 lines (65 loc) · 3.34 KB
/
FormDescriptionAggregator.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*******************************************************************************
* Copyright (c) 2019, 2023 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.collaborative.forms;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import org.eclipse.sirius.components.collaborative.forms.variables.FormVariableProvider;
import org.eclipse.sirius.components.core.api.IObjectService;
import org.eclipse.sirius.components.forms.description.FormDescription;
import org.eclipse.sirius.components.forms.description.PageDescription;
import org.eclipse.sirius.components.representations.GetOrCreateRandomIdProvider;
import org.eclipse.sirius.components.representations.VariableManager;
/**
* Aggregates all pages that can be created from the provided {@link FormDescription}s, according to all the given
* objects.
*
* @author fbarbin
*/
public class FormDescriptionAggregator {
public Optional<FormDescription> aggregate(List<PageDescription> pageDescriptions, List<Object> objects, IObjectService objectService) {
List<PageDescription> eligiblePageDescriptions = new ArrayList<>();
for (Object object : objects) {
VariableManager pageVariableManager = new VariableManager();
pageVariableManager.put(VariableManager.SELF, object);
pageVariableManager.put(FormVariableProvider.SELECTION.name(), objects);
eligiblePageDescriptions.addAll(pageDescriptions.stream()
.filter(pageDescription -> pageDescription.getCanCreatePredicate().test(pageVariableManager))
.toList());
}
if (eligiblePageDescriptions.isEmpty()) {
return Optional.empty();
}
Function<VariableManager, String> labelProvider = variableManager -> variableManager.get(VariableManager.SELF, Object.class)
.filter(self -> self instanceof List<?>)
.map(self -> (List<?>) self)
.flatMap(self -> self.stream().findFirst())
.map(objectService::getFullLabel)
.orElse("Properties");
Function<VariableManager, String> targetObjectIdProvider = variableManager -> variableManager.get(VariableManager.SELF, Object.class)
.filter(self -> self instanceof List<?>)
.map(self -> (List<?>) self)
.flatMap(self -> self.stream().findFirst())
.map(objectService::getId)
.orElse(null);
return Optional.of(FormDescription.newFormDescription(PropertiesEventProcessorFactory.DETAILS_VIEW_ID)
.label("Aggregated form description")
.idProvider(new GetOrCreateRandomIdProvider())
.labelProvider(labelProvider)
.targetObjectIdProvider(targetObjectIdProvider)
.canCreatePredicate(variableManager -> false)
.pageDescriptions(eligiblePageDescriptions)
.build());
}
}