001    /**
002     * Copyright (c) 2000-2013 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            @Override
118            public boolean containsPermissions_2(
119                            List<Permission> permissions, long userId, List<Group> groups,
120                            long groupId)
121                    throws SystemException {
122    
123                    Session session = null;
124    
125                    try {
126                            session = openSession();
127    
128                            String sql = null;
129    
130                            StringBundler sb = new StringBundler();
131    
132                            if (groups.size() > 0) {
133                                    sb.append("(");
134                                    sb.append(CustomSQLUtil.get(COUNT_BY_GROUPS_ROLES));
135                                    sb.append(") ");
136    
137                                    sql = sb.toString();
138    
139                                    sql = StringUtil.replace(
140                                            sql, "[$PERMISSION_ID$]",
141                                            getPermissionIds(permissions, "Roles_Permissions"));
142                                    sql = StringUtil.replace(
143                                            sql, "[$GROUP_ID$]", getGroupIds(groups, "Groups_Roles"));
144    
145                                    sb.setIndex(0);
146    
147                                    sb.append(sql);
148    
149                                    sb.append("UNION ALL (");
150                                    sb.append(CustomSQLUtil.get(COUNT_BY_GROUPS_PERMISSIONS));
151                                    sb.append(") ");
152    
153                                    sql = sb.toString();
154    
155                                    sql = StringUtil.replace(
156                                            sql, "[$PERMISSION_ID$]",
157                                            getPermissionIds(permissions, "Groups_Permissions"));
158                                    sql = StringUtil.replace(
159                                            sql, "[$GROUP_ID$]",
160                                            getGroupIds(groups, "Groups_Permissions"));
161    
162                                    sb.setIndex(0);
163    
164                                    sb.append(sql);
165    
166                                    sb.append("UNION ALL ");
167                            }
168    
169                            sb.append("(");
170                            sb.append(CustomSQLUtil.get(COUNT_BY_USERS_ROLES));
171                            sb.append(") ");
172    
173                            sql = sb.toString();
174    
175                            sql = StringUtil.replace(
176                                    sql, "[$PERMISSION_ID$]",
177                                    getPermissionIds(permissions, "Roles_Permissions"));
178    
179                            sb.setIndex(0);
180    
181                            sb.append(sql);
182    
183                            sb.append("UNION ALL (");
184                            sb.append(CustomSQLUtil.get(COUNT_BY_USER_GROUP_ROLE));
185                            sb.append(") ");
186    
187                            sql = sb.toString();
188    
189                            sql = StringUtil.replace(
190                                    sql, "[$PERMISSION_ID$]",
191                                    getPermissionIds(permissions, "Roles_Permissions"));
192    
193                            sb.setIndex(0);
194    
195                            sb.append(sql);
196    
197                            sb.append("UNION ALL (");
198                            sb.append(CustomSQLUtil.get(COUNT_BY_USERS_PERMISSIONS));
199                            sb.append(") ");
200    
201                            sql = sb.toString();
202    
203                            sql = StringUtil.replace(
204                                    sql, "[$PERMISSION_ID$]",
205                                    getPermissionIds(permissions, "Users_Permissions"));
206    
207                            SQLQuery q = session.createSQLQuery(sql);
208    
209                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
210    
211                            QueryPos qPos = QueryPos.getInstance(q);
212    
213                            if (groups.size() > 0) {
214                                    setPermissionIds(qPos, permissions);
215                                    setGroupIds(qPos, groups);
216                                    setPermissionIds(qPos, permissions);
217                                    setGroupIds(qPos, groups);
218                            }
219    
220                            setPermissionIds(qPos, permissions);
221                            qPos.add(userId);
222    
223                            qPos.add(groupId);
224                            setPermissionIds(qPos, permissions);
225                            qPos.add(userId);
226    
227                            setPermissionIds(qPos, permissions);
228                            qPos.add(userId);
229    
230                            Iterator<Long> itr = q.iterate();
231    
232                            while (itr.hasNext()) {
233                                    Long count = itr.next();
234    
235                                    if ((count != null) && (count.intValue() > 0)) {
236                                            return true;
237                                    }
238                            }
239    
240                            return false;
241                    }
242                    catch (Exception e) {
243                            throw new SystemException(e);
244                    }
245                    finally {
246                            closeSession(session);
247                    }
248            }
249    
250            @Override
251            public boolean containsPermissions_4(
252                            List<Permission> permissions, long userId, List<Group> groups,
253                            List<Role> roles)
254                    throws SystemException {
255    
256                    Session session = null;
257    
258                    try {
259                            session = openSession();
260    
261                            String sql = null;
262    
263                            StringBundler sb = new StringBundler();
264    
265                            if (groups.size() > 0) {
266                                    sb.append("(");
267                                    sb.append(CustomSQLUtil.get(COUNT_BY_GROUPS_PERMISSIONS));
268                                    sb.append(") ");
269    
270                                    sql = sb.toString();
271    
272                                    sql = StringUtil.replace(
273                                            sql, "[$PERMISSION_ID$]",
274                                            getPermissionIds(permissions, "Groups_Permissions"));
275                                    sql = StringUtil.replace(
276                                            sql, "[$GROUP_ID$]",
277                                            getGroupIds(groups, "Groups_Permissions"));
278    
279                                    sb.setIndex(0);
280    
281                                    sb.append(sql);
282    
283                                    sb.append("UNION ALL ");
284                            }
285    
286                            if (roles.size() > 0) {
287                                    sb.append("(");
288                                    sb.append(CustomSQLUtil.get(COUNT_BY_ROLES_PERMISSIONS));
289                                    sb.append(") ");
290    
291                                    sql = sb.toString();
292    
293                                    sql = StringUtil.replace(
294                                            sql, "[$PERMISSION_ID$]",
295                                            getPermissionIds(permissions, "Roles_Permissions"));
296                                    sql = StringUtil.replace(
297                                            sql, "[$ROLE_ID$]", getRoleIds(roles, "Roles_Permissions"));
298    
299                                    sb.setIndex(0);
300    
301                                    sb.append(sql);
302    
303                                    sb.append("UNION ALL ");
304                            }
305    
306                            sb.append("(");
307                            sb.append(CustomSQLUtil.get(COUNT_BY_USERS_PERMISSIONS));
308                            sb.append(") ");
309    
310                            sql = sb.toString();
311    
312                            sql = StringUtil.replace(
313                                    sql, "[$PERMISSION_ID$]",
314                                    getPermissionIds(permissions, "Users_Permissions"));
315    
316                            SQLQuery q = session.createSQLQuery(sql);
317    
318                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
319    
320                            QueryPos qPos = QueryPos.getInstance(q);
321    
322                            if (groups.size() > 0) {
323                                    setPermissionIds(qPos, permissions);
324                                    setGroupIds(qPos, groups);
325                            }
326    
327                            if (roles.size() > 0) {
328                                    setPermissionIds(qPos, permissions);
329                                    setRoleIds(qPos, roles);
330                            }
331    
332                            setPermissionIds(qPos, permissions);
333                            qPos.add(userId);
334    
335                            Iterator<Long> itr = q.iterate();
336    
337                            while (itr.hasNext()) {
338                                    Long count = itr.next();
339    
340                                    if ((count != null) && (count.intValue() > 0)) {
341                                            return true;
342                                    }
343                            }
344    
345                            return false;
346                    }
347                    catch (Exception e) {
348                            throw new SystemException(e);
349                    }
350                    finally {
351                            closeSession(session);
352                    }
353            }
354    
355            @Override
356            public int countByGroupsPermissions(
357                            List<Permission> permissions, List<Group> groups)
358                    throws SystemException {
359    
360                    Session session = null;
361    
362                    try {
363                            session = openSession();
364    
365                            String sql = CustomSQLUtil.get(COUNT_BY_GROUPS_PERMISSIONS);
366    
367                            sql = StringUtil.replace(
368                                    sql, "[$PERMISSION_ID$]",
369                                    getPermissionIds(permissions, "Groups_Permissions"));
370                            sql = StringUtil.replace(
371                                    sql, "[$GROUP_ID$]", getGroupIds(groups, "Groups_Permissions"));
372    
373                            SQLQuery q = session.createSQLQuery(sql);
374    
375                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
376    
377                            QueryPos qPos = QueryPos.getInstance(q);
378    
379                            setPermissionIds(qPos, permissions);
380                            setGroupIds(qPos, groups);
381    
382                            Iterator<Long> itr = q.iterate();
383    
384                            if (itr.hasNext()) {
385                                    Long count = itr.next();
386    
387                                    if (count != null) {
388                                            return count.intValue();
389                                    }
390                            }
391    
392                            return 0;
393                    }
394                    catch (Exception e) {
395                            throw new SystemException(e);
396                    }
397                    finally {
398                            closeSession(session);
399                    }
400            }
401    
402            @Override
403            public int countByGroupsRoles(
404                            List<Permission> permissions, List<Group> groups)
405                    throws SystemException {
406    
407                    Session session = null;
408    
409                    try {
410                            session = openSession();
411    
412                            String sql = CustomSQLUtil.get(COUNT_BY_GROUPS_ROLES);
413    
414                            sql = StringUtil.replace(
415                                    sql, "[$PERMISSION_ID$]",
416                                    getPermissionIds(permissions, "Roles_Permissions"));
417                            sql = StringUtil.replace(
418                                    sql, "[$GROUP_ID$]", getGroupIds(groups, "Groups_Roles"));
419    
420                            SQLQuery q = session.createSQLQuery(sql);
421    
422                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
423    
424                            QueryPos qPos = QueryPos.getInstance(q);
425    
426                            setPermissionIds(qPos, permissions);
427                            setGroupIds(qPos, groups);
428    
429                            Iterator<Long> itr = q.iterate();
430    
431                            if (itr.hasNext()) {
432                                    Long count = itr.next();
433    
434                                    if (count != null) {
435                                            return count.intValue();
436                                    }
437                            }
438    
439                            return 0;
440                    }
441                    catch (Exception e) {
442                            throw new SystemException(e);
443                    }
444                    finally {
445                            closeSession(session);
446                    }
447            }
448    
449            @Override
450            public int countByRolesPermissions(
451                            List<Permission> permissions, List<Role> roles)
452                    throws SystemException {
453    
454                    Object[] finderArgs = new Object[] {
455                            ListUtil.toString(permissions, Permission.PERMISSION_ID_ACCESSOR),
456                            ListUtil.toString(roles, Role.ROLE_ID_ACCESSOR)
457                    };
458    
459                    Long count = (Long)FinderCacheUtil.getResult(
460                            FINDER_PATH_COUNT_BY_ROLES_PERMISSIONS, finderArgs, this);
461    
462                    if (count != null) {
463                            return count.intValue();
464                    }
465    
466                    Session session = null;
467    
468                    try {
469                            session = openSession();
470    
471                            String sql = CustomSQLUtil.get(COUNT_BY_ROLES_PERMISSIONS);
472    
473                            sql = StringUtil.replace(
474                                    sql, "[$PERMISSION_ID$]",
475                                    getPermissionIds(permissions, "Roles_Permissions"));
476                            sql = StringUtil.replace(
477                                    sql, "[$ROLE_ID$]", getRoleIds(roles, "Roles_Permissions"));
478    
479                            SQLQuery q = session.createSQLQuery(sql);
480    
481                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
482    
483                            QueryPos qPos = QueryPos.getInstance(q);
484    
485                            setPermissionIds(qPos, permissions);
486                            setRoleIds(qPos, roles);
487    
488                            count = (Long)q.uniqueResult();
489                    }
490                    catch (Exception e) {
491                            throw new SystemException(e);
492                    }
493                    finally {
494                            if (count == null) {
495                                    count = Long.valueOf(0);
496                            }
497    
498                            FinderCacheUtil.putResult(
499                                    FINDER_PATH_COUNT_BY_ROLES_PERMISSIONS, finderArgs, count);
500    
501                            closeSession(session);
502                    }
503    
504                    return count.intValue();
505            }
506    
507            @Override
508            public int countByUserGroupRole(
509                            List<Permission> permissions, long userId, long groupId)
510                    throws SystemException {
511    
512                    Session session = null;
513    
514                    try {
515                            session = openSession();
516    
517                            String sql = CustomSQLUtil.get(COUNT_BY_USER_GROUP_ROLE);
518    
519                            sql = StringUtil.replace(
520                                    sql, "[$PERMISSION_ID$]",
521                                    getPermissionIds(permissions, "Roles_Permissions"));
522    
523                            SQLQuery q = session.createSQLQuery(sql);
524    
525                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
526    
527                            QueryPos qPos = QueryPos.getInstance(q);
528    
529                            qPos.add(groupId);
530                            setPermissionIds(qPos, permissions);
531                            qPos.add(userId);
532    
533                            Iterator<Long> itr = q.iterate();
534    
535                            if (itr.hasNext()) {
536                                    Long count = itr.next();
537    
538                                    if (count != null) {
539                                            return count.intValue();
540                                    }
541                            }
542    
543                            return 0;
544                    }
545                    catch (Exception e) {
546                            throw new SystemException(e);
547                    }
548                    finally {
549                            closeSession(session);
550                    }
551            }
552    
553            @Override
554            public int countByUsersPermissions(
555                            List<Permission> permissions, long userId)
556                    throws SystemException {
557    
558                    Session session = null;
559    
560                    try {
561                            session = openSession();
562    
563                            String sql = CustomSQLUtil.get(COUNT_BY_USERS_PERMISSIONS);
564    
565                            sql = StringUtil.replace(
566                                    sql, "[$PERMISSION_ID$]",
567                                    getPermissionIds(permissions, "Users_Permissions"));
568    
569                            SQLQuery q = session.createSQLQuery(sql);
570    
571                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
572    
573                            QueryPos qPos = QueryPos.getInstance(q);
574    
575                            setPermissionIds(qPos, permissions);
576                            qPos.add(userId);
577    
578                            Iterator<Long> itr = q.iterate();
579    
580                            if (itr.hasNext()) {
581                                    Long count = itr.next();
582    
583                                    if (count != null) {
584                                            return count.intValue();
585                                    }
586                            }
587    
588                            return 0;
589                    }
590                    catch (Exception e) {
591                            throw new SystemException(e);
592                    }
593                    finally {
594                            closeSession(session);
595                    }
596            }
597    
598            @Override
599            public int countByUsersRoles(List<Permission> permissions, long userId)
600                    throws SystemException {
601    
602                    Session session = null;
603    
604                    try {
605                            session = openSession();
606    
607                            String sql = CustomSQLUtil.get(COUNT_BY_USERS_ROLES);
608    
609                            sql = StringUtil.replace(
610                                    sql, "[$PERMISSION_ID$]",
611                                    getPermissionIds(permissions, "Roles_Permissions"));
612    
613                            SQLQuery q = session.createSQLQuery(sql);
614    
615                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
616    
617                            QueryPos qPos = QueryPos.getInstance(q);
618    
619                            setPermissionIds(qPos, permissions);
620                            qPos.add(userId);
621    
622                            Iterator<Long> itr = q.iterate();
623    
624                            if (itr.hasNext()) {
625                                    Long count = itr.next();
626    
627                                    if (count != null) {
628                                            return count.intValue();
629                                    }
630                            }
631    
632                            return 0;
633                    }
634                    catch (Exception e) {
635                            throw new SystemException(e);
636                    }
637                    finally {
638                            closeSession(session);
639                    }
640            }
641    
642            @Override
643            public int countByR_A_C(long roleId, String actionId, long codeId)
644                    throws SystemException {
645    
646                    Session session = null;
647    
648                    try {
649                            session = openSession();
650    
651                            String sql = CustomSQLUtil.get(COUNT_BY_R_A_C);
652    
653                            SQLQuery q = session.createSQLQuery(sql);
654    
655                            q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
656    
657                            QueryPos qPos = QueryPos.getInstance(q);
658    
659                            qPos.add(roleId);
660                            qPos.add(actionId);
661                            qPos.add(codeId);
662    
663                            Iterator<Long> itr = q.iterate();
664    
665                            if (itr.hasNext()) {
666                                    Long count = itr.next();
667    
668                                    if (count != null) {
669                                            return count.intValue();
670                                    }
671                            }
672    
673                            return 0;
674                    }
675                    catch (Exception e) {
676                            throw new SystemException(e);
677                    }
678                    finally {
679                            closeSession(session);
680                    }
681            }
682    
683            @Override
684            public List<Permission> findByA_C(String actionId, long codeId)
685                    throws SystemException {
686    
687                    Session session = null;
688    
689                    try {
690                            session = openSession();
691    
692                            String sql = CustomSQLUtil.get(FIND_BY_A_C);
693    
694                            SQLQuery q = session.createSQLQuery(sql);
695    
696                            q.addEntity("Permission_", PermissionImpl.class);
697    
698                            QueryPos qPos = QueryPos.getInstance(q);
699    
700                            qPos.add(actionId);
701                            qPos.add(codeId);
702    
703                            return q.list(true);
704                    }
705                    catch (Exception e) {
706                            throw new SystemException(e);
707                    }
708                    finally {
709                            closeSession(session);
710                    }
711            }
712    
713            @Override
714            public List<Permission> findByA_R(String actionId, long[] resourceIds)
715                    throws SystemException {
716    
717                    Object[] finderArgs = new Object[] {
718                            actionId, StringUtil.merge(ArrayUtil.toArray(resourceIds))
719                    };
720    
721                    List<Permission> list = (List<Permission>)FinderCacheUtil.getResult(
722                            FINDER_PATH_FIND_BY_A_R, finderArgs, this);
723    
724                    if ((list != null) && !list.isEmpty()) {
725                            for (Permission permission : list) {
726                                    if (!Validator.equals(actionId, permission.getActionId()) ||
727                                            !ArrayUtil.contains(
728                                                    resourceIds, permission.getResourceId())) {
729    
730                                            list = null;
731    
732                                            break;
733                                    }
734                            }
735                    }
736    
737                    if (list != null) {
738                            return list;
739                    }
740    
741                    Session session = null;
742    
743                    try {
744                            session = openSession();
745    
746                            String sql = CustomSQLUtil.get(FIND_BY_A_R);
747    
748                            sql = StringUtil.replace(
749                                    sql, "[$RESOURCE_ID$]", getResourceIds(resourceIds));
750    
751                            SQLQuery q = session.createSQLQuery(sql);
752    
753                            q.addEntity("Permission_", PermissionImpl.class);
754    
755                            QueryPos qPos = QueryPos.getInstance(q);
756    
757                            qPos.add(actionId);
758    
759                            setResourceIds(qPos, resourceIds);
760    
761                            list = q.list(true);
762                    }
763                    catch (Exception e) {
764                            throw new SystemException(e);
765                    }
766                    finally {
767                            if (list == null) {
768                                    list = new ArrayList<Permission>();
769                            }
770    
771                            FinderCacheUtil.putResult(
772                                    FINDER_PATH_FIND_BY_A_R, finderArgs, list);
773    
774                            closeSession(session);
775                    }
776    
777                    return list;
778            }
779    
780            @Override
781            public List<Permission> findByG_R(long groupId, long resourceId)
782                    throws SystemException {
783    
784                    Session session = null;
785    
786                    try {
787                            session = openSession();
788    
789                            String sql = CustomSQLUtil.get(FIND_BY_G_R);
790    
791                            SQLQuery q = session.createSQLQuery(sql);
792    
793                            q.addEntity("Permission_", PermissionImpl.class);
794    
795                            QueryPos qPos = QueryPos.getInstance(q);
796    
797                            qPos.add(groupId);
798                            qPos.add(resourceId);
799    
800                            return q.list(true);
801                    }
802                    catch (Exception e) {
803                            throw new SystemException(e);
804                    }
805                    finally {
806                            closeSession(session);
807                    }
808            }
809    
810            @Override
811            public List<Permission> findByR_R(long roleId, long resourceId)
812                    throws SystemException {
813    
814                    Session session = null;
815    
816                    try {
817                            session = openSession();
818    
819                            String sql = CustomSQLUtil.get(FIND_BY_R_R);
820    
821                            SQLQuery q = session.createSQLQuery(sql);
822    
823                            q.addEntity("Permission_", PermissionImpl.class);
824    
825                            QueryPos qPos = QueryPos.getInstance(q);
826    
827                            qPos.add(roleId);
828                            qPos.add(resourceId);
829    
830                            return q.list(true);
831                    }
832                    catch (Exception e) {
833                            throw new SystemException(e);
834                    }
835                    finally {
836                            closeSession(session);
837                    }
838            }
839    
840            @Override
841            public List<Permission> findByR_S(long roleId, int[] scopes)
842                    throws SystemException {
843    
844                    Session session = null;
845    
846                    try {
847                            session = openSession();
848    
849                            String sql = CustomSQLUtil.get(FIND_BY_R_S);
850    
851                            sql = StringUtil.replace(sql, "[$SCOPE$]", getScopes(scopes));
852    
853                            SQLQuery q = session.createSQLQuery(sql);
854    
855                            q.addEntity("Permission_", PermissionImpl.class);
856    
857                            QueryPos qPos = QueryPos.getInstance(q);
858    
859                            qPos.add(roleId);
860                            qPos.add(scopes);
861    
862                            return q.list(true);
863                    }
864                    catch (Exception e) {
865                            throw new SystemException(e);
866                    }
867                    finally {
868                            closeSession(session);
869                    }
870            }
871    
872            @Override
873            public List<Permission> findByU_R(long userId, long resourceId)
874                    throws SystemException {
875    
876                    Session session = null;
877    
878                    try {
879                            session = openSession();
880    
881                            String sql = CustomSQLUtil.get(FIND_BY_U_R);
882    
883                            SQLQuery q = session.createSQLQuery(sql);
884    
885                            q.addEntity("Permission_", PermissionImpl.class);
886    
887                            QueryPos qPos = QueryPos.getInstance(q);
888    
889                            qPos.add(userId);
890                            qPos.add(resourceId);
891    
892                            return q.list(true);
893                    }
894                    catch (Exception e) {
895                            throw new SystemException(e);
896                    }
897                    finally {
898                            closeSession(session);
899                    }
900            }
901    
902            @Override
903            public List<Permission> findByO_G_R(
904                            long organizationId, long groupId, long resourceId)
905                    throws SystemException {
906    
907                    Session session = null;
908    
909                    try {
910                            session = openSession();
911    
912                            String sql = CustomSQLUtil.get(FIND_BY_O_G_R);
913    
914                            SQLQuery q = session.createSQLQuery(sql);
915    
916                            q.addEntity("Permission_", PermissionImpl.class);
917    
918                            QueryPos qPos = QueryPos.getInstance(q);
919    
920                            qPos.add(organizationId);
921                            qPos.add(groupId);
922                            qPos.add(resourceId);
923    
924                            return q.list(true);
925                    }
926                    catch (Exception e) {
927                            throw new SystemException(e);
928                    }
929                    finally {
930                            closeSession(session);
931                    }
932            }
933    
934            @Override
935            public List<Permission> findByU_A_R(
936                            long userId, String[] actionIds, long resourceId)
937                    throws SystemException {
938    
939                    Session session = null;
940    
941                    try {
942                            session = openSession();
943    
944                            String sql = CustomSQLUtil.get(FIND_BY_U_R);
945    
946                            sql = StringUtil.replace(
947                                    sql, "[$ACTION_ID$]", getActionIds(actionIds));
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(userId);
956                            qPos.add(resourceId);
957    
958                            return q.list(true);
959                    }
960                    catch (Exception e) {
961                            throw new SystemException(e);
962                    }
963                    finally {
964                            closeSession(session);
965                    }
966            }
967    
968            @Override
969            public List<Permission> findByG_C_N_S_P(
970                            long groupId, long companyId, String name, int scope,
971                            String primKey)
972                    throws SystemException {
973    
974                    Session session = null;
975    
976                    try {
977                            session = openSession();
978    
979                            String sql = CustomSQLUtil.get(FIND_BY_G_C_N_S_P);
980    
981                            SQLQuery q = session.createSQLQuery(sql);
982    
983                            q.addEntity("Permission_", PermissionImpl.class);
984    
985                            QueryPos qPos = QueryPos.getInstance(q);
986    
987                            qPos.add(groupId);
988                            qPos.add(companyId);
989                            qPos.add(name);
990                            qPos.add(scope);
991                            qPos.add(primKey);
992    
993                            return q.list(true);
994                    }
995                    catch (Exception e) {
996                            throw new SystemException(e);
997                    }
998                    finally {
999                            closeSession(session);
1000                    }
1001            }
1002    
1003            @Override
1004            public List<Permission> findByU_C_N_S_P(
1005                            long userId, long companyId, String name, int scope, String primKey)
1006                    throws SystemException {
1007    
1008                    Session session = null;
1009    
1010                    try {
1011                            session = openSession();
1012    
1013                            String sql = CustomSQLUtil.get(FIND_BY_U_C_N_S_P);
1014    
1015                            SQLQuery q = session.createSQLQuery(sql);
1016    
1017                            q.addEntity("Permission_", PermissionImpl.class);
1018    
1019                            QueryPos qPos = QueryPos.getInstance(q);
1020    
1021                            qPos.add(userId);
1022                            qPos.add(companyId);
1023                            qPos.add(name);
1024                            qPos.add(scope);
1025                            qPos.add(primKey);
1026    
1027                            return q.list(true);
1028                    }
1029                    catch (Exception e) {
1030                            throw new SystemException(e);
1031                    }
1032                    finally {
1033                            closeSession(session);
1034                    }
1035            }
1036    
1037            protected String getActionIds(String[] actionIds) {
1038                    if (actionIds.length == 0) {
1039                            return StringPool.BLANK;
1040                    }
1041    
1042                    StringBundler sb = new StringBundler(actionIds.length * 2 - 1);
1043    
1044                    for (int i = 0; i < actionIds.length; i++) {
1045                            sb.append("Permission_.actionId = ?");
1046    
1047                            if ((i + 1) < actionIds.length) {
1048                                    sb.append(" OR ");
1049                            }
1050                    }
1051    
1052                    return sb.toString();
1053            }
1054    
1055            protected String getGroupIds(List<Group> groups, String table) {
1056                    if (groups.isEmpty()) {
1057                            return StringPool.BLANK;
1058                    }
1059    
1060                    StringBundler sb = new StringBundler(groups.size() * 3 - 1);
1061    
1062                    for (int i = 0; i < groups.size(); i++) {
1063                            sb.append(table);
1064                            sb.append(".groupId = ?");
1065    
1066                            if ((i + 1) < groups.size()) {
1067                                    sb.append(" OR ");
1068                            }
1069                    }
1070    
1071                    return sb.toString();
1072            }
1073    
1074            protected String getPermissionIds(
1075                    List<Permission> permissions, String table) {
1076    
1077                    if (permissions.isEmpty()) {
1078                            return StringPool.BLANK;
1079                    }
1080    
1081                    StringBundler sb = new StringBundler(permissions.size() * 3 - 1);
1082    
1083                    for (int i = 0; i < permissions.size(); i++) {
1084                            sb.append(table);
1085                            sb.append(".permissionId = ?");
1086    
1087                            if ((i + 1) < permissions.size()) {
1088                                    sb.append(" OR ");
1089                            }
1090                    }
1091    
1092                    return sb.toString();
1093            }
1094    
1095            protected String getResourceIds(long[] resourceIds) {
1096                    if (resourceIds.length == 0) {
1097                            return StringPool.BLANK;
1098                    }
1099    
1100                    StringBundler sb = new StringBundler(resourceIds.length * 2 - 1);
1101    
1102                    for (int i = 0; i < resourceIds.length; i++) {
1103                            sb.append("resourceId = ?");
1104    
1105                            if ((i + 1) < resourceIds.length) {
1106                                    sb.append(" OR ");
1107                            }
1108                    }
1109    
1110                    return sb.toString();
1111            }
1112    
1113            protected String getRoleIds(List<Role> roles, String table) {
1114                    if (roles.isEmpty()) {
1115                            return StringPool.BLANK;
1116                    }
1117    
1118                    StringBundler sb = new StringBundler(roles.size() * 3 - 1);
1119    
1120                    for (int i = 0; i < roles.size(); i++) {
1121                            sb.append(table);
1122                            sb.append(".roleId = ?");
1123    
1124                            if ((i + 1) < roles.size()) {
1125                                    sb.append(" OR ");
1126                            }
1127                    }
1128    
1129                    return sb.toString();
1130            }
1131    
1132            /**
1133             * @see {@link ResourcePermissionFinderImpl#getScopes(int[])}
1134             */
1135            protected String getScopes(int[] scopes) {
1136                    if (scopes.length == 0) {
1137                            return StringPool.BLANK;
1138                    }
1139    
1140                    StringBundler sb = new StringBundler(scopes.length * 2 + 1);
1141    
1142                    sb.append("(");
1143    
1144                    for (int i = 0; i < scopes.length; i++) {
1145                            sb.append("ResourceCode.scope = ? ");
1146    
1147                            if ((i + 1) != scopes.length) {
1148                                    sb.append("OR ");
1149                            }
1150                    }
1151    
1152                    sb.append(")");
1153    
1154                    return sb.toString();
1155            }
1156    
1157            protected void setGroupIds(QueryPos qPos, List<Group> groups) {
1158                    for (Group group : groups) {
1159                            qPos.add(group.getGroupId());
1160                    }
1161            }
1162    
1163            protected void setPermissionIds(
1164                    QueryPos qPos, List<Permission> permissions) {
1165    
1166                    for (Permission permission : permissions) {
1167                            qPos.add(permission.getPermissionId());
1168                    }
1169            }
1170    
1171            protected void setResourceIds(QueryPos qPos, long[] resourceIds) {
1172                    for (long resourceId : resourceIds) {
1173                            qPos.add(resourceId);
1174                    }
1175            }
1176    
1177            protected void setRoleIds(QueryPos qPos, List<Role> roles) {
1178                    for (Role role : roles) {
1179                            qPos.add(role.getRoleId());
1180                    }
1181            }
1182    
1183    }