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<Long> itr = q.list().iterator();
129 
130             if (itr.hasNext()) {
131                 Long count = 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<User> 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<User> users = new ArrayList<User>();
313 
314             List<Long> userIds = (List<Long>)QueryUtil.list(
315                 q, HibernateUtil.getDialect(), begin, end);
316 
317             for (long userId : userIds) {
318                 User user = UserUtil.findByPrimaryKey(userId);
319 
320                 users.add(user);
321             }
322 
323             return users;
324         }
325         catch (Exception e) {
326             throw new SystemException(e);
327         }
328         finally {
329             HibernateUtil.closeSession(session);
330         }
331     }
332 
333     public List<User> findByUserAndOrgGroupPermission(
334             long companyId, String name, String primKey, String actionId,
335             String firstName, String middleName, String lastName,
336             String emailAddress, boolean andOperator, int begin, int end)
337         throws SystemException {
338 
339         Session session = null;
340 
341         try {
342             session = HibernateUtil.openSession();
343 
344             StringMaker sm = new StringMaker();
345 
346             sm.append("(");
347             sm.append(CustomSQLUtil.get(FIND_BY_ADMIN_ROLE));
348             sm.append(") UNION (");
349             sm.append(CustomSQLUtil.get(FIND_BY_USER_PERMISSION));
350             sm.append(") UNION (");
351             sm.append(CustomSQLUtil.get(FIND_BY_ORG_GROUP_PERMISSION));
352             sm.append(") ");
353             sm.append("ORDER BY lastName ASC, firstName ASC, middleName ASC ");
354 
355             String sql = sm.toString();
356 
357             sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
358 
359             SQLQuery q = session.createSQLQuery(sql);
360 
361             q.addScalar("userId", Hibernate.LONG);
362 
363             QueryPos qPos = QueryPos.getInstance(q);
364 
365             for (int i = 0; i < 3; i++) {
366                 qPos.add(companyId);
367 
368                 if (i > 0) {
369                     qPos.add(name);
370                     qPos.add(primKey);
371                     qPos.add(actionId);
372                 }
373 
374                 qPos.add(firstName);
375                 qPos.add(firstName);
376                 qPos.add(middleName);
377                 qPos.add(middleName);
378                 qPos.add(lastName);
379                 qPos.add(lastName);
380                 qPos.add(emailAddress);
381                 qPos.add(emailAddress);
382             }
383 
384             List<User> users = new ArrayList<User>();
385 
386             List<Long> userIds = (List<Long>)QueryUtil.list(
387                 q, HibernateUtil.getDialect(), begin, end);
388 
389             for (long userId : userIds) {
390                 User user = UserUtil.findByPrimaryKey(userId);
391 
392                 users.add(user);
393             }
394 
395             return users;
396         }
397         catch (Exception e) {
398             throw new SystemException(e);
399         }
400         finally {
401             HibernateUtil.closeSession(session);
402         }
403     }
404 
405     protected int countUsers(
406             Session session, String sql, long companyId, long groupId,
407             String name, String primKey, String actionId, String firstName,
408             String middleName, String lastName, String emailAddress,
409             boolean andOperator, int countUsersType)
410         throws SystemException {
411 
412         sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
413 
414         SQLQuery q = session.createSQLQuery(sql);
415 
416         q.addScalar(HibernateUtil.getCountColumnName(), Hibernate.LONG);
417 
418         QueryPos qPos = QueryPos.getInstance(q);
419 
420         qPos.add(companyId);
421 
422         if (countUsersType != COUNT_USERS_TYPE_ADMIN) {
423             qPos.add(name);
424 
425             if (countUsersType == COUNT_USERS_TYPE_PERMISSION) {
426                 qPos.add(primKey);
427             }
428             else if (countUsersType == COUNT_USERS_TYPE_ROLE){
429                 qPos.add(companyId);
430                 qPos.add(groupId);
431             }
432 
433             qPos.add(actionId);
434         }
435 
436         qPos.add(firstName);
437         qPos.add(firstName);
438         qPos.add(middleName);
439         qPos.add(middleName);
440         qPos.add(lastName);
441         qPos.add(lastName);
442         qPos.add(emailAddress);
443         qPos.add(emailAddress);
444 
445         Iterator<Long> itr = q.list().iterator();
446 
447         if (itr.hasNext()) {
448             Long count = itr.next();
449 
450             if (count != null) {
451                 return count.intValue();
452             }
453         }
454 
455         return 0;
456     }
457 
458 }