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                            if (name == null) {
3399                                    query.append(_FINDER_COLUMN_C_N_S_P_R_NAME_4);
3400                            }
3401                            else if (name.equals(StringPool.BLANK)) {
3402                                    query.append(_FINDER_COLUMN_C_N_S_P_R_NAME_6);
3403                            }
3404                            else {
3405                                    query.append(_FINDER_COLUMN_C_N_S_P_R_NAME_5);
3406                            }
3407    
3408                            conjunctionable = true;
3409    
3410                            if (conjunctionable) {
3411                                    query.append(WHERE_AND);
3412                            }
3413    
3414                            query.append(_FINDER_COLUMN_C_N_S_P_R_SCOPE_5);
3415    
3416                            conjunctionable = true;
3417    
3418                            if (conjunctionable) {
3419                                    query.append(WHERE_AND);
3420                            }
3421    
3422                            if (primKey == null) {
3423                                    query.append(_FINDER_COLUMN_C_N_S_P_R_PRIMKEY_4);
3424                            }
3425                            else if (primKey.equals(StringPool.BLANK)) {
3426                                    query.append(_FINDER_COLUMN_C_N_S_P_R_PRIMKEY_6);
3427                            }
3428                            else {
3429                                    query.append(_FINDER_COLUMN_C_N_S_P_R_PRIMKEY_5);
3430                            }
3431    
3432                            conjunctionable = true;
3433    
3434                            if ((roleIds == null) || (roleIds.length > 0)) {
3435                                    if (conjunctionable) {
3436                                            query.append(WHERE_AND);
3437                                    }
3438    
3439                                    query.append(StringPool.OPEN_PARENTHESIS);
3440    
3441                                    for (int i = 0; i < roleIds.length; i++) {
3442                                            query.append(_FINDER_COLUMN_C_N_S_P_R_ROLEID_5);
3443    
3444                                            if ((i + 1) < roleIds.length) {
3445                                                    query.append(WHERE_OR);
3446                                            }
3447                                    }
3448    
3449                                    query.append(StringPool.CLOSE_PARENTHESIS);
3450    
3451                                    conjunctionable = true;
3452                            }
3453    
3454                            if (orderByComparator != null) {
3455                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
3456                                            orderByComparator);
3457                            }
3458                            else
3459                             if (pagination) {
3460                                    query.append(ResourcePermissionModelImpl.ORDER_BY_JPQL);
3461                            }
3462    
3463                            String sql = query.toString();
3464    
3465                            Session session = null;
3466    
3467                            try {
3468                                    session = openSession();
3469    
3470                                    Query q = session.createQuery(sql);
3471    
3472                                    QueryPos qPos = QueryPos.getInstance(q);
3473    
3474                                    qPos.add(companyId);
3475    
3476                                    if (name != null) {
3477                                            qPos.add(name);
3478                                    }
3479    
3480                                    qPos.add(scope);
3481    
3482                                    if (primKey != null) {
3483                                            qPos.add(primKey);
3484                                    }
3485    
3486                                    if (roleIds != null) {
3487                                            qPos.add(roleIds);
3488                                    }
3489    
3490                                    if (!pagination) {
3491                                            list = (List<ResourcePermission>)QueryUtil.list(q,
3492                                                            getDialect(), start, end, false);
3493    
3494                                            Collections.sort(list);
3495    
3496                                            list = new UnmodifiableList<ResourcePermission>(list);
3497                                    }
3498                                    else {
3499                                            list = (List<ResourcePermission>)QueryUtil.list(q,
3500                                                            getDialect(), start, end);
3501                                    }
3502    
3503                                    cacheResult(list);
3504    
3505                                    FinderCacheUtil.putResult(FINDER_PATH_WITH_PAGINATION_FIND_BY_C_N_S_P_R,
3506                                            finderArgs, list);
3507                            }
3508                            catch (Exception e) {
3509                                    FinderCacheUtil.removeResult(FINDER_PATH_WITH_PAGINATION_FIND_BY_C_N_S_P_R,
3510                                            finderArgs);
3511    
3512                                    throw processException(e);
3513                            }
3514                            finally {
3515                                    closeSession(session);
3516                            }
3517                    }
3518    
3519                    return list;
3520            }
3521    
3522            /**
3523             * 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.
3524             *
3525             * @param companyId the company ID
3526             * @param name the name
3527             * @param scope the scope
3528             * @param primKey the prim key
3529             * @param roleId the role ID
3530             * @return the matching resource permission
3531             * @throws com.liferay.portal.NoSuchResourcePermissionException if a matching resource permission could not be found
3532             * @throws SystemException if a system exception occurred
3533             */
3534            @Override
3535            public ResourcePermission findByC_N_S_P_R(long companyId, String name,
3536                    int scope, String primKey, long roleId)
3537                    throws NoSuchResourcePermissionException, SystemException {
3538                    ResourcePermission resourcePermission = fetchByC_N_S_P_R(companyId,
3539                                    name, scope, primKey, roleId);
3540    
3541                    if (resourcePermission == null) {
3542                            StringBundler msg = new StringBundler(12);
3543    
3544                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
3545    
3546                            msg.append("companyId=");
3547                            msg.append(companyId);
3548    
3549                            msg.append(", name=");
3550                            msg.append(name);
3551    
3552                            msg.append(", scope=");
3553                            msg.append(scope);
3554    
3555                            msg.append(", primKey=");
3556                            msg.append(primKey);
3557    
3558                            msg.append(", roleId=");
3559                            msg.append(roleId);
3560    
3561                            msg.append(StringPool.CLOSE_CURLY_BRACE);
3562    
3563                            if (_log.isWarnEnabled()) {
3564                                    _log.warn(msg.toString());
3565                            }
3566    
3567                            throw new NoSuchResourcePermissionException(msg.toString());
3568                    }
3569    
3570                    return resourcePermission;
3571            }
3572    
3573            /**
3574             * 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.
3575             *
3576             * @param companyId the company ID
3577             * @param name the name
3578             * @param scope the scope
3579             * @param primKey the prim key
3580             * @param roleId the role ID
3581             * @return the matching resource permission, or <code>null</code> if a matching resource permission could not be found
3582             * @throws SystemException if a system exception occurred
3583             */
3584            @Override
3585            public ResourcePermission fetchByC_N_S_P_R(long companyId, String name,
3586                    int scope, String primKey, long roleId) throws SystemException {
3587                    return fetchByC_N_S_P_R(companyId, name, scope, primKey, roleId, true);
3588            }
3589    
3590            /**
3591             * 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.
3592             *
3593             * @param companyId the company ID
3594             * @param name the name
3595             * @param scope the scope
3596             * @param primKey the prim key
3597             * @param roleId the role ID
3598             * @param retrieveFromCache whether to use the finder cache
3599             * @return the matching resource permission, or <code>null</code> if a matching resource permission could not be found
3600             * @throws SystemException if a system exception occurred
3601             */
3602            @Override
3603            public ResourcePermission fetchByC_N_S_P_R(long companyId, String name,
3604                    int scope, String primKey, long roleId, boolean retrieveFromCache)
3605                    throws SystemException {
3606                    Object[] finderArgs = new Object[] {
3607                                    companyId, name, scope, primKey, roleId
3608                            };
3609    
3610                    Object result = null;
3611    
3612                    if (retrieveFromCache) {
3613                            result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_C_N_S_P_R,
3614                                            finderArgs, this);
3615                    }
3616    
3617                    if (result instanceof ResourcePermission) {
3618                            ResourcePermission resourcePermission = (ResourcePermission)result;
3619    
3620                            if ((companyId != resourcePermission.getCompanyId()) ||
3621                                            !Validator.equals(name, resourcePermission.getName()) ||
3622                                            (scope != resourcePermission.getScope()) ||
3623                                            !Validator.equals(primKey, resourcePermission.getPrimKey()) ||
3624                                            (roleId != resourcePermission.getRoleId())) {
3625                                    result = null;
3626                            }
3627                    }
3628    
3629                    if (result == null) {
3630                            StringBundler query = new StringBundler(7);
3631    
3632                            query.append(_SQL_SELECT_RESOURCEPERMISSION_WHERE);
3633    
3634                            query.append(_FINDER_COLUMN_C_N_S_P_R_COMPANYID_2);
3635    
3636                            boolean bindName = false;
3637    
3638                            if (name == null) {
3639                                    query.append(_FINDER_COLUMN_C_N_S_P_R_NAME_1);
3640                            }
3641                            else if (name.equals(StringPool.BLANK)) {
3642                                    query.append(_FINDER_COLUMN_C_N_S_P_R_NAME_3);
3643                            }
3644                            else {
3645                                    bindName = true;
3646    
3647                                    query.append(_FINDER_COLUMN_C_N_S_P_R_NAME_2);
3648                            }
3649    
3650                            query.append(_FINDER_COLUMN_C_N_S_P_R_SCOPE_2);
3651    
3652                            boolean bindPrimKey = false;
3653    
3654                            if (primKey == null) {
3655                                    query.append(_FINDER_COLUMN_C_N_S_P_R_PRIMKEY_1);
3656                            }
3657                            else if (primKey.equals(StringPool.BLANK)) {
3658                                    query.append(_FINDER_COLUMN_C_N_S_P_R_PRIMKEY_3);
3659                            }
3660                            else {
3661                                    bindPrimKey = true;
3662    
3663                                    query.append(_FINDER_COLUMN_C_N_S_P_R_PRIMKEY_2);
3664                            }
3665    
3666                            query.append(_FINDER_COLUMN_C_N_S_P_R_ROLEID_2);
3667    
3668                            String sql = query.toString();
3669    
3670                            Session session = null;
3671    
3672                            try {
3673                                    session = openSession();
3674    
3675                                    Query q = session.createQuery(sql);
3676    
3677                                    QueryPos qPos = QueryPos.getInstance(q);
3678    
3679                                    qPos.add(companyId);
3680    
3681                                    if (bindName) {
3682                                            qPos.add(name);
3683                                    }
3684    
3685                                    qPos.add(scope);
3686    
3687                                    if (bindPrimKey) {
3688                                            qPos.add(primKey);
3689                                    }
3690    
3691                                    qPos.add(roleId);
3692    
3693                                    List<ResourcePermission> list = q.list();
3694    
3695                                    if (list.isEmpty()) {
3696                                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_N_S_P_R,
3697                                                    finderArgs, list);
3698                                    }
3699                                    else {
3700                                            ResourcePermission resourcePermission = list.get(0);
3701    
3702                                            result = resourcePermission;
3703    
3704                                            cacheResult(resourcePermission);
3705    
3706                                            if ((resourcePermission.getCompanyId() != companyId) ||
3707                                                            (resourcePermission.getName() == null) ||
3708                                                            !resourcePermission.getName().equals(name) ||
3709                                                            (resourcePermission.getScope() != scope) ||
3710                                                            (resourcePermission.getPrimKey() == null) ||
3711                                                            !resourcePermission.getPrimKey().equals(primKey) ||
3712                                                            (resourcePermission.getRoleId() != roleId)) {
3713                                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_N_S_P_R,
3714                                                            finderArgs, resourcePermission);
3715                                            }
3716                                    }
3717                            }
3718                            catch (Exception e) {
3719                                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_N_S_P_R,
3720                                            finderArgs);
3721    
3722                                    throw processException(e);
3723                            }
3724                            finally {
3725                                    closeSession(session);
3726                            }
3727                    }
3728    
3729                    if (result instanceof List<?>) {
3730                            return null;
3731                    }
3732                    else {
3733                            return (ResourcePermission)result;
3734                    }
3735            }
3736    
3737            /**
3738             * Removes the resource permission where companyId = &#63; and name = &#63; and scope = &#63; and primKey = &#63; and roleId = &#63; from the database.
3739             *
3740             * @param companyId the company ID
3741             * @param name the name
3742             * @param scope the scope
3743             * @param primKey the prim key
3744             * @param roleId the role ID
3745             * @return the resource permission that was removed
3746             * @throws SystemException if a system exception occurred
3747             */
3748            @Override
3749            public ResourcePermission removeByC_N_S_P_R(long companyId, String name,
3750                    int scope, String primKey, long roleId)
3751                    throws NoSuchResourcePermissionException, SystemException {
3752                    ResourcePermission resourcePermission = findByC_N_S_P_R(companyId,
3753                                    name, scope, primKey, roleId);
3754    
3755                    return remove(resourcePermission);
3756            }
3757    
3758            /**
3759             * Returns the number of resource permissions where companyId = &#63; and name = &#63; and scope = &#63; and primKey = &#63; and roleId = &#63;.
3760             *
3761             * @param companyId the company ID
3762             * @param name the name
3763             * @param scope the scope
3764             * @param primKey the prim key
3765             * @param roleId the role ID
3766             * @return the number of matching resource permissions
3767             * @throws SystemException if a system exception occurred
3768             */
3769            @Override
3770            public int countByC_N_S_P_R(long companyId, String name, int scope,
3771                    String primKey, long roleId) throws SystemException {
3772                    FinderPath finderPath = FINDER_PATH_COUNT_BY_C_N_S_P_R;
3773    
3774                    Object[] finderArgs = new Object[] {
3775                                    companyId, name, scope, primKey, roleId
3776                            };
3777    
3778                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
3779                                    this);
3780    
3781                    if (count == null) {
3782                            StringBundler query = new StringBundler(6);
3783    
3784                            query.append(_SQL_COUNT_RESOURCEPERMISSION_WHERE);
3785    
3786                            query.append(_FINDER_COLUMN_C_N_S_P_R_COMPANYID_2);
3787    
3788                            boolean bindName = false;
3789    
3790                            if (name == null) {
3791                                    query.append(_FINDER_COLUMN_C_N_S_P_R_NAME_1);
3792                            }
3793                            else if (name.equals(StringPool.BLANK)) {
3794                                    query.append(_FINDER_COLUMN_C_N_S_P_R_NAME_3);
3795                            }
3796                            else {
3797                                    bindName = true;
3798    
3799                                    query.append(_FINDER_COLUMN_C_N_S_P_R_NAME_2);
3800                            }
3801    
3802                            query.append(_FINDER_COLUMN_C_N_S_P_R_SCOPE_2);
3803    
3804                            boolean bindPrimKey = false;
3805    
3806                            if (primKey == null) {
3807                                    query.append(_FINDER_COLUMN_C_N_S_P_R_PRIMKEY_1);
3808                            }
3809                            else if (primKey.equals(StringPool.BLANK)) {
3810                                    query.append(_FINDER_COLUMN_C_N_S_P_R_PRIMKEY_3);
3811                            }
3812                            else {
3813                                    bindPrimKey = true;
3814    
3815                                    query.append(_FINDER_COLUMN_C_N_S_P_R_PRIMKEY_2);
3816                            }
3817    
3818                            query.append(_FINDER_COLUMN_C_N_S_P_R_ROLEID_2);
3819    
3820                            String sql = query.toString();
3821    
3822                            Session session = null;
3823    
3824                            try {
3825                                    session = openSession();
3826    
3827                                    Query q = session.createQuery(sql);
3828    
3829                                    QueryPos qPos = QueryPos.getInstance(q);
3830    
3831                                    qPos.add(companyId);
3832    
3833                                    if (bindName) {
3834                                            qPos.add(name);
3835                                    }
3836    
3837                                    qPos.add(scope);
3838    
3839                                    if (bindPrimKey) {
3840                                            qPos.add(primKey);
3841                                    }
3842    
3843                                    qPos.add(roleId);
3844    
3845                                    count = (Long)q.uniqueResult();
3846    
3847                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
3848                            }
3849                            catch (Exception e) {
3850                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
3851    
3852                                    throw processException(e);
3853                            }
3854                            finally {
3855                                    closeSession(session);
3856                            }
3857                    }
3858    
3859                    return count.intValue();
3860            }
3861    
3862            /**
3863             * Returns the number of resource permissions where companyId = &#63; and name = &#63; and scope = &#63; and primKey = &#63; and roleId = any &#63;.
3864             *
3865             * @param companyId the company ID
3866             * @param name the name
3867             * @param scope the scope
3868             * @param primKey the prim key
3869             * @param roleIds the role IDs
3870             * @return the number of matching resource permissions
3871             * @throws SystemException if a system exception occurred
3872             */
3873            @Override
3874            public int countByC_N_S_P_R(long companyId, String name, int scope,
3875                    String primKey, long[] roleIds) throws SystemException {
3876                    Object[] finderArgs = new Object[] {
3877                                    companyId, name, scope, primKey, StringUtil.merge(roleIds)
3878                            };
3879    
3880                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_WITH_PAGINATION_COUNT_BY_C_N_S_P_R,
3881                                    finderArgs, this);
3882    
3883                    if (count == null) {
3884                            StringBundler query = new StringBundler();
3885    
3886                            query.append(_SQL_COUNT_RESOURCEPERMISSION_WHERE);
3887    
3888                            boolean conjunctionable = false;
3889    
3890                            if (conjunctionable) {
3891                                    query.append(WHERE_AND);
3892                            }
3893    
3894                            query.append(_FINDER_COLUMN_C_N_S_P_R_COMPANYID_5);
3895    
3896                            conjunctionable = true;
3897    
3898                            if (conjunctionable) {
3899                                    query.append(WHERE_AND);
3900                            }
3901    
3902                            if (name == null) {
3903                                    query.append(_FINDER_COLUMN_C_N_S_P_R_NAME_4);
3904                            }
3905                            else if (name.equals(StringPool.BLANK)) {
3906                                    query.append(_FINDER_COLUMN_C_N_S_P_R_NAME_6);
3907                            }
3908                            else {
3909                                    query.append(_FINDER_COLUMN_C_N_S_P_R_NAME_5);
3910                            }
3911    
3912                            conjunctionable = true;
3913    
3914                            if (conjunctionable) {
3915                                    query.append(WHERE_AND);
3916                            }
3917    
3918                            query.append(_FINDER_COLUMN_C_N_S_P_R_SCOPE_5);
3919    
3920                            conjunctionable = true;
3921    
3922                            if (conjunctionable) {
3923                                    query.append(WHERE_AND);
3924                            }
3925    
3926                            if (primKey == null) {
3927                                    query.append(_FINDER_COLUMN_C_N_S_P_R_PRIMKEY_4);
3928                            }
3929                            else if (primKey.equals(StringPool.BLANK)) {
3930                                    query.append(_FINDER_COLUMN_C_N_S_P_R_PRIMKEY_6);
3931                            }
3932                            else {
3933                                    query.append(_FINDER_COLUMN_C_N_S_P_R_PRIMKEY_5);
3934                            }
3935    
3936                            conjunctionable = true;
3937    
3938                            if ((roleIds == null) || (roleIds.length > 0)) {
3939                                    if (conjunctionable) {
3940                                            query.append(WHERE_AND);
3941                                    }
3942    
3943                                    query.append(StringPool.OPEN_PARENTHESIS);
3944    
3945                                    for (int i = 0; i < roleIds.length; i++) {
3946                                            query.append(_FINDER_COLUMN_C_N_S_P_R_ROLEID_5);
3947    
3948                                            if ((i + 1) < roleIds.length) {
3949                                                    query.append(WHERE_OR);
3950                                            }
3951                                    }
3952    
3953                                    query.append(StringPool.CLOSE_PARENTHESIS);
3954    
3955                                    conjunctionable = true;
3956                            }
3957    
3958                            String sql = query.toString();
3959    
3960                            Session session = null;
3961    
3962                            try {
3963                                    session = openSession();
3964    
3965                                    Query q = session.createQuery(sql);
3966    
3967                                    QueryPos qPos = QueryPos.getInstance(q);
3968    
3969                                    qPos.add(companyId);
3970    
3971                                    if (name != null) {
3972                                            qPos.add(name);
3973                                    }
3974    
3975                                    qPos.add(scope);
3976    
3977                                    if (primKey != null) {
3978                                            qPos.add(primKey);
3979                                    }
3980    
3981                                    if (roleIds != null) {
3982                                            qPos.add(roleIds);
3983                                    }
3984    
3985                                    count = (Long)q.uniqueResult();
3986    
3987                                    FinderCacheUtil.putResult(FINDER_PATH_WITH_PAGINATION_COUNT_BY_C_N_S_P_R,
3988                                            finderArgs, count);
3989                            }
3990                            catch (Exception e) {
3991                                    FinderCacheUtil.removeResult(FINDER_PATH_WITH_PAGINATION_COUNT_BY_C_N_S_P_R,
3992                                            finderArgs);
3993    
3994                                    throw processException(e);
3995                            }
3996                            finally {
3997                                    closeSession(session);
3998                            }
3999                    }
4000    
4001                    return count.intValue();
4002            }
4003    
4004            private static final String _FINDER_COLUMN_C_N_S_P_R_COMPANYID_2 = "resourcePermission.companyId = ? AND ";
4005            private static final String _FINDER_COLUMN_C_N_S_P_R_COMPANYID_5 = "(" +
4006                    removeConjunction(_FINDER_COLUMN_C_N_S_P_R_COMPANYID_2) + ")";
4007            private static final String _FINDER_COLUMN_C_N_S_P_R_NAME_1 = "resourcePermission.name IS NULL AND ";
4008            private static final String _FINDER_COLUMN_C_N_S_P_R_NAME_2 = "resourcePermission.name = ? AND ";
4009            private static final String _FINDER_COLUMN_C_N_S_P_R_NAME_3 = "(resourcePermission.name IS NULL OR resourcePermission.name = '') AND ";
4010            private static final String _FINDER_COLUMN_C_N_S_P_R_NAME_4 = "(" +
4011                    removeConjunction(_FINDER_COLUMN_C_N_S_P_R_NAME_1) + ")";
4012            private static final String _FINDER_COLUMN_C_N_S_P_R_NAME_5 = "(" +
4013                    removeConjunction(_FINDER_COLUMN_C_N_S_P_R_NAME_2) + ")";
4014            private static final String _FINDER_COLUMN_C_N_S_P_R_NAME_6 = "(" +
4015                    removeConjunction(_FINDER_COLUMN_C_N_S_P_R_NAME_3) + ")";
4016            private static final String _FINDER_COLUMN_C_N_S_P_R_SCOPE_2 = "resourcePermission.scope = ? AND ";
4017            private static final String _FINDER_COLUMN_C_N_S_P_R_SCOPE_5 = "(" +
4018                    removeConjunction(_FINDER_COLUMN_C_N_S_P_R_SCOPE_2) + ")";
4019            private static final String _FINDER_COLUMN_C_N_S_P_R_PRIMKEY_1 = "resourcePermission.primKey IS NULL AND ";
4020            private static final String _FINDER_COLUMN_C_N_S_P_R_PRIMKEY_2 = "resourcePermission.primKey = ? AND ";
4021            private static final String _FINDER_COLUMN_C_N_S_P_R_PRIMKEY_3 = "(resourcePermission.primKey IS NULL OR resourcePermission.primKey = '') AND ";
4022            private static final String _FINDER_COLUMN_C_N_S_P_R_PRIMKEY_4 = "(" +
4023                    removeConjunction(_FINDER_COLUMN_C_N_S_P_R_PRIMKEY_1) + ")";
4024            private static final String _FINDER_COLUMN_C_N_S_P_R_PRIMKEY_5 = "(" +
4025                    removeConjunction(_FINDER_COLUMN_C_N_S_P_R_PRIMKEY_2) + ")";
4026            private static final String _FINDER_COLUMN_C_N_S_P_R_PRIMKEY_6 = "(" +
4027                    removeConjunction(_FINDER_COLUMN_C_N_S_P_R_PRIMKEY_3) + ")";
4028            private static final String _FINDER_COLUMN_C_N_S_P_R_ROLEID_2 = "resourcePermission.roleId = ?";
4029            private static final String _FINDER_COLUMN_C_N_S_P_R_ROLEID_5 = "(" +
4030                    removeConjunction(_FINDER_COLUMN_C_N_S_P_R_ROLEID_2) + ")";
4031    
4032            public ResourcePermissionPersistenceImpl() {
4033                    setModelClass(ResourcePermission.class);
4034            }
4035    
4036            /**
4037             * Caches the resource permission in the entity cache if it is enabled.
4038             *
4039             * @param resourcePermission the resource permission
4040             */
4041            @Override
4042            public void cacheResult(ResourcePermission resourcePermission) {
4043                    EntityCacheUtil.putResult(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
4044                            ResourcePermissionImpl.class, resourcePermission.getPrimaryKey(),
4045                            resourcePermission);
4046    
4047                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_N_S_P_R,
4048                            new Object[] {
4049                                    resourcePermission.getCompanyId(), resourcePermission.getName(),
4050                                    resourcePermission.getScope(), resourcePermission.getPrimKey(),
4051                                    resourcePermission.getRoleId()
4052                            }, resourcePermission);
4053    
4054                    resourcePermission.resetOriginalValues();
4055            }
4056    
4057            /**
4058             * Caches the resource permissions in the entity cache if it is enabled.
4059             *
4060             * @param resourcePermissions the resource permissions
4061             */
4062            @Override
4063            public void cacheResult(List<ResourcePermission> resourcePermissions) {
4064                    for (ResourcePermission resourcePermission : resourcePermissions) {
4065                            if (EntityCacheUtil.getResult(
4066                                                    ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
4067                                                    ResourcePermissionImpl.class,
4068                                                    resourcePermission.getPrimaryKey()) == null) {
4069                                    cacheResult(resourcePermission);
4070                            }
4071                            else {
4072                                    resourcePermission.resetOriginalValues();
4073                            }
4074                    }
4075            }
4076    
4077            /**
4078             * Clears the cache for all resource permissions.
4079             *
4080             * <p>
4081             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
4082             * </p>
4083             */
4084            @Override
4085            public void clearCache() {
4086                    if (_HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
4087                            CacheRegistryUtil.clear(ResourcePermissionImpl.class.getName());
4088                    }
4089    
4090                    EntityCacheUtil.clearCache(ResourcePermissionImpl.class.getName());
4091    
4092                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
4093                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
4094                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
4095            }
4096    
4097            /**
4098             * Clears the cache for the resource permission.
4099             *
4100             * <p>
4101             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
4102             * </p>
4103             */
4104            @Override
4105            public void clearCache(ResourcePermission resourcePermission) {
4106                    EntityCacheUtil.removeResult(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
4107                            ResourcePermissionImpl.class, resourcePermission.getPrimaryKey());
4108    
4109                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
4110                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
4111    
4112                    clearUniqueFindersCache(resourcePermission);
4113            }
4114    
4115            @Override
4116            public void clearCache(List<ResourcePermission> resourcePermissions) {
4117                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
4118                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
4119    
4120                    for (ResourcePermission resourcePermission : resourcePermissions) {
4121                            EntityCacheUtil.removeResult(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
4122                                    ResourcePermissionImpl.class, resourcePermission.getPrimaryKey());
4123    
4124                            clearUniqueFindersCache(resourcePermission);
4125                    }
4126            }
4127    
4128            protected void cacheUniqueFindersCache(
4129                    ResourcePermission resourcePermission) {
4130                    if (resourcePermission.isNew()) {
4131                            Object[] args = new Object[] {
4132                                            resourcePermission.getCompanyId(),
4133                                            resourcePermission.getName(), resourcePermission.getScope(),
4134                                            resourcePermission.getPrimKey(),
4135                                            resourcePermission.getRoleId()
4136                                    };
4137    
4138                            FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_N_S_P_R, args,
4139                                    Long.valueOf(1));
4140                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_N_S_P_R, args,
4141                                    resourcePermission);
4142                    }
4143                    else {
4144                            ResourcePermissionModelImpl resourcePermissionModelImpl = (ResourcePermissionModelImpl)resourcePermission;
4145    
4146                            if ((resourcePermissionModelImpl.getColumnBitmask() &
4147                                            FINDER_PATH_FETCH_BY_C_N_S_P_R.getColumnBitmask()) != 0) {
4148                                    Object[] args = new Object[] {
4149                                                    resourcePermission.getCompanyId(),
4150                                                    resourcePermission.getName(),
4151                                                    resourcePermission.getScope(),
4152                                                    resourcePermission.getPrimKey(),
4153                                                    resourcePermission.getRoleId()
4154                                            };
4155    
4156                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_N_S_P_R, args,
4157                                            Long.valueOf(1));
4158                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_N_S_P_R, args,
4159                                            resourcePermission);
4160                            }
4161                    }
4162            }
4163    
4164            protected void clearUniqueFindersCache(
4165                    ResourcePermission resourcePermission) {
4166                    ResourcePermissionModelImpl resourcePermissionModelImpl = (ResourcePermissionModelImpl)resourcePermission;
4167    
4168                    Object[] args = new Object[] {
4169                                    resourcePermission.getCompanyId(), resourcePermission.getName(),
4170                                    resourcePermission.getScope(), resourcePermission.getPrimKey(),
4171                                    resourcePermission.getRoleId()
4172                            };
4173    
4174                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_C_N_S_P_R, args);
4175                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_N_S_P_R, args);
4176    
4177                    if ((resourcePermissionModelImpl.getColumnBitmask() &
4178                                    FINDER_PATH_FETCH_BY_C_N_S_P_R.getColumnBitmask()) != 0) {
4179                            args = new Object[] {
4180                                            resourcePermissionModelImpl.getOriginalCompanyId(),
4181                                            resourcePermissionModelImpl.getOriginalName(),
4182                                            resourcePermissionModelImpl.getOriginalScope(),
4183                                            resourcePermissionModelImpl.getOriginalPrimKey(),
4184                                            resourcePermissionModelImpl.getOriginalRoleId()
4185                                    };
4186    
4187                            FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_C_N_S_P_R, args);
4188                            FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_N_S_P_R, args);
4189                    }
4190            }
4191    
4192            /**
4193             * Creates a new resource permission with the primary key. Does not add the resource permission to the database.
4194             *
4195             * @param resourcePermissionId the primary key for the new resource permission
4196             * @return the new resource permission
4197             */
4198            @Override
4199            public ResourcePermission create(long resourcePermissionId) {
4200                    ResourcePermission resourcePermission = new ResourcePermissionImpl();
4201    
4202                    resourcePermission.setNew(true);
4203                    resourcePermission.setPrimaryKey(resourcePermissionId);
4204    
4205                    return resourcePermission;
4206            }
4207    
4208            /**
4209             * Removes the resource permission with the primary key from the database. Also notifies the appropriate model listeners.
4210             *
4211             * @param resourcePermissionId the primary key of the resource permission
4212             * @return the resource permission that was removed
4213             * @throws com.liferay.portal.NoSuchResourcePermissionException if a resource permission with the primary key could not be found
4214             * @throws SystemException if a system exception occurred
4215             */
4216            @Override
4217            public ResourcePermission remove(long resourcePermissionId)
4218                    throws NoSuchResourcePermissionException, SystemException {
4219                    return remove((Serializable)resourcePermissionId);
4220            }
4221    
4222            /**
4223             * Removes the resource permission with the primary key from the database. Also notifies the appropriate model listeners.
4224             *
4225             * @param primaryKey the primary key of the resource permission
4226             * @return the resource permission that was removed
4227             * @throws com.liferay.portal.NoSuchResourcePermissionException if a resource permission with the primary key could not be found
4228             * @throws SystemException if a system exception occurred
4229             */
4230            @Override
4231            public ResourcePermission remove(Serializable primaryKey)
4232                    throws NoSuchResourcePermissionException, SystemException {
4233                    Session session = null;
4234    
4235                    try {
4236                            session = openSession();
4237    
4238                            ResourcePermission resourcePermission = (ResourcePermission)session.get(ResourcePermissionImpl.class,
4239                                            primaryKey);
4240    
4241                            if (resourcePermission == null) {
4242                                    if (_log.isWarnEnabled()) {
4243                                            _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
4244                                    }
4245    
4246                                    throw new NoSuchResourcePermissionException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
4247                                            primaryKey);
4248                            }
4249    
4250                            return remove(resourcePermission);
4251                    }
4252                    catch (NoSuchResourcePermissionException nsee) {
4253                            throw nsee;
4254                    }
4255                    catch (Exception e) {
4256                            throw processException(e);
4257                    }
4258                    finally {
4259                            closeSession(session);
4260                    }
4261            }
4262    
4263            @Override
4264            protected ResourcePermission removeImpl(
4265                    ResourcePermission resourcePermission) throws SystemException {
4266                    resourcePermission = toUnwrappedModel(resourcePermission);
4267    
4268                    Session session = null;
4269    
4270                    try {
4271                            session = openSession();
4272    
4273                            if (!session.contains(resourcePermission)) {
4274                                    resourcePermission = (ResourcePermission)session.get(ResourcePermissionImpl.class,
4275                                                    resourcePermission.getPrimaryKeyObj());
4276                            }
4277    
4278                            if (resourcePermission != null) {
4279                                    session.delete(resourcePermission);
4280                            }
4281                    }
4282                    catch (Exception e) {
4283                            throw processException(e);
4284                    }
4285                    finally {
4286                            closeSession(session);
4287                    }
4288    
4289                    if (resourcePermission != null) {
4290                            clearCache(resourcePermission);
4291                    }
4292    
4293                    return resourcePermission;
4294            }
4295    
4296            @Override
4297            public ResourcePermission updateImpl(
4298                    com.liferay.portal.model.ResourcePermission resourcePermission)
4299                    throws SystemException {
4300                    resourcePermission = toUnwrappedModel(resourcePermission);
4301    
4302                    boolean isNew = resourcePermission.isNew();
4303    
4304                    ResourcePermissionModelImpl resourcePermissionModelImpl = (ResourcePermissionModelImpl)resourcePermission;
4305    
4306                    Session session = null;
4307    
4308                    try {
4309                            session = openSession();
4310    
4311                            if (resourcePermission.isNew()) {
4312                                    session.save(resourcePermission);
4313    
4314                                    resourcePermission.setNew(false);
4315                            }
4316                            else {
4317                                    session.merge(resourcePermission);
4318                            }
4319                    }
4320                    catch (Exception e) {
4321                            throw processException(e);
4322                    }
4323                    finally {
4324                            closeSession(session);
4325                    }
4326    
4327                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
4328    
4329                    if (isNew || !ResourcePermissionModelImpl.COLUMN_BITMASK_ENABLED) {
4330                            FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
4331                    }
4332    
4333                    else {
4334                            if ((resourcePermissionModelImpl.getColumnBitmask() &
4335                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_SCOPE.getColumnBitmask()) != 0) {
4336                                    Object[] args = new Object[] {
4337                                                    resourcePermissionModelImpl.getOriginalScope()
4338                                            };
4339    
4340                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_SCOPE, args);
4341                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_SCOPE,
4342                                            args);
4343    
4344                                    args = new Object[] { resourcePermissionModelImpl.getScope() };
4345    
4346                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_SCOPE, args);
4347                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_SCOPE,
4348                                            args);
4349                            }
4350    
4351                            if ((resourcePermissionModelImpl.getColumnBitmask() &
4352                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_ROLEID.getColumnBitmask()) != 0) {
4353                                    Object[] args = new Object[] {
4354                                                    resourcePermissionModelImpl.getOriginalRoleId()
4355                                            };
4356    
4357                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_ROLEID, args);
4358                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_ROLEID,
4359                                            args);
4360    
4361                                    args = new Object[] { resourcePermissionModelImpl.getRoleId() };
4362    
4363                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_ROLEID, args);
4364                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_ROLEID,
4365                                            args);
4366                            }
4367    
4368                            if ((resourcePermissionModelImpl.getColumnBitmask() &
4369                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_N_S.getColumnBitmask()) != 0) {
4370                                    Object[] args = new Object[] {
4371                                                    resourcePermissionModelImpl.getOriginalCompanyId(),
4372                                                    resourcePermissionModelImpl.getOriginalName(),
4373                                                    resourcePermissionModelImpl.getOriginalScope()
4374                                            };
4375    
4376                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_C_N_S, args);
4377                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_N_S,
4378                                            args);
4379    
4380                                    args = new Object[] {
4381                                                    resourcePermissionModelImpl.getCompanyId(),
4382                                                    resourcePermissionModelImpl.getName(),
4383                                                    resourcePermissionModelImpl.getScope()
4384                                            };
4385    
4386                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_C_N_S, args);
4387                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_N_S,
4388                                            args);
4389                            }
4390    
4391                            if ((resourcePermissionModelImpl.getColumnBitmask() &
4392                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_N_S_P.getColumnBitmask()) != 0) {
4393                                    Object[] args = new Object[] {
4394                                                    resourcePermissionModelImpl.getOriginalCompanyId(),
4395                                                    resourcePermissionModelImpl.getOriginalName(),
4396                                                    resourcePermissionModelImpl.getOriginalScope(),
4397                                                    resourcePermissionModelImpl.getOriginalPrimKey()
4398                                            };
4399    
4400                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_C_N_S_P, args);
4401                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_N_S_P,
4402                                            args);
4403    
4404                                    args = new Object[] {
4405                                                    resourcePermissionModelImpl.getCompanyId(),
4406                                                    resourcePermissionModelImpl.getName(),
4407                                                    resourcePermissionModelImpl.getScope(),
4408                                                    resourcePermissionModelImpl.getPrimKey()
4409                                            };
4410    
4411                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_C_N_S_P, args);
4412                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_N_S_P,
4413                                            args);
4414                            }
4415    
4416                            if ((resourcePermissionModelImpl.getColumnBitmask() &
4417                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_N_S_P_R.getColumnBitmask()) != 0) {
4418                                    Object[] args = new Object[] {
4419                                                    resourcePermissionModelImpl.getOriginalCompanyId(),
4420                                                    resourcePermissionModelImpl.getOriginalName(),
4421                                                    resourcePermissionModelImpl.getOriginalScope(),
4422                                                    resourcePermissionModelImpl.getOriginalPrimKey(),
4423                                                    resourcePermissionModelImpl.getOriginalRoleId()
4424                                            };
4425    
4426                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_C_N_S_P_R,
4427                                            args);
4428                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_N_S_P_R,
4429                                            args);
4430    
4431                                    args = new Object[] {
4432                                                    resourcePermissionModelImpl.getCompanyId(),
4433                                                    resourcePermissionModelImpl.getName(),
4434                                                    resourcePermissionModelImpl.getScope(),
4435                                                    resourcePermissionModelImpl.getPrimKey(),
4436                                                    resourcePermissionModelImpl.getRoleId()
4437                                            };
4438    
4439                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_C_N_S_P_R,
4440                                            args);
4441                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_N_S_P_R,
4442                                            args);
4443                            }
4444                    }
4445    
4446                    EntityCacheUtil.putResult(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
4447                            ResourcePermissionImpl.class, resourcePermission.getPrimaryKey(),
4448                            resourcePermission);
4449    
4450                    clearUniqueFindersCache(resourcePermission);
4451                    cacheUniqueFindersCache(resourcePermission);
4452    
4453                    return resourcePermission;
4454            }
4455    
4456            protected ResourcePermission toUnwrappedModel(
4457                    ResourcePermission resourcePermission) {
4458                    if (resourcePermission instanceof ResourcePermissionImpl) {
4459                            return resourcePermission;
4460                    }
4461    
4462                    ResourcePermissionImpl resourcePermissionImpl = new ResourcePermissionImpl();
4463    
4464                    resourcePermissionImpl.setNew(resourcePermission.isNew());
4465                    resourcePermissionImpl.setPrimaryKey(resourcePermission.getPrimaryKey());
4466    
4467                    resourcePermissionImpl.setResourcePermissionId(resourcePermission.getResourcePermissionId());
4468                    resourcePermissionImpl.setCompanyId(resourcePermission.getCompanyId());
4469                    resourcePermissionImpl.setName(resourcePermission.getName());
4470                    resourcePermissionImpl.setScope(resourcePermission.getScope());
4471                    resourcePermissionImpl.setPrimKey(resourcePermission.getPrimKey());
4472                    resourcePermissionImpl.setRoleId(resourcePermission.getRoleId());
4473                    resourcePermissionImpl.setOwnerId(resourcePermission.getOwnerId());
4474                    resourcePermissionImpl.setActionIds(resourcePermission.getActionIds());
4475    
4476                    return resourcePermissionImpl;
4477            }
4478    
4479            /**
4480             * Returns the resource permission with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
4481             *
4482             * @param primaryKey the primary key of the resource permission
4483             * @return the resource permission
4484             * @throws com.liferay.portal.NoSuchResourcePermissionException if a resource permission with the primary key could not be found
4485             * @throws SystemException if a system exception occurred
4486             */
4487            @Override
4488            public ResourcePermission findByPrimaryKey(Serializable primaryKey)
4489                    throws NoSuchResourcePermissionException, SystemException {
4490                    ResourcePermission resourcePermission = fetchByPrimaryKey(primaryKey);
4491    
4492                    if (resourcePermission == null) {
4493                            if (_log.isWarnEnabled()) {
4494                                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
4495                            }
4496    
4497                            throw new NoSuchResourcePermissionException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
4498                                    primaryKey);
4499                    }
4500    
4501                    return resourcePermission;
4502            }
4503    
4504            /**
4505             * Returns the resource permission with the primary key or throws a {@link com.liferay.portal.NoSuchResourcePermissionException} if it could not be found.
4506             *
4507             * @param resourcePermissionId the primary key of the resource permission
4508             * @return the resource permission
4509             * @throws com.liferay.portal.NoSuchResourcePermissionException if a resource permission with the primary key could not be found
4510             * @throws SystemException if a system exception occurred
4511             */
4512            @Override
4513            public ResourcePermission findByPrimaryKey(long resourcePermissionId)
4514                    throws NoSuchResourcePermissionException, SystemException {
4515                    return findByPrimaryKey((Serializable)resourcePermissionId);
4516            }
4517    
4518            /**
4519             * Returns the resource permission with the primary key or returns <code>null</code> if it could not be found.
4520             *
4521             * @param primaryKey the primary key of the resource permission
4522             * @return the resource permission, or <code>null</code> if a resource permission with the primary key could not be found
4523             * @throws SystemException if a system exception occurred
4524             */
4525            @Override
4526            public ResourcePermission fetchByPrimaryKey(Serializable primaryKey)
4527                    throws SystemException {
4528                    ResourcePermission resourcePermission = (ResourcePermission)EntityCacheUtil.getResult(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
4529                                    ResourcePermissionImpl.class, primaryKey);
4530    
4531                    if (resourcePermission == _nullResourcePermission) {
4532                            return null;
4533                    }
4534    
4535                    if (resourcePermission == null) {
4536                            Session session = null;
4537    
4538                            try {
4539                                    session = openSession();
4540    
4541                                    resourcePermission = (ResourcePermission)session.get(ResourcePermissionImpl.class,
4542                                                    primaryKey);
4543    
4544                                    if (resourcePermission != null) {
4545                                            cacheResult(resourcePermission);
4546                                    }
4547                                    else {
4548                                            EntityCacheUtil.putResult(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
4549                                                    ResourcePermissionImpl.class, primaryKey,
4550                                                    _nullResourcePermission);
4551                                    }
4552                            }
4553                            catch (Exception e) {
4554                                    EntityCacheUtil.removeResult(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
4555                                            ResourcePermissionImpl.class, primaryKey);
4556    
4557                                    throw processException(e);
4558                            }
4559                            finally {
4560                                    closeSession(session);
4561                            }
4562                    }
4563    
4564                    return resourcePermission;
4565            }
4566    
4567            /**
4568             * Returns the resource permission with the primary key or returns <code>null</code> if it could not be found.
4569             *
4570             * @param resourcePermissionId the primary key of the resource permission
4571             * @return the resource permission, or <code>null</code> if a resource permission with the primary key could not be found
4572             * @throws SystemException if a system exception occurred
4573             */
4574            @Override
4575            public ResourcePermission fetchByPrimaryKey(long resourcePermissionId)
4576                    throws SystemException {
4577                    return fetchByPrimaryKey((Serializable)resourcePermissionId);
4578            }
4579    
4580            /**
4581             * Returns all the resource permissions.
4582             *
4583             * @return the resource permissions
4584             * @throws SystemException if a system exception occurred
4585             */
4586            @Override
4587            public List<ResourcePermission> findAll() throws SystemException {
4588                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
4589            }
4590    
4591            /**
4592             * Returns a range of all the resource permissions.
4593             *
4594             * <p>
4595             * 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.
4596             * </p>
4597             *
4598             * @param start the lower bound of the range of resource permissions
4599             * @param end the upper bound of the range of resource permissions (not inclusive)
4600             * @return the range of resource permissions
4601             * @throws SystemException if a system exception occurred
4602             */
4603            @Override
4604            public List<ResourcePermission> findAll(int start, int end)
4605                    throws SystemException {
4606                    return findAll(start, end, null);
4607            }
4608    
4609            /**
4610             * Returns an ordered range of all the resource permissions.
4611             *
4612             * <p>
4613             * 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.
4614             * </p>
4615             *
4616             * @param start the lower bound of the range of resource permissions
4617             * @param end the upper bound of the range of resource permissions (not inclusive)
4618             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
4619             * @return the ordered range of resource permissions
4620             * @throws SystemException if a system exception occurred
4621             */
4622            @Override
4623            public List<ResourcePermission> findAll(int start, int end,
4624                    OrderByComparator orderByComparator) throws SystemException {
4625                    boolean pagination = true;
4626                    FinderPath finderPath = null;
4627                    Object[] finderArgs = null;
4628    
4629                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
4630                                    (orderByComparator == null)) {
4631                            pagination = false;
4632                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
4633                            finderArgs = FINDER_ARGS_EMPTY;
4634                    }
4635                    else {
4636                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
4637                            finderArgs = new Object[] { start, end, orderByComparator };
4638                    }
4639    
4640                    List<ResourcePermission> list = (List<ResourcePermission>)FinderCacheUtil.getResult(finderPath,
4641                                    finderArgs, this);
4642    
4643                    if (list == null) {
4644                            StringBundler query = null;
4645                            String sql = null;
4646    
4647                            if (orderByComparator != null) {
4648                                    query = new StringBundler(2 +
4649                                                    (orderByComparator.getOrderByFields().length * 3));
4650    
4651                                    query.append(_SQL_SELECT_RESOURCEPERMISSION);
4652    
4653                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
4654                                            orderByComparator);
4655    
4656                                    sql = query.toString();
4657                            }
4658                            else {
4659                                    sql = _SQL_SELECT_RESOURCEPERMISSION;
4660    
4661                                    if (pagination) {
4662                                            sql = sql.concat(ResourcePermissionModelImpl.ORDER_BY_JPQL);
4663                                    }
4664                            }
4665    
4666                            Session session = null;
4667    
4668                            try {
4669                                    session = openSession();
4670    
4671                                    Query q = session.createQuery(sql);
4672    
4673                                    if (!pagination) {
4674                                            list = (List<ResourcePermission>)QueryUtil.list(q,
4675                                                            getDialect(), start, end, false);
4676    
4677                                            Collections.sort(list);
4678    
4679                                            list = new UnmodifiableList<ResourcePermission>(list);
4680                                    }
4681                                    else {
4682                                            list = (List<ResourcePermission>)QueryUtil.list(q,
4683                                                            getDialect(), start, end);
4684                                    }
4685    
4686                                    cacheResult(list);
4687    
4688                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
4689                            }
4690                            catch (Exception e) {
4691                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
4692    
4693                                    throw processException(e);
4694                            }
4695                            finally {
4696                                    closeSession(session);
4697                            }
4698                    }
4699    
4700                    return list;
4701            }
4702    
4703            /**
4704             * Removes all the resource permissions from the database.
4705             *
4706             * @throws SystemException if a system exception occurred
4707             */
4708            @Override
4709            public void removeAll() throws SystemException {
4710                    for (ResourcePermission resourcePermission : findAll()) {
4711                            remove(resourcePermission);
4712                    }
4713            }
4714    
4715            /**
4716             * Returns the number of resource permissions.
4717             *
4718             * @return the number of resource permissions
4719             * @throws SystemException if a system exception occurred
4720             */
4721            @Override
4722            public int countAll() throws SystemException {
4723                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
4724                                    FINDER_ARGS_EMPTY, this);
4725    
4726                    if (count == null) {
4727                            Session session = null;
4728    
4729                            try {
4730                                    session = openSession();
4731    
4732                                    Query q = session.createQuery(_SQL_COUNT_RESOURCEPERMISSION);
4733    
4734                                    count = (Long)q.uniqueResult();
4735    
4736                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
4737                                            FINDER_ARGS_EMPTY, count);
4738                            }
4739                            catch (Exception e) {
4740                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_ALL,
4741                                            FINDER_ARGS_EMPTY);
4742    
4743                                    throw processException(e);
4744                            }
4745                            finally {
4746                                    closeSession(session);
4747                            }
4748                    }
4749    
4750                    return count.intValue();
4751            }
4752    
4753            /**
4754             * Initializes the resource permission persistence.
4755             */
4756            public void afterPropertiesSet() {
4757                    String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
4758                                            com.liferay.portal.util.PropsUtil.get(
4759                                                    "value.object.listener.com.liferay.portal.model.ResourcePermission")));
4760    
4761                    if (listenerClassNames.length > 0) {
4762                            try {
4763                                    List<ModelListener<ResourcePermission>> listenersList = new ArrayList<ModelListener<ResourcePermission>>();
4764    
4765                                    for (String listenerClassName : listenerClassNames) {
4766                                            listenersList.add((ModelListener<ResourcePermission>)InstanceFactory.newInstance(
4767                                                            getClassLoader(), listenerClassName));
4768                                    }
4769    
4770                                    listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
4771                            }
4772                            catch (Exception e) {
4773                                    _log.error(e);
4774                            }
4775                    }
4776            }
4777    
4778            public void destroy() {
4779                    EntityCacheUtil.removeCache(ResourcePermissionImpl.class.getName());
4780                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
4781                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
4782                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
4783            }
4784    
4785            private static final String _SQL_SELECT_RESOURCEPERMISSION = "SELECT resourcePermission FROM ResourcePermission resourcePermission";
4786            private static final String _SQL_SELECT_RESOURCEPERMISSION_WHERE = "SELECT resourcePermission FROM ResourcePermission resourcePermission WHERE ";
4787            private static final String _SQL_COUNT_RESOURCEPERMISSION = "SELECT COUNT(resourcePermission) FROM ResourcePermission resourcePermission";
4788            private static final String _SQL_COUNT_RESOURCEPERMISSION_WHERE = "SELECT COUNT(resourcePermission) FROM ResourcePermission resourcePermission WHERE ";
4789            private static final String _ORDER_BY_ENTITY_ALIAS = "resourcePermission.";
4790            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No ResourcePermission exists with the primary key ";
4791            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No ResourcePermission exists with the key {";
4792            private static final boolean _HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE = com.liferay.portal.util.PropsValues.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE;
4793            private static Log _log = LogFactoryUtil.getLog(ResourcePermissionPersistenceImpl.class);
4794            private static ResourcePermission _nullResourcePermission = new ResourcePermissionImpl() {
4795                            @Override
4796                            public Object clone() {
4797                                    return this;
4798                            }
4799    
4800                            @Override
4801                            public CacheModel<ResourcePermission> toCacheModel() {
4802                                    return _nullResourcePermissionCacheModel;
4803                            }
4804                    };
4805    
4806            private static CacheModel<ResourcePermission> _nullResourcePermissionCacheModel =
4807                    new CacheModel<ResourcePermission>() {
4808                            @Override
4809                            public ResourcePermission toEntityModel() {
4810                                    return _nullResourcePermission;
4811                            }
4812                    };
4813    }