001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portal.service.persistence;
022:
023: import com.liferay.portal.SystemException;
024: import com.liferay.portal.kernel.util.StringMaker;
025: import com.liferay.portal.model.User;
026: import com.liferay.portal.spring.hibernate.CustomSQLUtil;
027: import com.liferay.portal.spring.hibernate.HibernateUtil;
028: import com.liferay.util.dao.hibernate.QueryPos;
029: import com.liferay.util.dao.hibernate.QueryUtil;
030:
031: import java.util.ArrayList;
032: import java.util.Iterator;
033: import java.util.List;
034:
035: import org.hibernate.Hibernate;
036: import org.hibernate.SQLQuery;
037: import org.hibernate.Session;
038:
039: /**
040: * <a href="PermissionUserFinderImpl.java.html"><b><i>View Source</i></b></a>
041: *
042: * @author Charles May
043: *
044: */
045: public class PermissionUserFinderImpl implements PermissionUserFinder {
046:
047: public static String COUNT_BY_ADMIN_ROLE = PermissionUserFinder.class
048: .getName()
049: + ".countByAdminRole";
050:
051: public static String COUNT_BY_GROUP_PERMISSION = PermissionUserFinder.class
052: .getName()
053: + ".countByGroupPermission";
054:
055: public static String COUNT_BY_GROUP_ROLE = PermissionUserFinder.class
056: .getName()
057: + ".countByGroupRole";
058:
059: public static String COUNT_BY_ORG_GROUP_PERMISSION = PermissionUserFinder.class
060: .getName()
061: + ".countByOrgGroupPermission";
062:
063: public static String COUNT_BY_ORG_GROUP_PERMISSIONS = PermissionUserFinder.class
064: .getName()
065: + ".countByOrgGroupPermissions";
066:
067: public static String COUNT_BY_ORG_PERMISSION = PermissionUserFinder.class
068: .getName()
069: + ".countByOrgPermission";
070:
071: public static String COUNT_BY_ORG_ROLE = PermissionUserFinder.class
072: .getName()
073: + ".countByOrgRole";
074:
075: public static String COUNT_BY_USER_PERMISSION = PermissionUserFinder.class
076: .getName()
077: + ".countByUserPermission";
078:
079: public static String COUNT_BY_USER_ROLE = PermissionUserFinder.class
080: .getName()
081: + ".countByUserRole";
082:
083: public static String FIND_BY_ADMIN_ROLE = PermissionUserFinder.class
084: .getName()
085: + ".findByAdminRole";
086:
087: public static String FIND_BY_GROUP_PERMISSION = PermissionUserFinder.class
088: .getName()
089: + ".findByGroupPermission";
090:
091: public static String FIND_BY_GROUP_ROLE = PermissionUserFinder.class
092: .getName()
093: + ".findByGroupRole";
094:
095: public static String FIND_BY_ORG_GROUP_PERMISSION = PermissionUserFinder.class
096: .getName()
097: + ".findByOrgGroupPermission";
098:
099: public static String FIND_BY_ORG_PERMISSION = PermissionUserFinder.class
100: .getName()
101: + ".findByOrgPermission";
102:
103: public static String FIND_BY_ORG_ROLE = PermissionUserFinder.class
104: .getName()
105: + ".findByOrgRole";
106:
107: public static String FIND_BY_USER_PERMISSION = PermissionUserFinder.class
108: .getName()
109: + ".findByUserPermission";
110:
111: public static String FIND_BY_USER_ROLE = PermissionUserFinder.class
112: .getName()
113: + ".findByUserRole";
114:
115: public static int COUNT_USERS_TYPE_ADMIN = 1;
116:
117: public static int COUNT_USERS_TYPE_PERMISSION = 2;
118:
119: public static int COUNT_USERS_TYPE_ROLE = 3;
120:
121: public int countByOrgGroupPermissions(long companyId, String name,
122: String primKey, String actionId) throws SystemException {
123:
124: Session session = null;
125:
126: try {
127: session = HibernateUtil.openSession();
128:
129: String sql = CustomSQLUtil
130: .get(COUNT_BY_ORG_GROUP_PERMISSIONS);
131:
132: SQLQuery q = session.createSQLQuery(sql);
133:
134: q.addScalar(HibernateUtil.getCountColumnName(),
135: Hibernate.LONG);
136:
137: QueryPos qPos = QueryPos.getInstance(q);
138:
139: qPos.add(companyId);
140: qPos.add(name);
141: qPos.add(primKey);
142: qPos.add(actionId);
143:
144: Iterator itr = q.list().iterator();
145:
146: if (itr.hasNext()) {
147: Long count = (Long) itr.next();
148:
149: if (count != null) {
150: return count.intValue();
151: }
152: }
153:
154: return 0;
155: } catch (Exception e) {
156: throw new SystemException(e);
157: } finally {
158: HibernateUtil.closeSession(session);
159: }
160: }
161:
162: public int countByPermissionAndRole(long companyId, long groupId,
163: String name, String primKey, String actionId,
164: String firstName, String middleName, String lastName,
165: String emailAddress, boolean andOperator)
166: throws SystemException {
167:
168: Session session = null;
169:
170: try {
171: session = HibernateUtil.openSession();
172:
173: int count = countUsers(session, CustomSQLUtil
174: .get(COUNT_BY_ADMIN_ROLE), companyId, groupId,
175: name, primKey, actionId, firstName, middleName,
176: lastName, emailAddress, andOperator,
177: COUNT_USERS_TYPE_ADMIN);
178:
179: count += countUsers(session, CustomSQLUtil
180: .get(COUNT_BY_USER_PERMISSION), companyId, groupId,
181: name, primKey, actionId, firstName, middleName,
182: lastName, emailAddress, andOperator,
183: COUNT_USERS_TYPE_PERMISSION);
184:
185: count += countUsers(session, CustomSQLUtil
186: .get(COUNT_BY_GROUP_PERMISSION), companyId,
187: groupId, name, primKey, actionId, firstName,
188: middleName, lastName, emailAddress, andOperator,
189: COUNT_USERS_TYPE_PERMISSION);
190:
191: count += countUsers(session, CustomSQLUtil
192: .get(COUNT_BY_ORG_PERMISSION), companyId, groupId,
193: name, primKey, actionId, firstName, middleName,
194: lastName, emailAddress, andOperator,
195: COUNT_USERS_TYPE_PERMISSION);
196:
197: count += countUsers(session, CustomSQLUtil
198: .get(COUNT_BY_USER_ROLE), companyId, groupId, name,
199: primKey, actionId, firstName, middleName, lastName,
200: emailAddress, andOperator, COUNT_USERS_TYPE_ROLE);
201:
202: count += countUsers(session, CustomSQLUtil
203: .get(COUNT_BY_GROUP_ROLE), companyId, groupId,
204: name, primKey, actionId, firstName, middleName,
205: lastName, emailAddress, andOperator,
206: COUNT_USERS_TYPE_ROLE);
207:
208: count += countUsers(session, CustomSQLUtil
209: .get(COUNT_BY_ORG_ROLE), companyId, groupId, name,
210: primKey, actionId, firstName, middleName, lastName,
211: emailAddress, andOperator, COUNT_USERS_TYPE_ROLE);
212:
213: return count;
214: } catch (Exception e) {
215: throw new SystemException(e);
216: } finally {
217: HibernateUtil.closeSession(session);
218: }
219: }
220:
221: public int countByUserAndOrgGroupPermission(long companyId,
222: String name, String primKey, String actionId,
223: String firstName, String middleName, String lastName,
224: String emailAddress, boolean andOperator)
225: throws SystemException {
226:
227: Session session = null;
228:
229: try {
230: session = HibernateUtil.openSession();
231:
232: int count = countUsers(session, CustomSQLUtil
233: .get(COUNT_BY_ADMIN_ROLE), companyId, 0, name,
234: primKey, actionId, firstName, middleName, lastName,
235: emailAddress, andOperator, COUNT_USERS_TYPE_ADMIN);
236:
237: count += countUsers(session, CustomSQLUtil
238: .get(COUNT_BY_USER_PERMISSION), companyId, 0, name,
239: primKey, actionId, firstName, middleName, lastName,
240: emailAddress, andOperator,
241: COUNT_USERS_TYPE_PERMISSION);
242:
243: count += countUsers(session, CustomSQLUtil
244: .get(COUNT_BY_ORG_GROUP_PERMISSION), companyId, 0,
245: name, primKey, actionId, firstName, middleName,
246: lastName, emailAddress, andOperator,
247: COUNT_USERS_TYPE_PERMISSION);
248:
249: return count;
250: } catch (Exception e) {
251: throw new SystemException(e);
252: } finally {
253: HibernateUtil.closeSession(session);
254: }
255: }
256:
257: public List findByPermissionAndRole(long companyId, long groupId,
258: String name, String primKey, String actionId,
259: String firstName, String middleName, String lastName,
260: String emailAddress, boolean andOperator, int begin, int end)
261: throws SystemException {
262:
263: Session session = null;
264:
265: try {
266: session = HibernateUtil.openSession();
267:
268: StringMaker sm = new StringMaker();
269:
270: sm.append("(");
271: sm.append(CustomSQLUtil.get(FIND_BY_ADMIN_ROLE));
272: sm.append(") UNION (");
273: sm.append(CustomSQLUtil.get(FIND_BY_USER_PERMISSION));
274: sm.append(") UNION (");
275: sm.append(CustomSQLUtil.get(FIND_BY_GROUP_PERMISSION));
276: sm.append(") UNION (");
277: sm.append(CustomSQLUtil.get(FIND_BY_ORG_PERMISSION));
278: sm.append(") UNION (");
279: sm.append(CustomSQLUtil.get(FIND_BY_USER_ROLE));
280: sm.append(") UNION (");
281: sm.append(CustomSQLUtil.get(FIND_BY_GROUP_ROLE));
282: sm.append(") UNION (");
283: sm.append(CustomSQLUtil.get(FIND_BY_ORG_ROLE));
284: sm.append(") ");
285: sm
286: .append("ORDER BY lastName ASC, firstName ASC, middleName ASC ");
287:
288: String sql = sm.toString();
289:
290: sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
291:
292: SQLQuery q = session.createSQLQuery(sql);
293:
294: q.addScalar("userId", Hibernate.LONG);
295:
296: QueryPos qPos = QueryPos.getInstance(q);
297:
298: for (int i = 0; i < 7; i++) {
299: qPos.add(companyId);
300:
301: if (i > 0) {
302: qPos.add(name);
303:
304: if (i < 4) {
305: qPos.add(primKey);
306: } else {
307: qPos.add(companyId);
308: qPos.add(groupId);
309: }
310:
311: qPos.add(actionId);
312: }
313:
314: qPos.add(firstName);
315: qPos.add(firstName);
316: qPos.add(middleName);
317: qPos.add(middleName);
318: qPos.add(lastName);
319: qPos.add(lastName);
320: qPos.add(emailAddress);
321: qPos.add(emailAddress);
322: }
323:
324: List list = new ArrayList();
325:
326: Iterator itr = QueryUtil.iterate(q, HibernateUtil
327: .getDialect(), begin, end);
328:
329: while (itr.hasNext()) {
330: Long userIdObj = (Long) itr.next();
331:
332: User user = UserUtil.findByPrimaryKey(userIdObj
333: .longValue());
334:
335: list.add(user);
336: }
337:
338: return list;
339: } catch (Exception e) {
340: throw new SystemException(e);
341: } finally {
342: HibernateUtil.closeSession(session);
343: }
344: }
345:
346: public List findByUserAndOrgGroupPermission(long companyId,
347: String name, String primKey, String actionId,
348: String firstName, String middleName, String lastName,
349: String emailAddress, boolean andOperator, int begin, int end)
350: throws SystemException {
351:
352: Session session = null;
353:
354: try {
355: session = HibernateUtil.openSession();
356:
357: StringMaker sm = new StringMaker();
358:
359: sm.append("(");
360: sm.append(CustomSQLUtil.get(FIND_BY_ADMIN_ROLE));
361: sm.append(") UNION (");
362: sm.append(CustomSQLUtil.get(FIND_BY_USER_PERMISSION));
363: sm.append(") UNION (");
364: sm.append(CustomSQLUtil.get(FIND_BY_ORG_GROUP_PERMISSION));
365: sm.append(") ");
366: sm
367: .append("ORDER BY lastName ASC, firstName ASC, middleName ASC ");
368:
369: String sql = sm.toString();
370:
371: sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
372:
373: SQLQuery q = session.createSQLQuery(sql);
374:
375: q.addScalar("userId", Hibernate.LONG);
376:
377: QueryPos qPos = QueryPos.getInstance(q);
378:
379: for (int i = 0; i < 3; i++) {
380: qPos.add(companyId);
381:
382: if (i > 0) {
383: qPos.add(name);
384: qPos.add(primKey);
385: qPos.add(actionId);
386: }
387:
388: qPos.add(firstName);
389: qPos.add(firstName);
390: qPos.add(middleName);
391: qPos.add(middleName);
392: qPos.add(lastName);
393: qPos.add(lastName);
394: qPos.add(emailAddress);
395: qPos.add(emailAddress);
396: }
397:
398: List list = new ArrayList();
399:
400: Iterator itr = QueryUtil.iterate(q, HibernateUtil
401: .getDialect(), begin, end);
402:
403: while (itr.hasNext()) {
404: Long userIdObj = (Long) itr.next();
405:
406: User user = UserUtil.findByPrimaryKey(userIdObj
407: .longValue());
408:
409: list.add(user);
410: }
411:
412: return list;
413: } catch (Exception e) {
414: throw new SystemException(e);
415: } finally {
416: HibernateUtil.closeSession(session);
417: }
418: }
419:
420: protected int countUsers(Session session, String sql,
421: long companyId, long groupId, String name, String primKey,
422: String actionId, String firstName, String middleName,
423: String lastName, String emailAddress, boolean andOperator,
424: int countUsersType) throws SystemException {
425:
426: sql = CustomSQLUtil.replaceAndOperator(sql, andOperator);
427:
428: SQLQuery q = session.createSQLQuery(sql);
429:
430: q.addScalar(HibernateUtil.getCountColumnName(), Hibernate.LONG);
431:
432: QueryPos qPos = QueryPos.getInstance(q);
433:
434: qPos.add(companyId);
435:
436: if (countUsersType != COUNT_USERS_TYPE_ADMIN) {
437: qPos.add(name);
438:
439: if (countUsersType == COUNT_USERS_TYPE_PERMISSION) {
440: qPos.add(primKey);
441: } else if (countUsersType == COUNT_USERS_TYPE_ROLE) {
442: qPos.add(companyId);
443: qPos.add(groupId);
444: }
445:
446: qPos.add(actionId);
447: }
448:
449: qPos.add(firstName);
450: qPos.add(firstName);
451: qPos.add(middleName);
452: qPos.add(middleName);
453: qPos.add(lastName);
454: qPos.add(lastName);
455: qPos.add(emailAddress);
456: qPos.add(emailAddress);
457:
458: Iterator itr = q.list().iterator();
459:
460: if (itr.hasNext()) {
461: Long count = (Long) itr.next();
462:
463: if (count != null) {
464: return count.intValue();
465: }
466: }
467:
468: return 0;
469: }
470:
471: }
|