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.common.lib.search;
20  
21  import java.lang.reflect.Field;
22  import java.util.Collection;
23  import java.util.Collections;
24  import java.util.HashSet;
25  import java.util.Map;
26  import java.util.Set;
27  import java.util.TreeMap;
28  import org.apache.commons.lang3.ArrayUtils;
29  import org.apache.syncope.common.lib.to.AnyObjectTO;
30  import org.apache.syncope.common.lib.to.AnyTO;
31  import org.apache.syncope.common.lib.to.GroupTO;
32  import org.apache.syncope.common.lib.to.UserTO;
33  import org.apache.syncope.common.lib.types.AttrSchemaType;
34  
35  public final class SearchableFields {
36  
37      private static final String[] ATTRIBUTES_NOTINCLUDED = {
38          "serialVersionUID", "discriminator", "password", "type", "udynMembershipCond", "securityAnswer",
39          "token", "tokenExpireTime"
40      };
41  
42      private static final Set<String> ANY_FIELDS = new HashSet<>();
43  
44      static {
45          ANY_FIELDS.addAll(get(UserTO.class).keySet());
46          ANY_FIELDS.addAll(get(GroupTO.class).keySet());
47          ANY_FIELDS.addAll(get(AnyObjectTO.class).keySet());
48      }
49  
50      public static boolean contains(final String schema) {
51          return ANY_FIELDS.contains(schema);
52      }
53  
54      public static Map<String, AttrSchemaType> get(final Class<? extends AnyTO> anyRef) {
55          final Map<String, AttrSchemaType> fields = new TreeMap<>(Collections.reverseOrder());
56  
57          // loop on class and all superclasses searching for field
58          Class<?> clazz = anyRef;
59          while (clazz != null && clazz != Object.class) {
60              for (Field field : clazz.getDeclaredFields()) {
61                  if (!ArrayUtils.contains(ATTRIBUTES_NOTINCLUDED, field.getName())
62                          && !Collection.class.isAssignableFrom(field.getType())
63                          && !Map.class.isAssignableFrom(field.getType())) {
64  
65                      fields.put(field.getName(), AttrSchemaType.getAttrSchemaTypeByClass(field.getType()));
66                  }
67              }
68              clazz = clazz.getSuperclass();
69          }
70  
71          return fields;
72      }
73  
74      private SearchableFields() {
75          // empty constructor for static utility class
76      }
77  }