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.NoSuchRoleException;
024: import com.liferay.portal.SystemException;
025: import com.liferay.portal.kernel.util.ArrayUtil;
026: import com.liferay.portal.kernel.util.OrderByComparator;
027: import com.liferay.portal.kernel.util.StringMaker;
028: import com.liferay.portal.kernel.util.StringPool;
029: import com.liferay.portal.kernel.util.StringUtil;
030: import com.liferay.portal.kernel.util.Validator;
031: import com.liferay.portal.model.Group;
032: import com.liferay.portal.model.Role;
033: import com.liferay.portal.model.impl.GroupModelImpl;
034: import com.liferay.portal.model.impl.RoleImpl;
035: import com.liferay.portal.model.impl.RoleModelImpl;
036: import com.liferay.portal.model.impl.UserModelImpl;
037: import com.liferay.portal.spring.hibernate.CustomSQLUtil;
038: import com.liferay.portal.spring.hibernate.FinderCache;
039: import com.liferay.portal.spring.hibernate.HibernateUtil;
040: import com.liferay.util.dao.hibernate.QueryPos;
041: import com.liferay.util.dao.hibernate.QueryUtil;
042:
043: import java.util.ArrayList;
044: import java.util.HashMap;
045: import java.util.Iterator;
046: import java.util.LinkedHashMap;
047: import java.util.List;
048: import java.util.Map;
049:
050: import org.hibernate.Hibernate;
051: import org.hibernate.SQLQuery;
052: import org.hibernate.Session;
053:
054: /**
055: * <a href="RoleFinderImpl.java.html"><b><i>View Source</i></b></a>
056: *
057: * @author Brian Wing Shun Chan
058: *
059: */
060: public class RoleFinderImpl implements RoleFinder {
061:
062: public static String COUNT_BY_C_N_D_T = RoleFinder.class.getName()
063: + ".countByC_N_D_T";
064:
065: public static String COUNT_BY_COMMUNITY = RoleFinder.class
066: .getName()
067: + ".countByCommunity";
068:
069: public static String COUNT_BY_ORGANIZATION = RoleFinder.class
070: .getName()
071: + ".countByOrganization";
072:
073: public static String COUNT_BY_USER = RoleFinder.class.getName()
074: + ".countByUser";
075:
076: public static String COUNT_BY_USER_GROUP = RoleFinder.class
077: .getName()
078: + ".countByUserGroup";
079:
080: public static String FIND_BY_USER_GROUP_ROLE = RoleFinder.class
081: .getName()
082: + ".findByUserGroupRole";
083:
084: public static String FIND_BY_C_N = RoleFinder.class.getName()
085: + ".findByC_N";
086:
087: public static String FIND_BY_U_G = RoleFinder.class.getName()
088: + ".findByU_G";
089:
090: public static String FIND_BY_C_N_D_T = RoleFinder.class.getName()
091: + ".findByC_N_D_T";
092:
093: public static String FIND_BY_C_N_S_P = RoleFinder.class.getName()
094: + ".findByC_N_S_P";
095:
096: public static String JOIN_BY_ROLES_PERMISSIONS = RoleFinder.class
097: .getName()
098: + ".joinByRolesPermissions";
099:
100: public static String JOIN_BY_USERS_ROLES = RoleFinder.class
101: .getName()
102: + ".joinByUsersRoles";
103:
104: public int countByR_U(long roleId, long userId)
105: throws SystemException {
106: String finderSQL = Role.class.getName();
107: boolean[] finderClassNamesCacheEnabled = new boolean[] {
108: GroupModelImpl.CACHE_ENABLED,
109: RoleModelImpl.CACHE_ENABLED,
110: GroupModelImpl.CACHE_ENABLED_GROUPS_ROLES,
111: UserModelImpl.CACHE_ENABLED_USERS_GROUPS,
112: UserModelImpl.CACHE_ENABLED_USERS_ORGS,
113: UserModelImpl.CACHE_ENABLED_USERS_ROLES,
114: UserModelImpl.CACHE_ENABLED_USERS_USERGROUPS };
115: String[] finderClassNames = new String[] {
116: Group.class.getName(), Role.class.getName(),
117: "Groups_Roles", "Users_Groups", "Users_Orgs",
118: "Users_Roles", "Users_UserGroups" };
119: String finderMethodName = "customCountByR_U";
120: String finderParams[] = new String[] { Long.class.getName(),
121: Long.class.getName() };
122: Object finderArgs[] = new Object[] { new Long(roleId),
123: new Long(userId) };
124:
125: Object result = null;
126:
127: if (!ArrayUtil.contains(finderClassNamesCacheEnabled, false)) {
128: result = FinderCache.getResult(finderSQL, finderClassNames,
129: finderMethodName, finderParams, finderArgs);
130: }
131:
132: if (result == null) {
133: Session session = null;
134:
135: try {
136: session = HibernateUtil.openSession();
137:
138: StringMaker sm = new StringMaker();
139:
140: sm.append("(");
141: sm.append(CustomSQLUtil.get(COUNT_BY_COMMUNITY));
142: sm.append(") UNION (");
143: sm.append(CustomSQLUtil.get(COUNT_BY_ORGANIZATION));
144: sm.append(") UNION (");
145: sm.append(CustomSQLUtil.get(COUNT_BY_USER));
146: sm.append(") UNION (");
147: sm.append(CustomSQLUtil.get(COUNT_BY_USER_GROUP));
148: sm.append(")");
149:
150: SQLQuery q = session.createSQLQuery(sm.toString());
151:
152: q.addScalar(HibernateUtil.getCountColumnName(),
153: Hibernate.LONG);
154:
155: QueryPos qPos = QueryPos.getInstance(q);
156:
157: for (int i = 0; i < 4; i++) {
158: qPos.add(roleId);
159: qPos.add(userId);
160: }
161:
162: int count = 0;
163:
164: Iterator itr = q.list().iterator();
165:
166: while (itr.hasNext()) {
167: Long l = (Long) itr.next();
168:
169: if (l != null) {
170: count += l.intValue();
171: }
172: }
173:
174: FinderCache.putResult(finderSQL,
175: finderClassNamesCacheEnabled, finderClassNames,
176: finderMethodName, finderParams, finderArgs,
177: new Long(count));
178:
179: return count;
180: } catch (Exception e) {
181: throw new SystemException(e);
182: } finally {
183: HibernateUtil.closeSession(session);
184: }
185: } else {
186: return ((Long) result).intValue();
187: }
188: }
189:
190: public int countByC_N_D_T(long companyId, String name,
191: String description, Integer type, LinkedHashMap params)
192: throws SystemException {
193:
194: name = StringUtil.lowerCase(name);
195: description = StringUtil.lowerCase(description);
196:
197: Session session = null;
198:
199: try {
200: session = HibernateUtil.openSession();
201:
202: String sql = CustomSQLUtil.get(COUNT_BY_C_N_D_T);
203:
204: if (type == null) {
205: sql = StringUtil.replace(sql, "AND (Role_.type_ = ?)",
206: "");
207: }
208:
209: sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
210: sql = StringUtil
211: .replace(sql, "[$WHERE$]", getWhere(params));
212:
213: SQLQuery q = session.createSQLQuery(sql);
214:
215: q.addScalar(HibernateUtil.getCountColumnName(),
216: Hibernate.LONG);
217:
218: QueryPos qPos = QueryPos.getInstance(q);
219:
220: setJoin(qPos, params);
221: qPos.add(companyId);
222: qPos.add(name);
223: qPos.add(name);
224: qPos.add(description);
225: qPos.add(description);
226:
227: if (type != null) {
228: qPos.add(type);
229: }
230:
231: Iterator itr = q.list().iterator();
232:
233: if (itr.hasNext()) {
234: Long count = (Long) itr.next();
235:
236: if (count != null) {
237: return count.intValue();
238: }
239: }
240:
241: return 0;
242: } catch (Exception e) {
243: throw new SystemException(e);
244: } finally {
245: HibernateUtil.closeSession(session);
246: }
247: }
248:
249: public List findByUserGroupRole(long userId, long groupId)
250: throws SystemException {
251:
252: Session session = null;
253:
254: try {
255: session = HibernateUtil.openSession();
256:
257: String sql = CustomSQLUtil.get(FIND_BY_USER_GROUP_ROLE);
258:
259: SQLQuery q = session.createSQLQuery(sql);
260:
261: q.addEntity("Role_", RoleImpl.class);
262:
263: QueryPos qPos = QueryPos.getInstance(q);
264:
265: qPos.add(userId);
266: qPos.add(groupId);
267:
268: return q.list();
269: } catch (Exception e) {
270: throw new SystemException(e);
271: } finally {
272: HibernateUtil.closeSession(session);
273: }
274: }
275:
276: public Role findByC_N(long companyId, String name)
277: throws NoSuchRoleException, SystemException {
278:
279: name = StringUtil.lowerCase(name);
280:
281: boolean finderClassNameCacheEnabled = RoleModelImpl.CACHE_ENABLED;
282: String finderClassName = Role.class.getName();
283: String finderMethodName = "customFindByC_N";
284: String finderParams[] = new String[] { Long.class.getName(),
285: String.class.getName() };
286: Object finderArgs[] = new Object[] { new Long(companyId), name };
287:
288: Object result = FinderCache.getResult(finderClassName,
289: finderMethodName, finderParams, finderArgs);
290:
291: if (result == null) {
292: Session session = null;
293:
294: try {
295: session = HibernateUtil.openSession();
296:
297: String sql = CustomSQLUtil.get(FIND_BY_C_N);
298:
299: SQLQuery q = session.createSQLQuery(sql);
300:
301: q.addEntity("Role_", RoleImpl.class);
302:
303: QueryPos qPos = QueryPos.getInstance(q);
304:
305: qPos.add(companyId);
306: qPos.add(name);
307:
308: Iterator itr = q.list().iterator();
309:
310: if (itr.hasNext()) {
311: Role role = (Role) itr.next();
312:
313: FinderCache.putResult(finderClassNameCacheEnabled,
314: finderClassName, finderMethodName,
315: finderParams, finderArgs, role);
316:
317: return role;
318: }
319: } catch (Exception e) {
320: throw new SystemException(e);
321: } finally {
322: HibernateUtil.closeSession(session);
323: }
324:
325: throw new NoSuchRoleException(
326: "No Role exists with the key {companyId="
327: + companyId + ", name=" + name + "}");
328: } else {
329: return (Role) result;
330: }
331: }
332:
333: public List findByU_G(long userId, long groupId)
334: throws SystemException {
335: return findByU_G(userId, new long[] { groupId });
336: }
337:
338: public List findByU_G(long userId, long[] groupIds)
339: throws SystemException {
340: Session session = null;
341:
342: try {
343: session = HibernateUtil.openSession();
344:
345: String sql = CustomSQLUtil.get(FIND_BY_U_G);
346:
347: sql = StringUtil.replace(sql, "[$GROUP_IDS$]", getGroupIds(
348: groupIds, "Groups_Roles"));
349:
350: SQLQuery q = session.createSQLQuery(sql);
351:
352: q.addEntity("Role_", RoleImpl.class);
353:
354: QueryPos qPos = QueryPos.getInstance(q);
355:
356: qPos.add(userId);
357: setGroupIds(qPos, groupIds);
358:
359: return q.list();
360: } catch (Exception e) {
361: throw new SystemException(e);
362: } finally {
363: HibernateUtil.closeSession(session);
364: }
365: }
366:
367: public List findByU_G(long userId, List groups)
368: throws SystemException {
369: long[] groupIds = new long[groups.size()];
370:
371: for (int i = 0; i < groups.size(); i++) {
372: Group group = (Group) groups.get(i);
373:
374: groupIds[i] = group.getGroupId();
375: }
376:
377: return findByU_G(userId, groupIds);
378: }
379:
380: public List findByC_N_D_T(long companyId, String name,
381: String description, Integer type, LinkedHashMap params,
382: int begin, int end, OrderByComparator obc)
383: throws SystemException {
384:
385: name = StringUtil.lowerCase(name);
386: description = StringUtil.lowerCase(description);
387:
388: Session session = null;
389:
390: try {
391: session = HibernateUtil.openSession();
392:
393: String sql = CustomSQLUtil.get(FIND_BY_C_N_D_T);
394:
395: if (type == null) {
396: sql = StringUtil.replace(sql, "AND (Role_.type_ = ?)",
397: "");
398: }
399:
400: sql = StringUtil.replace(sql, "[$JOIN$]", getJoin(params));
401: sql = StringUtil
402: .replace(sql, "[$WHERE$]", getWhere(params));
403: sql = CustomSQLUtil.replaceOrderBy(sql, obc);
404:
405: SQLQuery q = session.createSQLQuery(sql);
406:
407: q.addEntity("Role_", RoleImpl.class);
408:
409: QueryPos qPos = QueryPos.getInstance(q);
410:
411: setJoin(qPos, params);
412: qPos.add(companyId);
413: qPos.add(name);
414: qPos.add(name);
415: qPos.add(description);
416: qPos.add(description);
417:
418: if (type != null) {
419: qPos.add(type);
420: }
421:
422: return QueryUtil.list(q, HibernateUtil.getDialect(), begin,
423: end);
424: } catch (Exception e) {
425: throw new SystemException(e);
426: } finally {
427: HibernateUtil.closeSession(session);
428: }
429: }
430:
431: public Map findByC_N_S_P(long companyId, String name, int scope,
432: String primKey) throws SystemException {
433:
434: Session session = null;
435:
436: try {
437: session = HibernateUtil.openSession();
438:
439: String sql = CustomSQLUtil.get(FIND_BY_C_N_S_P);
440:
441: SQLQuery q = session.createSQLQuery(sql);
442:
443: q.addScalar("roleName", Hibernate.STRING);
444: q.addScalar("actionId", Hibernate.STRING);
445:
446: QueryPos qPos = QueryPos.getInstance(q);
447:
448: qPos.add(companyId);
449: qPos.add(name);
450: qPos.add(scope);
451: qPos.add(primKey);
452:
453: Map roleMap = new HashMap();
454:
455: Iterator itr = q.list().iterator();
456:
457: while (itr.hasNext()) {
458: Object[] array = (Object[]) itr.next();
459:
460: String roleName = (String) array[0];
461: String actionId = (String) array[1];
462:
463: List roleList = (List) roleMap.get(roleName);
464:
465: if (roleList == null) {
466: roleList = new ArrayList();
467: }
468:
469: roleList.add(actionId);
470:
471: roleMap.put(roleName, roleList);
472: }
473:
474: return roleMap;
475: } catch (Exception e) {
476: throw new SystemException(e);
477: } finally {
478: HibernateUtil.closeSession(session);
479: }
480: }
481:
482: protected String getGroupIds(long[] groupIds, String table) {
483: StringMaker sm = new StringMaker();
484:
485: for (int i = 0; i < groupIds.length; i++) {
486: sm.append(table);
487: sm.append(".groupId = ?");
488:
489: if ((i + 1) < groupIds.length) {
490: sm.append(" OR ");
491: }
492: }
493:
494: return sm.toString();
495: }
496:
497: protected void setGroupIds(QueryPos qPos, long[] groupIds) {
498: for (int i = 0; i < groupIds.length; i++) {
499: qPos.add(groupIds[i]);
500: }
501: }
502:
503: protected String getJoin(LinkedHashMap params) {
504: if (params == null) {
505: return StringPool.BLANK;
506: }
507:
508: StringMaker sm = new StringMaker();
509:
510: Iterator itr = params.entrySet().iterator();
511:
512: while (itr.hasNext()) {
513: Map.Entry entry = (Map.Entry) itr.next();
514:
515: String key = (String) entry.getKey();
516: Object value = entry.getValue();
517:
518: if (Validator.isNotNull(value)) {
519: sm.append(getJoin(key));
520: }
521: }
522:
523: return sm.toString();
524: }
525:
526: protected String getJoin(String key) {
527: String join = StringPool.BLANK;
528:
529: if (key.equals("permissionsResourceId")) {
530: join = CustomSQLUtil.get(JOIN_BY_ROLES_PERMISSIONS);
531: } else if (key.equals("usersRoles")) {
532: join = CustomSQLUtil.get(JOIN_BY_USERS_ROLES);
533: }
534:
535: if (Validator.isNotNull(join)) {
536: int pos = join.indexOf("WHERE");
537:
538: if (pos != -1) {
539: join = join.substring(0, pos);
540: }
541: }
542:
543: return join;
544: }
545:
546: protected String getWhere(LinkedHashMap params) {
547: if (params == null) {
548: return StringPool.BLANK;
549: }
550:
551: StringMaker sm = new StringMaker();
552:
553: Iterator itr = params.entrySet().iterator();
554:
555: while (itr.hasNext()) {
556: Map.Entry entry = (Map.Entry) itr.next();
557:
558: String key = (String) entry.getKey();
559: Object value = entry.getValue();
560:
561: if (Validator.isNotNull(value)) {
562: sm.append(getWhere(key));
563: }
564: }
565:
566: return sm.toString();
567: }
568:
569: protected String getWhere(String key) {
570: String join = StringPool.BLANK;
571:
572: if (key.equals("permissionsResourceId")) {
573: join = CustomSQLUtil.get(JOIN_BY_ROLES_PERMISSIONS);
574: } else if (key.equals("usersRoles")) {
575: join = CustomSQLUtil.get(JOIN_BY_USERS_ROLES);
576: }
577:
578: if (Validator.isNotNull(join)) {
579: int pos = join.indexOf("WHERE");
580:
581: if (pos != -1) {
582: join = join.substring(pos + 5, join.length()) + " AND ";
583: }
584: }
585:
586: return join;
587: }
588:
589: protected void setJoin(QueryPos qPos, LinkedHashMap params) {
590: if (params != null) {
591: Iterator itr = params.entrySet().iterator();
592:
593: while (itr.hasNext()) {
594: Map.Entry entry = (Map.Entry) itr.next();
595:
596: Object value = entry.getValue();
597:
598: if (value instanceof Long) {
599: Long valueLong = (Long) value;
600:
601: if (Validator.isNotNull(valueLong)) {
602: qPos.add(valueLong);
603: }
604: } else if (value instanceof String) {
605: String valueString = (String) value;
606:
607: if (Validator.isNotNull(valueString)) {
608: qPos.add(valueString);
609: }
610: }
611: }
612: }
613: }
614:
615: }
|