001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.service.persistence;
016    
017    import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
018    import com.liferay.portal.kernel.dao.orm.FinderPath;
019    import com.liferay.portal.kernel.dao.orm.QueryPos;
020    import com.liferay.portal.kernel.dao.orm.SQLQuery;
021    import com.liferay.portal.kernel.dao.orm.Session;
022    import com.liferay.portal.kernel.dao.orm.Type;
023    import com.liferay.portal.kernel.exception.SystemException;
024    import com.liferay.portal.kernel.util.ArrayUtil;
025    import com.liferay.portal.kernel.util.ListUtil;
026    import com.liferay.portal.kernel.util.StringBundler;
027    import com.liferay.portal.kernel.util.StringPool;
028    import com.liferay.portal.kernel.util.StringUtil;
029    import com.liferay.portal.kernel.util.Validator;
030    import com.liferay.portal.model.Group;
031    import com.liferay.portal.model.Permission;
032    import com.liferay.portal.model.Role;
033    import com.liferay.portal.model.impl.PermissionImpl;
034    import com.liferay.portal.model.impl.PermissionModelImpl;
035    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
036    import com.liferay.util.dao.orm.CustomSQLUtil;
037    
038    import java.util.ArrayList;
039    import java.util.Iterator;
040    import java.util.List;
041    
042    /**
043     * @author Brian Wing Shun Chan
044     */
045    public class PermissionFinderImpl
046            extends BasePersistenceImpl<Permission> implements PermissionFinder {
047    
048            public static final String COUNT_BY_GROUPS_PERMISSIONS =
049                    PermissionFinder.class.getName() + ".countByGroupsPermissions";
050    
051            public static final String COUNT_BY_GROUPS_ROLES =
052                    PermissionFinder.class.getName() + ".countByGroupsRoles";
053    
054            public static final String COUNT_BY_ROLES_PERMISSIONS =
055                    PermissionFinder.class.getName() + ".countByRolesPermissions";
056    
057            public static final String COUNT_BY_USER_GROUP_ROLE =
058                    PermissionFinder.class.getName() + ".countByUserGroupRole";
059    
060            public static final String COUNT_BY_USERS_PERMISSIONS =
061                    PermissionFinder.class.getName() + ".countByUsersPermissions";
062    
063            public static final String COUNT_BY_USERS_ROLES =
064                    PermissionFinder.class.getName() + ".countByUsersRoles";
065    
066            public static final String COUNT_BY_R_A_C =
067                    PermissionFinder.class.getName() + ".countByR_A_C";
068    
069            public static final String FIND_BY_A_C =
070                    PermissionFinder.class.getName() + ".findByA_C";
071    
072            public static final String FIND_BY_A_R =
073                    PermissionFinder.class.getName() + ".findByA_R";
074    
075            public static final String FIND_BY_G_R =
076                    PermissionFinder.class.getName() + ".findByG_R";
077    
078            public static final String FIND_BY_R_R =
079                    PermissionFinder.class.getName() + ".findByR_R";
080    
081            public static final String FIND_BY_R_S =
082                    PermissionFinder.class.getName() + ".findByR_S";
083    
084            public static final String FIND_BY_U_R =
085                    PermissionFinder.class.getName() + ".findByU_R";
086    
087            public static final String FIND_BY_O_G_R =
088                    PermissionFinder.class.getName() + ".findByO_G_R";
089    
090            public static final String FIND_BY_U_A_R =
091                    PermissionFinder.class.getName() + ".findByU_A_R";
092    
093            public static final String FIND_BY_G_C_N_S_P =
094                    PermissionFinder.class.getName() + ".findByG_C_N_S_P";
095    
096            public static final String FIND_BY_U_C_N_S_P =
097                    PermissionFinder.class.getName() + ".findByU_C_N_S_P";
098    
099            public static final FinderPath FINDER_PATH_COUNT_BY_ROLES_PERMISSIONS =
100                    new FinderPath(
101                            PermissionModelImpl.ENTITY_CACHE_ENABLED,
102                            PermissionModelImpl.FINDER_CACHE_ENABLED, Long.class,
103                            "Roles_Permissions", "customCountByRolesPermissions",
104                            new String[] {
105                                    java.util.List.class.getName(), java.util.List.class.getName()
106                            });
107    
108            public static final FinderPath FINDER_PATH_FIND_BY_A_R = new FinderPath(
109                    PermissionModelImpl.ENTITY_CACHE_ENABLED,
110                    PermissionModelImpl.FINDER_CACHE_ENABLED, PermissionImpl.class,
111                    PermissionPersistenceImpl.FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION,
112                    "customFindByA_R",
113                    new String[] {
114                            String.class.getName(), "[L" + Long.class.getName()
115                    });
116    
117            public boolean containsPermissions_2(
118                            List<Permission> permissions, long userId, List<Group> groups,
119                            long groupId)
120                    throws SystemException {
121    
122                    Session session = null;
123    
124                    try {
125                            session = openSession();
126    
127                            String sql = null;
128    
129                            StringBundler sb = new StringBundler();
130    
131                            if (groups.size() > 0) {
132                                    sb.append("(");
133                                    sb.append(CustomSQLUtil.get(COUNT_BY_GROUPS_ROLES));
134                                    sb.append(") ");
135    
136                                    sql = sb.toString();
137    
138                                    sql = StringUtil.replace(
139                                            sql, "[$PERMISSION_ID$]",
140                                            getPermissionIds(permissions, "Roles_Permissions"));
141                                    sql = StringUtil.replace(
142                                            sql, "[$GROUP_ID$]", getGroupIds(groups, "Groups_Roles"));
143    
144                                    sb.setIndex(0);
145    
146                                    sb.append(sql);
147    
148                                    sb.append("UNION ALL (");
149                                    sb.append(CustomSQLUtil.get(COUNT_BY_GROUPS_PERMISSIONS));
150                                    sb.append(") ");
151    
152                                    sql = sb.toString();
153    
154                                    sql = StringUtil.replace(
155                                            sql, "[$PERMISSION_ID$]",
156                                            getPermissionIds(permissions, "Groups_Permissions"));
157                                    sql = StringUtil.replace(
158                                            sql, "[$GROUP_ID$]",
159                                            getGroupIds(groups, "Groups_Permissions"));
160    
161                                    sb.setIndex(0);
162    
163                                    sb.append(sql);
164    
165                                    sb.append("UNION ALL ");
166                            }
167    
168                            sb.append("(");
169                            sb.append(CustomSQLUtil.get(COUNT_BY_USERS_ROLES));
170                            sb.append(") ");
171    
172                            sql = sb.toString();
173    
174                            sql = StringUtil.replace(
175                                    sql, "[$PERMISSION_ID$]",
176                                    getPermissionIds(permissions, "Roles_Permissions"));
177    
178                            sb.setIndex(0);
179    
180                            sb.append(sql);
181    
182                            sb.append("UNION ALL (");
183                            sb.append(CustomSQLUtil.get(COUNT_BY_USER_GROUP_ROLE));
184                            sb.append(") ");
185    
186                            sql = sb.toString();
187    
188                            sql = StringUtil.replace(
189                                    sql, "[$PERMISSION_ID$]",
190                                    getPermissionIds(permissions, "Roles_Permissions"));
191    
192                            sb.setIndex(0);
193    
194                            sb.append(sql);
195    
196                            sb.append("UNION ALL (");
197                            sb.append(CustomSQLUtil.get(COUNT_BY_USERS_PERMISSIONS));
198                            sb.append(") ");
199    
200                            sql = sb.toString();
201    
202                            sql = StringUtil.replace(
203                                    sql, "[$PERMISSION_ID$]",
204                                    getPermissionIds(permissions, "Users_Permissions"));
205    
206                            SQLQuery q = session.createSQLQuery(sql);
207    
208                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
209    
210                            QueryPos qPos = QueryPos.getInstance(q);
211    
212                            if (groups.size() > 0) {
213                                    setPermissionIds(qPos, permissions);
214                                    setGroupIds(qPos, groups);
215                                    setPermissionIds(qPos, permissions);
216                                    setGroupIds(qPos, groups);
217                            }
218    
219                            setPermissionIds(qPos, permissions);
220                            qPos.add(userId);
221    
222                            qPos.add(groupId);
223                            setPermissionIds(qPos, permissions);
224                            qPos.add(userId);
225    
226                            setPermissionIds(qPos, permissions);
227                            qPos.add(userId);
228    
229                            Iterator<Long> itr = q.iterate();
230    
231                            while (itr.hasNext()) {
232                                    Long count = itr.next();
233    
234                                    if ((count != null) && (count.intValue() > 0)) {
235                                            return true;
236                                    }
237                            }
238    
239                            return false;
240                    }
241                    catch (Exception e) {
242                            throw new SystemException(e);
243                    }
244                    finally {
245                            closeSession(session);
246                    }
247            }
248    
249            public boolean containsPermissions_4(
250                            List<Permission> permissions, long userId, List<Group> groups,
251                            List<Role> roles)
252                    throws SystemException {
253    
254                    Session session = null;
255    
256                    try {
257                            session = openSession();
258    
259                            String sql = null;
260    
261                            StringBundler sb = new StringBundler();
262    
263                            if (groups.size() > 0) {
264                                    sb.append("(");
265                                    sb.append(CustomSQLUtil.get(COUNT_BY_GROUPS_PERMISSIONS));
266                                    sb.append(") ");
267    
268                                    sql = sb.toString();
269    
270                                    sql = StringUtil.replace(
271                                            sql, "[$PERMISSION_ID$]",
272                                            getPermissionIds(permissions, "Groups_Permissions"));
273                                    sql = StringUtil.replace(
274                                            sql, "[$GROUP_ID$]",
275                                            getGroupIds(groups, "Groups_Permissions"));
276    
277                                    sb.setIndex(0);
278    
279                                    sb.append(sql);
280    
281                                    sb.append("UNION ALL ");
282                            }
283    
284                            if (roles.size() > 0) {
285                                    sb.append("(");
286                                    sb.append(CustomSQLUtil.get(COUNT_BY_ROLES_PERMISSIONS));
287                                    sb.append(") ");
288    
289                                    sql = sb.toString();
290    
291                                    sql = StringUtil.replace(
292                                            sql, "[$PERMISSION_ID$]",
293                                            getPermissionIds(permissions, "Roles_Permissions"));
294                                    sql = StringUtil.replace(
295                                            sql, "[$ROLE_ID$]", getRoleIds(roles, "Roles_Permissions"));
296    
297                                    sb.setIndex(0);
298    
299                                    sb.append(sql);
300    
301                                    sb.append("UNION ALL ");
302                            }
303    
304                            sb.append("(");
305                            sb.append(CustomSQLUtil.get(COUNT_BY_USERS_PERMISSIONS));
306                            sb.append(") ");
307    
308                            sql = sb.toString();
309    
310                            sql = StringUtil.replace(
311                                    sql, "[$PERMISSION_ID$]",
312                                    getPermissionIds(permissions, "Users_Permissions"));
313    
314                            SQLQuery q = session.createSQLQuery(sql);
315    
316                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
317    
318                            QueryPos qPos = QueryPos.getInstance(q);
319    
320                            if (groups.size() > 0) {
321                                    setPermissionIds(qPos, permissions);
322                                    setGroupIds(qPos, groups);
323                            }
324    
325                            if (roles.size() > 0) {
326                                    setPermissionIds(qPos, permissions);
327                                    setRoleIds(qPos, roles);
328                            }
329    
330                            setPermissionIds(qPos, permissions);
331                            qPos.add(userId);
332    
333                            Iterator<Long> itr = q.iterate();
334    
335                            while (itr.hasNext()) {
336                                    Long count = itr.next();
337    
338                                    if ((count != null) && (count.intValue() > 0)) {
339                                            return true;
340                                    }
341                            }
342    
343                            return false;
344                    }
345                    catch (Exception e) {
346                            throw new SystemException(e);
347                    }
348                    finally {
349                            closeSession(session);
350                    }
351            }
352    
353            public int countByGroupsPermissions(
354                            List<Permission> permissions, List<Group> groups)
355                    throws SystemException {
356    
357                    Session session = null;
358    
359                    try {
360                            session = openSession();
361    
362                            String sql = CustomSQLUtil.get(COUNT_BY_GROUPS_PERMISSIONS);
363    
364                            sql = StringUtil.replace(
365                                    sql, "[$PERMISSION_ID$]",
366                                    getPermissionIds(permissions, "Groups_Permissions"));
367                            sql = StringUtil.replace(
368                                    sql, "[$GROUP_ID$]", 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.iterate();
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_ID$]",
412                                    getPermissionIds(permissions, "Roles_Permissions"));
413                            sql = StringUtil.replace(
414                                    sql, "[$GROUP_ID$]", 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.iterate();
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, Permission.PERMISSION_ID_ACCESSOR),
451                            ListUtil.toString(roles, Role.ROLE_ID_ACCESSOR)
452                    };
453    
454                    Long count = (Long)FinderCacheUtil.getResult(
455                            FINDER_PATH_COUNT_BY_ROLES_PERMISSIONS, finderArgs, this);
456    
457                    if (count != null) {
458                            return count.intValue();
459                    }
460    
461                    Session session = null;
462    
463                    try {
464                            session = openSession();
465    
466                            String sql = CustomSQLUtil.get(COUNT_BY_ROLES_PERMISSIONS);
467    
468                            sql = StringUtil.replace(
469                                    sql, "[$PERMISSION_ID$]",
470                                    getPermissionIds(permissions, "Roles_Permissions"));
471                            sql = StringUtil.replace(
472                                    sql, "[$ROLE_ID$]", getRoleIds(roles, "Roles_Permissions"));
473    
474                            SQLQuery q = session.createSQLQuery(sql);
475    
476                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
477    
478                            QueryPos qPos = QueryPos.getInstance(q);
479    
480                            setPermissionIds(qPos, permissions);
481                            setRoleIds(qPos, roles);
482    
483                            count = (Long)q.uniqueResult();
484                    }
485                    catch (Exception e) {
486                            throw new SystemException(e);
487                    }
488                    finally {
489                            if (count == null) {
490                                    count = Long.valueOf(0);
491                            }
492    
493                            FinderCacheUtil.putResult(
494                                    FINDER_PATH_COUNT_BY_ROLES_PERMISSIONS, finderArgs, count);
495    
496                            closeSession(session);
497                    }
498    
499                    return count.intValue();
500            }
501    
502            public int countByUserGroupRole(
503                            List<Permission> permissions, long userId, long groupId)
504                    throws SystemException {
505    
506                    Session session = null;
507    
508                    try {
509                            session = openSession();
510    
511                            String sql = CustomSQLUtil.get(COUNT_BY_USER_GROUP_ROLE);
512    
513                            sql = StringUtil.replace(
514                                    sql, "[$PERMISSION_ID$]",
515                                    getPermissionIds(permissions, "Roles_Permissions"));
516    
517                            SQLQuery q = session.createSQLQuery(sql);
518    
519                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
520    
521                            QueryPos qPos = QueryPos.getInstance(q);
522    
523                            qPos.add(groupId);
524                            setPermissionIds(qPos, permissions);
525                            qPos.add(userId);
526    
527                            Iterator<Long> itr = q.iterate();
528    
529                            if (itr.hasNext()) {
530                                    Long count = itr.next();
531    
532                                    if (count != null) {
533                                            return count.intValue();
534                                    }
535                            }
536    
537                            return 0;
538                    }
539                    catch (Exception e) {
540                            throw new SystemException(e);
541                    }
542                    finally {
543                            closeSession(session);
544                    }
545            }
546    
547            public int countByUsersPermissions(
548                            List<Permission> permissions, long userId)
549                    throws SystemException {
550    
551                    Session session = null;
552    
553                    try {
554                            session = openSession();
555    
556                            String sql = CustomSQLUtil.get(COUNT_BY_USERS_PERMISSIONS);
557    
558                            sql = StringUtil.replace(
559                                    sql, "[$PERMISSION_ID$]",
560                                    getPermissionIds(permissions, "Users_Permissions"));
561    
562                            SQLQuery q = session.createSQLQuery(sql);
563    
564                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
565    
566                            QueryPos qPos = QueryPos.getInstance(q);
567    
568                            setPermissionIds(qPos, permissions);
569                            qPos.add(userId);
570    
571                            Iterator<Long> itr = q.iterate();
572    
573                            if (itr.hasNext()) {
574                                    Long count = itr.next();
575    
576                                    if (count != null) {
577                                            return count.intValue();
578                                    }
579                            }
580    
581                            return 0;
582                    }
583                    catch (Exception e) {
584                            throw new SystemException(e);
585                    }
586                    finally {
587                            closeSession(session);
588                    }
589            }
590    
591            public int countByUsersRoles(List<Permission> permissions, long userId)
592                    throws SystemException {
593    
594                    Session session = null;
595    
596                    try {
597                            session = openSession();
598    
599                            String sql = CustomSQLUtil.get(COUNT_BY_USERS_ROLES);
600    
601                            sql = StringUtil.replace(
602                                    sql, "[$PERMISSION_ID$]",
603                                    getPermissionIds(permissions, "Roles_Permissions"));
604    
605                            SQLQuery q = session.createSQLQuery(sql);
606    
607                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
608    
609                            QueryPos qPos = QueryPos.getInstance(q);
610    
611                            setPermissionIds(qPos, permissions);
612                            qPos.add(userId);
613    
614                            Iterator<Long> itr = q.iterate();
615    
616                            if (itr.hasNext()) {
617                                    Long count = itr.next();
618    
619                                    if (count != null) {
620                                            return count.intValue();
621                                    }
622                            }
623    
624                            return 0;
625                    }
626                    catch (Exception e) {
627                            throw new SystemException(e);
628                    }
629                    finally {
630                            closeSession(session);
631                    }
632            }
633    
634            public int countByR_A_C(long roleId, String actionId, long codeId)
635                    throws SystemException {
636    
637                    Session session = null;
638    
639                    try {
640                            session = openSession();
641    
642                            String sql = CustomSQLUtil.get(COUNT_BY_R_A_C);
643    
644                            SQLQuery q = session.createSQLQuery(sql);
645    
646                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
647    
648                            QueryPos qPos = QueryPos.getInstance(q);
649    
650                            qPos.add(roleId);
651                            qPos.add(actionId);
652                            qPos.add(codeId);
653    
654                            Iterator<Long> itr = q.iterate();
655    
656                            if (itr.hasNext()) {
657                                    Long count = itr.next();
658    
659                                    if (count != null) {
660                                            return count.intValue();
661                                    }
662                            }
663    
664                            return 0;
665                    }
666                    catch (Exception e) {
667                            throw new SystemException(e);
668                    }
669                    finally {
670                            closeSession(session);
671                    }
672            }
673    
674            public List<Permission> findByA_C(String actionId, long codeId)
675                    throws SystemException {
676    
677                    Session session = null;
678    
679                    try {
680                            session = openSession();
681    
682                            String sql = CustomSQLUtil.get(FIND_BY_A_C);
683    
684                            SQLQuery q = session.createSQLQuery(sql);
685    
686                            q.addEntity("Permission_", PermissionImpl.class);
687    
688                            QueryPos qPos = QueryPos.getInstance(q);
689    
690                            qPos.add(actionId);
691                            qPos.add(codeId);
692    
693                            return q.list(true);
694                    }
695                    catch (Exception e) {
696                            throw new SystemException(e);
697                    }
698                    finally {
699                            closeSession(session);
700                    }
701            }
702    
703            public List<Permission> findByA_R(String actionId, long[] resourceIds)
704                    throws SystemException {
705    
706                    Object[] finderArgs = new Object[] {
707                            actionId, StringUtil.merge(ArrayUtil.toArray(resourceIds))
708                    };
709    
710                    List<Permission> list = (List<Permission>)FinderCacheUtil.getResult(
711                            FINDER_PATH_FIND_BY_A_R, finderArgs, this);
712    
713                    if ((list != null) && !list.isEmpty()) {
714                            for (Permission permission : list) {
715                                    if (!Validator.equals(actionId, permission.getActionId()) ||
716                                            !ArrayUtil.contains(
717                                                    resourceIds, permission.getResourceId())) {
718    
719                                            list = null;
720    
721                                            break;
722                                    }
723                            }
724                    }
725    
726                    if (list != null) {
727                            return list;
728                    }
729    
730                    Session session = null;
731    
732                    try {
733                            session = openSession();
734    
735                            String sql = CustomSQLUtil.get(FIND_BY_A_R);
736    
737                            sql = StringUtil.replace(
738                                    sql, "[$RESOURCE_ID$]", getResourceIds(resourceIds));
739    
740                            SQLQuery q = session.createSQLQuery(sql);
741    
742                            q.addEntity("Permission_", PermissionImpl.class);
743    
744                            QueryPos qPos = QueryPos.getInstance(q);
745    
746                            qPos.add(actionId);
747    
748                            setResourceIds(qPos, resourceIds);
749    
750                            list = q.list(true);
751                    }
752                    catch (Exception e) {
753                            throw new SystemException(e);
754                    }
755                    finally {
756                            if (list == null) {
757                                    list = new ArrayList<Permission>();
758                            }
759    
760                            FinderCacheUtil.putResult(
761                                    FINDER_PATH_FIND_BY_A_R, finderArgs, list);
762    
763                            closeSession(session);
764                    }
765    
766                    return list;
767            }
768    
769            public List<Permission> findByG_R(long groupId, long resourceId)
770                    throws SystemException {
771    
772                    Session session = null;
773    
774                    try {
775                            session = openSession();
776    
777                            String sql = CustomSQLUtil.get(FIND_BY_G_R);
778    
779                            SQLQuery q = session.createSQLQuery(sql);
780    
781                            q.addEntity("Permission_", PermissionImpl.class);
782    
783                            QueryPos qPos = QueryPos.getInstance(q);
784    
785                            qPos.add(groupId);
786                            qPos.add(resourceId);
787    
788                            return q.list(true);
789                    }
790                    catch (Exception e) {
791                            throw new SystemException(e);
792                    }
793                    finally {
794                            closeSession(session);
795                    }
796            }
797    
798            public List<Permission> findByR_R(long roleId, long resourceId)
799                    throws SystemException {
800    
801                    Session session = null;
802    
803                    try {
804                            session = openSession();
805    
806                            String sql = CustomSQLUtil.get(FIND_BY_R_R);
807    
808                            SQLQuery q = session.createSQLQuery(sql);
809    
810                            q.addEntity("Permission_", PermissionImpl.class);
811    
812                            QueryPos qPos = QueryPos.getInstance(q);
813    
814                            qPos.add(roleId);
815                            qPos.add(resourceId);
816    
817                            return q.list(true);
818                    }
819                    catch (Exception e) {
820                            throw new SystemException(e);
821                    }
822                    finally {
823                            closeSession(session);
824                    }
825            }
826    
827            public List<Permission> findByR_S(long roleId, int[] scopes)
828                    throws SystemException {
829    
830                    Session session = null;
831    
832                    try {
833                            session = openSession();
834    
835                            String sql = CustomSQLUtil.get(FIND_BY_R_S);
836    
837                            sql = StringUtil.replace(sql, "[$SCOPE$]", getScopes(scopes));
838    
839                            SQLQuery q = session.createSQLQuery(sql);
840    
841                            q.addEntity("Permission_", PermissionImpl.class);
842    
843                            QueryPos qPos = QueryPos.getInstance(q);
844    
845                            qPos.add(roleId);
846                            qPos.add(scopes);
847    
848                            return q.list(true);
849                    }
850                    catch (Exception e) {
851                            throw new SystemException(e);
852                    }
853                    finally {
854                            closeSession(session);
855                    }
856            }
857    
858            public List<Permission> findByU_R(long userId, long resourceId)
859                    throws SystemException {
860    
861                    Session session = null;
862    
863                    try {
864                            session = openSession();
865    
866                            String sql = CustomSQLUtil.get(FIND_BY_U_R);
867    
868                            SQLQuery q = session.createSQLQuery(sql);
869    
870                            q.addEntity("Permission_", PermissionImpl.class);
871    
872                            QueryPos qPos = QueryPos.getInstance(q);
873    
874                            qPos.add(userId);
875                            qPos.add(resourceId);
876    
877                            return q.list(true);
878                    }
879                    catch (Exception e) {
880                            throw new SystemException(e);
881                    }
882                    finally {
883                            closeSession(session);
884                    }
885            }
886    
887            public List<Permission> findByO_G_R(
888                            long organizationId, long groupId, long resourceId)
889                    throws SystemException {
890    
891                    Session session = null;
892    
893                    try {
894                            session = openSession();
895    
896                            String sql = CustomSQLUtil.get(FIND_BY_O_G_R);
897    
898                            SQLQuery q = session.createSQLQuery(sql);
899    
900                            q.addEntity("Permission_", PermissionImpl.class);
901    
902                            QueryPos qPos = QueryPos.getInstance(q);
903    
904                            qPos.add(organizationId);
905                            qPos.add(groupId);
906                            qPos.add(resourceId);
907    
908                            return q.list(true);
909                    }
910                    catch (Exception e) {
911                            throw new SystemException(e);
912                    }
913                    finally {
914                            closeSession(session);
915                    }
916            }
917    
918            public List<Permission> findByU_A_R(
919                            long userId, String[] actionIds, long resourceId)
920                    throws SystemException {
921    
922                    Session session = null;
923    
924                    try {
925                            session = openSession();
926    
927                            String sql = CustomSQLUtil.get(FIND_BY_U_R);
928    
929                            sql = StringUtil.replace(
930                                    sql, "[$ACTION_ID$]", getActionIds(actionIds));
931    
932                            SQLQuery q = session.createSQLQuery(sql);
933    
934                            q.addEntity("Permission_", PermissionImpl.class);
935    
936                            QueryPos qPos = QueryPos.getInstance(q);
937    
938                            qPos.add(userId);
939                            qPos.add(resourceId);
940    
941                            return q.list(true);
942                    }
943                    catch (Exception e) {
944                            throw new SystemException(e);
945                    }
946                    finally {
947                            closeSession(session);
948                    }
949            }
950    
951            public List<Permission> findByG_C_N_S_P(
952                            long groupId, long companyId, String name, int scope,
953                            String primKey)
954                    throws SystemException {
955    
956                    Session session = null;
957    
958                    try {
959                            session = openSession();
960    
961                            String sql = CustomSQLUtil.get(FIND_BY_G_C_N_S_P);
962    
963                            SQLQuery q = session.createSQLQuery(sql);
964    
965                            q.addEntity("Permission_", PermissionImpl.class);
966    
967                            QueryPos qPos = QueryPos.getInstance(q);
968    
969                            qPos.add(groupId);
970                            qPos.add(companyId);
971                            qPos.add(name);
972                            qPos.add(scope);
973                            qPos.add(primKey);
974    
975                            return q.list(true);
976                    }
977                    catch (Exception e) {
978                            throw new SystemException(e);
979                    }
980                    finally {
981                            closeSession(session);
982                    }
983            }
984    
985            public List<Permission> findByU_C_N_S_P(
986                            long userId, long companyId, String name, int scope, String primKey)
987                    throws SystemException {
988    
989                    Session session = null;
990    
991                    try {
992                            session = openSession();
993    
994                            String sql = CustomSQLUtil.get(FIND_BY_U_C_N_S_P);
995    
996                            SQLQuery q = session.createSQLQuery(sql);
997    
998                            q.addEntity("Permission_", PermissionImpl.class);
999    
1000                            QueryPos qPos = QueryPos.getInstance(q);
1001    
1002                            qPos.add(userId);
1003                            qPos.add(companyId);
1004                            qPos.add(name);
1005                            qPos.add(scope);
1006                            qPos.add(primKey);
1007    
1008                            return q.list(true);
1009                    }
1010                    catch (Exception e) {
1011                            throw new SystemException(e);
1012                    }
1013                    finally {
1014                            closeSession(session);
1015                    }
1016            }
1017    
1018            protected String getActionIds(String[] actionIds) {
1019                    if (actionIds.length == 0) {
1020                            return StringPool.BLANK;
1021                    }
1022    
1023                    StringBundler sb = new StringBundler(actionIds.length * 2 - 1);
1024    
1025                    for (int i = 0; i < actionIds.length; i++) {
1026                            sb.append("Permission_.actionId = ?");
1027    
1028                            if ((i + 1) < actionIds.length) {
1029                                    sb.append(" OR ");
1030                            }
1031                    }
1032    
1033                    return sb.toString();
1034            }
1035    
1036            protected String getGroupIds(List<Group> groups, String table) {
1037                    if (groups.isEmpty()) {
1038                            return StringPool.BLANK;
1039                    }
1040    
1041                    StringBundler sb = new StringBundler(groups.size() * 3 - 1);
1042    
1043                    for (int i = 0; i < groups.size(); i++) {
1044                            sb.append(table);
1045                            sb.append(".groupId = ?");
1046    
1047                            if ((i + 1) < groups.size()) {
1048                                    sb.append(" OR ");
1049                            }
1050                    }
1051    
1052                    return sb.toString();
1053            }
1054    
1055            protected String getPermissionIds(
1056                    List<Permission> permissions, String table) {
1057    
1058                    if (permissions.isEmpty()) {
1059                            return StringPool.BLANK;
1060                    }
1061    
1062                    StringBundler sb = new StringBundler(permissions.size() * 3 - 1);
1063    
1064                    for (int i = 0; i < permissions.size(); i++) {
1065                            sb.append(table);
1066                            sb.append(".permissionId = ?");
1067    
1068                            if ((i + 1) < permissions.size()) {
1069                                    sb.append(" OR ");
1070                            }
1071                    }
1072    
1073                    return sb.toString();
1074            }
1075    
1076            protected String getResourceIds(long[] resourceIds) {
1077                    if (resourceIds.length == 0) {
1078                            return StringPool.BLANK;
1079                    }
1080    
1081                    StringBundler sb = new StringBundler(resourceIds.length * 2 - 1);
1082    
1083                    for (int i = 0; i < resourceIds.length; i++) {
1084                            sb.append("resourceId = ?");
1085    
1086                            if ((i + 1) < resourceIds.length) {
1087                                    sb.append(" OR ");
1088                            }
1089                    }
1090    
1091                    return sb.toString();
1092            }
1093    
1094            protected String getRoleIds(List<Role> roles, String table) {
1095                    if (roles.isEmpty()) {
1096                            return StringPool.BLANK;
1097                    }
1098    
1099                    StringBundler sb = new StringBundler(roles.size() * 3 - 1);
1100    
1101                    for (int i = 0; i < roles.size(); i++) {
1102                            sb.append(table);
1103                            sb.append(".roleId = ?");
1104    
1105                            if ((i + 1) < roles.size()) {
1106                                    sb.append(" OR ");
1107                            }
1108                    }
1109    
1110                    return sb.toString();
1111            }
1112    
1113            /**
1114             * @see {@link ResourcePermissionFinderImpl#getScopes(int[])}
1115             */
1116            protected String getScopes(int[] scopes) {
1117                    if (scopes.length == 0) {
1118                            return StringPool.BLANK;
1119                    }
1120    
1121                    StringBundler sb = new StringBundler(scopes.length * 2 + 1);
1122    
1123                    sb.append("(");
1124    
1125                    for (int i = 0; i < scopes.length; i++) {
1126                            sb.append("ResourceCode.scope = ? ");
1127    
1128                            if ((i + 1) != scopes.length) {
1129                                    sb.append("OR ");
1130                            }
1131                    }
1132    
1133                    sb.append(")");
1134    
1135                    return sb.toString();
1136            }
1137    
1138            protected void setGroupIds(QueryPos qPos, List<Group> groups) {
1139                    for (Group group : groups) {
1140                            qPos.add(group.getGroupId());
1141                    }
1142            }
1143    
1144            protected void setPermissionIds(
1145                    QueryPos qPos, List<Permission> permissions) {
1146    
1147                    for (Permission permission : permissions) {
1148                            qPos.add(permission.getPermissionId());
1149                    }
1150            }
1151    
1152            protected void setResourceIds(QueryPos qPos, long[] resourceIds) {
1153                    for (long resourceId : resourceIds) {
1154                            qPos.add(resourceId);
1155                    }
1156            }
1157    
1158            protected void setRoleIds(QueryPos qPos, List<Role> roles) {
1159                    for (Role role : roles) {
1160                            qPos.add(role.getRoleId());
1161                    }
1162            }
1163    
1164    }