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