Skip to content

Commit 3daca7b

Browse files
mdisabatinoilgrosso
authored andcommitted
[SYNCOPE-1859] SearchPanel displays the schema keys and doesn't consider translations
1 parent 18965b4 commit 3daca7b

File tree

2 files changed

+97
-30
lines changed

2 files changed

+97
-30
lines changed

client/idrepo/common-ui/src/main/java/org/apache/syncope/client/ui/commons/markup/html/form/AjaxTextFieldPanel.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import de.agilecoders.wicket.core.markup.html.bootstrap.components.TooltipConfig;
2222
import java.util.Iterator;
2323
import java.util.List;
24+
import java.util.Optional;
2425
import java.util.regex.Pattern;
2526
import org.apache.syncope.client.ui.commons.Constants;
2627
import org.apache.syncope.client.ui.commons.ajax.form.IndicatorAjaxFormComponentUpdatingBehavior;
@@ -33,6 +34,7 @@
3334
import org.apache.wicket.extensions.ajax.markup.html.autocomplete.IAutoCompleteRenderer;
3435
import org.apache.wicket.model.IModel;
3536
import org.apache.wicket.model.ResourceModel;
37+
import org.apache.wicket.util.convert.IConverter;
3638

3739
public class AjaxTextFieldPanel extends TextFieldPanel implements Cloneable {
3840

@@ -82,6 +84,14 @@ protected Iterator<String> getChoices(final String input) {
8284
}
8385
};
8486
}
87+
88+
@SuppressWarnings("unchecked")
89+
@Override
90+
public <C> IConverter<C> getConverter(final Class<C> type) {
91+
return AjaxTextFieldPanel.this.getConverter().
92+
map(converter -> (IConverter<C>)converter).
93+
orElseGet(() -> super.getConverter(type));
94+
}
8595
};
8696
setHTMLInputNotAllowed();
8797
add(field.setLabel(new ResourceModel(name, name)).setOutputMarkupId(true));
@@ -105,6 +115,10 @@ public void setChoices(final List<String> choices) {
105115
}
106116
}
107117

118+
protected Optional<IConverter<String>> getConverter() {
119+
return Optional.empty();
120+
}
121+
108122
public FieldPanel<String> enableJexlHelp() {
109123
questionMarkJexlHelp.setVisible(true);
110124
return this;

client/idrepo/console/src/main/java/org/apache/syncope/client/console/panels/search/SearchClausePanel.java

Lines changed: 83 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@
2222
import de.agilecoders.wicket.extensions.markup.html.bootstrap.form.checkbox.bootstraptoggle.BootstrapToggleConfig;
2323
import java.io.Serializable;
2424
import java.text.ParseException;
25-
import java.util.ArrayList;
2625
import java.util.Arrays;
2726
import java.util.Date;
2827
import java.util.List;
28+
import java.util.Locale;
2929
import java.util.Map;
3030
import java.util.Optional;
3131
import java.util.function.Consumer;
@@ -35,6 +35,7 @@
3535
import org.apache.commons.lang3.time.DateFormatUtils;
3636
import org.apache.commons.lang3.time.FastDateFormat;
3737
import org.apache.commons.lang3.tuple.Pair;
38+
import org.apache.syncope.client.console.SyncopeConsoleSession;
3839
import org.apache.syncope.client.console.panels.search.SearchClause.Comparator;
3940
import org.apache.syncope.client.console.panels.search.SearchClause.Operator;
4041
import org.apache.syncope.client.console.panels.search.SearchClause.Type;
@@ -52,7 +53,6 @@
5253
import org.apache.syncope.common.lib.SyncopeConstants;
5354
import org.apache.syncope.common.lib.to.GroupTO;
5455
import org.apache.syncope.common.lib.to.PlainSchemaTO;
55-
import org.apache.syncope.common.lib.to.RelationshipTypeTO;
5656
import org.apache.syncope.common.lib.types.AttrSchemaType;
5757
import org.apache.wicket.AttributeModifier;
5858
import org.apache.wicket.Component;
@@ -76,6 +76,9 @@
7676
import org.apache.wicket.model.Model;
7777
import org.apache.wicket.model.PropertyModel;
7878
import org.apache.wicket.spring.injection.annot.SpringBean;
79+
import org.apache.wicket.util.convert.ConversionException;
80+
import org.apache.wicket.util.convert.IConverter;
81+
import org.springframework.util.CollectionUtils;
7982

8083
public class SearchClausePanel extends FieldPanel<SearchClause> {
8184

@@ -121,15 +124,15 @@ default List<String> properties() {
121124
default void setFieldAccess(
122125
FieldPanel<String> value,
123126
AjaxTextFieldPanel property,
124-
LoadableDetachableModel<List<String>> properties) {
127+
LoadableDetachableModel<List<Pair<String, String>>> properties) {
125128

126129
value.setEnabled(true);
127130
value.setModelObject(StringUtils.EMPTY);
128131
property.setEnabled(true);
129132

130133
// reload properties list
131134
properties.detach();
132-
property.setChoices(properties.getObject());
135+
property.setChoices(properties.getObject().stream().map(Pair::getKey).collect(Collectors.toList()));
133136
}
134137
}
135138

@@ -163,7 +166,7 @@ default void setFieldAccess(
163166

164167
protected final LoadableDetachableModel<List<Comparator>> comparators;
165168

166-
protected final LoadableDetachableModel<List<String>> properties;
169+
protected final LoadableDetachableModel<List<Pair<String, String>>> properties;
167170

168171
protected final Fragment operatorFragment;
169172

@@ -277,46 +280,63 @@ protected List<Comparator> load() {
277280
private static final long serialVersionUID = 5275935387613157437L;
278281

279282
@Override
280-
protected List<String> load() {
283+
protected List<Pair<String, String>> load() {
281284
if (field.getModel().getObject() == null || field.getModel().getObject().getType() == null) {
282285
return List.of();
283286
}
284287

285288
switch (field.getModel().getObject().getType()) {
286289
case ATTRIBUTE:
287-
List<String> names = new ArrayList<>(dnames.getObject().keySet());
288-
if (anames != null && anames.getObject() != null && !anames.getObject().isEmpty()) {
289-
names.addAll(anames.getObject().keySet());
290+
Locale locale = SyncopeConsoleSession.get().getLocale();
291+
List<Pair<String, String>> names = dnames.getObject().entrySet().stream().
292+
map(item -> Pair.of(
293+
item.getKey(),
294+
Optional.ofNullable(item.getValue().getLabel(locale)).orElse(item.getKey()))).
295+
collect(Collectors.toList());
296+
if (anames != null && !CollectionUtils.isEmpty(anames.getObject())) {
297+
names.addAll(anames.getObject().entrySet().stream().
298+
map(item -> Pair.of(
299+
item.getKey(),
300+
Optional.ofNullable(item.getValue().getLabel(locale)).orElse(item.getKey()))).
301+
collect(Collectors.toList()));
290302
}
291-
return names.stream().sorted().collect(Collectors.toList());
303+
return names.stream().
304+
sorted(java.util.Comparator.comparing(name -> name.getValue().toLowerCase())).
305+
collect(Collectors.toList());
292306

293307
case GROUP_MEMBERSHIP:
294-
return groupInfo.getLeft().getObject();
308+
return groupInfo.getLeft().getObject().stream().
309+
map(item -> Pair.of(item, item)).collect(Collectors.toList());
295310

296311
case ROLE_MEMBERSHIP:
297312
return Optional.ofNullable(roleNames).
298-
map(r -> r.getObject().stream().sorted().collect(Collectors.toList())).
313+
map(r -> r.getObject().stream().sorted().map(item -> Pair.of(item, item)).
314+
collect(Collectors.toList())).
299315
orElse(List.of());
300316

301317
case PRIVILEGE:
302318
return Optional.ofNullable(privilegeNames).
303-
map(p -> p.getObject().stream().sorted().collect(Collectors.toList())).
319+
map(p -> p.getObject().stream().sorted().map(item -> Pair.of(item, item)).
320+
collect(Collectors.toList())).
304321
orElse(List.of());
305322

306323
case AUX_CLASS:
307-
return auxClassNames.getObject().stream().
308-
sorted().collect(Collectors.toList());
324+
return auxClassNames.getObject().stream().sorted().
325+
map(item -> Pair.of(item, item)).collect(Collectors.toList());
309326

310327
case RESOURCE:
311-
return resourceNames.getObject().stream().
312-
sorted().collect(Collectors.toList());
328+
return resourceNames.getObject().stream().sorted().
329+
map(item -> Pair.of(item, item)).collect(Collectors.toList());
313330

314331
case RELATIONSHIP:
315-
return relationshipTypeRestClient.list().stream().
316-
map(RelationshipTypeTO::getKey).collect(Collectors.toList());
332+
return relationshipTypeRestClient.list().stream().sorted().
333+
map(item -> Pair.of(item.getKey(), item.getKey())).
334+
collect(Collectors.toList());
317335

318336
case CUSTOM:
319-
return customizer.properties();
337+
return customizer.properties().stream().
338+
map(item -> Pair.of(item, item)).
339+
collect(Collectors.toList());
320340

321341
default:
322342
return List.of();
@@ -439,10 +459,40 @@ protected void onUpdate(final AjaxRequestTarget target) {
439459
operatorContainer.add(searchButtonFragment);
440460
}
441461

442-
AjaxTextFieldPanel property = new AjaxTextFieldPanel(
443-
"property", "property", new PropertyModel<>(searchClause, "property"), true);
462+
AjaxTextFieldPanel property = new AjaxTextFieldPanel("property", "property",
463+
new PropertyModel<>(searchClause, "property"), true) {
464+
465+
private static final long serialVersionUID = -7157802546272668001L;
466+
467+
@Override
468+
protected Optional<IConverter<String>> getConverter() {
469+
return Optional.of(new IConverter<String>() {
470+
471+
private static final long serialVersionUID = -2754107934642211828L;
472+
473+
@Override
474+
public String convertToObject(final String label, final Locale locale) throws ConversionException {
475+
return properties.getObject().stream().
476+
filter(property -> property.getValue().equalsIgnoreCase(label)).
477+
findFirst().
478+
map(Pair::getKey).
479+
orElse(label);
480+
}
481+
482+
@Override
483+
public String convertToString(final String key, final Locale locale) {
484+
return properties.getObject().stream().
485+
filter(property -> property.getKey().equalsIgnoreCase(key)).
486+
findFirst().
487+
map(Pair::getValue).
488+
orElse(key);
489+
}
490+
});
491+
}
492+
};
493+
444494
property.hideLabel().setOutputMarkupId(true).setEnabled(true);
445-
property.setChoices(properties.getObject());
495+
property.setChoices(properties.getObject().stream().map(Pair::getValue).collect(Collectors.toList()));
446496
field.add(property);
447497

448498
property.getField().add(PREVENT_DEFAULT_RETURN);
@@ -457,7 +507,8 @@ protected void onEvent(final AjaxRequestTarget target) {
457507

458508
String[] inputAsArray = property.getField().getInputAsArray();
459509
if (ArrayUtils.isEmpty(inputAsArray)) {
460-
property.setChoices(properties.getObject());
510+
property.setChoices(properties.getObject().stream().map(Pair::getKey)
511+
.collect(Collectors.toList()));
461512
} else if (groupInfo.getRight().getObject() > Constants.MAX_GROUP_LIST_SIZE) {
462513
String inputValue = inputAsArray.length > 1 && inputAsArray[1] != null
463514
? inputAsArray[1]
@@ -621,7 +672,9 @@ private void setFieldAccess(
621672

622673
// reload properties list
623674
properties.detach();
624-
property.setChoices(properties.getObject());
675+
property.setChoices(
676+
properties.getObject().stream().
677+
map(Pair::getValue).collect(Collectors.toList()));
625678
break;
626679

627680
case ROLE_MEMBERSHIP:
@@ -630,7 +683,7 @@ private void setFieldAccess(
630683

631684
// reload properties list
632685
properties.detach();
633-
property.setChoices(properties.getObject());
686+
property.setChoices(properties.getObject().stream().map(Pair::getKey).collect(Collectors.toList()));
634687
break;
635688

636689
case PRIVILEGE:
@@ -639,7 +692,7 @@ private void setFieldAccess(
639692

640693
// reload properties list
641694
properties.detach();
642-
property.setChoices(properties.getObject());
695+
property.setChoices(properties.getObject().stream().map(Pair::getKey).collect(Collectors.toList()));
643696
break;
644697

645698
case GROUP_MEMBERSHIP:
@@ -648,7 +701,7 @@ private void setFieldAccess(
648701

649702
// reload properties list
650703
properties.detach();
651-
property.setChoices(properties.getObject());
704+
property.setChoices(properties.getObject().stream().map(Pair::getKey).collect(Collectors.toList()));
652705
break;
653706

654707
case GROUP_MEMBER:
@@ -664,7 +717,7 @@ private void setFieldAccess(
664717

665718
// reload properties list
666719
properties.detach();
667-
property.setChoices(properties.getObject());
720+
property.setChoices(properties.getObject().stream().map(Pair::getKey).collect(Collectors.toList()));
668721
break;
669722

670723
case RELATIONSHIP:
@@ -674,7 +727,7 @@ private void setFieldAccess(
674727

675728
// reload properties list
676729
properties.detach();
677-
property.setChoices(properties.getObject());
730+
property.setChoices(properties.getObject().stream().map(Pair::getKey).collect(Collectors.toList()));
678731
break;
679732

680733
case CUSTOM:

0 commit comments

Comments
 (0)