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