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