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