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