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