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.NoSuchModelException;
018    import com.liferay.portal.NoSuchPermissionException;
019    import com.liferay.portal.kernel.bean.BeanReference;
020    import com.liferay.portal.kernel.cache.CacheRegistryUtil;
021    import com.liferay.portal.kernel.dao.jdbc.MappingSqlQuery;
022    import com.liferay.portal.kernel.dao.jdbc.MappingSqlQueryFactoryUtil;
023    import com.liferay.portal.kernel.dao.jdbc.RowMapper;
024    import com.liferay.portal.kernel.dao.jdbc.SqlUpdate;
025    import com.liferay.portal.kernel.dao.jdbc.SqlUpdateFactoryUtil;
026    import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
027    import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
028    import com.liferay.portal.kernel.dao.orm.FinderPath;
029    import com.liferay.portal.kernel.dao.orm.Query;
030    import com.liferay.portal.kernel.dao.orm.QueryPos;
031    import com.liferay.portal.kernel.dao.orm.QueryUtil;
032    import com.liferay.portal.kernel.dao.orm.SQLQuery;
033    import com.liferay.portal.kernel.dao.orm.Session;
034    import com.liferay.portal.kernel.exception.SystemException;
035    import com.liferay.portal.kernel.log.Log;
036    import com.liferay.portal.kernel.log.LogFactoryUtil;
037    import com.liferay.portal.kernel.util.GetterUtil;
038    import com.liferay.portal.kernel.util.InstanceFactory;
039    import com.liferay.portal.kernel.util.OrderByComparator;
040    import com.liferay.portal.kernel.util.SetUtil;
041    import com.liferay.portal.kernel.util.StringBundler;
042    import com.liferay.portal.kernel.util.StringPool;
043    import com.liferay.portal.kernel.util.StringUtil;
044    import com.liferay.portal.kernel.util.Validator;
045    import com.liferay.portal.model.CacheModel;
046    import com.liferay.portal.model.ModelListener;
047    import com.liferay.portal.model.Permission;
048    import com.liferay.portal.model.impl.PermissionImpl;
049    import com.liferay.portal.model.impl.PermissionModelImpl;
050    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
051    
052    import java.io.Serializable;
053    
054    import java.util.ArrayList;
055    import java.util.Collections;
056    import java.util.List;
057    import java.util.Set;
058    
059    /**
060     * The persistence implementation for the permission service.
061     *
062     * <p>
063     * Caching information and settings can be found in <code>portal.properties</code>
064     * </p>
065     *
066     * @author Brian Wing Shun Chan
067     * @see PermissionPersistence
068     * @see PermissionUtil
069     * @generated
070     */
071    public class PermissionPersistenceImpl extends BasePersistenceImpl<Permission>
072            implements PermissionPersistence {
073            /*
074             * NOTE FOR DEVELOPERS:
075             *
076             * Never modify or reference this class directly. Always use {@link PermissionUtil} to access the permission persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
077             */
078            public static final String FINDER_CLASS_NAME_ENTITY = PermissionImpl.class.getName();
079            public static final String FINDER_CLASS_NAME_LIST_WITH_PAGINATION = FINDER_CLASS_NAME_ENTITY +
080                    ".List1";
081            public static final String FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION = FINDER_CLASS_NAME_ENTITY +
082                    ".List2";
083            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_RESOURCEID =
084                    new FinderPath(PermissionModelImpl.ENTITY_CACHE_ENABLED,
085                            PermissionModelImpl.FINDER_CACHE_ENABLED, PermissionImpl.class,
086                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByResourceId",
087                            new String[] {
088                                    Long.class.getName(),
089                                    
090                            "java.lang.Integer", "java.lang.Integer",
091                                    "com.liferay.portal.kernel.util.OrderByComparator"
092                            });
093            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_RESOURCEID =
094                    new FinderPath(PermissionModelImpl.ENTITY_CACHE_ENABLED,
095                            PermissionModelImpl.FINDER_CACHE_ENABLED, PermissionImpl.class,
096                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByResourceId",
097                            new String[] { Long.class.getName() },
098                            PermissionModelImpl.RESOURCEID_COLUMN_BITMASK);
099            public static final FinderPath FINDER_PATH_COUNT_BY_RESOURCEID = new FinderPath(PermissionModelImpl.ENTITY_CACHE_ENABLED,
100                            PermissionModelImpl.FINDER_CACHE_ENABLED, Long.class,
101                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByResourceId",
102                            new String[] { Long.class.getName() });
103            public static final FinderPath FINDER_PATH_FETCH_BY_A_R = new FinderPath(PermissionModelImpl.ENTITY_CACHE_ENABLED,
104                            PermissionModelImpl.FINDER_CACHE_ENABLED, PermissionImpl.class,
105                            FINDER_CLASS_NAME_ENTITY, "fetchByA_R",
106                            new String[] { String.class.getName(), Long.class.getName() },
107                            PermissionModelImpl.ACTIONID_COLUMN_BITMASK |
108                            PermissionModelImpl.RESOURCEID_COLUMN_BITMASK);
109            public static final FinderPath FINDER_PATH_COUNT_BY_A_R = new FinderPath(PermissionModelImpl.ENTITY_CACHE_ENABLED,
110                            PermissionModelImpl.FINDER_CACHE_ENABLED, Long.class,
111                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByA_R",
112                            new String[] { String.class.getName(), Long.class.getName() });
113            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_ALL = new FinderPath(PermissionModelImpl.ENTITY_CACHE_ENABLED,
114                            PermissionModelImpl.FINDER_CACHE_ENABLED, PermissionImpl.class,
115                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findAll", new String[0]);
116            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(PermissionModelImpl.ENTITY_CACHE_ENABLED,
117                            PermissionModelImpl.FINDER_CACHE_ENABLED, PermissionImpl.class,
118                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
119            public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(PermissionModelImpl.ENTITY_CACHE_ENABLED,
120                            PermissionModelImpl.FINDER_CACHE_ENABLED, Long.class,
121                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]);
122    
123            /**
124             * Caches the permission in the entity cache if it is enabled.
125             *
126             * @param permission the permission
127             */
128            public void cacheResult(Permission permission) {
129                    EntityCacheUtil.putResult(PermissionModelImpl.ENTITY_CACHE_ENABLED,
130                            PermissionImpl.class, permission.getPrimaryKey(), permission);
131    
132                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_A_R,
133                            new Object[] {
134                                    permission.getActionId(),
135                                    Long.valueOf(permission.getResourceId())
136                            }, permission);
137    
138                    permission.resetOriginalValues();
139            }
140    
141            /**
142             * Caches the permissions in the entity cache if it is enabled.
143             *
144             * @param permissions the permissions
145             */
146            public void cacheResult(List<Permission> permissions) {
147                    for (Permission permission : permissions) {
148                            if (EntityCacheUtil.getResult(
149                                                    PermissionModelImpl.ENTITY_CACHE_ENABLED,
150                                                    PermissionImpl.class, permission.getPrimaryKey()) == null) {
151                                    cacheResult(permission);
152                            }
153                            else {
154                                    permission.resetOriginalValues();
155                            }
156                    }
157            }
158    
159            /**
160             * Clears the cache for all permissions.
161             *
162             * <p>
163             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
164             * </p>
165             */
166            @Override
167            public void clearCache() {
168                    if (_HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
169                            CacheRegistryUtil.clear(PermissionImpl.class.getName());
170                    }
171    
172                    EntityCacheUtil.clearCache(PermissionImpl.class.getName());
173    
174                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
175                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
176                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
177            }
178    
179            /**
180             * Clears the cache for the permission.
181             *
182             * <p>
183             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
184             * </p>
185             */
186            @Override
187            public void clearCache(Permission permission) {
188                    EntityCacheUtil.removeResult(PermissionModelImpl.ENTITY_CACHE_ENABLED,
189                            PermissionImpl.class, permission.getPrimaryKey());
190    
191                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
192                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
193    
194                    clearUniqueFindersCache(permission);
195            }
196    
197            @Override
198            public void clearCache(List<Permission> permissions) {
199                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
200                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
201    
202                    for (Permission permission : permissions) {
203                            EntityCacheUtil.removeResult(PermissionModelImpl.ENTITY_CACHE_ENABLED,
204                                    PermissionImpl.class, permission.getPrimaryKey());
205    
206                            clearUniqueFindersCache(permission);
207                    }
208            }
209    
210            protected void cacheUniqueFindersCache(Permission permission) {
211                    if (permission.isNew()) {
212                            Object[] args = new Object[] {
213                                            permission.getActionId(),
214                                            Long.valueOf(permission.getResourceId())
215                                    };
216    
217                            FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_A_R, args,
218                                    Long.valueOf(1));
219                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_A_R, args, permission);
220                    }
221                    else {
222                            PermissionModelImpl permissionModelImpl = (PermissionModelImpl)permission;
223    
224                            if ((permissionModelImpl.getColumnBitmask() &
225                                            FINDER_PATH_FETCH_BY_A_R.getColumnBitmask()) != 0) {
226                                    Object[] args = new Object[] {
227                                                    permission.getActionId(),
228                                                    Long.valueOf(permission.getResourceId())
229                                            };
230    
231                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_A_R, args,
232                                            Long.valueOf(1));
233                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_A_R, args,
234                                            permission);
235                            }
236                    }
237            }
238    
239            protected void clearUniqueFindersCache(Permission permission) {
240                    PermissionModelImpl permissionModelImpl = (PermissionModelImpl)permission;
241    
242                    Object[] args = new Object[] {
243                                    permission.getActionId(),
244                                    Long.valueOf(permission.getResourceId())
245                            };
246    
247                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_A_R, args);
248                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_A_R, args);
249    
250                    if ((permissionModelImpl.getColumnBitmask() &
251                                    FINDER_PATH_FETCH_BY_A_R.getColumnBitmask()) != 0) {
252                            args = new Object[] {
253                                            permissionModelImpl.getOriginalActionId(),
254                                            Long.valueOf(permissionModelImpl.getOriginalResourceId())
255                                    };
256    
257                            FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_A_R, args);
258                            FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_A_R, args);
259                    }
260            }
261    
262            /**
263             * Creates a new permission with the primary key. Does not add the permission to the database.
264             *
265             * @param permissionId the primary key for the new permission
266             * @return the new permission
267             */
268            public Permission create(long permissionId) {
269                    Permission permission = new PermissionImpl();
270    
271                    permission.setNew(true);
272                    permission.setPrimaryKey(permissionId);
273    
274                    return permission;
275            }
276    
277            /**
278             * Removes the permission with the primary key from the database. Also notifies the appropriate model listeners.
279             *
280             * @param permissionId the primary key of the permission
281             * @return the permission that was removed
282             * @throws com.liferay.portal.NoSuchPermissionException if a permission with the primary key could not be found
283             * @throws SystemException if a system exception occurred
284             */
285            public Permission remove(long permissionId)
286                    throws NoSuchPermissionException, SystemException {
287                    return remove(Long.valueOf(permissionId));
288            }
289    
290            /**
291             * Removes the permission with the primary key from the database. Also notifies the appropriate model listeners.
292             *
293             * @param primaryKey the primary key of the permission
294             * @return the permission that was removed
295             * @throws com.liferay.portal.NoSuchPermissionException if a permission with the primary key could not be found
296             * @throws SystemException if a system exception occurred
297             */
298            @Override
299            public Permission remove(Serializable primaryKey)
300                    throws NoSuchPermissionException, SystemException {
301                    Session session = null;
302    
303                    try {
304                            session = openSession();
305    
306                            Permission permission = (Permission)session.get(PermissionImpl.class,
307                                            primaryKey);
308    
309                            if (permission == null) {
310                                    if (_log.isWarnEnabled()) {
311                                            _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
312                                    }
313    
314                                    throw new NoSuchPermissionException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
315                                            primaryKey);
316                            }
317    
318                            return remove(permission);
319                    }
320                    catch (NoSuchPermissionException nsee) {
321                            throw nsee;
322                    }
323                    catch (Exception e) {
324                            throw processException(e);
325                    }
326                    finally {
327                            closeSession(session);
328                    }
329            }
330    
331            @Override
332            protected Permission removeImpl(Permission permission)
333                    throws SystemException {
334                    permission = toUnwrappedModel(permission);
335    
336                    try {
337                            clearGroups.clear(permission.getPrimaryKey());
338                    }
339                    catch (Exception e) {
340                            throw processException(e);
341                    }
342                    finally {
343                            FinderCacheUtil.clearCache(PermissionModelImpl.MAPPING_TABLE_GROUPS_PERMISSIONS_NAME);
344                    }
345    
346                    try {
347                            clearRoles.clear(permission.getPrimaryKey());
348                    }
349                    catch (Exception e) {
350                            throw processException(e);
351                    }
352                    finally {
353                            FinderCacheUtil.clearCache(PermissionModelImpl.MAPPING_TABLE_ROLES_PERMISSIONS_NAME);
354                    }
355    
356                    try {
357                            clearUsers.clear(permission.getPrimaryKey());
358                    }
359                    catch (Exception e) {
360                            throw processException(e);
361                    }
362                    finally {
363                            FinderCacheUtil.clearCache(PermissionModelImpl.MAPPING_TABLE_USERS_PERMISSIONS_NAME);
364                    }
365    
366                    Session session = null;
367    
368                    try {
369                            session = openSession();
370    
371                            BatchSessionUtil.delete(session, permission);
372                    }
373                    catch (Exception e) {
374                            throw processException(e);
375                    }
376                    finally {
377                            closeSession(session);
378                    }
379    
380                    clearCache(permission);
381    
382                    return permission;
383            }
384    
385            @Override
386            public Permission updateImpl(
387                    com.liferay.portal.model.Permission permission, boolean merge)
388                    throws SystemException {
389                    permission = toUnwrappedModel(permission);
390    
391                    boolean isNew = permission.isNew();
392    
393                    PermissionModelImpl permissionModelImpl = (PermissionModelImpl)permission;
394    
395                    Session session = null;
396    
397                    try {
398                            session = openSession();
399    
400                            BatchSessionUtil.update(session, permission, merge);
401    
402                            permission.setNew(false);
403                    }
404                    catch (Exception e) {
405                            throw processException(e);
406                    }
407                    finally {
408                            closeSession(session);
409                    }
410    
411                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
412    
413                    if (isNew || !PermissionModelImpl.COLUMN_BITMASK_ENABLED) {
414                            FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
415                    }
416    
417                    else {
418                            if ((permissionModelImpl.getColumnBitmask() &
419                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_RESOURCEID.getColumnBitmask()) != 0) {
420                                    Object[] args = new Object[] {
421                                                    Long.valueOf(permissionModelImpl.getOriginalResourceId())
422                                            };
423    
424                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_RESOURCEID,
425                                            args);
426                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_RESOURCEID,
427                                            args);
428    
429                                    args = new Object[] {
430                                                    Long.valueOf(permissionModelImpl.getResourceId())
431                                            };
432    
433                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_RESOURCEID,
434                                            args);
435                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_RESOURCEID,
436                                            args);
437                            }
438                    }
439    
440                    EntityCacheUtil.putResult(PermissionModelImpl.ENTITY_CACHE_ENABLED,
441                            PermissionImpl.class, permission.getPrimaryKey(), permission);
442    
443                    clearUniqueFindersCache(permission);
444                    cacheUniqueFindersCache(permission);
445    
446                    return permission;
447            }
448    
449            protected Permission toUnwrappedModel(Permission permission) {
450                    if (permission instanceof PermissionImpl) {
451                            return permission;
452                    }
453    
454                    PermissionImpl permissionImpl = new PermissionImpl();
455    
456                    permissionImpl.setNew(permission.isNew());
457                    permissionImpl.setPrimaryKey(permission.getPrimaryKey());
458    
459                    permissionImpl.setPermissionId(permission.getPermissionId());
460                    permissionImpl.setCompanyId(permission.getCompanyId());
461                    permissionImpl.setActionId(permission.getActionId());
462                    permissionImpl.setResourceId(permission.getResourceId());
463    
464                    return permissionImpl;
465            }
466    
467            /**
468             * Returns the permission with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
469             *
470             * @param primaryKey the primary key of the permission
471             * @return the permission
472             * @throws com.liferay.portal.NoSuchModelException if a permission with the primary key could not be found
473             * @throws SystemException if a system exception occurred
474             */
475            @Override
476            public Permission findByPrimaryKey(Serializable primaryKey)
477                    throws NoSuchModelException, SystemException {
478                    return findByPrimaryKey(((Long)primaryKey).longValue());
479            }
480    
481            /**
482             * Returns the permission with the primary key or throws a {@link com.liferay.portal.NoSuchPermissionException} if it could not be found.
483             *
484             * @param permissionId the primary key of the permission
485             * @return the permission
486             * @throws com.liferay.portal.NoSuchPermissionException if a permission with the primary key could not be found
487             * @throws SystemException if a system exception occurred
488             */
489            public Permission findByPrimaryKey(long permissionId)
490                    throws NoSuchPermissionException, SystemException {
491                    Permission permission = fetchByPrimaryKey(permissionId);
492    
493                    if (permission == null) {
494                            if (_log.isWarnEnabled()) {
495                                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + permissionId);
496                            }
497    
498                            throw new NoSuchPermissionException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
499                                    permissionId);
500                    }
501    
502                    return permission;
503            }
504    
505            /**
506             * Returns the permission with the primary key or returns <code>null</code> if it could not be found.
507             *
508             * @param primaryKey the primary key of the permission
509             * @return the permission, or <code>null</code> if a permission with the primary key could not be found
510             * @throws SystemException if a system exception occurred
511             */
512            @Override
513            public Permission fetchByPrimaryKey(Serializable primaryKey)
514                    throws SystemException {
515                    return fetchByPrimaryKey(((Long)primaryKey).longValue());
516            }
517    
518            /**
519             * Returns the permission with the primary key or returns <code>null</code> if it could not be found.
520             *
521             * @param permissionId the primary key of the permission
522             * @return the permission, or <code>null</code> if a permission with the primary key could not be found
523             * @throws SystemException if a system exception occurred
524             */
525            public Permission fetchByPrimaryKey(long permissionId)
526                    throws SystemException {
527                    Permission permission = (Permission)EntityCacheUtil.getResult(PermissionModelImpl.ENTITY_CACHE_ENABLED,
528                                    PermissionImpl.class, permissionId);
529    
530                    if (permission == _nullPermission) {
531                            return null;
532                    }
533    
534                    if (permission == null) {
535                            Session session = null;
536    
537                            boolean hasException = false;
538    
539                            try {
540                                    session = openSession();
541    
542                                    permission = (Permission)session.get(PermissionImpl.class,
543                                                    Long.valueOf(permissionId));
544                            }
545                            catch (Exception e) {
546                                    hasException = true;
547    
548                                    throw processException(e);
549                            }
550                            finally {
551                                    if (permission != null) {
552                                            cacheResult(permission);
553                                    }
554                                    else if (!hasException) {
555                                            EntityCacheUtil.putResult(PermissionModelImpl.ENTITY_CACHE_ENABLED,
556                                                    PermissionImpl.class, permissionId, _nullPermission);
557                                    }
558    
559                                    closeSession(session);
560                            }
561                    }
562    
563                    return permission;
564            }
565    
566            /**
567             * Returns all the permissions where resourceId = &#63;.
568             *
569             * @param resourceId the resource ID
570             * @return the matching permissions
571             * @throws SystemException if a system exception occurred
572             */
573            public List<Permission> findByResourceId(long resourceId)
574                    throws SystemException {
575                    return findByResourceId(resourceId, QueryUtil.ALL_POS,
576                            QueryUtil.ALL_POS, null);
577            }
578    
579            /**
580             * Returns a range of all the permissions where resourceId = &#63;.
581             *
582             * <p>
583             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
584             * </p>
585             *
586             * @param resourceId the resource ID
587             * @param start the lower bound of the range of permissions
588             * @param end the upper bound of the range of permissions (not inclusive)
589             * @return the range of matching permissions
590             * @throws SystemException if a system exception occurred
591             */
592            public List<Permission> findByResourceId(long resourceId, int start, int end)
593                    throws SystemException {
594                    return findByResourceId(resourceId, start, end, null);
595            }
596    
597            /**
598             * Returns an ordered range of all the permissions where resourceId = &#63;.
599             *
600             * <p>
601             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
602             * </p>
603             *
604             * @param resourceId the resource ID
605             * @param start the lower bound of the range of permissions
606             * @param end the upper bound of the range of permissions (not inclusive)
607             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
608             * @return the ordered range of matching permissions
609             * @throws SystemException if a system exception occurred
610             */
611            public List<Permission> findByResourceId(long resourceId, int start,
612                    int end, OrderByComparator orderByComparator) throws SystemException {
613                    FinderPath finderPath = null;
614                    Object[] finderArgs = null;
615    
616                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
617                                    (orderByComparator == null)) {
618                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_RESOURCEID;
619                            finderArgs = new Object[] { resourceId };
620                    }
621                    else {
622                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_RESOURCEID;
623                            finderArgs = new Object[] { resourceId, start, end, orderByComparator };
624                    }
625    
626                    List<Permission> list = (List<Permission>)FinderCacheUtil.getResult(finderPath,
627                                    finderArgs, this);
628    
629                    if ((list != null) && !list.isEmpty()) {
630                            for (Permission permission : list) {
631                                    if ((resourceId != permission.getResourceId())) {
632                                            list = null;
633    
634                                            break;
635                                    }
636                            }
637                    }
638    
639                    if (list == null) {
640                            StringBundler query = null;
641    
642                            if (orderByComparator != null) {
643                                    query = new StringBundler(3 +
644                                                    (orderByComparator.getOrderByFields().length * 3));
645                            }
646                            else {
647                                    query = new StringBundler(2);
648                            }
649    
650                            query.append(_SQL_SELECT_PERMISSION_WHERE);
651    
652                            query.append(_FINDER_COLUMN_RESOURCEID_RESOURCEID_2);
653    
654                            if (orderByComparator != null) {
655                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
656                                            orderByComparator);
657                            }
658    
659                            String sql = query.toString();
660    
661                            Session session = null;
662    
663                            try {
664                                    session = openSession();
665    
666                                    Query q = session.createQuery(sql);
667    
668                                    QueryPos qPos = QueryPos.getInstance(q);
669    
670                                    qPos.add(resourceId);
671    
672                                    list = (List<Permission>)QueryUtil.list(q, getDialect(), start,
673                                                    end);
674                            }
675                            catch (Exception e) {
676                                    throw processException(e);
677                            }
678                            finally {
679                                    if (list == null) {
680                                            FinderCacheUtil.removeResult(finderPath, finderArgs);
681                                    }
682                                    else {
683                                            cacheResult(list);
684    
685                                            FinderCacheUtil.putResult(finderPath, finderArgs, list);
686                                    }
687    
688                                    closeSession(session);
689                            }
690                    }
691    
692                    return list;
693            }
694    
695            /**
696             * Returns the first permission in the ordered set where resourceId = &#63;.
697             *
698             * @param resourceId the resource ID
699             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
700             * @return the first matching permission
701             * @throws com.liferay.portal.NoSuchPermissionException if a matching permission could not be found
702             * @throws SystemException if a system exception occurred
703             */
704            public Permission findByResourceId_First(long resourceId,
705                    OrderByComparator orderByComparator)
706                    throws NoSuchPermissionException, SystemException {
707                    Permission permission = fetchByResourceId_First(resourceId,
708                                    orderByComparator);
709    
710                    if (permission != null) {
711                            return permission;
712                    }
713    
714                    StringBundler msg = new StringBundler(4);
715    
716                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
717    
718                    msg.append("resourceId=");
719                    msg.append(resourceId);
720    
721                    msg.append(StringPool.CLOSE_CURLY_BRACE);
722    
723                    throw new NoSuchPermissionException(msg.toString());
724            }
725    
726            /**
727             * Returns the first permission in the ordered set where resourceId = &#63;.
728             *
729             * @param resourceId the resource ID
730             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
731             * @return the first matching permission, or <code>null</code> if a matching permission could not be found
732             * @throws SystemException if a system exception occurred
733             */
734            public Permission fetchByResourceId_First(long resourceId,
735                    OrderByComparator orderByComparator) throws SystemException {
736                    List<Permission> list = findByResourceId(resourceId, 0, 1,
737                                    orderByComparator);
738    
739                    if (!list.isEmpty()) {
740                            return list.get(0);
741                    }
742    
743                    return null;
744            }
745    
746            /**
747             * Returns the last permission in the ordered set where resourceId = &#63;.
748             *
749             * @param resourceId the resource ID
750             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
751             * @return the last matching permission
752             * @throws com.liferay.portal.NoSuchPermissionException if a matching permission could not be found
753             * @throws SystemException if a system exception occurred
754             */
755            public Permission findByResourceId_Last(long resourceId,
756                    OrderByComparator orderByComparator)
757                    throws NoSuchPermissionException, SystemException {
758                    Permission permission = fetchByResourceId_Last(resourceId,
759                                    orderByComparator);
760    
761                    if (permission != null) {
762                            return permission;
763                    }
764    
765                    StringBundler msg = new StringBundler(4);
766    
767                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
768    
769                    msg.append("resourceId=");
770                    msg.append(resourceId);
771    
772                    msg.append(StringPool.CLOSE_CURLY_BRACE);
773    
774                    throw new NoSuchPermissionException(msg.toString());
775            }
776    
777            /**
778             * Returns the last permission in the ordered set where resourceId = &#63;.
779             *
780             * @param resourceId the resource ID
781             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
782             * @return the last matching permission, or <code>null</code> if a matching permission could not be found
783             * @throws SystemException if a system exception occurred
784             */
785            public Permission fetchByResourceId_Last(long resourceId,
786                    OrderByComparator orderByComparator) throws SystemException {
787                    int count = countByResourceId(resourceId);
788    
789                    List<Permission> list = findByResourceId(resourceId, count - 1, count,
790                                    orderByComparator);
791    
792                    if (!list.isEmpty()) {
793                            return list.get(0);
794                    }
795    
796                    return null;
797            }
798    
799            /**
800             * Returns the permissions before and after the current permission in the ordered set where resourceId = &#63;.
801             *
802             * @param permissionId the primary key of the current permission
803             * @param resourceId the resource ID
804             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
805             * @return the previous, current, and next permission
806             * @throws com.liferay.portal.NoSuchPermissionException if a permission with the primary key could not be found
807             * @throws SystemException if a system exception occurred
808             */
809            public Permission[] findByResourceId_PrevAndNext(long permissionId,
810                    long resourceId, OrderByComparator orderByComparator)
811                    throws NoSuchPermissionException, SystemException {
812                    Permission permission = findByPrimaryKey(permissionId);
813    
814                    Session session = null;
815    
816                    try {
817                            session = openSession();
818    
819                            Permission[] array = new PermissionImpl[3];
820    
821                            array[0] = getByResourceId_PrevAndNext(session, permission,
822                                            resourceId, orderByComparator, true);
823    
824                            array[1] = permission;
825    
826                            array[2] = getByResourceId_PrevAndNext(session, permission,
827                                            resourceId, orderByComparator, false);
828    
829                            return array;
830                    }
831                    catch (Exception e) {
832                            throw processException(e);
833                    }
834                    finally {
835                            closeSession(session);
836                    }
837            }
838    
839            protected Permission getByResourceId_PrevAndNext(Session session,
840                    Permission permission, long resourceId,
841                    OrderByComparator orderByComparator, boolean previous) {
842                    StringBundler query = null;
843    
844                    if (orderByComparator != null) {
845                            query = new StringBundler(6 +
846                                            (orderByComparator.getOrderByFields().length * 6));
847                    }
848                    else {
849                            query = new StringBundler(3);
850                    }
851    
852                    query.append(_SQL_SELECT_PERMISSION_WHERE);
853    
854                    query.append(_FINDER_COLUMN_RESOURCEID_RESOURCEID_2);
855    
856                    if (orderByComparator != null) {
857                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
858    
859                            if (orderByConditionFields.length > 0) {
860                                    query.append(WHERE_AND);
861                            }
862    
863                            for (int i = 0; i < orderByConditionFields.length; i++) {
864                                    query.append(_ORDER_BY_ENTITY_ALIAS);
865                                    query.append(orderByConditionFields[i]);
866    
867                                    if ((i + 1) < orderByConditionFields.length) {
868                                            if (orderByComparator.isAscending() ^ previous) {
869                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
870                                            }
871                                            else {
872                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
873                                            }
874                                    }
875                                    else {
876                                            if (orderByComparator.isAscending() ^ previous) {
877                                                    query.append(WHERE_GREATER_THAN);
878                                            }
879                                            else {
880                                                    query.append(WHERE_LESSER_THAN);
881                                            }
882                                    }
883                            }
884    
885                            query.append(ORDER_BY_CLAUSE);
886    
887                            String[] orderByFields = orderByComparator.getOrderByFields();
888    
889                            for (int i = 0; i < orderByFields.length; i++) {
890                                    query.append(_ORDER_BY_ENTITY_ALIAS);
891                                    query.append(orderByFields[i]);
892    
893                                    if ((i + 1) < orderByFields.length) {
894                                            if (orderByComparator.isAscending() ^ previous) {
895                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
896                                            }
897                                            else {
898                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
899                                            }
900                                    }
901                                    else {
902                                            if (orderByComparator.isAscending() ^ previous) {
903                                                    query.append(ORDER_BY_ASC);
904                                            }
905                                            else {
906                                                    query.append(ORDER_BY_DESC);
907                                            }
908                                    }
909                            }
910                    }
911    
912                    String sql = query.toString();
913    
914                    Query q = session.createQuery(sql);
915    
916                    q.setFirstResult(0);
917                    q.setMaxResults(2);
918    
919                    QueryPos qPos = QueryPos.getInstance(q);
920    
921                    qPos.add(resourceId);
922    
923                    if (orderByComparator != null) {
924                            Object[] values = orderByComparator.getOrderByConditionValues(permission);
925    
926                            for (Object value : values) {
927                                    qPos.add(value);
928                            }
929                    }
930    
931                    List<Permission> list = q.list();
932    
933                    if (list.size() == 2) {
934                            return list.get(1);
935                    }
936                    else {
937                            return null;
938                    }
939            }
940    
941            /**
942             * Returns the permission where actionId = &#63; and resourceId = &#63; or throws a {@link com.liferay.portal.NoSuchPermissionException} if it could not be found.
943             *
944             * @param actionId the action ID
945             * @param resourceId the resource ID
946             * @return the matching permission
947             * @throws com.liferay.portal.NoSuchPermissionException if a matching permission could not be found
948             * @throws SystemException if a system exception occurred
949             */
950            public Permission findByA_R(String actionId, long resourceId)
951                    throws NoSuchPermissionException, SystemException {
952                    Permission permission = fetchByA_R(actionId, resourceId);
953    
954                    if (permission == null) {
955                            StringBundler msg = new StringBundler(6);
956    
957                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
958    
959                            msg.append("actionId=");
960                            msg.append(actionId);
961    
962                            msg.append(", resourceId=");
963                            msg.append(resourceId);
964    
965                            msg.append(StringPool.CLOSE_CURLY_BRACE);
966    
967                            if (_log.isWarnEnabled()) {
968                                    _log.warn(msg.toString());
969                            }
970    
971                            throw new NoSuchPermissionException(msg.toString());
972                    }
973    
974                    return permission;
975            }
976    
977            /**
978             * Returns the permission where actionId = &#63; and resourceId = &#63; or returns <code>null</code> if it could not be found. Uses the finder cache.
979             *
980             * @param actionId the action ID
981             * @param resourceId the resource ID
982             * @return the matching permission, or <code>null</code> if a matching permission could not be found
983             * @throws SystemException if a system exception occurred
984             */
985            public Permission fetchByA_R(String actionId, long resourceId)
986                    throws SystemException {
987                    return fetchByA_R(actionId, resourceId, true);
988            }
989    
990            /**
991             * Returns the permission where actionId = &#63; and resourceId = &#63; or returns <code>null</code> if it could not be found, optionally using the finder cache.
992             *
993             * @param actionId the action ID
994             * @param resourceId the resource ID
995             * @param retrieveFromCache whether to use the finder cache
996             * @return the matching permission, or <code>null</code> if a matching permission could not be found
997             * @throws SystemException if a system exception occurred
998             */
999            public Permission fetchByA_R(String actionId, long resourceId,
1000                    boolean retrieveFromCache) throws SystemException {
1001                    Object[] finderArgs = new Object[] { actionId, resourceId };
1002    
1003                    Object result = null;
1004    
1005                    if (retrieveFromCache) {
1006                            result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_A_R,
1007                                            finderArgs, this);
1008                    }
1009    
1010                    if (result instanceof Permission) {
1011                            Permission permission = (Permission)result;
1012    
1013                            if (!Validator.equals(actionId, permission.getActionId()) ||
1014                                            (resourceId != permission.getResourceId())) {
1015                                    result = null;
1016                            }
1017                    }
1018    
1019                    if (result == null) {
1020                            StringBundler query = new StringBundler(3);
1021    
1022                            query.append(_SQL_SELECT_PERMISSION_WHERE);
1023    
1024                            if (actionId == null) {
1025                                    query.append(_FINDER_COLUMN_A_R_ACTIONID_1);
1026                            }
1027                            else {
1028                                    if (actionId.equals(StringPool.BLANK)) {
1029                                            query.append(_FINDER_COLUMN_A_R_ACTIONID_3);
1030                                    }
1031                                    else {
1032                                            query.append(_FINDER_COLUMN_A_R_ACTIONID_2);
1033                                    }
1034                            }
1035    
1036                            query.append(_FINDER_COLUMN_A_R_RESOURCEID_2);
1037    
1038                            String sql = query.toString();
1039    
1040                            Session session = null;
1041    
1042                            try {
1043                                    session = openSession();
1044    
1045                                    Query q = session.createQuery(sql);
1046    
1047                                    QueryPos qPos = QueryPos.getInstance(q);
1048    
1049                                    if (actionId != null) {
1050                                            qPos.add(actionId);
1051                                    }
1052    
1053                                    qPos.add(resourceId);
1054    
1055                                    List<Permission> list = q.list();
1056    
1057                                    result = list;
1058    
1059                                    Permission permission = null;
1060    
1061                                    if (list.isEmpty()) {
1062                                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_A_R,
1063                                                    finderArgs, list);
1064                                    }
1065                                    else {
1066                                            permission = list.get(0);
1067    
1068                                            cacheResult(permission);
1069    
1070                                            if ((permission.getActionId() == null) ||
1071                                                            !permission.getActionId().equals(actionId) ||
1072                                                            (permission.getResourceId() != resourceId)) {
1073                                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_A_R,
1074                                                            finderArgs, permission);
1075                                            }
1076                                    }
1077    
1078                                    return permission;
1079                            }
1080                            catch (Exception e) {
1081                                    throw processException(e);
1082                            }
1083                            finally {
1084                                    if (result == null) {
1085                                            FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_A_R,
1086                                                    finderArgs);
1087                                    }
1088    
1089                                    closeSession(session);
1090                            }
1091                    }
1092                    else {
1093                            if (result instanceof List<?>) {
1094                                    return null;
1095                            }
1096                            else {
1097                                    return (Permission)result;
1098                            }
1099                    }
1100            }
1101    
1102            /**
1103             * Returns all the permissions.
1104             *
1105             * @return the permissions
1106             * @throws SystemException if a system exception occurred
1107             */
1108            public List<Permission> findAll() throws SystemException {
1109                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
1110            }
1111    
1112            /**
1113             * Returns a range of all the permissions.
1114             *
1115             * <p>
1116             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
1117             * </p>
1118             *
1119             * @param start the lower bound of the range of permissions
1120             * @param end the upper bound of the range of permissions (not inclusive)
1121             * @return the range of permissions
1122             * @throws SystemException if a system exception occurred
1123             */
1124            public List<Permission> findAll(int start, int end)
1125                    throws SystemException {
1126                    return findAll(start, end, null);
1127            }
1128    
1129            /**
1130             * Returns an ordered range of all the permissions.
1131             *
1132             * <p>
1133             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
1134             * </p>
1135             *
1136             * @param start the lower bound of the range of permissions
1137             * @param end the upper bound of the range of permissions (not inclusive)
1138             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
1139             * @return the ordered range of permissions
1140             * @throws SystemException if a system exception occurred
1141             */
1142            public List<Permission> findAll(int start, int end,
1143                    OrderByComparator orderByComparator) throws SystemException {
1144                    FinderPath finderPath = null;
1145                    Object[] finderArgs = new Object[] { start, end, orderByComparator };
1146    
1147                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
1148                                    (orderByComparator == null)) {
1149                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
1150                            finderArgs = FINDER_ARGS_EMPTY;
1151                    }
1152                    else {
1153                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
1154                            finderArgs = new Object[] { start, end, orderByComparator };
1155                    }
1156    
1157                    List<Permission> list = (List<Permission>)FinderCacheUtil.getResult(finderPath,
1158                                    finderArgs, this);
1159    
1160                    if (list == null) {
1161                            StringBundler query = null;
1162                            String sql = null;
1163    
1164                            if (orderByComparator != null) {
1165                                    query = new StringBundler(2 +
1166                                                    (orderByComparator.getOrderByFields().length * 3));
1167    
1168                                    query.append(_SQL_SELECT_PERMISSION);
1169    
1170                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1171                                            orderByComparator);
1172    
1173                                    sql = query.toString();
1174                            }
1175                            else {
1176                                    sql = _SQL_SELECT_PERMISSION;
1177                            }
1178    
1179                            Session session = null;
1180    
1181                            try {
1182                                    session = openSession();
1183    
1184                                    Query q = session.createQuery(sql);
1185    
1186                                    if (orderByComparator == null) {
1187                                            list = (List<Permission>)QueryUtil.list(q, getDialect(),
1188                                                            start, end, false);
1189    
1190                                            Collections.sort(list);
1191                                    }
1192                                    else {
1193                                            list = (List<Permission>)QueryUtil.list(q, getDialect(),
1194                                                            start, end);
1195                                    }
1196                            }
1197                            catch (Exception e) {
1198                                    throw processException(e);
1199                            }
1200                            finally {
1201                                    if (list == null) {
1202                                            FinderCacheUtil.removeResult(finderPath, finderArgs);
1203                                    }
1204                                    else {
1205                                            cacheResult(list);
1206    
1207                                            FinderCacheUtil.putResult(finderPath, finderArgs, list);
1208                                    }
1209    
1210                                    closeSession(session);
1211                            }
1212                    }
1213    
1214                    return list;
1215            }
1216    
1217            /**
1218             * Removes all the permissions where resourceId = &#63; from the database.
1219             *
1220             * @param resourceId the resource ID
1221             * @throws SystemException if a system exception occurred
1222             */
1223            public void removeByResourceId(long resourceId) throws SystemException {
1224                    for (Permission permission : findByResourceId(resourceId)) {
1225                            remove(permission);
1226                    }
1227            }
1228    
1229            /**
1230             * Removes the permission where actionId = &#63; and resourceId = &#63; from the database.
1231             *
1232             * @param actionId the action ID
1233             * @param resourceId the resource ID
1234             * @return the permission that was removed
1235             * @throws SystemException if a system exception occurred
1236             */
1237            public Permission removeByA_R(String actionId, long resourceId)
1238                    throws NoSuchPermissionException, SystemException {
1239                    Permission permission = findByA_R(actionId, resourceId);
1240    
1241                    return remove(permission);
1242            }
1243    
1244            /**
1245             * Removes all the permissions from the database.
1246             *
1247             * @throws SystemException if a system exception occurred
1248             */
1249            public void removeAll() throws SystemException {
1250                    for (Permission permission : findAll()) {
1251                            remove(permission);
1252                    }
1253            }
1254    
1255            /**
1256             * Returns the number of permissions where resourceId = &#63;.
1257             *
1258             * @param resourceId the resource ID
1259             * @return the number of matching permissions
1260             * @throws SystemException if a system exception occurred
1261             */
1262            public int countByResourceId(long resourceId) throws SystemException {
1263                    Object[] finderArgs = new Object[] { resourceId };
1264    
1265                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_RESOURCEID,
1266                                    finderArgs, this);
1267    
1268                    if (count == null) {
1269                            StringBundler query = new StringBundler(2);
1270    
1271                            query.append(_SQL_COUNT_PERMISSION_WHERE);
1272    
1273                            query.append(_FINDER_COLUMN_RESOURCEID_RESOURCEID_2);
1274    
1275                            String sql = query.toString();
1276    
1277                            Session session = null;
1278    
1279                            try {
1280                                    session = openSession();
1281    
1282                                    Query q = session.createQuery(sql);
1283    
1284                                    QueryPos qPos = QueryPos.getInstance(q);
1285    
1286                                    qPos.add(resourceId);
1287    
1288                                    count = (Long)q.uniqueResult();
1289                            }
1290                            catch (Exception e) {
1291                                    throw processException(e);
1292                            }
1293                            finally {
1294                                    if (count == null) {
1295                                            count = Long.valueOf(0);
1296                                    }
1297    
1298                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_RESOURCEID,
1299                                            finderArgs, count);
1300    
1301                                    closeSession(session);
1302                            }
1303                    }
1304    
1305                    return count.intValue();
1306            }
1307    
1308            /**
1309             * Returns the number of permissions where actionId = &#63; and resourceId = &#63;.
1310             *
1311             * @param actionId the action ID
1312             * @param resourceId the resource ID
1313             * @return the number of matching permissions
1314             * @throws SystemException if a system exception occurred
1315             */
1316            public int countByA_R(String actionId, long resourceId)
1317                    throws SystemException {
1318                    Object[] finderArgs = new Object[] { actionId, resourceId };
1319    
1320                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_A_R,
1321                                    finderArgs, this);
1322    
1323                    if (count == null) {
1324                            StringBundler query = new StringBundler(3);
1325    
1326                            query.append(_SQL_COUNT_PERMISSION_WHERE);
1327    
1328                            if (actionId == null) {
1329                                    query.append(_FINDER_COLUMN_A_R_ACTIONID_1);
1330                            }
1331                            else {
1332                                    if (actionId.equals(StringPool.BLANK)) {
1333                                            query.append(_FINDER_COLUMN_A_R_ACTIONID_3);
1334                                    }
1335                                    else {
1336                                            query.append(_FINDER_COLUMN_A_R_ACTIONID_2);
1337                                    }
1338                            }
1339    
1340                            query.append(_FINDER_COLUMN_A_R_RESOURCEID_2);
1341    
1342                            String sql = query.toString();
1343    
1344                            Session session = null;
1345    
1346                            try {
1347                                    session = openSession();
1348    
1349                                    Query q = session.createQuery(sql);
1350    
1351                                    QueryPos qPos = QueryPos.getInstance(q);
1352    
1353                                    if (actionId != null) {
1354                                            qPos.add(actionId);
1355                                    }
1356    
1357                                    qPos.add(resourceId);
1358    
1359                                    count = (Long)q.uniqueResult();
1360                            }
1361                            catch (Exception e) {
1362                                    throw processException(e);
1363                            }
1364                            finally {
1365                                    if (count == null) {
1366                                            count = Long.valueOf(0);
1367                                    }
1368    
1369                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_A_R, finderArgs,
1370                                            count);
1371    
1372                                    closeSession(session);
1373                            }
1374                    }
1375    
1376                    return count.intValue();
1377            }
1378    
1379            /**
1380             * Returns the number of permissions.
1381             *
1382             * @return the number of permissions
1383             * @throws SystemException if a system exception occurred
1384             */
1385            public int countAll() throws SystemException {
1386                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1387                                    FINDER_ARGS_EMPTY, this);
1388    
1389                    if (count == null) {
1390                            Session session = null;
1391    
1392                            try {
1393                                    session = openSession();
1394    
1395                                    Query q = session.createQuery(_SQL_COUNT_PERMISSION);
1396    
1397                                    count = (Long)q.uniqueResult();
1398                            }
1399                            catch (Exception e) {
1400                                    throw processException(e);
1401                            }
1402                            finally {
1403                                    if (count == null) {
1404                                            count = Long.valueOf(0);
1405                                    }
1406    
1407                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
1408                                            FINDER_ARGS_EMPTY, count);
1409    
1410                                    closeSession(session);
1411                            }
1412                    }
1413    
1414                    return count.intValue();
1415            }
1416    
1417            /**
1418             * Returns all the groups associated with the permission.
1419             *
1420             * @param pk the primary key of the permission
1421             * @return the groups associated with the permission
1422             * @throws SystemException if a system exception occurred
1423             */
1424            public List<com.liferay.portal.model.Group> getGroups(long pk)
1425                    throws SystemException {
1426                    return getGroups(pk, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
1427            }
1428    
1429            /**
1430             * Returns a range of all the groups associated with the permission.
1431             *
1432             * <p>
1433             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
1434             * </p>
1435             *
1436             * @param pk the primary key of the permission
1437             * @param start the lower bound of the range of permissions
1438             * @param end the upper bound of the range of permissions (not inclusive)
1439             * @return the range of groups associated with the permission
1440             * @throws SystemException if a system exception occurred
1441             */
1442            public List<com.liferay.portal.model.Group> getGroups(long pk, int start,
1443                    int end) throws SystemException {
1444                    return getGroups(pk, start, end, null);
1445            }
1446    
1447            public static final FinderPath FINDER_PATH_GET_GROUPS = new FinderPath(com.liferay.portal.model.impl.GroupModelImpl.ENTITY_CACHE_ENABLED,
1448                            PermissionModelImpl.FINDER_CACHE_ENABLED_GROUPS_PERMISSIONS,
1449                            com.liferay.portal.model.impl.GroupImpl.class,
1450                            PermissionModelImpl.MAPPING_TABLE_GROUPS_PERMISSIONS_NAME,
1451                            "getGroups",
1452                            new String[] {
1453                                    Long.class.getName(), "java.lang.Integer", "java.lang.Integer",
1454                                    "com.liferay.portal.kernel.util.OrderByComparator"
1455                            });
1456    
1457            static {
1458                    FINDER_PATH_GET_GROUPS.setCacheKeyGeneratorCacheName(null);
1459            }
1460    
1461            /**
1462             * Returns an ordered range of all the groups associated with the permission.
1463             *
1464             * <p>
1465             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
1466             * </p>
1467             *
1468             * @param pk the primary key of the permission
1469             * @param start the lower bound of the range of permissions
1470             * @param end the upper bound of the range of permissions (not inclusive)
1471             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
1472             * @return the ordered range of groups associated with the permission
1473             * @throws SystemException if a system exception occurred
1474             */
1475            public List<com.liferay.portal.model.Group> getGroups(long pk, int start,
1476                    int end, OrderByComparator orderByComparator) throws SystemException {
1477                    Object[] finderArgs = new Object[] { pk, start, end, orderByComparator };
1478    
1479                    List<com.liferay.portal.model.Group> list = (List<com.liferay.portal.model.Group>)FinderCacheUtil.getResult(FINDER_PATH_GET_GROUPS,
1480                                    finderArgs, this);
1481    
1482                    if (list == null) {
1483                            Session session = null;
1484    
1485                            try {
1486                                    session = openSession();
1487    
1488                                    String sql = null;
1489    
1490                                    if (orderByComparator != null) {
1491                                            sql = _SQL_GETGROUPS.concat(ORDER_BY_CLAUSE)
1492                                                                                    .concat(orderByComparator.getOrderBy());
1493                                    }
1494                                    else {
1495                                            sql = _SQL_GETGROUPS.concat(com.liferay.portal.model.impl.GroupModelImpl.ORDER_BY_SQL);
1496                                    }
1497    
1498                                    SQLQuery q = session.createSQLQuery(sql);
1499    
1500                                    q.addEntity("Group_",
1501                                            com.liferay.portal.model.impl.GroupImpl.class);
1502    
1503                                    QueryPos qPos = QueryPos.getInstance(q);
1504    
1505                                    qPos.add(pk);
1506    
1507                                    list = (List<com.liferay.portal.model.Group>)QueryUtil.list(q,
1508                                                    getDialect(), start, end);
1509                            }
1510                            catch (Exception e) {
1511                                    throw processException(e);
1512                            }
1513                            finally {
1514                                    if (list == null) {
1515                                            FinderCacheUtil.removeResult(FINDER_PATH_GET_GROUPS,
1516                                                    finderArgs);
1517                                    }
1518                                    else {
1519                                            groupPersistence.cacheResult(list);
1520    
1521                                            FinderCacheUtil.putResult(FINDER_PATH_GET_GROUPS,
1522                                                    finderArgs, list);
1523                                    }
1524    
1525                                    closeSession(session);
1526                            }
1527                    }
1528    
1529                    return list;
1530            }
1531    
1532            public static final FinderPath FINDER_PATH_GET_GROUPS_SIZE = new FinderPath(com.liferay.portal.model.impl.GroupModelImpl.ENTITY_CACHE_ENABLED,
1533                            PermissionModelImpl.FINDER_CACHE_ENABLED_GROUPS_PERMISSIONS,
1534                            Long.class,
1535                            PermissionModelImpl.MAPPING_TABLE_GROUPS_PERMISSIONS_NAME,
1536                            "getGroupsSize", new String[] { Long.class.getName() });
1537    
1538            static {
1539                    FINDER_PATH_GET_GROUPS_SIZE.setCacheKeyGeneratorCacheName(null);
1540            }
1541    
1542            /**
1543             * Returns the number of groups associated with the permission.
1544             *
1545             * @param pk the primary key of the permission
1546             * @return the number of groups associated with the permission
1547             * @throws SystemException if a system exception occurred
1548             */
1549            public int getGroupsSize(long pk) throws SystemException {
1550                    Object[] finderArgs = new Object[] { pk };
1551    
1552                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_GET_GROUPS_SIZE,
1553                                    finderArgs, this);
1554    
1555                    if (count == null) {
1556                            Session session = null;
1557    
1558                            try {
1559                                    session = openSession();
1560    
1561                                    SQLQuery q = session.createSQLQuery(_SQL_GETGROUPSSIZE);
1562    
1563                                    q.addScalar(COUNT_COLUMN_NAME,
1564                                            com.liferay.portal.kernel.dao.orm.Type.LONG);
1565    
1566                                    QueryPos qPos = QueryPos.getInstance(q);
1567    
1568                                    qPos.add(pk);
1569    
1570                                    count = (Long)q.uniqueResult();
1571                            }
1572                            catch (Exception e) {
1573                                    throw processException(e);
1574                            }
1575                            finally {
1576                                    if (count == null) {
1577                                            count = Long.valueOf(0);
1578                                    }
1579    
1580                                    FinderCacheUtil.putResult(FINDER_PATH_GET_GROUPS_SIZE,
1581                                            finderArgs, count);
1582    
1583                                    closeSession(session);
1584                            }
1585                    }
1586    
1587                    return count.intValue();
1588            }
1589    
1590            public static final FinderPath FINDER_PATH_CONTAINS_GROUP = new FinderPath(com.liferay.portal.model.impl.GroupModelImpl.ENTITY_CACHE_ENABLED,
1591                            PermissionModelImpl.FINDER_CACHE_ENABLED_GROUPS_PERMISSIONS,
1592                            Boolean.class,
1593                            PermissionModelImpl.MAPPING_TABLE_GROUPS_PERMISSIONS_NAME,
1594                            "containsGroup",
1595                            new String[] { Long.class.getName(), Long.class.getName() });
1596    
1597            /**
1598             * Returns <code>true</code> if the group is associated with the permission.
1599             *
1600             * @param pk the primary key of the permission
1601             * @param groupPK the primary key of the group
1602             * @return <code>true</code> if the group is associated with the permission; <code>false</code> otherwise
1603             * @throws SystemException if a system exception occurred
1604             */
1605            public boolean containsGroup(long pk, long groupPK)
1606                    throws SystemException {
1607                    Object[] finderArgs = new Object[] { pk, groupPK };
1608    
1609                    Boolean value = (Boolean)FinderCacheUtil.getResult(FINDER_PATH_CONTAINS_GROUP,
1610                                    finderArgs, this);
1611    
1612                    if (value == null) {
1613                            try {
1614                                    value = Boolean.valueOf(containsGroup.contains(pk, groupPK));
1615                            }
1616                            catch (Exception e) {
1617                                    throw processException(e);
1618                            }
1619                            finally {
1620                                    if (value == null) {
1621                                            value = Boolean.FALSE;
1622                                    }
1623    
1624                                    FinderCacheUtil.putResult(FINDER_PATH_CONTAINS_GROUP,
1625                                            finderArgs, value);
1626                            }
1627                    }
1628    
1629                    return value.booleanValue();
1630            }
1631    
1632            /**
1633             * Returns <code>true</code> if the permission has any groups associated with it.
1634             *
1635             * @param pk the primary key of the permission to check for associations with groups
1636             * @return <code>true</code> if the permission has any groups associated with it; <code>false</code> otherwise
1637             * @throws SystemException if a system exception occurred
1638             */
1639            public boolean containsGroups(long pk) throws SystemException {
1640                    if (getGroupsSize(pk) > 0) {
1641                            return true;
1642                    }
1643                    else {
1644                            return false;
1645                    }
1646            }
1647    
1648            /**
1649             * Adds an association between the permission and the group. Also notifies the appropriate model listeners and clears the mapping table finder cache.
1650             *
1651             * @param pk the primary key of the permission
1652             * @param groupPK the primary key of the group
1653             * @throws SystemException if a system exception occurred
1654             */
1655            public void addGroup(long pk, long groupPK) throws SystemException {
1656                    try {
1657                            addGroup.add(pk, groupPK);
1658                    }
1659                    catch (Exception e) {
1660                            throw processException(e);
1661                    }
1662                    finally {
1663                            FinderCacheUtil.clearCache(PermissionModelImpl.MAPPING_TABLE_GROUPS_PERMISSIONS_NAME);
1664                    }
1665            }
1666    
1667            /**
1668             * Adds an association between the permission and the group. Also notifies the appropriate model listeners and clears the mapping table finder cache.
1669             *
1670             * @param pk the primary key of the permission
1671             * @param group the group
1672             * @throws SystemException if a system exception occurred
1673             */
1674            public void addGroup(long pk, com.liferay.portal.model.Group group)
1675                    throws SystemException {
1676                    try {
1677                            addGroup.add(pk, group.getPrimaryKey());
1678                    }
1679                    catch (Exception e) {
1680                            throw processException(e);
1681                    }
1682                    finally {
1683                            FinderCacheUtil.clearCache(PermissionModelImpl.MAPPING_TABLE_GROUPS_PERMISSIONS_NAME);
1684                    }
1685            }
1686    
1687            /**
1688             * Adds an association between the permission and the groups. Also notifies the appropriate model listeners and clears the mapping table finder cache.
1689             *
1690             * @param pk the primary key of the permission
1691             * @param groupPKs the primary keys of the groups
1692             * @throws SystemException if a system exception occurred
1693             */
1694            public void addGroups(long pk, long[] groupPKs) throws SystemException {
1695                    try {
1696                            for (long groupPK : groupPKs) {
1697                                    addGroup.add(pk, groupPK);
1698                            }
1699                    }
1700                    catch (Exception e) {
1701                            throw processException(e);
1702                    }
1703                    finally {
1704                            FinderCacheUtil.clearCache(PermissionModelImpl.MAPPING_TABLE_GROUPS_PERMISSIONS_NAME);
1705                    }
1706            }
1707    
1708            /**
1709             * Adds an association between the permission and the groups. Also notifies the appropriate model listeners and clears the mapping table finder cache.
1710             *
1711             * @param pk the primary key of the permission
1712             * @param groups the groups
1713             * @throws SystemException if a system exception occurred
1714             */
1715            public void addGroups(long pk, List<com.liferay.portal.model.Group> groups)
1716                    throws SystemException {
1717                    try {
1718                            for (com.liferay.portal.model.Group group : groups) {
1719                                    addGroup.add(pk, group.getPrimaryKey());
1720                            }
1721                    }
1722                    catch (Exception e) {
1723                            throw processException(e);
1724                    }
1725                    finally {
1726                            FinderCacheUtil.clearCache(PermissionModelImpl.MAPPING_TABLE_GROUPS_PERMISSIONS_NAME);
1727                    }
1728            }
1729    
1730            /**
1731             * Clears all associations between the permission and its groups. Also notifies the appropriate model listeners and clears the mapping table finder cache.
1732             *
1733             * @param pk the primary key of the permission to clear the associated groups from
1734             * @throws SystemException if a system exception occurred
1735             */
1736            public void clearGroups(long pk) throws SystemException {
1737                    try {
1738                            clearGroups.clear(pk);
1739                    }
1740                    catch (Exception e) {
1741                            throw processException(e);
1742                    }
1743                    finally {
1744                            FinderCacheUtil.clearCache(PermissionModelImpl.MAPPING_TABLE_GROUPS_PERMISSIONS_NAME);
1745                    }
1746            }
1747    
1748            /**
1749             * Removes the association between the permission and the group. Also notifies the appropriate model listeners and clears the mapping table finder cache.
1750             *
1751             * @param pk the primary key of the permission
1752             * @param groupPK the primary key of the group
1753             * @throws SystemException if a system exception occurred
1754             */
1755            public void removeGroup(long pk, long groupPK) throws SystemException {
1756                    try {
1757                            removeGroup.remove(pk, groupPK);
1758                    }
1759                    catch (Exception e) {
1760                            throw processException(e);
1761                    }
1762                    finally {
1763                            FinderCacheUtil.clearCache(PermissionModelImpl.MAPPING_TABLE_GROUPS_PERMISSIONS_NAME);
1764                    }
1765            }
1766    
1767            /**
1768             * Removes the association between the permission and the group. Also notifies the appropriate model listeners and clears the mapping table finder cache.
1769             *
1770             * @param pk the primary key of the permission
1771             * @param group the group
1772             * @throws SystemException if a system exception occurred
1773             */
1774            public void removeGroup(long pk, com.liferay.portal.model.Group group)
1775                    throws SystemException {
1776                    try {
1777                            removeGroup.remove(pk, group.getPrimaryKey());
1778                    }
1779                    catch (Exception e) {
1780                            throw processException(e);
1781                    }
1782                    finally {
1783                            FinderCacheUtil.clearCache(PermissionModelImpl.MAPPING_TABLE_GROUPS_PERMISSIONS_NAME);
1784                    }
1785            }
1786    
1787            /**
1788             * Removes the association between the permission and the groups. Also notifies the appropriate model listeners and clears the mapping table finder cache.
1789             *
1790             * @param pk the primary key of the permission
1791             * @param groupPKs the primary keys of the groups
1792             * @throws SystemException if a system exception occurred
1793             */
1794            public void removeGroups(long pk, long[] groupPKs)
1795                    throws SystemException {
1796                    try {
1797                            for (long groupPK : groupPKs) {
1798                                    removeGroup.remove(pk, groupPK);
1799                            }
1800                    }
1801                    catch (Exception e) {
1802                            throw processException(e);
1803                    }
1804                    finally {
1805                            FinderCacheUtil.clearCache(PermissionModelImpl.MAPPING_TABLE_GROUPS_PERMISSIONS_NAME);
1806                    }
1807            }
1808    
1809            /**
1810             * Removes the association between the permission and the groups. Also notifies the appropriate model listeners and clears the mapping table finder cache.
1811             *
1812             * @param pk the primary key of the permission
1813             * @param groups the groups
1814             * @throws SystemException if a system exception occurred
1815             */
1816            public void removeGroups(long pk,
1817                    List<com.liferay.portal.model.Group> groups) throws SystemException {
1818                    try {
1819                            for (com.liferay.portal.model.Group group : groups) {
1820                                    removeGroup.remove(pk, group.getPrimaryKey());
1821                            }
1822                    }
1823                    catch (Exception e) {
1824                            throw processException(e);
1825                    }
1826                    finally {
1827                            FinderCacheUtil.clearCache(PermissionModelImpl.MAPPING_TABLE_GROUPS_PERMISSIONS_NAME);
1828                    }
1829            }
1830    
1831            /**
1832             * Sets the groups associated with the permission, removing and adding associations as necessary. Also notifies the appropriate model listeners and clears the mapping table finder cache.
1833             *
1834             * @param pk the primary key of the permission
1835             * @param groupPKs the primary keys of the groups to be associated with the permission
1836             * @throws SystemException if a system exception occurred
1837             */
1838            public void setGroups(long pk, long[] groupPKs) throws SystemException {
1839                    try {
1840                            Set<Long> groupPKSet = SetUtil.fromArray(groupPKs);
1841    
1842                            List<com.liferay.portal.model.Group> groups = getGroups(pk);
1843    
1844                            for (com.liferay.portal.model.Group group : groups) {
1845                                    if (!groupPKSet.remove(group.getPrimaryKey())) {
1846                                            removeGroup.remove(pk, group.getPrimaryKey());
1847                                    }
1848                            }
1849    
1850                            for (Long groupPK : groupPKSet) {
1851                                    addGroup.add(pk, groupPK);
1852                            }
1853                    }
1854                    catch (Exception e) {
1855                            throw processException(e);
1856                    }
1857                    finally {
1858                            FinderCacheUtil.clearCache(PermissionModelImpl.MAPPING_TABLE_GROUPS_PERMISSIONS_NAME);
1859                    }
1860            }
1861    
1862            /**
1863             * Sets the groups associated with the permission, removing and adding associations as necessary. Also notifies the appropriate model listeners and clears the mapping table finder cache.
1864             *
1865             * @param pk the primary key of the permission
1866             * @param groups the groups to be associated with the permission
1867             * @throws SystemException if a system exception occurred
1868             */
1869            public void setGroups(long pk, List<com.liferay.portal.model.Group> groups)
1870                    throws SystemException {
1871                    try {
1872                            long[] groupPKs = new long[groups.size()];
1873    
1874                            for (int i = 0; i < groups.size(); i++) {
1875                                    com.liferay.portal.model.Group group = groups.get(i);
1876    
1877                                    groupPKs[i] = group.getPrimaryKey();
1878                            }
1879    
1880                            setGroups(pk, groupPKs);
1881                    }
1882                    catch (Exception e) {
1883                            throw processException(e);
1884                    }
1885                    finally {
1886                            FinderCacheUtil.clearCache(PermissionModelImpl.MAPPING_TABLE_GROUPS_PERMISSIONS_NAME);
1887                    }
1888            }
1889    
1890            /**
1891             * Returns all the roles associated with the permission.
1892             *
1893             * @param pk the primary key of the permission
1894             * @return the roles associated with the permission
1895             * @throws SystemException if a system exception occurred
1896             */
1897            public List<com.liferay.portal.model.Role> getRoles(long pk)
1898                    throws SystemException {
1899                    return getRoles(pk, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
1900            }
1901    
1902            /**
1903             * Returns a range of all the roles associated with the permission.
1904             *
1905             * <p>
1906             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
1907             * </p>
1908             *
1909             * @param pk the primary key of the permission
1910             * @param start the lower bound of the range of permissions
1911             * @param end the upper bound of the range of permissions (not inclusive)
1912             * @return the range of roles associated with the permission
1913             * @throws SystemException if a system exception occurred
1914             */
1915            public List<com.liferay.portal.model.Role> getRoles(long pk, int start,
1916                    int end) throws SystemException {
1917                    return getRoles(pk, start, end, null);
1918            }
1919    
1920            public static final FinderPath FINDER_PATH_GET_ROLES = new FinderPath(com.liferay.portal.model.impl.RoleModelImpl.ENTITY_CACHE_ENABLED,
1921                            PermissionModelImpl.FINDER_CACHE_ENABLED_ROLES_PERMISSIONS,
1922                            com.liferay.portal.model.impl.RoleImpl.class,
1923                            PermissionModelImpl.MAPPING_TABLE_ROLES_PERMISSIONS_NAME,
1924                            "getRoles",
1925                            new String[] {
1926                                    Long.class.getName(), "java.lang.Integer", "java.lang.Integer",
1927                                    "com.liferay.portal.kernel.util.OrderByComparator"
1928                            });
1929    
1930            static {
1931                    FINDER_PATH_GET_ROLES.setCacheKeyGeneratorCacheName(null);
1932            }
1933    
1934            /**
1935             * Returns an ordered range of all the roles associated with the permission.
1936             *
1937             * <p>
1938             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
1939             * </p>
1940             *
1941             * @param pk the primary key of the permission
1942             * @param start the lower bound of the range of permissions
1943             * @param end the upper bound of the range of permissions (not inclusive)
1944             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
1945             * @return the ordered range of roles associated with the permission
1946             * @throws SystemException if a system exception occurred
1947             */
1948            public List<com.liferay.portal.model.Role> getRoles(long pk, int start,
1949                    int end, OrderByComparator orderByComparator) throws SystemException {
1950                    Object[] finderArgs = new Object[] { pk, start, end, orderByComparator };
1951    
1952                    List<com.liferay.portal.model.Role> list = (List<com.liferay.portal.model.Role>)FinderCacheUtil.getResult(FINDER_PATH_GET_ROLES,
1953                                    finderArgs, this);
1954    
1955                    if (list == null) {
1956                            Session session = null;
1957    
1958                            try {
1959                                    session = openSession();
1960    
1961                                    String sql = null;
1962    
1963                                    if (orderByComparator != null) {
1964                                            sql = _SQL_GETROLES.concat(ORDER_BY_CLAUSE)
1965                                                                               .concat(orderByComparator.getOrderBy());
1966                                    }
1967                                    else {
1968                                            sql = _SQL_GETROLES.concat(com.liferay.portal.model.impl.RoleModelImpl.ORDER_BY_SQL);
1969                                    }
1970    
1971                                    SQLQuery q = session.createSQLQuery(sql);
1972    
1973                                    q.addEntity("Role_",
1974                                            com.liferay.portal.model.impl.RoleImpl.class);
1975    
1976                                    QueryPos qPos = QueryPos.getInstance(q);
1977    
1978                                    qPos.add(pk);
1979    
1980                                    list = (List<com.liferay.portal.model.Role>)QueryUtil.list(q,
1981                                                    getDialect(), start, end);
1982                            }
1983                            catch (Exception e) {
1984                                    throw processException(e);
1985                            }
1986                            finally {
1987                                    if (list == null) {
1988                                            FinderCacheUtil.removeResult(FINDER_PATH_GET_ROLES,
1989                                                    finderArgs);
1990                                    }
1991                                    else {
1992                                            rolePersistence.cacheResult(list);
1993    
1994                                            FinderCacheUtil.putResult(FINDER_PATH_GET_ROLES,
1995                                                    finderArgs, list);
1996                                    }
1997    
1998                                    closeSession(session);
1999                            }
2000                    }
2001    
2002                    return list;
2003            }
2004    
2005            public static final FinderPath FINDER_PATH_GET_ROLES_SIZE = new FinderPath(com.liferay.portal.model.impl.RoleModelImpl.ENTITY_CACHE_ENABLED,
2006                            PermissionModelImpl.FINDER_CACHE_ENABLED_ROLES_PERMISSIONS,
2007                            Long.class,
2008                            PermissionModelImpl.MAPPING_TABLE_ROLES_PERMISSIONS_NAME,
2009                            "getRolesSize", new String[] { Long.class.getName() });
2010    
2011            static {
2012                    FINDER_PATH_GET_ROLES_SIZE.setCacheKeyGeneratorCacheName(null);
2013            }
2014    
2015            /**
2016             * Returns the number of roles associated with the permission.
2017             *
2018             * @param pk the primary key of the permission
2019             * @return the number of roles associated with the permission
2020             * @throws SystemException if a system exception occurred
2021             */
2022            public int getRolesSize(long pk) throws SystemException {
2023                    Object[] finderArgs = new Object[] { pk };
2024    
2025                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_GET_ROLES_SIZE,
2026                                    finderArgs, this);
2027    
2028                    if (count == null) {
2029                            Session session = null;
2030    
2031                            try {
2032                                    session = openSession();
2033    
2034                                    SQLQuery q = session.createSQLQuery(_SQL_GETROLESSIZE);
2035    
2036                                    q.addScalar(COUNT_COLUMN_NAME,
2037                                            com.liferay.portal.kernel.dao.orm.Type.LONG);
2038    
2039                                    QueryPos qPos = QueryPos.getInstance(q);
2040    
2041                                    qPos.add(pk);
2042    
2043                                    count = (Long)q.uniqueResult();
2044                            }
2045                            catch (Exception e) {
2046                                    throw processException(e);
2047                            }
2048                            finally {
2049                                    if (count == null) {
2050                                            count = Long.valueOf(0);
2051                                    }
2052    
2053                                    FinderCacheUtil.putResult(FINDER_PATH_GET_ROLES_SIZE,
2054                                            finderArgs, count);
2055    
2056                                    closeSession(session);
2057                            }
2058                    }
2059    
2060                    return count.intValue();
2061            }
2062    
2063            public static final FinderPath FINDER_PATH_CONTAINS_ROLE = new FinderPath(com.liferay.portal.model.impl.RoleModelImpl.ENTITY_CACHE_ENABLED,
2064                            PermissionModelImpl.FINDER_CACHE_ENABLED_ROLES_PERMISSIONS,
2065                            Boolean.class,
2066                            PermissionModelImpl.MAPPING_TABLE_ROLES_PERMISSIONS_NAME,
2067                            "containsRole",
2068                            new String[] { Long.class.getName(), Long.class.getName() });
2069    
2070            /**
2071             * Returns <code>true</code> if the role is associated with the permission.
2072             *
2073             * @param pk the primary key of the permission
2074             * @param rolePK the primary key of the role
2075             * @return <code>true</code> if the role is associated with the permission; <code>false</code> otherwise
2076             * @throws SystemException if a system exception occurred
2077             */
2078            public boolean containsRole(long pk, long rolePK) throws SystemException {
2079                    Object[] finderArgs = new Object[] { pk, rolePK };
2080    
2081                    Boolean value = (Boolean)FinderCacheUtil.getResult(FINDER_PATH_CONTAINS_ROLE,
2082                                    finderArgs, this);
2083    
2084                    if (value == null) {
2085                            try {
2086                                    value = Boolean.valueOf(containsRole.contains(pk, rolePK));
2087                            }
2088                            catch (Exception e) {
2089                                    throw processException(e);
2090                            }
2091                            finally {
2092                                    if (value == null) {
2093                                            value = Boolean.FALSE;
2094                                    }
2095    
2096                                    FinderCacheUtil.putResult(FINDER_PATH_CONTAINS_ROLE,
2097                                            finderArgs, value);
2098                            }
2099                    }
2100    
2101                    return value.booleanValue();
2102            }
2103    
2104            /**
2105             * Returns <code>true</code> if the permission has any roles associated with it.
2106             *
2107             * @param pk the primary key of the permission to check for associations with roles
2108             * @return <code>true</code> if the permission has any roles associated with it; <code>false</code> otherwise
2109             * @throws SystemException if a system exception occurred
2110             */
2111            public boolean containsRoles(long pk) throws SystemException {
2112                    if (getRolesSize(pk) > 0) {
2113                            return true;
2114                    }
2115                    else {
2116                            return false;
2117                    }
2118            }
2119    
2120            /**
2121             * Adds an association between the permission and the role. Also notifies the appropriate model listeners and clears the mapping table finder cache.
2122             *
2123             * @param pk the primary key of the permission
2124             * @param rolePK the primary key of the role
2125             * @throws SystemException if a system exception occurred
2126             */
2127            public void addRole(long pk, long rolePK) throws SystemException {
2128                    try {
2129                            addRole.add(pk, rolePK);
2130                    }
2131                    catch (Exception e) {
2132                            throw processException(e);
2133                    }
2134                    finally {
2135                            FinderCacheUtil.clearCache(PermissionModelImpl.MAPPING_TABLE_ROLES_PERMISSIONS_NAME);
2136                    }
2137            }
2138    
2139            /**
2140             * Adds an association between the permission and the role. Also notifies the appropriate model listeners and clears the mapping table finder cache.
2141             *
2142             * @param pk the primary key of the permission
2143             * @param role the role
2144             * @throws SystemException if a system exception occurred
2145             */
2146            public void addRole(long pk, com.liferay.portal.model.Role role)
2147                    throws SystemException {
2148                    try {
2149                            addRole.add(pk, role.getPrimaryKey());
2150                    }
2151                    catch (Exception e) {
2152                            throw processException(e);
2153                    }
2154                    finally {
2155                            FinderCacheUtil.clearCache(PermissionModelImpl.MAPPING_TABLE_ROLES_PERMISSIONS_NAME);
2156                    }
2157            }
2158    
2159            /**
2160             * Adds an association between the permission and the roles. Also notifies the appropriate model listeners and clears the mapping table finder cache.
2161             *
2162             * @param pk the primary key of the permission
2163             * @param rolePKs the primary keys of the roles
2164             * @throws SystemException if a system exception occurred
2165             */
2166            public void addRoles(long pk, long[] rolePKs) throws SystemException {
2167                    try {
2168                            for (long rolePK : rolePKs) {
2169                                    addRole.add(pk, rolePK);
2170                            }
2171                    }
2172                    catch (Exception e) {
2173                            throw processException(e);
2174                    }
2175                    finally {
2176                            FinderCacheUtil.clearCache(PermissionModelImpl.MAPPING_TABLE_ROLES_PERMISSIONS_NAME);
2177                    }
2178            }
2179    
2180            /**
2181             * Adds an association between the permission and the roles. Also notifies the appropriate model listeners and clears the mapping table finder cache.
2182             *
2183             * @param pk the primary key of the permission
2184             * @param roles the roles
2185             * @throws SystemException if a system exception occurred
2186             */
2187            public void addRoles(long pk, List<com.liferay.portal.model.Role> roles)
2188                    throws SystemException {
2189                    try {
2190                            for (com.liferay.portal.model.Role role : roles) {
2191                                    addRole.add(pk, role.getPrimaryKey());
2192                            }
2193                    }
2194                    catch (Exception e) {
2195                            throw processException(e);
2196                    }
2197                    finally {
2198                            FinderCacheUtil.clearCache(PermissionModelImpl.MAPPING_TABLE_ROLES_PERMISSIONS_NAME);
2199                    }
2200            }
2201    
2202            /**
2203             * Clears all associations between the permission and its roles. Also notifies the appropriate model listeners and clears the mapping table finder cache.
2204             *
2205             * @param pk the primary key of the permission to clear the associated roles from
2206             * @throws SystemException if a system exception occurred
2207             */
2208            public void clearRoles(long pk) throws SystemException {
2209                    try {
2210                            clearRoles.clear(pk);
2211                    }
2212                    catch (Exception e) {
2213                            throw processException(e);
2214                    }
2215                    finally {
2216                            FinderCacheUtil.clearCache(PermissionModelImpl.MAPPING_TABLE_ROLES_PERMISSIONS_NAME);
2217                    }
2218            }
2219    
2220            /**
2221             * Removes the association between the permission and the role. Also notifies the appropriate model listeners and clears the mapping table finder cache.
2222             *
2223             * @param pk the primary key of the permission
2224             * @param rolePK the primary key of the role
2225             * @throws SystemException if a system exception occurred
2226             */
2227            public void removeRole(long pk, long rolePK) throws SystemException {
2228                    try {
2229                            removeRole.remove(pk, rolePK);
2230                    }
2231                    catch (Exception e) {
2232                            throw processException(e);
2233                    }
2234                    finally {
2235                            FinderCacheUtil.clearCache(PermissionModelImpl.MAPPING_TABLE_ROLES_PERMISSIONS_NAME);
2236                    }
2237            }
2238    
2239            /**
2240             * Removes the association between the permission and the role. Also notifies the appropriate model listeners and clears the mapping table finder cache.
2241             *
2242             * @param pk the primary key of the permission
2243             * @param role the role
2244             * @throws SystemException if a system exception occurred
2245             */
2246            public void removeRole(long pk, com.liferay.portal.model.Role role)
2247                    throws SystemException {
2248                    try {
2249                            removeRole.remove(pk, role.getPrimaryKey());
2250                    }
2251                    catch (Exception e) {
2252                            throw processException(e);
2253                    }
2254                    finally {
2255                            FinderCacheUtil.clearCache(PermissionModelImpl.MAPPING_TABLE_ROLES_PERMISSIONS_NAME);
2256                    }
2257            }
2258    
2259            /**
2260             * Removes the association between the permission and the roles. Also notifies the appropriate model listeners and clears the mapping table finder cache.
2261             *
2262             * @param pk the primary key of the permission
2263             * @param rolePKs the primary keys of the roles
2264             * @throws SystemException if a system exception occurred
2265             */
2266            public void removeRoles(long pk, long[] rolePKs) throws SystemException {
2267                    try {
2268                            for (long rolePK : rolePKs) {
2269                                    removeRole.remove(pk, rolePK);
2270                            }
2271                    }
2272                    catch (Exception e) {
2273                            throw processException(e);
2274                    }
2275                    finally {
2276                            FinderCacheUtil.clearCache(PermissionModelImpl.MAPPING_TABLE_ROLES_PERMISSIONS_NAME);
2277                    }
2278            }
2279    
2280            /**
2281             * Removes the association between the permission and the roles. Also notifies the appropriate model listeners and clears the mapping table finder cache.
2282             *
2283             * @param pk the primary key of the permission
2284             * @param roles the roles
2285             * @throws SystemException if a system exception occurred
2286             */
2287            public void removeRoles(long pk, List<com.liferay.portal.model.Role> roles)
2288                    throws SystemException {
2289                    try {
2290                            for (com.liferay.portal.model.Role role : roles) {
2291                                    removeRole.remove(pk, role.getPrimaryKey());
2292                            }
2293                    }
2294                    catch (Exception e) {
2295                            throw processException(e);
2296                    }
2297                    finally {
2298                            FinderCacheUtil.clearCache(PermissionModelImpl.MAPPING_TABLE_ROLES_PERMISSIONS_NAME);
2299                    }
2300            }
2301    
2302            /**
2303             * Sets the roles associated with the permission, removing and adding associations as necessary. Also notifies the appropriate model listeners and clears the mapping table finder cache.
2304             *
2305             * @param pk the primary key of the permission
2306             * @param rolePKs the primary keys of the roles to be associated with the permission
2307             * @throws SystemException if a system exception occurred
2308             */
2309            public void setRoles(long pk, long[] rolePKs) throws SystemException {
2310                    try {
2311                            Set<Long> rolePKSet = SetUtil.fromArray(rolePKs);
2312    
2313                            List<com.liferay.portal.model.Role> roles = getRoles(pk);
2314    
2315                            for (com.liferay.portal.model.Role role : roles) {
2316                                    if (!rolePKSet.remove(role.getPrimaryKey())) {
2317                                            removeRole.remove(pk, role.getPrimaryKey());
2318                                    }
2319                            }
2320    
2321                            for (Long rolePK : rolePKSet) {
2322                                    addRole.add(pk, rolePK);
2323                            }
2324                    }
2325                    catch (Exception e) {
2326                            throw processException(e);
2327                    }
2328                    finally {
2329                            FinderCacheUtil.clearCache(PermissionModelImpl.MAPPING_TABLE_ROLES_PERMISSIONS_NAME);
2330                    }
2331            }
2332    
2333            /**
2334             * Sets the roles associated with the permission, removing and adding associations as necessary. Also notifies the appropriate model listeners and clears the mapping table finder cache.
2335             *
2336             * @param pk the primary key of the permission
2337             * @param roles the roles to be associated with the permission
2338             * @throws SystemException if a system exception occurred
2339             */
2340            public void setRoles(long pk, List<com.liferay.portal.model.Role> roles)
2341                    throws SystemException {
2342                    try {
2343                            long[] rolePKs = new long[roles.size()];
2344    
2345                            for (int i = 0; i < roles.size(); i++) {
2346                                    com.liferay.portal.model.Role role = roles.get(i);
2347    
2348                                    rolePKs[i] = role.getPrimaryKey();
2349                            }
2350    
2351                            setRoles(pk, rolePKs);
2352                    }
2353                    catch (Exception e) {
2354                            throw processException(e);
2355                    }
2356                    finally {
2357                            FinderCacheUtil.clearCache(PermissionModelImpl.MAPPING_TABLE_ROLES_PERMISSIONS_NAME);
2358                    }
2359            }
2360    
2361            /**
2362             * Returns all the users associated with the permission.
2363             *
2364             * @param pk the primary key of the permission
2365             * @return the users associated with the permission
2366             * @throws SystemException if a system exception occurred
2367             */
2368            public List<com.liferay.portal.model.User> getUsers(long pk)
2369                    throws SystemException {
2370                    return getUsers(pk, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
2371            }
2372    
2373            /**
2374             * Returns a range of all the users associated with the permission.
2375             *
2376             * <p>
2377             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
2378             * </p>
2379             *
2380             * @param pk the primary key of the permission
2381             * @param start the lower bound of the range of permissions
2382             * @param end the upper bound of the range of permissions (not inclusive)
2383             * @return the range of users associated with the permission
2384             * @throws SystemException if a system exception occurred
2385             */
2386            public List<com.liferay.portal.model.User> getUsers(long pk, int start,
2387                    int end) throws SystemException {
2388                    return getUsers(pk, start, end, null);
2389            }
2390    
2391            public static final FinderPath FINDER_PATH_GET_USERS = new FinderPath(com.liferay.portal.model.impl.UserModelImpl.ENTITY_CACHE_ENABLED,
2392                            PermissionModelImpl.FINDER_CACHE_ENABLED_USERS_PERMISSIONS,
2393                            com.liferay.portal.model.impl.UserImpl.class,
2394                            PermissionModelImpl.MAPPING_TABLE_USERS_PERMISSIONS_NAME,
2395                            "getUsers",
2396                            new String[] {
2397                                    Long.class.getName(), "java.lang.Integer", "java.lang.Integer",
2398                                    "com.liferay.portal.kernel.util.OrderByComparator"
2399                            });
2400    
2401            static {
2402                    FINDER_PATH_GET_USERS.setCacheKeyGeneratorCacheName(null);
2403            }
2404    
2405            /**
2406             * Returns an ordered range of all the users associated with the permission.
2407             *
2408             * <p>
2409             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS} will return the full result set.
2410             * </p>
2411             *
2412             * @param pk the primary key of the permission
2413             * @param start the lower bound of the range of permissions
2414             * @param end the upper bound of the range of permissions (not inclusive)
2415             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
2416             * @return the ordered range of users associated with the permission
2417             * @throws SystemException if a system exception occurred
2418             */
2419            public List<com.liferay.portal.model.User> getUsers(long pk, int start,
2420                    int end, OrderByComparator orderByComparator) throws SystemException {
2421                    Object[] finderArgs = new Object[] { pk, start, end, orderByComparator };
2422    
2423                    List<com.liferay.portal.model.User> list = (List<com.liferay.portal.model.User>)FinderCacheUtil.getResult(FINDER_PATH_GET_USERS,
2424                                    finderArgs, this);
2425    
2426                    if (list == null) {
2427                            Session session = null;
2428    
2429                            try {
2430                                    session = openSession();
2431    
2432                                    String sql = null;
2433    
2434                                    if (orderByComparator != null) {
2435                                            sql = _SQL_GETUSERS.concat(ORDER_BY_CLAUSE)
2436                                                                               .concat(orderByComparator.getOrderBy());
2437                                    }
2438                                    else {
2439                                            sql = _SQL_GETUSERS;
2440                                    }
2441    
2442                                    SQLQuery q = session.createSQLQuery(sql);
2443    
2444                                    q.addEntity("User_",
2445                                            com.liferay.portal.model.impl.UserImpl.class);
2446    
2447                                    QueryPos qPos = QueryPos.getInstance(q);
2448    
2449                                    qPos.add(pk);
2450    
2451                                    list = (List<com.liferay.portal.model.User>)QueryUtil.list(q,
2452                                                    getDialect(), start, end);
2453                            }
2454                            catch (Exception e) {
2455                                    throw processException(e);
2456                            }
2457                            finally {
2458                                    if (list == null) {
2459                                            FinderCacheUtil.removeResult(FINDER_PATH_GET_USERS,
2460                                                    finderArgs);
2461                                    }
2462                                    else {
2463                                            userPersistence.cacheResult(list);
2464    
2465                                            FinderCacheUtil.putResult(FINDER_PATH_GET_USERS,
2466                                                    finderArgs, list);
2467                                    }
2468    
2469                                    closeSession(session);
2470                            }
2471                    }
2472    
2473                    return list;
2474            }
2475    
2476            public static final FinderPath FINDER_PATH_GET_USERS_SIZE = new FinderPath(com.liferay.portal.model.impl.UserModelImpl.ENTITY_CACHE_ENABLED,
2477                            PermissionModelImpl.FINDER_CACHE_ENABLED_USERS_PERMISSIONS,
2478                            Long.class,
2479                            PermissionModelImpl.MAPPING_TABLE_USERS_PERMISSIONS_NAME,
2480                            "getUsersSize", new String[] { Long.class.getName() });
2481    
2482            static {
2483                    FINDER_PATH_GET_USERS_SIZE.setCacheKeyGeneratorCacheName(null);
2484            }
2485    
2486            /**
2487             * Returns the number of users associated with the permission.
2488             *
2489             * @param pk the primary key of the permission
2490             * @return the number of users associated with the permission
2491             * @throws SystemException if a system exception occurred
2492             */
2493            public int getUsersSize(long pk) throws SystemException {
2494                    Object[] finderArgs = new Object[] { pk };
2495    
2496                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_GET_USERS_SIZE,
2497                                    finderArgs, this);
2498    
2499                    if (count == null) {
2500                            Session session = null;
2501    
2502                            try {
2503                                    session = openSession();
2504    
2505                                    SQLQuery q = session.createSQLQuery(_SQL_GETUSERSSIZE);
2506    
2507                                    q.addScalar(COUNT_COLUMN_NAME,
2508                                            com.liferay.portal.kernel.dao.orm.Type.LONG);
2509    
2510                                    QueryPos qPos = QueryPos.getInstance(q);
2511    
2512                                    qPos.add(pk);
2513    
2514                                    count = (Long)q.uniqueResult();
2515                            }
2516                            catch (Exception e) {
2517                                    throw processException(e);
2518                            }
2519                            finally {
2520                                    if (count == null) {
2521                                            count = Long.valueOf(0);
2522                                    }
2523    
2524                                    FinderCacheUtil.putResult(FINDER_PATH_GET_USERS_SIZE,
2525                                            finderArgs, count);
2526    
2527                                    closeSession(session);
2528                            }
2529                    }
2530    
2531                    return count.intValue();
2532            }
2533    
2534            public static final FinderPath FINDER_PATH_CONTAINS_USER = new FinderPath(com.liferay.portal.model.impl.UserModelImpl.ENTITY_CACHE_ENABLED,
2535                            PermissionModelImpl.FINDER_CACHE_ENABLED_USERS_PERMISSIONS,
2536                            Boolean.class,
2537                            PermissionModelImpl.MAPPING_TABLE_USERS_PERMISSIONS_NAME,
2538                            "containsUser",
2539                            new String[] { Long.class.getName(), Long.class.getName() });
2540    
2541            /**
2542             * Returns <code>true</code> if the user is associated with the permission.
2543             *
2544             * @param pk the primary key of the permission
2545             * @param userPK the primary key of the user
2546             * @return <code>true</code> if the user is associated with the permission; <code>false</code> otherwise
2547             * @throws SystemException if a system exception occurred
2548             */
2549            public boolean containsUser(long pk, long userPK) throws SystemException {
2550                    Object[] finderArgs = new Object[] { pk, userPK };
2551    
2552                    Boolean value = (Boolean)FinderCacheUtil.getResult(FINDER_PATH_CONTAINS_USER,
2553                                    finderArgs, this);
2554    
2555                    if (value == null) {
2556                            try {
2557                                    value = Boolean.valueOf(containsUser.contains(pk, userPK));
2558                            }
2559                            catch (Exception e) {
2560                                    throw processException(e);
2561                            }
2562                            finally {
2563                                    if (value == null) {
2564                                            value = Boolean.FALSE;
2565                                    }
2566    
2567                                    FinderCacheUtil.putResult(FINDER_PATH_CONTAINS_USER,
2568                                            finderArgs, value);
2569                            }
2570                    }
2571    
2572                    return value.booleanValue();
2573            }
2574    
2575            /**
2576             * Returns <code>true</code> if the permission has any users associated with it.
2577             *
2578             * @param pk the primary key of the permission to check for associations with users
2579             * @return <code>true</code> if the permission has any users associated with it; <code>false</code> otherwise
2580             * @throws SystemException if a system exception occurred
2581             */
2582            public boolean containsUsers(long pk) throws SystemException {
2583                    if (getUsersSize(pk) > 0) {
2584                            return true;
2585                    }
2586                    else {
2587                            return false;
2588                    }
2589            }
2590    
2591            /**
2592             * Adds an association between the permission and the user. Also notifies the appropriate model listeners and clears the mapping table finder cache.
2593             *
2594             * @param pk the primary key of the permission
2595             * @param userPK the primary key of the user
2596             * @throws SystemException if a system exception occurred
2597             */
2598            public void addUser(long pk, long userPK) throws SystemException {
2599                    try {
2600                            addUser.add(pk, userPK);
2601                    }
2602                    catch (Exception e) {
2603                            throw processException(e);
2604                    }
2605                    finally {
2606                            FinderCacheUtil.clearCache(PermissionModelImpl.MAPPING_TABLE_USERS_PERMISSIONS_NAME);
2607                    }
2608            }
2609    
2610            /**
2611             * Adds an association between the permission and the user. Also notifies the appropriate model listeners and clears the mapping table finder cache.
2612             *
2613             * @param pk the primary key of the permission
2614             * @param user the user
2615             * @throws SystemException if a system exception occurred
2616             */
2617            public void addUser(long pk, com.liferay.portal.model.User user)
2618                    throws SystemException {
2619                    try {
2620                            addUser.add(pk, user.getPrimaryKey());
2621                    }
2622                    catch (Exception e) {
2623                            throw processException(e);
2624                    }
2625                    finally {
2626                            FinderCacheUtil.clearCache(PermissionModelImpl.MAPPING_TABLE_USERS_PERMISSIONS_NAME);
2627                    }
2628            }
2629    
2630            /**
2631             * Adds an association between the permission and the users. Also notifies the appropriate model listeners and clears the mapping table finder cache.
2632             *
2633             * @param pk the primary key of the permission
2634             * @param userPKs the primary keys of the users
2635             * @throws SystemException if a system exception occurred
2636             */
2637            public void addUsers(long pk, long[] userPKs) throws SystemException {
2638                    try {
2639                            for (long userPK : userPKs) {
2640                                    addUser.add(pk, userPK);
2641                            }
2642                    }
2643                    catch (Exception e) {
2644                            throw processException(e);
2645                    }
2646                    finally {
2647                            FinderCacheUtil.clearCache(PermissionModelImpl.MAPPING_TABLE_USERS_PERMISSIONS_NAME);
2648                    }
2649            }
2650    
2651            /**
2652             * Adds an association between the permission and the users. Also notifies the appropriate model listeners and clears the mapping table finder cache.
2653             *
2654             * @param pk the primary key of the permission
2655             * @param users the users
2656             * @throws SystemException if a system exception occurred
2657             */
2658            public void addUsers(long pk, List<com.liferay.portal.model.User> users)
2659                    throws SystemException {
2660                    try {
2661                            for (com.liferay.portal.model.User user : users) {
2662                                    addUser.add(pk, user.getPrimaryKey());
2663                            }
2664                    }
2665                    catch (Exception e) {
2666                            throw processException(e);
2667                    }
2668                    finally {
2669                            FinderCacheUtil.clearCache(PermissionModelImpl.MAPPING_TABLE_USERS_PERMISSIONS_NAME);
2670                    }
2671            }
2672    
2673            /**
2674             * Clears all associations between the permission and its users. Also notifies the appropriate model listeners and clears the mapping table finder cache.
2675             *
2676             * @param pk the primary key of the permission to clear the associated users from
2677             * @throws SystemException if a system exception occurred
2678             */
2679            public void clearUsers(long pk) throws SystemException {
2680                    try {
2681                            clearUsers.clear(pk);
2682                    }
2683                    catch (Exception e) {
2684                            throw processException(e);
2685                    }
2686                    finally {
2687                            FinderCacheUtil.clearCache(PermissionModelImpl.MAPPING_TABLE_USERS_PERMISSIONS_NAME);
2688                    }
2689            }
2690    
2691            /**
2692             * Removes the association between the permission and the user. Also notifies the appropriate model listeners and clears the mapping table finder cache.
2693             *
2694             * @param pk the primary key of the permission
2695             * @param userPK the primary key of the user
2696             * @throws SystemException if a system exception occurred
2697             */
2698            public void removeUser(long pk, long userPK) throws SystemException {
2699                    try {
2700                            removeUser.remove(pk, userPK);
2701                    }
2702                    catch (Exception e) {
2703                            throw processException(e);
2704                    }
2705                    finally {
2706                            FinderCacheUtil.clearCache(PermissionModelImpl.MAPPING_TABLE_USERS_PERMISSIONS_NAME);
2707                    }
2708            }
2709    
2710            /**
2711             * Removes the association between the permission and the user. Also notifies the appropriate model listeners and clears the mapping table finder cache.
2712             *
2713             * @param pk the primary key of the permission
2714             * @param user the user
2715             * @throws SystemException if a system exception occurred
2716             */
2717            public void removeUser(long pk, com.liferay.portal.model.User user)
2718                    throws SystemException {
2719                    try {
2720                            removeUser.remove(pk, user.getPrimaryKey());
2721                    }
2722                    catch (Exception e) {
2723                            throw processException(e);
2724                    }
2725                    finally {
2726                            FinderCacheUtil.clearCache(PermissionModelImpl.MAPPING_TABLE_USERS_PERMISSIONS_NAME);
2727                    }
2728            }
2729    
2730            /**
2731             * Removes the association between the permission and the users. Also notifies the appropriate model listeners and clears the mapping table finder cache.
2732             *
2733             * @param pk the primary key of the permission
2734             * @param userPKs the primary keys of the users
2735             * @throws SystemException if a system exception occurred
2736             */
2737            public void removeUsers(long pk, long[] userPKs) throws SystemException {
2738                    try {
2739                            for (long userPK : userPKs) {
2740                                    removeUser.remove(pk, userPK);
2741                            }
2742                    }
2743                    catch (Exception e) {
2744                            throw processException(e);
2745                    }
2746                    finally {
2747                            FinderCacheUtil.clearCache(PermissionModelImpl.MAPPING_TABLE_USERS_PERMISSIONS_NAME);
2748                    }
2749            }
2750    
2751            /**
2752             * Removes the association between the permission and the users. Also notifies the appropriate model listeners and clears the mapping table finder cache.
2753             *
2754             * @param pk the primary key of the permission
2755             * @param users the users
2756             * @throws SystemException if a system exception occurred
2757             */
2758            public void removeUsers(long pk, List<com.liferay.portal.model.User> users)
2759                    throws SystemException {
2760                    try {
2761                            for (com.liferay.portal.model.User user : users) {
2762                                    removeUser.remove(pk, user.getPrimaryKey());
2763                            }
2764                    }
2765                    catch (Exception e) {
2766                            throw processException(e);
2767                    }
2768                    finally {
2769                            FinderCacheUtil.clearCache(PermissionModelImpl.MAPPING_TABLE_USERS_PERMISSIONS_NAME);
2770                    }
2771            }
2772    
2773            /**
2774             * Sets the users associated with the permission, removing and adding associations as necessary. Also notifies the appropriate model listeners and clears the mapping table finder cache.
2775             *
2776             * @param pk the primary key of the permission
2777             * @param userPKs the primary keys of the users to be associated with the permission
2778             * @throws SystemException if a system exception occurred
2779             */
2780            public void setUsers(long pk, long[] userPKs) throws SystemException {
2781                    try {
2782                            Set<Long> userPKSet = SetUtil.fromArray(userPKs);
2783    
2784                            List<com.liferay.portal.model.User> users = getUsers(pk);
2785    
2786                            for (com.liferay.portal.model.User user : users) {
2787                                    if (!userPKSet.remove(user.getPrimaryKey())) {
2788                                            removeUser.remove(pk, user.getPrimaryKey());
2789                                    }
2790                            }
2791    
2792                            for (Long userPK : userPKSet) {
2793                                    addUser.add(pk, userPK);
2794                            }
2795                    }
2796                    catch (Exception e) {
2797                            throw processException(e);
2798                    }
2799                    finally {
2800                            FinderCacheUtil.clearCache(PermissionModelImpl.MAPPING_TABLE_USERS_PERMISSIONS_NAME);
2801                    }
2802            }
2803    
2804            /**
2805             * Sets the users associated with the permission, removing and adding associations as necessary. Also notifies the appropriate model listeners and clears the mapping table finder cache.
2806             *
2807             * @param pk the primary key of the permission
2808             * @param users the users to be associated with the permission
2809             * @throws SystemException if a system exception occurred
2810             */
2811            public void setUsers(long pk, List<com.liferay.portal.model.User> users)
2812                    throws SystemException {
2813                    try {
2814                            long[] userPKs = new long[users.size()];
2815    
2816                            for (int i = 0; i < users.size(); i++) {
2817                                    com.liferay.portal.model.User user = users.get(i);
2818    
2819                                    userPKs[i] = user.getPrimaryKey();
2820                            }
2821    
2822                            setUsers(pk, userPKs);
2823                    }
2824                    catch (Exception e) {
2825                            throw processException(e);
2826                    }
2827                    finally {
2828                            FinderCacheUtil.clearCache(PermissionModelImpl.MAPPING_TABLE_USERS_PERMISSIONS_NAME);
2829                    }
2830            }
2831    
2832            /**
2833             * Initializes the permission persistence.
2834             */
2835            public void afterPropertiesSet() {
2836                    String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
2837                                            com.liferay.portal.util.PropsUtil.get(
2838                                                    "value.object.listener.com.liferay.portal.model.Permission")));
2839    
2840                    if (listenerClassNames.length > 0) {
2841                            try {
2842                                    List<ModelListener<Permission>> listenersList = new ArrayList<ModelListener<Permission>>();
2843    
2844                                    for (String listenerClassName : listenerClassNames) {
2845                                            Class<?> clazz = getClass();
2846    
2847                                            listenersList.add((ModelListener<Permission>)InstanceFactory.newInstance(
2848                                                            clazz.getClassLoader(), listenerClassName));
2849                                    }
2850    
2851                                    listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
2852                            }
2853                            catch (Exception e) {
2854                                    _log.error(e);
2855                            }
2856                    }
2857    
2858                    containsGroup = new ContainsGroup();
2859    
2860                    addGroup = new AddGroup();
2861                    clearGroups = new ClearGroups();
2862                    removeGroup = new RemoveGroup();
2863    
2864                    containsRole = new ContainsRole();
2865    
2866                    addRole = new AddRole();
2867                    clearRoles = new ClearRoles();
2868                    removeRole = new RemoveRole();
2869    
2870                    containsUser = new ContainsUser();
2871    
2872                    addUser = new AddUser();
2873                    clearUsers = new ClearUsers();
2874                    removeUser = new RemoveUser();
2875            }
2876    
2877            public void destroy() {
2878                    EntityCacheUtil.removeCache(PermissionImpl.class.getName());
2879                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
2880                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
2881            }
2882    
2883            @BeanReference(type = AccountPersistence.class)
2884            protected AccountPersistence accountPersistence;
2885            @BeanReference(type = AddressPersistence.class)
2886            protected AddressPersistence addressPersistence;
2887            @BeanReference(type = BrowserTrackerPersistence.class)
2888            protected BrowserTrackerPersistence browserTrackerPersistence;
2889            @BeanReference(type = ClassNamePersistence.class)
2890            protected ClassNamePersistence classNamePersistence;
2891            @BeanReference(type = ClusterGroupPersistence.class)
2892            protected ClusterGroupPersistence clusterGroupPersistence;
2893            @BeanReference(type = CompanyPersistence.class)
2894            protected CompanyPersistence companyPersistence;
2895            @BeanReference(type = ContactPersistence.class)
2896            protected ContactPersistence contactPersistence;
2897            @BeanReference(type = CountryPersistence.class)
2898            protected CountryPersistence countryPersistence;
2899            @BeanReference(type = EmailAddressPersistence.class)
2900            protected EmailAddressPersistence emailAddressPersistence;
2901            @BeanReference(type = GroupPersistence.class)
2902            protected GroupPersistence groupPersistence;
2903            @BeanReference(type = ImagePersistence.class)
2904            protected ImagePersistence imagePersistence;
2905            @BeanReference(type = LayoutPersistence.class)
2906            protected LayoutPersistence layoutPersistence;
2907            @BeanReference(type = LayoutBranchPersistence.class)
2908            protected LayoutBranchPersistence layoutBranchPersistence;
2909            @BeanReference(type = LayoutPrototypePersistence.class)
2910            protected LayoutPrototypePersistence layoutPrototypePersistence;
2911            @BeanReference(type = LayoutRevisionPersistence.class)
2912            protected LayoutRevisionPersistence layoutRevisionPersistence;
2913            @BeanReference(type = LayoutSetPersistence.class)
2914            protected LayoutSetPersistence layoutSetPersistence;
2915            @BeanReference(type = LayoutSetBranchPersistence.class)
2916            protected LayoutSetBranchPersistence layoutSetBranchPersistence;
2917            @BeanReference(type = LayoutSetPrototypePersistence.class)
2918            protected LayoutSetPrototypePersistence layoutSetPrototypePersistence;
2919            @BeanReference(type = ListTypePersistence.class)
2920            protected ListTypePersistence listTypePersistence;
2921            @BeanReference(type = LockPersistence.class)
2922            protected LockPersistence lockPersistence;
2923            @BeanReference(type = MembershipRequestPersistence.class)
2924            protected MembershipRequestPersistence membershipRequestPersistence;
2925            @BeanReference(type = OrganizationPersistence.class)
2926            protected OrganizationPersistence organizationPersistence;
2927            @BeanReference(type = OrgGroupPermissionPersistence.class)
2928            protected OrgGroupPermissionPersistence orgGroupPermissionPersistence;
2929            @BeanReference(type = OrgGroupRolePersistence.class)
2930            protected OrgGroupRolePersistence orgGroupRolePersistence;
2931            @BeanReference(type = OrgLaborPersistence.class)
2932            protected OrgLaborPersistence orgLaborPersistence;
2933            @BeanReference(type = PasswordPolicyPersistence.class)
2934            protected PasswordPolicyPersistence passwordPolicyPersistence;
2935            @BeanReference(type = PasswordPolicyRelPersistence.class)
2936            protected PasswordPolicyRelPersistence passwordPolicyRelPersistence;
2937            @BeanReference(type = PasswordTrackerPersistence.class)
2938            protected PasswordTrackerPersistence passwordTrackerPersistence;
2939            @BeanReference(type = PermissionPersistence.class)
2940            protected PermissionPersistence permissionPersistence;
2941            @BeanReference(type = PhonePersistence.class)
2942            protected PhonePersistence phonePersistence;
2943            @BeanReference(type = PluginSettingPersistence.class)
2944            protected PluginSettingPersistence pluginSettingPersistence;
2945            @BeanReference(type = PortalPreferencesPersistence.class)
2946            protected PortalPreferencesPersistence portalPreferencesPersistence;
2947            @BeanReference(type = PortletPersistence.class)
2948            protected PortletPersistence portletPersistence;
2949            @BeanReference(type = PortletItemPersistence.class)
2950            protected PortletItemPersistence portletItemPersistence;
2951            @BeanReference(type = PortletPreferencesPersistence.class)
2952            protected PortletPreferencesPersistence portletPreferencesPersistence;
2953            @BeanReference(type = RegionPersistence.class)
2954            protected RegionPersistence regionPersistence;
2955            @BeanReference(type = ReleasePersistence.class)
2956            protected ReleasePersistence releasePersistence;
2957            @BeanReference(type = RepositoryPersistence.class)
2958            protected RepositoryPersistence repositoryPersistence;
2959            @BeanReference(type = RepositoryEntryPersistence.class)
2960            protected RepositoryEntryPersistence repositoryEntryPersistence;
2961            @BeanReference(type = ResourcePersistence.class)
2962            protected ResourcePersistence resourcePersistence;
2963            @BeanReference(type = ResourceActionPersistence.class)
2964            protected ResourceActionPersistence resourceActionPersistence;
2965            @BeanReference(type = ResourceBlockPersistence.class)
2966            protected ResourceBlockPersistence resourceBlockPersistence;
2967            @BeanReference(type = ResourceBlockPermissionPersistence.class)
2968            protected ResourceBlockPermissionPersistence resourceBlockPermissionPersistence;
2969            @BeanReference(type = ResourceCodePersistence.class)
2970            protected ResourceCodePersistence resourceCodePersistence;
2971            @BeanReference(type = ResourcePermissionPersistence.class)
2972            protected ResourcePermissionPersistence resourcePermissionPersistence;
2973            @BeanReference(type = ResourceTypePermissionPersistence.class)
2974            protected ResourceTypePermissionPersistence resourceTypePermissionPersistence;
2975            @BeanReference(type = RolePersistence.class)
2976            protected RolePersistence rolePersistence;
2977            @BeanReference(type = ServiceComponentPersistence.class)
2978            protected ServiceComponentPersistence serviceComponentPersistence;
2979            @BeanReference(type = ShardPersistence.class)
2980            protected ShardPersistence shardPersistence;
2981            @BeanReference(type = SubscriptionPersistence.class)
2982            protected SubscriptionPersistence subscriptionPersistence;
2983            @BeanReference(type = TeamPersistence.class)
2984            protected TeamPersistence teamPersistence;
2985            @BeanReference(type = TicketPersistence.class)
2986            protected TicketPersistence ticketPersistence;
2987            @BeanReference(type = UserPersistence.class)
2988            protected UserPersistence userPersistence;
2989            @BeanReference(type = UserGroupPersistence.class)
2990            protected UserGroupPersistence userGroupPersistence;
2991            @BeanReference(type = UserGroupGroupRolePersistence.class)
2992            protected UserGroupGroupRolePersistence userGroupGroupRolePersistence;
2993            @BeanReference(type = UserGroupRolePersistence.class)
2994            protected UserGroupRolePersistence userGroupRolePersistence;
2995            @BeanReference(type = UserIdMapperPersistence.class)
2996            protected UserIdMapperPersistence userIdMapperPersistence;
2997            @BeanReference(type = UserNotificationEventPersistence.class)
2998            protected UserNotificationEventPersistence userNotificationEventPersistence;
2999            @BeanReference(type = UserTrackerPersistence.class)
3000            protected UserTrackerPersistence userTrackerPersistence;
3001            @BeanReference(type = UserTrackerPathPersistence.class)
3002            protected UserTrackerPathPersistence userTrackerPathPersistence;
3003            @BeanReference(type = VirtualHostPersistence.class)
3004            protected VirtualHostPersistence virtualHostPersistence;
3005            @BeanReference(type = WebDAVPropsPersistence.class)
3006            protected WebDAVPropsPersistence webDAVPropsPersistence;
3007            @BeanReference(type = WebsitePersistence.class)
3008            protected WebsitePersistence websitePersistence;
3009            @BeanReference(type = WorkflowDefinitionLinkPersistence.class)
3010            protected WorkflowDefinitionLinkPersistence workflowDefinitionLinkPersistence;
3011            @BeanReference(type = WorkflowInstanceLinkPersistence.class)
3012            protected WorkflowInstanceLinkPersistence workflowInstanceLinkPersistence;
3013            protected ContainsGroup containsGroup;
3014            protected AddGroup addGroup;
3015            protected ClearGroups clearGroups;
3016            protected RemoveGroup removeGroup;
3017            protected ContainsRole containsRole;
3018            protected AddRole addRole;
3019            protected ClearRoles clearRoles;
3020            protected RemoveRole removeRole;
3021            protected ContainsUser containsUser;
3022            protected AddUser addUser;
3023            protected ClearUsers clearUsers;
3024            protected RemoveUser removeUser;
3025    
3026            protected class ContainsGroup {
3027                    protected ContainsGroup() {
3028                            _mappingSqlQuery = MappingSqlQueryFactoryUtil.getMappingSqlQuery(getDataSource(),
3029                                            _SQL_CONTAINSGROUP,
3030                                            new int[] { java.sql.Types.BIGINT, java.sql.Types.BIGINT },
3031                                            RowMapper.COUNT);
3032                    }
3033    
3034                    protected boolean contains(long permissionId, long groupId) {
3035                            List<Integer> results = _mappingSqlQuery.execute(new Object[] {
3036                                                    new Long(permissionId), new Long(groupId)
3037                                            });
3038    
3039                            if (results.size() > 0) {
3040                                    Integer count = results.get(0);
3041    
3042                                    if (count.intValue() > 0) {
3043                                            return true;
3044                                    }
3045                            }
3046    
3047                            return false;
3048                    }
3049    
3050                    private MappingSqlQuery<Integer> _mappingSqlQuery;
3051            }
3052    
3053            protected class AddGroup {
3054                    protected AddGroup() {
3055                            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
3056                                            "INSERT INTO Groups_Permissions (permissionId, groupId) VALUES (?, ?)",
3057                                            new int[] { java.sql.Types.BIGINT, java.sql.Types.BIGINT });
3058                    }
3059    
3060                    protected void add(long permissionId, long groupId)
3061                            throws SystemException {
3062                            if (!containsGroup.contains(permissionId, groupId)) {
3063                                    ModelListener<com.liferay.portal.model.Group>[] groupListeners = groupPersistence.getListeners();
3064    
3065                                    for (ModelListener<Permission> listener : listeners) {
3066                                            listener.onBeforeAddAssociation(permissionId,
3067                                                    com.liferay.portal.model.Group.class.getName(), groupId);
3068                                    }
3069    
3070                                    for (ModelListener<com.liferay.portal.model.Group> listener : groupListeners) {
3071                                            listener.onBeforeAddAssociation(groupId,
3072                                                    Permission.class.getName(), permissionId);
3073                                    }
3074    
3075                                    _sqlUpdate.update(new Object[] {
3076                                                    new Long(permissionId), new Long(groupId)
3077                                            });
3078    
3079                                    for (ModelListener<Permission> listener : listeners) {
3080                                            listener.onAfterAddAssociation(permissionId,
3081                                                    com.liferay.portal.model.Group.class.getName(), groupId);
3082                                    }
3083    
3084                                    for (ModelListener<com.liferay.portal.model.Group> listener : groupListeners) {
3085                                            listener.onAfterAddAssociation(groupId,
3086                                                    Permission.class.getName(), permissionId);
3087                                    }
3088                            }
3089                    }
3090    
3091                    private SqlUpdate _sqlUpdate;
3092            }
3093    
3094            protected class ClearGroups {
3095                    protected ClearGroups() {
3096                            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
3097                                            "DELETE FROM Groups_Permissions WHERE permissionId = ?",
3098                                            new int[] { java.sql.Types.BIGINT });
3099                    }
3100    
3101                    protected void clear(long permissionId) throws SystemException {
3102                            ModelListener<com.liferay.portal.model.Group>[] groupListeners = groupPersistence.getListeners();
3103    
3104                            List<com.liferay.portal.model.Group> groups = null;
3105    
3106                            if ((listeners.length > 0) || (groupListeners.length > 0)) {
3107                                    groups = getGroups(permissionId);
3108    
3109                                    for (com.liferay.portal.model.Group group : groups) {
3110                                            for (ModelListener<Permission> listener : listeners) {
3111                                                    listener.onBeforeRemoveAssociation(permissionId,
3112                                                            com.liferay.portal.model.Group.class.getName(),
3113                                                            group.getPrimaryKey());
3114                                            }
3115    
3116                                            for (ModelListener<com.liferay.portal.model.Group> listener : groupListeners) {
3117                                                    listener.onBeforeRemoveAssociation(group.getPrimaryKey(),
3118                                                            Permission.class.getName(), permissionId);
3119                                            }
3120                                    }
3121                            }
3122    
3123                            _sqlUpdate.update(new Object[] { new Long(permissionId) });
3124    
3125                            if ((listeners.length > 0) || (groupListeners.length > 0)) {
3126                                    for (com.liferay.portal.model.Group group : groups) {
3127                                            for (ModelListener<Permission> listener : listeners) {
3128                                                    listener.onAfterRemoveAssociation(permissionId,
3129                                                            com.liferay.portal.model.Group.class.getName(),
3130                                                            group.getPrimaryKey());
3131                                            }
3132    
3133                                            for (ModelListener<com.liferay.portal.model.Group> listener : groupListeners) {
3134                                                    listener.onAfterRemoveAssociation(group.getPrimaryKey(),
3135                                                            Permission.class.getName(), permissionId);
3136                                            }
3137                                    }
3138                            }
3139                    }
3140    
3141                    private SqlUpdate _sqlUpdate;
3142            }
3143    
3144            protected class RemoveGroup {
3145                    protected RemoveGroup() {
3146                            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
3147                                            "DELETE FROM Groups_Permissions WHERE permissionId = ? AND groupId = ?",
3148                                            new int[] { java.sql.Types.BIGINT, java.sql.Types.BIGINT });
3149                    }
3150    
3151                    protected void remove(long permissionId, long groupId)
3152                            throws SystemException {
3153                            if (containsGroup.contains(permissionId, groupId)) {
3154                                    ModelListener<com.liferay.portal.model.Group>[] groupListeners = groupPersistence.getListeners();
3155    
3156                                    for (ModelListener<Permission> listener : listeners) {
3157                                            listener.onBeforeRemoveAssociation(permissionId,
3158                                                    com.liferay.portal.model.Group.class.getName(), groupId);
3159                                    }
3160    
3161                                    for (ModelListener<com.liferay.portal.model.Group> listener : groupListeners) {
3162                                            listener.onBeforeRemoveAssociation(groupId,
3163                                                    Permission.class.getName(), permissionId);
3164                                    }
3165    
3166                                    _sqlUpdate.update(new Object[] {
3167                                                    new Long(permissionId), new Long(groupId)
3168                                            });
3169    
3170                                    for (ModelListener<Permission> listener : listeners) {
3171                                            listener.onAfterRemoveAssociation(permissionId,
3172                                                    com.liferay.portal.model.Group.class.getName(), groupId);
3173                                    }
3174    
3175                                    for (ModelListener<com.liferay.portal.model.Group> listener : groupListeners) {
3176                                            listener.onAfterRemoveAssociation(groupId,
3177                                                    Permission.class.getName(), permissionId);
3178                                    }
3179                            }
3180                    }
3181    
3182                    private SqlUpdate _sqlUpdate;
3183            }
3184    
3185            protected class ContainsRole {
3186                    protected ContainsRole() {
3187                            _mappingSqlQuery = MappingSqlQueryFactoryUtil.getMappingSqlQuery(getDataSource(),
3188                                            _SQL_CONTAINSROLE,
3189                                            new int[] { java.sql.Types.BIGINT, java.sql.Types.BIGINT },
3190                                            RowMapper.COUNT);
3191                    }
3192    
3193                    protected boolean contains(long permissionId, long roleId) {
3194                            List<Integer> results = _mappingSqlQuery.execute(new Object[] {
3195                                                    new Long(permissionId), new Long(roleId)
3196                                            });
3197    
3198                            if (results.size() > 0) {
3199                                    Integer count = results.get(0);
3200    
3201                                    if (count.intValue() > 0) {
3202                                            return true;
3203                                    }
3204                            }
3205    
3206                            return false;
3207                    }
3208    
3209                    private MappingSqlQuery<Integer> _mappingSqlQuery;
3210            }
3211    
3212            protected class AddRole {
3213                    protected AddRole() {
3214                            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
3215                                            "INSERT INTO Roles_Permissions (permissionId, roleId) VALUES (?, ?)",
3216                                            new int[] { java.sql.Types.BIGINT, java.sql.Types.BIGINT });
3217                    }
3218    
3219                    protected void add(long permissionId, long roleId)
3220                            throws SystemException {
3221                            if (!containsRole.contains(permissionId, roleId)) {
3222                                    ModelListener<com.liferay.portal.model.Role>[] roleListeners = rolePersistence.getListeners();
3223    
3224                                    for (ModelListener<Permission> listener : listeners) {
3225                                            listener.onBeforeAddAssociation(permissionId,
3226                                                    com.liferay.portal.model.Role.class.getName(), roleId);
3227                                    }
3228    
3229                                    for (ModelListener<com.liferay.portal.model.Role> listener : roleListeners) {
3230                                            listener.onBeforeAddAssociation(roleId,
3231                                                    Permission.class.getName(), permissionId);
3232                                    }
3233    
3234                                    _sqlUpdate.update(new Object[] {
3235                                                    new Long(permissionId), new Long(roleId)
3236                                            });
3237    
3238                                    for (ModelListener<Permission> listener : listeners) {
3239                                            listener.onAfterAddAssociation(permissionId,
3240                                                    com.liferay.portal.model.Role.class.getName(), roleId);
3241                                    }
3242    
3243                                    for (ModelListener<com.liferay.portal.model.Role> listener : roleListeners) {
3244                                            listener.onAfterAddAssociation(roleId,
3245                                                    Permission.class.getName(), permissionId);
3246                                    }
3247                            }
3248                    }
3249    
3250                    private SqlUpdate _sqlUpdate;
3251            }
3252    
3253            protected class ClearRoles {
3254                    protected ClearRoles() {
3255                            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
3256                                            "DELETE FROM Roles_Permissions WHERE permissionId = ?",
3257                                            new int[] { java.sql.Types.BIGINT });
3258                    }
3259    
3260                    protected void clear(long permissionId) throws SystemException {
3261                            ModelListener<com.liferay.portal.model.Role>[] roleListeners = rolePersistence.getListeners();
3262    
3263                            List<com.liferay.portal.model.Role> roles = null;
3264    
3265                            if ((listeners.length > 0) || (roleListeners.length > 0)) {
3266                                    roles = getRoles(permissionId);
3267    
3268                                    for (com.liferay.portal.model.Role role : roles) {
3269                                            for (ModelListener<Permission> listener : listeners) {
3270                                                    listener.onBeforeRemoveAssociation(permissionId,
3271                                                            com.liferay.portal.model.Role.class.getName(),
3272                                                            role.getPrimaryKey());
3273                                            }
3274    
3275                                            for (ModelListener<com.liferay.portal.model.Role> listener : roleListeners) {
3276                                                    listener.onBeforeRemoveAssociation(role.getPrimaryKey(),
3277                                                            Permission.class.getName(), permissionId);
3278                                            }
3279                                    }
3280                            }
3281    
3282                            _sqlUpdate.update(new Object[] { new Long(permissionId) });
3283    
3284                            if ((listeners.length > 0) || (roleListeners.length > 0)) {
3285                                    for (com.liferay.portal.model.Role role : roles) {
3286                                            for (ModelListener<Permission> listener : listeners) {
3287                                                    listener.onAfterRemoveAssociation(permissionId,
3288                                                            com.liferay.portal.model.Role.class.getName(),
3289                                                            role.getPrimaryKey());
3290                                            }
3291    
3292                                            for (ModelListener<com.liferay.portal.model.Role> listener : roleListeners) {
3293                                                    listener.onAfterRemoveAssociation(role.getPrimaryKey(),
3294                                                            Permission.class.getName(), permissionId);
3295                                            }
3296                                    }
3297                            }
3298                    }
3299    
3300                    private SqlUpdate _sqlUpdate;
3301            }
3302    
3303            protected class RemoveRole {
3304                    protected RemoveRole() {
3305                            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
3306                                            "DELETE FROM Roles_Permissions WHERE permissionId = ? AND roleId = ?",
3307                                            new int[] { java.sql.Types.BIGINT, java.sql.Types.BIGINT });
3308                    }
3309    
3310                    protected void remove(long permissionId, long roleId)
3311                            throws SystemException {
3312                            if (containsRole.contains(permissionId, roleId)) {
3313                                    ModelListener<com.liferay.portal.model.Role>[] roleListeners = rolePersistence.getListeners();
3314    
3315                                    for (ModelListener<Permission> listener : listeners) {
3316                                            listener.onBeforeRemoveAssociation(permissionId,
3317                                                    com.liferay.portal.model.Role.class.getName(), roleId);
3318                                    }
3319    
3320                                    for (ModelListener<com.liferay.portal.model.Role> listener : roleListeners) {
3321                                            listener.onBeforeRemoveAssociation(roleId,
3322                                                    Permission.class.getName(), permissionId);
3323                                    }
3324    
3325                                    _sqlUpdate.update(new Object[] {
3326                                                    new Long(permissionId), new Long(roleId)
3327                                            });
3328    
3329                                    for (ModelListener<Permission> listener : listeners) {
3330                                            listener.onAfterRemoveAssociation(permissionId,
3331                                                    com.liferay.portal.model.Role.class.getName(), roleId);
3332                                    }
3333    
3334                                    for (ModelListener<com.liferay.portal.model.Role> listener : roleListeners) {
3335                                            listener.onAfterRemoveAssociation(roleId,
3336                                                    Permission.class.getName(), permissionId);
3337                                    }
3338                            }
3339                    }
3340    
3341                    private SqlUpdate _sqlUpdate;
3342            }
3343    
3344            protected class ContainsUser {
3345                    protected ContainsUser() {
3346                            _mappingSqlQuery = MappingSqlQueryFactoryUtil.getMappingSqlQuery(getDataSource(),
3347                                            _SQL_CONTAINSUSER,
3348                                            new int[] { java.sql.Types.BIGINT, java.sql.Types.BIGINT },
3349                                            RowMapper.COUNT);
3350                    }
3351    
3352                    protected boolean contains(long permissionId, long userId) {
3353                            List<Integer> results = _mappingSqlQuery.execute(new Object[] {
3354                                                    new Long(permissionId), new Long(userId)
3355                                            });
3356    
3357                            if (results.size() > 0) {
3358                                    Integer count = results.get(0);
3359    
3360                                    if (count.intValue() > 0) {
3361                                            return true;
3362                                    }
3363                            }
3364    
3365                            return false;
3366                    }
3367    
3368                    private MappingSqlQuery<Integer> _mappingSqlQuery;
3369            }
3370    
3371            protected class AddUser {
3372                    protected AddUser() {
3373                            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
3374                                            "INSERT INTO Users_Permissions (permissionId, userId) VALUES (?, ?)",
3375                                            new int[] { java.sql.Types.BIGINT, java.sql.Types.BIGINT });
3376                    }
3377    
3378                    protected void add(long permissionId, long userId)
3379                            throws SystemException {
3380                            if (!containsUser.contains(permissionId, userId)) {
3381                                    ModelListener<com.liferay.portal.model.User>[] userListeners = userPersistence.getListeners();
3382    
3383                                    for (ModelListener<Permission> listener : listeners) {
3384                                            listener.onBeforeAddAssociation(permissionId,
3385                                                    com.liferay.portal.model.User.class.getName(), userId);
3386                                    }
3387    
3388                                    for (ModelListener<com.liferay.portal.model.User> listener : userListeners) {
3389                                            listener.onBeforeAddAssociation(userId,
3390                                                    Permission.class.getName(), permissionId);
3391                                    }
3392    
3393                                    _sqlUpdate.update(new Object[] {
3394                                                    new Long(permissionId), new Long(userId)
3395                                            });
3396    
3397                                    for (ModelListener<Permission> listener : listeners) {
3398                                            listener.onAfterAddAssociation(permissionId,
3399                                                    com.liferay.portal.model.User.class.getName(), userId);
3400                                    }
3401    
3402                                    for (ModelListener<com.liferay.portal.model.User> listener : userListeners) {
3403                                            listener.onAfterAddAssociation(userId,
3404                                                    Permission.class.getName(), permissionId);
3405                                    }
3406                            }
3407                    }
3408    
3409                    private SqlUpdate _sqlUpdate;
3410            }
3411    
3412            protected class ClearUsers {
3413                    protected ClearUsers() {
3414                            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
3415                                            "DELETE FROM Users_Permissions WHERE permissionId = ?",
3416                                            new int[] { java.sql.Types.BIGINT });
3417                    }
3418    
3419                    protected void clear(long permissionId) throws SystemException {
3420                            ModelListener<com.liferay.portal.model.User>[] userListeners = userPersistence.getListeners();
3421    
3422                            List<com.liferay.portal.model.User> users = null;
3423    
3424                            if ((listeners.length > 0) || (userListeners.length > 0)) {
3425                                    users = getUsers(permissionId);
3426    
3427                                    for (com.liferay.portal.model.User user : users) {
3428                                            for (ModelListener<Permission> listener : listeners) {
3429                                                    listener.onBeforeRemoveAssociation(permissionId,
3430                                                            com.liferay.portal.model.User.class.getName(),
3431                                                            user.getPrimaryKey());
3432                                            }
3433    
3434                                            for (ModelListener<com.liferay.portal.model.User> listener : userListeners) {
3435                                                    listener.onBeforeRemoveAssociation(user.getPrimaryKey(),
3436                                                            Permission.class.getName(), permissionId);
3437                                            }
3438                                    }
3439                            }
3440    
3441                            _sqlUpdate.update(new Object[] { new Long(permissionId) });
3442    
3443                            if ((listeners.length > 0) || (userListeners.length > 0)) {
3444                                    for (com.liferay.portal.model.User user : users) {
3445                                            for (ModelListener<Permission> listener : listeners) {
3446                                                    listener.onAfterRemoveAssociation(permissionId,
3447                                                            com.liferay.portal.model.User.class.getName(),
3448                                                            user.getPrimaryKey());
3449                                            }
3450    
3451                                            for (ModelListener<com.liferay.portal.model.User> listener : userListeners) {
3452                                                    listener.onAfterRemoveAssociation(user.getPrimaryKey(),
3453                                                            Permission.class.getName(), permissionId);
3454                                            }
3455                                    }
3456                            }
3457                    }
3458    
3459                    private SqlUpdate _sqlUpdate;
3460            }
3461    
3462            protected class RemoveUser {
3463                    protected RemoveUser() {
3464                            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
3465                                            "DELETE FROM Users_Permissions WHERE permissionId = ? AND userId = ?",
3466                                            new int[] { java.sql.Types.BIGINT, java.sql.Types.BIGINT });
3467                    }
3468    
3469                    protected void remove(long permissionId, long userId)
3470                            throws SystemException {
3471                            if (containsUser.contains(permissionId, userId)) {
3472                                    ModelListener<com.liferay.portal.model.User>[] userListeners = userPersistence.getListeners();
3473    
3474                                    for (ModelListener<Permission> listener : listeners) {
3475                                            listener.onBeforeRemoveAssociation(permissionId,
3476                                                    com.liferay.portal.model.User.class.getName(), userId);
3477                                    }
3478    
3479                                    for (ModelListener<com.liferay.portal.model.User> listener : userListeners) {
3480                                            listener.onBeforeRemoveAssociation(userId,
3481                                                    Permission.class.getName(), permissionId);
3482                                    }
3483    
3484                                    _sqlUpdate.update(new Object[] {
3485                                                    new Long(permissionId), new Long(userId)
3486                                            });
3487    
3488                                    for (ModelListener<Permission> listener : listeners) {
3489                                            listener.onAfterRemoveAssociation(permissionId,
3490                                                    com.liferay.portal.model.User.class.getName(), userId);
3491                                    }
3492    
3493                                    for (ModelListener<com.liferay.portal.model.User> listener : userListeners) {
3494                                            listener.onAfterRemoveAssociation(userId,
3495                                                    Permission.class.getName(), permissionId);
3496                                    }
3497                            }
3498                    }
3499    
3500                    private SqlUpdate _sqlUpdate;
3501            }
3502    
3503            private static final String _SQL_SELECT_PERMISSION = "SELECT permission FROM Permission permission";
3504            private static final String _SQL_SELECT_PERMISSION_WHERE = "SELECT permission FROM Permission permission WHERE ";
3505            private static final String _SQL_COUNT_PERMISSION = "SELECT COUNT(permission) FROM Permission permission";
3506            private static final String _SQL_COUNT_PERMISSION_WHERE = "SELECT COUNT(permission) FROM Permission permission WHERE ";
3507            private static final String _SQL_GETGROUPS = "SELECT {Group_.*} FROM Group_ INNER JOIN Groups_Permissions ON (Groups_Permissions.groupId = Group_.groupId) WHERE (Groups_Permissions.permissionId = ?)";
3508            private static final String _SQL_GETGROUPSSIZE = "SELECT COUNT(*) AS COUNT_VALUE FROM Groups_Permissions WHERE permissionId = ?";
3509            private static final String _SQL_CONTAINSGROUP = "SELECT COUNT(*) AS COUNT_VALUE FROM Groups_Permissions WHERE permissionId = ? AND groupId = ?";
3510            private static final String _SQL_GETROLES = "SELECT {Role_.*} FROM Role_ INNER JOIN Roles_Permissions ON (Roles_Permissions.roleId = Role_.roleId) WHERE (Roles_Permissions.permissionId = ?)";
3511            private static final String _SQL_GETROLESSIZE = "SELECT COUNT(*) AS COUNT_VALUE FROM Roles_Permissions WHERE permissionId = ?";
3512            private static final String _SQL_CONTAINSROLE = "SELECT COUNT(*) AS COUNT_VALUE FROM Roles_Permissions WHERE permissionId = ? AND roleId = ?";
3513            private static final String _SQL_GETUSERS = "SELECT {User_.*} FROM User_ INNER JOIN Users_Permissions ON (Users_Permissions.userId = User_.userId) WHERE (Users_Permissions.permissionId = ?)";
3514            private static final String _SQL_GETUSERSSIZE = "SELECT COUNT(*) AS COUNT_VALUE FROM Users_Permissions WHERE permissionId = ?";
3515            private static final String _SQL_CONTAINSUSER = "SELECT COUNT(*) AS COUNT_VALUE FROM Users_Permissions WHERE permissionId = ? AND userId = ?";
3516            private static final String _FINDER_COLUMN_RESOURCEID_RESOURCEID_2 = "permission.resourceId = ?";
3517            private static final String _FINDER_COLUMN_A_R_ACTIONID_1 = "permission.actionId IS NULL AND ";
3518            private static final String _FINDER_COLUMN_A_R_ACTIONID_2 = "permission.actionId = ? AND ";
3519            private static final String _FINDER_COLUMN_A_R_ACTIONID_3 = "(permission.actionId IS NULL OR permission.actionId = ?) AND ";
3520            private static final String _FINDER_COLUMN_A_R_RESOURCEID_2 = "permission.resourceId = ?";
3521            private static final String _ORDER_BY_ENTITY_ALIAS = "permission.";
3522            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No Permission exists with the primary key ";
3523            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No Permission exists with the key {";
3524            private static final boolean _HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE = com.liferay.portal.util.PropsValues.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE;
3525            private static Log _log = LogFactoryUtil.getLog(PermissionPersistenceImpl.class);
3526            private static Permission _nullPermission = new PermissionImpl() {
3527                            @Override
3528                            public Object clone() {
3529                                    return this;
3530                            }
3531    
3532                            @Override
3533                            public CacheModel<Permission> toCacheModel() {
3534                                    return _nullPermissionCacheModel;
3535                            }
3536                    };
3537    
3538            private static CacheModel<Permission> _nullPermissionCacheModel = new CacheModel<Permission>() {
3539                            public Permission toEntityModel() {
3540                                    return _nullPermission;
3541                            }
3542                    };
3543    }