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