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