1   /**
2    * Copyright (c) 2000-2008 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.SystemException;
26  import com.liferay.portal.kernel.util.StringMaker;
27  import com.liferay.portal.model.User;
28  import com.liferay.portal.spring.hibernate.CustomSQLUtil;
29  import com.liferay.portal.spring.hibernate.HibernateUtil;
30  import com.liferay.util.dao.hibernate.QueryPos;
31  import com.liferay.util.dao.hibernate.QueryUtil;
32  
33  import java.util.ArrayList;
34  import java.util.Iterator;
35  import java.util.List;
36  
37  import org.hibernate.Hibernate;
38  import org.hibernate.SQLQuery;
39  import org.hibernate.Session;
40  
41  /**
42   * <a href="PermissionUserFinderImpl.java.html"><b><i>View Source</i></b></a>
43   *
44   * @author Charles May
45   *
46   */
47  public class PermissionUserFinderImpl implements PermissionUserFinder {
48  
49      public static String COUNT_BY_ADMIN_ROLE =
50          PermissionUserFinder.class.getName() + ".countByAdminRole";
51  
52      public static String COUNT_BY_GROUP_PERMISSION =
53          PermissionUserFinder.class.getName() + ".countByGroupPermission";
54  
55      public static String COUNT_BY_GROUP_ROLE =
56          PermissionUserFinder.class.getName() + ".countByGroupRole";
57  
58      public static String COUNT_BY_ORG_GROUP_PERMISSION =
59          PermissionUserFinder.class.getName() + ".countByOrgGroupPermission";
60  
61      public static String COUNT_BY_ORG_GROUP_PERMISSIONS =
62          PermissionUserFinder.class.getName() + ".countByOrgGroupPermissions";
63  
64      public static String COUNT_BY_ORG_PERMISSION =
65          PermissionUserFinder.class.getName() + ".countByOrgPermission";
66  
67      public static String COUNT_BY_ORG_ROLE =
68          PermissionUserFinder.class.getName() + ".countByOrgRole";
69  
70      public static String COUNT_BY_USER_PERMISSION =
71          PermissionUserFinder.class.getName() + ".countByUserPermission";
72  
73      public static String COUNT_BY_USER_ROLE =
74          PermissionUserFinder.class.getName() + ".countByUserRole";
75  
76      public static String FIND_BY_ADMIN_ROLE =
77          PermissionUserFinder.class.getName() + ".findByAdminRole";
78  
79      public static String FIND_BY_GROUP_PERMISSION =
80          PermissionUserFinder.class.getName() + ".findByGroupPermission";
81  
82      public static String FIND_BY_GROUP_ROLE =
83          PermissionUserFinder.class.getName() + ".findByGroupRole";
84  
85      public static String FIND_BY_ORG_GROUP_PERMISSION =
86          PermissionUserFinder.class.getName() + ".findByOrgGroupPermission";
87  
88      public static String FIND_BY_ORG_PERMISSION =
89          PermissionUserFinder.class.getName() + ".findByOrgPermission";
90  
91      public static String FIND_BY_ORG_ROLE =
92          PermissionUserFinder.class.getName() + ".findByOrgRole";
93  
94      public static String FIND_BY_USER_PERMISSION =
95          PermissionUserFinder.class.getName() + ".findByUserPermission";
96  
97      public static String FIND_BY_USER_ROLE =
98          PermissionUserFinder.class.getName() + ".findByUserRole";
99  
100     public static int COUNT_USERS_TYPE_ADMIN = 1;
101 
102     public static int COUNT_USERS_TYPE_PERMISSION = 2;
103 
104     public static int COUNT_USERS_TYPE_ROLE = 3;
105 
106     public int countByOrgGroupPermissions(
107             long companyId, String name, String primKey, String actionId)
108         throws SystemException {
109 
110         Session session = null;
111 
112         try {
113             session = HibernateUtil.openSession();
114 
115             String sql = CustomSQLUtil.get(COUNT_BY_ORG_GROUP_PERMISSIONS);
116 
117             SQLQuery q = session.createSQLQuery(sql);
118 
119             q.addScalar(HibernateUtil.getCountColumnName(), Hibernate.LONG);
120 
121             QueryPos qPos = QueryPos.getInstance(q);
122 
123             qPos.add(companyId);
124             qPos.add(name);
125             qPos.add(primKey);
126             qPos.add(actionId);
127 
128             Iterator itr = q.list().iterator();
129 
130             if (itr.hasNext()) {
131                 Long count = (Long)itr.next();
132 
133                 if (count != null) {
134                     return count.intValue();
135                 }
136             }
137 
138             return 0;
139         }
140         catch (Exception e) {
141             throw new SystemException(e);
142         }
143         finally {
144             HibernateUtil.closeSession(session);
145         }
146     }
147 
148     public int countByPermissionAndRole(
149             long companyId, long groupId, String name, String primKey,
150             String actionId, String firstName, String middleName,
151             String lastName, String emailAddress, boolean andOperator)
152         throws SystemException {
153 
154         Session session = null;
155 
156         try {
157             session = HibernateUtil.openSession();
158 
159             int count = countUsers(
160                 session, CustomSQLUtil.get(COUNT_BY_ADMIN_ROLE), companyId,
161                 groupId, name, primKey, actionId, firstName, middleName,
162                 lastName, emailAddress, andOperator, COUNT_USERS_TYPE_ADMIN);
163 
164             count += countUsers(
165                 session, CustomSQLUtil.get(COUNT_BY_USER_PERMISSION), companyId,
166                 groupId, name, primKey, actionId, firstName, middleName,
167                 lastName, emailAddress, andOperator,
168                 COUNT_USERS_TYPE_PERMISSION);
169 
170             count += countUsers(
171                 session, CustomSQLUtil.get(COUNT_BY_GROUP_PERMISSION),
172                 companyId, groupId, name, primKey, actionId, firstName,
173                 middleName, lastName, emailAddress, andOperator,
174                 COUNT_USERS_TYPE_PERMISSION);
175 
176             count += countUsers(
177                 session, CustomSQLUtil.get(COUNT_BY_ORG_PERMISSION), companyId,
178                 groupId, name, primKey, actionId, firstName, middleName,
179                 lastName, emailAddress, andOperator,
180                 COUNT_USERS_TYPE_PERMISSION);
181 
182             count += countUsers(
183                 session, CustomSQLUtil.get(COUNT_BY_USER_ROLE), companyId,
184                 groupId, name, primKey, actionId, firstName, middleName,
185                 lastName, emailAddress, andOperator, COUNT_USERS_TYPE_ROLE);
186 
187             count += countUsers(
188                 session, CustomSQLUtil.get(COUNT_BY_GROUP_ROLE), companyId,
189                 groupId, name, primKey, actionId, firstName, middleName,
190                 lastName, emailAddress, andOperator, COUNT_USERS_TYPE_ROLE);
191 
192             count += countUsers(
193                 session, CustomSQLUtil.get(COUNT_BY_ORG_ROLE), companyId,
194                 groupId, name, primKey, actionId, firstName, middleName,
195                 lastName, emailAddress, andOperator, COUNT_USERS_TYPE_ROLE);
196 
197             return count;
198         }
199         catch (Exception e) {
200             throw new SystemException(e);
201         }
202         finally {
203             HibernateUtil.closeSession(session);
204         }
205     }
206 
207     public int countByUserAndOrgGroupPermission(
208             long companyId, String name, String primKey, String actionId,
209             String firstName, String middleName, String lastName,
210             String emailAddress, boolean andOperator)
211         throws SystemException {
212 
213         Session session = null;
214 
215         try {
216             session = HibernateUtil.openSession();
217 
218             int count = countUsers(
219                 session, CustomSQLUtil.get(COUNT_BY_ADMIN_ROLE), companyId,
220                 0, name, primKey, actionId, firstName, middleName, lastName,
221                 emailAddress, andOperator, COUNT_USERS_TYPE_ADMIN);
222 
223             count += countUsers(
224                 session, CustomSQLUtil.get(COUNT_BY_USER_PERMISSION), companyId,
225                 0, name, primKey, actionId, firstName, middleName, lastName,
226                 emailAddress, andOperator, COUNT_USERS_TYPE_PERMISSION);
227 
228             count += countUsers(
229                 session, CustomSQLUtil.get(COUNT_BY_ORG_GROUP_PERMISSION),
230                 companyId, 0, name, primKey, actionId, firstName, middleName,
231                 lastName, emailAddress, andOperator,
232                 COUNT_USERS_TYPE_PERMISSION);
233 
234             return count;
235         }
236         catch (Exception e) {
237             throw new SystemException(e);
238         }
239         finally {
240             HibernateUtil.closeSession(session);
241         }
242     }
243 
244     public List findByPermissionAndRole(
245             long companyId, long groupId, String name, String primKey,
246             String actionId, String firstName, String middleName,
247             String lastName, String emailAddress, boolean andOperator,
248             int begin, int end)
249         throws SystemException {
250 
251         Session session = null;
252 
253         try {
254             session = HibernateUtil.openSession();
255 
256             StringMaker sm = new StringMaker();
257 
258             sm.append("(");
259             sm.append(CustomSQLUtil.get(FIND_BY_ADMIN_ROLE));
260             sm.append(") UNION (");
261             sm.append(CustomSQLUtil.get(FIND_BY_USER_PERMISSION));
262             sm.append(") UNION (");
263             sm.append(CustomSQLUtil.get(FIND_BY_GROUP_PERMISSION));
264             sm.append(") UNION (");
265             sm.append(CustomSQLUtil.get(FIND_BY_ORG_PERMISSION));
266             sm.append(") UNION (");
267             sm.append(CustomSQLUtil.get(FIND_BY_USER_ROLE));
268             sm.append(") UNION (");
269             sm.append(CustomSQLUtil.get(FIND_BY_GROUP_ROLE));
270             sm.append(") UNION (");
271             sm.append(CustomSQLUtil.get(FIND_BY_ORG_ROLE));
272             sm.append(") ");
273             sm.append("ORDER BY lastName ASC, firstName ASC, middleName ASC ");
274 
275             String sql = sm.toString();
276 
277             sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
278 
279             SQLQuery q = session.createSQLQuery(sql);
280 
281             q.addScalar("userId", Hibernate.LONG);
282 
283             QueryPos qPos = QueryPos.getInstance(q);
284 
285             for (int i = 0; i < 7; i++) {
286                 qPos.add(companyId);
287 
288                 if (i > 0) {
289                     qPos.add(name);
290 
291                     if (i < 4) {
292                         qPos.add(primKey);
293                     }
294                     else {
295                         qPos.add(companyId);
296                         qPos.add(groupId);
297                     }
298 
299                     qPos.add(actionId);
300                 }
301 
302                 qPos.add(firstName);
303                 qPos.add(firstName);
304                 qPos.add(middleName);
305                 qPos.add(middleName);
306                 qPos.add(lastName);
307                 qPos.add(lastName);
308                 qPos.add(emailAddress);
309                 qPos.add(emailAddress);
310             }
311 
312             List list = new ArrayList();
313 
314             Iterator itr = QueryUtil.iterate(
315                 q, HibernateUtil.getDialect(), begin, end);
316 
317             while (itr.hasNext()) {
318                 Long userIdObj = (Long)itr.next();
319 
320                 User user = UserUtil.findByPrimaryKey(userIdObj.longValue());
321 
322                 list.add(user);
323             }
324 
325             return list;
326         }
327         catch (Exception e) {
328             throw new SystemException(e);
329         }
330         finally {
331             HibernateUtil.closeSession(session);
332         }
333     }
334 
335     public List findByUserAndOrgGroupPermission(
336             long companyId, String name, String primKey, String actionId,
337             String firstName, String middleName, String lastName,
338             String emailAddress, boolean andOperator, int begin, int end)
339         throws SystemException {
340 
341         Session session = null;
342 
343         try {
344             session = HibernateUtil.openSession();
345 
346             StringMaker sm = new StringMaker();
347 
348             sm.append("(");
349             sm.append(CustomSQLUtil.get(FIND_BY_ADMIN_ROLE));
350             sm.append(") UNION (");
351             sm.append(CustomSQLUtil.get(FIND_BY_USER_PERMISSION));
352             sm.append(") UNION (");
353             sm.append(CustomSQLUtil.get(FIND_BY_ORG_GROUP_PERMISSION));
354             sm.append(") ");
355             sm.append("ORDER BY lastName ASC, firstName ASC, middleName ASC ");
356 
357             String sql = sm.toString();
358 
359             sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
360 
361             SQLQuery q = session.createSQLQuery(sql);
362 
363             q.addScalar("userId", Hibernate.LONG);
364 
365             QueryPos qPos = QueryPos.getInstance(q);
366 
367             for (int i = 0; i < 3; i++) {
368                 qPos.add(companyId);
369 
370                 if (i > 0) {
371                     qPos.add(name);
372                     qPos.add(primKey);
373                     qPos.add(actionId);
374                 }
375 
376                 qPos.add(firstName);
377                 qPos.add(firstName);
378                 qPos.add(middleName);
379                 qPos.add(middleName);
380                 qPos.add(lastName);
381                 qPos.add(lastName);
382                 qPos.add(emailAddress);
383                 qPos.add(emailAddress);
384             }
385 
386             List list = new ArrayList();
387 
388             Iterator itr = QueryUtil.iterate(
389                 q, HibernateUtil.getDialect(), begin, end);
390 
391             while (itr.hasNext()) {
392                 Long userIdObj = (Long)itr.next();
393 
394                 User user = UserUtil.findByPrimaryKey(userIdObj.longValue());
395 
396                 list.add(user);
397             }
398 
399             return list;
400         }
401         catch (Exception e) {
402             throw new SystemException(e);
403         }
404         finally {
405             HibernateUtil.closeSession(session);
406         }
407     }
408 
409     protected int countUsers(
410             Session session, String sql, long companyId, long groupId,
411             String name, String primKey, String actionId, String firstName,
412             String middleName, String lastName, String emailAddress,
413             boolean andOperator, int countUsersType)
414         throws SystemException {
415 
416         sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
417 
418         SQLQuery q = session.createSQLQuery(sql);
419 
420         q.addScalar(HibernateUtil.getCountColumnName(), Hibernate.LONG);
421 
422         QueryPos qPos = QueryPos.getInstance(q);
423 
424         qPos.add(companyId);
425 
426         if (countUsersType != COUNT_USERS_TYPE_ADMIN) {
427             qPos.add(name);
428 
429             if (countUsersType == COUNT_USERS_TYPE_PERMISSION) {
430                 qPos.add(primKey);
431             }
432             else if (countUsersType == COUNT_USERS_TYPE_ROLE){
433                 qPos.add(companyId);
434                 qPos.add(groupId);
435             }
436 
437             qPos.add(actionId);
438         }
439 
440         qPos.add(firstName);
441         qPos.add(firstName);
442         qPos.add(middleName);
443         qPos.add(middleName);
444         qPos.add(lastName);
445         qPos.add(lastName);
446         qPos.add(emailAddress);
447         qPos.add(emailAddress);
448 
449         Iterator itr = q.list().iterator();
450 
451         if (itr.hasNext()) {
452             Long count = (Long)itr.next();
453 
454             if (count != null) {
455                 return count.intValue();
456             }
457         }
458 
459         return 0;
460     }
461 
462 }