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