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