9a5d86ab01f8cf2e98e7fab8a5e82756eb0e2cf2
[syncope.git] / client / console / src / main / java / org / apache / syncope / client / console / commons / SortableAnyProviderComparator.java
1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.apache.syncope.client.console.commons;
20
21 import java.util.Arrays;
22 import java.util.HashSet;
23 import java.util.List;
24 import java.util.Set;
25 import org.apache.syncope.common.lib.to.AnyTO;
26 import org.apache.syncope.common.lib.to.AttrTO;
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 = new HashSet<>(Arrays.asList(
36 new String[] { "key", "status", "token", "username", "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 /**
64 * @see UserAttrColumn constructor
65 */
66 @Override
67 public Comparable getObject() {
68 int hashPos = provider.getSort().getProperty().indexOf('#');
69
70 SchemaType schemaType = null;
71 final String schema;
72 if (hashPos == -1) {
73 schema = provider.getSort().getProperty();
74 } else {
75 String[] splitted = provider.getSort().getProperty().split("#");
76 try {
77 schemaType = SchemaType.valueOf(splitted[0]);
78 } catch (IllegalArgumentException e) {
79 // this should never happen
80 }
81 schema = provider.getSort().getProperty().substring(hashPos + 1);
82 }
83
84 final AttrTO attr;
85 if (schemaType == null) {
86 attr = this.anyTO.getPlainAttr(schema).get();
87 } else {
88 switch (schemaType) {
89 case PLAIN:
90 default:
91 attr = this.anyTO.getPlainAttr(schema).get();
92 break;
93
94 case DERIVED:
95 attr = this.anyTO.getDerAttr(schema).get();
96 break;
97
98 case VIRTUAL:
99 attr = this.anyTO.getVirAttr(schema).get();
100 break;
101 }
102 }
103
104 Comparable result = null;
105
106 List<String> values = attr == null ? null : attr.getValues();
107 if (values != null && !values.isEmpty()) {
108 result = values.iterator().next();
109 }
110
111 return result;
112 }
113 }
114 }