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