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