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.ArrayUtil;
025: import com.liferay.portal.kernel.util.StringMaker;
026: import com.liferay.portal.kernel.util.StringUtil;
027: import com.liferay.portal.model.Group;
028: import com.liferay.portal.model.Permission;
029: import com.liferay.portal.model.Role;
030: import com.liferay.portal.model.impl.PermissionImpl;
031: import com.liferay.portal.model.impl.PermissionModelImpl;
032: import com.liferay.portal.spring.hibernate.CustomSQLUtil;
033: import com.liferay.portal.spring.hibernate.FinderCache;
034: import com.liferay.portal.spring.hibernate.HibernateUtil;
035: import com.liferay.util.dao.hibernate.QueryPos;
036:
037: import java.util.Iterator;
038: import java.util.List;
039:
040: import org.hibernate.Hibernate;
041: import org.hibernate.SQLQuery;
042: import org.hibernate.Session;
043:
044: /**
045: * <a href="PermissionFinderImpl.java.html"><b><i>View Source</i></b></a>
046: *
047: * @author Brian Wing Shun Chan
048: *
049: */
050: public class PermissionFinderImpl implements PermissionFinder {
051:
052: public static String COUNT_BY_GROUPS_PERMISSIONS = PermissionFinder.class
053: .getName()
054: + ".countByGroupsPermissions";
055:
056: public static String COUNT_BY_GROUPS_ROLES = PermissionFinder.class
057: .getName()
058: + ".countByGroupsRoles";
059:
060: public static String COUNT_BY_ROLES_PERMISSIONS = PermissionFinder.class
061: .getName()
062: + ".countByRolesPermissions";
063:
064: public static String COUNT_BY_USER_GROUP_ROLE = PermissionFinder.class
065: .getName()
066: + ".countByUserGroupRole";
067:
068: public static String COUNT_BY_USERS_PERMISSIONS = PermissionFinder.class
069: .getName()
070: + ".countByUsersPermissions";
071:
072: public static String COUNT_BY_USERS_ROLES = PermissionFinder.class
073: .getName()
074: + ".countByUsersRoles";
075:
076: public static String FIND_BY_A_R = PermissionFinder.class.getName()
077: + ".findByA_R";
078:
079: public static String FIND_BY_G_R = PermissionFinder.class.getName()
080: + ".findByG_R";
081:
082: public static String FIND_BY_R_R = PermissionFinder.class.getName()
083: + ".findByR_R";
084:
085: public static String FIND_BY_U_R = PermissionFinder.class.getName()
086: + ".findByU_R";
087:
088: public static String FIND_BY_O_G_R = PermissionFinder.class
089: .getName()
090: + ".findByO_G_R";
091:
092: public static String FIND_BY_U_A_R = PermissionFinder.class
093: .getName()
094: + ".findByU_A_R";
095:
096: public static String FIND_BY_G_C_N_S_P = PermissionFinder.class
097: .getName()
098: + ".findByG_C_N_S_P";
099:
100: public static String FIND_BY_U_C_N_S_P = PermissionFinder.class
101: .getName()
102: + ".findByU_C_N_S_P";
103:
104: public boolean containsPermissions_2(List permissions, long userId,
105: List groups, long groupId) throws SystemException {
106:
107: Session session = null;
108:
109: try {
110: session = HibernateUtil.openSession();
111:
112: String sql = null;
113:
114: StringMaker sm = new StringMaker();
115:
116: if (groups.size() > 0) {
117: sm.append("(");
118: sm.append(CustomSQLUtil.get(COUNT_BY_GROUPS_ROLES));
119: sm.append(") ");
120:
121: sql = sm.toString();
122:
123: sql = StringUtil.replace(sql, "[$PERMISSION_IDS$]",
124: getPermissionIds(permissions,
125: "Roles_Permissions"));
126: sql = StringUtil.replace(sql, "[$GROUP_IDS$]",
127: getGroupIds(groups, "Groups_Roles"));
128:
129: sm = new StringMaker();
130:
131: sm.append(sql);
132:
133: sm.append("UNION ALL (");
134: sm.append(CustomSQLUtil
135: .get(COUNT_BY_GROUPS_PERMISSIONS));
136: sm.append(") ");
137:
138: sql = sm.toString();
139:
140: sql = StringUtil.replace(sql, "[$PERMISSION_IDS$]",
141: getPermissionIds(permissions,
142: "Groups_Permissions"));
143: sql = StringUtil.replace(sql, "[$GROUP_IDS$]",
144: getGroupIds(groups, "Groups_Permissions"));
145:
146: sm = new StringMaker();
147:
148: sm.append(sql);
149:
150: sm.append("UNION ALL ");
151: }
152:
153: sm.append("(");
154: sm.append(CustomSQLUtil.get(COUNT_BY_USERS_ROLES));
155: sm.append(") ");
156:
157: sql = sm.toString();
158:
159: sql = StringUtil.replace(sql, "[$PERMISSION_IDS$]",
160: getPermissionIds(permissions, "Roles_Permissions"));
161:
162: sm = new StringMaker();
163:
164: sm.append(sql);
165:
166: sm.append("UNION ALL (");
167: sm.append(CustomSQLUtil.get(COUNT_BY_USER_GROUP_ROLE));
168: sm.append(") ");
169:
170: sql = sm.toString();
171:
172: sql = StringUtil.replace(sql, "[$PERMISSION_IDS$]",
173: getPermissionIds(permissions, "Roles_Permissions"));
174:
175: sm = new StringMaker();
176:
177: sm.append(sql);
178:
179: sm.append("UNION ALL (");
180: sm.append(CustomSQLUtil.get(COUNT_BY_USERS_PERMISSIONS));
181: sm.append(") ");
182:
183: sql = sm.toString();
184:
185: sql = StringUtil.replace(sql, "[$PERMISSION_IDS$]",
186: getPermissionIds(permissions, "Users_Permissions"));
187:
188: SQLQuery q = session.createSQLQuery(sql);
189:
190: q.addScalar(HibernateUtil.getCountColumnName(),
191: Hibernate.LONG);
192:
193: QueryPos qPos = QueryPos.getInstance(q);
194:
195: if (groups.size() > 0) {
196: setPermissionIds(qPos, permissions);
197: setGroupIds(qPos, groups);
198: setPermissionIds(qPos, permissions);
199: setGroupIds(qPos, groups);
200: }
201:
202: setPermissionIds(qPos, permissions);
203: qPos.add(userId);
204:
205: qPos.add(groupId);
206: setPermissionIds(qPos, permissions);
207: qPos.add(userId);
208:
209: setPermissionIds(qPos, permissions);
210: qPos.add(userId);
211:
212: Iterator itr = q.list().iterator();
213:
214: while (itr.hasNext()) {
215: Long count = (Long) itr.next();
216:
217: if ((count != null) && (count.intValue() > 0)) {
218: return true;
219: }
220: }
221:
222: return false;
223: } catch (Exception e) {
224: throw new SystemException(e);
225: } finally {
226: HibernateUtil.closeSession(session);
227: }
228: }
229:
230: public boolean containsPermissions_4(List permissions, long userId,
231: List groups, List roles) throws SystemException {
232:
233: Session session = null;
234:
235: try {
236: session = HibernateUtil.openSession();
237:
238: String sql = null;
239:
240: StringMaker sm = new StringMaker();
241:
242: if (groups.size() > 0) {
243: sm.append("(");
244: sm.append(CustomSQLUtil
245: .get(COUNT_BY_GROUPS_PERMISSIONS));
246: sm.append(") ");
247:
248: sql = sm.toString();
249:
250: sql = StringUtil.replace(sql, "[$PERMISSION_IDS$]",
251: getPermissionIds(permissions,
252: "Groups_Permissions"));
253: sql = StringUtil.replace(sql, "[$GROUP_IDS$]",
254: getGroupIds(groups, "Groups_Permissions"));
255:
256: sm = new StringMaker();
257:
258: sm.append(sql);
259:
260: sm.append("UNION ALL ");
261: }
262:
263: if (roles.size() > 0) {
264: sm.append("(");
265: sm
266: .append(CustomSQLUtil
267: .get(COUNT_BY_ROLES_PERMISSIONS));
268: sm.append(") ");
269:
270: sql = sm.toString();
271:
272: sql = StringUtil.replace(sql, "[$PERMISSION_IDS$]",
273: getPermissionIds(permissions,
274: "Roles_Permissions"));
275: sql = StringUtil.replace(sql, "[$ROLE_IDS$]",
276: getRoleIds(roles, "Roles_Permissions"));
277:
278: sm = new StringMaker();
279:
280: sm.append(sql);
281:
282: sm.append("UNION ALL ");
283: }
284:
285: sm.append("(");
286: sm.append(CustomSQLUtil.get(COUNT_BY_USERS_PERMISSIONS));
287: sm.append(") ");
288:
289: sql = sm.toString();
290:
291: sql = StringUtil.replace(sql, "[$PERMISSION_IDS$]",
292: getPermissionIds(permissions, "Users_Permissions"));
293:
294: SQLQuery q = session.createSQLQuery(sql);
295:
296: q.addScalar(HibernateUtil.getCountColumnName(),
297: Hibernate.LONG);
298:
299: QueryPos qPos = QueryPos.getInstance(q);
300:
301: if (groups.size() > 0) {
302: setPermissionIds(qPos, permissions);
303: setGroupIds(qPos, groups);
304: }
305:
306: if (roles.size() > 0) {
307: setPermissionIds(qPos, permissions);
308: setRoleIds(qPos, roles);
309: }
310:
311: setPermissionIds(qPos, permissions);
312: qPos.add(userId);
313:
314: Iterator itr = q.list().iterator();
315:
316: while (itr.hasNext()) {
317: Long count = (Long) itr.next();
318:
319: if ((count != null) && (count.intValue() > 0)) {
320: return true;
321: }
322: }
323:
324: return false;
325: } catch (Exception e) {
326: throw new SystemException(e);
327: } finally {
328: HibernateUtil.closeSession(session);
329: }
330: }
331:
332: public int countByGroupsPermissions(List permissions, List groups)
333: throws SystemException {
334:
335: Session session = null;
336:
337: try {
338: session = HibernateUtil.openSession();
339:
340: String sql = CustomSQLUtil.get(COUNT_BY_GROUPS_PERMISSIONS);
341:
342: sql = StringUtil
343: .replace(sql, "[$PERMISSION_IDS$]",
344: getPermissionIds(permissions,
345: "Groups_Permissions"));
346: sql = StringUtil.replace(sql, "[$GROUP_IDS$]", getGroupIds(
347: groups, "Groups_Permissions"));
348:
349: SQLQuery q = session.createSQLQuery(sql);
350:
351: q.addScalar(HibernateUtil.getCountColumnName(),
352: Hibernate.LONG);
353:
354: QueryPos qPos = QueryPos.getInstance(q);
355:
356: setPermissionIds(qPos, permissions);
357: setGroupIds(qPos, groups);
358:
359: Iterator itr = q.list().iterator();
360:
361: if (itr.hasNext()) {
362: Long count = (Long) itr.next();
363:
364: if (count != null) {
365: return count.intValue();
366: }
367: }
368:
369: return 0;
370: } catch (Exception e) {
371: throw new SystemException(e);
372: } finally {
373: HibernateUtil.closeSession(session);
374: }
375: }
376:
377: public int countByGroupsRoles(List permissions, List groups)
378: throws SystemException {
379:
380: Session session = null;
381:
382: try {
383: session = HibernateUtil.openSession();
384:
385: String sql = CustomSQLUtil.get(COUNT_BY_GROUPS_ROLES);
386:
387: sql = StringUtil.replace(sql, "[$PERMISSION_IDS$]",
388: getPermissionIds(permissions, "Roles_Permissions"));
389: sql = StringUtil.replace(sql, "[$GROUP_IDS$]", getGroupIds(
390: groups, "Groups_Roles"));
391:
392: SQLQuery q = session.createSQLQuery(sql);
393:
394: q.addScalar(HibernateUtil.getCountColumnName(),
395: Hibernate.LONG);
396:
397: QueryPos qPos = QueryPos.getInstance(q);
398:
399: setPermissionIds(qPos, permissions);
400: setGroupIds(qPos, groups);
401:
402: Iterator itr = q.list().iterator();
403:
404: if (itr.hasNext()) {
405: Long count = (Long) itr.next();
406:
407: if (count != null) {
408: return count.intValue();
409: }
410: }
411:
412: return 0;
413: } catch (Exception e) {
414: throw new SystemException(e);
415: } finally {
416: HibernateUtil.closeSession(session);
417: }
418: }
419:
420: public int countByRolesPermissions(List permissions, List roles)
421: throws SystemException {
422:
423: Session session = null;
424:
425: try {
426: session = HibernateUtil.openSession();
427:
428: String sql = CustomSQLUtil.get(COUNT_BY_ROLES_PERMISSIONS);
429:
430: sql = StringUtil.replace(sql, "[$PERMISSION_IDS$]",
431: getPermissionIds(permissions, "Roles_Permissions"));
432: sql = StringUtil.replace(sql, "[$ROLE_IDS$]", getRoleIds(
433: roles, "Roles_Permissions"));
434:
435: SQLQuery q = session.createSQLQuery(sql);
436:
437: q.addScalar(HibernateUtil.getCountColumnName(),
438: Hibernate.LONG);
439:
440: QueryPos qPos = QueryPos.getInstance(q);
441:
442: setPermissionIds(qPos, permissions);
443: setRoleIds(qPos, roles);
444:
445: Iterator itr = q.list().iterator();
446:
447: if (itr.hasNext()) {
448: Long count = (Long) itr.next();
449:
450: if (count != null) {
451: return count.intValue();
452: }
453: }
454:
455: return 0;
456: } catch (Exception e) {
457: throw new SystemException(e);
458: } finally {
459: HibernateUtil.closeSession(session);
460: }
461: }
462:
463: public int countByUserGroupRole(List permissions, long userId,
464: long groupId) throws SystemException {
465:
466: Session session = null;
467:
468: try {
469: session = HibernateUtil.openSession();
470:
471: String sql = CustomSQLUtil.get(COUNT_BY_USER_GROUP_ROLE);
472:
473: sql = StringUtil.replace(sql, "[$PERMISSION_IDS$]",
474: getPermissionIds(permissions, "Roles_Permissions"));
475:
476: SQLQuery q = session.createSQLQuery(sql);
477:
478: q.addScalar(HibernateUtil.getCountColumnName(),
479: Hibernate.LONG);
480:
481: QueryPos qPos = QueryPos.getInstance(q);
482:
483: qPos.add(groupId);
484: setPermissionIds(qPos, permissions);
485: qPos.add(userId);
486:
487: Iterator itr = q.list().iterator();
488:
489: if (itr.hasNext()) {
490: Long count = (Long) itr.next();
491:
492: if (count != null) {
493: return count.intValue();
494: }
495: }
496:
497: return 0;
498: } catch (Exception e) {
499: throw new SystemException(e);
500: } finally {
501: HibernateUtil.closeSession(session);
502: }
503: }
504:
505: public int countByUsersPermissions(List permissions, long userId)
506: throws SystemException {
507:
508: Session session = null;
509:
510: try {
511: session = HibernateUtil.openSession();
512:
513: String sql = CustomSQLUtil.get(COUNT_BY_USERS_PERMISSIONS);
514:
515: sql = StringUtil.replace(sql, "[$PERMISSION_IDS$]",
516: getPermissionIds(permissions, "Users_Permissions"));
517:
518: SQLQuery q = session.createSQLQuery(sql);
519:
520: q.addScalar(HibernateUtil.getCountColumnName(),
521: Hibernate.LONG);
522:
523: QueryPos qPos = QueryPos.getInstance(q);
524:
525: setPermissionIds(qPos, permissions);
526: qPos.add(userId);
527:
528: Iterator itr = q.list().iterator();
529:
530: if (itr.hasNext()) {
531: Long count = (Long) itr.next();
532:
533: if (count != null) {
534: return count.intValue();
535: }
536: }
537:
538: return 0;
539: } catch (Exception e) {
540: throw new SystemException(e);
541: } finally {
542: HibernateUtil.closeSession(session);
543: }
544: }
545:
546: public int countByUsersRoles(List permissions, long userId)
547: throws SystemException {
548:
549: Session session = null;
550:
551: try {
552: session = HibernateUtil.openSession();
553:
554: String sql = CustomSQLUtil.get(COUNT_BY_USERS_ROLES);
555:
556: sql = StringUtil.replace(sql, "[$PERMISSION_IDS$]",
557: getPermissionIds(permissions, "Roles_Permissions"));
558:
559: SQLQuery q = session.createSQLQuery(sql);
560:
561: q.addScalar(HibernateUtil.getCountColumnName(),
562: Hibernate.LONG);
563:
564: QueryPos qPos = QueryPos.getInstance(q);
565:
566: setPermissionIds(qPos, permissions);
567: qPos.add(userId);
568:
569: Iterator itr = q.list().iterator();
570:
571: if (itr.hasNext()) {
572: Long count = (Long) itr.next();
573:
574: if (count != null) {
575: return count.intValue();
576: }
577: }
578:
579: return 0;
580: } catch (Exception e) {
581: throw new SystemException(e);
582: } finally {
583: HibernateUtil.closeSession(session);
584: }
585: }
586:
587: public List findByA_R(String actionId, long[] resourceIds)
588: throws SystemException {
589:
590: boolean finderClassNameCacheEnabled = PermissionModelImpl.CACHE_ENABLED;
591: String finderClassName = Permission.class.getName();
592: String finderMethodName = "customFindByA_R";
593: String finderParams[] = new String[] { String.class.getName(),
594: "[L" + Long.class.getName() };
595: Object finderArgs[] = new Object[] { actionId,
596: StringUtil.merge(ArrayUtil.toObjectArray(resourceIds)) };
597:
598: Object result = FinderCache.getResult(finderClassName,
599: finderMethodName, finderParams, finderArgs);
600:
601: if (result == null) {
602: Session session = null;
603:
604: try {
605: session = HibernateUtil.openSession();
606:
607: String sql = CustomSQLUtil.get(FIND_BY_A_R);
608:
609: sql = StringUtil.replace(sql, "[$RESOURCE_IDS$]",
610: getResourceIds(resourceIds));
611:
612: SQLQuery q = session.createSQLQuery(sql);
613:
614: q.addEntity("Permission_", PermissionImpl.class);
615:
616: QueryPos qPos = QueryPos.getInstance(q);
617:
618: qPos.add(actionId);
619: setResourceIds(qPos, resourceIds);
620:
621: List list = q.list();
622:
623: FinderCache.putResult(finderClassNameCacheEnabled,
624: finderClassName, finderMethodName,
625: finderParams, finderArgs, list);
626:
627: return list;
628: } catch (Exception e) {
629: throw new SystemException(e);
630: } finally {
631: HibernateUtil.closeSession(session);
632: }
633: } else {
634: return (List) result;
635: }
636: }
637:
638: public List findByG_R(long groupId, long resourceId)
639: throws SystemException {
640:
641: Session session = null;
642:
643: try {
644: session = HibernateUtil.openSession();
645:
646: String sql = CustomSQLUtil.get(FIND_BY_G_R);
647:
648: SQLQuery q = session.createSQLQuery(sql);
649:
650: q.addEntity("Permission_", PermissionImpl.class);
651:
652: QueryPos qPos = QueryPos.getInstance(q);
653:
654: qPos.add(groupId);
655: qPos.add(resourceId);
656:
657: return q.list();
658: } catch (Exception e) {
659: throw new SystemException(e);
660: } finally {
661: HibernateUtil.closeSession(session);
662: }
663: }
664:
665: public List findByR_R(long roleId, long resourceId)
666: throws SystemException {
667: Session session = null;
668:
669: try {
670: session = HibernateUtil.openSession();
671:
672: String sql = CustomSQLUtil.get(FIND_BY_R_R);
673:
674: SQLQuery q = session.createSQLQuery(sql);
675:
676: q.addEntity("Permission_", PermissionImpl.class);
677:
678: QueryPos qPos = QueryPos.getInstance(q);
679:
680: qPos.add(roleId);
681: qPos.add(resourceId);
682:
683: return q.list();
684: } catch (Exception e) {
685: throw new SystemException(e);
686: } finally {
687: HibernateUtil.closeSession(session);
688: }
689: }
690:
691: public List findByU_R(long userId, long resourceId)
692: throws SystemException {
693: Session session = null;
694:
695: try {
696: session = HibernateUtil.openSession();
697:
698: String sql = CustomSQLUtil.get(FIND_BY_U_R);
699:
700: SQLQuery q = session.createSQLQuery(sql);
701:
702: q.addEntity("Permission_", PermissionImpl.class);
703:
704: QueryPos qPos = QueryPos.getInstance(q);
705:
706: qPos.add(userId);
707: qPos.add(resourceId);
708:
709: return q.list();
710: } catch (Exception e) {
711: throw new SystemException(e);
712: } finally {
713: HibernateUtil.closeSession(session);
714: }
715: }
716:
717: public List findByO_G_R(long organizationId, long groupId,
718: long resourceId) throws SystemException {
719:
720: Session session = null;
721:
722: try {
723: session = HibernateUtil.openSession();
724:
725: String sql = CustomSQLUtil.get(FIND_BY_O_G_R);
726:
727: SQLQuery q = session.createSQLQuery(sql);
728:
729: q.addEntity("Permission_", PermissionImpl.class);
730:
731: QueryPos qPos = QueryPos.getInstance(q);
732:
733: qPos.add(organizationId);
734: qPos.add(groupId);
735: qPos.add(resourceId);
736:
737: return q.list();
738: } catch (Exception e) {
739: throw new SystemException(e);
740: } finally {
741: HibernateUtil.closeSession(session);
742: }
743: }
744:
745: public List findByU_A_R(long userId, String[] actionIds,
746: long resourceId) throws SystemException {
747:
748: Session session = null;
749:
750: try {
751: session = HibernateUtil.openSession();
752:
753: String sql = CustomSQLUtil.get(FIND_BY_U_R);
754:
755: sql = StringUtil.replace(sql, "[$ACTION_IDS$]",
756: getActionIds(actionIds));
757:
758: SQLQuery q = session.createSQLQuery(sql);
759:
760: q.addEntity("Permission_", PermissionImpl.class);
761:
762: QueryPos qPos = QueryPos.getInstance(q);
763:
764: qPos.add(userId);
765: qPos.add(resourceId);
766:
767: return q.list();
768: } catch (Exception e) {
769: throw new SystemException(e);
770: } finally {
771: HibernateUtil.closeSession(session);
772: }
773: }
774:
775: public List findByG_C_N_S_P(long groupId, long companyId,
776: String name, int scope, String primKey)
777: throws SystemException {
778:
779: Session session = null;
780:
781: try {
782: session = HibernateUtil.openSession();
783:
784: String sql = CustomSQLUtil.get(FIND_BY_G_C_N_S_P);
785:
786: SQLQuery q = session.createSQLQuery(sql);
787:
788: q.addEntity("Permission_", PermissionImpl.class);
789:
790: QueryPos qPos = QueryPos.getInstance(q);
791:
792: qPos.add(groupId);
793: qPos.add(companyId);
794: qPos.add(name);
795: qPos.add(scope);
796: qPos.add(primKey);
797:
798: return q.list();
799: } catch (Exception e) {
800: throw new SystemException(e);
801: } finally {
802: HibernateUtil.closeSession(session);
803: }
804: }
805:
806: public List findByU_C_N_S_P(long userId, long companyId,
807: String name, int scope, String primKey)
808: throws SystemException {
809:
810: Session session = null;
811:
812: try {
813: session = HibernateUtil.openSession();
814:
815: String sql = CustomSQLUtil.get(FIND_BY_U_C_N_S_P);
816:
817: SQLQuery q = session.createSQLQuery(sql);
818:
819: q.addEntity("Permission_", PermissionImpl.class);
820:
821: QueryPos qPos = QueryPos.getInstance(q);
822:
823: qPos.add(userId);
824: qPos.add(companyId);
825: qPos.add(name);
826: qPos.add(scope);
827: qPos.add(primKey);
828:
829: return q.list();
830: } catch (Exception e) {
831: throw new SystemException(e);
832: } finally {
833: HibernateUtil.closeSession(session);
834: }
835: }
836:
837: protected String getActionIds(String[] actionIds) {
838: StringMaker sm = new StringMaker();
839:
840: for (int i = 0; i < actionIds.length; i++) {
841: sm.append("Permission_.actionId = ?");
842:
843: if ((i + 1) < actionIds.length) {
844: sm.append(" OR ");
845: }
846: }
847:
848: return sm.toString();
849: }
850:
851: protected String getGroupIds(List groups, String table) {
852: StringMaker sm = new StringMaker();
853:
854: for (int i = 0; i < groups.size(); i++) {
855: sm.append(table);
856: sm.append(".groupId = ?");
857:
858: if ((i + 1) < groups.size()) {
859: sm.append(" OR ");
860: }
861: }
862:
863: return sm.toString();
864: }
865:
866: protected String getPermissionIds(List permissions, String table) {
867: StringMaker sm = new StringMaker();
868:
869: for (int i = 0; i < permissions.size(); i++) {
870: sm.append(table);
871: sm.append(".permissionId = ?");
872:
873: if ((i + 1) < permissions.size()) {
874: sm.append(" OR ");
875: }
876: }
877:
878: return sm.toString();
879: }
880:
881: protected String getResourceIds(long[] resourceIds) {
882: StringMaker sm = new StringMaker();
883:
884: for (int i = 0; i < resourceIds.length; i++) {
885: sm.append("resourceId = ?");
886:
887: if ((i + 1) < resourceIds.length) {
888: sm.append(" OR ");
889: }
890: }
891:
892: return sm.toString();
893: }
894:
895: protected String getRoleIds(List roles, String table) {
896: StringMaker sm = new StringMaker();
897:
898: for (int i = 0; i < roles.size(); i++) {
899: sm.append(table);
900: sm.append(".roleId = ?");
901:
902: if ((i + 1) < roles.size()) {
903: sm.append(" OR ");
904: }
905: }
906:
907: return sm.toString();
908: }
909:
910: protected void setGroupIds(QueryPos qPos, List groups) {
911: for (int i = 0; i < groups.size(); i++) {
912: Group group = (Group) groups.get(i);
913:
914: qPos.add(group.getGroupId());
915: }
916: }
917:
918: protected void setPermissionIds(QueryPos qPos, List permissions) {
919: for (int i = 0; i < permissions.size(); i++) {
920: Permission permission = (Permission) permissions.get(i);
921:
922: qPos.add(permission.getPermissionId());
923: }
924: }
925:
926: protected void setResourceIds(QueryPos qPos, long[] resourceIds) {
927: for (int i = 0; i < resourceIds.length; i++) {
928: long resourceId = resourceIds[i];
929:
930: qPos.add(resourceId);
931: }
932: }
933:
934: protected void setRoleIds(QueryPos qPos, List roles) {
935: for (int i = 0; i < roles.size(); i++) {
936: Role role = (Role) roles.get(i);
937:
938: qPos.add(role.getRoleId());
939: }
940: }
941:
942: }
|