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