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