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