001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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.model.Group;
030    import com.liferay.portal.model.Permission;
031    import com.liferay.portal.model.Role;
032    import com.liferay.portal.model.impl.PermissionImpl;
033    import com.liferay.portal.model.impl.PermissionModelImpl;
034    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
035    import com.liferay.util.dao.orm.CustomSQLUtil;
036    
037    import java.util.ArrayList;
038    import java.util.Iterator;
039    import java.util.List;
040    
041    /**
042     * @author Brian Wing Shun Chan
043     */
044    public class PermissionFinderImpl
045            extends BasePersistenceImpl<Permission> implements PermissionFinder {
046    
047            public static String COUNT_BY_GROUPS_PERMISSIONS =
048                    PermissionFinder.class.getName() + ".countByGroupsPermissions";
049    
050            public static String COUNT_BY_GROUPS_ROLES =
051                    PermissionFinder.class.getName() + ".countByGroupsRoles";
052    
053            public static String COUNT_BY_ROLES_PERMISSIONS =
054                    PermissionFinder.class.getName() + ".countByRolesPermissions";
055    
056            public static String COUNT_BY_USER_GROUP_ROLE =
057                    PermissionFinder.class.getName() + ".countByUserGroupRole";
058    
059            public static String COUNT_BY_USERS_PERMISSIONS =
060                    PermissionFinder.class.getName() + ".countByUsersPermissions";
061    
062            public static String COUNT_BY_USERS_ROLES =
063                    PermissionFinder.class.getName() + ".countByUsersRoles";
064    
065            public static String COUNT_BY_R_A_C =
066                    PermissionFinder.class.getName() + ".countByR_A_C";
067    
068            public static String FIND_BY_A_C =
069                    PermissionFinder.class.getName() + ".findByA_C";
070    
071            public static String FIND_BY_A_R =
072                    PermissionFinder.class.getName() + ".findByA_R";
073    
074            public static String FIND_BY_G_R =
075                    PermissionFinder.class.getName() + ".findByG_R";
076    
077            public static String FIND_BY_R_R =
078                    PermissionFinder.class.getName() + ".findByR_R";
079    
080            public static String FIND_BY_R_S =
081                    PermissionFinder.class.getName() + ".findByR_S";
082    
083            public static String FIND_BY_U_R =
084                    PermissionFinder.class.getName() + ".findByU_R";
085    
086            public static String FIND_BY_O_G_R =
087                    PermissionFinder.class.getName() + ".findByO_G_R";
088    
089            public static String FIND_BY_U_A_R =
090                    PermissionFinder.class.getName() + ".findByU_A_R";
091    
092            public static String FIND_BY_G_C_N_S_P =
093                    PermissionFinder.class.getName() + ".findByG_C_N_S_P";
094    
095            public static String FIND_BY_U_C_N_S_P =
096                    PermissionFinder.class.getName() + ".findByU_C_N_S_P";
097    
098            public static final FinderPath FINDER_PATH_COUNT_BY_ROLES_PERMISSIONS =
099                    new FinderPath(
100                            PermissionModelImpl.ENTITY_CACHE_ENABLED,
101                            PermissionModelImpl.FINDER_CACHE_ENABLED, Long.class,
102                            "Roles_Permissions", "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, PermissionImpl.class,
110                    PermissionPersistenceImpl.FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION,
111                    "customFindByA_R",
112                    new String[] {
113                            String.class.getName(), "[L" + Long.class.getName()
114                    });
115    
116            public boolean containsPermissions_2(
117                            List<Permission> permissions, long userId, List<Group> groups,
118                            long groupId)
119                    throws SystemException {
120    
121                    Session session = null;
122    
123                    try {
124                            session = openSession();
125    
126                            String sql = null;
127    
128                            StringBundler sb = new StringBundler();
129    
130                            if (groups.size() > 0) {
131                                    sb.append("(");
132                                    sb.append(CustomSQLUtil.get(COUNT_BY_GROUPS_ROLES));
133                                    sb.append(") ");
134    
135                                    sql = sb.toString();
136    
137                                    sql = StringUtil.replace(
138                                            sql, "[$PERMISSION_ID$]",
139                                            getPermissionIds(permissions, "Roles_Permissions"));
140                                    sql = StringUtil.replace(
141                                            sql, "[$GROUP_ID$]", getGroupIds(groups, "Groups_Roles"));
142    
143                                    sb.setIndex(0);
144    
145                                    sb.append(sql);
146    
147                                    sb.append("UNION ALL (");
148                                    sb.append(CustomSQLUtil.get(COUNT_BY_GROUPS_PERMISSIONS));
149                                    sb.append(") ");
150    
151                                    sql = sb.toString();
152    
153                                    sql = StringUtil.replace(
154                                            sql, "[$PERMISSION_ID$]",
155                                            getPermissionIds(permissions, "Groups_Permissions"));
156                                    sql = StringUtil.replace(
157                                            sql, "[$GROUP_ID$]",
158                                            getGroupIds(groups, "Groups_Permissions"));
159    
160                                    sb.setIndex(0);
161    
162                                    sb.append(sql);
163    
164                                    sb.append("UNION ALL ");
165                            }
166    
167                            sb.append("(");
168                            sb.append(CustomSQLUtil.get(COUNT_BY_USERS_ROLES));
169                            sb.append(") ");
170    
171                            sql = sb.toString();
172    
173                            sql = StringUtil.replace(
174                                    sql, "[$PERMISSION_ID$]",
175                                    getPermissionIds(permissions, "Roles_Permissions"));
176    
177                            sb.setIndex(0);
178    
179                            sb.append(sql);
180    
181                            sb.append("UNION ALL (");
182                            sb.append(CustomSQLUtil.get(COUNT_BY_USER_GROUP_ROLE));
183                            sb.append(") ");
184    
185                            sql = sb.toString();
186    
187                            sql = StringUtil.replace(
188                                    sql, "[$PERMISSION_ID$]",
189                                    getPermissionIds(permissions, "Roles_Permissions"));
190    
191                            sb.setIndex(0);
192    
193                            sb.append(sql);
194    
195                            sb.append("UNION ALL (");
196                            sb.append(CustomSQLUtil.get(COUNT_BY_USERS_PERMISSIONS));
197                            sb.append(") ");
198    
199                            sql = sb.toString();
200    
201                            sql = StringUtil.replace(
202                                    sql, "[$PERMISSION_ID$]",
203                                    getPermissionIds(permissions, "Users_Permissions"));
204    
205                            SQLQuery q = session.createSQLQuery(sql);
206    
207                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
208    
209                            QueryPos qPos = QueryPos.getInstance(q);
210    
211                            if (groups.size() > 0) {
212                                    setPermissionIds(qPos, permissions);
213                                    setGroupIds(qPos, groups);
214                                    setPermissionIds(qPos, permissions);
215                                    setGroupIds(qPos, groups);
216                            }
217    
218                            setPermissionIds(qPos, permissions);
219                            qPos.add(userId);
220    
221                            qPos.add(groupId);
222                            setPermissionIds(qPos, permissions);
223                            qPos.add(userId);
224    
225                            setPermissionIds(qPos, permissions);
226                            qPos.add(userId);
227    
228                            Iterator<Long> itr = q.iterate();
229    
230                            while (itr.hasNext()) {
231                                    Long count = itr.next();
232    
233                                    if ((count != null) && (count.intValue() > 0)) {
234                                            return true;
235                                    }
236                            }
237    
238                            return false;
239                    }
240                    catch (Exception e) {
241                            throw new SystemException(e);
242                    }
243                    finally {
244                            closeSession(session);
245                    }
246            }
247    
248            public boolean containsPermissions_4(
249                            List<Permission> permissions, long userId, List<Group> groups,
250                            List<Role> roles)
251                    throws SystemException {
252    
253                    Session session = null;
254    
255                    try {
256                            session = openSession();
257    
258                            String sql = null;
259    
260                            StringBundler sb = new StringBundler();
261    
262                            if (groups.size() > 0) {
263                                    sb.append("(");
264                                    sb.append(CustomSQLUtil.get(COUNT_BY_GROUPS_PERMISSIONS));
265                                    sb.append(") ");
266    
267                                    sql = sb.toString();
268    
269                                    sql = StringUtil.replace(
270                                            sql, "[$PERMISSION_ID$]",
271                                            getPermissionIds(permissions, "Groups_Permissions"));
272                                    sql = StringUtil.replace(
273                                            sql, "[$GROUP_ID$]",
274                                            getGroupIds(groups, "Groups_Permissions"));
275    
276                                    sb.setIndex(0);
277    
278                                    sb.append(sql);
279    
280                                    sb.append("UNION ALL ");
281                            }
282    
283                            if (roles.size() > 0) {
284                                    sb.append("(");
285                                    sb.append(CustomSQLUtil.get(COUNT_BY_ROLES_PERMISSIONS));
286                                    sb.append(") ");
287    
288                                    sql = sb.toString();
289    
290                                    sql = StringUtil.replace(
291                                            sql, "[$PERMISSION_ID$]",
292                                            getPermissionIds(permissions, "Roles_Permissions"));
293                                    sql = StringUtil.replace(
294                                            sql, "[$ROLE_ID$]", getRoleIds(roles, "Roles_Permissions"));
295    
296                                    sb.setIndex(0);
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_ID$]",
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.iterate();
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_ID$]",
365                                    getPermissionIds(permissions, "Groups_Permissions"));
366                            sql = StringUtil.replace(
367                                    sql, "[$GROUP_ID$]", getGroupIds(groups, "Groups_Permissions"));
368    
369                            SQLQuery q = session.createSQLQuery(sql);
370    
371                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
372    
373                            QueryPos qPos = QueryPos.getInstance(q);
374    
375                            setPermissionIds(qPos, permissions);
376                            setGroupIds(qPos, groups);
377    
378                            Iterator<Long> itr = q.iterate();
379    
380                            if (itr.hasNext()) {
381                                    Long count = itr.next();
382    
383                                    if (count != null) {
384                                            return count.intValue();
385                                    }
386                            }
387    
388                            return 0;
389                    }
390                    catch (Exception e) {
391                            throw new SystemException(e);
392                    }
393                    finally {
394                            closeSession(session);
395                    }
396            }
397    
398            public int countByGroupsRoles(
399                            List<Permission> permissions, List<Group> groups)
400                    throws SystemException {
401    
402                    Session session = null;
403    
404                    try {
405                            session = openSession();
406    
407                            String sql = CustomSQLUtil.get(COUNT_BY_GROUPS_ROLES);
408    
409                            sql = StringUtil.replace(
410                                    sql, "[$PERMISSION_ID$]",
411                                    getPermissionIds(permissions, "Roles_Permissions"));
412                            sql = StringUtil.replace(
413                                    sql, "[$GROUP_ID$]", getGroupIds(groups, "Groups_Roles"));
414    
415                            SQLQuery q = session.createSQLQuery(sql);
416    
417                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
418    
419                            QueryPos qPos = QueryPos.getInstance(q);
420    
421                            setPermissionIds(qPos, permissions);
422                            setGroupIds(qPos, groups);
423    
424                            Iterator<Long> itr = q.iterate();
425    
426                            if (itr.hasNext()) {
427                                    Long count = itr.next();
428    
429                                    if (count != null) {
430                                            return count.intValue();
431                                    }
432                            }
433    
434                            return 0;
435                    }
436                    catch (Exception e) {
437                            throw new SystemException(e);
438                    }
439                    finally {
440                            closeSession(session);
441                    }
442            }
443    
444            public int countByRolesPermissions(
445                            List<Permission> permissions, List<Role> roles)
446                    throws SystemException {
447    
448                    Object[] finderArgs = new Object[] {
449                            ListUtil.toString(permissions, Permission.PERMISSION_ID_ACCESSOR),
450                            ListUtil.toString(roles, Role.ROLE_ID_ACCESSOR)
451                    };
452    
453                    Long count = (Long)FinderCacheUtil.getResult(
454                            FINDER_PATH_COUNT_BY_ROLES_PERMISSIONS, finderArgs, this);
455    
456                    if (count != null) {
457                            return count.intValue();
458                    }
459    
460                    Session session = null;
461    
462                    try {
463                            session = openSession();
464    
465                            String sql = CustomSQLUtil.get(COUNT_BY_ROLES_PERMISSIONS);
466    
467                            sql = StringUtil.replace(
468                                    sql, "[$PERMISSION_ID$]",
469                                    getPermissionIds(permissions, "Roles_Permissions"));
470                            sql = StringUtil.replace(
471                                    sql, "[$ROLE_ID$]", getRoleIds(roles, "Roles_Permissions"));
472    
473                            SQLQuery q = session.createSQLQuery(sql);
474    
475                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
476    
477                            QueryPos qPos = QueryPos.getInstance(q);
478    
479                            setPermissionIds(qPos, permissions);
480                            setRoleIds(qPos, roles);
481    
482                            count = (Long)q.uniqueResult();
483                    }
484                    catch (Exception e) {
485                            throw new SystemException(e);
486                    }
487                    finally {
488                            if (count == null) {
489                                    count = Long.valueOf(0);
490                            }
491    
492                            FinderCacheUtil.putResult(
493                                    FINDER_PATH_COUNT_BY_ROLES_PERMISSIONS, finderArgs, count);
494    
495                            closeSession(session);
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_ID$]",
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.iterate();
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_ID$]",
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.iterate();
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_ID$]",
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.iterate();
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 int countByR_A_C(long roleId, String actionId, long codeId)
634                    throws SystemException {
635    
636                    Session session = null;
637    
638                    try {
639                            session = openSession();
640    
641                            String sql = CustomSQLUtil.get(COUNT_BY_R_A_C);
642    
643                            SQLQuery q = session.createSQLQuery(sql);
644    
645                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
646    
647                            QueryPos qPos = QueryPos.getInstance(q);
648    
649                            qPos.add(roleId);
650                            qPos.add(actionId);
651                            qPos.add(codeId);
652    
653                            Iterator<Long> itr = q.iterate();
654    
655                            if (itr.hasNext()) {
656                                    Long count = itr.next();
657    
658                                    if (count != null) {
659                                            return count.intValue();
660                                    }
661                            }
662    
663                            return 0;
664                    }
665                    catch (Exception e) {
666                            throw new SystemException(e);
667                    }
668                    finally {
669                            closeSession(session);
670                    }
671            }
672    
673            public List<Permission> findByA_C(String actionId, long codeId)
674                    throws SystemException {
675    
676                    Session session = null;
677    
678                    try {
679                            session = openSession();
680    
681                            String sql = CustomSQLUtil.get(FIND_BY_A_C);
682    
683                            SQLQuery q = session.createSQLQuery(sql);
684    
685                            q.addEntity("Permission_", PermissionImpl.class);
686    
687                            QueryPos qPos = QueryPos.getInstance(q);
688    
689                            qPos.add(actionId);
690                            qPos.add(codeId);
691    
692                            return q.list(true);
693                    }
694                    catch (Exception e) {
695                            throw new SystemException(e);
696                    }
697                    finally {
698                            closeSession(session);
699                    }
700            }
701    
702            public List<Permission> findByA_R(String actionId, long[] resourceIds)
703                    throws SystemException {
704    
705                    Object[] finderArgs = new Object[] {
706                            actionId, StringUtil.merge(ArrayUtil.toArray(resourceIds))
707                    };
708    
709                    List<Permission> list = (List<Permission>)FinderCacheUtil.getResult(
710                            FINDER_PATH_FIND_BY_A_R, finderArgs, this);
711    
712                    if (list != null) {
713                            return list;
714                    }
715    
716                    Session session = null;
717    
718                    try {
719                            session = openSession();
720    
721                            String sql = CustomSQLUtil.get(FIND_BY_A_R);
722    
723                            sql = StringUtil.replace(
724                                    sql, "[$RESOURCE_ID$]", getResourceIds(resourceIds));
725    
726                            SQLQuery q = session.createSQLQuery(sql);
727    
728                            q.addEntity("Permission_", PermissionImpl.class);
729    
730                            QueryPos qPos = QueryPos.getInstance(q);
731    
732                            qPos.add(actionId);
733    
734                            setResourceIds(qPos, resourceIds);
735    
736                            list = q.list(true);
737                    }
738                    catch (Exception e) {
739                            throw new SystemException(e);
740                    }
741                    finally {
742                            if (list == null) {
743                                    list = new ArrayList<Permission>();
744                            }
745    
746                            FinderCacheUtil.putResult(
747                                    FINDER_PATH_FIND_BY_A_R, finderArgs, list);
748    
749                            closeSession(session);
750                    }
751    
752                    return list;
753            }
754    
755            public List<Permission> findByG_R(long groupId, long resourceId)
756                    throws SystemException {
757    
758                    Session session = null;
759    
760                    try {
761                            session = openSession();
762    
763                            String sql = CustomSQLUtil.get(FIND_BY_G_R);
764    
765                            SQLQuery q = session.createSQLQuery(sql);
766    
767                            q.addEntity("Permission_", PermissionImpl.class);
768    
769                            QueryPos qPos = QueryPos.getInstance(q);
770    
771                            qPos.add(groupId);
772                            qPos.add(resourceId);
773    
774                            return q.list(true);
775                    }
776                    catch (Exception e) {
777                            throw new SystemException(e);
778                    }
779                    finally {
780                            closeSession(session);
781                    }
782            }
783    
784            public List<Permission> findByR_R(long roleId, long resourceId)
785                    throws SystemException {
786    
787                    Session session = null;
788    
789                    try {
790                            session = openSession();
791    
792                            String sql = CustomSQLUtil.get(FIND_BY_R_R);
793    
794                            SQLQuery q = session.createSQLQuery(sql);
795    
796                            q.addEntity("Permission_", PermissionImpl.class);
797    
798                            QueryPos qPos = QueryPos.getInstance(q);
799    
800                            qPos.add(roleId);
801                            qPos.add(resourceId);
802    
803                            return q.list(true);
804                    }
805                    catch (Exception e) {
806                            throw new SystemException(e);
807                    }
808                    finally {
809                            closeSession(session);
810                    }
811            }
812    
813            public List<Permission> findByR_S(long roleId, int[] scopes)
814                    throws SystemException {
815    
816                    Session session = null;
817    
818                    try {
819                            session = openSession();
820    
821                            String sql = CustomSQLUtil.get(FIND_BY_R_S);
822    
823                            sql = StringUtil.replace(sql, "[$SCOPE$]", getScopes(scopes));
824    
825                            SQLQuery q = session.createSQLQuery(sql);
826    
827                            q.addEntity("Permission_", PermissionImpl.class);
828    
829                            QueryPos qPos = QueryPos.getInstance(q);
830    
831                            qPos.add(roleId);
832                            qPos.add(scopes);
833    
834                            return q.list(true);
835                    }
836                    catch (Exception e) {
837                            throw new SystemException(e);
838                    }
839                    finally {
840                            closeSession(session);
841                    }
842            }
843    
844            public List<Permission> findByU_R(long userId, long resourceId)
845                    throws SystemException {
846    
847                    Session session = null;
848    
849                    try {
850                            session = openSession();
851    
852                            String sql = CustomSQLUtil.get(FIND_BY_U_R);
853    
854                            SQLQuery q = session.createSQLQuery(sql);
855    
856                            q.addEntity("Permission_", PermissionImpl.class);
857    
858                            QueryPos qPos = QueryPos.getInstance(q);
859    
860                            qPos.add(userId);
861                            qPos.add(resourceId);
862    
863                            return q.list(true);
864                    }
865                    catch (Exception e) {
866                            throw new SystemException(e);
867                    }
868                    finally {
869                            closeSession(session);
870                    }
871            }
872    
873            public List<Permission> findByO_G_R(
874                            long organizationId, long groupId, long resourceId)
875                    throws SystemException {
876    
877                    Session session = null;
878    
879                    try {
880                            session = openSession();
881    
882                            String sql = CustomSQLUtil.get(FIND_BY_O_G_R);
883    
884                            SQLQuery q = session.createSQLQuery(sql);
885    
886                            q.addEntity("Permission_", PermissionImpl.class);
887    
888                            QueryPos qPos = QueryPos.getInstance(q);
889    
890                            qPos.add(organizationId);
891                            qPos.add(groupId);
892                            qPos.add(resourceId);
893    
894                            return q.list(true);
895                    }
896                    catch (Exception e) {
897                            throw new SystemException(e);
898                    }
899                    finally {
900                            closeSession(session);
901                    }
902            }
903    
904            public List<Permission> findByU_A_R(
905                            long userId, String[] actionIds, long resourceId)
906                    throws SystemException {
907    
908                    Session session = null;
909    
910                    try {
911                            session = openSession();
912    
913                            String sql = CustomSQLUtil.get(FIND_BY_U_R);
914    
915                            sql = StringUtil.replace(
916                                    sql, "[$ACTION_ID$]", getActionIds(actionIds));
917    
918                            SQLQuery q = session.createSQLQuery(sql);
919    
920                            q.addEntity("Permission_", PermissionImpl.class);
921    
922                            QueryPos qPos = QueryPos.getInstance(q);
923    
924                            qPos.add(userId);
925                            qPos.add(resourceId);
926    
927                            return q.list(true);
928                    }
929                    catch (Exception e) {
930                            throw new SystemException(e);
931                    }
932                    finally {
933                            closeSession(session);
934                    }
935            }
936    
937            public List<Permission> findByG_C_N_S_P(
938                            long groupId, long companyId, String name, int scope,
939                            String primKey)
940                    throws SystemException {
941    
942                    Session session = null;
943    
944                    try {
945                            session = openSession();
946    
947                            String sql = CustomSQLUtil.get(FIND_BY_G_C_N_S_P);
948    
949                            SQLQuery q = session.createSQLQuery(sql);
950    
951                            q.addEntity("Permission_", PermissionImpl.class);
952    
953                            QueryPos qPos = QueryPos.getInstance(q);
954    
955                            qPos.add(groupId);
956                            qPos.add(companyId);
957                            qPos.add(name);
958                            qPos.add(scope);
959                            qPos.add(primKey);
960    
961                            return q.list(true);
962                    }
963                    catch (Exception e) {
964                            throw new SystemException(e);
965                    }
966                    finally {
967                            closeSession(session);
968                    }
969            }
970    
971            public List<Permission> findByU_C_N_S_P(
972                            long userId, long companyId, String name, int scope, String primKey)
973                    throws SystemException {
974    
975                    Session session = null;
976    
977                    try {
978                            session = openSession();
979    
980                            String sql = CustomSQLUtil.get(FIND_BY_U_C_N_S_P);
981    
982                            SQLQuery q = session.createSQLQuery(sql);
983    
984                            q.addEntity("Permission_", PermissionImpl.class);
985    
986                            QueryPos qPos = QueryPos.getInstance(q);
987    
988                            qPos.add(userId);
989                            qPos.add(companyId);
990                            qPos.add(name);
991                            qPos.add(scope);
992                            qPos.add(primKey);
993    
994                            return q.list(true);
995                    }
996                    catch (Exception e) {
997                            throw new SystemException(e);
998                    }
999                    finally {
1000                            closeSession(session);
1001                    }
1002            }
1003    
1004            protected String getActionIds(String[] actionIds) {
1005                    if (actionIds.length == 0) {
1006                            return StringPool.BLANK;
1007                    }
1008    
1009                    StringBundler sb = new StringBundler(actionIds.length * 2 - 1);
1010    
1011                    for (int i = 0; i < actionIds.length; i++) {
1012                            sb.append("Permission_.actionId = ?");
1013    
1014                            if ((i + 1) < actionIds.length) {
1015                                    sb.append(" OR ");
1016                            }
1017                    }
1018    
1019                    return sb.toString();
1020            }
1021    
1022            protected String getGroupIds(List<Group> groups, String table) {
1023                    if (groups.isEmpty()) {
1024                            return StringPool.BLANK;
1025                    }
1026    
1027                    StringBundler sb = new StringBundler(groups.size() * 3 - 1);
1028    
1029                    for (int i = 0; i < groups.size(); i++) {
1030                            sb.append(table);
1031                            sb.append(".groupId = ?");
1032    
1033                            if ((i + 1) < groups.size()) {
1034                                    sb.append(" OR ");
1035                            }
1036                    }
1037    
1038                    return sb.toString();
1039            }
1040    
1041            protected String getPermissionIds(
1042                    List<Permission> permissions, String table) {
1043    
1044                    if (permissions.isEmpty()) {
1045                            return StringPool.BLANK;
1046                    }
1047    
1048                    StringBundler sb = new StringBundler(permissions.size() * 3 - 1);
1049    
1050                    for (int i = 0; i < permissions.size(); i++) {
1051                            sb.append(table);
1052                            sb.append(".permissionId = ?");
1053    
1054                            if ((i + 1) < permissions.size()) {
1055                                    sb.append(" OR ");
1056                            }
1057                    }
1058    
1059                    return sb.toString();
1060            }
1061    
1062            protected String getResourceIds(long[] resourceIds) {
1063                    if (resourceIds.length == 0) {
1064                            return StringPool.BLANK;
1065                    }
1066    
1067                    StringBundler sb = new StringBundler(resourceIds.length * 2 - 1);
1068    
1069                    for (int i = 0; i < resourceIds.length; i++) {
1070                            sb.append("resourceId = ?");
1071    
1072                            if ((i + 1) < resourceIds.length) {
1073                                    sb.append(" OR ");
1074                            }
1075                    }
1076    
1077                    return sb.toString();
1078            }
1079    
1080            protected String getRoleIds(List<Role> roles, String table) {
1081                    if (roles.isEmpty()) {
1082                            return StringPool.BLANK;
1083                    }
1084    
1085                    StringBundler sb = new StringBundler(roles.size() * 3 - 1);
1086    
1087                    for (int i = 0; i < roles.size(); i++) {
1088                            sb.append(table);
1089                            sb.append(".roleId = ?");
1090    
1091                            if ((i + 1) < roles.size()) {
1092                                    sb.append(" OR ");
1093                            }
1094                    }
1095    
1096                    return sb.toString();
1097            }
1098    
1099            protected String getScopes(int[] scopes) {
1100                    if (scopes.length == 0) {
1101                            return StringPool.BLANK;
1102                    }
1103    
1104                    StringBundler sb = new StringBundler(scopes.length * 2 + 1);
1105    
1106                    sb.append("(");
1107    
1108                    for (int i = 0; i < scopes.length; i++) {
1109                            sb.append("ResourceCode.scope = ? ");
1110    
1111                            if ((i + 1) != scopes.length) {
1112                                    sb.append("OR ");
1113                            }
1114                    }
1115    
1116                    sb.append(")");
1117    
1118                    return sb.toString();
1119            }
1120    
1121            protected void setGroupIds(QueryPos qPos, List<Group> groups) {
1122                    for (Group group : groups) {
1123                            qPos.add(group.getGroupId());
1124                    }
1125            }
1126    
1127            protected void setPermissionIds(
1128                    QueryPos qPos, List<Permission> permissions) {
1129    
1130                    for (Permission permission : permissions) {
1131                            qPos.add(permission.getPermissionId());
1132                    }
1133            }
1134    
1135            protected void setResourceIds(QueryPos qPos, long[] resourceIds) {
1136                    for (long resourceId : resourceIds) {
1137                            qPos.add(resourceId);
1138                    }
1139            }
1140    
1141            protected void setRoleIds(QueryPos qPos, List<Role> roles) {
1142                    for (Role role : roles) {
1143                            qPos.add(role.getRoleId());
1144                    }
1145            }
1146    
1147    }