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