View Javadoc
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.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                      // this should never happen
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 }