Skip to content

Commit 4bd050e

Browse files
authored
fix(#3667): Enhance handling of AnyStaticProperty and RuntimeResolvableAnyStaticProperty in PipelineElementTemplateVisitor (#3668)
1 parent ab71605 commit 4bd050e

File tree

1 file changed

+80
-35
lines changed

1 file changed

+80
-35
lines changed

streampipes-pipeline-management/src/main/java/org/apache/streampipes/manager/template/PipelineElementTemplateVisitor.java

Lines changed: 80 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.apache.streampipes.model.staticproperty.MatchingStaticProperty;
2929
import org.apache.streampipes.model.staticproperty.OneOfStaticProperty;
3030
import org.apache.streampipes.model.staticproperty.Option;
31+
import org.apache.streampipes.model.staticproperty.RuntimeResolvableAnyStaticProperty;
3132
import org.apache.streampipes.model.staticproperty.RuntimeResolvableGroupStaticProperty;
3233
import org.apache.streampipes.model.staticproperty.RuntimeResolvableOneOfStaticProperty;
3334
import org.apache.streampipes.model.staticproperty.RuntimeResolvableTreeInputStaticProperty;
@@ -58,13 +59,40 @@ public PipelineElementTemplateVisitor(List<Map<String, Object>> configs) {
5859
@Override
5960
public void visit(AnyStaticProperty property) {
6061
if (hasConfig(property)) {
61-
List<String> value = getConfigValueAsStringList(property);
62-
property.getOptions().forEach(option -> {
63-
option.setSelected(value.stream().anyMatch(v -> v.equals(option.getName())));
64-
});
62+
var values = getConfigValueAsStringList(property);
63+
if (property instanceof RuntimeResolvableAnyStaticProperty) {
64+
this.handleRuntimeResolvableAnyStaticProperty(property, values);
65+
} else {
66+
this.handleAnyStaticProperty(property, values);
67+
}
6568
}
6669
}
6770

71+
/**
72+
* Creates options an option for each value provided in the valuse list.
73+
* Compact adapters do not call the runtime resovable endpoint, so we need to generate the options here
74+
*/
75+
private AnyStaticProperty handleRuntimeResolvableAnyStaticProperty(AnyStaticProperty property, List<String> values) {
76+
var options = new ArrayList<Option>();
77+
values.forEach(v -> {
78+
options.add(new Option(v, true));
79+
});
80+
property.setOptions(options);
81+
return property;
82+
}
83+
84+
/**
85+
* For AnyStaticProperties the options are already defined, therefore we only need to set the selected to true
86+
*/
87+
private AnyStaticProperty handleAnyStaticProperty(AnyStaticProperty property, List<String> values) {
88+
property.getOptions()
89+
.forEach(option -> {
90+
option.setSelected(values.stream()
91+
.anyMatch(v -> v.equals(option.getName())));
92+
});
93+
return property;
94+
}
95+
6896
@Override
6997
public void visit(CodeInputStaticProperty codeInputStaticProperty) {
7098
if (hasConfig(codeInputStaticProperty)) {
@@ -81,7 +109,8 @@ public void visit(CollectionStaticProperty collectionStaticProperty) {
81109
StaticProperty sp = new Cloner().staticProperty(collectionStaticProperty.getStaticPropertyTemplate());
82110
PipelineElementTemplateVisitor visitor = new PipelineElementTemplateVisitor(List.of(v));
83111
sp.accept(visitor);
84-
collectionStaticProperty.getMembers().add(sp);
112+
collectionStaticProperty.getMembers()
113+
.add(sp);
85114
});
86115
}
87116
}
@@ -132,10 +161,13 @@ public void visit(MatchingStaticProperty matchingStaticProperty) {
132161
public void visit(OneOfStaticProperty oneOfStaticProperty) {
133162
if (hasConfig(oneOfStaticProperty)) {
134163
String value = getConfigValueAsString(oneOfStaticProperty);
135-
oneOfStaticProperty.getOptions().forEach(option ->
136-
option.setSelected(option.getName().equals(value)));
164+
oneOfStaticProperty.getOptions()
165+
.forEach(option ->
166+
option.setSelected(option.getName()
167+
.equals(value)));
137168
if (oneOfStaticProperty instanceof RuntimeResolvableOneOfStaticProperty
138-
&& oneOfStaticProperty.getOptions().isEmpty()) {
169+
&& oneOfStaticProperty.getOptions()
170+
.isEmpty()) {
139171
oneOfStaticProperty.setOptions(List.of(new Option(value, true)));
140172
}
141173
}
@@ -172,11 +204,13 @@ public void visit(StaticPropertyAlternatives staticPropertyAlternatives) {
172204
if (hasConfig(staticPropertyAlternatives)) {
173205
Map<String, Object> values = getConfig(staticPropertyAlternatives);
174206
var selectedId = getConfigValueAsString(staticPropertyAlternatives);
175-
staticPropertyAlternatives.getAlternatives().forEach(a -> a.setSelected(false));
207+
staticPropertyAlternatives.getAlternatives()
208+
.forEach(a -> a.setSelected(false));
176209
staticPropertyAlternatives
177210
.getAlternatives()
178211
.stream()
179-
.filter(a -> a.getInternalName().equalsIgnoreCase(selectedId))
212+
.filter(a -> a.getInternalName()
213+
.equalsIgnoreCase(selectedId))
180214
.forEach(a -> {
181215
a.setSelected(true);
182216
PipelineElementTemplateVisitor visitor = new PipelineElementTemplateVisitor(List.of(values));
@@ -187,11 +221,12 @@ public void visit(StaticPropertyAlternatives staticPropertyAlternatives) {
187221

188222
@Override
189223
public void visit(StaticPropertyGroup staticPropertyGroup) {
190-
staticPropertyGroup.getStaticProperties().forEach(group -> {
191-
PipelineElementTemplateVisitor visitor =
192-
new PipelineElementTemplateVisitor(configs);
193-
group.accept(visitor);
194-
});
224+
staticPropertyGroup.getStaticProperties()
225+
.forEach(group -> {
226+
PipelineElementTemplateVisitor visitor =
227+
new PipelineElementTemplateVisitor(configs);
228+
group.accept(visitor);
229+
});
195230
}
196231

197232
@Override
@@ -211,11 +246,12 @@ public void visit(RuntimeResolvableTreeInputStaticProperty property) {
211246

212247
@Override
213248
public void visit(RuntimeResolvableGroupStaticProperty staticPropertyGroup) {
214-
staticPropertyGroup.getStaticProperties().forEach(group -> {
215-
PipelineElementTemplateVisitor visitor =
216-
new PipelineElementTemplateVisitor(configs);
217-
group.accept(visitor);
218-
});
249+
staticPropertyGroup.getStaticProperties()
250+
.forEach(group -> {
251+
PipelineElementTemplateVisitor visitor =
252+
new PipelineElementTemplateVisitor(configs);
253+
group.accept(visitor);
254+
});
219255
}
220256

221257

@@ -231,16 +267,20 @@ private Map<String, Object> getConfig(String key) {
231267
.orElseThrow(() -> new IllegalArgumentException(String.format("No key found: %s", key)));
232268
}
233269

234-
private boolean hasKeyCaseInsensitive(String internalName,
235-
Map<String, Object> templateConfig) {
270+
private boolean hasKeyCaseInsensitive(
271+
String internalName,
272+
Map<String, Object> templateConfig
273+
) {
236274
return templateConfig
237275
.entrySet()
238276
.stream()
239-
.anyMatch(entry -> entry.getKey().equalsIgnoreCase(internalName));
277+
.anyMatch(entry -> entry.getKey()
278+
.equalsIgnoreCase(internalName));
240279
}
241280

242281
private boolean hasConfig(StaticProperty sp) {
243-
return configs.stream().anyMatch(c -> hasKeyCaseInsensitive(sp.getInternalName(), c));
282+
return configs.stream()
283+
.anyMatch(c -> hasKeyCaseInsensitive(sp.getInternalName(), c));
244284
}
245285

246286
private String getConfigValueAsString(StaticProperty sp) {
@@ -261,22 +301,26 @@ private List<String> getConfigValueAsStringList(StaticProperty sp) {
261301
return getValueAsStringList(getConfig(sp), sp.getInternalName());
262302
}
263303

264-
private String getCaseInsensitiveKey(Map<String, Object> config,
265-
String key) {
266-
return config.keySet().stream()
267-
.filter(k -> k.equalsIgnoreCase(key))
268-
.findFirst()
269-
.orElseThrow(() -> new IllegalArgumentException("Key not found: " + key));
304+
private String getCaseInsensitiveKey(
305+
Map<String, Object> config,
306+
String key
307+
) {
308+
return config.keySet()
309+
.stream()
310+
.filter(k -> k.equalsIgnoreCase(key))
311+
.findFirst()
312+
.orElseThrow(() -> new IllegalArgumentException("Key not found: " + key));
270313
}
271314

272315
private List<String> getValueAsStringList(Map<String, Object> config, String key) {
273316
String caseInsensitiveKey = getCaseInsensitiveKey(config, key);
274317

275318
return Optional.ofNullable(config.get(caseInsensitiveKey))
276-
.filter(value -> value instanceof List<?>)
277-
.map(value -> ((List<?>) value).stream())
278-
.map(s -> s.map(String.class::cast).collect(Collectors.toList()))
279-
.orElseThrow(() -> new IllegalArgumentException("Value is not a List<String>"));
319+
.filter(value -> value instanceof List<?>)
320+
.map(value -> ((List<?>) value).stream())
321+
.map(s -> s.map(String.class::cast)
322+
.collect(Collectors.toList()))
323+
.orElseThrow(() -> new IllegalArgumentException("Value is not a List<String>"));
280324
}
281325

282326
private List<Map<String, Object>> getConfigValueAsList(StaticProperty sp) {
@@ -290,7 +334,8 @@ private List<Map<String, Object>> getConfigValueAsList(StaticProperty sp) {
290334

291335
private List<Map<String, Object>> getCaseInsensitiveList(Map<String, Object> map, String key) {
292336
for (Map.Entry<String, Object> entry : map.entrySet()) {
293-
if (entry.getKey().equalsIgnoreCase(key)) {
337+
if (entry.getKey()
338+
.equalsIgnoreCase(key)) {
294339
return (List<Map<String, Object>>) entry.getValue();
295340
}
296341
}

0 commit comments

Comments
 (0)