1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.service.persistence;
24  
25  import com.liferay.portal.NoSuchUserGroupException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.util.StringMaker;
28  import com.liferay.portal.kernel.util.StringPool;
29  import com.liferay.portal.kernel.util.StringUtil;
30  import com.liferay.portal.kernel.util.Validator;
31  import com.liferay.portal.model.UserGroup;
32  import com.liferay.portal.model.impl.UserGroupImpl;
33  import com.liferay.portal.spring.hibernate.CustomSQLUtil;
34  import com.liferay.portal.spring.hibernate.FinderCache;
35  import com.liferay.portal.spring.hibernate.HibernateUtil;
36  import com.liferay.util.dao.hibernate.QueryPos;
37  import com.liferay.util.dao.hibernate.QueryUtil;
38  
39  import java.util.Iterator;
40  import java.util.LinkedHashMap;
41  import java.util.List;
42  import java.util.Map;
43  
44  import org.hibernate.Hibernate;
45  import org.hibernate.SQLQuery;
46  import org.hibernate.Session;
47  
48  /**
49   * <a href="UserGroupFinder.java.html"><b><i>View Source</i></b></a>
50   *
51   * @author Charles May
52   *
53   */
54  public class UserGroupFinder {
55  
56      public static String COUNT_BY_C_N_D =
57          UserGroupFinder.class.getName() + ".countByC_N_D";
58  
59      public static String FIND_BY_C_N =
60          UserGroupFinder.class.getName() + ".findByC_N";
61  
62      public static String FIND_BY_C_N_D =
63          UserGroupFinder.class.getName() + ".findByC_N_D";
64  
65      public static String JOIN_BY_GROUPS_PERMISSIONS =
66          UserGroupFinder.class.getName() + ".joinByGroupsPermissions";
67  
68      public static String JOIN_BY_USER_GROUPS_GROUPS =
69          UserGroupFinder.class.getName() + ".joinByUserGroupsGroups";
70  
71      public static String JOIN_BY_USER_GROUPS_ROLES =
72          UserGroupFinder.class.getName() + ".joinByUserGroupsRoles";
73  
74      public static int countByC_N_D(
75              long companyId, String name, String description,
76              LinkedHashMap params)
77          throws SystemException {
78  
79          name = StringUtil.lowerCase(name);
80          description = StringUtil.lowerCase(description);
81  
82          Session session = null;
83  
84          try {
85              session = HibernateUtil.openSession();
86  
87              String sql = CustomSQLUtil.get(COUNT_BY_C_N_D);
88  
89              sql = StringUtil.replace(sql, "[$JOIN$]", _getJoin(params));
90              sql = StringUtil.replace(sql, "[$WHERE$]", _getWhere(params));
91  
92              SQLQuery q = session.createSQLQuery(sql);
93  
94              q.addScalar(HibernateUtil.getCountColumnName(), Hibernate.LONG);
95  
96              QueryPos qPos = QueryPos.getInstance(q);
97  
98              _setJoin(qPos, params);
99              qPos.add(companyId);
100             qPos.add(name);
101             qPos.add(name);
102             qPos.add(description);
103             qPos.add(description);
104 
105             Iterator itr = q.list().iterator();
106 
107             if (itr.hasNext()) {
108                 Long count = (Long)itr.next();
109 
110                 if (count != null) {
111                     return count.intValue();
112                 }
113             }
114 
115             return 0;
116         }
117         catch (Exception e) {
118             throw new SystemException(e);
119         }
120         finally {
121             HibernateUtil.closeSession(session);
122         }
123     }
124 
125     public static UserGroup findByC_N(long companyId, String name)
126         throws NoSuchUserGroupException, SystemException {
127 
128         name = StringUtil.lowerCase(name);
129 
130         String finderClassName = UserGroup.class.getName();
131         String finderMethodName = "customFindByC_N";
132         String finderParams[] = new String[] {
133             Long.class.getName(), String.class.getName()
134         };
135         Object finderArgs[] = new Object[] {new Long(companyId), name};
136 
137         Object result = FinderCache.getResult(
138             finderClassName, finderMethodName, finderParams, finderArgs);
139 
140         if (result == null) {
141             Session session = null;
142 
143             try {
144                 session = HibernateUtil.openSession();
145 
146                 String sql = CustomSQLUtil.get(FIND_BY_C_N);
147 
148                 SQLQuery q = session.createSQLQuery(sql);
149 
150                 q.addEntity("UserGroup", UserGroupImpl.class);
151 
152                 QueryPos qPos = QueryPos.getInstance(q);
153 
154                 qPos.add(companyId);
155                 qPos.add(name);
156 
157                 Iterator itr = q.list().iterator();
158 
159                 if (itr.hasNext()) {
160                     UserGroup userGroup = (UserGroup)itr.next();
161 
162                     FinderCache.putResult(
163                         finderClassName, finderMethodName, finderParams,
164                         finderArgs, userGroup);
165 
166                     return userGroup;
167                 }
168             }
169             catch (Exception e) {
170                 throw new SystemException(e);
171             }
172             finally {
173                 HibernateUtil.closeSession(session);
174             }
175 
176             throw new NoSuchUserGroupException(
177                 "No UserGroup exists with the key {companyId=" + companyId +
178                     ", name=" + name + "}");
179         }
180         else {
181             return (UserGroup)result;
182         }
183     }
184 
185     public static List findByC_N_D(
186             long companyId, String name, String description,
187             LinkedHashMap params, int begin, int end)
188         throws SystemException {
189 
190         name = StringUtil.lowerCase(name);
191         description = StringUtil.lowerCase(description);
192 
193         Session session = null;
194 
195         try {
196             session = HibernateUtil.openSession();
197 
198             String sql = CustomSQLUtil.get(FIND_BY_C_N_D);
199 
200             sql = StringUtil.replace(sql, "[$JOIN$]", _getJoin(params));
201             sql = StringUtil.replace(sql, "[$WHERE$]", _getWhere(params));
202 
203             SQLQuery q = session.createSQLQuery(sql);
204 
205             q.addEntity("UserGroup", UserGroupImpl.class);
206 
207             QueryPos qPos = QueryPos.getInstance(q);
208 
209             _setJoin(qPos, params);
210             qPos.add(companyId);
211             qPos.add(name);
212             qPos.add(name);
213             qPos.add(description);
214             qPos.add(description);
215 
216             return QueryUtil.list(q, HibernateUtil.getDialect(), begin, end);
217         }
218         catch (Exception e) {
219             throw new SystemException(e);
220         }
221         finally {
222             HibernateUtil.closeSession(session);
223         }
224     }
225 
226     private static String _getJoin(LinkedHashMap params) {
227         if (params == null) {
228             return StringPool.BLANK;
229         }
230 
231         StringMaker sm = new StringMaker();
232 
233         Iterator itr = params.entrySet().iterator();
234 
235         while (itr.hasNext()) {
236             Map.Entry entry = (Map.Entry)itr.next();
237 
238             String key = (String)entry.getKey();
239             Object value = entry.getValue();
240 
241             if (Validator.isNotNull(value)) {
242                 sm.append(_getJoin(key));
243             }
244         }
245 
246         return sm.toString();
247     }
248 
249     private static String _getJoin(String key) {
250         String join = StringPool.BLANK;
251 
252         if (key.equals("permissionsResourceId")) {
253             join = CustomSQLUtil.get(JOIN_BY_GROUPS_PERMISSIONS);
254         }
255         else if (key.equals("userGroupsGroups")) {
256             join = CustomSQLUtil.get(JOIN_BY_USER_GROUPS_GROUPS);
257         }
258         else if (key.equals("userGroupsRoles")) {
259             join = CustomSQLUtil.get(JOIN_BY_USER_GROUPS_ROLES);
260         }
261 
262         if (Validator.isNotNull(join)) {
263             int pos = join.indexOf("WHERE");
264 
265             if (pos != -1) {
266                 join = join.substring(0, pos);
267             }
268         }
269 
270         return join;
271     }
272 
273     private static String _getWhere(LinkedHashMap params) {
274         if (params == null) {
275             return StringPool.BLANK;
276         }
277 
278         StringMaker sm = new StringMaker();
279 
280         Iterator itr = params.entrySet().iterator();
281 
282         while (itr.hasNext()) {
283             Map.Entry entry = (Map.Entry)itr.next();
284 
285             String key = (String)entry.getKey();
286             Object value = entry.getValue();
287 
288             if (Validator.isNotNull(value)) {
289                 sm.append(_getWhere(key));
290             }
291         }
292 
293         return sm.toString();
294     }
295 
296     private static String _getWhere(String key) {
297         String join = StringPool.BLANK;
298 
299         if (key.equals("permissionsResourceId")) {
300             join = CustomSQLUtil.get(JOIN_BY_GROUPS_PERMISSIONS);
301         }
302         else if (key.equals("userGroupsGroups")) {
303             join = CustomSQLUtil.get(JOIN_BY_USER_GROUPS_GROUPS);
304         }
305         else if (key.equals("userGroupsRoles")) {
306             join = CustomSQLUtil.get(JOIN_BY_USER_GROUPS_ROLES);
307         }
308 
309         if (Validator.isNotNull(join)) {
310             int pos = join.indexOf("WHERE");
311 
312             if (pos != -1) {
313                 join = join.substring(pos + 5, join.length()) + " AND ";
314             }
315         }
316 
317         return join;
318     }
319 
320     private static void _setJoin(QueryPos qPos, LinkedHashMap params) {
321         if (params != null) {
322             Iterator itr = params.entrySet().iterator();
323 
324             while (itr.hasNext()) {
325                 Map.Entry entry = (Map.Entry)itr.next();
326 
327                 Object value = entry.getValue();
328 
329                 if (value instanceof Long) {
330                     Long valueLong = (Long)value;
331 
332                     if (Validator.isNotNull(valueLong)) {
333                         qPos.add(valueLong);
334                     }
335                 }
336                 else if (value instanceof String) {
337                     String valueString = (String)value;
338 
339                     if (Validator.isNotNull(valueString)) {
340                         qPos.add(valueString);
341                     }
342                 }
343             }
344         }
345     }
346 
347 }