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.core.persistence.jpa.dao;
20  
21  import org.apache.commons.lang3.builder.EqualsBuilder;
22  import org.apache.commons.lang3.builder.HashCodeBuilder;
23  import org.apache.syncope.common.lib.types.AnyTypeKind;
24  import org.apache.syncope.core.persistence.jpa.entity.anyobject.JPAAnyObject;
25  import org.apache.syncope.core.persistence.jpa.entity.group.JPAGroup;
26  import org.apache.syncope.core.persistence.jpa.entity.user.JPAUser;
27  
28  public class SearchSupport {
29  
30      public static class SearchView {
31  
32          protected String alias;
33  
34          protected String name;
35  
36          protected SearchView(final String alias, final String name) {
37              this.alias = alias;
38              this.name = name;
39          }
40  
41          @Override
42          public int hashCode() {
43              return new HashCodeBuilder().
44                      append(alias).
45                      append(name).
46                      build();
47          }
48  
49          @Override
50          public boolean equals(final Object obj) {
51              if (this == obj) {
52                  return true;
53              }
54              if (obj == null) {
55                  return false;
56              }
57              if (getClass() != obj.getClass()) {
58                  return false;
59              }
60              final SearchView other = (SearchView) obj;
61              return new EqualsBuilder().
62                      append(alias, other.alias).
63                      append(name, other.name).
64                      build();
65          }
66      }
67  
68      protected final AnyTypeKind anyTypeKind;
69  
70      protected boolean nonMandatorySchemas = false;
71  
72      public SearchSupport(final AnyTypeKind anyTypeKind) {
73          this.anyTypeKind = anyTypeKind;
74      }
75  
76      public SearchView table() {
77          String result;
78  
79          switch (anyTypeKind) {
80              case ANY_OBJECT:
81                  result = JPAAnyObject.TABLE;
82                  break;
83  
84              case GROUP:
85                  result = JPAGroup.TABLE;
86                  break;
87  
88              case USER:
89              default:
90                  result = JPAUser.TABLE;
91                  break;
92          }
93  
94          return new SearchView("t", result);
95      }
96  
97      public SearchView field() {
98          String result;
99  
100         switch (anyTypeKind) {
101             case ANY_OBJECT:
102                 result = "anyObject_search";
103                 break;
104 
105             case GROUP:
106                 result = "group_search";
107                 break;
108 
109             case USER:
110             default:
111                 result = "user_search";
112                 break;
113         }
114 
115         return new SearchView("sv", result);
116     }
117 
118     public SearchView relationship() {
119         String kind = anyTypeKind == AnyTypeKind.USER ? "u" : "a";
120         return new SearchView("sv" + kind + 'm', field().name + '_' + kind + "relationship");
121     }
122 
123     public SearchView membership() {
124         String kind = anyTypeKind == AnyTypeKind.USER ? "u" : "a";
125         return new SearchView("sv" + kind + 'm', field().name + '_' + kind + "membership");
126     }
127 
128     public SearchView dyngroupmembership() {
129         return new SearchView("sv" + anyTypeKind.name() + "dgm",
130                 anyTypeKind == AnyTypeKind.USER ? JPAGroupDAO.UDYNMEMB_TABLE : JPAGroupDAO.ADYNMEMB_TABLE);
131     }
132 
133     public SearchView role() {
134         return new SearchView("svr", field().name + "_role");
135     }
136 
137     public SearchView priv() {
138         return new SearchView("svp", field().name + "_priv");
139     }
140 
141     public SearchView dynpriv() {
142         return new SearchView("svdp", field().name + "_dynpriv");
143     }
144 
145     public static SearchView dynrolemembership() {
146         return new SearchView("svdr", JPARoleDAO.DYNMEMB_TABLE);
147     }
148 
149     public static SearchView dynrealmmembership() {
150         return new SearchView("svdrealm", JPADynRealmDAO.DYNMEMB_TABLE);
151     }
152 
153     public SearchView auxClass() {
154         return new SearchView("svac", field().name + "_auxClass");
155     }
156 
157     public SearchView resource() {
158         return new SearchView("svr", field().name + "_resource");
159     }
160 
161     public SearchView groupResource() {
162         return new SearchView("svrr", field().name + "_group_res");
163     }
164 
165     public SearchView entitlements() {
166         return new SearchView("sve", field().name + "_entitlements");
167     }
168 
169     SearchViewSupport asSearchViewSupport() {
170         if (this instanceof SearchViewSupport) {
171             return (SearchViewSupport) this;
172         }
173         throw new IllegalArgumentException("Not an " + SearchViewSupport.class + " instance");
174     }
175 }