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