001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.service.persistence;
016    
017    import com.liferay.portal.NoSuchResourcePermissionException;
018    import com.liferay.portal.kernel.cache.CacheRegistryUtil;
019    import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
020    import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
021    import com.liferay.portal.kernel.dao.orm.FinderPath;
022    import com.liferay.portal.kernel.dao.orm.Query;
023    import com.liferay.portal.kernel.dao.orm.QueryPos;
024    import com.liferay.portal.kernel.dao.orm.QueryUtil;
025    import com.liferay.portal.kernel.dao.orm.Session;
026    import com.liferay.portal.kernel.exception.SystemException;
027    import com.liferay.portal.kernel.log.Log;
028    import com.liferay.portal.kernel.log.LogFactoryUtil;
029    import com.liferay.portal.kernel.util.ArrayUtil;
030    import com.liferay.portal.kernel.util.GetterUtil;
031    import com.liferay.portal.kernel.util.InstanceFactory;
032    import com.liferay.portal.kernel.util.OrderByComparator;
033    import com.liferay.portal.kernel.util.StringBundler;
034    import com.liferay.portal.kernel.util.StringPool;
035    import com.liferay.portal.kernel.util.StringUtil;
036    import com.liferay.portal.kernel.util.UnmodifiableList;
037    import com.liferay.portal.kernel.util.Validator;
038    import com.liferay.portal.model.CacheModel;
039    import com.liferay.portal.model.ModelListener;
040    import com.liferay.portal.model.ResourcePermission;
041    import com.liferay.portal.model.impl.ResourcePermissionImpl;
042    import com.liferay.portal.model.impl.ResourcePermissionModelImpl;
043    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
044    
045    import java.io.Serializable;
046    
047    import java.util.ArrayList;
048    import java.util.Collections;
049    import java.util.List;
050    
051    /**
052     * The persistence implementation for the resource permission service.
053     *
054     * <p>
055     * Caching information and settings can be found in <code>portal.properties</code>
056     * </p>
057     *
058     * @author Brian Wing Shun Chan
059     * @see ResourcePermissionPersistence
060     * @see ResourcePermissionUtil
061     * @generated
062     */
063    public class ResourcePermissionPersistenceImpl extends BasePersistenceImpl<ResourcePermission>
064            implements ResourcePermissionPersistence {
065            /*
066             * NOTE FOR DEVELOPERS:
067             *
068             * Never modify or reference this class directly. Always use {@link ResourcePermissionUtil} to access the resource permission persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
069             */
070            public static final String FINDER_CLASS_NAME_ENTITY = ResourcePermissionImpl.class.getName();
071            public static final String FINDER_CLASS_NAME_LIST_WITH_PAGINATION = FINDER_CLASS_NAME_ENTITY +
072                    ".List1";
073            public static final String FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION = FINDER_CLASS_NAME_ENTITY +
074                    ".List2";
075            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_ALL = new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
076                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED,
077                            ResourcePermissionImpl.class,
078                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findAll", new String[0]);
079            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
080                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED,
081                            ResourcePermissionImpl.class,
082                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
083            public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
084                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED, Long.class,
085                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]);
086            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_SCOPE = new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
087                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED,
088                            ResourcePermissionImpl.class,
089                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByScope",
090                            new String[] {
091                                    Integer.class.getName(),
092                                    
093                            Integer.class.getName(), Integer.class.getName(),
094                                    OrderByComparator.class.getName()
095                            });
096            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_SCOPE = new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
097                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED,
098                            ResourcePermissionImpl.class,
099                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByScope",
100                            new String[] { Integer.class.getName() },
101                            ResourcePermissionModelImpl.SCOPE_COLUMN_BITMASK);
102            public static final FinderPath FINDER_PATH_COUNT_BY_SCOPE = new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
103                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED, Long.class,
104                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByScope",
105                            new String[] { Integer.class.getName() });
106            public static final FinderPath FINDER_PATH_WITH_PAGINATION_COUNT_BY_SCOPE = new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
107                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED, Long.class,
108                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "countByScope",
109                            new String[] { Integer.class.getName() });
110    
111            /**
112             * Returns all the resource permissions where scope = &#63;.
113             *
114             * @param scope the scope
115             * @return the matching resource permissions
116             * @throws SystemException if a system exception occurred
117             */
118            @Override
119            public List<ResourcePermission> findByScope(int scope)
120                    throws SystemException {
121                    return findByScope(scope, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
122            }
123    
124            /**
125             * Returns a range of all the resource permissions where scope = &#63;.
126             *
127             * <p>
128             * 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. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portal.model.impl.ResourcePermissionModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
129             * </p>
130             *
131             * @param scope the scope
132             * @param start the lower bound of the range of resource permissions
133             * @param end the upper bound of the range of resource permissions (not inclusive)
134             * @return the range of matching resource permissions
135             * @throws SystemException if a system exception occurred
136             */
137            @Override
138            public List<ResourcePermission> findByScope(int scope, int start, int end)
139                    throws SystemException {
140                    return findByScope(scope, start, end, null);
141            }
142    
143            /**
144             * Returns an ordered range of all the resource permissions where scope = &#63;.
145             *
146             * <p>
147             * 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. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portal.model.impl.ResourcePermissionModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
148             * </p>
149             *
150             * @param scope the scope
151             * @param start the lower bound of the range of resource permissions
152             * @param end the upper bound of the range of resource permissions (not inclusive)
153             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
154             * @return the ordered range of matching resource permissions
155             * @throws SystemException if a system exception occurred
156             */
157            @Override
158            public List<ResourcePermission> findByScope(int scope, int start, int end,
159                    OrderByComparator orderByComparator) throws SystemException {
160                    boolean pagination = true;
161                    FinderPath finderPath = null;
162                    Object[] finderArgs = null;
163    
164                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
165                                    (orderByComparator == null)) {
166                            pagination = false;
167                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_SCOPE;
168                            finderArgs = new Object[] { scope };
169                    }
170                    else {
171                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_SCOPE;
172                            finderArgs = new Object[] { scope, start, end, orderByComparator };
173                    }
174    
175                    List<ResourcePermission> list = (List<ResourcePermission>)FinderCacheUtil.getResult(finderPath,
176                                    finderArgs, this);
177    
178                    if ((list != null) && !list.isEmpty()) {
179                            for (ResourcePermission resourcePermission : list) {
180                                    if ((scope != resourcePermission.getScope())) {
181                                            list = null;
182    
183                                            break;
184                                    }
185                            }
186                    }
187    
188                    if (list == null) {
189                            StringBundler query = null;
190    
191                            if (orderByComparator != null) {
192                                    query = new StringBundler(3 +
193                                                    (orderByComparator.getOrderByFields().length * 3));
194                            }
195                            else {
196                                    query = new StringBundler(3);
197                            }
198    
199                            query.append(_SQL_SELECT_RESOURCEPERMISSION_WHERE);
200    
201                            query.append(_FINDER_COLUMN_SCOPE_SCOPE_2);
202    
203                            if (orderByComparator != null) {
204                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
205                                            orderByComparator);
206                            }
207                            else
208                             if (pagination) {
209                                    query.append(ResourcePermissionModelImpl.ORDER_BY_JPQL);
210                            }
211    
212                            String sql = query.toString();
213    
214                            Session session = null;
215    
216                            try {
217                                    session = openSession();
218    
219                                    Query q = session.createQuery(sql);
220    
221                                    QueryPos qPos = QueryPos.getInstance(q);
222    
223                                    qPos.add(scope);
224    
225                                    if (!pagination) {
226                                            list = (List<ResourcePermission>)QueryUtil.list(q,
227                                                            getDialect(), start, end, false);
228    
229                                            Collections.sort(list);
230    
231                                            list = new UnmodifiableList<ResourcePermission>(list);
232                                    }
233                                    else {
234                                            list = (List<ResourcePermission>)QueryUtil.list(q,
235                                                            getDialect(), start, end);
236                                    }
237    
238                                    cacheResult(list);
239    
240                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
241                            }
242                            catch (Exception e) {
243                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
244    
245                                    throw processException(e);
246                            }
247                            finally {
248                                    closeSession(session);
249                            }
250                    }
251    
252                    return list;
253            }
254    
255            /**
256             * Returns the first resource permission in the ordered set where scope = &#63;.
257             *
258             * @param scope the scope
259             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
260             * @return the first matching resource permission
261             * @throws com.liferay.portal.NoSuchResourcePermissionException if a matching resource permission could not be found
262             * @throws SystemException if a system exception occurred
263             */
264            @Override
265            public ResourcePermission findByScope_First(int scope,
266                    OrderByComparator orderByComparator)
267                    throws NoSuchResourcePermissionException, SystemException {
268                    ResourcePermission resourcePermission = fetchByScope_First(scope,
269                                    orderByComparator);
270    
271                    if (resourcePermission != null) {
272                            return resourcePermission;
273                    }
274    
275                    StringBundler msg = new StringBundler(4);
276    
277                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
278    
279                    msg.append("scope=");
280                    msg.append(scope);
281    
282                    msg.append(StringPool.CLOSE_CURLY_BRACE);
283    
284                    throw new NoSuchResourcePermissionException(msg.toString());
285            }
286    
287            /**
288             * Returns the first resource permission in the ordered set where scope = &#63;.
289             *
290             * @param scope the scope
291             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
292             * @return the first matching resource permission, or <code>null</code> if a matching resource permission could not be found
293             * @throws SystemException if a system exception occurred
294             */
295            @Override
296            public ResourcePermission fetchByScope_First(int scope,
297                    OrderByComparator orderByComparator) throws SystemException {
298                    List<ResourcePermission> list = findByScope(scope, 0, 1,
299                                    orderByComparator);
300    
301                    if (!list.isEmpty()) {
302                            return list.get(0);
303                    }
304    
305                    return null;
306            }
307    
308            /**
309             * Returns the last resource permission in the ordered set where scope = &#63;.
310             *
311             * @param scope the scope
312             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
313             * @return the last matching resource permission
314             * @throws com.liferay.portal.NoSuchResourcePermissionException if a matching resource permission could not be found
315             * @throws SystemException if a system exception occurred
316             */
317            @Override
318            public ResourcePermission findByScope_Last(int scope,
319                    OrderByComparator orderByComparator)
320                    throws NoSuchResourcePermissionException, SystemException {
321                    ResourcePermission resourcePermission = fetchByScope_Last(scope,
322                                    orderByComparator);
323    
324                    if (resourcePermission != null) {
325                            return resourcePermission;
326                    }
327    
328                    StringBundler msg = new StringBundler(4);
329    
330                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
331    
332                    msg.append("scope=");
333                    msg.append(scope);
334    
335                    msg.append(StringPool.CLOSE_CURLY_BRACE);
336    
337                    throw new NoSuchResourcePermissionException(msg.toString());
338            }
339    
340            /**
341             * Returns the last resource permission in the ordered set where scope = &#63;.
342             *
343             * @param scope the scope
344             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
345             * @return the last matching resource permission, or <code>null</code> if a matching resource permission could not be found
346             * @throws SystemException if a system exception occurred
347             */
348            @Override
349            public ResourcePermission fetchByScope_Last(int scope,
350                    OrderByComparator orderByComparator) throws SystemException {
351                    int count = countByScope(scope);
352    
353                    if (count == 0) {
354                            return null;
355                    }
356    
357                    List<ResourcePermission> list = findByScope(scope, count - 1, count,
358                                    orderByComparator);
359    
360                    if (!list.isEmpty()) {
361                            return list.get(0);
362                    }
363    
364                    return null;
365            }
366    
367            /**
368             * Returns the resource permissions before and after the current resource permission in the ordered set where scope = &#63;.
369             *
370             * @param resourcePermissionId the primary key of the current resource permission
371             * @param scope the scope
372             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
373             * @return the previous, current, and next resource permission
374             * @throws com.liferay.portal.NoSuchResourcePermissionException if a resource permission with the primary key could not be found
375             * @throws SystemException if a system exception occurred
376             */
377            @Override
378            public ResourcePermission[] findByScope_PrevAndNext(
379                    long resourcePermissionId, int scope,
380                    OrderByComparator orderByComparator)
381                    throws NoSuchResourcePermissionException, SystemException {
382                    ResourcePermission resourcePermission = findByPrimaryKey(resourcePermissionId);
383    
384                    Session session = null;
385    
386                    try {
387                            session = openSession();
388    
389                            ResourcePermission[] array = new ResourcePermissionImpl[3];
390    
391                            array[0] = getByScope_PrevAndNext(session, resourcePermission,
392                                            scope, orderByComparator, true);
393    
394                            array[1] = resourcePermission;
395    
396                            array[2] = getByScope_PrevAndNext(session, resourcePermission,
397                                            scope, orderByComparator, false);
398    
399                            return array;
400                    }
401                    catch (Exception e) {
402                            throw processException(e);
403                    }
404                    finally {
405                            closeSession(session);
406                    }
407            }
408    
409            protected ResourcePermission getByScope_PrevAndNext(Session session,
410                    ResourcePermission resourcePermission, int scope,
411                    OrderByComparator orderByComparator, boolean previous) {
412                    StringBundler query = null;
413    
414                    if (orderByComparator != null) {
415                            query = new StringBundler(6 +
416                                            (orderByComparator.getOrderByFields().length * 6));
417                    }
418                    else {
419                            query = new StringBundler(3);
420                    }
421    
422                    query.append(_SQL_SELECT_RESOURCEPERMISSION_WHERE);
423    
424                    query.append(_FINDER_COLUMN_SCOPE_SCOPE_2);
425    
426                    if (orderByComparator != null) {
427                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
428    
429                            if (orderByConditionFields.length > 0) {
430                                    query.append(WHERE_AND);
431                            }
432    
433                            for (int i = 0; i < orderByConditionFields.length; i++) {
434                                    query.append(_ORDER_BY_ENTITY_ALIAS);
435                                    query.append(orderByConditionFields[i]);
436    
437                                    if ((i + 1) < orderByConditionFields.length) {
438                                            if (orderByComparator.isAscending() ^ previous) {
439                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
440                                            }
441                                            else {
442                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
443                                            }
444                                    }
445                                    else {
446                                            if (orderByComparator.isAscending() ^ previous) {
447                                                    query.append(WHERE_GREATER_THAN);
448                                            }
449                                            else {
450                                                    query.append(WHERE_LESSER_THAN);
451                                            }
452                                    }
453                            }
454    
455                            query.append(ORDER_BY_CLAUSE);
456    
457                            String[] orderByFields = orderByComparator.getOrderByFields();
458    
459                            for (int i = 0; i < orderByFields.length; i++) {
460                                    query.append(_ORDER_BY_ENTITY_ALIAS);
461                                    query.append(orderByFields[i]);
462    
463                                    if ((i + 1) < orderByFields.length) {
464                                            if (orderByComparator.isAscending() ^ previous) {
465                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
466                                            }
467                                            else {
468                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
469                                            }
470                                    }
471                                    else {
472                                            if (orderByComparator.isAscending() ^ previous) {
473                                                    query.append(ORDER_BY_ASC);
474                                            }
475                                            else {
476                                                    query.append(ORDER_BY_DESC);
477                                            }
478                                    }
479                            }
480                    }
481                    else {
482                            query.append(ResourcePermissionModelImpl.ORDER_BY_JPQL);
483                    }
484    
485                    String sql = query.toString();
486    
487                    Query q = session.createQuery(sql);
488    
489                    q.setFirstResult(0);
490                    q.setMaxResults(2);
491    
492                    QueryPos qPos = QueryPos.getInstance(q);
493    
494                    qPos.add(scope);
495    
496                    if (orderByComparator != null) {
497                            Object[] values = orderByComparator.getOrderByConditionValues(resourcePermission);
498    
499                            for (Object value : values) {
500                                    qPos.add(value);
501                            }
502                    }
503    
504                    List<ResourcePermission> list = q.list();
505    
506                    if (list.size() == 2) {
507                            return list.get(1);
508                    }
509                    else {
510                            return null;
511                    }
512            }
513    
514            /**
515             * Returns all the resource permissions where scope = any &#63;.
516             *
517             * <p>
518             * 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. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portal.model.impl.ResourcePermissionModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
519             * </p>
520             *
521             * @param scopes the scopes
522             * @return the matching resource permissions
523             * @throws SystemException if a system exception occurred
524             */
525            @Override
526            public List<ResourcePermission> findByScope(int[] scopes)
527                    throws SystemException {
528                    return findByScope(scopes, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
529            }
530    
531            /**
532             * Returns a range of all the resource permissions where scope = any &#63;.
533             *
534             * <p>
535             * 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. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portal.model.impl.ResourcePermissionModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
536             * </p>
537             *
538             * @param scopes the scopes
539             * @param start the lower bound of the range of resource permissions
540             * @param end the upper bound of the range of resource permissions (not inclusive)
541             * @return the range of matching resource permissions
542             * @throws SystemException if a system exception occurred
543             */
544            @Override
545            public List<ResourcePermission> findByScope(int[] scopes, int start, int end)
546                    throws SystemException {
547                    return findByScope(scopes, start, end, null);
548            }
549    
550            /**
551             * Returns an ordered range of all the resource permissions where scope = any &#63;.
552             *
553             * <p>
554             * 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. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portal.model.impl.ResourcePermissionModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
555             * </p>
556             *
557             * @param scopes the scopes
558             * @param start the lower bound of the range of resource permissions
559             * @param end the upper bound of the range of resource permissions (not inclusive)
560             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
561             * @return the ordered range of matching resource permissions
562             * @throws SystemException if a system exception occurred
563             */
564            @Override
565            public List<ResourcePermission> findByScope(int[] scopes, int start,
566                    int end, OrderByComparator orderByComparator) throws SystemException {
567                    if ((scopes != null) && (scopes.length == 1)) {
568                            return findByScope(scopes[0], start, end, orderByComparator);
569                    }
570    
571                    boolean pagination = true;
572                    Object[] finderArgs = null;
573    
574                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
575                                    (orderByComparator == null)) {
576                            pagination = false;
577                            finderArgs = new Object[] { StringUtil.merge(scopes) };
578                    }
579                    else {
580                            finderArgs = new Object[] {
581                                            StringUtil.merge(scopes),
582                                            
583                                            start, end, orderByComparator
584                                    };
585                    }
586    
587                    List<ResourcePermission> list = (List<ResourcePermission>)FinderCacheUtil.getResult(FINDER_PATH_WITH_PAGINATION_FIND_BY_SCOPE,
588                                    finderArgs, this);
589    
590                    if ((list != null) && !list.isEmpty()) {
591                            for (ResourcePermission resourcePermission : list) {
592                                    if (!ArrayUtil.contains(scopes, resourcePermission.getScope())) {
593                                            list = null;
594    
595                                            break;
596                                    }
597                            }
598                    }
599    
600                    if (list == null) {
601                            StringBundler query = new StringBundler();
602    
603                            query.append(_SQL_SELECT_RESOURCEPERMISSION_WHERE);
604    
605                            boolean conjunctionable = false;
606    
607                            if ((scopes == null) || (scopes.length > 0)) {
608                                    if (conjunctionable) {
609                                            query.append(WHERE_AND);
610                                    }
611    
612                                    query.append(StringPool.OPEN_PARENTHESIS);
613    
614                                    for (int i = 0; i < scopes.length; i++) {
615                                            query.append(_FINDER_COLUMN_SCOPE_SCOPE_5);
616    
617                                            if ((i + 1) < scopes.length) {
618                                                    query.append(WHERE_OR);
619                                            }
620                                    }
621    
622                                    query.append(StringPool.CLOSE_PARENTHESIS);
623    
624                                    conjunctionable = true;
625                            }
626    
627                            if (orderByComparator != null) {
628                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
629                                            orderByComparator);
630                            }
631                            else
632                             if (pagination) {
633                                    query.append(ResourcePermissionModelImpl.ORDER_BY_JPQL);
634                            }
635    
636                            String sql = query.toString();
637    
638                            Session session = null;
639    
640                            try {
641                                    session = openSession();
642    
643                                    Query q = session.createQuery(sql);
644    
645                                    QueryPos qPos = QueryPos.getInstance(q);
646    
647                                    if (scopes != null) {
648                                            qPos.add(scopes);
649                                    }
650    
651                                    if (!pagination) {
652                                            list = (List<ResourcePermission>)QueryUtil.list(q,
653                                                            getDialect(), start, end, false);
654    
655                                            Collections.sort(list);
656    
657                                            list = new UnmodifiableList<ResourcePermission>(list);
658                                    }
659                                    else {
660                                            list = (List<ResourcePermission>)QueryUtil.list(q,
661                                                            getDialect(), start, end);
662                                    }
663    
664                                    cacheResult(list);
665    
666                                    FinderCacheUtil.putResult(FINDER_PATH_WITH_PAGINATION_FIND_BY_SCOPE,
667                                            finderArgs, list);
668                            }
669                            catch (Exception e) {
670                                    FinderCacheUtil.removeResult(FINDER_PATH_WITH_PAGINATION_FIND_BY_SCOPE,
671                                            finderArgs);
672    
673                                    throw processException(e);
674                            }
675                            finally {
676                                    closeSession(session);
677                            }
678                    }
679    
680                    return list;
681            }
682    
683            /**
684             * Removes all the resource permissions where scope = &#63; from the database.
685             *
686             * @param scope the scope
687             * @throws SystemException if a system exception occurred
688             */
689            @Override
690            public void removeByScope(int scope) throws SystemException {
691                    for (ResourcePermission resourcePermission : findByScope(scope,
692                                    QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
693                            remove(resourcePermission);
694                    }
695            }
696    
697            /**
698             * Returns the number of resource permissions where scope = &#63;.
699             *
700             * @param scope the scope
701             * @return the number of matching resource permissions
702             * @throws SystemException if a system exception occurred
703             */
704            @Override
705            public int countByScope(int scope) throws SystemException {
706                    FinderPath finderPath = FINDER_PATH_COUNT_BY_SCOPE;
707    
708                    Object[] finderArgs = new Object[] { scope };
709    
710                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
711                                    this);
712    
713                    if (count == null) {
714                            StringBundler query = new StringBundler(2);
715    
716                            query.append(_SQL_COUNT_RESOURCEPERMISSION_WHERE);
717    
718                            query.append(_FINDER_COLUMN_SCOPE_SCOPE_2);
719    
720                            String sql = query.toString();
721    
722                            Session session = null;
723    
724                            try {
725                                    session = openSession();
726    
727                                    Query q = session.createQuery(sql);
728    
729                                    QueryPos qPos = QueryPos.getInstance(q);
730    
731                                    qPos.add(scope);
732    
733                                    count = (Long)q.uniqueResult();
734    
735                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
736                            }
737                            catch (Exception e) {
738                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
739    
740                                    throw processException(e);
741                            }
742                            finally {
743                                    closeSession(session);
744                            }
745                    }
746    
747                    return count.intValue();
748            }
749    
750            /**
751             * Returns the number of resource permissions where scope = any &#63;.
752             *
753             * @param scopes the scopes
754             * @return the number of matching resource permissions
755             * @throws SystemException if a system exception occurred
756             */
757            @Override
758            public int countByScope(int[] scopes) throws SystemException {
759                    Object[] finderArgs = new Object[] { StringUtil.merge(scopes) };
760    
761                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_WITH_PAGINATION_COUNT_BY_SCOPE,
762                                    finderArgs, this);
763    
764                    if (count == null) {
765                            StringBundler query = new StringBundler();
766    
767                            query.append(_SQL_COUNT_RESOURCEPERMISSION_WHERE);
768    
769                            boolean conjunctionable = false;
770    
771                            if ((scopes == null) || (scopes.length > 0)) {
772                                    if (conjunctionable) {
773                                            query.append(WHERE_AND);
774                                    }
775    
776                                    query.append(StringPool.OPEN_PARENTHESIS);
777    
778                                    for (int i = 0; i < scopes.length; i++) {
779                                            query.append(_FINDER_COLUMN_SCOPE_SCOPE_5);
780    
781                                            if ((i + 1) < scopes.length) {
782                                                    query.append(WHERE_OR);
783                                            }
784                                    }
785    
786                                    query.append(StringPool.CLOSE_PARENTHESIS);
787    
788                                    conjunctionable = true;
789                            }
790    
791                            String sql = query.toString();
792    
793                            Session session = null;
794    
795                            try {
796                                    session = openSession();
797    
798                                    Query q = session.createQuery(sql);
799    
800                                    QueryPos qPos = QueryPos.getInstance(q);
801    
802                                    if (scopes != null) {
803                                            qPos.add(scopes);
804                                    }
805    
806                                    count = (Long)q.uniqueResult();
807    
808                                    FinderCacheUtil.putResult(FINDER_PATH_WITH_PAGINATION_COUNT_BY_SCOPE,
809                                            finderArgs, count);
810                            }
811                            catch (Exception e) {
812                                    FinderCacheUtil.removeResult(FINDER_PATH_WITH_PAGINATION_COUNT_BY_SCOPE,
813                                            finderArgs);
814    
815                                    throw processException(e);
816                            }
817                            finally {
818                                    closeSession(session);
819                            }
820                    }
821    
822                    return count.intValue();
823            }
824    
825            private static final String _FINDER_COLUMN_SCOPE_SCOPE_2 = "resourcePermission.scope = ?";
826            private static final String _FINDER_COLUMN_SCOPE_SCOPE_5 = "(" +
827                    removeConjunction(_FINDER_COLUMN_SCOPE_SCOPE_2) + ")";
828            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_ROLEID = new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
829                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED,
830                            ResourcePermissionImpl.class,
831                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByRoleId",
832                            new String[] {
833                                    Long.class.getName(),
834                                    
835                            Integer.class.getName(), Integer.class.getName(),
836                                    OrderByComparator.class.getName()
837                            });
838            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_ROLEID =
839                    new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
840                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED,
841                            ResourcePermissionImpl.class,
842                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByRoleId",
843                            new String[] { Long.class.getName() },
844                            ResourcePermissionModelImpl.ROLEID_COLUMN_BITMASK);
845            public static final FinderPath FINDER_PATH_COUNT_BY_ROLEID = new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
846                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED, Long.class,
847                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByRoleId",
848                            new String[] { Long.class.getName() });
849    
850            /**
851             * Returns all the resource permissions where roleId = &#63;.
852             *
853             * @param roleId the role ID
854             * @return the matching resource permissions
855             * @throws SystemException if a system exception occurred
856             */
857            @Override
858            public List<ResourcePermission> findByRoleId(long roleId)
859                    throws SystemException {
860                    return findByRoleId(roleId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
861            }
862    
863            /**
864             * Returns a range of all the resource permissions where roleId = &#63;.
865             *
866             * <p>
867             * 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. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portal.model.impl.ResourcePermissionModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
868             * </p>
869             *
870             * @param roleId the role ID
871             * @param start the lower bound of the range of resource permissions
872             * @param end the upper bound of the range of resource permissions (not inclusive)
873             * @return the range of matching resource permissions
874             * @throws SystemException if a system exception occurred
875             */
876            @Override
877            public List<ResourcePermission> findByRoleId(long roleId, int start, int end)
878                    throws SystemException {
879                    return findByRoleId(roleId, start, end, null);
880            }
881    
882            /**
883             * Returns an ordered range of all the resource permissions where roleId = &#63;.
884             *
885             * <p>
886             * 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. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portal.model.impl.ResourcePermissionModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
887             * </p>
888             *
889             * @param roleId the role ID
890             * @param start the lower bound of the range of resource permissions
891             * @param end the upper bound of the range of resource permissions (not inclusive)
892             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
893             * @return the ordered range of matching resource permissions
894             * @throws SystemException if a system exception occurred
895             */
896            @Override
897            public List<ResourcePermission> findByRoleId(long roleId, int start,
898                    int end, OrderByComparator orderByComparator) throws SystemException {
899                    boolean pagination = true;
900                    FinderPath finderPath = null;
901                    Object[] finderArgs = null;
902    
903                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
904                                    (orderByComparator == null)) {
905                            pagination = false;
906                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_ROLEID;
907                            finderArgs = new Object[] { roleId };
908                    }
909                    else {
910                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_ROLEID;
911                            finderArgs = new Object[] { roleId, start, end, orderByComparator };
912                    }
913    
914                    List<ResourcePermission> list = (List<ResourcePermission>)FinderCacheUtil.getResult(finderPath,
915                                    finderArgs, this);
916    
917                    if ((list != null) && !list.isEmpty()) {
918                            for (ResourcePermission resourcePermission : list) {
919                                    if ((roleId != resourcePermission.getRoleId())) {
920                                            list = null;
921    
922                                            break;
923                                    }
924                            }
925                    }
926    
927                    if (list == null) {
928                            StringBundler query = null;
929    
930                            if (orderByComparator != null) {
931                                    query = new StringBundler(3 +
932                                                    (orderByComparator.getOrderByFields().length * 3));
933                            }
934                            else {
935                                    query = new StringBundler(3);
936                            }
937    
938                            query.append(_SQL_SELECT_RESOURCEPERMISSION_WHERE);
939    
940                            query.append(_FINDER_COLUMN_ROLEID_ROLEID_2);
941    
942                            if (orderByComparator != null) {
943                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
944                                            orderByComparator);
945                            }
946                            else
947                             if (pagination) {
948                                    query.append(ResourcePermissionModelImpl.ORDER_BY_JPQL);
949                            }
950    
951                            String sql = query.toString();
952    
953                            Session session = null;
954    
955                            try {
956                                    session = openSession();
957    
958                                    Query q = session.createQuery(sql);
959    
960                                    QueryPos qPos = QueryPos.getInstance(q);
961    
962                                    qPos.add(roleId);
963    
964                                    if (!pagination) {
965                                            list = (List<ResourcePermission>)QueryUtil.list(q,
966                                                            getDialect(), start, end, false);
967    
968                                            Collections.sort(list);
969    
970                                            list = new UnmodifiableList<ResourcePermission>(list);
971                                    }
972                                    else {
973                                            list = (List<ResourcePermission>)QueryUtil.list(q,
974                                                            getDialect(), start, end);
975                                    }
976    
977                                    cacheResult(list);
978    
979                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
980                            }
981                            catch (Exception e) {
982                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
983    
984                                    throw processException(e);
985                            }
986                            finally {
987                                    closeSession(session);
988                            }
989                    }
990    
991                    return list;
992            }
993    
994            /**
995             * Returns the first resource permission in the ordered set where roleId = &#63;.
996             *
997             * @param roleId the role ID
998             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
999             * @return the first matching resource permission
1000             * @throws com.liferay.portal.NoSuchResourcePermissionException if a matching resource permission could not be found
1001             * @throws SystemException if a system exception occurred
1002             */
1003            @Override
1004            public ResourcePermission findByRoleId_First(long roleId,
1005                    OrderByComparator orderByComparator)
1006                    throws NoSuchResourcePermissionException, SystemException {
1007                    ResourcePermission resourcePermission = fetchByRoleId_First(roleId,
1008                                    orderByComparator);
1009    
1010                    if (resourcePermission != null) {
1011                            return resourcePermission;
1012                    }
1013    
1014                    StringBundler msg = new StringBundler(4);
1015    
1016                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1017    
1018                    msg.append("roleId=");
1019                    msg.append(roleId);
1020    
1021                    msg.append(StringPool.CLOSE_CURLY_BRACE);
1022    
1023                    throw new NoSuchResourcePermissionException(msg.toString());
1024            }
1025    
1026            /**
1027             * Returns the first resource permission in the ordered set where roleId = &#63;.
1028             *
1029             * @param roleId the role ID
1030             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1031             * @return the first matching resource permission, or <code>null</code> if a matching resource permission could not be found
1032             * @throws SystemException if a system exception occurred
1033             */
1034            @Override
1035            public ResourcePermission fetchByRoleId_First(long roleId,
1036                    OrderByComparator orderByComparator) throws SystemException {
1037                    List<ResourcePermission> list = findByRoleId(roleId, 0, 1,
1038                                    orderByComparator);
1039    
1040                    if (!list.isEmpty()) {
1041                            return list.get(0);
1042                    }
1043    
1044                    return null;
1045            }
1046    
1047            /**
1048             * Returns the last resource permission in the ordered set where roleId = &#63;.
1049             *
1050             * @param roleId the role ID
1051             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1052             * @return the last matching resource permission
1053             * @throws com.liferay.portal.NoSuchResourcePermissionException if a matching resource permission could not be found
1054             * @throws SystemException if a system exception occurred
1055             */
1056            @Override
1057            public ResourcePermission findByRoleId_Last(long roleId,
1058                    OrderByComparator orderByComparator)
1059                    throws NoSuchResourcePermissionException, SystemException {
1060                    ResourcePermission resourcePermission = fetchByRoleId_Last(roleId,
1061                                    orderByComparator);
1062    
1063                    if (resourcePermission != null) {
1064                            return resourcePermission;
1065                    }
1066    
1067                    StringBundler msg = new StringBundler(4);
1068    
1069                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1070    
1071                    msg.append("roleId=");
1072                    msg.append(roleId);
1073    
1074                    msg.append(StringPool.CLOSE_CURLY_BRACE);
1075    
1076                    throw new NoSuchResourcePermissionException(msg.toString());
1077            }
1078    
1079            /**
1080             * Returns the last resource permission in the ordered set where roleId = &#63;.
1081             *
1082             * @param roleId the role ID
1083             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1084             * @return the last matching resource permission, or <code>null</code> if a matching resource permission could not be found
1085             * @throws SystemException if a system exception occurred
1086             */
1087            @Override
1088            public ResourcePermission fetchByRoleId_Last(long roleId,
1089                    OrderByComparator orderByComparator) throws SystemException {
1090                    int count = countByRoleId(roleId);
1091    
1092                    if (count == 0) {
1093                            return null;
1094                    }
1095    
1096                    List<ResourcePermission> list = findByRoleId(roleId, count - 1, count,
1097                                    orderByComparator);
1098    
1099                    if (!list.isEmpty()) {
1100                            return list.get(0);
1101                    }
1102    
1103                    return null;
1104            }
1105    
1106            /**
1107             * Returns the resource permissions before and after the current resource permission in the ordered set where roleId = &#63;.
1108             *
1109             * @param resourcePermissionId the primary key of the current resource permission
1110             * @param roleId the role ID
1111             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1112             * @return the previous, current, and next resource permission
1113             * @throws com.liferay.portal.NoSuchResourcePermissionException if a resource permission with the primary key could not be found
1114             * @throws SystemException if a system exception occurred
1115             */
1116            @Override
1117            public ResourcePermission[] findByRoleId_PrevAndNext(
1118                    long resourcePermissionId, long roleId,
1119                    OrderByComparator orderByComparator)
1120                    throws NoSuchResourcePermissionException, SystemException {
1121                    ResourcePermission resourcePermission = findByPrimaryKey(resourcePermissionId);
1122    
1123                    Session session = null;
1124    
1125                    try {
1126                            session = openSession();
1127    
1128                            ResourcePermission[] array = new ResourcePermissionImpl[3];
1129    
1130                            array[0] = getByRoleId_PrevAndNext(session, resourcePermission,
1131                                            roleId, orderByComparator, true);
1132    
1133                            array[1] = resourcePermission;
1134    
1135                            array[2] = getByRoleId_PrevAndNext(session, resourcePermission,
1136                                            roleId, orderByComparator, false);
1137    
1138                            return array;
1139                    }
1140                    catch (Exception e) {
1141                            throw processException(e);
1142                    }
1143                    finally {
1144                            closeSession(session);
1145                    }
1146            }
1147    
1148            protected ResourcePermission getByRoleId_PrevAndNext(Session session,
1149                    ResourcePermission resourcePermission, long roleId,
1150                    OrderByComparator orderByComparator, boolean previous) {
1151                    StringBundler query = null;
1152    
1153                    if (orderByComparator != null) {
1154                            query = new StringBundler(6 +
1155                                            (orderByComparator.getOrderByFields().length * 6));
1156                    }
1157                    else {
1158                            query = new StringBundler(3);
1159                    }
1160    
1161                    query.append(_SQL_SELECT_RESOURCEPERMISSION_WHERE);
1162    
1163                    query.append(_FINDER_COLUMN_ROLEID_ROLEID_2);
1164    
1165                    if (orderByComparator != null) {
1166                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
1167    
1168                            if (orderByConditionFields.length > 0) {
1169                                    query.append(WHERE_AND);
1170                            }
1171    
1172                            for (int i = 0; i < orderByConditionFields.length; i++) {
1173                                    query.append(_ORDER_BY_ENTITY_ALIAS);
1174                                    query.append(orderByConditionFields[i]);
1175    
1176                                    if ((i + 1) < orderByConditionFields.length) {
1177                                            if (orderByComparator.isAscending() ^ previous) {
1178                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
1179                                            }
1180                                            else {
1181                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
1182                                            }
1183                                    }
1184                                    else {
1185                                            if (orderByComparator.isAscending() ^ previous) {
1186                                                    query.append(WHERE_GREATER_THAN);
1187                                            }
1188                                            else {
1189                                                    query.append(WHERE_LESSER_THAN);
1190                                            }
1191                                    }
1192                            }
1193    
1194                            query.append(ORDER_BY_CLAUSE);
1195    
1196                            String[] orderByFields = orderByComparator.getOrderByFields();
1197    
1198                            for (int i = 0; i < orderByFields.length; i++) {
1199                                    query.append(_ORDER_BY_ENTITY_ALIAS);
1200                                    query.append(orderByFields[i]);
1201    
1202                                    if ((i + 1) < orderByFields.length) {
1203                                            if (orderByComparator.isAscending() ^ previous) {
1204                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
1205                                            }
1206                                            else {
1207                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
1208                                            }
1209                                    }
1210                                    else {
1211                                            if (orderByComparator.isAscending() ^ previous) {
1212                                                    query.append(ORDER_BY_ASC);
1213                                            }
1214                                            else {
1215                                                    query.append(ORDER_BY_DESC);
1216                                            }
1217                                    }
1218                            }
1219                    }
1220                    else {
1221                            query.append(ResourcePermissionModelImpl.ORDER_BY_JPQL);
1222                    }
1223    
1224                    String sql = query.toString();
1225    
1226                    Query q = session.createQuery(sql);
1227    
1228                    q.setFirstResult(0);
1229                    q.setMaxResults(2);
1230    
1231                    QueryPos qPos = QueryPos.getInstance(q);
1232    
1233                    qPos.add(roleId);
1234    
1235                    if (orderByComparator != null) {
1236                            Object[] values = orderByComparator.getOrderByConditionValues(resourcePermission);
1237    
1238                            for (Object value : values) {
1239                                    qPos.add(value);
1240                            }
1241                    }
1242    
1243                    List<ResourcePermission> list = q.list();
1244    
1245                    if (list.size() == 2) {
1246                            return list.get(1);
1247                    }
1248                    else {
1249                            return null;
1250                    }
1251            }
1252    
1253            /**
1254             * Removes all the resource permissions where roleId = &#63; from the database.
1255             *
1256             * @param roleId the role ID
1257             * @throws SystemException if a system exception occurred
1258             */
1259            @Override
1260            public void removeByRoleId(long roleId) throws SystemException {
1261                    for (ResourcePermission resourcePermission : findByRoleId(roleId,
1262                                    QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
1263                            remove(resourcePermission);
1264                    }
1265            }
1266    
1267            /**
1268             * Returns the number of resource permissions where roleId = &#63;.
1269             *
1270             * @param roleId the role ID
1271             * @return the number of matching resource permissions
1272             * @throws SystemException if a system exception occurred
1273             */
1274            @Override
1275            public int countByRoleId(long roleId) throws SystemException {
1276                    FinderPath finderPath = FINDER_PATH_COUNT_BY_ROLEID;
1277    
1278                    Object[] finderArgs = new Object[] { roleId };
1279    
1280                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
1281                                    this);
1282    
1283                    if (count == null) {
1284                            StringBundler query = new StringBundler(2);
1285    
1286                            query.append(_SQL_COUNT_RESOURCEPERMISSION_WHERE);
1287    
1288                            query.append(_FINDER_COLUMN_ROLEID_ROLEID_2);
1289    
1290                            String sql = query.toString();
1291    
1292                            Session session = null;
1293    
1294                            try {
1295                                    session = openSession();
1296    
1297                                    Query q = session.createQuery(sql);
1298    
1299                                    QueryPos qPos = QueryPos.getInstance(q);
1300    
1301                                    qPos.add(roleId);
1302    
1303                                    count = (Long)q.uniqueResult();
1304    
1305                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
1306                            }
1307                            catch (Exception e) {
1308                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
1309    
1310                                    throw processException(e);
1311                            }
1312                            finally {
1313                                    closeSession(session);
1314                            }
1315                    }
1316    
1317                    return count.intValue();
1318            }
1319    
1320            private static final String _FINDER_COLUMN_ROLEID_ROLEID_2 = "resourcePermission.roleId = ?";
1321            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_C_LIKEP = new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
1322                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED,
1323                            ResourcePermissionImpl.class,
1324                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByC_LikeP",
1325                            new String[] {
1326                                    Long.class.getName(), String.class.getName(),
1327                                    
1328                            Integer.class.getName(), Integer.class.getName(),
1329                                    OrderByComparator.class.getName()
1330                            });
1331            public static final FinderPath FINDER_PATH_WITH_PAGINATION_COUNT_BY_C_LIKEP = new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
1332                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED, Long.class,
1333                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "countByC_LikeP",
1334                            new String[] { Long.class.getName(), String.class.getName() });
1335    
1336            /**
1337             * Returns all the resource permissions where companyId = &#63; and primKey LIKE &#63;.
1338             *
1339             * @param companyId the company ID
1340             * @param primKey the prim key
1341             * @return the matching resource permissions
1342             * @throws SystemException if a system exception occurred
1343             */
1344            @Override
1345            public List<ResourcePermission> findByC_LikeP(long companyId, String primKey)
1346                    throws SystemException {
1347                    return findByC_LikeP(companyId, primKey, QueryUtil.ALL_POS,
1348                            QueryUtil.ALL_POS, null);
1349            }
1350    
1351            /**
1352             * Returns a range of all the resource permissions where companyId = &#63; and primKey LIKE &#63;.
1353             *
1354             * <p>
1355             * 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. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portal.model.impl.ResourcePermissionModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
1356             * </p>
1357             *
1358             * @param companyId the company ID
1359             * @param primKey the prim key
1360             * @param start the lower bound of the range of resource permissions
1361             * @param end the upper bound of the range of resource permissions (not inclusive)
1362             * @return the range of matching resource permissions
1363             * @throws SystemException if a system exception occurred
1364             */
1365            @Override
1366            public List<ResourcePermission> findByC_LikeP(long companyId,
1367                    String primKey, int start, int end) throws SystemException {
1368                    return findByC_LikeP(companyId, primKey, start, end, null);
1369            }
1370    
1371            /**
1372             * Returns an ordered range of all the resource permissions where companyId = &#63; and primKey LIKE &#63;.
1373             *
1374             * <p>
1375             * 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. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portal.model.impl.ResourcePermissionModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
1376             * </p>
1377             *
1378             * @param companyId the company ID
1379             * @param primKey the prim key
1380             * @param start the lower bound of the range of resource permissions
1381             * @param end the upper bound of the range of resource permissions (not inclusive)
1382             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
1383             * @return the ordered range of matching resource permissions
1384             * @throws SystemException if a system exception occurred
1385             */
1386            @Override
1387            public List<ResourcePermission> findByC_LikeP(long companyId,
1388                    String primKey, int start, int end, OrderByComparator orderByComparator)
1389                    throws SystemException {
1390                    boolean pagination = true;
1391                    FinderPath finderPath = null;
1392                    Object[] finderArgs = null;
1393    
1394                    finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_C_LIKEP;
1395                    finderArgs = new Object[] {
1396                                    companyId, primKey,
1397                                    
1398                                    start, end, orderByComparator
1399                            };
1400    
1401                    List<ResourcePermission> list = (List<ResourcePermission>)FinderCacheUtil.getResult(finderPath,
1402                                    finderArgs, this);
1403    
1404                    if ((list != null) && !list.isEmpty()) {
1405                            for (ResourcePermission resourcePermission : list) {
1406                                    if ((companyId != resourcePermission.getCompanyId()) ||
1407                                                    !Validator.equals(primKey,
1408                                                            resourcePermission.getPrimKey())) {
1409                                            list = null;
1410    
1411                                            break;
1412                                    }
1413                            }
1414                    }
1415    
1416                    if (list == null) {
1417                            StringBundler query = null;
1418    
1419                            if (orderByComparator != null) {
1420                                    query = new StringBundler(4 +
1421                                                    (orderByComparator.getOrderByFields().length * 3));
1422                            }
1423                            else {
1424                                    query = new StringBundler(4);
1425                            }
1426    
1427                            query.append(_SQL_SELECT_RESOURCEPERMISSION_WHERE);
1428    
1429                            query.append(_FINDER_COLUMN_C_LIKEP_COMPANYID_2);
1430    
1431                            boolean bindPrimKey = false;
1432    
1433                            if (primKey == null) {
1434                                    query.append(_FINDER_COLUMN_C_LIKEP_PRIMKEY_1);
1435                            }
1436                            else if (primKey.equals(StringPool.BLANK)) {
1437                                    query.append(_FINDER_COLUMN_C_LIKEP_PRIMKEY_3);
1438                            }
1439                            else {
1440                                    bindPrimKey = true;
1441    
1442                                    query.append(_FINDER_COLUMN_C_LIKEP_PRIMKEY_2);
1443                            }
1444    
1445                            if (orderByComparator != null) {
1446                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1447                                            orderByComparator);
1448                            }
1449                            else
1450                             if (pagination) {
1451                                    query.append(ResourcePermissionModelImpl.ORDER_BY_JPQL);
1452                            }
1453    
1454                            String sql = query.toString();
1455    
1456                            Session session = null;
1457    
1458                            try {
1459                                    session = openSession();
1460    
1461                                    Query q = session.createQuery(sql);
1462    
1463                                    QueryPos qPos = QueryPos.getInstance(q);
1464    
1465                                    qPos.add(companyId);
1466    
1467                                    if (bindPrimKey) {
1468                                            qPos.add(primKey);
1469                                    }
1470    
1471                                    if (!pagination) {
1472                                            list = (List<ResourcePermission>)QueryUtil.list(q,
1473                                                            getDialect(), start, end, false);
1474    
1475                                            Collections.sort(list);
1476    
1477                                            list = new UnmodifiableList<ResourcePermission>(list);
1478                                    }
1479                                    else {
1480                                            list = (List<ResourcePermission>)QueryUtil.list(q,
1481                                                            getDialect(), start, end);
1482                                    }
1483    
1484                                    cacheResult(list);
1485    
1486                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
1487                            }
1488                            catch (Exception e) {
1489                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
1490    
1491                                    throw processException(e);
1492                            }
1493                            finally {
1494                                    closeSession(session);
1495                            }
1496                    }
1497    
1498                    return list;
1499            }
1500    
1501            /**
1502             * Returns the first resource permission in the ordered set where companyId = &#63; and primKey LIKE &#63;.
1503             *
1504             * @param companyId the company ID
1505             * @param primKey the prim key
1506             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1507             * @return the first matching resource permission
1508             * @throws com.liferay.portal.NoSuchResourcePermissionException if a matching resource permission could not be found
1509             * @throws SystemException if a system exception occurred
1510             */
1511            @Override
1512            public ResourcePermission findByC_LikeP_First(long companyId,
1513                    String primKey, OrderByComparator orderByComparator)
1514                    throws NoSuchResourcePermissionException, SystemException {
1515                    ResourcePermission resourcePermission = fetchByC_LikeP_First(companyId,
1516                                    primKey, orderByComparator);
1517    
1518                    if (resourcePermission != null) {
1519                            return resourcePermission;
1520                    }
1521    
1522                    StringBundler msg = new StringBundler(6);
1523    
1524                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1525    
1526                    msg.append("companyId=");
1527                    msg.append(companyId);
1528    
1529                    msg.append(", primKey=");
1530                    msg.append(primKey);
1531    
1532                    msg.append(StringPool.CLOSE_CURLY_BRACE);
1533    
1534                    throw new NoSuchResourcePermissionException(msg.toString());
1535            }
1536    
1537            /**
1538             * Returns the first resource permission in the ordered set where companyId = &#63; and primKey LIKE &#63;.
1539             *
1540             * @param companyId the company ID
1541             * @param primKey the prim key
1542             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1543             * @return the first matching resource permission, or <code>null</code> if a matching resource permission could not be found
1544             * @throws SystemException if a system exception occurred
1545             */
1546            @Override
1547            public ResourcePermission fetchByC_LikeP_First(long companyId,
1548                    String primKey, OrderByComparator orderByComparator)
1549                    throws SystemException {
1550                    List<ResourcePermission> list = findByC_LikeP(companyId, primKey, 0, 1,
1551                                    orderByComparator);
1552    
1553                    if (!list.isEmpty()) {
1554                            return list.get(0);
1555                    }
1556    
1557                    return null;
1558            }
1559    
1560            /**
1561             * Returns the last resource permission in the ordered set where companyId = &#63; and primKey LIKE &#63;.
1562             *
1563             * @param companyId the company ID
1564             * @param primKey the prim key
1565             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1566             * @return the last matching resource permission
1567             * @throws com.liferay.portal.NoSuchResourcePermissionException if a matching resource permission could not be found
1568             * @throws SystemException if a system exception occurred
1569             */
1570            @Override
1571            public ResourcePermission findByC_LikeP_Last(long companyId,
1572                    String primKey, OrderByComparator orderByComparator)
1573                    throws NoSuchResourcePermissionException, SystemException {
1574                    ResourcePermission resourcePermission = fetchByC_LikeP_Last(companyId,
1575                                    primKey, orderByComparator);
1576    
1577                    if (resourcePermission != null) {
1578                            return resourcePermission;
1579                    }
1580    
1581                    StringBundler msg = new StringBundler(6);
1582    
1583                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1584    
1585                    msg.append("companyId=");
1586                    msg.append(companyId);
1587    
1588                    msg.append(", primKey=");
1589                    msg.append(primKey);
1590    
1591                    msg.append(StringPool.CLOSE_CURLY_BRACE);
1592    
1593                    throw new NoSuchResourcePermissionException(msg.toString());
1594            }
1595    
1596            /**
1597             * Returns the last resource permission in the ordered set where companyId = &#63; and primKey LIKE &#63;.
1598             *
1599             * @param companyId the company ID
1600             * @param primKey the prim key
1601             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1602             * @return the last matching resource permission, or <code>null</code> if a matching resource permission could not be found
1603             * @throws SystemException if a system exception occurred
1604             */
1605            @Override
1606            public ResourcePermission fetchByC_LikeP_Last(long companyId,
1607                    String primKey, OrderByComparator orderByComparator)
1608                    throws SystemException {
1609                    int count = countByC_LikeP(companyId, primKey);
1610    
1611                    if (count == 0) {
1612                            return null;
1613                    }
1614    
1615                    List<ResourcePermission> list = findByC_LikeP(companyId, primKey,
1616                                    count - 1, count, orderByComparator);
1617    
1618                    if (!list.isEmpty()) {
1619                            return list.get(0);
1620                    }
1621    
1622                    return null;
1623            }
1624    
1625            /**
1626             * Returns the resource permissions before and after the current resource permission in the ordered set where companyId = &#63; and primKey LIKE &#63;.
1627             *
1628             * @param resourcePermissionId the primary key of the current resource permission
1629             * @param companyId the company ID
1630             * @param primKey the prim key
1631             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1632             * @return the previous, current, and next resource permission
1633             * @throws com.liferay.portal.NoSuchResourcePermissionException if a resource permission with the primary key could not be found
1634             * @throws SystemException if a system exception occurred
1635             */
1636            @Override
1637            public ResourcePermission[] findByC_LikeP_PrevAndNext(
1638                    long resourcePermissionId, long companyId, String primKey,
1639                    OrderByComparator orderByComparator)
1640                    throws NoSuchResourcePermissionException, SystemException {
1641                    ResourcePermission resourcePermission = findByPrimaryKey(resourcePermissionId);
1642    
1643                    Session session = null;
1644    
1645                    try {
1646                            session = openSession();
1647    
1648                            ResourcePermission[] array = new ResourcePermissionImpl[3];
1649    
1650                            array[0] = getByC_LikeP_PrevAndNext(session, resourcePermission,
1651                                            companyId, primKey, orderByComparator, true);
1652    
1653                            array[1] = resourcePermission;
1654    
1655                            array[2] = getByC_LikeP_PrevAndNext(session, resourcePermission,
1656                                            companyId, primKey, orderByComparator, false);
1657    
1658                            return array;
1659                    }
1660                    catch (Exception e) {
1661                            throw processException(e);
1662                    }
1663                    finally {
1664                            closeSession(session);
1665                    }
1666            }
1667    
1668            protected ResourcePermission getByC_LikeP_PrevAndNext(Session session,
1669                    ResourcePermission resourcePermission, long companyId, String primKey,
1670                    OrderByComparator orderByComparator, boolean previous) {
1671                    StringBundler query = null;
1672    
1673                    if (orderByComparator != null) {
1674                            query = new StringBundler(6 +
1675                                            (orderByComparator.getOrderByFields().length * 6));
1676                    }
1677                    else {
1678                            query = new StringBundler(3);
1679                    }
1680    
1681                    query.append(_SQL_SELECT_RESOURCEPERMISSION_WHERE);
1682    
1683                    query.append(_FINDER_COLUMN_C_LIKEP_COMPANYID_2);
1684    
1685                    boolean bindPrimKey = false;
1686    
1687                    if (primKey == null) {
1688                            query.append(_FINDER_COLUMN_C_LIKEP_PRIMKEY_1);
1689                    }
1690                    else if (primKey.equals(StringPool.BLANK)) {
1691                            query.append(_FINDER_COLUMN_C_LIKEP_PRIMKEY_3);
1692                    }
1693                    else {
1694                            bindPrimKey = true;
1695    
1696                            query.append(_FINDER_COLUMN_C_LIKEP_PRIMKEY_2);
1697                    }
1698    
1699                    if (orderByComparator != null) {
1700                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
1701    
1702                            if (orderByConditionFields.length > 0) {
1703                                    query.append(WHERE_AND);
1704                            }
1705    
1706                            for (int i = 0; i < orderByConditionFields.length; i++) {
1707                                    query.append(_ORDER_BY_ENTITY_ALIAS);
1708                                    query.append(orderByConditionFields[i]);
1709    
1710                                    if ((i + 1) < orderByConditionFields.length) {
1711                                            if (orderByComparator.isAscending() ^ previous) {
1712                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
1713                                            }
1714                                            else {
1715                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
1716                                            }
1717                                    }
1718                                    else {
1719                                            if (orderByComparator.isAscending() ^ previous) {
1720                                                    query.append(WHERE_GREATER_THAN);
1721                                            }
1722                                            else {
1723                                                    query.append(WHERE_LESSER_THAN);
1724                                            }
1725                                    }
1726                            }
1727    
1728                            query.append(ORDER_BY_CLAUSE);
1729    
1730                            String[] orderByFields = orderByComparator.getOrderByFields();
1731    
1732                            for (int i = 0; i < orderByFields.length; i++) {
1733                                    query.append(_ORDER_BY_ENTITY_ALIAS);
1734                                    query.append(orderByFields[i]);
1735    
1736                                    if ((i + 1) < orderByFields.length) {
1737                                            if (orderByComparator.isAscending() ^ previous) {
1738                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
1739                                            }
1740                                            else {
1741                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
1742                                            }
1743                                    }
1744                                    else {
1745                                            if (orderByComparator.isAscending() ^ previous) {
1746                                                    query.append(ORDER_BY_ASC);
1747                                            }
1748                                            else {
1749                                                    query.append(ORDER_BY_DESC);
1750                                            }
1751                                    }
1752                            }
1753                    }
1754                    else {
1755                            query.append(ResourcePermissionModelImpl.ORDER_BY_JPQL);
1756                    }
1757    
1758                    String sql = query.toString();
1759    
1760                    Query q = session.createQuery(sql);
1761    
1762                    q.setFirstResult(0);
1763                    q.setMaxResults(2);
1764    
1765                    QueryPos qPos = QueryPos.getInstance(q);
1766    
1767                    qPos.add(companyId);
1768    
1769                    if (bindPrimKey) {
1770                            qPos.add(primKey);
1771                    }
1772    
1773                    if (orderByComparator != null) {
1774                            Object[] values = orderByComparator.getOrderByConditionValues(resourcePermission);
1775    
1776                            for (Object value : values) {
1777                                    qPos.add(value);
1778                            }
1779                    }
1780    
1781                    List<ResourcePermission> list = q.list();
1782    
1783                    if (list.size() == 2) {
1784                            return list.get(1);
1785                    }
1786                    else {
1787                            return null;
1788                    }
1789            }
1790    
1791            /**
1792             * Removes all the resource permissions where companyId = &#63; and primKey LIKE &#63; from the database.
1793             *
1794             * @param companyId the company ID
1795             * @param primKey the prim key
1796             * @throws SystemException if a system exception occurred
1797             */
1798            @Override
1799            public void removeByC_LikeP(long companyId, String primKey)
1800                    throws SystemException {
1801                    for (ResourcePermission resourcePermission : findByC_LikeP(companyId,
1802                                    primKey, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
1803                            remove(resourcePermission);
1804                    }
1805            }
1806    
1807            /**
1808             * Returns the number of resource permissions where companyId = &#63; and primKey LIKE &#63;.
1809             *
1810             * @param companyId the company ID
1811             * @param primKey the prim key
1812             * @return the number of matching resource permissions
1813             * @throws SystemException if a system exception occurred
1814             */
1815            @Override
1816            public int countByC_LikeP(long companyId, String primKey)
1817                    throws SystemException {
1818                    FinderPath finderPath = FINDER_PATH_WITH_PAGINATION_COUNT_BY_C_LIKEP;
1819    
1820                    Object[] finderArgs = new Object[] { companyId, primKey };
1821    
1822                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
1823                                    this);
1824    
1825                    if (count == null) {
1826                            StringBundler query = new StringBundler(3);
1827    
1828                            query.append(_SQL_COUNT_RESOURCEPERMISSION_WHERE);
1829    
1830                            query.append(_FINDER_COLUMN_C_LIKEP_COMPANYID_2);
1831    
1832                            boolean bindPrimKey = false;
1833    
1834                            if (primKey == null) {
1835                                    query.append(_FINDER_COLUMN_C_LIKEP_PRIMKEY_1);
1836                            }
1837                            else if (primKey.equals(StringPool.BLANK)) {
1838                                    query.append(_FINDER_COLUMN_C_LIKEP_PRIMKEY_3);
1839                            }
1840                            else {
1841                                    bindPrimKey = true;
1842    
1843                                    query.append(_FINDER_COLUMN_C_LIKEP_PRIMKEY_2);
1844                            }
1845    
1846                            String sql = query.toString();
1847    
1848                            Session session = null;
1849    
1850                            try {
1851                                    session = openSession();
1852    
1853                                    Query q = session.createQuery(sql);
1854    
1855                                    QueryPos qPos = QueryPos.getInstance(q);
1856    
1857                                    qPos.add(companyId);
1858    
1859                                    if (bindPrimKey) {
1860                                            qPos.add(primKey);
1861                                    }
1862    
1863                                    count = (Long)q.uniqueResult();
1864    
1865                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
1866                            }
1867                            catch (Exception e) {
1868                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
1869    
1870                                    throw processException(e);
1871                            }
1872                            finally {
1873                                    closeSession(session);
1874                            }
1875                    }
1876    
1877                    return count.intValue();
1878            }
1879    
1880            private static final String _FINDER_COLUMN_C_LIKEP_COMPANYID_2 = "resourcePermission.companyId = ? AND ";
1881            private static final String _FINDER_COLUMN_C_LIKEP_PRIMKEY_1 = "resourcePermission.primKey LIKE NULL";
1882            private static final String _FINDER_COLUMN_C_LIKEP_PRIMKEY_2 = "resourcePermission.primKey LIKE ?";
1883            private static final String _FINDER_COLUMN_C_LIKEP_PRIMKEY_3 = "(resourcePermission.primKey IS NULL OR resourcePermission.primKey LIKE '')";
1884            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_C_N_S = new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
1885                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED,
1886                            ResourcePermissionImpl.class,
1887                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByC_N_S",
1888                            new String[] {
1889                                    Long.class.getName(), String.class.getName(),
1890                                    Integer.class.getName(),
1891                                    
1892                            Integer.class.getName(), Integer.class.getName(),
1893                                    OrderByComparator.class.getName()
1894                            });
1895            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_N_S = new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
1896                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED,
1897                            ResourcePermissionImpl.class,
1898                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByC_N_S",
1899                            new String[] {
1900                                    Long.class.getName(), String.class.getName(),
1901                                    Integer.class.getName()
1902                            },
1903                            ResourcePermissionModelImpl.COMPANYID_COLUMN_BITMASK |
1904                            ResourcePermissionModelImpl.NAME_COLUMN_BITMASK |
1905                            ResourcePermissionModelImpl.SCOPE_COLUMN_BITMASK);
1906            public static final FinderPath FINDER_PATH_COUNT_BY_C_N_S = new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
1907                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED, Long.class,
1908                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByC_N_S",
1909                            new String[] {
1910                                    Long.class.getName(), String.class.getName(),
1911                                    Integer.class.getName()
1912                            });
1913    
1914            /**
1915             * Returns all the resource permissions where companyId = &#63; and name = &#63; and scope = &#63;.
1916             *
1917             * @param companyId the company ID
1918             * @param name the name
1919             * @param scope the scope
1920             * @return the matching resource permissions
1921             * @throws SystemException if a system exception occurred
1922             */
1923            @Override
1924            public List<ResourcePermission> findByC_N_S(long companyId, String name,
1925                    int scope) throws SystemException {
1926                    return findByC_N_S(companyId, name, scope, QueryUtil.ALL_POS,
1927                            QueryUtil.ALL_POS, null);
1928            }
1929    
1930            /**
1931             * Returns a range of all the resource permissions where companyId = &#63; and name = &#63; and scope = &#63;.
1932             *
1933             * <p>
1934             * 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. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portal.model.impl.ResourcePermissionModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
1935             * </p>
1936             *
1937             * @param companyId the company ID
1938             * @param name the name
1939             * @param scope the scope
1940             * @param start the lower bound of the range of resource permissions
1941             * @param end the upper bound of the range of resource permissions (not inclusive)
1942             * @return the range of matching resource permissions
1943             * @throws SystemException if a system exception occurred
1944             */
1945            @Override
1946            public List<ResourcePermission> findByC_N_S(long companyId, String name,
1947                    int scope, int start, int end) throws SystemException {
1948                    return findByC_N_S(companyId, name, scope, start, end, null);
1949            }
1950    
1951            /**
1952             * Returns an ordered range of all the resource permissions where companyId = &#63; and name = &#63; and scope = &#63;.
1953             *
1954             * <p>
1955             * 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. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portal.model.impl.ResourcePermissionModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
1956             * </p>
1957             *
1958             * @param companyId the company ID
1959             * @param name the name
1960             * @param scope the scope
1961             * @param start the lower bound of the range of resource permissions
1962             * @param end the upper bound of the range of resource permissions (not inclusive)
1963             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
1964             * @return the ordered range of matching resource permissions
1965             * @throws SystemException if a system exception occurred
1966             */
1967            @Override
1968            public List<ResourcePermission> findByC_N_S(long companyId, String name,
1969                    int scope, int start, int end, OrderByComparator orderByComparator)
1970                    throws SystemException {
1971                    boolean pagination = true;
1972                    FinderPath finderPath = null;
1973                    Object[] finderArgs = null;
1974    
1975                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
1976                                    (orderByComparator == null)) {
1977                            pagination = false;
1978                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_N_S;
1979                            finderArgs = new Object[] { companyId, name, scope };
1980                    }
1981                    else {
1982                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_C_N_S;
1983                            finderArgs = new Object[] {
1984                                            companyId, name, scope,
1985                                            
1986                                            start, end, orderByComparator
1987                                    };
1988                    }
1989    
1990                    List<ResourcePermission> list = (List<ResourcePermission>)FinderCacheUtil.getResult(finderPath,
1991                                    finderArgs, this);
1992    
1993                    if ((list != null) && !list.isEmpty()) {
1994                            for (ResourcePermission resourcePermission : list) {
1995                                    if ((companyId != resourcePermission.getCompanyId()) ||
1996                                                    !Validator.equals(name, resourcePermission.getName()) ||
1997                                                    (scope != resourcePermission.getScope())) {
1998                                            list = null;
1999    
2000                                            break;
2001                                    }
2002                            }
2003                    }
2004    
2005                    if (list == null) {
2006                            StringBundler query = null;
2007    
2008                            if (orderByComparator != null) {
2009                                    query = new StringBundler(5 +
2010                                                    (orderByComparator.getOrderByFields().length * 3));
2011                            }
2012                            else {
2013                                    query = new StringBundler(5);
2014                            }
2015    
2016                            query.append(_SQL_SELECT_RESOURCEPERMISSION_WHERE);
2017    
2018                            query.append(_FINDER_COLUMN_C_N_S_COMPANYID_2);
2019    
2020                            boolean bindName = false;
2021    
2022                            if (name == null) {
2023                                    query.append(_FINDER_COLUMN_C_N_S_NAME_1);
2024                            }
2025                            else if (name.equals(StringPool.BLANK)) {
2026                                    query.append(_FINDER_COLUMN_C_N_S_NAME_3);
2027                            }
2028                            else {
2029                                    bindName = true;
2030    
2031                                    query.append(_FINDER_COLUMN_C_N_S_NAME_2);
2032                            }
2033    
2034                            query.append(_FINDER_COLUMN_C_N_S_SCOPE_2);
2035    
2036                            if (orderByComparator != null) {
2037                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
2038                                            orderByComparator);
2039                            }
2040                            else
2041                             if (pagination) {
2042                                    query.append(ResourcePermissionModelImpl.ORDER_BY_JPQL);
2043                            }
2044    
2045                            String sql = query.toString();
2046    
2047                            Session session = null;
2048    
2049                            try {
2050                                    session = openSession();
2051    
2052                                    Query q = session.createQuery(sql);
2053    
2054                                    QueryPos qPos = QueryPos.getInstance(q);
2055    
2056                                    qPos.add(companyId);
2057    
2058                                    if (bindName) {
2059                                            qPos.add(name);
2060                                    }
2061    
2062                                    qPos.add(scope);
2063    
2064                                    if (!pagination) {
2065                                            list = (List<ResourcePermission>)QueryUtil.list(q,
2066                                                            getDialect(), start, end, false);
2067    
2068                                            Collections.sort(list);
2069    
2070                                            list = new UnmodifiableList<ResourcePermission>(list);
2071                                    }
2072                                    else {
2073                                            list = (List<ResourcePermission>)QueryUtil.list(q,
2074                                                            getDialect(), start, end);
2075                                    }
2076    
2077                                    cacheResult(list);
2078    
2079                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
2080                            }
2081                            catch (Exception e) {
2082                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
2083    
2084                                    throw processException(e);
2085                            }
2086                            finally {
2087                                    closeSession(session);
2088                            }
2089                    }
2090    
2091                    return list;
2092            }
2093    
2094            /**
2095             * Returns the first resource permission in the ordered set where companyId = &#63; and name = &#63; and scope = &#63;.
2096             *
2097             * @param companyId the company ID
2098             * @param name the name
2099             * @param scope the scope
2100             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2101             * @return the first matching resource permission
2102             * @throws com.liferay.portal.NoSuchResourcePermissionException if a matching resource permission could not be found
2103             * @throws SystemException if a system exception occurred
2104             */
2105            @Override
2106            public ResourcePermission findByC_N_S_First(long companyId, String name,
2107                    int scope, OrderByComparator orderByComparator)
2108                    throws NoSuchResourcePermissionException, SystemException {
2109                    ResourcePermission resourcePermission = fetchByC_N_S_First(companyId,
2110                                    name, scope, orderByComparator);
2111    
2112                    if (resourcePermission != null) {
2113                            return resourcePermission;
2114                    }
2115    
2116                    StringBundler msg = new StringBundler(8);
2117    
2118                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
2119    
2120                    msg.append("companyId=");
2121                    msg.append(companyId);
2122    
2123                    msg.append(", name=");
2124                    msg.append(name);
2125    
2126                    msg.append(", scope=");
2127                    msg.append(scope);
2128    
2129                    msg.append(StringPool.CLOSE_CURLY_BRACE);
2130    
2131                    throw new NoSuchResourcePermissionException(msg.toString());
2132            }
2133    
2134            /**
2135             * Returns the first resource permission in the ordered set where companyId = &#63; and name = &#63; and scope = &#63;.
2136             *
2137             * @param companyId the company ID
2138             * @param name the name
2139             * @param scope the scope
2140             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2141             * @return the first matching resource permission, or <code>null</code> if a matching resource permission could not be found
2142             * @throws SystemException if a system exception occurred
2143             */
2144            @Override
2145            public ResourcePermission fetchByC_N_S_First(long companyId, String name,
2146                    int scope, OrderByComparator orderByComparator)
2147                    throws SystemException {
2148                    List<ResourcePermission> list = findByC_N_S(companyId, name, scope, 0,
2149                                    1, orderByComparator);
2150    
2151                    if (!list.isEmpty()) {
2152                            return list.get(0);
2153                    }
2154    
2155                    return null;
2156            }
2157    
2158            /**
2159             * Returns the last resource permission in the ordered set where companyId = &#63; and name = &#63; and scope = &#63;.
2160             *
2161             * @param companyId the company ID
2162             * @param name the name
2163             * @param scope the scope
2164             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2165             * @return the last matching resource permission
2166             * @throws com.liferay.portal.NoSuchResourcePermissionException if a matching resource permission could not be found
2167             * @throws SystemException if a system exception occurred
2168             */
2169            @Override
2170            public ResourcePermission findByC_N_S_Last(long companyId, String name,
2171                    int scope, OrderByComparator orderByComparator)
2172                    throws NoSuchResourcePermissionException, SystemException {
2173                    ResourcePermission resourcePermission = fetchByC_N_S_Last(companyId,
2174                                    name, scope, orderByComparator);
2175    
2176                    if (resourcePermission != null) {
2177                            return resourcePermission;
2178                    }
2179    
2180                    StringBundler msg = new StringBundler(8);
2181    
2182                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
2183    
2184                    msg.append("companyId=");
2185                    msg.append(companyId);
2186    
2187                    msg.append(", name=");
2188                    msg.append(name);
2189    
2190                    msg.append(", scope=");
2191                    msg.append(scope);
2192    
2193                    msg.append(StringPool.CLOSE_CURLY_BRACE);
2194    
2195                    throw new NoSuchResourcePermissionException(msg.toString());
2196            }
2197    
2198            /**
2199             * Returns the last resource permission in the ordered set where companyId = &#63; and name = &#63; and scope = &#63;.
2200             *
2201             * @param companyId the company ID
2202             * @param name the name
2203             * @param scope the scope
2204             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2205             * @return the last matching resource permission, or <code>null</code> if a matching resource permission could not be found
2206             * @throws SystemException if a system exception occurred
2207             */
2208            @Override
2209            public ResourcePermission fetchByC_N_S_Last(long companyId, String name,
2210                    int scope, OrderByComparator orderByComparator)
2211                    throws SystemException {
2212                    int count = countByC_N_S(companyId, name, scope);
2213    
2214                    if (count == 0) {
2215                            return null;
2216                    }
2217    
2218                    List<ResourcePermission> list = findByC_N_S(companyId, name, scope,
2219                                    count - 1, count, orderByComparator);
2220    
2221                    if (!list.isEmpty()) {
2222                            return list.get(0);
2223                    }
2224    
2225                    return null;
2226            }
2227    
2228            /**
2229             * Returns the resource permissions before and after the current resource permission in the ordered set where companyId = &#63; and name = &#63; and scope = &#63;.
2230             *
2231             * @param resourcePermissionId the primary key of the current resource permission
2232             * @param companyId the company ID
2233             * @param name the name
2234             * @param scope the scope
2235             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2236             * @return the previous, current, and next resource permission
2237             * @throws com.liferay.portal.NoSuchResourcePermissionException if a resource permission with the primary key could not be found
2238             * @throws SystemException if a system exception occurred
2239             */
2240            @Override
2241            public ResourcePermission[] findByC_N_S_PrevAndNext(
2242                    long resourcePermissionId, long companyId, String name, int scope,
2243                    OrderByComparator orderByComparator)
2244                    throws NoSuchResourcePermissionException, SystemException {
2245                    ResourcePermission resourcePermission = findByPrimaryKey(resourcePermissionId);
2246    
2247                    Session session = null;
2248    
2249                    try {
2250                            session = openSession();
2251    
2252                            ResourcePermission[] array = new ResourcePermissionImpl[3];
2253    
2254                            array[0] = getByC_N_S_PrevAndNext(session, resourcePermission,
2255                                            companyId, name, scope, orderByComparator, true);
2256    
2257                            array[1] = resourcePermission;
2258    
2259                            array[2] = getByC_N_S_PrevAndNext(session, resourcePermission,
2260                                            companyId, name, scope, orderByComparator, false);
2261    
2262                            return array;
2263                    }
2264                    catch (Exception e) {
2265                            throw processException(e);
2266                    }
2267                    finally {
2268                            closeSession(session);
2269                    }
2270            }
2271    
2272            protected ResourcePermission getByC_N_S_PrevAndNext(Session session,
2273                    ResourcePermission resourcePermission, long companyId, String name,
2274                    int scope, OrderByComparator orderByComparator, boolean previous) {
2275                    StringBundler query = null;
2276    
2277                    if (orderByComparator != null) {
2278                            query = new StringBundler(6 +
2279                                            (orderByComparator.getOrderByFields().length * 6));
2280                    }
2281                    else {
2282                            query = new StringBundler(3);
2283                    }
2284    
2285                    query.append(_SQL_SELECT_RESOURCEPERMISSION_WHERE);
2286    
2287                    query.append(_FINDER_COLUMN_C_N_S_COMPANYID_2);
2288    
2289                    boolean bindName = false;
2290    
2291                    if (name == null) {
2292                            query.append(_FINDER_COLUMN_C_N_S_NAME_1);
2293                    }
2294                    else if (name.equals(StringPool.BLANK)) {
2295                            query.append(_FINDER_COLUMN_C_N_S_NAME_3);
2296                    }
2297                    else {
2298                            bindName = true;
2299    
2300                            query.append(_FINDER_COLUMN_C_N_S_NAME_2);
2301                    }
2302    
2303                    query.append(_FINDER_COLUMN_C_N_S_SCOPE_2);
2304    
2305                    if (orderByComparator != null) {
2306                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
2307    
2308                            if (orderByConditionFields.length > 0) {
2309                                    query.append(WHERE_AND);
2310                            }
2311    
2312                            for (int i = 0; i < orderByConditionFields.length; i++) {
2313                                    query.append(_ORDER_BY_ENTITY_ALIAS);
2314                                    query.append(orderByConditionFields[i]);
2315    
2316                                    if ((i + 1) < orderByConditionFields.length) {
2317                                            if (orderByComparator.isAscending() ^ previous) {
2318                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
2319                                            }
2320                                            else {
2321                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
2322                                            }
2323                                    }
2324                                    else {
2325                                            if (orderByComparator.isAscending() ^ previous) {
2326                                                    query.append(WHERE_GREATER_THAN);
2327                                            }
2328                                            else {
2329                                                    query.append(WHERE_LESSER_THAN);
2330                                            }
2331                                    }
2332                            }
2333    
2334                            query.append(ORDER_BY_CLAUSE);
2335    
2336                            String[] orderByFields = orderByComparator.getOrderByFields();
2337    
2338                            for (int i = 0; i < orderByFields.length; i++) {
2339                                    query.append(_ORDER_BY_ENTITY_ALIAS);
2340                                    query.append(orderByFields[i]);
2341    
2342                                    if ((i + 1) < orderByFields.length) {
2343                                            if (orderByComparator.isAscending() ^ previous) {
2344                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
2345                                            }
2346                                            else {
2347                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
2348                                            }
2349                                    }
2350                                    else {
2351                                            if (orderByComparator.isAscending() ^ previous) {
2352                                                    query.append(ORDER_BY_ASC);
2353                                            }
2354                                            else {
2355                                                    query.append(ORDER_BY_DESC);
2356                                            }
2357                                    }
2358                            }
2359                    }
2360                    else {
2361                            query.append(ResourcePermissionModelImpl.ORDER_BY_JPQL);
2362                    }
2363    
2364                    String sql = query.toString();
2365    
2366                    Query q = session.createQuery(sql);
2367    
2368                    q.setFirstResult(0);
2369                    q.setMaxResults(2);
2370    
2371                    QueryPos qPos = QueryPos.getInstance(q);
2372    
2373                    qPos.add(companyId);
2374    
2375                    if (bindName) {
2376                            qPos.add(name);
2377                    }
2378    
2379                    qPos.add(scope);
2380    
2381                    if (orderByComparator != null) {
2382                            Object[] values = orderByComparator.getOrderByConditionValues(resourcePermission);
2383    
2384                            for (Object value : values) {
2385                                    qPos.add(value);
2386                            }
2387                    }
2388    
2389                    List<ResourcePermission> list = q.list();
2390    
2391                    if (list.size() == 2) {
2392                            return list.get(1);
2393                    }
2394                    else {
2395                            return null;
2396                    }
2397            }
2398    
2399            /**
2400             * Removes all the resource permissions where companyId = &#63; and name = &#63; and scope = &#63; from the database.
2401             *
2402             * @param companyId the company ID
2403             * @param name the name
2404             * @param scope the scope
2405             * @throws SystemException if a system exception occurred
2406             */
2407            @Override
2408            public void removeByC_N_S(long companyId, String name, int scope)
2409                    throws SystemException {
2410                    for (ResourcePermission resourcePermission : findByC_N_S(companyId,
2411                                    name, scope, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
2412                            remove(resourcePermission);
2413                    }
2414            }
2415    
2416            /**
2417             * Returns the number of resource permissions where companyId = &#63; and name = &#63; and scope = &#63;.
2418             *
2419             * @param companyId the company ID
2420             * @param name the name
2421             * @param scope the scope
2422             * @return the number of matching resource permissions
2423             * @throws SystemException if a system exception occurred
2424             */
2425            @Override
2426            public int countByC_N_S(long companyId, String name, int scope)
2427                    throws SystemException {
2428                    FinderPath finderPath = FINDER_PATH_COUNT_BY_C_N_S;
2429    
2430                    Object[] finderArgs = new Object[] { companyId, name, scope };
2431    
2432                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
2433                                    this);
2434    
2435                    if (count == null) {
2436                            StringBundler query = new StringBundler(4);
2437    
2438                            query.append(_SQL_COUNT_RESOURCEPERMISSION_WHERE);
2439    
2440                            query.append(_FINDER_COLUMN_C_N_S_COMPANYID_2);
2441    
2442                            boolean bindName = false;
2443    
2444                            if (name == null) {
2445                                    query.append(_FINDER_COLUMN_C_N_S_NAME_1);
2446                            }
2447                            else if (name.equals(StringPool.BLANK)) {
2448                                    query.append(_FINDER_COLUMN_C_N_S_NAME_3);
2449                            }
2450                            else {
2451                                    bindName = true;
2452    
2453                                    query.append(_FINDER_COLUMN_C_N_S_NAME_2);
2454                            }
2455    
2456                            query.append(_FINDER_COLUMN_C_N_S_SCOPE_2);
2457    
2458                            String sql = query.toString();
2459    
2460                            Session session = null;
2461    
2462                            try {
2463                                    session = openSession();
2464    
2465                                    Query q = session.createQuery(sql);
2466    
2467                                    QueryPos qPos = QueryPos.getInstance(q);
2468    
2469                                    qPos.add(companyId);
2470    
2471                                    if (bindName) {
2472                                            qPos.add(name);
2473                                    }
2474    
2475                                    qPos.add(scope);
2476    
2477                                    count = (Long)q.uniqueResult();
2478    
2479                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
2480                            }
2481                            catch (Exception e) {
2482                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
2483    
2484                                    throw processException(e);
2485                            }
2486                            finally {
2487                                    closeSession(session);
2488                            }
2489                    }
2490    
2491                    return count.intValue();
2492            }
2493    
2494            private static final String _FINDER_COLUMN_C_N_S_COMPANYID_2 = "resourcePermission.companyId = ? AND ";
2495            private static final String _FINDER_COLUMN_C_N_S_NAME_1 = "resourcePermission.name IS NULL AND ";
2496            private static final String _FINDER_COLUMN_C_N_S_NAME_2 = "resourcePermission.name = ? AND ";
2497            private static final String _FINDER_COLUMN_C_N_S_NAME_3 = "(resourcePermission.name IS NULL OR resourcePermission.name = '') AND ";
2498            private static final String _FINDER_COLUMN_C_N_S_SCOPE_2 = "resourcePermission.scope = ?";
2499            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_C_N_S_P = new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
2500                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED,
2501                            ResourcePermissionImpl.class,
2502                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByC_N_S_P",
2503                            new String[] {
2504                                    Long.class.getName(), String.class.getName(),
2505                                    Integer.class.getName(), String.class.getName(),
2506                                    
2507                            Integer.class.getName(), Integer.class.getName(),
2508                                    OrderByComparator.class.getName()
2509                            });
2510            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_N_S_P =
2511                    new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
2512                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED,
2513                            ResourcePermissionImpl.class,
2514                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByC_N_S_P",
2515                            new String[] {
2516                                    Long.class.getName(), String.class.getName(),
2517                                    Integer.class.getName(), String.class.getName()
2518                            },
2519                            ResourcePermissionModelImpl.COMPANYID_COLUMN_BITMASK |
2520                            ResourcePermissionModelImpl.NAME_COLUMN_BITMASK |
2521                            ResourcePermissionModelImpl.SCOPE_COLUMN_BITMASK |
2522                            ResourcePermissionModelImpl.PRIMKEY_COLUMN_BITMASK);
2523            public static final FinderPath FINDER_PATH_COUNT_BY_C_N_S_P = new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
2524                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED, Long.class,
2525                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByC_N_S_P",
2526                            new String[] {
2527                                    Long.class.getName(), String.class.getName(),
2528                                    Integer.class.getName(), String.class.getName()
2529                            });
2530    
2531            /**
2532             * Returns all the resource permissions where companyId = &#63; and name = &#63; and scope = &#63; and primKey = &#63;.
2533             *
2534             * @param companyId the company ID
2535             * @param name the name
2536             * @param scope the scope
2537             * @param primKey the prim key
2538             * @return the matching resource permissions
2539             * @throws SystemException if a system exception occurred
2540             */
2541            @Override
2542            public List<ResourcePermission> findByC_N_S_P(long companyId, String name,
2543                    int scope, String primKey) throws SystemException {
2544                    return findByC_N_S_P(companyId, name, scope, primKey,
2545                            QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
2546            }
2547    
2548            /**
2549             * Returns a range of all the resource permissions where companyId = &#63; and name = &#63; and scope = &#63; and primKey = &#63;.
2550             *
2551             * <p>
2552             * 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. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portal.model.impl.ResourcePermissionModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
2553             * </p>
2554             *
2555             * @param companyId the company ID
2556             * @param name the name
2557             * @param scope the scope
2558             * @param primKey the prim key
2559             * @param start the lower bound of the range of resource permissions
2560             * @param end the upper bound of the range of resource permissions (not inclusive)
2561             * @return the range of matching resource permissions
2562             * @throws SystemException if a system exception occurred
2563             */
2564            @Override
2565            public List<ResourcePermission> findByC_N_S_P(long companyId, String name,
2566                    int scope, String primKey, int start, int end)
2567                    throws SystemException {
2568                    return findByC_N_S_P(companyId, name, scope, primKey, start, end, null);
2569            }
2570    
2571            /**
2572             * Returns an ordered range of all the resource permissions where companyId = &#63; and name = &#63; and scope = &#63; and primKey = &#63;.
2573             *
2574             * <p>
2575             * 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. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portal.model.impl.ResourcePermissionModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
2576             * </p>
2577             *
2578             * @param companyId the company ID
2579             * @param name the name
2580             * @param scope the scope
2581             * @param primKey the prim key
2582             * @param start the lower bound of the range of resource permissions
2583             * @param end the upper bound of the range of resource permissions (not inclusive)
2584             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
2585             * @return the ordered range of matching resource permissions
2586             * @throws SystemException if a system exception occurred
2587             */
2588            @Override
2589            public List<ResourcePermission> findByC_N_S_P(long companyId, String name,
2590                    int scope, String primKey, int start, int end,
2591                    OrderByComparator orderByComparator) throws SystemException {
2592                    boolean pagination = true;
2593                    FinderPath finderPath = null;
2594                    Object[] finderArgs = null;
2595    
2596                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
2597                                    (orderByComparator == null)) {
2598                            pagination = false;
2599                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_N_S_P;
2600                            finderArgs = new Object[] { companyId, name, scope, primKey };
2601                    }
2602                    else {
2603                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_C_N_S_P;
2604                            finderArgs = new Object[] {
2605                                            companyId, name, scope, primKey,
2606                                            
2607                                            start, end, orderByComparator
2608                                    };
2609                    }
2610    
2611                    List<ResourcePermission> list = (List<ResourcePermission>)FinderCacheUtil.getResult(finderPath,
2612                                    finderArgs, this);
2613    
2614                    if ((list != null) && !list.isEmpty()) {
2615                            for (ResourcePermission resourcePermission : list) {
2616                                    if ((companyId != resourcePermission.getCompanyId()) ||
2617                                                    !Validator.equals(name, resourcePermission.getName()) ||
2618                                                    (scope != resourcePermission.getScope()) ||
2619                                                    !Validator.equals(primKey,
2620                                                            resourcePermission.getPrimKey())) {
2621                                            list = null;
2622    
2623                                            break;
2624                                    }
2625                            }
2626                    }
2627    
2628                    if (list == null) {
2629                            StringBundler query = null;
2630    
2631                            if (orderByComparator != null) {
2632                                    query = new StringBundler(6 +
2633                                                    (orderByComparator.getOrderByFields().length * 3));
2634                            }
2635                            else {
2636                                    query = new StringBundler(6);
2637                            }
2638    
2639                            query.append(_SQL_SELECT_RESOURCEPERMISSION_WHERE);
2640    
2641                            query.append(_FINDER_COLUMN_C_N_S_P_COMPANYID_2);
2642    
2643                            boolean bindName = false;
2644    
2645                            if (name == null) {
2646                                    query.append(_FINDER_COLUMN_C_N_S_P_NAME_1);
2647                            }
2648                            else if (name.equals(StringPool.BLANK)) {
2649                                    query.append(_FINDER_COLUMN_C_N_S_P_NAME_3);
2650                            }
2651                            else {
2652                                    bindName = true;
2653    
2654                                    query.append(_FINDER_COLUMN_C_N_S_P_NAME_2);
2655                            }
2656    
2657                            query.append(_FINDER_COLUMN_C_N_S_P_SCOPE_2);
2658    
2659                            boolean bindPrimKey = false;
2660    
2661                            if (primKey == null) {
2662                                    query.append(_FINDER_COLUMN_C_N_S_P_PRIMKEY_1);
2663                            }
2664                            else if (primKey.equals(StringPool.BLANK)) {
2665                                    query.append(_FINDER_COLUMN_C_N_S_P_PRIMKEY_3);
2666                            }
2667                            else {
2668                                    bindPrimKey = true;
2669    
2670                                    query.append(_FINDER_COLUMN_C_N_S_P_PRIMKEY_2);
2671                            }
2672    
2673                            if (orderByComparator != null) {
2674                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
2675                                            orderByComparator);
2676                            }
2677                            else
2678                             if (pagination) {
2679                                    query.append(ResourcePermissionModelImpl.ORDER_BY_JPQL);
2680                            }
2681    
2682                            String sql = query.toString();
2683    
2684                            Session session = null;
2685    
2686                            try {
2687                                    session = openSession();
2688    
2689                                    Query q = session.createQuery(sql);
2690    
2691                                    QueryPos qPos = QueryPos.getInstance(q);
2692    
2693                                    qPos.add(companyId);
2694    
2695                                    if (bindName) {
2696                                            qPos.add(name);
2697                                    }
2698    
2699                                    qPos.add(scope);
2700    
2701                                    if (bindPrimKey) {
2702                                            qPos.add(primKey);
2703                                    }
2704    
2705                                    if (!pagination) {
2706                                            list = (List<ResourcePermission>)QueryUtil.list(q,
2707                                                            getDialect(), start, end, false);
2708    
2709                                            Collections.sort(list);
2710    
2711                                            list = new UnmodifiableList<ResourcePermission>(list);
2712                                    }
2713                                    else {
2714                                            list = (List<ResourcePermission>)QueryUtil.list(q,
2715                                                            getDialect(), start, end);
2716                                    }
2717    
2718                                    cacheResult(list);
2719    
2720                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
2721                            }
2722                            catch (Exception e) {
2723                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
2724    
2725                                    throw processException(e);
2726                            }
2727                            finally {
2728                                    closeSession(session);
2729                            }
2730                    }
2731    
2732                    return list;
2733            }
2734    
2735            /**
2736             * Returns the first resource permission in the ordered set where companyId = &#63; and name = &#63; and scope = &#63; and primKey = &#63;.
2737             *
2738             * @param companyId the company ID
2739             * @param name the name
2740             * @param scope the scope
2741             * @param primKey the prim key
2742             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2743             * @return the first matching resource permission
2744             * @throws com.liferay.portal.NoSuchResourcePermissionException if a matching resource permission could not be found
2745             * @throws SystemException if a system exception occurred
2746             */
2747            @Override
2748            public ResourcePermission findByC_N_S_P_First(long companyId, String name,
2749                    int scope, String primKey, OrderByComparator orderByComparator)
2750                    throws NoSuchResourcePermissionException, SystemException {
2751                    ResourcePermission resourcePermission = fetchByC_N_S_P_First(companyId,
2752                                    name, scope, primKey, orderByComparator);
2753    
2754                    if (resourcePermission != null) {
2755                            return resourcePermission;
2756                    }
2757    
2758                    StringBundler msg = new StringBundler(10);
2759    
2760                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
2761    
2762                    msg.append("companyId=");
2763                    msg.append(companyId);
2764    
2765                    msg.append(", name=");
2766                    msg.append(name);
2767    
2768                    msg.append(", scope=");
2769                    msg.append(scope);
2770    
2771                    msg.append(", primKey=");
2772                    msg.append(primKey);
2773    
2774                    msg.append(StringPool.CLOSE_CURLY_BRACE);
2775    
2776                    throw new NoSuchResourcePermissionException(msg.toString());
2777            }
2778    
2779            /**
2780             * Returns the first resource permission in the ordered set where companyId = &#63; and name = &#63; and scope = &#63; and primKey = &#63;.
2781             *
2782             * @param companyId the company ID
2783             * @param name the name
2784             * @param scope the scope
2785             * @param primKey the prim key
2786             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2787             * @return the first matching resource permission, or <code>null</code> if a matching resource permission could not be found
2788             * @throws SystemException if a system exception occurred
2789             */
2790            @Override
2791            public ResourcePermission fetchByC_N_S_P_First(long companyId, String name,
2792                    int scope, String primKey, OrderByComparator orderByComparator)
2793                    throws SystemException {
2794                    List<ResourcePermission> list = findByC_N_S_P(companyId, name, scope,
2795                                    primKey, 0, 1, orderByComparator);
2796    
2797                    if (!list.isEmpty()) {
2798                            return list.get(0);
2799                    }
2800    
2801                    return null;
2802            }
2803    
2804            /**
2805             * Returns the last resource permission in the ordered set where companyId = &#63; and name = &#63; and scope = &#63; and primKey = &#63;.
2806             *
2807             * @param companyId the company ID
2808             * @param name the name
2809             * @param scope the scope
2810             * @param primKey the prim key
2811             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2812             * @return the last matching resource permission
2813             * @throws com.liferay.portal.NoSuchResourcePermissionException if a matching resource permission could not be found
2814             * @throws SystemException if a system exception occurred
2815             */
2816            @Override
2817            public ResourcePermission findByC_N_S_P_Last(long companyId, String name,
2818                    int scope, String primKey, OrderByComparator orderByComparator)
2819                    throws NoSuchResourcePermissionException, SystemException {
2820                    ResourcePermission resourcePermission = fetchByC_N_S_P_Last(companyId,
2821                                    name, scope, primKey, orderByComparator);
2822    
2823                    if (resourcePermission != null) {
2824                            return resourcePermission;
2825                    }
2826    
2827                    StringBundler msg = new StringBundler(10);
2828    
2829                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
2830    
2831                    msg.append("companyId=");
2832                    msg.append(companyId);
2833    
2834                    msg.append(", name=");
2835                    msg.append(name);
2836    
2837                    msg.append(", scope=");
2838                    msg.append(scope);
2839    
2840                    msg.append(", primKey=");
2841                    msg.append(primKey);
2842    
2843                    msg.append(StringPool.CLOSE_CURLY_BRACE);
2844    
2845                    throw new NoSuchResourcePermissionException(msg.toString());
2846            }
2847    
2848            /**
2849             * Returns the last resource permission in the ordered set where companyId = &#63; and name = &#63; and scope = &#63; and primKey = &#63;.
2850             *
2851             * @param companyId the company ID
2852             * @param name the name
2853             * @param scope the scope
2854             * @param primKey the prim key
2855             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2856             * @return the last matching resource permission, or <code>null</code> if a matching resource permission could not be found
2857             * @throws SystemException if a system exception occurred
2858             */
2859            @Override
2860            public ResourcePermission fetchByC_N_S_P_Last(long companyId, String name,
2861                    int scope, String primKey, OrderByComparator orderByComparator)
2862                    throws SystemException {
2863                    int count = countByC_N_S_P(companyId, name, scope, primKey);
2864    
2865                    if (count == 0) {
2866                            return null;
2867                    }
2868    
2869                    List<ResourcePermission> list = findByC_N_S_P(companyId, name, scope,
2870                                    primKey, count - 1, count, orderByComparator);
2871    
2872                    if (!list.isEmpty()) {
2873                            return list.get(0);
2874                    }
2875    
2876                    return null;
2877            }
2878    
2879            /**
2880             * Returns the resource permissions before and after the current resource permission in the ordered set where companyId = &#63; and name = &#63; and scope = &#63; and primKey = &#63;.
2881             *
2882             * @param resourcePermissionId the primary key of the current resource permission
2883             * @param companyId the company ID
2884             * @param name the name
2885             * @param scope the scope
2886             * @param primKey the prim key
2887             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2888             * @return the previous, current, and next resource permission
2889             * @throws com.liferay.portal.NoSuchResourcePermissionException if a resource permission with the primary key could not be found
2890             * @throws SystemException if a system exception occurred
2891             */
2892            @Override
2893            public ResourcePermission[] findByC_N_S_P_PrevAndNext(
2894                    long resourcePermissionId, long companyId, String name, int scope,
2895                    String primKey, OrderByComparator orderByComparator)
2896                    throws NoSuchResourcePermissionException, SystemException {
2897                    ResourcePermission resourcePermission = findByPrimaryKey(resourcePermissionId);
2898    
2899                    Session session = null;
2900    
2901                    try {
2902                            session = openSession();
2903    
2904                            ResourcePermission[] array = new ResourcePermissionImpl[3];
2905    
2906                            array[0] = getByC_N_S_P_PrevAndNext(session, resourcePermission,
2907                                            companyId, name, scope, primKey, orderByComparator, true);
2908    
2909                            array[1] = resourcePermission;
2910    
2911                            array[2] = getByC_N_S_P_PrevAndNext(session, resourcePermission,
2912                                            companyId, name, scope, primKey, orderByComparator, false);
2913    
2914                            return array;
2915                    }
2916                    catch (Exception e) {
2917                            throw processException(e);
2918                    }
2919                    finally {
2920                            closeSession(session);
2921                    }
2922            }
2923    
2924            protected ResourcePermission getByC_N_S_P_PrevAndNext(Session session,
2925                    ResourcePermission resourcePermission, long companyId, String name,
2926                    int scope, String primKey, OrderByComparator orderByComparator,
2927                    boolean previous) {
2928                    StringBundler query = null;
2929    
2930                    if (orderByComparator != null) {
2931                            query = new StringBundler(6 +
2932                                            (orderByComparator.getOrderByFields().length * 6));
2933                    }
2934                    else {
2935                            query = new StringBundler(3);
2936                    }
2937    
2938                    query.append(_SQL_SELECT_RESOURCEPERMISSION_WHERE);
2939    
2940                    query.append(_FINDER_COLUMN_C_N_S_P_COMPANYID_2);
2941    
2942                    boolean bindName = false;
2943    
2944                    if (name == null) {
2945                            query.append(_FINDER_COLUMN_C_N_S_P_NAME_1);
2946                    }
2947                    else if (name.equals(StringPool.BLANK)) {
2948                            query.append(_FINDER_COLUMN_C_N_S_P_NAME_3);
2949                    }
2950                    else {
2951                            bindName = true;
2952    
2953                            query.append(_FINDER_COLUMN_C_N_S_P_NAME_2);
2954                    }
2955    
2956                    query.append(_FINDER_COLUMN_C_N_S_P_SCOPE_2);
2957    
2958                    boolean bindPrimKey = false;
2959    
2960                    if (primKey == null) {
2961                            query.append(_FINDER_COLUMN_C_N_S_P_PRIMKEY_1);
2962                    }
2963                    else if (primKey.equals(StringPool.BLANK)) {
2964                            query.append(_FINDER_COLUMN_C_N_S_P_PRIMKEY_3);
2965                    }
2966                    else {
2967                            bindPrimKey = true;
2968    
2969                            query.append(_FINDER_COLUMN_C_N_S_P_PRIMKEY_2);
2970                    }
2971    
2972                    if (orderByComparator != null) {
2973                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
2974    
2975                            if (orderByConditionFields.length > 0) {
2976                                    query.append(WHERE_AND);
2977                            }
2978    
2979                            for (int i = 0; i < orderByConditionFields.length; i++) {
2980                                    query.append(_ORDER_BY_ENTITY_ALIAS);
2981                                    query.append(orderByConditionFields[i]);
2982    
2983                                    if ((i + 1) < orderByConditionFields.length) {
2984                                            if (orderByComparator.isAscending() ^ previous) {
2985                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
2986                                            }
2987                                            else {
2988                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
2989                                            }
2990                                    }
2991                                    else {
2992                                            if (orderByComparator.isAscending() ^ previous) {
2993                                                    query.append(WHERE_GREATER_THAN);
2994                                            }
2995                                            else {
2996                                                    query.append(WHERE_LESSER_THAN);
2997                                            }
2998                                    }
2999                            }
3000    
3001                            query.append(ORDER_BY_CLAUSE);
3002    
3003                            String[] orderByFields = orderByComparator.getOrderByFields();
3004    
3005                            for (int i = 0; i < orderByFields.length; i++) {
3006                                    query.append(_ORDER_BY_ENTITY_ALIAS);
3007                                    query.append(orderByFields[i]);
3008    
3009                                    if ((i + 1) < orderByFields.length) {
3010                                            if (orderByComparator.isAscending() ^ previous) {
3011                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
3012                                            }
3013                                            else {
3014                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
3015                                            }
3016                                    }
3017                                    else {
3018                                            if (orderByComparator.isAscending() ^ previous) {
3019                                                    query.append(ORDER_BY_ASC);
3020                                            }
3021                                            else {
3022                                                    query.append(ORDER_BY_DESC);
3023                                            }
3024                                    }
3025                            }
3026                    }
3027                    else {
3028                            query.append(ResourcePermissionModelImpl.ORDER_BY_JPQL);
3029                    }
3030    
3031                    String sql = query.toString();
3032    
3033                    Query q = session.createQuery(sql);
3034    
3035                    q.setFirstResult(0);
3036                    q.setMaxResults(2);
3037    
3038                    QueryPos qPos = QueryPos.getInstance(q);
3039    
3040                    qPos.add(companyId);
3041    
3042                    if (bindName) {
3043                            qPos.add(name);
3044                    }
3045    
3046                    qPos.add(scope);
3047    
3048                    if (bindPrimKey) {
3049                            qPos.add(primKey);
3050                    }
3051    
3052                    if (orderByComparator != null) {
3053                            Object[] values = orderByComparator.getOrderByConditionValues(resourcePermission);
3054    
3055                            for (Object value : values) {
3056                                    qPos.add(value);
3057                            }
3058                    }
3059    
3060                    List<ResourcePermission> list = q.list();
3061    
3062                    if (list.size() == 2) {
3063                            return list.get(1);
3064                    }
3065                    else {
3066                            return null;
3067                    }
3068            }
3069    
3070            /**
3071             * Removes all the resource permissions where companyId = &#63; and name = &#63; and scope = &#63; and primKey = &#63; from the database.
3072             *
3073             * @param companyId the company ID
3074             * @param name the name
3075             * @param scope the scope
3076             * @param primKey the prim key
3077             * @throws SystemException if a system exception occurred
3078             */
3079            @Override
3080            public void removeByC_N_S_P(long companyId, String name, int scope,
3081                    String primKey) throws SystemException {
3082                    for (ResourcePermission resourcePermission : findByC_N_S_P(companyId,
3083                                    name, scope, primKey, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
3084                            remove(resourcePermission);
3085                    }
3086            }
3087    
3088            /**
3089             * Returns the number of resource permissions where companyId = &#63; and name = &#63; and scope = &#63; and primKey = &#63;.
3090             *
3091             * @param companyId the company ID
3092             * @param name the name
3093             * @param scope the scope
3094             * @param primKey the prim key
3095             * @return the number of matching resource permissions
3096             * @throws SystemException if a system exception occurred
3097             */
3098            @Override
3099            public int countByC_N_S_P(long companyId, String name, int scope,
3100                    String primKey) throws SystemException {
3101                    FinderPath finderPath = FINDER_PATH_COUNT_BY_C_N_S_P;
3102    
3103                    Object[] finderArgs = new Object[] { companyId, name, scope, primKey };
3104    
3105                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
3106                                    this);
3107    
3108                    if (count == null) {
3109                            StringBundler query = new StringBundler(5);
3110    
3111                            query.append(_SQL_COUNT_RESOURCEPERMISSION_WHERE);
3112    
3113                            query.append(_FINDER_COLUMN_C_N_S_P_COMPANYID_2);
3114    
3115                            boolean bindName = false;
3116    
3117                            if (name == null) {
3118                                    query.append(_FINDER_COLUMN_C_N_S_P_NAME_1);
3119                            }
3120                            else if (name.equals(StringPool.BLANK)) {
3121                                    query.append(_FINDER_COLUMN_C_N_S_P_NAME_3);
3122                            }
3123                            else {
3124                                    bindName = true;
3125    
3126                                    query.append(_FINDER_COLUMN_C_N_S_P_NAME_2);
3127                            }
3128    
3129                            query.append(_FINDER_COLUMN_C_N_S_P_SCOPE_2);
3130    
3131                            boolean bindPrimKey = false;
3132    
3133                            if (primKey == null) {
3134                                    query.append(_FINDER_COLUMN_C_N_S_P_PRIMKEY_1);
3135                            }
3136                            else if (primKey.equals(StringPool.BLANK)) {
3137                                    query.append(_FINDER_COLUMN_C_N_S_P_PRIMKEY_3);
3138                            }
3139                            else {
3140                                    bindPrimKey = true;
3141    
3142                                    query.append(_FINDER_COLUMN_C_N_S_P_PRIMKEY_2);
3143                            }
3144    
3145                            String sql = query.toString();
3146    
3147                            Session session = null;
3148    
3149                            try {
3150                                    session = openSession();
3151    
3152                                    Query q = session.createQuery(sql);
3153    
3154                                    QueryPos qPos = QueryPos.getInstance(q);
3155    
3156                                    qPos.add(companyId);
3157    
3158                                    if (bindName) {
3159                                            qPos.add(name);
3160                                    }
3161    
3162                                    qPos.add(scope);
3163    
3164                                    if (bindPrimKey) {
3165                                            qPos.add(primKey);
3166                                    }
3167    
3168                                    count = (Long)q.uniqueResult();
3169    
3170                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
3171                            }
3172                            catch (Exception e) {
3173                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
3174    
3175                                    throw processException(e);
3176                            }
3177                            finally {
3178                                    closeSession(session);
3179                            }
3180                    }
3181    
3182                    return count.intValue();
3183            }
3184    
3185            private static final String _FINDER_COLUMN_C_N_S_P_COMPANYID_2 = "resourcePermission.companyId = ? AND ";
3186            private static final String _FINDER_COLUMN_C_N_S_P_NAME_1 = "resourcePermission.name IS NULL AND ";
3187            private static final String _FINDER_COLUMN_C_N_S_P_NAME_2 = "resourcePermission.name = ? AND ";
3188            private static final String _FINDER_COLUMN_C_N_S_P_NAME_3 = "(resourcePermission.name IS NULL OR resourcePermission.name = '') AND ";
3189            private static final String _FINDER_COLUMN_C_N_S_P_SCOPE_2 = "resourcePermission.scope = ? AND ";
3190            private static final String _FINDER_COLUMN_C_N_S_P_PRIMKEY_1 = "resourcePermission.primKey IS NULL";
3191            private static final String _FINDER_COLUMN_C_N_S_P_PRIMKEY_2 = "resourcePermission.primKey = ?";
3192            private static final String _FINDER_COLUMN_C_N_S_P_PRIMKEY_3 = "(resourcePermission.primKey IS NULL OR resourcePermission.primKey = '')";
3193            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_C_N_S_P_R =
3194                    new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
3195                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED,
3196                            ResourcePermissionImpl.class,
3197                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByC_N_S_P_R",
3198                            new String[] {
3199                                    Long.class.getName(), String.class.getName(),
3200                                    Integer.class.getName(), String.class.getName(),
3201                                    Long.class.getName(),
3202                                    
3203                            Integer.class.getName(), Integer.class.getName(),
3204                                    OrderByComparator.class.getName()
3205                            });
3206            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_N_S_P_R =
3207                    new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
3208                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED,
3209                            ResourcePermissionImpl.class,
3210                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByC_N_S_P_R",
3211                            new String[] {
3212                                    Long.class.getName(), String.class.getName(),
3213                                    Integer.class.getName(), String.class.getName(),
3214                                    Long.class.getName()
3215                            },
3216                            ResourcePermissionModelImpl.COMPANYID_COLUMN_BITMASK |
3217                            ResourcePermissionModelImpl.NAME_COLUMN_BITMASK |
3218                            ResourcePermissionModelImpl.SCOPE_COLUMN_BITMASK |
3219                            ResourcePermissionModelImpl.PRIMKEY_COLUMN_BITMASK |
3220                            ResourcePermissionModelImpl.ROLEID_COLUMN_BITMASK);
3221            public static final FinderPath FINDER_PATH_FETCH_BY_C_N_S_P_R = new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
3222                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED,
3223                            ResourcePermissionImpl.class, FINDER_CLASS_NAME_ENTITY,
3224                            "fetchByC_N_S_P_R",
3225                            new String[] {
3226                                    Long.class.getName(), String.class.getName(),
3227                                    Integer.class.getName(), String.class.getName(),
3228                                    Long.class.getName()
3229                            },
3230                            ResourcePermissionModelImpl.COMPANYID_COLUMN_BITMASK |
3231                            ResourcePermissionModelImpl.NAME_COLUMN_BITMASK |
3232                            ResourcePermissionModelImpl.SCOPE_COLUMN_BITMASK |
3233                            ResourcePermissionModelImpl.PRIMKEY_COLUMN_BITMASK |
3234                            ResourcePermissionModelImpl.ROLEID_COLUMN_BITMASK);
3235            public static final FinderPath FINDER_PATH_COUNT_BY_C_N_S_P_R = new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
3236                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED, Long.class,
3237                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByC_N_S_P_R",
3238                            new String[] {
3239                                    Long.class.getName(), String.class.getName(),
3240                                    Integer.class.getName(), String.class.getName(),
3241                                    Long.class.getName()
3242                            });
3243            public static final FinderPath FINDER_PATH_WITH_PAGINATION_COUNT_BY_C_N_S_P_R =
3244                    new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
3245                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED, Long.class,
3246                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "countByC_N_S_P_R",
3247                            new String[] {
3248                                    Long.class.getName(), String.class.getName(),
3249                                    Integer.class.getName(), String.class.getName(),
3250                                    Long.class.getName()
3251                            });
3252    
3253            /**
3254             * Returns all the resource permissions where companyId = &#63; and name = &#63; and scope = &#63; and primKey = &#63; and roleId = any &#63;.
3255             *
3256             * <p>
3257             * 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. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portal.model.impl.ResourcePermissionModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
3258             * </p>
3259             *
3260             * @param companyId the company ID
3261             * @param name the name
3262             * @param scope the scope
3263             * @param primKey the prim key
3264             * @param roleIds the role IDs
3265             * @return the matching resource permissions
3266             * @throws SystemException if a system exception occurred
3267             */
3268            @Override
3269            public List<ResourcePermission> findByC_N_S_P_R(long companyId,
3270                    String name, int scope, String primKey, long[] roleIds)
3271                    throws SystemException {
3272                    return findByC_N_S_P_R(companyId, name, scope, primKey, roleIds,
3273                            QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
3274            }
3275    
3276            /**
3277             * Returns a range of all the resource permissions where companyId = &#63; and name = &#63; and scope = &#63; and primKey = &#63; and roleId = any &#63;.
3278             *
3279             * <p>
3280             * 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. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portal.model.impl.ResourcePermissionModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
3281             * </p>
3282             *
3283             * @param companyId the company ID
3284             * @param name the name
3285             * @param scope the scope
3286             * @param primKey the prim key
3287             * @param roleIds the role IDs
3288             * @param start the lower bound of the range of resource permissions
3289             * @param end the upper bound of the range of resource permissions (not inclusive)
3290             * @return the range of matching resource permissions
3291             * @throws SystemException if a system exception occurred
3292             */
3293            @Override
3294            public List<ResourcePermission> findByC_N_S_P_R(long companyId,
3295                    String name, int scope, String primKey, long[] roleIds, int start,
3296                    int end) throws SystemException {
3297                    return findByC_N_S_P_R(companyId, name, scope, primKey, roleIds, start,
3298                            end, null);
3299            }
3300    
3301            /**
3302             * Returns an ordered range of all the resource permissions where companyId = &#63; and name = &#63; and scope = &#63; and primKey = &#63; and roleId = any &#63;.
3303             *
3304             * <p>
3305             * 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. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portal.model.impl.ResourcePermissionModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
3306             * </p>
3307             *
3308             * @param companyId the company ID
3309             * @param name the name
3310             * @param scope the scope
3311             * @param primKey the prim key
3312             * @param roleIds the role IDs
3313             * @param start the lower bound of the range of resource permissions
3314             * @param end the upper bound of the range of resource permissions (not inclusive)
3315             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
3316             * @return the ordered range of matching resource permissions
3317             * @throws SystemException if a system exception occurred
3318             */
3319            @Override
3320            public List<ResourcePermission> findByC_N_S_P_R(long companyId,
3321                    String name, int scope, String primKey, long[] roleIds, int start,
3322                    int end, OrderByComparator orderByComparator) throws SystemException {
3323                    if ((roleIds != null) && (roleIds.length == 1)) {
3324                            ResourcePermission resourcePermission = fetchByC_N_S_P_R(companyId,
3325                                            name, scope, primKey, roleIds[0]);
3326    
3327                            if (resourcePermission == null) {
3328                                    return Collections.emptyList();
3329                            }
3330                            else {
3331                                    List<ResourcePermission> list = new ArrayList<ResourcePermission>(1);
3332    
3333                                    list.add(resourcePermission);
3334    
3335                                    return list;
3336                            }
3337                    }
3338    
3339                    boolean pagination = true;
3340                    Object[] finderArgs = null;
3341    
3342                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
3343                                    (orderByComparator == null)) {
3344                            pagination = false;
3345                            finderArgs = new Object[] {
3346                                            companyId, name, scope, primKey, StringUtil.merge(roleIds)
3347                                    };
3348                    }
3349                    else {
3350                            finderArgs = new Object[] {
3351                                            companyId, name, scope, primKey, StringUtil.merge(roleIds),
3352                                            
3353                                            start, end, orderByComparator
3354                                    };
3355                    }
3356    
3357                    List<ResourcePermission> list = (List<ResourcePermission>)FinderCacheUtil.getResult(FINDER_PATH_WITH_PAGINATION_FIND_BY_C_N_S_P_R,
3358                                    finderArgs, this);
3359    
3360                    if ((list != null) && !list.isEmpty()) {
3361                            for (ResourcePermission resourcePermission : list) {
3362                                    if ((companyId != resourcePermission.getCompanyId()) ||
3363                                                    !Validator.equals(name, resourcePermission.getName()) ||
3364                                                    (scope != resourcePermission.getScope()) ||
3365                                                    !Validator.equals(primKey,
3366                                                            resourcePermission.getPrimKey()) ||
3367                                                    !ArrayUtil.contains(roleIds,
3368                                                            resourcePermission.getRoleId())) {
3369                                            list = null;
3370    
3371                                            break;
3372                                    }
3373                            }
3374                    }
3375    
3376                    if (list == null) {
3377                            StringBundler query = new StringBundler();
3378    
3379                            query.append(_SQL_SELECT_RESOURCEPERMISSION_WHERE);
3380    
3381                            boolean conjunctionable = false;
3382    
3383                            if (conjunctionable) {
3384                                    query.append(WHERE_AND);
3385                            }
3386    
3387                            query.append(_FINDER_COLUMN_C_N_S_P_R_COMPANYID_5);
3388    
3389                            conjunctionable = true;
3390    
3391                            if (conjunctionable) {
3392                                    query.append(WHERE_AND);
3393                            }
3394    
3395                            if (name == null) {
3396                                    query.append(_FINDER_COLUMN_C_N_S_P_R_NAME_4);
3397                            }
3398                            else if (name.equals(StringPool.BLANK)) {
3399                                    query.append(_FINDER_COLUMN_C_N_S_P_R_NAME_6);
3400                            }
3401                            else {
3402                                    query.append(_FINDER_COLUMN_C_N_S_P_R_NAME_5);
3403                            }
3404    
3405                            conjunctionable = true;
3406    
3407                            if (conjunctionable) {
3408                                    query.append(WHERE_AND);
3409                            }
3410    
3411                            query.append(_FINDER_COLUMN_C_N_S_P_R_SCOPE_5);
3412    
3413                            conjunctionable = true;
3414    
3415                            if (conjunctionable) {
3416                                    query.append(WHERE_AND);
3417                            }
3418    
3419                            if (primKey == null) {
3420                                    query.append(_FINDER_COLUMN_C_N_S_P_R_PRIMKEY_4);
3421                            }
3422                            else if (primKey.equals(StringPool.BLANK)) {
3423                                    query.append(_FINDER_COLUMN_C_N_S_P_R_PRIMKEY_6);
3424                            }
3425                            else {
3426                                    query.append(_FINDER_COLUMN_C_N_S_P_R_PRIMKEY_5);
3427                            }
3428    
3429                            conjunctionable = true;
3430    
3431                            if ((roleIds == null) || (roleIds.length > 0)) {
3432                                    if (conjunctionable) {
3433                                            query.append(WHERE_AND);
3434                                    }
3435    
3436                                    query.append(StringPool.OPEN_PARENTHESIS);
3437    
3438                                    for (int i = 0; i < roleIds.length; i++) {
3439                                            query.append(_FINDER_COLUMN_C_N_S_P_R_ROLEID_5);
3440    
3441                                            if ((i + 1) < roleIds.length) {
3442                                                    query.append(WHERE_OR);
3443                                            }
3444                                    }
3445    
3446                                    query.append(StringPool.CLOSE_PARENTHESIS);
3447    
3448                                    conjunctionable = true;
3449                            }
3450    
3451                            if (orderByComparator != null) {
3452                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
3453                                            orderByComparator);
3454                            }
3455                            else
3456                             if (pagination) {
3457                                    query.append(ResourcePermissionModelImpl.ORDER_BY_JPQL);
3458                            }
3459    
3460                            String sql = query.toString();
3461    
3462                            Session session = null;
3463    
3464                            try {
3465                                    session = openSession();
3466    
3467                                    Query q = session.createQuery(sql);
3468    
3469                                    QueryPos qPos = QueryPos.getInstance(q);
3470    
3471                                    qPos.add(companyId);
3472    
3473                                    if (name != null) {
3474                                            qPos.add(name);
3475                                    }
3476    
3477                                    qPos.add(scope);
3478    
3479                                    if (primKey != null) {
3480                                            qPos.add(primKey);
3481                                    }
3482    
3483                                    if (roleIds != null) {
3484                                            qPos.add(roleIds);
3485                                    }
3486    
3487                                    if (!pagination) {
3488                                            list = (List<ResourcePermission>)QueryUtil.list(q,
3489                                                            getDialect(), start, end, false);
3490    
3491                                            Collections.sort(list);
3492    
3493                                            list = new UnmodifiableList<ResourcePermission>(list);
3494                                    }
3495                                    else {
3496                                            list = (List<ResourcePermission>)QueryUtil.list(q,
3497                                                            getDialect(), start, end);
3498                                    }
3499    
3500                                    cacheResult(list);
3501    
3502                                    FinderCacheUtil.putResult(FINDER_PATH_WITH_PAGINATION_FIND_BY_C_N_S_P_R,
3503                                            finderArgs, list);
3504                            }
3505                            catch (Exception e) {
3506                                    FinderCacheUtil.removeResult(FINDER_PATH_WITH_PAGINATION_FIND_BY_C_N_S_P_R,
3507                                            finderArgs);
3508    
3509                                    throw processException(e);
3510                            }
3511                            finally {
3512                                    closeSession(session);
3513                            }
3514                    }
3515    
3516                    return list;
3517            }
3518    
3519            /**
3520             * Returns the resource permission where companyId = &#63; and name = &#63; and scope = &#63; and primKey = &#63; and roleId = &#63; or throws a {@link com.liferay.portal.NoSuchResourcePermissionException} if it could not be found.
3521             *
3522             * @param companyId the company ID
3523             * @param name the name
3524             * @param scope the scope
3525             * @param primKey the prim key
3526             * @param roleId the role ID
3527             * @return the matching resource permission
3528             * @throws com.liferay.portal.NoSuchResourcePermissionException if a matching resource permission could not be found
3529             * @throws SystemException if a system exception occurred
3530             */
3531            @Override
3532            public ResourcePermission findByC_N_S_P_R(long companyId, String name,
3533                    int scope, String primKey, long roleId)
3534                    throws NoSuchResourcePermissionException, SystemException {
3535                    ResourcePermission resourcePermission = fetchByC_N_S_P_R(companyId,
3536                                    name, scope, primKey, roleId);
3537    
3538                    if (resourcePermission == null) {
3539                            StringBundler msg = new StringBundler(12);
3540    
3541                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
3542    
3543                            msg.append("companyId=");
3544                            msg.append(companyId);
3545    
3546                            msg.append(", name=");
3547                            msg.append(name);
3548    
3549                            msg.append(", scope=");
3550                            msg.append(scope);
3551    
3552                            msg.append(", primKey=");
3553                            msg.append(primKey);
3554    
3555                            msg.append(", roleId=");
3556                            msg.append(roleId);
3557    
3558                            msg.append(StringPool.CLOSE_CURLY_BRACE);
3559    
3560                            if (_log.isWarnEnabled()) {
3561                                    _log.warn(msg.toString());
3562                            }
3563    
3564                            throw new NoSuchResourcePermissionException(msg.toString());
3565                    }
3566    
3567                    return resourcePermission;
3568            }
3569    
3570            /**
3571             * Returns the resource permission where companyId = &#63; and name = &#63; and scope = &#63; and primKey = &#63; and roleId = &#63; or returns <code>null</code> if it could not be found. Uses the finder cache.
3572             *
3573             * @param companyId the company ID
3574             * @param name the name
3575             * @param scope the scope
3576             * @param primKey the prim key
3577             * @param roleId the role ID
3578             * @return the matching resource permission, or <code>null</code> if a matching resource permission could not be found
3579             * @throws SystemException if a system exception occurred
3580             */
3581            @Override
3582            public ResourcePermission fetchByC_N_S_P_R(long companyId, String name,
3583                    int scope, String primKey, long roleId) throws SystemException {
3584                    return fetchByC_N_S_P_R(companyId, name, scope, primKey, roleId, true);
3585            }
3586    
3587            /**
3588             * Returns the resource permission where companyId = &#63; and name = &#63; and scope = &#63; and primKey = &#63; and roleId = &#63; or returns <code>null</code> if it could not be found, optionally using the finder cache.
3589             *
3590             * @param companyId the company ID
3591             * @param name the name
3592             * @param scope the scope
3593             * @param primKey the prim key
3594             * @param roleId the role ID
3595             * @param retrieveFromCache whether to use the finder cache
3596             * @return the matching resource permission, or <code>null</code> if a matching resource permission could not be found
3597             * @throws SystemException if a system exception occurred
3598             */
3599            @Override
3600            public ResourcePermission fetchByC_N_S_P_R(long companyId, String name,
3601                    int scope, String primKey, long roleId, boolean retrieveFromCache)
3602                    throws SystemException {
3603                    Object[] finderArgs = new Object[] {
3604                                    companyId, name, scope, primKey, roleId
3605                            };
3606    
3607                    Object result = null;
3608    
3609                    if (retrieveFromCache) {
3610                            result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_C_N_S_P_R,
3611                                            finderArgs, this);
3612                    }
3613    
3614                    if (result instanceof ResourcePermission) {
3615                            ResourcePermission resourcePermission = (ResourcePermission)result;
3616    
3617                            if ((companyId != resourcePermission.getCompanyId()) ||
3618                                            !Validator.equals(name, resourcePermission.getName()) ||
3619                                            (scope != resourcePermission.getScope()) ||
3620                                            !Validator.equals(primKey, resourcePermission.getPrimKey()) ||
3621                                            (roleId != resourcePermission.getRoleId())) {
3622                                    result = null;
3623                            }
3624                    }
3625    
3626                    if (result == null) {
3627                            StringBundler query = new StringBundler(7);
3628    
3629                            query.append(_SQL_SELECT_RESOURCEPERMISSION_WHERE);
3630    
3631                            query.append(_FINDER_COLUMN_C_N_S_P_R_COMPANYID_2);
3632    
3633                            boolean bindName = false;
3634    
3635                            if (name == null) {
3636                                    query.append(_FINDER_COLUMN_C_N_S_P_R_NAME_1);
3637                            }
3638                            else if (name.equals(StringPool.BLANK)) {
3639                                    query.append(_FINDER_COLUMN_C_N_S_P_R_NAME_3);
3640                            }
3641                            else {
3642                                    bindName = true;
3643    
3644                                    query.append(_FINDER_COLUMN_C_N_S_P_R_NAME_2);
3645                            }
3646    
3647                            query.append(_FINDER_COLUMN_C_N_S_P_R_SCOPE_2);
3648    
3649                            boolean bindPrimKey = false;
3650    
3651                            if (primKey == null) {
3652                                    query.append(_FINDER_COLUMN_C_N_S_P_R_PRIMKEY_1);
3653                            }
3654                            else if (primKey.equals(StringPool.BLANK)) {
3655                                    query.append(_FINDER_COLUMN_C_N_S_P_R_PRIMKEY_3);
3656                            }
3657                            else {
3658                                    bindPrimKey = true;
3659    
3660                                    query.append(_FINDER_COLUMN_C_N_S_P_R_PRIMKEY_2);
3661                            }
3662    
3663                            query.append(_FINDER_COLUMN_C_N_S_P_R_ROLEID_2);
3664    
3665                            String sql = query.toString();
3666    
3667                            Session session = null;
3668    
3669                            try {
3670                                    session = openSession();
3671    
3672                                    Query q = session.createQuery(sql);
3673    
3674                                    QueryPos qPos = QueryPos.getInstance(q);
3675    
3676                                    qPos.add(companyId);
3677    
3678                                    if (bindName) {
3679                                            qPos.add(name);
3680                                    }
3681    
3682                                    qPos.add(scope);
3683    
3684                                    if (bindPrimKey) {
3685                                            qPos.add(primKey);
3686                                    }
3687    
3688                                    qPos.add(roleId);
3689    
3690                                    List<ResourcePermission> list = q.list();
3691    
3692                                    if (list.isEmpty()) {
3693                                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_N_S_P_R,
3694                                                    finderArgs, list);
3695                                    }
3696                                    else {
3697                                            ResourcePermission resourcePermission = list.get(0);
3698    
3699                                            result = resourcePermission;
3700    
3701                                            cacheResult(resourcePermission);
3702    
3703                                            if ((resourcePermission.getCompanyId() != companyId) ||
3704                                                            (resourcePermission.getName() == null) ||
3705                                                            !resourcePermission.getName().equals(name) ||
3706                                                            (resourcePermission.getScope() != scope) ||
3707                                                            (resourcePermission.getPrimKey() == null) ||
3708                                                            !resourcePermission.getPrimKey().equals(primKey) ||
3709                                                            (resourcePermission.getRoleId() != roleId)) {
3710                                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_N_S_P_R,
3711                                                            finderArgs, resourcePermission);
3712                                            }
3713                                    }
3714                            }
3715                            catch (Exception e) {
3716                                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_N_S_P_R,
3717                                            finderArgs);
3718    
3719                                    throw processException(e);
3720                            }
3721                            finally {
3722                                    closeSession(session);
3723                            }
3724                    }
3725    
3726                    if (result instanceof List<?>) {
3727                            return null;
3728                    }
3729                    else {
3730                            return (ResourcePermission)result;
3731                    }
3732            }
3733    
3734            /**
3735             * Removes the resource permission where companyId = &#63; and name = &#63; and scope = &#63; and primKey = &#63; and roleId = &#63; from the database.
3736             *
3737             * @param companyId the company ID
3738             * @param name the name
3739             * @param scope the scope
3740             * @param primKey the prim key
3741             * @param roleId the role ID
3742             * @return the resource permission that was removed
3743             * @throws SystemException if a system exception occurred
3744             */
3745            @Override
3746            public ResourcePermission removeByC_N_S_P_R(long companyId, String name,
3747                    int scope, String primKey, long roleId)
3748                    throws NoSuchResourcePermissionException, SystemException {
3749                    ResourcePermission resourcePermission = findByC_N_S_P_R(companyId,
3750                                    name, scope, primKey, roleId);
3751    
3752                    return remove(resourcePermission);
3753            }
3754    
3755            /**
3756             * Returns the number of resource permissions where companyId = &#63; and name = &#63; and scope = &#63; and primKey = &#63; and roleId = &#63;.
3757             *
3758             * @param companyId the company ID
3759             * @param name the name
3760             * @param scope the scope
3761             * @param primKey the prim key
3762             * @param roleId the role ID
3763             * @return the number of matching resource permissions
3764             * @throws SystemException if a system exception occurred
3765             */
3766            @Override
3767            public int countByC_N_S_P_R(long companyId, String name, int scope,
3768                    String primKey, long roleId) throws SystemException {
3769                    FinderPath finderPath = FINDER_PATH_COUNT_BY_C_N_S_P_R;
3770    
3771                    Object[] finderArgs = new Object[] {
3772                                    companyId, name, scope, primKey, roleId
3773                            };
3774    
3775                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
3776                                    this);
3777    
3778                    if (count == null) {
3779                            StringBundler query = new StringBundler(6);
3780    
3781                            query.append(_SQL_COUNT_RESOURCEPERMISSION_WHERE);
3782    
3783                            query.append(_FINDER_COLUMN_C_N_S_P_R_COMPANYID_2);
3784    
3785                            boolean bindName = false;
3786    
3787                            if (name == null) {
3788                                    query.append(_FINDER_COLUMN_C_N_S_P_R_NAME_1);
3789                            }
3790                            else if (name.equals(StringPool.BLANK)) {
3791                                    query.append(_FINDER_COLUMN_C_N_S_P_R_NAME_3);
3792                            }
3793                            else {
3794                                    bindName = true;
3795    
3796                                    query.append(_FINDER_COLUMN_C_N_S_P_R_NAME_2);
3797                            }
3798    
3799                            query.append(_FINDER_COLUMN_C_N_S_P_R_SCOPE_2);
3800    
3801                            boolean bindPrimKey = false;
3802    
3803                            if (primKey == null) {
3804                                    query.append(_FINDER_COLUMN_C_N_S_P_R_PRIMKEY_1);
3805                            }
3806                            else if (primKey.equals(StringPool.BLANK)) {
3807                                    query.append(_FINDER_COLUMN_C_N_S_P_R_PRIMKEY_3);
3808                            }
3809                            else {
3810                                    bindPrimKey = true;
3811    
3812                                    query.append(_FINDER_COLUMN_C_N_S_P_R_PRIMKEY_2);
3813                            }
3814    
3815                            query.append(_FINDER_COLUMN_C_N_S_P_R_ROLEID_2);
3816    
3817                            String sql = query.toString();
3818    
3819                            Session session = null;
3820    
3821                            try {
3822                                    session = openSession();
3823    
3824                                    Query q = session.createQuery(sql);
3825    
3826                                    QueryPos qPos = QueryPos.getInstance(q);
3827    
3828                                    qPos.add(companyId);
3829    
3830                                    if (bindName) {
3831                                            qPos.add(name);
3832                                    }
3833    
3834                                    qPos.add(scope);
3835    
3836                                    if (bindPrimKey) {
3837                                            qPos.add(primKey);
3838                                    }
3839    
3840                                    qPos.add(roleId);
3841    
3842                                    count = (Long)q.uniqueResult();
3843    
3844                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
3845                            }
3846                            catch (Exception e) {
3847                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
3848    
3849                                    throw processException(e);
3850                            }
3851                            finally {
3852                                    closeSession(session);
3853                            }
3854                    }
3855    
3856                    return count.intValue();
3857            }
3858    
3859            /**
3860             * Returns the number of resource permissions where companyId = &#63; and name = &#63; and scope = &#63; and primKey = &#63; and roleId = any &#63;.
3861             *
3862             * @param companyId the company ID
3863             * @param name the name
3864             * @param scope the scope
3865             * @param primKey the prim key
3866             * @param roleIds the role IDs
3867             * @return the number of matching resource permissions
3868             * @throws SystemException if a system exception occurred
3869             */
3870            @Override
3871            public int countByC_N_S_P_R(long companyId, String name, int scope,
3872                    String primKey, long[] roleIds) throws SystemException {
3873                    Object[] finderArgs = new Object[] {
3874                                    companyId, name, scope, primKey, StringUtil.merge(roleIds)
3875                            };
3876    
3877                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_WITH_PAGINATION_COUNT_BY_C_N_S_P_R,
3878                                    finderArgs, this);
3879    
3880                    if (count == null) {
3881                            StringBundler query = new StringBundler();
3882    
3883                            query.append(_SQL_COUNT_RESOURCEPERMISSION_WHERE);
3884    
3885                            boolean conjunctionable = false;
3886    
3887                            if (conjunctionable) {
3888                                    query.append(WHERE_AND);
3889                            }
3890    
3891                            query.append(_FINDER_COLUMN_C_N_S_P_R_COMPANYID_5);
3892    
3893                            conjunctionable = true;
3894    
3895                            if (conjunctionable) {
3896                                    query.append(WHERE_AND);
3897                            }
3898    
3899                            if (name == null) {
3900                                    query.append(_FINDER_COLUMN_C_N_S_P_R_NAME_4);
3901                            }
3902                            else if (name.equals(StringPool.BLANK)) {
3903                                    query.append(_FINDER_COLUMN_C_N_S_P_R_NAME_6);
3904                            }
3905                            else {
3906                                    query.append(_FINDER_COLUMN_C_N_S_P_R_NAME_5);
3907                            }
3908    
3909                            conjunctionable = true;
3910    
3911                            if (conjunctionable) {
3912                                    query.append(WHERE_AND);
3913                            }
3914    
3915                            query.append(_FINDER_COLUMN_C_N_S_P_R_SCOPE_5);
3916    
3917                            conjunctionable = true;
3918    
3919                            if (conjunctionable) {
3920                                    query.append(WHERE_AND);
3921                            }
3922    
3923                            if (primKey == null) {
3924                                    query.append(_FINDER_COLUMN_C_N_S_P_R_PRIMKEY_4);
3925                            }
3926                            else if (primKey.equals(StringPool.BLANK)) {
3927                                    query.append(_FINDER_COLUMN_C_N_S_P_R_PRIMKEY_6);
3928                            }
3929                            else {
3930                                    query.append(_FINDER_COLUMN_C_N_S_P_R_PRIMKEY_5);
3931                            }
3932    
3933                            conjunctionable = true;
3934    
3935                            if ((roleIds == null) || (roleIds.length > 0)) {
3936                                    if (conjunctionable) {
3937                                            query.append(WHERE_AND);
3938                                    }
3939    
3940                                    query.append(StringPool.OPEN_PARENTHESIS);
3941    
3942                                    for (int i = 0; i < roleIds.length; i++) {
3943                                            query.append(_FINDER_COLUMN_C_N_S_P_R_ROLEID_5);
3944    
3945                                            if ((i + 1) < roleIds.length) {
3946                                                    query.append(WHERE_OR);
3947                                            }
3948                                    }
3949    
3950                                    query.append(StringPool.CLOSE_PARENTHESIS);
3951    
3952                                    conjunctionable = true;
3953                            }
3954    
3955                            String sql = query.toString();
3956    
3957                            Session session = null;
3958    
3959                            try {
3960                                    session = openSession();
3961    
3962                                    Query q = session.createQuery(sql);
3963    
3964                                    QueryPos qPos = QueryPos.getInstance(q);
3965    
3966                                    qPos.add(companyId);
3967    
3968                                    if (name != null) {
3969                                            qPos.add(name);
3970                                    }
3971    
3972                                    qPos.add(scope);
3973    
3974                                    if (primKey != null) {
3975                                            qPos.add(primKey);
3976                                    }
3977    
3978                                    if (roleIds != null) {
3979                                            qPos.add(roleIds);
3980                                    }
3981    
3982                                    count = (Long)q.uniqueResult();
3983    
3984                                    FinderCacheUtil.putResult(FINDER_PATH_WITH_PAGINATION_COUNT_BY_C_N_S_P_R,
3985                                            finderArgs, count);
3986                            }
3987                            catch (Exception e) {
3988                                    FinderCacheUtil.removeResult(FINDER_PATH_WITH_PAGINATION_COUNT_BY_C_N_S_P_R,
3989                                            finderArgs);
3990    
3991                                    throw processException(e);
3992                            }
3993                            finally {
3994                                    closeSession(session);
3995                            }
3996                    }
3997    
3998                    return count.intValue();
3999            }
4000    
4001            private static final String _FINDER_COLUMN_C_N_S_P_R_COMPANYID_2 = "resourcePermission.companyId = ? AND ";
4002            private static final String _FINDER_COLUMN_C_N_S_P_R_COMPANYID_5 = "(" +
4003                    removeConjunction(_FINDER_COLUMN_C_N_S_P_R_COMPANYID_2) + ")";
4004            private static final String _FINDER_COLUMN_C_N_S_P_R_NAME_1 = "resourcePermission.name IS NULL AND ";
4005            private static final String _FINDER_COLUMN_C_N_S_P_R_NAME_2 = "resourcePermission.name = ? AND ";
4006            private static final String _FINDER_COLUMN_C_N_S_P_R_NAME_3 = "(resourcePermission.name IS NULL OR resourcePermission.name = '') AND ";
4007            private static final String _FINDER_COLUMN_C_N_S_P_R_NAME_4 = "(" +
4008                    removeConjunction(_FINDER_COLUMN_C_N_S_P_R_NAME_1) + ")";
4009            private static final String _FINDER_COLUMN_C_N_S_P_R_NAME_5 = "(" +
4010                    removeConjunction(_FINDER_COLUMN_C_N_S_P_R_NAME_2) + ")";
4011            private static final String _FINDER_COLUMN_C_N_S_P_R_NAME_6 = "(" +
4012                    removeConjunction(_FINDER_COLUMN_C_N_S_P_R_NAME_3) + ")";
4013            private static final String _FINDER_COLUMN_C_N_S_P_R_SCOPE_2 = "resourcePermission.scope = ? AND ";
4014            private static final String _FINDER_COLUMN_C_N_S_P_R_SCOPE_5 = "(" +
4015                    removeConjunction(_FINDER_COLUMN_C_N_S_P_R_SCOPE_2) + ")";
4016            private static final String _FINDER_COLUMN_C_N_S_P_R_PRIMKEY_1 = "resourcePermission.primKey IS NULL AND ";
4017            private static final String _FINDER_COLUMN_C_N_S_P_R_PRIMKEY_2 = "resourcePermission.primKey = ? AND ";
4018            private static final String _FINDER_COLUMN_C_N_S_P_R_PRIMKEY_3 = "(resourcePermission.primKey IS NULL OR resourcePermission.primKey = '') AND ";
4019            private static final String _FINDER_COLUMN_C_N_S_P_R_PRIMKEY_4 = "(" +
4020                    removeConjunction(_FINDER_COLUMN_C_N_S_P_R_PRIMKEY_1) + ")";
4021            private static final String _FINDER_COLUMN_C_N_S_P_R_PRIMKEY_5 = "(" +
4022                    removeConjunction(_FINDER_COLUMN_C_N_S_P_R_PRIMKEY_2) + ")";
4023            private static final String _FINDER_COLUMN_C_N_S_P_R_PRIMKEY_6 = "(" +
4024                    removeConjunction(_FINDER_COLUMN_C_N_S_P_R_PRIMKEY_3) + ")";
4025            private static final String _FINDER_COLUMN_C_N_S_P_R_ROLEID_2 = "resourcePermission.roleId = ?";
4026            private static final String _FINDER_COLUMN_C_N_S_P_R_ROLEID_5 = "(" +
4027                    removeConjunction(_FINDER_COLUMN_C_N_S_P_R_ROLEID_2) + ")";
4028    
4029            /**
4030             * Caches the resource permission in the entity cache if it is enabled.
4031             *
4032             * @param resourcePermission the resource permission
4033             */
4034            @Override
4035            public void cacheResult(ResourcePermission resourcePermission) {
4036                    EntityCacheUtil.putResult(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
4037                            ResourcePermissionImpl.class, resourcePermission.getPrimaryKey(),
4038                            resourcePermission);
4039    
4040                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_N_S_P_R,
4041                            new Object[] {
4042                                    resourcePermission.getCompanyId(), resourcePermission.getName(),
4043                                    resourcePermission.getScope(), resourcePermission.getPrimKey(),
4044                                    resourcePermission.getRoleId()
4045                            }, resourcePermission);
4046    
4047                    resourcePermission.resetOriginalValues();
4048            }
4049    
4050            /**
4051             * Caches the resource permissions in the entity cache if it is enabled.
4052             *
4053             * @param resourcePermissions the resource permissions
4054             */
4055            @Override
4056            public void cacheResult(List<ResourcePermission> resourcePermissions) {
4057                    for (ResourcePermission resourcePermission : resourcePermissions) {
4058                            if (EntityCacheUtil.getResult(
4059                                                    ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
4060                                                    ResourcePermissionImpl.class,
4061                                                    resourcePermission.getPrimaryKey()) == null) {
4062                                    cacheResult(resourcePermission);
4063                            }
4064                            else {
4065                                    resourcePermission.resetOriginalValues();
4066                            }
4067                    }
4068            }
4069    
4070            /**
4071             * Clears the cache for all resource permissions.
4072             *
4073             * <p>
4074             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
4075             * </p>
4076             */
4077            @Override
4078            public void clearCache() {
4079                    if (_HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
4080                            CacheRegistryUtil.clear(ResourcePermissionImpl.class.getName());
4081                    }
4082    
4083                    EntityCacheUtil.clearCache(ResourcePermissionImpl.class.getName());
4084    
4085                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
4086                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
4087                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
4088            }
4089    
4090            /**
4091             * Clears the cache for the resource permission.
4092             *
4093             * <p>
4094             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
4095             * </p>
4096             */
4097            @Override
4098            public void clearCache(ResourcePermission resourcePermission) {
4099                    EntityCacheUtil.removeResult(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
4100                            ResourcePermissionImpl.class, resourcePermission.getPrimaryKey());
4101    
4102                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
4103                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
4104    
4105                    clearUniqueFindersCache(resourcePermission);
4106            }
4107    
4108            @Override
4109            public void clearCache(List<ResourcePermission> resourcePermissions) {
4110                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
4111                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
4112    
4113                    for (ResourcePermission resourcePermission : resourcePermissions) {
4114                            EntityCacheUtil.removeResult(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
4115                                    ResourcePermissionImpl.class, resourcePermission.getPrimaryKey());
4116    
4117                            clearUniqueFindersCache(resourcePermission);
4118                    }
4119            }
4120    
4121            protected void cacheUniqueFindersCache(
4122                    ResourcePermission resourcePermission) {
4123                    if (resourcePermission.isNew()) {
4124                            Object[] args = new Object[] {
4125                                            resourcePermission.getCompanyId(),
4126                                            resourcePermission.getName(), resourcePermission.getScope(),
4127                                            resourcePermission.getPrimKey(),
4128                                            resourcePermission.getRoleId()
4129                                    };
4130    
4131                            FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_N_S_P_R, args,
4132                                    Long.valueOf(1));
4133                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_N_S_P_R, args,
4134                                    resourcePermission);
4135                    }
4136                    else {
4137                            ResourcePermissionModelImpl resourcePermissionModelImpl = (ResourcePermissionModelImpl)resourcePermission;
4138    
4139                            if ((resourcePermissionModelImpl.getColumnBitmask() &
4140                                            FINDER_PATH_FETCH_BY_C_N_S_P_R.getColumnBitmask()) != 0) {
4141                                    Object[] args = new Object[] {
4142                                                    resourcePermission.getCompanyId(),
4143                                                    resourcePermission.getName(),
4144                                                    resourcePermission.getScope(),
4145                                                    resourcePermission.getPrimKey(),
4146                                                    resourcePermission.getRoleId()
4147                                            };
4148    
4149                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_N_S_P_R, args,
4150                                            Long.valueOf(1));
4151                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_N_S_P_R, args,
4152                                            resourcePermission);
4153                            }
4154                    }
4155            }
4156    
4157            protected void clearUniqueFindersCache(
4158                    ResourcePermission resourcePermission) {
4159                    ResourcePermissionModelImpl resourcePermissionModelImpl = (ResourcePermissionModelImpl)resourcePermission;
4160    
4161                    Object[] args = new Object[] {
4162                                    resourcePermission.getCompanyId(), resourcePermission.getName(),
4163                                    resourcePermission.getScope(), resourcePermission.getPrimKey(),
4164                                    resourcePermission.getRoleId()
4165                            };
4166    
4167                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_C_N_S_P_R, args);
4168                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_N_S_P_R, args);
4169    
4170                    if ((resourcePermissionModelImpl.getColumnBitmask() &
4171                                    FINDER_PATH_FETCH_BY_C_N_S_P_R.getColumnBitmask()) != 0) {
4172                            args = new Object[] {
4173                                            resourcePermissionModelImpl.getOriginalCompanyId(),
4174                                            resourcePermissionModelImpl.getOriginalName(),
4175                                            resourcePermissionModelImpl.getOriginalScope(),
4176                                            resourcePermissionModelImpl.getOriginalPrimKey(),
4177                                            resourcePermissionModelImpl.getOriginalRoleId()
4178                                    };
4179    
4180                            FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_C_N_S_P_R, args);
4181                            FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_N_S_P_R, args);
4182                    }
4183            }
4184    
4185            /**
4186             * Creates a new resource permission with the primary key. Does not add the resource permission to the database.
4187             *
4188             * @param resourcePermissionId the primary key for the new resource permission
4189             * @return the new resource permission
4190             */
4191            @Override
4192            public ResourcePermission create(long resourcePermissionId) {
4193                    ResourcePermission resourcePermission = new ResourcePermissionImpl();
4194    
4195                    resourcePermission.setNew(true);
4196                    resourcePermission.setPrimaryKey(resourcePermissionId);
4197    
4198                    return resourcePermission;
4199            }
4200    
4201            /**
4202             * Removes the resource permission with the primary key from the database. Also notifies the appropriate model listeners.
4203             *
4204             * @param resourcePermissionId the primary key of the resource permission
4205             * @return the resource permission that was removed
4206             * @throws com.liferay.portal.NoSuchResourcePermissionException if a resource permission with the primary key could not be found
4207             * @throws SystemException if a system exception occurred
4208             */
4209            @Override
4210            public ResourcePermission remove(long resourcePermissionId)
4211                    throws NoSuchResourcePermissionException, SystemException {
4212                    return remove((Serializable)resourcePermissionId);
4213            }
4214    
4215            /**
4216             * Removes the resource permission with the primary key from the database. Also notifies the appropriate model listeners.
4217             *
4218             * @param primaryKey the primary key of the resource permission
4219             * @return the resource permission that was removed
4220             * @throws com.liferay.portal.NoSuchResourcePermissionException if a resource permission with the primary key could not be found
4221             * @throws SystemException if a system exception occurred
4222             */
4223            @Override
4224            public ResourcePermission remove(Serializable primaryKey)
4225                    throws NoSuchResourcePermissionException, SystemException {
4226                    Session session = null;
4227    
4228                    try {
4229                            session = openSession();
4230    
4231                            ResourcePermission resourcePermission = (ResourcePermission)session.get(ResourcePermissionImpl.class,
4232                                            primaryKey);
4233    
4234                            if (resourcePermission == null) {
4235                                    if (_log.isWarnEnabled()) {
4236                                            _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
4237                                    }
4238    
4239                                    throw new NoSuchResourcePermissionException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
4240                                            primaryKey);
4241                            }
4242    
4243                            return remove(resourcePermission);
4244                    }
4245                    catch (NoSuchResourcePermissionException nsee) {
4246                            throw nsee;
4247                    }
4248                    catch (Exception e) {
4249                            throw processException(e);
4250                    }
4251                    finally {
4252                            closeSession(session);
4253                    }
4254            }
4255    
4256            @Override
4257            protected ResourcePermission removeImpl(
4258                    ResourcePermission resourcePermission) throws SystemException {
4259                    resourcePermission = toUnwrappedModel(resourcePermission);
4260    
4261                    Session session = null;
4262    
4263                    try {
4264                            session = openSession();
4265    
4266                            if (!session.contains(resourcePermission)) {
4267                                    resourcePermission = (ResourcePermission)session.get(ResourcePermissionImpl.class,
4268                                                    resourcePermission.getPrimaryKeyObj());
4269                            }
4270    
4271                            if (resourcePermission != null) {
4272                                    session.delete(resourcePermission);
4273                            }
4274                    }
4275                    catch (Exception e) {
4276                            throw processException(e);
4277                    }
4278                    finally {
4279                            closeSession(session);
4280                    }
4281    
4282                    if (resourcePermission != null) {
4283                            clearCache(resourcePermission);
4284                    }
4285    
4286                    return resourcePermission;
4287            }
4288    
4289            @Override
4290            public ResourcePermission updateImpl(
4291                    com.liferay.portal.model.ResourcePermission resourcePermission)
4292                    throws SystemException {
4293                    resourcePermission = toUnwrappedModel(resourcePermission);
4294    
4295                    boolean isNew = resourcePermission.isNew();
4296    
4297                    ResourcePermissionModelImpl resourcePermissionModelImpl = (ResourcePermissionModelImpl)resourcePermission;
4298    
4299                    Session session = null;
4300    
4301                    try {
4302                            session = openSession();
4303    
4304                            if (resourcePermission.isNew()) {
4305                                    session.save(resourcePermission);
4306    
4307                                    resourcePermission.setNew(false);
4308                            }
4309                            else {
4310                                    session.merge(resourcePermission);
4311                            }
4312                    }
4313                    catch (Exception e) {
4314                            throw processException(e);
4315                    }
4316                    finally {
4317                            closeSession(session);
4318                    }
4319    
4320                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
4321    
4322                    if (isNew || !ResourcePermissionModelImpl.COLUMN_BITMASK_ENABLED) {
4323                            FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
4324                    }
4325    
4326                    else {
4327                            if ((resourcePermissionModelImpl.getColumnBitmask() &
4328                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_SCOPE.getColumnBitmask()) != 0) {
4329                                    Object[] args = new Object[] {
4330                                                    resourcePermissionModelImpl.getOriginalScope()
4331                                            };
4332    
4333                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_SCOPE, args);
4334                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_SCOPE,
4335                                            args);
4336    
4337                                    args = new Object[] { resourcePermissionModelImpl.getScope() };
4338    
4339                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_SCOPE, args);
4340                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_SCOPE,
4341                                            args);
4342                            }
4343    
4344                            if ((resourcePermissionModelImpl.getColumnBitmask() &
4345                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_ROLEID.getColumnBitmask()) != 0) {
4346                                    Object[] args = new Object[] {
4347                                                    resourcePermissionModelImpl.getOriginalRoleId()
4348                                            };
4349    
4350                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_ROLEID, args);
4351                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_ROLEID,
4352                                            args);
4353    
4354                                    args = new Object[] { resourcePermissionModelImpl.getRoleId() };
4355    
4356                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_ROLEID, args);
4357                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_ROLEID,
4358                                            args);
4359                            }
4360    
4361                            if ((resourcePermissionModelImpl.getColumnBitmask() &
4362                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_N_S.getColumnBitmask()) != 0) {
4363                                    Object[] args = new Object[] {
4364                                                    resourcePermissionModelImpl.getOriginalCompanyId(),
4365                                                    resourcePermissionModelImpl.getOriginalName(),
4366                                                    resourcePermissionModelImpl.getOriginalScope()
4367                                            };
4368    
4369                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_C_N_S, args);
4370                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_N_S,
4371                                            args);
4372    
4373                                    args = new Object[] {
4374                                                    resourcePermissionModelImpl.getCompanyId(),
4375                                                    resourcePermissionModelImpl.getName(),
4376                                                    resourcePermissionModelImpl.getScope()
4377                                            };
4378    
4379                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_C_N_S, args);
4380                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_N_S,
4381                                            args);
4382                            }
4383    
4384                            if ((resourcePermissionModelImpl.getColumnBitmask() &
4385                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_N_S_P.getColumnBitmask()) != 0) {
4386                                    Object[] args = new Object[] {
4387                                                    resourcePermissionModelImpl.getOriginalCompanyId(),
4388                                                    resourcePermissionModelImpl.getOriginalName(),
4389                                                    resourcePermissionModelImpl.getOriginalScope(),
4390                                                    resourcePermissionModelImpl.getOriginalPrimKey()
4391                                            };
4392    
4393                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_C_N_S_P, args);
4394                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_N_S_P,
4395                                            args);
4396    
4397                                    args = new Object[] {
4398                                                    resourcePermissionModelImpl.getCompanyId(),
4399                                                    resourcePermissionModelImpl.getName(),
4400                                                    resourcePermissionModelImpl.getScope(),
4401                                                    resourcePermissionModelImpl.getPrimKey()
4402                                            };
4403    
4404                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_C_N_S_P, args);
4405                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_N_S_P,
4406                                            args);
4407                            }
4408    
4409                            if ((resourcePermissionModelImpl.getColumnBitmask() &
4410                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_N_S_P_R.getColumnBitmask()) != 0) {
4411                                    Object[] args = new Object[] {
4412                                                    resourcePermissionModelImpl.getOriginalCompanyId(),
4413                                                    resourcePermissionModelImpl.getOriginalName(),
4414                                                    resourcePermissionModelImpl.getOriginalScope(),
4415                                                    resourcePermissionModelImpl.getOriginalPrimKey(),
4416                                                    resourcePermissionModelImpl.getOriginalRoleId()
4417                                            };
4418    
4419                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_C_N_S_P_R,
4420                                            args);
4421                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_N_S_P_R,
4422                                            args);
4423    
4424                                    args = new Object[] {
4425                                                    resourcePermissionModelImpl.getCompanyId(),
4426                                                    resourcePermissionModelImpl.getName(),
4427                                                    resourcePermissionModelImpl.getScope(),
4428                                                    resourcePermissionModelImpl.getPrimKey(),
4429                                                    resourcePermissionModelImpl.getRoleId()
4430                                            };
4431    
4432                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_C_N_S_P_R,
4433                                            args);
4434                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_N_S_P_R,
4435                                            args);
4436                            }
4437                    }
4438    
4439                    EntityCacheUtil.putResult(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
4440                            ResourcePermissionImpl.class, resourcePermission.getPrimaryKey(),
4441                            resourcePermission);
4442    
4443                    clearUniqueFindersCache(resourcePermission);
4444                    cacheUniqueFindersCache(resourcePermission);
4445    
4446                    return resourcePermission;
4447            }
4448    
4449            protected ResourcePermission toUnwrappedModel(
4450                    ResourcePermission resourcePermission) {
4451                    if (resourcePermission instanceof ResourcePermissionImpl) {
4452                            return resourcePermission;
4453                    }
4454    
4455                    ResourcePermissionImpl resourcePermissionImpl = new ResourcePermissionImpl();
4456    
4457                    resourcePermissionImpl.setNew(resourcePermission.isNew());
4458                    resourcePermissionImpl.setPrimaryKey(resourcePermission.getPrimaryKey());
4459    
4460                    resourcePermissionImpl.setResourcePermissionId(resourcePermission.getResourcePermissionId());
4461                    resourcePermissionImpl.setCompanyId(resourcePermission.getCompanyId());
4462                    resourcePermissionImpl.setName(resourcePermission.getName());
4463                    resourcePermissionImpl.setScope(resourcePermission.getScope());
4464                    resourcePermissionImpl.setPrimKey(resourcePermission.getPrimKey());
4465                    resourcePermissionImpl.setRoleId(resourcePermission.getRoleId());
4466                    resourcePermissionImpl.setOwnerId(resourcePermission.getOwnerId());
4467                    resourcePermissionImpl.setActionIds(resourcePermission.getActionIds());
4468    
4469                    return resourcePermissionImpl;
4470            }
4471    
4472            /**
4473             * Returns the resource permission with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
4474             *
4475             * @param primaryKey the primary key of the resource permission
4476             * @return the resource permission
4477             * @throws com.liferay.portal.NoSuchResourcePermissionException if a resource permission with the primary key could not be found
4478             * @throws SystemException if a system exception occurred
4479             */
4480            @Override
4481            public ResourcePermission findByPrimaryKey(Serializable primaryKey)
4482                    throws NoSuchResourcePermissionException, SystemException {
4483                    ResourcePermission resourcePermission = fetchByPrimaryKey(primaryKey);
4484    
4485                    if (resourcePermission == null) {
4486                            if (_log.isWarnEnabled()) {
4487                                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
4488                            }
4489    
4490                            throw new NoSuchResourcePermissionException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
4491                                    primaryKey);
4492                    }
4493    
4494                    return resourcePermission;
4495            }
4496    
4497            /**
4498             * Returns the resource permission with the primary key or throws a {@link com.liferay.portal.NoSuchResourcePermissionException} if it could not be found.
4499             *
4500             * @param resourcePermissionId the primary key of the resource permission
4501             * @return the resource permission
4502             * @throws com.liferay.portal.NoSuchResourcePermissionException if a resource permission with the primary key could not be found
4503             * @throws SystemException if a system exception occurred
4504             */
4505            @Override
4506            public ResourcePermission findByPrimaryKey(long resourcePermissionId)
4507                    throws NoSuchResourcePermissionException, SystemException {
4508                    return findByPrimaryKey((Serializable)resourcePermissionId);
4509            }
4510    
4511            /**
4512             * Returns the resource permission with the primary key or returns <code>null</code> if it could not be found.
4513             *
4514             * @param primaryKey the primary key of the resource permission
4515             * @return the resource permission, or <code>null</code> if a resource permission with the primary key could not be found
4516             * @throws SystemException if a system exception occurred
4517             */
4518            @Override
4519            public ResourcePermission fetchByPrimaryKey(Serializable primaryKey)
4520                    throws SystemException {
4521                    ResourcePermission resourcePermission = (ResourcePermission)EntityCacheUtil.getResult(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
4522                                    ResourcePermissionImpl.class, primaryKey);
4523    
4524                    if (resourcePermission == _nullResourcePermission) {
4525                            return null;
4526                    }
4527    
4528                    if (resourcePermission == null) {
4529                            Session session = null;
4530    
4531                            try {
4532                                    session = openSession();
4533    
4534                                    resourcePermission = (ResourcePermission)session.get(ResourcePermissionImpl.class,
4535                                                    primaryKey);
4536    
4537                                    if (resourcePermission != null) {
4538                                            cacheResult(resourcePermission);
4539                                    }
4540                                    else {
4541                                            EntityCacheUtil.putResult(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
4542                                                    ResourcePermissionImpl.class, primaryKey,
4543                                                    _nullResourcePermission);
4544                                    }
4545                            }
4546                            catch (Exception e) {
4547                                    EntityCacheUtil.removeResult(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
4548                                            ResourcePermissionImpl.class, primaryKey);
4549    
4550                                    throw processException(e);
4551                            }
4552                            finally {
4553                                    closeSession(session);
4554                            }
4555                    }
4556    
4557                    return resourcePermission;
4558            }
4559    
4560            /**
4561             * Returns the resource permission with the primary key or returns <code>null</code> if it could not be found.
4562             *
4563             * @param resourcePermissionId the primary key of the resource permission
4564             * @return the resource permission, or <code>null</code> if a resource permission with the primary key could not be found
4565             * @throws SystemException if a system exception occurred
4566             */
4567            @Override
4568            public ResourcePermission fetchByPrimaryKey(long resourcePermissionId)
4569                    throws SystemException {
4570                    return fetchByPrimaryKey((Serializable)resourcePermissionId);
4571            }
4572    
4573            /**
4574             * Returns all the resource permissions.
4575             *
4576             * @return the resource permissions
4577             * @throws SystemException if a system exception occurred
4578             */
4579            @Override
4580            public List<ResourcePermission> findAll() throws SystemException {
4581                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
4582            }
4583    
4584            /**
4585             * Returns a range of all the resource permissions.
4586             *
4587             * <p>
4588             * 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. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portal.model.impl.ResourcePermissionModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
4589             * </p>
4590             *
4591             * @param start the lower bound of the range of resource permissions
4592             * @param end the upper bound of the range of resource permissions (not inclusive)
4593             * @return the range of resource permissions
4594             * @throws SystemException if a system exception occurred
4595             */
4596            @Override
4597            public List<ResourcePermission> findAll(int start, int end)
4598                    throws SystemException {
4599                    return findAll(start, end, null);
4600            }
4601    
4602            /**
4603             * Returns an ordered range of all the resource permissions.
4604             *
4605             * <p>
4606             * 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. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portal.model.impl.ResourcePermissionModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
4607             * </p>
4608             *
4609             * @param start the lower bound of the range of resource permissions
4610             * @param end the upper bound of the range of resource permissions (not inclusive)
4611             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
4612             * @return the ordered range of resource permissions
4613             * @throws SystemException if a system exception occurred
4614             */
4615            @Override
4616            public List<ResourcePermission> findAll(int start, int end,
4617                    OrderByComparator orderByComparator) throws SystemException {
4618                    boolean pagination = true;
4619                    FinderPath finderPath = null;
4620                    Object[] finderArgs = null;
4621    
4622                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
4623                                    (orderByComparator == null)) {
4624                            pagination = false;
4625                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
4626                            finderArgs = FINDER_ARGS_EMPTY;
4627                    }
4628                    else {
4629                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
4630                            finderArgs = new Object[] { start, end, orderByComparator };
4631                    }
4632    
4633                    List<ResourcePermission> list = (List<ResourcePermission>)FinderCacheUtil.getResult(finderPath,
4634                                    finderArgs, this);
4635    
4636                    if (list == null) {
4637                            StringBundler query = null;
4638                            String sql = null;
4639    
4640                            if (orderByComparator != null) {
4641                                    query = new StringBundler(2 +
4642                                                    (orderByComparator.getOrderByFields().length * 3));
4643    
4644                                    query.append(_SQL_SELECT_RESOURCEPERMISSION);
4645    
4646                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
4647                                            orderByComparator);
4648    
4649                                    sql = query.toString();
4650                            }
4651                            else {
4652                                    sql = _SQL_SELECT_RESOURCEPERMISSION;
4653    
4654                                    if (pagination) {
4655                                            sql = sql.concat(ResourcePermissionModelImpl.ORDER_BY_JPQL);
4656                                    }
4657                            }
4658    
4659                            Session session = null;
4660    
4661                            try {
4662                                    session = openSession();
4663    
4664                                    Query q = session.createQuery(sql);
4665    
4666                                    if (!pagination) {
4667                                            list = (List<ResourcePermission>)QueryUtil.list(q,
4668                                                            getDialect(), start, end, false);
4669    
4670                                            Collections.sort(list);
4671    
4672                                            list = new UnmodifiableList<ResourcePermission>(list);
4673                                    }
4674                                    else {
4675                                            list = (List<ResourcePermission>)QueryUtil.list(q,
4676                                                            getDialect(), start, end);
4677                                    }
4678    
4679                                    cacheResult(list);
4680    
4681                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
4682                            }
4683                            catch (Exception e) {
4684                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
4685    
4686                                    throw processException(e);
4687                            }
4688                            finally {
4689                                    closeSession(session);
4690                            }
4691                    }
4692    
4693                    return list;
4694            }
4695    
4696            /**
4697             * Removes all the resource permissions from the database.
4698             *
4699             * @throws SystemException if a system exception occurred
4700             */
4701            @Override
4702            public void removeAll() throws SystemException {
4703                    for (ResourcePermission resourcePermission : findAll()) {
4704                            remove(resourcePermission);
4705                    }
4706            }
4707    
4708            /**
4709             * Returns the number of resource permissions.
4710             *
4711             * @return the number of resource permissions
4712             * @throws SystemException if a system exception occurred
4713             */
4714            @Override
4715            public int countAll() throws SystemException {
4716                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
4717                                    FINDER_ARGS_EMPTY, this);
4718    
4719                    if (count == null) {
4720                            Session session = null;
4721    
4722                            try {
4723                                    session = openSession();
4724    
4725                                    Query q = session.createQuery(_SQL_COUNT_RESOURCEPERMISSION);
4726    
4727                                    count = (Long)q.uniqueResult();
4728    
4729                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
4730                                            FINDER_ARGS_EMPTY, count);
4731                            }
4732                            catch (Exception e) {
4733                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_ALL,
4734                                            FINDER_ARGS_EMPTY);
4735    
4736                                    throw processException(e);
4737                            }
4738                            finally {
4739                                    closeSession(session);
4740                            }
4741                    }
4742    
4743                    return count.intValue();
4744            }
4745    
4746            /**
4747             * Initializes the resource permission persistence.
4748             */
4749            public void afterPropertiesSet() {
4750                    String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
4751                                            com.liferay.portal.util.PropsUtil.get(
4752                                                    "value.object.listener.com.liferay.portal.model.ResourcePermission")));
4753    
4754                    if (listenerClassNames.length > 0) {
4755                            try {
4756                                    List<ModelListener<ResourcePermission>> listenersList = new ArrayList<ModelListener<ResourcePermission>>();
4757    
4758                                    for (String listenerClassName : listenerClassNames) {
4759                                            listenersList.add((ModelListener<ResourcePermission>)InstanceFactory.newInstance(
4760                                                            getClassLoader(), listenerClassName));
4761                                    }
4762    
4763                                    listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
4764                            }
4765                            catch (Exception e) {
4766                                    _log.error(e);
4767                            }
4768                    }
4769            }
4770    
4771            public void destroy() {
4772                    EntityCacheUtil.removeCache(ResourcePermissionImpl.class.getName());
4773                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
4774                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
4775                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
4776            }
4777    
4778            private static final String _SQL_SELECT_RESOURCEPERMISSION = "SELECT resourcePermission FROM ResourcePermission resourcePermission";
4779            private static final String _SQL_SELECT_RESOURCEPERMISSION_WHERE = "SELECT resourcePermission FROM ResourcePermission resourcePermission WHERE ";
4780            private static final String _SQL_COUNT_RESOURCEPERMISSION = "SELECT COUNT(resourcePermission) FROM ResourcePermission resourcePermission";
4781            private static final String _SQL_COUNT_RESOURCEPERMISSION_WHERE = "SELECT COUNT(resourcePermission) FROM ResourcePermission resourcePermission WHERE ";
4782            private static final String _ORDER_BY_ENTITY_ALIAS = "resourcePermission.";
4783            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No ResourcePermission exists with the primary key ";
4784            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No ResourcePermission exists with the key {";
4785            private static final boolean _HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE = com.liferay.portal.util.PropsValues.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE;
4786            private static Log _log = LogFactoryUtil.getLog(ResourcePermissionPersistenceImpl.class);
4787            private static ResourcePermission _nullResourcePermission = new ResourcePermissionImpl() {
4788                            @Override
4789                            public Object clone() {
4790                                    return this;
4791                            }
4792    
4793                            @Override
4794                            public CacheModel<ResourcePermission> toCacheModel() {
4795                                    return _nullResourcePermissionCacheModel;
4796                            }
4797                    };
4798    
4799            private static CacheModel<ResourcePermission> _nullResourcePermissionCacheModel =
4800                    new CacheModel<ResourcePermission>() {
4801                            @Override
4802                            public ResourcePermission toEntityModel() {
4803                                    return _nullResourcePermission;
4804                            }
4805                    };
4806    }