1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.syncope.client.console.commons;
20
21 import java.util.List;
22 import java.util.Optional;
23 import java.util.Set;
24 import org.apache.syncope.client.ui.commons.Constants;
25 import org.apache.syncope.common.lib.Attr;
26 import org.apache.syncope.common.lib.to.AnyTO;
27 import org.apache.syncope.common.lib.types.SchemaType;
28 import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
29 import org.apache.wicket.model.IModel;
30
31 public class SortableAnyProviderComparator<T extends AnyTO> extends SortableDataProviderComparator<T> {
32
33 private static final long serialVersionUID = 1775967163571699258L;
34
35 private static final Set<String> INLINE_PROPS = Set.of(
36 Constants.KEY_FIELD_NAME, "status", "token", Constants.USERNAME_FIELD_NAME, Constants.NAME_FIELD_NAME);
37
38 public SortableAnyProviderComparator(final SortableDataProvider<T, String> provider) {
39 super(provider);
40 }
41
42 @Override
43 public int compare(final T any1, final T any2) {
44 if (INLINE_PROPS.contains(provider.getSort().getProperty())) {
45 return super.compare(any1, any2);
46 }
47
48 return super.compare(new AttrModel(any1), new AttrModel(any2));
49 }
50
51 @SuppressWarnings("rawtypes")
52 private class AttrModel implements IModel<Comparable> {
53
54 private static final long serialVersionUID = -7856686374020091808L;
55
56 private final AnyTO anyTO;
57
58 AttrModel(final AnyTO anyTO) {
59 super();
60 this.anyTO = anyTO;
61 }
62
63 @Override
64 public Comparable getObject() {
65 int hashPos = provider.getSort().getProperty().indexOf('#');
66
67 SchemaType schemaType = null;
68 final String schema;
69 if (hashPos == -1) {
70 schema = provider.getSort().getProperty();
71 } else {
72 String[] splitted = provider.getSort().getProperty().split("#");
73 try {
74 schemaType = SchemaType.valueOf(splitted[0]);
75 } catch (IllegalArgumentException e) {
76
77 }
78 schema = provider.getSort().getProperty().substring(hashPos + 1);
79 }
80
81 final Attr attr;
82 if (schemaType == null) {
83 attr = this.anyTO.getPlainAttr(schema).orElse(null);
84 } else {
85 switch (schemaType) {
86 case PLAIN:
87 default:
88 attr = this.anyTO.getPlainAttr(schema).orElse(null);
89 break;
90
91 case DERIVED:
92 attr = this.anyTO.getDerAttr(schema).orElse(null);
93 break;
94
95 case VIRTUAL:
96 attr = this.anyTO.getVirAttr(schema).orElse(null);
97 break;
98 }
99 }
100
101 Comparable result = null;
102
103 List<String> values = Optional.ofNullable(attr).map(Attr::getValues).orElse(null);
104 if (values != null && !values.isEmpty()) {
105 result = values.iterator().next();
106 }
107
108 return result;
109 }
110 }
111 }