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