001    /**
002     * Copyright (c) 2000-present 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.impl;
016    
017    import aQute.bnd.annotation.ProviderType;
018    
019    import com.liferay.portal.NoSuchResourcePermissionException;
020    import com.liferay.portal.kernel.dao.orm.EntityCache;
021    import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
022    import com.liferay.portal.kernel.dao.orm.FinderCache;
023    import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
024    import com.liferay.portal.kernel.dao.orm.FinderPath;
025    import com.liferay.portal.kernel.dao.orm.Query;
026    import com.liferay.portal.kernel.dao.orm.QueryPos;
027    import com.liferay.portal.kernel.dao.orm.QueryUtil;
028    import com.liferay.portal.kernel.dao.orm.Session;
029    import com.liferay.portal.kernel.log.Log;
030    import com.liferay.portal.kernel.log.LogFactoryUtil;
031    import com.liferay.portal.kernel.util.ArrayUtil;
032    import com.liferay.portal.kernel.util.CharPool;
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.Validator;
038    import com.liferay.portal.model.CacheModel;
039    import com.liferay.portal.model.MVCCModel;
040    import com.liferay.portal.model.ResourcePermission;
041    import com.liferay.portal.model.impl.ResourcePermissionImpl;
042    import com.liferay.portal.model.impl.ResourcePermissionModelImpl;
043    import com.liferay.portal.service.persistence.ResourcePermissionPersistence;
044    
045    import java.io.Serializable;
046    
047    import java.util.ArrayList;
048    import java.util.Arrays;
049    import java.util.Collections;
050    import java.util.HashMap;
051    import java.util.HashSet;
052    import java.util.Iterator;
053    import java.util.List;
054    import java.util.Map;
055    import java.util.Set;
056    
057    /**
058     * The persistence implementation for the resource permission service.
059     *
060     * <p>
061     * Caching information and settings can be found in <code>portal.properties</code>
062     * </p>
063     *
064     * @author Brian Wing Shun Chan
065     * @see ResourcePermissionPersistence
066     * @see com.liferay.portal.service.persistence.ResourcePermissionUtil
067     * @generated
068     */
069    @ProviderType
070    public class ResourcePermissionPersistenceImpl extends BasePersistenceImpl<ResourcePermission>
071            implements ResourcePermissionPersistence {
072            /*
073             * NOTE FOR DEVELOPERS:
074             *
075             * 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.
076             */
077            public static final String FINDER_CLASS_NAME_ENTITY = ResourcePermissionImpl.class.getName();
078            public static final String FINDER_CLASS_NAME_LIST_WITH_PAGINATION = FINDER_CLASS_NAME_ENTITY +
079                    ".List1";
080            public static final String FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION = FINDER_CLASS_NAME_ENTITY +
081                    ".List2";
082            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_ALL = new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
083                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED,
084                            ResourcePermissionImpl.class,
085                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findAll", new String[0]);
086            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
087                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED,
088                            ResourcePermissionImpl.class,
089                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
090            public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
091                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED, Long.class,
092                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]);
093            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_SCOPE = new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
094                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED,
095                            ResourcePermissionImpl.class,
096                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByScope",
097                            new String[] {
098                                    Integer.class.getName(),
099                                    
100                            Integer.class.getName(), Integer.class.getName(),
101                                    OrderByComparator.class.getName()
102                            });
103            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_SCOPE = new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
104                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED,
105                            ResourcePermissionImpl.class,
106                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByScope",
107                            new String[] { Integer.class.getName() },
108                            ResourcePermissionModelImpl.SCOPE_COLUMN_BITMASK);
109            public static final FinderPath FINDER_PATH_COUNT_BY_SCOPE = new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
110                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED, Long.class,
111                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByScope",
112                            new String[] { Integer.class.getName() });
113            public static final FinderPath FINDER_PATH_WITH_PAGINATION_COUNT_BY_SCOPE = new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
114                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED, Long.class,
115                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "countByScope",
116                            new String[] { Integer.class.getName() });
117    
118            /**
119             * Returns all the resource permissions where scope = &#63;.
120             *
121             * @param scope the scope
122             * @return the matching resource permissions
123             */
124            @Override
125            public List<ResourcePermission> findByScope(int scope) {
126                    return findByScope(scope, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
127            }
128    
129            /**
130             * Returns a range of all the resource permissions where scope = &#63;.
131             *
132             * <p>
133             * 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 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 QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link 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.
134             * </p>
135             *
136             * @param scope the scope
137             * @param start the lower bound of the range of resource permissions
138             * @param end the upper bound of the range of resource permissions (not inclusive)
139             * @return the range of matching resource permissions
140             */
141            @Override
142            public List<ResourcePermission> findByScope(int scope, int start, int end) {
143                    return findByScope(scope, start, end, null);
144            }
145    
146            /**
147             * Returns an ordered range of all the resource permissions where scope = &#63;.
148             *
149             * <p>
150             * 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 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 QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link 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.
151             * </p>
152             *
153             * @param scope the scope
154             * @param start the lower bound of the range of resource permissions
155             * @param end the upper bound of the range of resource permissions (not inclusive)
156             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
157             * @return the ordered range of matching resource permissions
158             */
159            @Override
160            public List<ResourcePermission> findByScope(int scope, int start, int end,
161                    OrderByComparator<ResourcePermission> orderByComparator) {
162                    return findByScope(scope, start, end, orderByComparator, true);
163            }
164    
165            /**
166             * Returns an ordered range of all the resource permissions where scope = &#63;.
167             *
168             * <p>
169             * 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 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 QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link 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.
170             * </p>
171             *
172             * @param scope the scope
173             * @param start the lower bound of the range of resource permissions
174             * @param end the upper bound of the range of resource permissions (not inclusive)
175             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
176             * @param retrieveFromCache whether to retrieve from the finder cache
177             * @return the ordered range of matching resource permissions
178             */
179            @Override
180            public List<ResourcePermission> findByScope(int scope, int start, int end,
181                    OrderByComparator<ResourcePermission> orderByComparator,
182                    boolean retrieveFromCache) {
183                    boolean pagination = true;
184                    FinderPath finderPath = null;
185                    Object[] finderArgs = null;
186    
187                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
188                                    (orderByComparator == null)) {
189                            pagination = false;
190                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_SCOPE;
191                            finderArgs = new Object[] { scope };
192                    }
193                    else {
194                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_SCOPE;
195                            finderArgs = new Object[] { scope, start, end, orderByComparator };
196                    }
197    
198                    List<ResourcePermission> list = null;
199    
200                    if (retrieveFromCache) {
201                            list = (List<ResourcePermission>)finderCache.getResult(finderPath,
202                                            finderArgs, this);
203    
204                            if ((list != null) && !list.isEmpty()) {
205                                    for (ResourcePermission resourcePermission : list) {
206                                            if ((scope != resourcePermission.getScope())) {
207                                                    list = null;
208    
209                                                    break;
210                                            }
211                                    }
212                            }
213                    }
214    
215                    if (list == null) {
216                            StringBundler query = null;
217    
218                            if (orderByComparator != null) {
219                                    query = new StringBundler(3 +
220                                                    (orderByComparator.getOrderByFields().length * 3));
221                            }
222                            else {
223                                    query = new StringBundler(3);
224                            }
225    
226                            query.append(_SQL_SELECT_RESOURCEPERMISSION_WHERE);
227    
228                            query.append(_FINDER_COLUMN_SCOPE_SCOPE_2);
229    
230                            if (orderByComparator != null) {
231                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
232                                            orderByComparator);
233                            }
234                            else
235                             if (pagination) {
236                                    query.append(ResourcePermissionModelImpl.ORDER_BY_JPQL);
237                            }
238    
239                            String sql = query.toString();
240    
241                            Session session = null;
242    
243                            try {
244                                    session = openSession();
245    
246                                    Query q = session.createQuery(sql);
247    
248                                    QueryPos qPos = QueryPos.getInstance(q);
249    
250                                    qPos.add(scope);
251    
252                                    if (!pagination) {
253                                            list = (List<ResourcePermission>)QueryUtil.list(q,
254                                                            getDialect(), start, end, false);
255    
256                                            Collections.sort(list);
257    
258                                            list = Collections.unmodifiableList(list);
259                                    }
260                                    else {
261                                            list = (List<ResourcePermission>)QueryUtil.list(q,
262                                                            getDialect(), start, end);
263                                    }
264    
265                                    cacheResult(list);
266    
267                                    finderCache.putResult(finderPath, finderArgs, list);
268                            }
269                            catch (Exception e) {
270                                    finderCache.removeResult(finderPath, finderArgs);
271    
272                                    throw processException(e);
273                            }
274                            finally {
275                                    closeSession(session);
276                            }
277                    }
278    
279                    return list;
280            }
281    
282            /**
283             * Returns the first resource permission in the ordered set where scope = &#63;.
284             *
285             * @param scope the scope
286             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
287             * @return the first matching resource permission
288             * @throws NoSuchResourcePermissionException if a matching resource permission could not be found
289             */
290            @Override
291            public ResourcePermission findByScope_First(int scope,
292                    OrderByComparator<ResourcePermission> orderByComparator)
293                    throws NoSuchResourcePermissionException {
294                    ResourcePermission resourcePermission = fetchByScope_First(scope,
295                                    orderByComparator);
296    
297                    if (resourcePermission != null) {
298                            return resourcePermission;
299                    }
300    
301                    StringBundler msg = new StringBundler(4);
302    
303                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
304    
305                    msg.append("scope=");
306                    msg.append(scope);
307    
308                    msg.append(StringPool.CLOSE_CURLY_BRACE);
309    
310                    throw new NoSuchResourcePermissionException(msg.toString());
311            }
312    
313            /**
314             * Returns the first resource permission in the ordered set where scope = &#63;.
315             *
316             * @param scope the scope
317             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
318             * @return the first matching resource permission, or <code>null</code> if a matching resource permission could not be found
319             */
320            @Override
321            public ResourcePermission fetchByScope_First(int scope,
322                    OrderByComparator<ResourcePermission> orderByComparator) {
323                    List<ResourcePermission> list = findByScope(scope, 0, 1,
324                                    orderByComparator);
325    
326                    if (!list.isEmpty()) {
327                            return list.get(0);
328                    }
329    
330                    return null;
331            }
332    
333            /**
334             * Returns the last resource permission in the ordered set where scope = &#63;.
335             *
336             * @param scope the scope
337             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
338             * @return the last matching resource permission
339             * @throws NoSuchResourcePermissionException if a matching resource permission could not be found
340             */
341            @Override
342            public ResourcePermission findByScope_Last(int scope,
343                    OrderByComparator<ResourcePermission> orderByComparator)
344                    throws NoSuchResourcePermissionException {
345                    ResourcePermission resourcePermission = fetchByScope_Last(scope,
346                                    orderByComparator);
347    
348                    if (resourcePermission != null) {
349                            return resourcePermission;
350                    }
351    
352                    StringBundler msg = new StringBundler(4);
353    
354                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
355    
356                    msg.append("scope=");
357                    msg.append(scope);
358    
359                    msg.append(StringPool.CLOSE_CURLY_BRACE);
360    
361                    throw new NoSuchResourcePermissionException(msg.toString());
362            }
363    
364            /**
365             * Returns the last resource permission in the ordered set where scope = &#63;.
366             *
367             * @param scope the scope
368             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
369             * @return the last matching resource permission, or <code>null</code> if a matching resource permission could not be found
370             */
371            @Override
372            public ResourcePermission fetchByScope_Last(int scope,
373                    OrderByComparator<ResourcePermission> orderByComparator) {
374                    int count = countByScope(scope);
375    
376                    if (count == 0) {
377                            return null;
378                    }
379    
380                    List<ResourcePermission> list = findByScope(scope, count - 1, count,
381                                    orderByComparator);
382    
383                    if (!list.isEmpty()) {
384                            return list.get(0);
385                    }
386    
387                    return null;
388            }
389    
390            /**
391             * Returns the resource permissions before and after the current resource permission in the ordered set where scope = &#63;.
392             *
393             * @param resourcePermissionId the primary key of the current resource permission
394             * @param scope the scope
395             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
396             * @return the previous, current, and next resource permission
397             * @throws NoSuchResourcePermissionException if a resource permission with the primary key could not be found
398             */
399            @Override
400            public ResourcePermission[] findByScope_PrevAndNext(
401                    long resourcePermissionId, int scope,
402                    OrderByComparator<ResourcePermission> orderByComparator)
403                    throws NoSuchResourcePermissionException {
404                    ResourcePermission resourcePermission = findByPrimaryKey(resourcePermissionId);
405    
406                    Session session = null;
407    
408                    try {
409                            session = openSession();
410    
411                            ResourcePermission[] array = new ResourcePermissionImpl[3];
412    
413                            array[0] = getByScope_PrevAndNext(session, resourcePermission,
414                                            scope, orderByComparator, true);
415    
416                            array[1] = resourcePermission;
417    
418                            array[2] = getByScope_PrevAndNext(session, resourcePermission,
419                                            scope, orderByComparator, false);
420    
421                            return array;
422                    }
423                    catch (Exception e) {
424                            throw processException(e);
425                    }
426                    finally {
427                            closeSession(session);
428                    }
429            }
430    
431            protected ResourcePermission getByScope_PrevAndNext(Session session,
432                    ResourcePermission resourcePermission, int scope,
433                    OrderByComparator<ResourcePermission> orderByComparator,
434                    boolean previous) {
435                    StringBundler query = null;
436    
437                    if (orderByComparator != null) {
438                            query = new StringBundler(6 +
439                                            (orderByComparator.getOrderByFields().length * 6));
440                    }
441                    else {
442                            query = new StringBundler(3);
443                    }
444    
445                    query.append(_SQL_SELECT_RESOURCEPERMISSION_WHERE);
446    
447                    query.append(_FINDER_COLUMN_SCOPE_SCOPE_2);
448    
449                    if (orderByComparator != null) {
450                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
451    
452                            if (orderByConditionFields.length > 0) {
453                                    query.append(WHERE_AND);
454                            }
455    
456                            for (int i = 0; i < orderByConditionFields.length; i++) {
457                                    query.append(_ORDER_BY_ENTITY_ALIAS);
458                                    query.append(orderByConditionFields[i]);
459    
460                                    if ((i + 1) < orderByConditionFields.length) {
461                                            if (orderByComparator.isAscending() ^ previous) {
462                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
463                                            }
464                                            else {
465                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
466                                            }
467                                    }
468                                    else {
469                                            if (orderByComparator.isAscending() ^ previous) {
470                                                    query.append(WHERE_GREATER_THAN);
471                                            }
472                                            else {
473                                                    query.append(WHERE_LESSER_THAN);
474                                            }
475                                    }
476                            }
477    
478                            query.append(ORDER_BY_CLAUSE);
479    
480                            String[] orderByFields = orderByComparator.getOrderByFields();
481    
482                            for (int i = 0; i < orderByFields.length; i++) {
483                                    query.append(_ORDER_BY_ENTITY_ALIAS);
484                                    query.append(orderByFields[i]);
485    
486                                    if ((i + 1) < orderByFields.length) {
487                                            if (orderByComparator.isAscending() ^ previous) {
488                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
489                                            }
490                                            else {
491                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
492                                            }
493                                    }
494                                    else {
495                                            if (orderByComparator.isAscending() ^ previous) {
496                                                    query.append(ORDER_BY_ASC);
497                                            }
498                                            else {
499                                                    query.append(ORDER_BY_DESC);
500                                            }
501                                    }
502                            }
503                    }
504                    else {
505                            query.append(ResourcePermissionModelImpl.ORDER_BY_JPQL);
506                    }
507    
508                    String sql = query.toString();
509    
510                    Query q = session.createQuery(sql);
511    
512                    q.setFirstResult(0);
513                    q.setMaxResults(2);
514    
515                    QueryPos qPos = QueryPos.getInstance(q);
516    
517                    qPos.add(scope);
518    
519                    if (orderByComparator != null) {
520                            Object[] values = orderByComparator.getOrderByConditionValues(resourcePermission);
521    
522                            for (Object value : values) {
523                                    qPos.add(value);
524                            }
525                    }
526    
527                    List<ResourcePermission> list = q.list();
528    
529                    if (list.size() == 2) {
530                            return list.get(1);
531                    }
532                    else {
533                            return null;
534                    }
535            }
536    
537            /**
538             * Returns all the resource permissions where scope = any &#63;.
539             *
540             * <p>
541             * 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 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 QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link 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.
542             * </p>
543             *
544             * @param scopes the scopes
545             * @return the matching resource permissions
546             */
547            @Override
548            public List<ResourcePermission> findByScope(int[] scopes) {
549                    return findByScope(scopes, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
550            }
551    
552            /**
553             * Returns a range of all the resource permissions where scope = any &#63;.
554             *
555             * <p>
556             * 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 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 QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link 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.
557             * </p>
558             *
559             * @param scopes the scopes
560             * @param start the lower bound of the range of resource permissions
561             * @param end the upper bound of the range of resource permissions (not inclusive)
562             * @return the range of matching resource permissions
563             */
564            @Override
565            public List<ResourcePermission> findByScope(int[] scopes, int start, int end) {
566                    return findByScope(scopes, start, end, null);
567            }
568    
569            /**
570             * Returns an ordered range of all the resource permissions where scope = any &#63;.
571             *
572             * <p>
573             * 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 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 QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link 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.
574             * </p>
575             *
576             * @param scopes the scopes
577             * @param start the lower bound of the range of resource permissions
578             * @param end the upper bound of the range of resource permissions (not inclusive)
579             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
580             * @return the ordered range of matching resource permissions
581             */
582            @Override
583            public List<ResourcePermission> findByScope(int[] scopes, int start,
584                    int end, OrderByComparator<ResourcePermission> orderByComparator) {
585                    return findByScope(scopes, start, end, orderByComparator, true);
586            }
587    
588            /**
589             * Returns an ordered range of all the resource permissions where scope = &#63;, optionally using the finder cache.
590             *
591             * <p>
592             * 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 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 QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link 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.
593             * </p>
594             *
595             * @param scope the scope
596             * @param start the lower bound of the range of resource permissions
597             * @param end the upper bound of the range of resource permissions (not inclusive)
598             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
599             * @param retrieveFromCache whether to retrieve from the finder cache
600             * @return the ordered range of matching resource permissions
601             */
602            @Override
603            public List<ResourcePermission> findByScope(int[] scopes, int start,
604                    int end, OrderByComparator<ResourcePermission> orderByComparator,
605                    boolean retrieveFromCache) {
606                    if (scopes == null) {
607                            scopes = new int[0];
608                    }
609                    else if (scopes.length > 1) {
610                            scopes = ArrayUtil.unique(scopes);
611    
612                            Arrays.sort(scopes);
613                    }
614    
615                    if (scopes.length == 1) {
616                            return findByScope(scopes[0], start, end, orderByComparator);
617                    }
618    
619                    boolean pagination = true;
620                    Object[] finderArgs = null;
621    
622                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
623                                    (orderByComparator == null)) {
624                            pagination = false;
625                            finderArgs = new Object[] { StringUtil.merge(scopes) };
626                    }
627                    else {
628                            finderArgs = new Object[] {
629                                            StringUtil.merge(scopes),
630                                            
631                                            start, end, orderByComparator
632                                    };
633                    }
634    
635                    List<ResourcePermission> list = null;
636    
637                    if (retrieveFromCache) {
638                            list = (List<ResourcePermission>)finderCache.getResult(FINDER_PATH_WITH_PAGINATION_FIND_BY_SCOPE,
639                                            finderArgs, this);
640    
641                            if ((list != null) && !list.isEmpty()) {
642                                    for (ResourcePermission resourcePermission : list) {
643                                            if (!ArrayUtil.contains(scopes,
644                                                                    resourcePermission.getScope())) {
645                                                    list = null;
646    
647                                                    break;
648                                            }
649                                    }
650                            }
651                    }
652    
653                    if (list == null) {
654                            StringBundler query = new StringBundler();
655    
656                            query.append(_SQL_SELECT_RESOURCEPERMISSION_WHERE);
657    
658                            if (scopes.length > 0) {
659                                    query.append(StringPool.OPEN_PARENTHESIS);
660    
661                                    query.append(_FINDER_COLUMN_SCOPE_SCOPE_7);
662    
663                                    query.append(StringUtil.merge(scopes));
664    
665                                    query.append(StringPool.CLOSE_PARENTHESIS);
666    
667                                    query.append(StringPool.CLOSE_PARENTHESIS);
668                            }
669    
670                            query.setStringAt(removeConjunction(query.stringAt(query.index() -
671                                                    1)), query.index() - 1);
672    
673                            if (orderByComparator != null) {
674                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
675                                            orderByComparator);
676                            }
677                            else
678                             if (pagination) {
679                                    query.append(ResourcePermissionModelImpl.ORDER_BY_JPQL);
680                            }
681    
682                            String sql = query.toString();
683    
684                            Session session = null;
685    
686                            try {
687                                    session = openSession();
688    
689                                    Query q = session.createQuery(sql);
690    
691                                    if (!pagination) {
692                                            list = (List<ResourcePermission>)QueryUtil.list(q,
693                                                            getDialect(), start, end, false);
694    
695                                            Collections.sort(list);
696    
697                                            list = Collections.unmodifiableList(list);
698                                    }
699                                    else {
700                                            list = (List<ResourcePermission>)QueryUtil.list(q,
701                                                            getDialect(), start, end);
702                                    }
703    
704                                    cacheResult(list);
705    
706                                    finderCache.putResult(FINDER_PATH_WITH_PAGINATION_FIND_BY_SCOPE,
707                                            finderArgs, list);
708                            }
709                            catch (Exception e) {
710                                    finderCache.removeResult(FINDER_PATH_WITH_PAGINATION_FIND_BY_SCOPE,
711                                            finderArgs);
712    
713                                    throw processException(e);
714                            }
715                            finally {
716                                    closeSession(session);
717                            }
718                    }
719    
720                    return list;
721            }
722    
723            /**
724             * Removes all the resource permissions where scope = &#63; from the database.
725             *
726             * @param scope the scope
727             */
728            @Override
729            public void removeByScope(int scope) {
730                    for (ResourcePermission resourcePermission : findByScope(scope,
731                                    QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
732                            remove(resourcePermission);
733                    }
734            }
735    
736            /**
737             * Returns the number of resource permissions where scope = &#63;.
738             *
739             * @param scope the scope
740             * @return the number of matching resource permissions
741             */
742            @Override
743            public int countByScope(int scope) {
744                    FinderPath finderPath = FINDER_PATH_COUNT_BY_SCOPE;
745    
746                    Object[] finderArgs = new Object[] { scope };
747    
748                    Long count = (Long)finderCache.getResult(finderPath, finderArgs, this);
749    
750                    if (count == null) {
751                            StringBundler query = new StringBundler(2);
752    
753                            query.append(_SQL_COUNT_RESOURCEPERMISSION_WHERE);
754    
755                            query.append(_FINDER_COLUMN_SCOPE_SCOPE_2);
756    
757                            String sql = query.toString();
758    
759                            Session session = null;
760    
761                            try {
762                                    session = openSession();
763    
764                                    Query q = session.createQuery(sql);
765    
766                                    QueryPos qPos = QueryPos.getInstance(q);
767    
768                                    qPos.add(scope);
769    
770                                    count = (Long)q.uniqueResult();
771    
772                                    finderCache.putResult(finderPath, finderArgs, count);
773                            }
774                            catch (Exception e) {
775                                    finderCache.removeResult(finderPath, finderArgs);
776    
777                                    throw processException(e);
778                            }
779                            finally {
780                                    closeSession(session);
781                            }
782                    }
783    
784                    return count.intValue();
785            }
786    
787            /**
788             * Returns the number of resource permissions where scope = any &#63;.
789             *
790             * @param scopes the scopes
791             * @return the number of matching resource permissions
792             */
793            @Override
794            public int countByScope(int[] scopes) {
795                    if (scopes == null) {
796                            scopes = new int[0];
797                    }
798                    else if (scopes.length > 1) {
799                            scopes = ArrayUtil.unique(scopes);
800    
801                            Arrays.sort(scopes);
802                    }
803    
804                    Object[] finderArgs = new Object[] { StringUtil.merge(scopes) };
805    
806                    Long count = (Long)finderCache.getResult(FINDER_PATH_WITH_PAGINATION_COUNT_BY_SCOPE,
807                                    finderArgs, this);
808    
809                    if (count == null) {
810                            StringBundler query = new StringBundler();
811    
812                            query.append(_SQL_COUNT_RESOURCEPERMISSION_WHERE);
813    
814                            if (scopes.length > 0) {
815                                    query.append(StringPool.OPEN_PARENTHESIS);
816    
817                                    query.append(_FINDER_COLUMN_SCOPE_SCOPE_7);
818    
819                                    query.append(StringUtil.merge(scopes));
820    
821                                    query.append(StringPool.CLOSE_PARENTHESIS);
822    
823                                    query.append(StringPool.CLOSE_PARENTHESIS);
824                            }
825    
826                            query.setStringAt(removeConjunction(query.stringAt(query.index() -
827                                                    1)), query.index() - 1);
828    
829                            String sql = query.toString();
830    
831                            Session session = null;
832    
833                            try {
834                                    session = openSession();
835    
836                                    Query q = session.createQuery(sql);
837    
838                                    count = (Long)q.uniqueResult();
839    
840                                    finderCache.putResult(FINDER_PATH_WITH_PAGINATION_COUNT_BY_SCOPE,
841                                            finderArgs, count);
842                            }
843                            catch (Exception e) {
844                                    finderCache.removeResult(FINDER_PATH_WITH_PAGINATION_COUNT_BY_SCOPE,
845                                            finderArgs);
846    
847                                    throw processException(e);
848                            }
849                            finally {
850                                    closeSession(session);
851                            }
852                    }
853    
854                    return count.intValue();
855            }
856    
857            private static final String _FINDER_COLUMN_SCOPE_SCOPE_2 = "resourcePermission.scope = ?";
858            private static final String _FINDER_COLUMN_SCOPE_SCOPE_7 = "resourcePermission.scope IN (";
859            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_ROLEID = new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
860                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED,
861                            ResourcePermissionImpl.class,
862                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByRoleId",
863                            new String[] {
864                                    Long.class.getName(),
865                                    
866                            Integer.class.getName(), Integer.class.getName(),
867                                    OrderByComparator.class.getName()
868                            });
869            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_ROLEID =
870                    new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
871                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED,
872                            ResourcePermissionImpl.class,
873                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByRoleId",
874                            new String[] { Long.class.getName() },
875                            ResourcePermissionModelImpl.ROLEID_COLUMN_BITMASK);
876            public static final FinderPath FINDER_PATH_COUNT_BY_ROLEID = new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
877                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED, Long.class,
878                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByRoleId",
879                            new String[] { Long.class.getName() });
880    
881            /**
882             * Returns all the resource permissions where roleId = &#63;.
883             *
884             * @param roleId the role ID
885             * @return the matching resource permissions
886             */
887            @Override
888            public List<ResourcePermission> findByRoleId(long roleId) {
889                    return findByRoleId(roleId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
890            }
891    
892            /**
893             * Returns a range of all the resource permissions where roleId = &#63;.
894             *
895             * <p>
896             * 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 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 QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link 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.
897             * </p>
898             *
899             * @param roleId the role ID
900             * @param start the lower bound of the range of resource permissions
901             * @param end the upper bound of the range of resource permissions (not inclusive)
902             * @return the range of matching resource permissions
903             */
904            @Override
905            public List<ResourcePermission> findByRoleId(long roleId, int start, int end) {
906                    return findByRoleId(roleId, start, end, null);
907            }
908    
909            /**
910             * Returns an ordered range of all the resource permissions where roleId = &#63;.
911             *
912             * <p>
913             * 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 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 QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link 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.
914             * </p>
915             *
916             * @param roleId the role ID
917             * @param start the lower bound of the range of resource permissions
918             * @param end the upper bound of the range of resource permissions (not inclusive)
919             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
920             * @return the ordered range of matching resource permissions
921             */
922            @Override
923            public List<ResourcePermission> findByRoleId(long roleId, int start,
924                    int end, OrderByComparator<ResourcePermission> orderByComparator) {
925                    return findByRoleId(roleId, start, end, orderByComparator, true);
926            }
927    
928            /**
929             * Returns an ordered range of all the resource permissions where roleId = &#63;.
930             *
931             * <p>
932             * 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 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 QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link 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.
933             * </p>
934             *
935             * @param roleId the role ID
936             * @param start the lower bound of the range of resource permissions
937             * @param end the upper bound of the range of resource permissions (not inclusive)
938             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
939             * @param retrieveFromCache whether to retrieve from the finder cache
940             * @return the ordered range of matching resource permissions
941             */
942            @Override
943            public List<ResourcePermission> findByRoleId(long roleId, int start,
944                    int end, OrderByComparator<ResourcePermission> orderByComparator,
945                    boolean retrieveFromCache) {
946                    boolean pagination = true;
947                    FinderPath finderPath = null;
948                    Object[] finderArgs = null;
949    
950                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
951                                    (orderByComparator == null)) {
952                            pagination = false;
953                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_ROLEID;
954                            finderArgs = new Object[] { roleId };
955                    }
956                    else {
957                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_ROLEID;
958                            finderArgs = new Object[] { roleId, start, end, orderByComparator };
959                    }
960    
961                    List<ResourcePermission> list = null;
962    
963                    if (retrieveFromCache) {
964                            list = (List<ResourcePermission>)finderCache.getResult(finderPath,
965                                            finderArgs, this);
966    
967                            if ((list != null) && !list.isEmpty()) {
968                                    for (ResourcePermission resourcePermission : list) {
969                                            if ((roleId != resourcePermission.getRoleId())) {
970                                                    list = null;
971    
972                                                    break;
973                                            }
974                                    }
975                            }
976                    }
977    
978                    if (list == null) {
979                            StringBundler query = null;
980    
981                            if (orderByComparator != null) {
982                                    query = new StringBundler(3 +
983                                                    (orderByComparator.getOrderByFields().length * 3));
984                            }
985                            else {
986                                    query = new StringBundler(3);
987                            }
988    
989                            query.append(_SQL_SELECT_RESOURCEPERMISSION_WHERE);
990    
991                            query.append(_FINDER_COLUMN_ROLEID_ROLEID_2);
992    
993                            if (orderByComparator != null) {
994                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
995                                            orderByComparator);
996                            }
997                            else
998                             if (pagination) {
999                                    query.append(ResourcePermissionModelImpl.ORDER_BY_JPQL);
1000                            }
1001    
1002                            String sql = query.toString();
1003    
1004                            Session session = null;
1005    
1006                            try {
1007                                    session = openSession();
1008    
1009                                    Query q = session.createQuery(sql);
1010    
1011                                    QueryPos qPos = QueryPos.getInstance(q);
1012    
1013                                    qPos.add(roleId);
1014    
1015                                    if (!pagination) {
1016                                            list = (List<ResourcePermission>)QueryUtil.list(q,
1017                                                            getDialect(), start, end, false);
1018    
1019                                            Collections.sort(list);
1020    
1021                                            list = Collections.unmodifiableList(list);
1022                                    }
1023                                    else {
1024                                            list = (List<ResourcePermission>)QueryUtil.list(q,
1025                                                            getDialect(), start, end);
1026                                    }
1027    
1028                                    cacheResult(list);
1029    
1030                                    finderCache.putResult(finderPath, finderArgs, list);
1031                            }
1032                            catch (Exception e) {
1033                                    finderCache.removeResult(finderPath, finderArgs);
1034    
1035                                    throw processException(e);
1036                            }
1037                            finally {
1038                                    closeSession(session);
1039                            }
1040                    }
1041    
1042                    return list;
1043            }
1044    
1045            /**
1046             * Returns the first resource permission in the ordered set where roleId = &#63;.
1047             *
1048             * @param roleId the role ID
1049             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1050             * @return the first matching resource permission
1051             * @throws NoSuchResourcePermissionException if a matching resource permission could not be found
1052             */
1053            @Override
1054            public ResourcePermission findByRoleId_First(long roleId,
1055                    OrderByComparator<ResourcePermission> orderByComparator)
1056                    throws NoSuchResourcePermissionException {
1057                    ResourcePermission resourcePermission = fetchByRoleId_First(roleId,
1058                                    orderByComparator);
1059    
1060                    if (resourcePermission != null) {
1061                            return resourcePermission;
1062                    }
1063    
1064                    StringBundler msg = new StringBundler(4);
1065    
1066                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1067    
1068                    msg.append("roleId=");
1069                    msg.append(roleId);
1070    
1071                    msg.append(StringPool.CLOSE_CURLY_BRACE);
1072    
1073                    throw new NoSuchResourcePermissionException(msg.toString());
1074            }
1075    
1076            /**
1077             * Returns the first resource permission in the ordered set where roleId = &#63;.
1078             *
1079             * @param roleId the role ID
1080             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1081             * @return the first matching resource permission, or <code>null</code> if a matching resource permission could not be found
1082             */
1083            @Override
1084            public ResourcePermission fetchByRoleId_First(long roleId,
1085                    OrderByComparator<ResourcePermission> orderByComparator) {
1086                    List<ResourcePermission> list = findByRoleId(roleId, 0, 1,
1087                                    orderByComparator);
1088    
1089                    if (!list.isEmpty()) {
1090                            return list.get(0);
1091                    }
1092    
1093                    return null;
1094            }
1095    
1096            /**
1097             * Returns the last resource permission in the ordered set where roleId = &#63;.
1098             *
1099             * @param roleId the role ID
1100             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1101             * @return the last matching resource permission
1102             * @throws NoSuchResourcePermissionException if a matching resource permission could not be found
1103             */
1104            @Override
1105            public ResourcePermission findByRoleId_Last(long roleId,
1106                    OrderByComparator<ResourcePermission> orderByComparator)
1107                    throws NoSuchResourcePermissionException {
1108                    ResourcePermission resourcePermission = fetchByRoleId_Last(roleId,
1109                                    orderByComparator);
1110    
1111                    if (resourcePermission != null) {
1112                            return resourcePermission;
1113                    }
1114    
1115                    StringBundler msg = new StringBundler(4);
1116    
1117                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1118    
1119                    msg.append("roleId=");
1120                    msg.append(roleId);
1121    
1122                    msg.append(StringPool.CLOSE_CURLY_BRACE);
1123    
1124                    throw new NoSuchResourcePermissionException(msg.toString());
1125            }
1126    
1127            /**
1128             * Returns the last resource permission in the ordered set where roleId = &#63;.
1129             *
1130             * @param roleId the role ID
1131             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1132             * @return the last matching resource permission, or <code>null</code> if a matching resource permission could not be found
1133             */
1134            @Override
1135            public ResourcePermission fetchByRoleId_Last(long roleId,
1136                    OrderByComparator<ResourcePermission> orderByComparator) {
1137                    int count = countByRoleId(roleId);
1138    
1139                    if (count == 0) {
1140                            return null;
1141                    }
1142    
1143                    List<ResourcePermission> list = findByRoleId(roleId, count - 1, count,
1144                                    orderByComparator);
1145    
1146                    if (!list.isEmpty()) {
1147                            return list.get(0);
1148                    }
1149    
1150                    return null;
1151            }
1152    
1153            /**
1154             * Returns the resource permissions before and after the current resource permission in the ordered set where roleId = &#63;.
1155             *
1156             * @param resourcePermissionId the primary key of the current resource permission
1157             * @param roleId the role ID
1158             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1159             * @return the previous, current, and next resource permission
1160             * @throws NoSuchResourcePermissionException if a resource permission with the primary key could not be found
1161             */
1162            @Override
1163            public ResourcePermission[] findByRoleId_PrevAndNext(
1164                    long resourcePermissionId, long roleId,
1165                    OrderByComparator<ResourcePermission> orderByComparator)
1166                    throws NoSuchResourcePermissionException {
1167                    ResourcePermission resourcePermission = findByPrimaryKey(resourcePermissionId);
1168    
1169                    Session session = null;
1170    
1171                    try {
1172                            session = openSession();
1173    
1174                            ResourcePermission[] array = new ResourcePermissionImpl[3];
1175    
1176                            array[0] = getByRoleId_PrevAndNext(session, resourcePermission,
1177                                            roleId, orderByComparator, true);
1178    
1179                            array[1] = resourcePermission;
1180    
1181                            array[2] = getByRoleId_PrevAndNext(session, resourcePermission,
1182                                            roleId, orderByComparator, false);
1183    
1184                            return array;
1185                    }
1186                    catch (Exception e) {
1187                            throw processException(e);
1188                    }
1189                    finally {
1190                            closeSession(session);
1191                    }
1192            }
1193    
1194            protected ResourcePermission getByRoleId_PrevAndNext(Session session,
1195                    ResourcePermission resourcePermission, long roleId,
1196                    OrderByComparator<ResourcePermission> orderByComparator,
1197                    boolean previous) {
1198                    StringBundler query = null;
1199    
1200                    if (orderByComparator != null) {
1201                            query = new StringBundler(6 +
1202                                            (orderByComparator.getOrderByFields().length * 6));
1203                    }
1204                    else {
1205                            query = new StringBundler(3);
1206                    }
1207    
1208                    query.append(_SQL_SELECT_RESOURCEPERMISSION_WHERE);
1209    
1210                    query.append(_FINDER_COLUMN_ROLEID_ROLEID_2);
1211    
1212                    if (orderByComparator != null) {
1213                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
1214    
1215                            if (orderByConditionFields.length > 0) {
1216                                    query.append(WHERE_AND);
1217                            }
1218    
1219                            for (int i = 0; i < orderByConditionFields.length; i++) {
1220                                    query.append(_ORDER_BY_ENTITY_ALIAS);
1221                                    query.append(orderByConditionFields[i]);
1222    
1223                                    if ((i + 1) < orderByConditionFields.length) {
1224                                            if (orderByComparator.isAscending() ^ previous) {
1225                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
1226                                            }
1227                                            else {
1228                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
1229                                            }
1230                                    }
1231                                    else {
1232                                            if (orderByComparator.isAscending() ^ previous) {
1233                                                    query.append(WHERE_GREATER_THAN);
1234                                            }
1235                                            else {
1236                                                    query.append(WHERE_LESSER_THAN);
1237                                            }
1238                                    }
1239                            }
1240    
1241                            query.append(ORDER_BY_CLAUSE);
1242    
1243                            String[] orderByFields = orderByComparator.getOrderByFields();
1244    
1245                            for (int i = 0; i < orderByFields.length; i++) {
1246                                    query.append(_ORDER_BY_ENTITY_ALIAS);
1247                                    query.append(orderByFields[i]);
1248    
1249                                    if ((i + 1) < orderByFields.length) {
1250                                            if (orderByComparator.isAscending() ^ previous) {
1251                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
1252                                            }
1253                                            else {
1254                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
1255                                            }
1256                                    }
1257                                    else {
1258                                            if (orderByComparator.isAscending() ^ previous) {
1259                                                    query.append(ORDER_BY_ASC);
1260                                            }
1261                                            else {
1262                                                    query.append(ORDER_BY_DESC);
1263                                            }
1264                                    }
1265                            }
1266                    }
1267                    else {
1268                            query.append(ResourcePermissionModelImpl.ORDER_BY_JPQL);
1269                    }
1270    
1271                    String sql = query.toString();
1272    
1273                    Query q = session.createQuery(sql);
1274    
1275                    q.setFirstResult(0);
1276                    q.setMaxResults(2);
1277    
1278                    QueryPos qPos = QueryPos.getInstance(q);
1279    
1280                    qPos.add(roleId);
1281    
1282                    if (orderByComparator != null) {
1283                            Object[] values = orderByComparator.getOrderByConditionValues(resourcePermission);
1284    
1285                            for (Object value : values) {
1286                                    qPos.add(value);
1287                            }
1288                    }
1289    
1290                    List<ResourcePermission> list = q.list();
1291    
1292                    if (list.size() == 2) {
1293                            return list.get(1);
1294                    }
1295                    else {
1296                            return null;
1297                    }
1298            }
1299    
1300            /**
1301             * Removes all the resource permissions where roleId = &#63; from the database.
1302             *
1303             * @param roleId the role ID
1304             */
1305            @Override
1306            public void removeByRoleId(long roleId) {
1307                    for (ResourcePermission resourcePermission : findByRoleId(roleId,
1308                                    QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
1309                            remove(resourcePermission);
1310                    }
1311            }
1312    
1313            /**
1314             * Returns the number of resource permissions where roleId = &#63;.
1315             *
1316             * @param roleId the role ID
1317             * @return the number of matching resource permissions
1318             */
1319            @Override
1320            public int countByRoleId(long roleId) {
1321                    FinderPath finderPath = FINDER_PATH_COUNT_BY_ROLEID;
1322    
1323                    Object[] finderArgs = new Object[] { roleId };
1324    
1325                    Long count = (Long)finderCache.getResult(finderPath, finderArgs, this);
1326    
1327                    if (count == null) {
1328                            StringBundler query = new StringBundler(2);
1329    
1330                            query.append(_SQL_COUNT_RESOURCEPERMISSION_WHERE);
1331    
1332                            query.append(_FINDER_COLUMN_ROLEID_ROLEID_2);
1333    
1334                            String sql = query.toString();
1335    
1336                            Session session = null;
1337    
1338                            try {
1339                                    session = openSession();
1340    
1341                                    Query q = session.createQuery(sql);
1342    
1343                                    QueryPos qPos = QueryPos.getInstance(q);
1344    
1345                                    qPos.add(roleId);
1346    
1347                                    count = (Long)q.uniqueResult();
1348    
1349                                    finderCache.putResult(finderPath, finderArgs, count);
1350                            }
1351                            catch (Exception e) {
1352                                    finderCache.removeResult(finderPath, finderArgs);
1353    
1354                                    throw processException(e);
1355                            }
1356                            finally {
1357                                    closeSession(session);
1358                            }
1359                    }
1360    
1361                    return count.intValue();
1362            }
1363    
1364            private static final String _FINDER_COLUMN_ROLEID_ROLEID_2 = "resourcePermission.roleId = ?";
1365            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_C_LIKEP = new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
1366                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED,
1367                            ResourcePermissionImpl.class,
1368                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByC_LikeP",
1369                            new String[] {
1370                                    Long.class.getName(), String.class.getName(),
1371                                    
1372                            Integer.class.getName(), Integer.class.getName(),
1373                                    OrderByComparator.class.getName()
1374                            });
1375            public static final FinderPath FINDER_PATH_WITH_PAGINATION_COUNT_BY_C_LIKEP = new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
1376                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED, Long.class,
1377                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "countByC_LikeP",
1378                            new String[] { Long.class.getName(), String.class.getName() });
1379    
1380            /**
1381             * Returns all the resource permissions where companyId = &#63; and primKey LIKE &#63;.
1382             *
1383             * @param companyId the company ID
1384             * @param primKey the prim key
1385             * @return the matching resource permissions
1386             */
1387            @Override
1388            public List<ResourcePermission> findByC_LikeP(long companyId, String primKey) {
1389                    return findByC_LikeP(companyId, primKey, QueryUtil.ALL_POS,
1390                            QueryUtil.ALL_POS, null);
1391            }
1392    
1393            /**
1394             * Returns a range of all the resource permissions where companyId = &#63; and primKey LIKE &#63;.
1395             *
1396             * <p>
1397             * 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 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 QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link 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.
1398             * </p>
1399             *
1400             * @param companyId the company ID
1401             * @param primKey the prim key
1402             * @param start the lower bound of the range of resource permissions
1403             * @param end the upper bound of the range of resource permissions (not inclusive)
1404             * @return the range of matching resource permissions
1405             */
1406            @Override
1407            public List<ResourcePermission> findByC_LikeP(long companyId,
1408                    String primKey, int start, int end) {
1409                    return findByC_LikeP(companyId, primKey, start, end, null);
1410            }
1411    
1412            /**
1413             * Returns an ordered range of all the resource permissions where companyId = &#63; and primKey LIKE &#63;.
1414             *
1415             * <p>
1416             * 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 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 QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link 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.
1417             * </p>
1418             *
1419             * @param companyId the company ID
1420             * @param primKey the prim key
1421             * @param start the lower bound of the range of resource permissions
1422             * @param end the upper bound of the range of resource permissions (not inclusive)
1423             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
1424             * @return the ordered range of matching resource permissions
1425             */
1426            @Override
1427            public List<ResourcePermission> findByC_LikeP(long companyId,
1428                    String primKey, int start, int end,
1429                    OrderByComparator<ResourcePermission> orderByComparator) {
1430                    return findByC_LikeP(companyId, primKey, start, end, orderByComparator,
1431                            true);
1432            }
1433    
1434            /**
1435             * Returns an ordered range of all the resource permissions where companyId = &#63; and primKey LIKE &#63;.
1436             *
1437             * <p>
1438             * 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 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 QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link 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.
1439             * </p>
1440             *
1441             * @param companyId the company ID
1442             * @param primKey the prim key
1443             * @param start the lower bound of the range of resource permissions
1444             * @param end the upper bound of the range of resource permissions (not inclusive)
1445             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
1446             * @param retrieveFromCache whether to retrieve from the finder cache
1447             * @return the ordered range of matching resource permissions
1448             */
1449            @Override
1450            public List<ResourcePermission> findByC_LikeP(long companyId,
1451                    String primKey, int start, int end,
1452                    OrderByComparator<ResourcePermission> orderByComparator,
1453                    boolean retrieveFromCache) {
1454                    boolean pagination = true;
1455                    FinderPath finderPath = null;
1456                    Object[] finderArgs = null;
1457    
1458                    finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_C_LIKEP;
1459                    finderArgs = new Object[] {
1460                                    companyId, primKey,
1461                                    
1462                                    start, end, orderByComparator
1463                            };
1464    
1465                    List<ResourcePermission> list = null;
1466    
1467                    if (retrieveFromCache) {
1468                            list = (List<ResourcePermission>)finderCache.getResult(finderPath,
1469                                            finderArgs, this);
1470    
1471                            if ((list != null) && !list.isEmpty()) {
1472                                    for (ResourcePermission resourcePermission : list) {
1473                                            if ((companyId != resourcePermission.getCompanyId()) ||
1474                                                            !StringUtil.wildcardMatches(
1475                                                                    resourcePermission.getPrimKey(), primKey,
1476                                                                    CharPool.UNDERLINE, CharPool.PERCENT,
1477                                                                    CharPool.BACK_SLASH, true)) {
1478                                                    list = null;
1479    
1480                                                    break;
1481                                            }
1482                                    }
1483                            }
1484                    }
1485    
1486                    if (list == null) {
1487                            StringBundler query = null;
1488    
1489                            if (orderByComparator != null) {
1490                                    query = new StringBundler(4 +
1491                                                    (orderByComparator.getOrderByFields().length * 3));
1492                            }
1493                            else {
1494                                    query = new StringBundler(4);
1495                            }
1496    
1497                            query.append(_SQL_SELECT_RESOURCEPERMISSION_WHERE);
1498    
1499                            query.append(_FINDER_COLUMN_C_LIKEP_COMPANYID_2);
1500    
1501                            boolean bindPrimKey = false;
1502    
1503                            if (primKey == null) {
1504                                    query.append(_FINDER_COLUMN_C_LIKEP_PRIMKEY_1);
1505                            }
1506                            else if (primKey.equals(StringPool.BLANK)) {
1507                                    query.append(_FINDER_COLUMN_C_LIKEP_PRIMKEY_3);
1508                            }
1509                            else {
1510                                    bindPrimKey = true;
1511    
1512                                    query.append(_FINDER_COLUMN_C_LIKEP_PRIMKEY_2);
1513                            }
1514    
1515                            if (orderByComparator != null) {
1516                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1517                                            orderByComparator);
1518                            }
1519                            else
1520                             if (pagination) {
1521                                    query.append(ResourcePermissionModelImpl.ORDER_BY_JPQL);
1522                            }
1523    
1524                            String sql = query.toString();
1525    
1526                            Session session = null;
1527    
1528                            try {
1529                                    session = openSession();
1530    
1531                                    Query q = session.createQuery(sql);
1532    
1533                                    QueryPos qPos = QueryPos.getInstance(q);
1534    
1535                                    qPos.add(companyId);
1536    
1537                                    if (bindPrimKey) {
1538                                            qPos.add(primKey);
1539                                    }
1540    
1541                                    if (!pagination) {
1542                                            list = (List<ResourcePermission>)QueryUtil.list(q,
1543                                                            getDialect(), start, end, false);
1544    
1545                                            Collections.sort(list);
1546    
1547                                            list = Collections.unmodifiableList(list);
1548                                    }
1549                                    else {
1550                                            list = (List<ResourcePermission>)QueryUtil.list(q,
1551                                                            getDialect(), start, end);
1552                                    }
1553    
1554                                    cacheResult(list);
1555    
1556                                    finderCache.putResult(finderPath, finderArgs, list);
1557                            }
1558                            catch (Exception e) {
1559                                    finderCache.removeResult(finderPath, finderArgs);
1560    
1561                                    throw processException(e);
1562                            }
1563                            finally {
1564                                    closeSession(session);
1565                            }
1566                    }
1567    
1568                    return list;
1569            }
1570    
1571            /**
1572             * Returns the first resource permission in the ordered set where companyId = &#63; and primKey LIKE &#63;.
1573             *
1574             * @param companyId the company ID
1575             * @param primKey the prim key
1576             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1577             * @return the first matching resource permission
1578             * @throws NoSuchResourcePermissionException if a matching resource permission could not be found
1579             */
1580            @Override
1581            public ResourcePermission findByC_LikeP_First(long companyId,
1582                    String primKey, OrderByComparator<ResourcePermission> orderByComparator)
1583                    throws NoSuchResourcePermissionException {
1584                    ResourcePermission resourcePermission = fetchByC_LikeP_First(companyId,
1585                                    primKey, orderByComparator);
1586    
1587                    if (resourcePermission != null) {
1588                            return resourcePermission;
1589                    }
1590    
1591                    StringBundler msg = new StringBundler(6);
1592    
1593                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1594    
1595                    msg.append("companyId=");
1596                    msg.append(companyId);
1597    
1598                    msg.append(", primKey=");
1599                    msg.append(primKey);
1600    
1601                    msg.append(StringPool.CLOSE_CURLY_BRACE);
1602    
1603                    throw new NoSuchResourcePermissionException(msg.toString());
1604            }
1605    
1606            /**
1607             * Returns the first resource permission in the ordered set where companyId = &#63; and primKey LIKE &#63;.
1608             *
1609             * @param companyId the company ID
1610             * @param primKey the prim key
1611             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1612             * @return the first matching resource permission, or <code>null</code> if a matching resource permission could not be found
1613             */
1614            @Override
1615            public ResourcePermission fetchByC_LikeP_First(long companyId,
1616                    String primKey, OrderByComparator<ResourcePermission> orderByComparator) {
1617                    List<ResourcePermission> list = findByC_LikeP(companyId, primKey, 0, 1,
1618                                    orderByComparator);
1619    
1620                    if (!list.isEmpty()) {
1621                            return list.get(0);
1622                    }
1623    
1624                    return null;
1625            }
1626    
1627            /**
1628             * Returns the last resource permission in the ordered set where companyId = &#63; and primKey LIKE &#63;.
1629             *
1630             * @param companyId the company ID
1631             * @param primKey the prim key
1632             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1633             * @return the last matching resource permission
1634             * @throws NoSuchResourcePermissionException if a matching resource permission could not be found
1635             */
1636            @Override
1637            public ResourcePermission findByC_LikeP_Last(long companyId,
1638                    String primKey, OrderByComparator<ResourcePermission> orderByComparator)
1639                    throws NoSuchResourcePermissionException {
1640                    ResourcePermission resourcePermission = fetchByC_LikeP_Last(companyId,
1641                                    primKey, orderByComparator);
1642    
1643                    if (resourcePermission != null) {
1644                            return resourcePermission;
1645                    }
1646    
1647                    StringBundler msg = new StringBundler(6);
1648    
1649                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1650    
1651                    msg.append("companyId=");
1652                    msg.append(companyId);
1653    
1654                    msg.append(", primKey=");
1655                    msg.append(primKey);
1656    
1657                    msg.append(StringPool.CLOSE_CURLY_BRACE);
1658    
1659                    throw new NoSuchResourcePermissionException(msg.toString());
1660            }
1661    
1662            /**
1663             * Returns the last resource permission in the ordered set where companyId = &#63; and primKey LIKE &#63;.
1664             *
1665             * @param companyId the company ID
1666             * @param primKey the prim key
1667             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1668             * @return the last matching resource permission, or <code>null</code> if a matching resource permission could not be found
1669             */
1670            @Override
1671            public ResourcePermission fetchByC_LikeP_Last(long companyId,
1672                    String primKey, OrderByComparator<ResourcePermission> orderByComparator) {
1673                    int count = countByC_LikeP(companyId, primKey);
1674    
1675                    if (count == 0) {
1676                            return null;
1677                    }
1678    
1679                    List<ResourcePermission> list = findByC_LikeP(companyId, primKey,
1680                                    count - 1, count, orderByComparator);
1681    
1682                    if (!list.isEmpty()) {
1683                            return list.get(0);
1684                    }
1685    
1686                    return null;
1687            }
1688    
1689            /**
1690             * Returns the resource permissions before and after the current resource permission in the ordered set where companyId = &#63; and primKey LIKE &#63;.
1691             *
1692             * @param resourcePermissionId the primary key of the current resource permission
1693             * @param companyId the company ID
1694             * @param primKey the prim key
1695             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1696             * @return the previous, current, and next resource permission
1697             * @throws NoSuchResourcePermissionException if a resource permission with the primary key could not be found
1698             */
1699            @Override
1700            public ResourcePermission[] findByC_LikeP_PrevAndNext(
1701                    long resourcePermissionId, long companyId, String primKey,
1702                    OrderByComparator<ResourcePermission> orderByComparator)
1703                    throws NoSuchResourcePermissionException {
1704                    ResourcePermission resourcePermission = findByPrimaryKey(resourcePermissionId);
1705    
1706                    Session session = null;
1707    
1708                    try {
1709                            session = openSession();
1710    
1711                            ResourcePermission[] array = new ResourcePermissionImpl[3];
1712    
1713                            array[0] = getByC_LikeP_PrevAndNext(session, resourcePermission,
1714                                            companyId, primKey, orderByComparator, true);
1715    
1716                            array[1] = resourcePermission;
1717    
1718                            array[2] = getByC_LikeP_PrevAndNext(session, resourcePermission,
1719                                            companyId, primKey, orderByComparator, false);
1720    
1721                            return array;
1722                    }
1723                    catch (Exception e) {
1724                            throw processException(e);
1725                    }
1726                    finally {
1727                            closeSession(session);
1728                    }
1729            }
1730    
1731            protected ResourcePermission getByC_LikeP_PrevAndNext(Session session,
1732                    ResourcePermission resourcePermission, long companyId, String primKey,
1733                    OrderByComparator<ResourcePermission> orderByComparator,
1734                    boolean previous) {
1735                    StringBundler query = null;
1736    
1737                    if (orderByComparator != null) {
1738                            query = new StringBundler(6 +
1739                                            (orderByComparator.getOrderByFields().length * 6));
1740                    }
1741                    else {
1742                            query = new StringBundler(3);
1743                    }
1744    
1745                    query.append(_SQL_SELECT_RESOURCEPERMISSION_WHERE);
1746    
1747                    query.append(_FINDER_COLUMN_C_LIKEP_COMPANYID_2);
1748    
1749                    boolean bindPrimKey = false;
1750    
1751                    if (primKey == null) {
1752                            query.append(_FINDER_COLUMN_C_LIKEP_PRIMKEY_1);
1753                    }
1754                    else if (primKey.equals(StringPool.BLANK)) {
1755                            query.append(_FINDER_COLUMN_C_LIKEP_PRIMKEY_3);
1756                    }
1757                    else {
1758                            bindPrimKey = true;
1759    
1760                            query.append(_FINDER_COLUMN_C_LIKEP_PRIMKEY_2);
1761                    }
1762    
1763                    if (orderByComparator != null) {
1764                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
1765    
1766                            if (orderByConditionFields.length > 0) {
1767                                    query.append(WHERE_AND);
1768                            }
1769    
1770                            for (int i = 0; i < orderByConditionFields.length; i++) {
1771                                    query.append(_ORDER_BY_ENTITY_ALIAS);
1772                                    query.append(orderByConditionFields[i]);
1773    
1774                                    if ((i + 1) < orderByConditionFields.length) {
1775                                            if (orderByComparator.isAscending() ^ previous) {
1776                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
1777                                            }
1778                                            else {
1779                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
1780                                            }
1781                                    }
1782                                    else {
1783                                            if (orderByComparator.isAscending() ^ previous) {
1784                                                    query.append(WHERE_GREATER_THAN);
1785                                            }
1786                                            else {
1787                                                    query.append(WHERE_LESSER_THAN);
1788                                            }
1789                                    }
1790                            }
1791    
1792                            query.append(ORDER_BY_CLAUSE);
1793    
1794                            String[] orderByFields = orderByComparator.getOrderByFields();
1795    
1796                            for (int i = 0; i < orderByFields.length; i++) {
1797                                    query.append(_ORDER_BY_ENTITY_ALIAS);
1798                                    query.append(orderByFields[i]);
1799    
1800                                    if ((i + 1) < orderByFields.length) {
1801                                            if (orderByComparator.isAscending() ^ previous) {
1802                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
1803                                            }
1804                                            else {
1805                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
1806                                            }
1807                                    }
1808                                    else {
1809                                            if (orderByComparator.isAscending() ^ previous) {
1810                                                    query.append(ORDER_BY_ASC);
1811                                            }
1812                                            else {
1813                                                    query.append(ORDER_BY_DESC);
1814                                            }
1815                                    }
1816                            }
1817                    }
1818                    else {
1819                            query.append(ResourcePermissionModelImpl.ORDER_BY_JPQL);
1820                    }
1821    
1822                    String sql = query.toString();
1823    
1824                    Query q = session.createQuery(sql);
1825    
1826                    q.setFirstResult(0);
1827                    q.setMaxResults(2);
1828    
1829                    QueryPos qPos = QueryPos.getInstance(q);
1830    
1831                    qPos.add(companyId);
1832    
1833                    if (bindPrimKey) {
1834                            qPos.add(primKey);
1835                    }
1836    
1837                    if (orderByComparator != null) {
1838                            Object[] values = orderByComparator.getOrderByConditionValues(resourcePermission);
1839    
1840                            for (Object value : values) {
1841                                    qPos.add(value);
1842                            }
1843                    }
1844    
1845                    List<ResourcePermission> list = q.list();
1846    
1847                    if (list.size() == 2) {
1848                            return list.get(1);
1849                    }
1850                    else {
1851                            return null;
1852                    }
1853            }
1854    
1855            /**
1856             * Removes all the resource permissions where companyId = &#63; and primKey LIKE &#63; from the database.
1857             *
1858             * @param companyId the company ID
1859             * @param primKey the prim key
1860             */
1861            @Override
1862            public void removeByC_LikeP(long companyId, String primKey) {
1863                    for (ResourcePermission resourcePermission : findByC_LikeP(companyId,
1864                                    primKey, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
1865                            remove(resourcePermission);
1866                    }
1867            }
1868    
1869            /**
1870             * Returns the number of resource permissions where companyId = &#63; and primKey LIKE &#63;.
1871             *
1872             * @param companyId the company ID
1873             * @param primKey the prim key
1874             * @return the number of matching resource permissions
1875             */
1876            @Override
1877            public int countByC_LikeP(long companyId, String primKey) {
1878                    FinderPath finderPath = FINDER_PATH_WITH_PAGINATION_COUNT_BY_C_LIKEP;
1879    
1880                    Object[] finderArgs = new Object[] { companyId, primKey };
1881    
1882                    Long count = (Long)finderCache.getResult(finderPath, finderArgs, this);
1883    
1884                    if (count == null) {
1885                            StringBundler query = new StringBundler(3);
1886    
1887                            query.append(_SQL_COUNT_RESOURCEPERMISSION_WHERE);
1888    
1889                            query.append(_FINDER_COLUMN_C_LIKEP_COMPANYID_2);
1890    
1891                            boolean bindPrimKey = false;
1892    
1893                            if (primKey == null) {
1894                                    query.append(_FINDER_COLUMN_C_LIKEP_PRIMKEY_1);
1895                            }
1896                            else if (primKey.equals(StringPool.BLANK)) {
1897                                    query.append(_FINDER_COLUMN_C_LIKEP_PRIMKEY_3);
1898                            }
1899                            else {
1900                                    bindPrimKey = true;
1901    
1902                                    query.append(_FINDER_COLUMN_C_LIKEP_PRIMKEY_2);
1903                            }
1904    
1905                            String sql = query.toString();
1906    
1907                            Session session = null;
1908    
1909                            try {
1910                                    session = openSession();
1911    
1912                                    Query q = session.createQuery(sql);
1913    
1914                                    QueryPos qPos = QueryPos.getInstance(q);
1915    
1916                                    qPos.add(companyId);
1917    
1918                                    if (bindPrimKey) {
1919                                            qPos.add(primKey);
1920                                    }
1921    
1922                                    count = (Long)q.uniqueResult();
1923    
1924                                    finderCache.putResult(finderPath, finderArgs, count);
1925                            }
1926                            catch (Exception e) {
1927                                    finderCache.removeResult(finderPath, finderArgs);
1928    
1929                                    throw processException(e);
1930                            }
1931                            finally {
1932                                    closeSession(session);
1933                            }
1934                    }
1935    
1936                    return count.intValue();
1937            }
1938    
1939            private static final String _FINDER_COLUMN_C_LIKEP_COMPANYID_2 = "resourcePermission.companyId = ? AND ";
1940            private static final String _FINDER_COLUMN_C_LIKEP_PRIMKEY_1 = "resourcePermission.primKey IS NULL";
1941            private static final String _FINDER_COLUMN_C_LIKEP_PRIMKEY_2 = "resourcePermission.primKey LIKE ?";
1942            private static final String _FINDER_COLUMN_C_LIKEP_PRIMKEY_3 = "(resourcePermission.primKey IS NULL OR resourcePermission.primKey LIKE '')";
1943            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_C_N_S = new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
1944                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED,
1945                            ResourcePermissionImpl.class,
1946                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByC_N_S",
1947                            new String[] {
1948                                    Long.class.getName(), String.class.getName(),
1949                                    Integer.class.getName(),
1950                                    
1951                            Integer.class.getName(), Integer.class.getName(),
1952                                    OrderByComparator.class.getName()
1953                            });
1954            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_N_S = new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
1955                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED,
1956                            ResourcePermissionImpl.class,
1957                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByC_N_S",
1958                            new String[] {
1959                                    Long.class.getName(), String.class.getName(),
1960                                    Integer.class.getName()
1961                            },
1962                            ResourcePermissionModelImpl.COMPANYID_COLUMN_BITMASK |
1963                            ResourcePermissionModelImpl.NAME_COLUMN_BITMASK |
1964                            ResourcePermissionModelImpl.SCOPE_COLUMN_BITMASK);
1965            public static final FinderPath FINDER_PATH_COUNT_BY_C_N_S = new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
1966                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED, Long.class,
1967                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByC_N_S",
1968                            new String[] {
1969                                    Long.class.getName(), String.class.getName(),
1970                                    Integer.class.getName()
1971                            });
1972    
1973            /**
1974             * Returns all the resource permissions where companyId = &#63; and name = &#63; and scope = &#63;.
1975             *
1976             * @param companyId the company ID
1977             * @param name the name
1978             * @param scope the scope
1979             * @return the matching resource permissions
1980             */
1981            @Override
1982            public List<ResourcePermission> findByC_N_S(long companyId, String name,
1983                    int scope) {
1984                    return findByC_N_S(companyId, name, scope, QueryUtil.ALL_POS,
1985                            QueryUtil.ALL_POS, null);
1986            }
1987    
1988            /**
1989             * Returns a range of all the resource permissions where companyId = &#63; and name = &#63; and scope = &#63;.
1990             *
1991             * <p>
1992             * 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 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 QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link 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.
1993             * </p>
1994             *
1995             * @param companyId the company ID
1996             * @param name the name
1997             * @param scope the scope
1998             * @param start the lower bound of the range of resource permissions
1999             * @param end the upper bound of the range of resource permissions (not inclusive)
2000             * @return the range of matching resource permissions
2001             */
2002            @Override
2003            public List<ResourcePermission> findByC_N_S(long companyId, String name,
2004                    int scope, int start, int end) {
2005                    return findByC_N_S(companyId, name, scope, start, end, null);
2006            }
2007    
2008            /**
2009             * Returns an ordered range of all the resource permissions where companyId = &#63; and name = &#63; and scope = &#63;.
2010             *
2011             * <p>
2012             * 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 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 QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link 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.
2013             * </p>
2014             *
2015             * @param companyId the company ID
2016             * @param name the name
2017             * @param scope the scope
2018             * @param start the lower bound of the range of resource permissions
2019             * @param end the upper bound of the range of resource permissions (not inclusive)
2020             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
2021             * @return the ordered range of matching resource permissions
2022             */
2023            @Override
2024            public List<ResourcePermission> findByC_N_S(long companyId, String name,
2025                    int scope, int start, int end,
2026                    OrderByComparator<ResourcePermission> orderByComparator) {
2027                    return findByC_N_S(companyId, name, scope, start, end,
2028                            orderByComparator, true);
2029            }
2030    
2031            /**
2032             * Returns an ordered range of all the resource permissions where companyId = &#63; and name = &#63; and scope = &#63;.
2033             *
2034             * <p>
2035             * 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 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 QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link 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.
2036             * </p>
2037             *
2038             * @param companyId the company ID
2039             * @param name the name
2040             * @param scope the scope
2041             * @param start the lower bound of the range of resource permissions
2042             * @param end the upper bound of the range of resource permissions (not inclusive)
2043             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
2044             * @param retrieveFromCache whether to retrieve from the finder cache
2045             * @return the ordered range of matching resource permissions
2046             */
2047            @Override
2048            public List<ResourcePermission> findByC_N_S(long companyId, String name,
2049                    int scope, int start, int end,
2050                    OrderByComparator<ResourcePermission> orderByComparator,
2051                    boolean retrieveFromCache) {
2052                    boolean pagination = true;
2053                    FinderPath finderPath = null;
2054                    Object[] finderArgs = null;
2055    
2056                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
2057                                    (orderByComparator == null)) {
2058                            pagination = false;
2059                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_N_S;
2060                            finderArgs = new Object[] { companyId, name, scope };
2061                    }
2062                    else {
2063                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_C_N_S;
2064                            finderArgs = new Object[] {
2065                                            companyId, name, scope,
2066                                            
2067                                            start, end, orderByComparator
2068                                    };
2069                    }
2070    
2071                    List<ResourcePermission> list = null;
2072    
2073                    if (retrieveFromCache) {
2074                            list = (List<ResourcePermission>)finderCache.getResult(finderPath,
2075                                            finderArgs, this);
2076    
2077                            if ((list != null) && !list.isEmpty()) {
2078                                    for (ResourcePermission resourcePermission : list) {
2079                                            if ((companyId != resourcePermission.getCompanyId()) ||
2080                                                            !Validator.equals(name, resourcePermission.getName()) ||
2081                                                            (scope != resourcePermission.getScope())) {
2082                                                    list = null;
2083    
2084                                                    break;
2085                                            }
2086                                    }
2087                            }
2088                    }
2089    
2090                    if (list == null) {
2091                            StringBundler query = null;
2092    
2093                            if (orderByComparator != null) {
2094                                    query = new StringBundler(5 +
2095                                                    (orderByComparator.getOrderByFields().length * 3));
2096                            }
2097                            else {
2098                                    query = new StringBundler(5);
2099                            }
2100    
2101                            query.append(_SQL_SELECT_RESOURCEPERMISSION_WHERE);
2102    
2103                            query.append(_FINDER_COLUMN_C_N_S_COMPANYID_2);
2104    
2105                            boolean bindName = false;
2106    
2107                            if (name == null) {
2108                                    query.append(_FINDER_COLUMN_C_N_S_NAME_1);
2109                            }
2110                            else if (name.equals(StringPool.BLANK)) {
2111                                    query.append(_FINDER_COLUMN_C_N_S_NAME_3);
2112                            }
2113                            else {
2114                                    bindName = true;
2115    
2116                                    query.append(_FINDER_COLUMN_C_N_S_NAME_2);
2117                            }
2118    
2119                            query.append(_FINDER_COLUMN_C_N_S_SCOPE_2);
2120    
2121                            if (orderByComparator != null) {
2122                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
2123                                            orderByComparator);
2124                            }
2125                            else
2126                             if (pagination) {
2127                                    query.append(ResourcePermissionModelImpl.ORDER_BY_JPQL);
2128                            }
2129    
2130                            String sql = query.toString();
2131    
2132                            Session session = null;
2133    
2134                            try {
2135                                    session = openSession();
2136    
2137                                    Query q = session.createQuery(sql);
2138    
2139                                    QueryPos qPos = QueryPos.getInstance(q);
2140    
2141                                    qPos.add(companyId);
2142    
2143                                    if (bindName) {
2144                                            qPos.add(name);
2145                                    }
2146    
2147                                    qPos.add(scope);
2148    
2149                                    if (!pagination) {
2150                                            list = (List<ResourcePermission>)QueryUtil.list(q,
2151                                                            getDialect(), start, end, false);
2152    
2153                                            Collections.sort(list);
2154    
2155                                            list = Collections.unmodifiableList(list);
2156                                    }
2157                                    else {
2158                                            list = (List<ResourcePermission>)QueryUtil.list(q,
2159                                                            getDialect(), start, end);
2160                                    }
2161    
2162                                    cacheResult(list);
2163    
2164                                    finderCache.putResult(finderPath, finderArgs, list);
2165                            }
2166                            catch (Exception e) {
2167                                    finderCache.removeResult(finderPath, finderArgs);
2168    
2169                                    throw processException(e);
2170                            }
2171                            finally {
2172                                    closeSession(session);
2173                            }
2174                    }
2175    
2176                    return list;
2177            }
2178    
2179            /**
2180             * Returns the first resource permission in the ordered set where companyId = &#63; and name = &#63; and scope = &#63;.
2181             *
2182             * @param companyId the company ID
2183             * @param name the name
2184             * @param scope the scope
2185             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2186             * @return the first matching resource permission
2187             * @throws NoSuchResourcePermissionException if a matching resource permission could not be found
2188             */
2189            @Override
2190            public ResourcePermission findByC_N_S_First(long companyId, String name,
2191                    int scope, OrderByComparator<ResourcePermission> orderByComparator)
2192                    throws NoSuchResourcePermissionException {
2193                    ResourcePermission resourcePermission = fetchByC_N_S_First(companyId,
2194                                    name, scope, orderByComparator);
2195    
2196                    if (resourcePermission != null) {
2197                            return resourcePermission;
2198                    }
2199    
2200                    StringBundler msg = new StringBundler(8);
2201    
2202                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
2203    
2204                    msg.append("companyId=");
2205                    msg.append(companyId);
2206    
2207                    msg.append(", name=");
2208                    msg.append(name);
2209    
2210                    msg.append(", scope=");
2211                    msg.append(scope);
2212    
2213                    msg.append(StringPool.CLOSE_CURLY_BRACE);
2214    
2215                    throw new NoSuchResourcePermissionException(msg.toString());
2216            }
2217    
2218            /**
2219             * Returns the first resource permission in the ordered set where companyId = &#63; and name = &#63; and scope = &#63;.
2220             *
2221             * @param companyId the company ID
2222             * @param name the name
2223             * @param scope the scope
2224             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2225             * @return the first matching resource permission, or <code>null</code> if a matching resource permission could not be found
2226             */
2227            @Override
2228            public ResourcePermission fetchByC_N_S_First(long companyId, String name,
2229                    int scope, OrderByComparator<ResourcePermission> orderByComparator) {
2230                    List<ResourcePermission> list = findByC_N_S(companyId, name, scope, 0,
2231                                    1, orderByComparator);
2232    
2233                    if (!list.isEmpty()) {
2234                            return list.get(0);
2235                    }
2236    
2237                    return null;
2238            }
2239    
2240            /**
2241             * Returns the last resource permission in the ordered set where companyId = &#63; and name = &#63; and scope = &#63;.
2242             *
2243             * @param companyId the company ID
2244             * @param name the name
2245             * @param scope the scope
2246             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2247             * @return the last matching resource permission
2248             * @throws NoSuchResourcePermissionException if a matching resource permission could not be found
2249             */
2250            @Override
2251            public ResourcePermission findByC_N_S_Last(long companyId, String name,
2252                    int scope, OrderByComparator<ResourcePermission> orderByComparator)
2253                    throws NoSuchResourcePermissionException {
2254                    ResourcePermission resourcePermission = fetchByC_N_S_Last(companyId,
2255                                    name, scope, orderByComparator);
2256    
2257                    if (resourcePermission != null) {
2258                            return resourcePermission;
2259                    }
2260    
2261                    StringBundler msg = new StringBundler(8);
2262    
2263                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
2264    
2265                    msg.append("companyId=");
2266                    msg.append(companyId);
2267    
2268                    msg.append(", name=");
2269                    msg.append(name);
2270    
2271                    msg.append(", scope=");
2272                    msg.append(scope);
2273    
2274                    msg.append(StringPool.CLOSE_CURLY_BRACE);
2275    
2276                    throw new NoSuchResourcePermissionException(msg.toString());
2277            }
2278    
2279            /**
2280             * Returns the last resource permission in the ordered set where companyId = &#63; and name = &#63; and scope = &#63;.
2281             *
2282             * @param companyId the company ID
2283             * @param name the name
2284             * @param scope the scope
2285             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2286             * @return the last matching resource permission, or <code>null</code> if a matching resource permission could not be found
2287             */
2288            @Override
2289            public ResourcePermission fetchByC_N_S_Last(long companyId, String name,
2290                    int scope, OrderByComparator<ResourcePermission> orderByComparator) {
2291                    int count = countByC_N_S(companyId, name, scope);
2292    
2293                    if (count == 0) {
2294                            return null;
2295                    }
2296    
2297                    List<ResourcePermission> list = findByC_N_S(companyId, name, scope,
2298                                    count - 1, count, orderByComparator);
2299    
2300                    if (!list.isEmpty()) {
2301                            return list.get(0);
2302                    }
2303    
2304                    return null;
2305            }
2306    
2307            /**
2308             * Returns the resource permissions before and after the current resource permission in the ordered set where companyId = &#63; and name = &#63; and scope = &#63;.
2309             *
2310             * @param resourcePermissionId the primary key of the current resource permission
2311             * @param companyId the company ID
2312             * @param name the name
2313             * @param scope the scope
2314             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2315             * @return the previous, current, and next resource permission
2316             * @throws NoSuchResourcePermissionException if a resource permission with the primary key could not be found
2317             */
2318            @Override
2319            public ResourcePermission[] findByC_N_S_PrevAndNext(
2320                    long resourcePermissionId, long companyId, String name, int scope,
2321                    OrderByComparator<ResourcePermission> orderByComparator)
2322                    throws NoSuchResourcePermissionException {
2323                    ResourcePermission resourcePermission = findByPrimaryKey(resourcePermissionId);
2324    
2325                    Session session = null;
2326    
2327                    try {
2328                            session = openSession();
2329    
2330                            ResourcePermission[] array = new ResourcePermissionImpl[3];
2331    
2332                            array[0] = getByC_N_S_PrevAndNext(session, resourcePermission,
2333                                            companyId, name, scope, orderByComparator, true);
2334    
2335                            array[1] = resourcePermission;
2336    
2337                            array[2] = getByC_N_S_PrevAndNext(session, resourcePermission,
2338                                            companyId, name, scope, orderByComparator, false);
2339    
2340                            return array;
2341                    }
2342                    catch (Exception e) {
2343                            throw processException(e);
2344                    }
2345                    finally {
2346                            closeSession(session);
2347                    }
2348            }
2349    
2350            protected ResourcePermission getByC_N_S_PrevAndNext(Session session,
2351                    ResourcePermission resourcePermission, long companyId, String name,
2352                    int scope, OrderByComparator<ResourcePermission> orderByComparator,
2353                    boolean previous) {
2354                    StringBundler query = null;
2355    
2356                    if (orderByComparator != null) {
2357                            query = new StringBundler(6 +
2358                                            (orderByComparator.getOrderByFields().length * 6));
2359                    }
2360                    else {
2361                            query = new StringBundler(3);
2362                    }
2363    
2364                    query.append(_SQL_SELECT_RESOURCEPERMISSION_WHERE);
2365    
2366                    query.append(_FINDER_COLUMN_C_N_S_COMPANYID_2);
2367    
2368                    boolean bindName = false;
2369    
2370                    if (name == null) {
2371                            query.append(_FINDER_COLUMN_C_N_S_NAME_1);
2372                    }
2373                    else if (name.equals(StringPool.BLANK)) {
2374                            query.append(_FINDER_COLUMN_C_N_S_NAME_3);
2375                    }
2376                    else {
2377                            bindName = true;
2378    
2379                            query.append(_FINDER_COLUMN_C_N_S_NAME_2);
2380                    }
2381    
2382                    query.append(_FINDER_COLUMN_C_N_S_SCOPE_2);
2383    
2384                    if (orderByComparator != null) {
2385                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
2386    
2387                            if (orderByConditionFields.length > 0) {
2388                                    query.append(WHERE_AND);
2389                            }
2390    
2391                            for (int i = 0; i < orderByConditionFields.length; i++) {
2392                                    query.append(_ORDER_BY_ENTITY_ALIAS);
2393                                    query.append(orderByConditionFields[i]);
2394    
2395                                    if ((i + 1) < orderByConditionFields.length) {
2396                                            if (orderByComparator.isAscending() ^ previous) {
2397                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
2398                                            }
2399                                            else {
2400                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
2401                                            }
2402                                    }
2403                                    else {
2404                                            if (orderByComparator.isAscending() ^ previous) {
2405                                                    query.append(WHERE_GREATER_THAN);
2406                                            }
2407                                            else {
2408                                                    query.append(WHERE_LESSER_THAN);
2409                                            }
2410                                    }
2411                            }
2412    
2413                            query.append(ORDER_BY_CLAUSE);
2414    
2415                            String[] orderByFields = orderByComparator.getOrderByFields();
2416    
2417                            for (int i = 0; i < orderByFields.length; i++) {
2418                                    query.append(_ORDER_BY_ENTITY_ALIAS);
2419                                    query.append(orderByFields[i]);
2420    
2421                                    if ((i + 1) < orderByFields.length) {
2422                                            if (orderByComparator.isAscending() ^ previous) {
2423                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
2424                                            }
2425                                            else {
2426                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
2427                                            }
2428                                    }
2429                                    else {
2430                                            if (orderByComparator.isAscending() ^ previous) {
2431                                                    query.append(ORDER_BY_ASC);
2432                                            }
2433                                            else {
2434                                                    query.append(ORDER_BY_DESC);
2435                                            }
2436                                    }
2437                            }
2438                    }
2439                    else {
2440                            query.append(ResourcePermissionModelImpl.ORDER_BY_JPQL);
2441                    }
2442    
2443                    String sql = query.toString();
2444    
2445                    Query q = session.createQuery(sql);
2446    
2447                    q.setFirstResult(0);
2448                    q.setMaxResults(2);
2449    
2450                    QueryPos qPos = QueryPos.getInstance(q);
2451    
2452                    qPos.add(companyId);
2453    
2454                    if (bindName) {
2455                            qPos.add(name);
2456                    }
2457    
2458                    qPos.add(scope);
2459    
2460                    if (orderByComparator != null) {
2461                            Object[] values = orderByComparator.getOrderByConditionValues(resourcePermission);
2462    
2463                            for (Object value : values) {
2464                                    qPos.add(value);
2465                            }
2466                    }
2467    
2468                    List<ResourcePermission> list = q.list();
2469    
2470                    if (list.size() == 2) {
2471                            return list.get(1);
2472                    }
2473                    else {
2474                            return null;
2475                    }
2476            }
2477    
2478            /**
2479             * Removes all the resource permissions where companyId = &#63; and name = &#63; and scope = &#63; from the database.
2480             *
2481             * @param companyId the company ID
2482             * @param name the name
2483             * @param scope the scope
2484             */
2485            @Override
2486            public void removeByC_N_S(long companyId, String name, int scope) {
2487                    for (ResourcePermission resourcePermission : findByC_N_S(companyId,
2488                                    name, scope, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
2489                            remove(resourcePermission);
2490                    }
2491            }
2492    
2493            /**
2494             * Returns the number of resource permissions where companyId = &#63; and name = &#63; and scope = &#63;.
2495             *
2496             * @param companyId the company ID
2497             * @param name the name
2498             * @param scope the scope
2499             * @return the number of matching resource permissions
2500             */
2501            @Override
2502            public int countByC_N_S(long companyId, String name, int scope) {
2503                    FinderPath finderPath = FINDER_PATH_COUNT_BY_C_N_S;
2504    
2505                    Object[] finderArgs = new Object[] { companyId, name, scope };
2506    
2507                    Long count = (Long)finderCache.getResult(finderPath, finderArgs, this);
2508    
2509                    if (count == null) {
2510                            StringBundler query = new StringBundler(4);
2511    
2512                            query.append(_SQL_COUNT_RESOURCEPERMISSION_WHERE);
2513    
2514                            query.append(_FINDER_COLUMN_C_N_S_COMPANYID_2);
2515    
2516                            boolean bindName = false;
2517    
2518                            if (name == null) {
2519                                    query.append(_FINDER_COLUMN_C_N_S_NAME_1);
2520                            }
2521                            else if (name.equals(StringPool.BLANK)) {
2522                                    query.append(_FINDER_COLUMN_C_N_S_NAME_3);
2523                            }
2524                            else {
2525                                    bindName = true;
2526    
2527                                    query.append(_FINDER_COLUMN_C_N_S_NAME_2);
2528                            }
2529    
2530                            query.append(_FINDER_COLUMN_C_N_S_SCOPE_2);
2531    
2532                            String sql = query.toString();
2533    
2534                            Session session = null;
2535    
2536                            try {
2537                                    session = openSession();
2538    
2539                                    Query q = session.createQuery(sql);
2540    
2541                                    QueryPos qPos = QueryPos.getInstance(q);
2542    
2543                                    qPos.add(companyId);
2544    
2545                                    if (bindName) {
2546                                            qPos.add(name);
2547                                    }
2548    
2549                                    qPos.add(scope);
2550    
2551                                    count = (Long)q.uniqueResult();
2552    
2553                                    finderCache.putResult(finderPath, finderArgs, count);
2554                            }
2555                            catch (Exception e) {
2556                                    finderCache.removeResult(finderPath, finderArgs);
2557    
2558                                    throw processException(e);
2559                            }
2560                            finally {
2561                                    closeSession(session);
2562                            }
2563                    }
2564    
2565                    return count.intValue();
2566            }
2567    
2568            private static final String _FINDER_COLUMN_C_N_S_COMPANYID_2 = "resourcePermission.companyId = ? AND ";
2569            private static final String _FINDER_COLUMN_C_N_S_NAME_1 = "resourcePermission.name IS NULL AND ";
2570            private static final String _FINDER_COLUMN_C_N_S_NAME_2 = "resourcePermission.name = ? AND ";
2571            private static final String _FINDER_COLUMN_C_N_S_NAME_3 = "(resourcePermission.name IS NULL OR resourcePermission.name = '') AND ";
2572            private static final String _FINDER_COLUMN_C_N_S_SCOPE_2 = "resourcePermission.scope = ?";
2573            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_C_N_S_P = new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
2574                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED,
2575                            ResourcePermissionImpl.class,
2576                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByC_N_S_P",
2577                            new String[] {
2578                                    Long.class.getName(), String.class.getName(),
2579                                    Integer.class.getName(), String.class.getName(),
2580                                    
2581                            Integer.class.getName(), Integer.class.getName(),
2582                                    OrderByComparator.class.getName()
2583                            });
2584            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_N_S_P =
2585                    new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
2586                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED,
2587                            ResourcePermissionImpl.class,
2588                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByC_N_S_P",
2589                            new String[] {
2590                                    Long.class.getName(), String.class.getName(),
2591                                    Integer.class.getName(), String.class.getName()
2592                            },
2593                            ResourcePermissionModelImpl.COMPANYID_COLUMN_BITMASK |
2594                            ResourcePermissionModelImpl.NAME_COLUMN_BITMASK |
2595                            ResourcePermissionModelImpl.SCOPE_COLUMN_BITMASK |
2596                            ResourcePermissionModelImpl.PRIMKEY_COLUMN_BITMASK);
2597            public static final FinderPath FINDER_PATH_COUNT_BY_C_N_S_P = new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
2598                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED, Long.class,
2599                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByC_N_S_P",
2600                            new String[] {
2601                                    Long.class.getName(), String.class.getName(),
2602                                    Integer.class.getName(), String.class.getName()
2603                            });
2604    
2605            /**
2606             * Returns all the resource permissions where companyId = &#63; and name = &#63; and scope = &#63; and primKey = &#63;.
2607             *
2608             * @param companyId the company ID
2609             * @param name the name
2610             * @param scope the scope
2611             * @param primKey the prim key
2612             * @return the matching resource permissions
2613             */
2614            @Override
2615            public List<ResourcePermission> findByC_N_S_P(long companyId, String name,
2616                    int scope, String primKey) {
2617                    return findByC_N_S_P(companyId, name, scope, primKey,
2618                            QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
2619            }
2620    
2621            /**
2622             * Returns a range of all the resource permissions where companyId = &#63; and name = &#63; and scope = &#63; and primKey = &#63;.
2623             *
2624             * <p>
2625             * 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 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 QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link 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.
2626             * </p>
2627             *
2628             * @param companyId the company ID
2629             * @param name the name
2630             * @param scope the scope
2631             * @param primKey the prim key
2632             * @param start the lower bound of the range of resource permissions
2633             * @param end the upper bound of the range of resource permissions (not inclusive)
2634             * @return the range of matching resource permissions
2635             */
2636            @Override
2637            public List<ResourcePermission> findByC_N_S_P(long companyId, String name,
2638                    int scope, String primKey, int start, int end) {
2639                    return findByC_N_S_P(companyId, name, scope, primKey, start, end, null);
2640            }
2641    
2642            /**
2643             * Returns an ordered range of all the resource permissions where companyId = &#63; and name = &#63; and scope = &#63; and primKey = &#63;.
2644             *
2645             * <p>
2646             * 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 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 QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link 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.
2647             * </p>
2648             *
2649             * @param companyId the company ID
2650             * @param name the name
2651             * @param scope the scope
2652             * @param primKey the prim key
2653             * @param start the lower bound of the range of resource permissions
2654             * @param end the upper bound of the range of resource permissions (not inclusive)
2655             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
2656             * @return the ordered range of matching resource permissions
2657             */
2658            @Override
2659            public List<ResourcePermission> findByC_N_S_P(long companyId, String name,
2660                    int scope, String primKey, int start, int end,
2661                    OrderByComparator<ResourcePermission> orderByComparator) {
2662                    return findByC_N_S_P(companyId, name, scope, primKey, start, end,
2663                            orderByComparator, true);
2664            }
2665    
2666            /**
2667             * Returns an ordered range of all the resource permissions where companyId = &#63; and name = &#63; and scope = &#63; and primKey = &#63;.
2668             *
2669             * <p>
2670             * 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 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 QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link 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.
2671             * </p>
2672             *
2673             * @param companyId the company ID
2674             * @param name the name
2675             * @param scope the scope
2676             * @param primKey the prim key
2677             * @param start the lower bound of the range of resource permissions
2678             * @param end the upper bound of the range of resource permissions (not inclusive)
2679             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
2680             * @param retrieveFromCache whether to retrieve from the finder cache
2681             * @return the ordered range of matching resource permissions
2682             */
2683            @Override
2684            public List<ResourcePermission> findByC_N_S_P(long companyId, String name,
2685                    int scope, String primKey, int start, int end,
2686                    OrderByComparator<ResourcePermission> orderByComparator,
2687                    boolean retrieveFromCache) {
2688                    boolean pagination = true;
2689                    FinderPath finderPath = null;
2690                    Object[] finderArgs = null;
2691    
2692                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
2693                                    (orderByComparator == null)) {
2694                            pagination = false;
2695                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_N_S_P;
2696                            finderArgs = new Object[] { companyId, name, scope, primKey };
2697                    }
2698                    else {
2699                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_C_N_S_P;
2700                            finderArgs = new Object[] {
2701                                            companyId, name, scope, primKey,
2702                                            
2703                                            start, end, orderByComparator
2704                                    };
2705                    }
2706    
2707                    List<ResourcePermission> list = null;
2708    
2709                    if (retrieveFromCache) {
2710                            list = (List<ResourcePermission>)finderCache.getResult(finderPath,
2711                                            finderArgs, this);
2712    
2713                            if ((list != null) && !list.isEmpty()) {
2714                                    for (ResourcePermission resourcePermission : list) {
2715                                            if ((companyId != resourcePermission.getCompanyId()) ||
2716                                                            !Validator.equals(name, resourcePermission.getName()) ||
2717                                                            (scope != resourcePermission.getScope()) ||
2718                                                            !Validator.equals(primKey,
2719                                                                    resourcePermission.getPrimKey())) {
2720                                                    list = null;
2721    
2722                                                    break;
2723                                            }
2724                                    }
2725                            }
2726                    }
2727    
2728                    if (list == null) {
2729                            StringBundler query = null;
2730    
2731                            if (orderByComparator != null) {
2732                                    query = new StringBundler(6 +
2733                                                    (orderByComparator.getOrderByFields().length * 3));
2734                            }
2735                            else {
2736                                    query = new StringBundler(6);
2737                            }
2738    
2739                            query.append(_SQL_SELECT_RESOURCEPERMISSION_WHERE);
2740    
2741                            query.append(_FINDER_COLUMN_C_N_S_P_COMPANYID_2);
2742    
2743                            boolean bindName = false;
2744    
2745                            if (name == null) {
2746                                    query.append(_FINDER_COLUMN_C_N_S_P_NAME_1);
2747                            }
2748                            else if (name.equals(StringPool.BLANK)) {
2749                                    query.append(_FINDER_COLUMN_C_N_S_P_NAME_3);
2750                            }
2751                            else {
2752                                    bindName = true;
2753    
2754                                    query.append(_FINDER_COLUMN_C_N_S_P_NAME_2);
2755                            }
2756    
2757                            query.append(_FINDER_COLUMN_C_N_S_P_SCOPE_2);
2758    
2759                            boolean bindPrimKey = false;
2760    
2761                            if (primKey == null) {
2762                                    query.append(_FINDER_COLUMN_C_N_S_P_PRIMKEY_1);
2763                            }
2764                            else if (primKey.equals(StringPool.BLANK)) {
2765                                    query.append(_FINDER_COLUMN_C_N_S_P_PRIMKEY_3);
2766                            }
2767                            else {
2768                                    bindPrimKey = true;
2769    
2770                                    query.append(_FINDER_COLUMN_C_N_S_P_PRIMKEY_2);
2771                            }
2772    
2773                            if (orderByComparator != null) {
2774                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
2775                                            orderByComparator);
2776                            }
2777                            else
2778                             if (pagination) {
2779                                    query.append(ResourcePermissionModelImpl.ORDER_BY_JPQL);
2780                            }
2781    
2782                            String sql = query.toString();
2783    
2784                            Session session = null;
2785    
2786                            try {
2787                                    session = openSession();
2788    
2789                                    Query q = session.createQuery(sql);
2790    
2791                                    QueryPos qPos = QueryPos.getInstance(q);
2792    
2793                                    qPos.add(companyId);
2794    
2795                                    if (bindName) {
2796                                            qPos.add(name);
2797                                    }
2798    
2799                                    qPos.add(scope);
2800    
2801                                    if (bindPrimKey) {
2802                                            qPos.add(primKey);
2803                                    }
2804    
2805                                    if (!pagination) {
2806                                            list = (List<ResourcePermission>)QueryUtil.list(q,
2807                                                            getDialect(), start, end, false);
2808    
2809                                            Collections.sort(list);
2810    
2811                                            list = Collections.unmodifiableList(list);
2812                                    }
2813                                    else {
2814                                            list = (List<ResourcePermission>)QueryUtil.list(q,
2815                                                            getDialect(), start, end);
2816                                    }
2817    
2818                                    cacheResult(list);
2819    
2820                                    finderCache.putResult(finderPath, finderArgs, list);
2821                            }
2822                            catch (Exception e) {
2823                                    finderCache.removeResult(finderPath, finderArgs);
2824    
2825                                    throw processException(e);
2826                            }
2827                            finally {
2828                                    closeSession(session);
2829                            }
2830                    }
2831    
2832                    return list;
2833            }
2834    
2835            /**
2836             * Returns the first resource permission in the ordered set where companyId = &#63; and name = &#63; and scope = &#63; and primKey = &#63;.
2837             *
2838             * @param companyId the company ID
2839             * @param name the name
2840             * @param scope the scope
2841             * @param primKey the prim key
2842             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2843             * @return the first matching resource permission
2844             * @throws NoSuchResourcePermissionException if a matching resource permission could not be found
2845             */
2846            @Override
2847            public ResourcePermission findByC_N_S_P_First(long companyId, String name,
2848                    int scope, String primKey,
2849                    OrderByComparator<ResourcePermission> orderByComparator)
2850                    throws NoSuchResourcePermissionException {
2851                    ResourcePermission resourcePermission = fetchByC_N_S_P_First(companyId,
2852                                    name, scope, primKey, orderByComparator);
2853    
2854                    if (resourcePermission != null) {
2855                            return resourcePermission;
2856                    }
2857    
2858                    StringBundler msg = new StringBundler(10);
2859    
2860                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
2861    
2862                    msg.append("companyId=");
2863                    msg.append(companyId);
2864    
2865                    msg.append(", name=");
2866                    msg.append(name);
2867    
2868                    msg.append(", scope=");
2869                    msg.append(scope);
2870    
2871                    msg.append(", primKey=");
2872                    msg.append(primKey);
2873    
2874                    msg.append(StringPool.CLOSE_CURLY_BRACE);
2875    
2876                    throw new NoSuchResourcePermissionException(msg.toString());
2877            }
2878    
2879            /**
2880             * Returns the first resource permission in the ordered set where companyId = &#63; and name = &#63; and scope = &#63; and primKey = &#63;.
2881             *
2882             * @param companyId the company ID
2883             * @param name the name
2884             * @param scope the scope
2885             * @param primKey the prim key
2886             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2887             * @return the first matching resource permission, or <code>null</code> if a matching resource permission could not be found
2888             */
2889            @Override
2890            public ResourcePermission fetchByC_N_S_P_First(long companyId, String name,
2891                    int scope, String primKey,
2892                    OrderByComparator<ResourcePermission> orderByComparator) {
2893                    List<ResourcePermission> list = findByC_N_S_P(companyId, name, scope,
2894                                    primKey, 0, 1, orderByComparator);
2895    
2896                    if (!list.isEmpty()) {
2897                            return list.get(0);
2898                    }
2899    
2900                    return null;
2901            }
2902    
2903            /**
2904             * Returns the last resource permission in the ordered set where companyId = &#63; and name = &#63; and scope = &#63; and primKey = &#63;.
2905             *
2906             * @param companyId the company ID
2907             * @param name the name
2908             * @param scope the scope
2909             * @param primKey the prim key
2910             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2911             * @return the last matching resource permission
2912             * @throws NoSuchResourcePermissionException if a matching resource permission could not be found
2913             */
2914            @Override
2915            public ResourcePermission findByC_N_S_P_Last(long companyId, String name,
2916                    int scope, String primKey,
2917                    OrderByComparator<ResourcePermission> orderByComparator)
2918                    throws NoSuchResourcePermissionException {
2919                    ResourcePermission resourcePermission = fetchByC_N_S_P_Last(companyId,
2920                                    name, scope, primKey, orderByComparator);
2921    
2922                    if (resourcePermission != null) {
2923                            return resourcePermission;
2924                    }
2925    
2926                    StringBundler msg = new StringBundler(10);
2927    
2928                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
2929    
2930                    msg.append("companyId=");
2931                    msg.append(companyId);
2932    
2933                    msg.append(", name=");
2934                    msg.append(name);
2935    
2936                    msg.append(", scope=");
2937                    msg.append(scope);
2938    
2939                    msg.append(", primKey=");
2940                    msg.append(primKey);
2941    
2942                    msg.append(StringPool.CLOSE_CURLY_BRACE);
2943    
2944                    throw new NoSuchResourcePermissionException(msg.toString());
2945            }
2946    
2947            /**
2948             * Returns the last resource permission in the ordered set where companyId = &#63; and name = &#63; and scope = &#63; and primKey = &#63;.
2949             *
2950             * @param companyId the company ID
2951             * @param name the name
2952             * @param scope the scope
2953             * @param primKey the prim key
2954             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2955             * @return the last matching resource permission, or <code>null</code> if a matching resource permission could not be found
2956             */
2957            @Override
2958            public ResourcePermission fetchByC_N_S_P_Last(long companyId, String name,
2959                    int scope, String primKey,
2960                    OrderByComparator<ResourcePermission> orderByComparator) {
2961                    int count = countByC_N_S_P(companyId, name, scope, primKey);
2962    
2963                    if (count == 0) {
2964                            return null;
2965                    }
2966    
2967                    List<ResourcePermission> list = findByC_N_S_P(companyId, name, scope,
2968                                    primKey, count - 1, count, orderByComparator);
2969    
2970                    if (!list.isEmpty()) {
2971                            return list.get(0);
2972                    }
2973    
2974                    return null;
2975            }
2976    
2977            /**
2978             * 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;.
2979             *
2980             * @param resourcePermissionId the primary key of the current resource permission
2981             * @param companyId the company ID
2982             * @param name the name
2983             * @param scope the scope
2984             * @param primKey the prim key
2985             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2986             * @return the previous, current, and next resource permission
2987             * @throws NoSuchResourcePermissionException if a resource permission with the primary key could not be found
2988             */
2989            @Override
2990            public ResourcePermission[] findByC_N_S_P_PrevAndNext(
2991                    long resourcePermissionId, long companyId, String name, int scope,
2992                    String primKey, OrderByComparator<ResourcePermission> orderByComparator)
2993                    throws NoSuchResourcePermissionException {
2994                    ResourcePermission resourcePermission = findByPrimaryKey(resourcePermissionId);
2995    
2996                    Session session = null;
2997    
2998                    try {
2999                            session = openSession();
3000    
3001                            ResourcePermission[] array = new ResourcePermissionImpl[3];
3002    
3003                            array[0] = getByC_N_S_P_PrevAndNext(session, resourcePermission,
3004                                            companyId, name, scope, primKey, orderByComparator, true);
3005    
3006                            array[1] = resourcePermission;
3007    
3008                            array[2] = getByC_N_S_P_PrevAndNext(session, resourcePermission,
3009                                            companyId, name, scope, primKey, orderByComparator, false);
3010    
3011                            return array;
3012                    }
3013                    catch (Exception e) {
3014                            throw processException(e);
3015                    }
3016                    finally {
3017                            closeSession(session);
3018                    }
3019            }
3020    
3021            protected ResourcePermission getByC_N_S_P_PrevAndNext(Session session,
3022                    ResourcePermission resourcePermission, long companyId, String name,
3023                    int scope, String primKey,
3024                    OrderByComparator<ResourcePermission> orderByComparator,
3025                    boolean previous) {
3026                    StringBundler query = null;
3027    
3028                    if (orderByComparator != null) {
3029                            query = new StringBundler(6 +
3030                                            (orderByComparator.getOrderByFields().length * 6));
3031                    }
3032                    else {
3033                            query = new StringBundler(3);
3034                    }
3035    
3036                    query.append(_SQL_SELECT_RESOURCEPERMISSION_WHERE);
3037    
3038                    query.append(_FINDER_COLUMN_C_N_S_P_COMPANYID_2);
3039    
3040                    boolean bindName = false;
3041    
3042                    if (name == null) {
3043                            query.append(_FINDER_COLUMN_C_N_S_P_NAME_1);
3044                    }
3045                    else if (name.equals(StringPool.BLANK)) {
3046                            query.append(_FINDER_COLUMN_C_N_S_P_NAME_3);
3047                    }
3048                    else {
3049                            bindName = true;
3050    
3051                            query.append(_FINDER_COLUMN_C_N_S_P_NAME_2);
3052                    }
3053    
3054                    query.append(_FINDER_COLUMN_C_N_S_P_SCOPE_2);
3055    
3056                    boolean bindPrimKey = false;
3057    
3058                    if (primKey == null) {
3059                            query.append(_FINDER_COLUMN_C_N_S_P_PRIMKEY_1);
3060                    }
3061                    else if (primKey.equals(StringPool.BLANK)) {
3062                            query.append(_FINDER_COLUMN_C_N_S_P_PRIMKEY_3);
3063                    }
3064                    else {
3065                            bindPrimKey = true;
3066    
3067                            query.append(_FINDER_COLUMN_C_N_S_P_PRIMKEY_2);
3068                    }
3069    
3070                    if (orderByComparator != null) {
3071                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
3072    
3073                            if (orderByConditionFields.length > 0) {
3074                                    query.append(WHERE_AND);
3075                            }
3076    
3077                            for (int i = 0; i < orderByConditionFields.length; i++) {
3078                                    query.append(_ORDER_BY_ENTITY_ALIAS);
3079                                    query.append(orderByConditionFields[i]);
3080    
3081                                    if ((i + 1) < orderByConditionFields.length) {
3082                                            if (orderByComparator.isAscending() ^ previous) {
3083                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
3084                                            }
3085                                            else {
3086                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
3087                                            }
3088                                    }
3089                                    else {
3090                                            if (orderByComparator.isAscending() ^ previous) {
3091                                                    query.append(WHERE_GREATER_THAN);
3092                                            }
3093                                            else {
3094                                                    query.append(WHERE_LESSER_THAN);
3095                                            }
3096                                    }
3097                            }
3098    
3099                            query.append(ORDER_BY_CLAUSE);
3100    
3101                            String[] orderByFields = orderByComparator.getOrderByFields();
3102    
3103                            for (int i = 0; i < orderByFields.length; i++) {
3104                                    query.append(_ORDER_BY_ENTITY_ALIAS);
3105                                    query.append(orderByFields[i]);
3106    
3107                                    if ((i + 1) < orderByFields.length) {
3108                                            if (orderByComparator.isAscending() ^ previous) {
3109                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
3110                                            }
3111                                            else {
3112                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
3113                                            }
3114                                    }
3115                                    else {
3116                                            if (orderByComparator.isAscending() ^ previous) {
3117                                                    query.append(ORDER_BY_ASC);
3118                                            }
3119                                            else {
3120                                                    query.append(ORDER_BY_DESC);
3121                                            }
3122                                    }
3123                            }
3124                    }
3125                    else {
3126                            query.append(ResourcePermissionModelImpl.ORDER_BY_JPQL);
3127                    }
3128    
3129                    String sql = query.toString();
3130    
3131                    Query q = session.createQuery(sql);
3132    
3133                    q.setFirstResult(0);
3134                    q.setMaxResults(2);
3135    
3136                    QueryPos qPos = QueryPos.getInstance(q);
3137    
3138                    qPos.add(companyId);
3139    
3140                    if (bindName) {
3141                            qPos.add(name);
3142                    }
3143    
3144                    qPos.add(scope);
3145    
3146                    if (bindPrimKey) {
3147                            qPos.add(primKey);
3148                    }
3149    
3150                    if (orderByComparator != null) {
3151                            Object[] values = orderByComparator.getOrderByConditionValues(resourcePermission);
3152    
3153                            for (Object value : values) {
3154                                    qPos.add(value);
3155                            }
3156                    }
3157    
3158                    List<ResourcePermission> list = q.list();
3159    
3160                    if (list.size() == 2) {
3161                            return list.get(1);
3162                    }
3163                    else {
3164                            return null;
3165                    }
3166            }
3167    
3168            /**
3169             * Removes all the resource permissions where companyId = &#63; and name = &#63; and scope = &#63; and primKey = &#63; from the database.
3170             *
3171             * @param companyId the company ID
3172             * @param name the name
3173             * @param scope the scope
3174             * @param primKey the prim key
3175             */
3176            @Override
3177            public void removeByC_N_S_P(long companyId, String name, int scope,
3178                    String primKey) {
3179                    for (ResourcePermission resourcePermission : findByC_N_S_P(companyId,
3180                                    name, scope, primKey, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
3181                            remove(resourcePermission);
3182                    }
3183            }
3184    
3185            /**
3186             * Returns the number of resource permissions where companyId = &#63; and name = &#63; and scope = &#63; and primKey = &#63;.
3187             *
3188             * @param companyId the company ID
3189             * @param name the name
3190             * @param scope the scope
3191             * @param primKey the prim key
3192             * @return the number of matching resource permissions
3193             */
3194            @Override
3195            public int countByC_N_S_P(long companyId, String name, int scope,
3196                    String primKey) {
3197                    FinderPath finderPath = FINDER_PATH_COUNT_BY_C_N_S_P;
3198    
3199                    Object[] finderArgs = new Object[] { companyId, name, scope, primKey };
3200    
3201                    Long count = (Long)finderCache.getResult(finderPath, finderArgs, this);
3202    
3203                    if (count == null) {
3204                            StringBundler query = new StringBundler(5);
3205    
3206                            query.append(_SQL_COUNT_RESOURCEPERMISSION_WHERE);
3207    
3208                            query.append(_FINDER_COLUMN_C_N_S_P_COMPANYID_2);
3209    
3210                            boolean bindName = false;
3211    
3212                            if (name == null) {
3213                                    query.append(_FINDER_COLUMN_C_N_S_P_NAME_1);
3214                            }
3215                            else if (name.equals(StringPool.BLANK)) {
3216                                    query.append(_FINDER_COLUMN_C_N_S_P_NAME_3);
3217                            }
3218                            else {
3219                                    bindName = true;
3220    
3221                                    query.append(_FINDER_COLUMN_C_N_S_P_NAME_2);
3222                            }
3223    
3224                            query.append(_FINDER_COLUMN_C_N_S_P_SCOPE_2);
3225    
3226                            boolean bindPrimKey = false;
3227    
3228                            if (primKey == null) {
3229                                    query.append(_FINDER_COLUMN_C_N_S_P_PRIMKEY_1);
3230                            }
3231                            else if (primKey.equals(StringPool.BLANK)) {
3232                                    query.append(_FINDER_COLUMN_C_N_S_P_PRIMKEY_3);
3233                            }
3234                            else {
3235                                    bindPrimKey = true;
3236    
3237                                    query.append(_FINDER_COLUMN_C_N_S_P_PRIMKEY_2);
3238                            }
3239    
3240                            String sql = query.toString();
3241    
3242                            Session session = null;
3243    
3244                            try {
3245                                    session = openSession();
3246    
3247                                    Query q = session.createQuery(sql);
3248    
3249                                    QueryPos qPos = QueryPos.getInstance(q);
3250    
3251                                    qPos.add(companyId);
3252    
3253                                    if (bindName) {
3254                                            qPos.add(name);
3255                                    }
3256    
3257                                    qPos.add(scope);
3258    
3259                                    if (bindPrimKey) {
3260                                            qPos.add(primKey);
3261                                    }
3262    
3263                                    count = (Long)q.uniqueResult();
3264    
3265                                    finderCache.putResult(finderPath, finderArgs, count);
3266                            }
3267                            catch (Exception e) {
3268                                    finderCache.removeResult(finderPath, finderArgs);
3269    
3270                                    throw processException(e);
3271                            }
3272                            finally {
3273                                    closeSession(session);
3274                            }
3275                    }
3276    
3277                    return count.intValue();
3278            }
3279    
3280            private static final String _FINDER_COLUMN_C_N_S_P_COMPANYID_2 = "resourcePermission.companyId = ? AND ";
3281            private static final String _FINDER_COLUMN_C_N_S_P_NAME_1 = "resourcePermission.name IS NULL AND ";
3282            private static final String _FINDER_COLUMN_C_N_S_P_NAME_2 = "resourcePermission.name = ? AND ";
3283            private static final String _FINDER_COLUMN_C_N_S_P_NAME_3 = "(resourcePermission.name IS NULL OR resourcePermission.name = '') AND ";
3284            private static final String _FINDER_COLUMN_C_N_S_P_SCOPE_2 = "resourcePermission.scope = ? AND ";
3285            private static final String _FINDER_COLUMN_C_N_S_P_PRIMKEY_1 = "resourcePermission.primKey IS NULL";
3286            private static final String _FINDER_COLUMN_C_N_S_P_PRIMKEY_2 = "resourcePermission.primKey = ?";
3287            private static final String _FINDER_COLUMN_C_N_S_P_PRIMKEY_3 = "(resourcePermission.primKey IS NULL OR resourcePermission.primKey = '')";
3288            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_C_N_S_P_R =
3289                    new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
3290                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED,
3291                            ResourcePermissionImpl.class,
3292                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByC_N_S_P_R",
3293                            new String[] {
3294                                    Long.class.getName(), String.class.getName(),
3295                                    Integer.class.getName(), String.class.getName(),
3296                                    Long.class.getName(),
3297                                    
3298                            Integer.class.getName(), Integer.class.getName(),
3299                                    OrderByComparator.class.getName()
3300                            });
3301            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_N_S_P_R =
3302                    new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
3303                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED,
3304                            ResourcePermissionImpl.class,
3305                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByC_N_S_P_R",
3306                            new String[] {
3307                                    Long.class.getName(), String.class.getName(),
3308                                    Integer.class.getName(), String.class.getName(),
3309                                    Long.class.getName()
3310                            },
3311                            ResourcePermissionModelImpl.COMPANYID_COLUMN_BITMASK |
3312                            ResourcePermissionModelImpl.NAME_COLUMN_BITMASK |
3313                            ResourcePermissionModelImpl.SCOPE_COLUMN_BITMASK |
3314                            ResourcePermissionModelImpl.PRIMKEY_COLUMN_BITMASK |
3315                            ResourcePermissionModelImpl.ROLEID_COLUMN_BITMASK);
3316            public static final FinderPath FINDER_PATH_FETCH_BY_C_N_S_P_R = new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
3317                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED,
3318                            ResourcePermissionImpl.class, FINDER_CLASS_NAME_ENTITY,
3319                            "fetchByC_N_S_P_R",
3320                            new String[] {
3321                                    Long.class.getName(), String.class.getName(),
3322                                    Integer.class.getName(), String.class.getName(),
3323                                    Long.class.getName()
3324                            },
3325                            ResourcePermissionModelImpl.COMPANYID_COLUMN_BITMASK |
3326                            ResourcePermissionModelImpl.NAME_COLUMN_BITMASK |
3327                            ResourcePermissionModelImpl.SCOPE_COLUMN_BITMASK |
3328                            ResourcePermissionModelImpl.PRIMKEY_COLUMN_BITMASK |
3329                            ResourcePermissionModelImpl.ROLEID_COLUMN_BITMASK);
3330            public static final FinderPath FINDER_PATH_COUNT_BY_C_N_S_P_R = new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
3331                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED, Long.class,
3332                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByC_N_S_P_R",
3333                            new String[] {
3334                                    Long.class.getName(), String.class.getName(),
3335                                    Integer.class.getName(), String.class.getName(),
3336                                    Long.class.getName()
3337                            });
3338            public static final FinderPath FINDER_PATH_WITH_PAGINATION_COUNT_BY_C_N_S_P_R =
3339                    new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
3340                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED, Long.class,
3341                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "countByC_N_S_P_R",
3342                            new String[] {
3343                                    Long.class.getName(), String.class.getName(),
3344                                    Integer.class.getName(), String.class.getName(),
3345                                    Long.class.getName()
3346                            });
3347    
3348            /**
3349             * Returns all the resource permissions where companyId = &#63; and name = &#63; and scope = &#63; and primKey = &#63; and roleId = any &#63;.
3350             *
3351             * <p>
3352             * 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 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 QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link 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.
3353             * </p>
3354             *
3355             * @param companyId the company ID
3356             * @param name the name
3357             * @param scope the scope
3358             * @param primKey the prim key
3359             * @param roleIds the role IDs
3360             * @return the matching resource permissions
3361             */
3362            @Override
3363            public List<ResourcePermission> findByC_N_S_P_R(long companyId,
3364                    String name, int scope, String primKey, long[] roleIds) {
3365                    return findByC_N_S_P_R(companyId, name, scope, primKey, roleIds,
3366                            QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
3367            }
3368    
3369            /**
3370             * Returns a range of all the resource permissions where companyId = &#63; and name = &#63; and scope = &#63; and primKey = &#63; and roleId = any &#63;.
3371             *
3372             * <p>
3373             * 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 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 QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link 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.
3374             * </p>
3375             *
3376             * @param companyId the company ID
3377             * @param name the name
3378             * @param scope the scope
3379             * @param primKey the prim key
3380             * @param roleIds the role IDs
3381             * @param start the lower bound of the range of resource permissions
3382             * @param end the upper bound of the range of resource permissions (not inclusive)
3383             * @return the range of matching resource permissions
3384             */
3385            @Override
3386            public List<ResourcePermission> findByC_N_S_P_R(long companyId,
3387                    String name, int scope, String primKey, long[] roleIds, int start,
3388                    int end) {
3389                    return findByC_N_S_P_R(companyId, name, scope, primKey, roleIds, start,
3390                            end, null);
3391            }
3392    
3393            /**
3394             * 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;.
3395             *
3396             * <p>
3397             * 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 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 QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link 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.
3398             * </p>
3399             *
3400             * @param companyId the company ID
3401             * @param name the name
3402             * @param scope the scope
3403             * @param primKey the prim key
3404             * @param roleIds the role IDs
3405             * @param start the lower bound of the range of resource permissions
3406             * @param end the upper bound of the range of resource permissions (not inclusive)
3407             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
3408             * @return the ordered range of matching resource permissions
3409             */
3410            @Override
3411            public List<ResourcePermission> findByC_N_S_P_R(long companyId,
3412                    String name, int scope, String primKey, long[] roleIds, int start,
3413                    int end, OrderByComparator<ResourcePermission> orderByComparator) {
3414                    return findByC_N_S_P_R(companyId, name, scope, primKey, roleIds, start,
3415                            end, orderByComparator, true);
3416            }
3417    
3418            /**
3419             * Returns an ordered range of all the resource permissions where companyId = &#63; and name = &#63; and scope = &#63; and primKey = &#63; and roleId = &#63;, optionally using the finder cache.
3420             *
3421             * <p>
3422             * 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 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 QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link 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.
3423             * </p>
3424             *
3425             * @param companyId the company ID
3426             * @param name the name
3427             * @param scope the scope
3428             * @param primKey the prim key
3429             * @param roleId the role ID
3430             * @param start the lower bound of the range of resource permissions
3431             * @param end the upper bound of the range of resource permissions (not inclusive)
3432             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
3433             * @param retrieveFromCache whether to retrieve from the finder cache
3434             * @return the ordered range of matching resource permissions
3435             */
3436            @Override
3437            public List<ResourcePermission> findByC_N_S_P_R(long companyId,
3438                    String name, int scope, String primKey, long[] roleIds, int start,
3439                    int end, OrderByComparator<ResourcePermission> orderByComparator,
3440                    boolean retrieveFromCache) {
3441                    if (roleIds == null) {
3442                            roleIds = new long[0];
3443                    }
3444                    else if (roleIds.length > 1) {
3445                            roleIds = ArrayUtil.unique(roleIds);
3446    
3447                            Arrays.sort(roleIds);
3448                    }
3449    
3450                    if (roleIds.length == 1) {
3451                            ResourcePermission resourcePermission = fetchByC_N_S_P_R(companyId,
3452                                            name, scope, primKey, roleIds[0]);
3453    
3454                            if (resourcePermission == null) {
3455                                    return Collections.emptyList();
3456                            }
3457                            else {
3458                                    List<ResourcePermission> list = new ArrayList<ResourcePermission>(1);
3459    
3460                                    list.add(resourcePermission);
3461    
3462                                    return list;
3463                            }
3464                    }
3465    
3466                    boolean pagination = true;
3467                    Object[] finderArgs = null;
3468    
3469                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
3470                                    (orderByComparator == null)) {
3471                            pagination = false;
3472                            finderArgs = new Object[] {
3473                                            companyId, name, scope, primKey, StringUtil.merge(roleIds)
3474                                    };
3475                    }
3476                    else {
3477                            finderArgs = new Object[] {
3478                                            companyId, name, scope, primKey, StringUtil.merge(roleIds),
3479                                            
3480                                            start, end, orderByComparator
3481                                    };
3482                    }
3483    
3484                    List<ResourcePermission> list = null;
3485    
3486                    if (retrieveFromCache) {
3487                            list = (List<ResourcePermission>)finderCache.getResult(FINDER_PATH_WITH_PAGINATION_FIND_BY_C_N_S_P_R,
3488                                            finderArgs, this);
3489    
3490                            if ((list != null) && !list.isEmpty()) {
3491                                    for (ResourcePermission resourcePermission : list) {
3492                                            if ((companyId != resourcePermission.getCompanyId()) ||
3493                                                            !Validator.equals(name, resourcePermission.getName()) ||
3494                                                            (scope != resourcePermission.getScope()) ||
3495                                                            !Validator.equals(primKey,
3496                                                                    resourcePermission.getPrimKey()) ||
3497                                                            !ArrayUtil.contains(roleIds,
3498                                                                    resourcePermission.getRoleId())) {
3499                                                    list = null;
3500    
3501                                                    break;
3502                                            }
3503                                    }
3504                            }
3505                    }
3506    
3507                    if (list == null) {
3508                            StringBundler query = new StringBundler();
3509    
3510                            query.append(_SQL_SELECT_RESOURCEPERMISSION_WHERE);
3511    
3512                            query.append(_FINDER_COLUMN_C_N_S_P_R_COMPANYID_2);
3513    
3514                            boolean bindName = false;
3515    
3516                            if (name == null) {
3517                                    query.append(_FINDER_COLUMN_C_N_S_P_R_NAME_1);
3518                            }
3519                            else if (name.equals(StringPool.BLANK)) {
3520                                    query.append(_FINDER_COLUMN_C_N_S_P_R_NAME_3);
3521                            }
3522                            else {
3523                                    bindName = true;
3524    
3525                                    query.append(_FINDER_COLUMN_C_N_S_P_R_NAME_2);
3526                            }
3527    
3528                            query.append(_FINDER_COLUMN_C_N_S_P_R_SCOPE_2);
3529    
3530                            boolean bindPrimKey = false;
3531    
3532                            if (primKey == null) {
3533                                    query.append(_FINDER_COLUMN_C_N_S_P_R_PRIMKEY_1);
3534                            }
3535                            else if (primKey.equals(StringPool.BLANK)) {
3536                                    query.append(_FINDER_COLUMN_C_N_S_P_R_PRIMKEY_3);
3537                            }
3538                            else {
3539                                    bindPrimKey = true;
3540    
3541                                    query.append(_FINDER_COLUMN_C_N_S_P_R_PRIMKEY_2);
3542                            }
3543    
3544                            if (roleIds.length > 0) {
3545                                    query.append(StringPool.OPEN_PARENTHESIS);
3546    
3547                                    query.append(_FINDER_COLUMN_C_N_S_P_R_ROLEID_7);
3548    
3549                                    query.append(StringUtil.merge(roleIds));
3550    
3551                                    query.append(StringPool.CLOSE_PARENTHESIS);
3552    
3553                                    query.append(StringPool.CLOSE_PARENTHESIS);
3554                            }
3555    
3556                            query.setStringAt(removeConjunction(query.stringAt(query.index() -
3557                                                    1)), query.index() - 1);
3558    
3559                            if (orderByComparator != null) {
3560                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
3561                                            orderByComparator);
3562                            }
3563                            else
3564                             if (pagination) {
3565                                    query.append(ResourcePermissionModelImpl.ORDER_BY_JPQL);
3566                            }
3567    
3568                            String sql = query.toString();
3569    
3570                            Session session = null;
3571    
3572                            try {
3573                                    session = openSession();
3574    
3575                                    Query q = session.createQuery(sql);
3576    
3577                                    QueryPos qPos = QueryPos.getInstance(q);
3578    
3579                                    qPos.add(companyId);
3580    
3581                                    if (bindName) {
3582                                            qPos.add(name);
3583                                    }
3584    
3585                                    qPos.add(scope);
3586    
3587                                    if (bindPrimKey) {
3588                                            qPos.add(primKey);
3589                                    }
3590    
3591                                    if (!pagination) {
3592                                            list = (List<ResourcePermission>)QueryUtil.list(q,
3593                                                            getDialect(), start, end, false);
3594    
3595                                            Collections.sort(list);
3596    
3597                                            list = Collections.unmodifiableList(list);
3598                                    }
3599                                    else {
3600                                            list = (List<ResourcePermission>)QueryUtil.list(q,
3601                                                            getDialect(), start, end);
3602                                    }
3603    
3604                                    cacheResult(list);
3605    
3606                                    finderCache.putResult(FINDER_PATH_WITH_PAGINATION_FIND_BY_C_N_S_P_R,
3607                                            finderArgs, list);
3608                            }
3609                            catch (Exception e) {
3610                                    finderCache.removeResult(FINDER_PATH_WITH_PAGINATION_FIND_BY_C_N_S_P_R,
3611                                            finderArgs);
3612    
3613                                    throw processException(e);
3614                            }
3615                            finally {
3616                                    closeSession(session);
3617                            }
3618                    }
3619    
3620                    return list;
3621            }
3622    
3623            /**
3624             * Returns the resource permission where companyId = &#63; and name = &#63; and scope = &#63; and primKey = &#63; and roleId = &#63; or throws a {@link NoSuchResourcePermissionException} if it could not be found.
3625             *
3626             * @param companyId the company ID
3627             * @param name the name
3628             * @param scope the scope
3629             * @param primKey the prim key
3630             * @param roleId the role ID
3631             * @return the matching resource permission
3632             * @throws NoSuchResourcePermissionException if a matching resource permission could not be found
3633             */
3634            @Override
3635            public ResourcePermission findByC_N_S_P_R(long companyId, String name,
3636                    int scope, String primKey, long roleId)
3637                    throws NoSuchResourcePermissionException {
3638                    ResourcePermission resourcePermission = fetchByC_N_S_P_R(companyId,
3639                                    name, scope, primKey, roleId);
3640    
3641                    if (resourcePermission == null) {
3642                            StringBundler msg = new StringBundler(12);
3643    
3644                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
3645    
3646                            msg.append("companyId=");
3647                            msg.append(companyId);
3648    
3649                            msg.append(", name=");
3650                            msg.append(name);
3651    
3652                            msg.append(", scope=");
3653                            msg.append(scope);
3654    
3655                            msg.append(", primKey=");
3656                            msg.append(primKey);
3657    
3658                            msg.append(", roleId=");
3659                            msg.append(roleId);
3660    
3661                            msg.append(StringPool.CLOSE_CURLY_BRACE);
3662    
3663                            if (_log.isWarnEnabled()) {
3664                                    _log.warn(msg.toString());
3665                            }
3666    
3667                            throw new NoSuchResourcePermissionException(msg.toString());
3668                    }
3669    
3670                    return resourcePermission;
3671            }
3672    
3673            /**
3674             * 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.
3675             *
3676             * @param companyId the company ID
3677             * @param name the name
3678             * @param scope the scope
3679             * @param primKey the prim key
3680             * @param roleId the role ID
3681             * @return the matching resource permission, or <code>null</code> if a matching resource permission could not be found
3682             */
3683            @Override
3684            public ResourcePermission fetchByC_N_S_P_R(long companyId, String name,
3685                    int scope, String primKey, long roleId) {
3686                    return fetchByC_N_S_P_R(companyId, name, scope, primKey, roleId, true);
3687            }
3688    
3689            /**
3690             * 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.
3691             *
3692             * @param companyId the company ID
3693             * @param name the name
3694             * @param scope the scope
3695             * @param primKey the prim key
3696             * @param roleId the role ID
3697             * @param retrieveFromCache whether to retrieve from the finder cache
3698             * @return the matching resource permission, or <code>null</code> if a matching resource permission could not be found
3699             */
3700            @Override
3701            public ResourcePermission fetchByC_N_S_P_R(long companyId, String name,
3702                    int scope, String primKey, long roleId, boolean retrieveFromCache) {
3703                    Object[] finderArgs = new Object[] {
3704                                    companyId, name, scope, primKey, roleId
3705                            };
3706    
3707                    Object result = null;
3708    
3709                    if (retrieveFromCache) {
3710                            result = finderCache.getResult(FINDER_PATH_FETCH_BY_C_N_S_P_R,
3711                                            finderArgs, this);
3712                    }
3713    
3714                    if (result instanceof ResourcePermission) {
3715                            ResourcePermission resourcePermission = (ResourcePermission)result;
3716    
3717                            if ((companyId != resourcePermission.getCompanyId()) ||
3718                                            !Validator.equals(name, resourcePermission.getName()) ||
3719                                            (scope != resourcePermission.getScope()) ||
3720                                            !Validator.equals(primKey, resourcePermission.getPrimKey()) ||
3721                                            (roleId != resourcePermission.getRoleId())) {
3722                                    result = null;
3723                            }
3724                    }
3725    
3726                    if (result == null) {
3727                            StringBundler query = new StringBundler(7);
3728    
3729                            query.append(_SQL_SELECT_RESOURCEPERMISSION_WHERE);
3730    
3731                            query.append(_FINDER_COLUMN_C_N_S_P_R_COMPANYID_2);
3732    
3733                            boolean bindName = false;
3734    
3735                            if (name == null) {
3736                                    query.append(_FINDER_COLUMN_C_N_S_P_R_NAME_1);
3737                            }
3738                            else if (name.equals(StringPool.BLANK)) {
3739                                    query.append(_FINDER_COLUMN_C_N_S_P_R_NAME_3);
3740                            }
3741                            else {
3742                                    bindName = true;
3743    
3744                                    query.append(_FINDER_COLUMN_C_N_S_P_R_NAME_2);
3745                            }
3746    
3747                            query.append(_FINDER_COLUMN_C_N_S_P_R_SCOPE_2);
3748    
3749                            boolean bindPrimKey = false;
3750    
3751                            if (primKey == null) {
3752                                    query.append(_FINDER_COLUMN_C_N_S_P_R_PRIMKEY_1);
3753                            }
3754                            else if (primKey.equals(StringPool.BLANK)) {
3755                                    query.append(_FINDER_COLUMN_C_N_S_P_R_PRIMKEY_3);
3756                            }
3757                            else {
3758                                    bindPrimKey = true;
3759    
3760                                    query.append(_FINDER_COLUMN_C_N_S_P_R_PRIMKEY_2);
3761                            }
3762    
3763                            query.append(_FINDER_COLUMN_C_N_S_P_R_ROLEID_2);
3764    
3765                            String sql = query.toString();
3766    
3767                            Session session = null;
3768    
3769                            try {
3770                                    session = openSession();
3771    
3772                                    Query q = session.createQuery(sql);
3773    
3774                                    QueryPos qPos = QueryPos.getInstance(q);
3775    
3776                                    qPos.add(companyId);
3777    
3778                                    if (bindName) {
3779                                            qPos.add(name);
3780                                    }
3781    
3782                                    qPos.add(scope);
3783    
3784                                    if (bindPrimKey) {
3785                                            qPos.add(primKey);
3786                                    }
3787    
3788                                    qPos.add(roleId);
3789    
3790                                    List<ResourcePermission> list = q.list();
3791    
3792                                    if (list.isEmpty()) {
3793                                            finderCache.putResult(FINDER_PATH_FETCH_BY_C_N_S_P_R,
3794                                                    finderArgs, list);
3795                                    }
3796                                    else {
3797                                            ResourcePermission resourcePermission = list.get(0);
3798    
3799                                            result = resourcePermission;
3800    
3801                                            cacheResult(resourcePermission);
3802    
3803                                            if ((resourcePermission.getCompanyId() != companyId) ||
3804                                                            (resourcePermission.getName() == null) ||
3805                                                            !resourcePermission.getName().equals(name) ||
3806                                                            (resourcePermission.getScope() != scope) ||
3807                                                            (resourcePermission.getPrimKey() == null) ||
3808                                                            !resourcePermission.getPrimKey().equals(primKey) ||
3809                                                            (resourcePermission.getRoleId() != roleId)) {
3810                                                    finderCache.putResult(FINDER_PATH_FETCH_BY_C_N_S_P_R,
3811                                                            finderArgs, resourcePermission);
3812                                            }
3813                                    }
3814                            }
3815                            catch (Exception e) {
3816                                    finderCache.removeResult(FINDER_PATH_FETCH_BY_C_N_S_P_R,
3817                                            finderArgs);
3818    
3819                                    throw processException(e);
3820                            }
3821                            finally {
3822                                    closeSession(session);
3823                            }
3824                    }
3825    
3826                    if (result instanceof List<?>) {
3827                            return null;
3828                    }
3829                    else {
3830                            return (ResourcePermission)result;
3831                    }
3832            }
3833    
3834            /**
3835             * Removes the resource permission where companyId = &#63; and name = &#63; and scope = &#63; and primKey = &#63; and roleId = &#63; from the database.
3836             *
3837             * @param companyId the company ID
3838             * @param name the name
3839             * @param scope the scope
3840             * @param primKey the prim key
3841             * @param roleId the role ID
3842             * @return the resource permission that was removed
3843             */
3844            @Override
3845            public ResourcePermission removeByC_N_S_P_R(long companyId, String name,
3846                    int scope, String primKey, long roleId)
3847                    throws NoSuchResourcePermissionException {
3848                    ResourcePermission resourcePermission = findByC_N_S_P_R(companyId,
3849                                    name, scope, primKey, roleId);
3850    
3851                    return remove(resourcePermission);
3852            }
3853    
3854            /**
3855             * Returns the number of resource permissions where companyId = &#63; and name = &#63; and scope = &#63; and primKey = &#63; and roleId = &#63;.
3856             *
3857             * @param companyId the company ID
3858             * @param name the name
3859             * @param scope the scope
3860             * @param primKey the prim key
3861             * @param roleId the role ID
3862             * @return the number of matching resource permissions
3863             */
3864            @Override
3865            public int countByC_N_S_P_R(long companyId, String name, int scope,
3866                    String primKey, long roleId) {
3867                    FinderPath finderPath = FINDER_PATH_COUNT_BY_C_N_S_P_R;
3868    
3869                    Object[] finderArgs = new Object[] {
3870                                    companyId, name, scope, primKey, roleId
3871                            };
3872    
3873                    Long count = (Long)finderCache.getResult(finderPath, finderArgs, this);
3874    
3875                    if (count == null) {
3876                            StringBundler query = new StringBundler(6);
3877    
3878                            query.append(_SQL_COUNT_RESOURCEPERMISSION_WHERE);
3879    
3880                            query.append(_FINDER_COLUMN_C_N_S_P_R_COMPANYID_2);
3881    
3882                            boolean bindName = false;
3883    
3884                            if (name == null) {
3885                                    query.append(_FINDER_COLUMN_C_N_S_P_R_NAME_1);
3886                            }
3887                            else if (name.equals(StringPool.BLANK)) {
3888                                    query.append(_FINDER_COLUMN_C_N_S_P_R_NAME_3);
3889                            }
3890                            else {
3891                                    bindName = true;
3892    
3893                                    query.append(_FINDER_COLUMN_C_N_S_P_R_NAME_2);
3894                            }
3895    
3896                            query.append(_FINDER_COLUMN_C_N_S_P_R_SCOPE_2);
3897    
3898                            boolean bindPrimKey = false;
3899    
3900                            if (primKey == null) {
3901                                    query.append(_FINDER_COLUMN_C_N_S_P_R_PRIMKEY_1);
3902                            }
3903                            else if (primKey.equals(StringPool.BLANK)) {
3904                                    query.append(_FINDER_COLUMN_C_N_S_P_R_PRIMKEY_3);
3905                            }
3906                            else {
3907                                    bindPrimKey = true;
3908    
3909                                    query.append(_FINDER_COLUMN_C_N_S_P_R_PRIMKEY_2);
3910                            }
3911    
3912                            query.append(_FINDER_COLUMN_C_N_S_P_R_ROLEID_2);
3913    
3914                            String sql = query.toString();
3915    
3916                            Session session = null;
3917    
3918                            try {
3919                                    session = openSession();
3920    
3921                                    Query q = session.createQuery(sql);
3922    
3923                                    QueryPos qPos = QueryPos.getInstance(q);
3924    
3925                                    qPos.add(companyId);
3926    
3927                                    if (bindName) {
3928                                            qPos.add(name);
3929                                    }
3930    
3931                                    qPos.add(scope);
3932    
3933                                    if (bindPrimKey) {
3934                                            qPos.add(primKey);
3935                                    }
3936    
3937                                    qPos.add(roleId);
3938    
3939                                    count = (Long)q.uniqueResult();
3940    
3941                                    finderCache.putResult(finderPath, finderArgs, count);
3942                            }
3943                            catch (Exception e) {
3944                                    finderCache.removeResult(finderPath, finderArgs);
3945    
3946                                    throw processException(e);
3947                            }
3948                            finally {
3949                                    closeSession(session);
3950                            }
3951                    }
3952    
3953                    return count.intValue();
3954            }
3955    
3956            /**
3957             * Returns the number of resource permissions where companyId = &#63; and name = &#63; and scope = &#63; and primKey = &#63; and roleId = any &#63;.
3958             *
3959             * @param companyId the company ID
3960             * @param name the name
3961             * @param scope the scope
3962             * @param primKey the prim key
3963             * @param roleIds the role IDs
3964             * @return the number of matching resource permissions
3965             */
3966            @Override
3967            public int countByC_N_S_P_R(long companyId, String name, int scope,
3968                    String primKey, long[] roleIds) {
3969                    if (roleIds == null) {
3970                            roleIds = new long[0];
3971                    }
3972                    else if (roleIds.length > 1) {
3973                            roleIds = ArrayUtil.unique(roleIds);
3974    
3975                            Arrays.sort(roleIds);
3976                    }
3977    
3978                    Object[] finderArgs = new Object[] {
3979                                    companyId, name, scope, primKey, StringUtil.merge(roleIds)
3980                            };
3981    
3982                    Long count = (Long)finderCache.getResult(FINDER_PATH_WITH_PAGINATION_COUNT_BY_C_N_S_P_R,
3983                                    finderArgs, this);
3984    
3985                    if (count == null) {
3986                            StringBundler query = new StringBundler();
3987    
3988                            query.append(_SQL_COUNT_RESOURCEPERMISSION_WHERE);
3989    
3990                            query.append(_FINDER_COLUMN_C_N_S_P_R_COMPANYID_2);
3991    
3992                            boolean bindName = false;
3993    
3994                            if (name == null) {
3995                                    query.append(_FINDER_COLUMN_C_N_S_P_R_NAME_1);
3996                            }
3997                            else if (name.equals(StringPool.BLANK)) {
3998                                    query.append(_FINDER_COLUMN_C_N_S_P_R_NAME_3);
3999                            }
4000                            else {
4001                                    bindName = true;
4002    
4003                                    query.append(_FINDER_COLUMN_C_N_S_P_R_NAME_2);
4004                            }
4005    
4006                            query.append(_FINDER_COLUMN_C_N_S_P_R_SCOPE_2);
4007    
4008                            boolean bindPrimKey = false;
4009    
4010                            if (primKey == null) {
4011                                    query.append(_FINDER_COLUMN_C_N_S_P_R_PRIMKEY_1);
4012                            }
4013                            else if (primKey.equals(StringPool.BLANK)) {
4014                                    query.append(_FINDER_COLUMN_C_N_S_P_R_PRIMKEY_3);
4015                            }
4016                            else {
4017                                    bindPrimKey = true;
4018    
4019                                    query.append(_FINDER_COLUMN_C_N_S_P_R_PRIMKEY_2);
4020                            }
4021    
4022                            if (roleIds.length > 0) {
4023                                    query.append(StringPool.OPEN_PARENTHESIS);
4024    
4025                                    query.append(_FINDER_COLUMN_C_N_S_P_R_ROLEID_7);
4026    
4027                                    query.append(StringUtil.merge(roleIds));
4028    
4029                                    query.append(StringPool.CLOSE_PARENTHESIS);
4030    
4031                                    query.append(StringPool.CLOSE_PARENTHESIS);
4032                            }
4033    
4034                            query.setStringAt(removeConjunction(query.stringAt(query.index() -
4035                                                    1)), query.index() - 1);
4036    
4037                            String sql = query.toString();
4038    
4039                            Session session = null;
4040    
4041                            try {
4042                                    session = openSession();
4043    
4044                                    Query q = session.createQuery(sql);
4045    
4046                                    QueryPos qPos = QueryPos.getInstance(q);
4047    
4048                                    qPos.add(companyId);
4049    
4050                                    if (bindName) {
4051                                            qPos.add(name);
4052                                    }
4053    
4054                                    qPos.add(scope);
4055    
4056                                    if (bindPrimKey) {
4057                                            qPos.add(primKey);
4058                                    }
4059    
4060                                    count = (Long)q.uniqueResult();
4061    
4062                                    finderCache.putResult(FINDER_PATH_WITH_PAGINATION_COUNT_BY_C_N_S_P_R,
4063                                            finderArgs, count);
4064                            }
4065                            catch (Exception e) {
4066                                    finderCache.removeResult(FINDER_PATH_WITH_PAGINATION_COUNT_BY_C_N_S_P_R,
4067                                            finderArgs);
4068    
4069                                    throw processException(e);
4070                            }
4071                            finally {
4072                                    closeSession(session);
4073                            }
4074                    }
4075    
4076                    return count.intValue();
4077            }
4078    
4079            private static final String _FINDER_COLUMN_C_N_S_P_R_COMPANYID_2 = "resourcePermission.companyId = ? AND ";
4080            private static final String _FINDER_COLUMN_C_N_S_P_R_NAME_1 = "resourcePermission.name IS NULL AND ";
4081            private static final String _FINDER_COLUMN_C_N_S_P_R_NAME_2 = "resourcePermission.name = ? AND ";
4082            private static final String _FINDER_COLUMN_C_N_S_P_R_NAME_3 = "(resourcePermission.name IS NULL OR resourcePermission.name = '') AND ";
4083            private static final String _FINDER_COLUMN_C_N_S_P_R_SCOPE_2 = "resourcePermission.scope = ? AND ";
4084            private static final String _FINDER_COLUMN_C_N_S_P_R_PRIMKEY_1 = "resourcePermission.primKey IS NULL AND ";
4085            private static final String _FINDER_COLUMN_C_N_S_P_R_PRIMKEY_2 = "resourcePermission.primKey = ? AND ";
4086            private static final String _FINDER_COLUMN_C_N_S_P_R_PRIMKEY_3 = "(resourcePermission.primKey IS NULL OR resourcePermission.primKey = '') AND ";
4087            private static final String _FINDER_COLUMN_C_N_S_P_R_ROLEID_2 = "resourcePermission.roleId = ?";
4088            private static final String _FINDER_COLUMN_C_N_S_P_R_ROLEID_7 = "resourcePermission.roleId IN (";
4089            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_C_N_S_P_R_V =
4090                    new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
4091                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED,
4092                            ResourcePermissionImpl.class,
4093                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByC_N_S_P_R_V",
4094                            new String[] {
4095                                    Long.class.getName(), String.class.getName(),
4096                                    Integer.class.getName(), Long.class.getName(),
4097                                    Long.class.getName(), Boolean.class.getName(),
4098                                    
4099                            Integer.class.getName(), Integer.class.getName(),
4100                                    OrderByComparator.class.getName()
4101                            });
4102            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_N_S_P_R_V =
4103                    new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
4104                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED,
4105                            ResourcePermissionImpl.class,
4106                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByC_N_S_P_R_V",
4107                            new String[] {
4108                                    Long.class.getName(), String.class.getName(),
4109                                    Integer.class.getName(), Long.class.getName(),
4110                                    Long.class.getName(), Boolean.class.getName()
4111                            },
4112                            ResourcePermissionModelImpl.COMPANYID_COLUMN_BITMASK |
4113                            ResourcePermissionModelImpl.NAME_COLUMN_BITMASK |
4114                            ResourcePermissionModelImpl.SCOPE_COLUMN_BITMASK |
4115                            ResourcePermissionModelImpl.PRIMKEYID_COLUMN_BITMASK |
4116                            ResourcePermissionModelImpl.ROLEID_COLUMN_BITMASK |
4117                            ResourcePermissionModelImpl.VIEWACTIONID_COLUMN_BITMASK);
4118            public static final FinderPath FINDER_PATH_COUNT_BY_C_N_S_P_R_V = new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
4119                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED, Long.class,
4120                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByC_N_S_P_R_V",
4121                            new String[] {
4122                                    Long.class.getName(), String.class.getName(),
4123                                    Integer.class.getName(), Long.class.getName(),
4124                                    Long.class.getName(), Boolean.class.getName()
4125                            });
4126            public static final FinderPath FINDER_PATH_WITH_PAGINATION_COUNT_BY_C_N_S_P_R_V =
4127                    new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
4128                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED, Long.class,
4129                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "countByC_N_S_P_R_V",
4130                            new String[] {
4131                                    Long.class.getName(), String.class.getName(),
4132                                    Integer.class.getName(), Long.class.getName(),
4133                                    Long.class.getName(), Boolean.class.getName()
4134                            });
4135    
4136            /**
4137             * Returns all the resource permissions where companyId = &#63; and name = &#63; and scope = &#63; and primKeyId = &#63; and roleId = &#63; and viewActionId = &#63;.
4138             *
4139             * @param companyId the company ID
4140             * @param name the name
4141             * @param scope the scope
4142             * @param primKeyId the prim key ID
4143             * @param roleId the role ID
4144             * @param viewActionId the view action ID
4145             * @return the matching resource permissions
4146             */
4147            @Override
4148            public List<ResourcePermission> findByC_N_S_P_R_V(long companyId,
4149                    String name, int scope, long primKeyId, long roleId,
4150                    boolean viewActionId) {
4151                    return findByC_N_S_P_R_V(companyId, name, scope, primKeyId, roleId,
4152                            viewActionId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
4153            }
4154    
4155            /**
4156             * Returns a range of all the resource permissions where companyId = &#63; and name = &#63; and scope = &#63; and primKeyId = &#63; and roleId = &#63; and viewActionId = &#63;.
4157             *
4158             * <p>
4159             * 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 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 QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link 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.
4160             * </p>
4161             *
4162             * @param companyId the company ID
4163             * @param name the name
4164             * @param scope the scope
4165             * @param primKeyId the prim key ID
4166             * @param roleId the role ID
4167             * @param viewActionId the view action ID
4168             * @param start the lower bound of the range of resource permissions
4169             * @param end the upper bound of the range of resource permissions (not inclusive)
4170             * @return the range of matching resource permissions
4171             */
4172            @Override
4173            public List<ResourcePermission> findByC_N_S_P_R_V(long companyId,
4174                    String name, int scope, long primKeyId, long roleId,
4175                    boolean viewActionId, int start, int end) {
4176                    return findByC_N_S_P_R_V(companyId, name, scope, primKeyId, roleId,
4177                            viewActionId, start, end, null);
4178            }
4179    
4180            /**
4181             * Returns an ordered range of all the resource permissions where companyId = &#63; and name = &#63; and scope = &#63; and primKeyId = &#63; and roleId = &#63; and viewActionId = &#63;.
4182             *
4183             * <p>
4184             * 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 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 QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link 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.
4185             * </p>
4186             *
4187             * @param companyId the company ID
4188             * @param name the name
4189             * @param scope the scope
4190             * @param primKeyId the prim key ID
4191             * @param roleId the role ID
4192             * @param viewActionId the view action ID
4193             * @param start the lower bound of the range of resource permissions
4194             * @param end the upper bound of the range of resource permissions (not inclusive)
4195             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
4196             * @return the ordered range of matching resource permissions
4197             */
4198            @Override
4199            public List<ResourcePermission> findByC_N_S_P_R_V(long companyId,
4200                    String name, int scope, long primKeyId, long roleId,
4201                    boolean viewActionId, int start, int end,
4202                    OrderByComparator<ResourcePermission> orderByComparator) {
4203                    return findByC_N_S_P_R_V(companyId, name, scope, primKeyId, roleId,
4204                            viewActionId, start, end, orderByComparator, true);
4205            }
4206    
4207            /**
4208             * Returns an ordered range of all the resource permissions where companyId = &#63; and name = &#63; and scope = &#63; and primKeyId = &#63; and roleId = &#63; and viewActionId = &#63;.
4209             *
4210             * <p>
4211             * 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 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 QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link 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.
4212             * </p>
4213             *
4214             * @param companyId the company ID
4215             * @param name the name
4216             * @param scope the scope
4217             * @param primKeyId the prim key ID
4218             * @param roleId the role ID
4219             * @param viewActionId the view action ID
4220             * @param start the lower bound of the range of resource permissions
4221             * @param end the upper bound of the range of resource permissions (not inclusive)
4222             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
4223             * @param retrieveFromCache whether to retrieve from the finder cache
4224             * @return the ordered range of matching resource permissions
4225             */
4226            @Override
4227            public List<ResourcePermission> findByC_N_S_P_R_V(long companyId,
4228                    String name, int scope, long primKeyId, long roleId,
4229                    boolean viewActionId, int start, int end,
4230                    OrderByComparator<ResourcePermission> orderByComparator,
4231                    boolean retrieveFromCache) {
4232                    boolean pagination = true;
4233                    FinderPath finderPath = null;
4234                    Object[] finderArgs = null;
4235    
4236                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
4237                                    (orderByComparator == null)) {
4238                            pagination = false;
4239                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_N_S_P_R_V;
4240                            finderArgs = new Object[] {
4241                                            companyId, name, scope, primKeyId, roleId, viewActionId
4242                                    };
4243                    }
4244                    else {
4245                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_C_N_S_P_R_V;
4246                            finderArgs = new Object[] {
4247                                            companyId, name, scope, primKeyId, roleId, viewActionId,
4248                                            
4249                                            start, end, orderByComparator
4250                                    };
4251                    }
4252    
4253                    List<ResourcePermission> list = null;
4254    
4255                    if (retrieveFromCache) {
4256                            list = (List<ResourcePermission>)finderCache.getResult(finderPath,
4257                                            finderArgs, this);
4258    
4259                            if ((list != null) && !list.isEmpty()) {
4260                                    for (ResourcePermission resourcePermission : list) {
4261                                            if ((companyId != resourcePermission.getCompanyId()) ||
4262                                                            !Validator.equals(name, resourcePermission.getName()) ||
4263                                                            (scope != resourcePermission.getScope()) ||
4264                                                            (primKeyId != resourcePermission.getPrimKeyId()) ||
4265                                                            (roleId != resourcePermission.getRoleId()) ||
4266                                                            (viewActionId != resourcePermission.getViewActionId())) {
4267                                                    list = null;
4268    
4269                                                    break;
4270                                            }
4271                                    }
4272                            }
4273                    }
4274    
4275                    if (list == null) {
4276                            StringBundler query = null;
4277    
4278                            if (orderByComparator != null) {
4279                                    query = new StringBundler(8 +
4280                                                    (orderByComparator.getOrderByFields().length * 3));
4281                            }
4282                            else {
4283                                    query = new StringBundler(8);
4284                            }
4285    
4286                            query.append(_SQL_SELECT_RESOURCEPERMISSION_WHERE);
4287    
4288                            query.append(_FINDER_COLUMN_C_N_S_P_R_V_COMPANYID_2);
4289    
4290                            boolean bindName = false;
4291    
4292                            if (name == null) {
4293                                    query.append(_FINDER_COLUMN_C_N_S_P_R_V_NAME_1);
4294                            }
4295                            else if (name.equals(StringPool.BLANK)) {
4296                                    query.append(_FINDER_COLUMN_C_N_S_P_R_V_NAME_3);
4297                            }
4298                            else {
4299                                    bindName = true;
4300    
4301                                    query.append(_FINDER_COLUMN_C_N_S_P_R_V_NAME_2);
4302                            }
4303    
4304                            query.append(_FINDER_COLUMN_C_N_S_P_R_V_SCOPE_2);
4305    
4306                            query.append(_FINDER_COLUMN_C_N_S_P_R_V_PRIMKEYID_2);
4307    
4308                            query.append(_FINDER_COLUMN_C_N_S_P_R_V_ROLEID_2);
4309    
4310                            query.append(_FINDER_COLUMN_C_N_S_P_R_V_VIEWACTIONID_2);
4311    
4312                            if (orderByComparator != null) {
4313                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
4314                                            orderByComparator);
4315                            }
4316                            else
4317                             if (pagination) {
4318                                    query.append(ResourcePermissionModelImpl.ORDER_BY_JPQL);
4319                            }
4320    
4321                            String sql = query.toString();
4322    
4323                            Session session = null;
4324    
4325                            try {
4326                                    session = openSession();
4327    
4328                                    Query q = session.createQuery(sql);
4329    
4330                                    QueryPos qPos = QueryPos.getInstance(q);
4331    
4332                                    qPos.add(companyId);
4333    
4334                                    if (bindName) {
4335                                            qPos.add(name);
4336                                    }
4337    
4338                                    qPos.add(scope);
4339    
4340                                    qPos.add(primKeyId);
4341    
4342                                    qPos.add(roleId);
4343    
4344                                    qPos.add(viewActionId);
4345    
4346                                    if (!pagination) {
4347                                            list = (List<ResourcePermission>)QueryUtil.list(q,
4348                                                            getDialect(), start, end, false);
4349    
4350                                            Collections.sort(list);
4351    
4352                                            list = Collections.unmodifiableList(list);
4353                                    }
4354                                    else {
4355                                            list = (List<ResourcePermission>)QueryUtil.list(q,
4356                                                            getDialect(), start, end);
4357                                    }
4358    
4359                                    cacheResult(list);
4360    
4361                                    finderCache.putResult(finderPath, finderArgs, list);
4362                            }
4363                            catch (Exception e) {
4364                                    finderCache.removeResult(finderPath, finderArgs);
4365    
4366                                    throw processException(e);
4367                            }
4368                            finally {
4369                                    closeSession(session);
4370                            }
4371                    }
4372    
4373                    return list;
4374            }
4375    
4376            /**
4377             * Returns the first resource permission in the ordered set where companyId = &#63; and name = &#63; and scope = &#63; and primKeyId = &#63; and roleId = &#63; and viewActionId = &#63;.
4378             *
4379             * @param companyId the company ID
4380             * @param name the name
4381             * @param scope the scope
4382             * @param primKeyId the prim key ID
4383             * @param roleId the role ID
4384             * @param viewActionId the view action ID
4385             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
4386             * @return the first matching resource permission
4387             * @throws NoSuchResourcePermissionException if a matching resource permission could not be found
4388             */
4389            @Override
4390            public ResourcePermission findByC_N_S_P_R_V_First(long companyId,
4391                    String name, int scope, long primKeyId, long roleId,
4392                    boolean viewActionId,
4393                    OrderByComparator<ResourcePermission> orderByComparator)
4394                    throws NoSuchResourcePermissionException {
4395                    ResourcePermission resourcePermission = fetchByC_N_S_P_R_V_First(companyId,
4396                                    name, scope, primKeyId, roleId, viewActionId, orderByComparator);
4397    
4398                    if (resourcePermission != null) {
4399                            return resourcePermission;
4400                    }
4401    
4402                    StringBundler msg = new StringBundler(14);
4403    
4404                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
4405    
4406                    msg.append("companyId=");
4407                    msg.append(companyId);
4408    
4409                    msg.append(", name=");
4410                    msg.append(name);
4411    
4412                    msg.append(", scope=");
4413                    msg.append(scope);
4414    
4415                    msg.append(", primKeyId=");
4416                    msg.append(primKeyId);
4417    
4418                    msg.append(", roleId=");
4419                    msg.append(roleId);
4420    
4421                    msg.append(", viewActionId=");
4422                    msg.append(viewActionId);
4423    
4424                    msg.append(StringPool.CLOSE_CURLY_BRACE);
4425    
4426                    throw new NoSuchResourcePermissionException(msg.toString());
4427            }
4428    
4429            /**
4430             * Returns the first resource permission in the ordered set where companyId = &#63; and name = &#63; and scope = &#63; and primKeyId = &#63; and roleId = &#63; and viewActionId = &#63;.
4431             *
4432             * @param companyId the company ID
4433             * @param name the name
4434             * @param scope the scope
4435             * @param primKeyId the prim key ID
4436             * @param roleId the role ID
4437             * @param viewActionId the view action ID
4438             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
4439             * @return the first matching resource permission, or <code>null</code> if a matching resource permission could not be found
4440             */
4441            @Override
4442            public ResourcePermission fetchByC_N_S_P_R_V_First(long companyId,
4443                    String name, int scope, long primKeyId, long roleId,
4444                    boolean viewActionId,
4445                    OrderByComparator<ResourcePermission> orderByComparator) {
4446                    List<ResourcePermission> list = findByC_N_S_P_R_V(companyId, name,
4447                                    scope, primKeyId, roleId, viewActionId, 0, 1, orderByComparator);
4448    
4449                    if (!list.isEmpty()) {
4450                            return list.get(0);
4451                    }
4452    
4453                    return null;
4454            }
4455    
4456            /**
4457             * Returns the last resource permission in the ordered set where companyId = &#63; and name = &#63; and scope = &#63; and primKeyId = &#63; and roleId = &#63; and viewActionId = &#63;.
4458             *
4459             * @param companyId the company ID
4460             * @param name the name
4461             * @param scope the scope
4462             * @param primKeyId the prim key ID
4463             * @param roleId the role ID
4464             * @param viewActionId the view action ID
4465             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
4466             * @return the last matching resource permission
4467             * @throws NoSuchResourcePermissionException if a matching resource permission could not be found
4468             */
4469            @Override
4470            public ResourcePermission findByC_N_S_P_R_V_Last(long companyId,
4471                    String name, int scope, long primKeyId, long roleId,
4472                    boolean viewActionId,
4473                    OrderByComparator<ResourcePermission> orderByComparator)
4474                    throws NoSuchResourcePermissionException {
4475                    ResourcePermission resourcePermission = fetchByC_N_S_P_R_V_Last(companyId,
4476                                    name, scope, primKeyId, roleId, viewActionId, orderByComparator);
4477    
4478                    if (resourcePermission != null) {
4479                            return resourcePermission;
4480                    }
4481    
4482                    StringBundler msg = new StringBundler(14);
4483    
4484                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
4485    
4486                    msg.append("companyId=");
4487                    msg.append(companyId);
4488    
4489                    msg.append(", name=");
4490                    msg.append(name);
4491    
4492                    msg.append(", scope=");
4493                    msg.append(scope);
4494    
4495                    msg.append(", primKeyId=");
4496                    msg.append(primKeyId);
4497    
4498                    msg.append(", roleId=");
4499                    msg.append(roleId);
4500    
4501                    msg.append(", viewActionId=");
4502                    msg.append(viewActionId);
4503    
4504                    msg.append(StringPool.CLOSE_CURLY_BRACE);
4505    
4506                    throw new NoSuchResourcePermissionException(msg.toString());
4507            }
4508    
4509            /**
4510             * Returns the last resource permission in the ordered set where companyId = &#63; and name = &#63; and scope = &#63; and primKeyId = &#63; and roleId = &#63; and viewActionId = &#63;.
4511             *
4512             * @param companyId the company ID
4513             * @param name the name
4514             * @param scope the scope
4515             * @param primKeyId the prim key ID
4516             * @param roleId the role ID
4517             * @param viewActionId the view action ID
4518             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
4519             * @return the last matching resource permission, or <code>null</code> if a matching resource permission could not be found
4520             */
4521            @Override
4522            public ResourcePermission fetchByC_N_S_P_R_V_Last(long companyId,
4523                    String name, int scope, long primKeyId, long roleId,
4524                    boolean viewActionId,
4525                    OrderByComparator<ResourcePermission> orderByComparator) {
4526                    int count = countByC_N_S_P_R_V(companyId, name, scope, primKeyId,
4527                                    roleId, viewActionId);
4528    
4529                    if (count == 0) {
4530                            return null;
4531                    }
4532    
4533                    List<ResourcePermission> list = findByC_N_S_P_R_V(companyId, name,
4534                                    scope, primKeyId, roleId, viewActionId, count - 1, count,
4535                                    orderByComparator);
4536    
4537                    if (!list.isEmpty()) {
4538                            return list.get(0);
4539                    }
4540    
4541                    return null;
4542            }
4543    
4544            /**
4545             * 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 primKeyId = &#63; and roleId = &#63; and viewActionId = &#63;.
4546             *
4547             * @param resourcePermissionId the primary key of the current resource permission
4548             * @param companyId the company ID
4549             * @param name the name
4550             * @param scope the scope
4551             * @param primKeyId the prim key ID
4552             * @param roleId the role ID
4553             * @param viewActionId the view action ID
4554             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
4555             * @return the previous, current, and next resource permission
4556             * @throws NoSuchResourcePermissionException if a resource permission with the primary key could not be found
4557             */
4558            @Override
4559            public ResourcePermission[] findByC_N_S_P_R_V_PrevAndNext(
4560                    long resourcePermissionId, long companyId, String name, int scope,
4561                    long primKeyId, long roleId, boolean viewActionId,
4562                    OrderByComparator<ResourcePermission> orderByComparator)
4563                    throws NoSuchResourcePermissionException {
4564                    ResourcePermission resourcePermission = findByPrimaryKey(resourcePermissionId);
4565    
4566                    Session session = null;
4567    
4568                    try {
4569                            session = openSession();
4570    
4571                            ResourcePermission[] array = new ResourcePermissionImpl[3];
4572    
4573                            array[0] = getByC_N_S_P_R_V_PrevAndNext(session,
4574                                            resourcePermission, companyId, name, scope, primKeyId,
4575                                            roleId, viewActionId, orderByComparator, true);
4576    
4577                            array[1] = resourcePermission;
4578    
4579                            array[2] = getByC_N_S_P_R_V_PrevAndNext(session,
4580                                            resourcePermission, companyId, name, scope, primKeyId,
4581                                            roleId, viewActionId, orderByComparator, false);
4582    
4583                            return array;
4584                    }
4585                    catch (Exception e) {
4586                            throw processException(e);
4587                    }
4588                    finally {
4589                            closeSession(session);
4590                    }
4591            }
4592    
4593            protected ResourcePermission getByC_N_S_P_R_V_PrevAndNext(Session session,
4594                    ResourcePermission resourcePermission, long companyId, String name,
4595                    int scope, long primKeyId, long roleId, boolean viewActionId,
4596                    OrderByComparator<ResourcePermission> orderByComparator,
4597                    boolean previous) {
4598                    StringBundler query = null;
4599    
4600                    if (orderByComparator != null) {
4601                            query = new StringBundler(6 +
4602                                            (orderByComparator.getOrderByFields().length * 6));
4603                    }
4604                    else {
4605                            query = new StringBundler(3);
4606                    }
4607    
4608                    query.append(_SQL_SELECT_RESOURCEPERMISSION_WHERE);
4609    
4610                    query.append(_FINDER_COLUMN_C_N_S_P_R_V_COMPANYID_2);
4611    
4612                    boolean bindName = false;
4613    
4614                    if (name == null) {
4615                            query.append(_FINDER_COLUMN_C_N_S_P_R_V_NAME_1);
4616                    }
4617                    else if (name.equals(StringPool.BLANK)) {
4618                            query.append(_FINDER_COLUMN_C_N_S_P_R_V_NAME_3);
4619                    }
4620                    else {
4621                            bindName = true;
4622    
4623                            query.append(_FINDER_COLUMN_C_N_S_P_R_V_NAME_2);
4624                    }
4625    
4626                    query.append(_FINDER_COLUMN_C_N_S_P_R_V_SCOPE_2);
4627    
4628                    query.append(_FINDER_COLUMN_C_N_S_P_R_V_PRIMKEYID_2);
4629    
4630                    query.append(_FINDER_COLUMN_C_N_S_P_R_V_ROLEID_2);
4631    
4632                    query.append(_FINDER_COLUMN_C_N_S_P_R_V_VIEWACTIONID_2);
4633    
4634                    if (orderByComparator != null) {
4635                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
4636    
4637                            if (orderByConditionFields.length > 0) {
4638                                    query.append(WHERE_AND);
4639                            }
4640    
4641                            for (int i = 0; i < orderByConditionFields.length; i++) {
4642                                    query.append(_ORDER_BY_ENTITY_ALIAS);
4643                                    query.append(orderByConditionFields[i]);
4644    
4645                                    if ((i + 1) < orderByConditionFields.length) {
4646                                            if (orderByComparator.isAscending() ^ previous) {
4647                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
4648                                            }
4649                                            else {
4650                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
4651                                            }
4652                                    }
4653                                    else {
4654                                            if (orderByComparator.isAscending() ^ previous) {
4655                                                    query.append(WHERE_GREATER_THAN);
4656                                            }
4657                                            else {
4658                                                    query.append(WHERE_LESSER_THAN);
4659                                            }
4660                                    }
4661                            }
4662    
4663                            query.append(ORDER_BY_CLAUSE);
4664    
4665                            String[] orderByFields = orderByComparator.getOrderByFields();
4666    
4667                            for (int i = 0; i < orderByFields.length; i++) {
4668                                    query.append(_ORDER_BY_ENTITY_ALIAS);
4669                                    query.append(orderByFields[i]);
4670    
4671                                    if ((i + 1) < orderByFields.length) {
4672                                            if (orderByComparator.isAscending() ^ previous) {
4673                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
4674                                            }
4675                                            else {
4676                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
4677                                            }
4678                                    }
4679                                    else {
4680                                            if (orderByComparator.isAscending() ^ previous) {
4681                                                    query.append(ORDER_BY_ASC);
4682                                            }
4683                                            else {
4684                                                    query.append(ORDER_BY_DESC);
4685                                            }
4686                                    }
4687                            }
4688                    }
4689                    else {
4690                            query.append(ResourcePermissionModelImpl.ORDER_BY_JPQL);
4691                    }
4692    
4693                    String sql = query.toString();
4694    
4695                    Query q = session.createQuery(sql);
4696    
4697                    q.setFirstResult(0);
4698                    q.setMaxResults(2);
4699    
4700                    QueryPos qPos = QueryPos.getInstance(q);
4701    
4702                    qPos.add(companyId);
4703    
4704                    if (bindName) {
4705                            qPos.add(name);
4706                    }
4707    
4708                    qPos.add(scope);
4709    
4710                    qPos.add(primKeyId);
4711    
4712                    qPos.add(roleId);
4713    
4714                    qPos.add(viewActionId);
4715    
4716                    if (orderByComparator != null) {
4717                            Object[] values = orderByComparator.getOrderByConditionValues(resourcePermission);
4718    
4719                            for (Object value : values) {
4720                                    qPos.add(value);
4721                            }
4722                    }
4723    
4724                    List<ResourcePermission> list = q.list();
4725    
4726                    if (list.size() == 2) {
4727                            return list.get(1);
4728                    }
4729                    else {
4730                            return null;
4731                    }
4732            }
4733    
4734            /**
4735             * Returns all the resource permissions where companyId = &#63; and name = &#63; and scope = &#63; and primKeyId = &#63; and roleId = any &#63; and viewActionId = &#63;.
4736             *
4737             * <p>
4738             * 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 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 QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link 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.
4739             * </p>
4740             *
4741             * @param companyId the company ID
4742             * @param name the name
4743             * @param scope the scope
4744             * @param primKeyId the prim key ID
4745             * @param roleIds the role IDs
4746             * @param viewActionId the view action ID
4747             * @return the matching resource permissions
4748             */
4749            @Override
4750            public List<ResourcePermission> findByC_N_S_P_R_V(long companyId,
4751                    String name, int scope, long primKeyId, long[] roleIds,
4752                    boolean viewActionId) {
4753                    return findByC_N_S_P_R_V(companyId, name, scope, primKeyId, roleIds,
4754                            viewActionId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
4755            }
4756    
4757            /**
4758             * Returns a range of all the resource permissions where companyId = &#63; and name = &#63; and scope = &#63; and primKeyId = &#63; and roleId = any &#63; and viewActionId = &#63;.
4759             *
4760             * <p>
4761             * 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 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 QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link 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.
4762             * </p>
4763             *
4764             * @param companyId the company ID
4765             * @param name the name
4766             * @param scope the scope
4767             * @param primKeyId the prim key ID
4768             * @param roleIds the role IDs
4769             * @param viewActionId the view action ID
4770             * @param start the lower bound of the range of resource permissions
4771             * @param end the upper bound of the range of resource permissions (not inclusive)
4772             * @return the range of matching resource permissions
4773             */
4774            @Override
4775            public List<ResourcePermission> findByC_N_S_P_R_V(long companyId,
4776                    String name, int scope, long primKeyId, long[] roleIds,
4777                    boolean viewActionId, int start, int end) {
4778                    return findByC_N_S_P_R_V(companyId, name, scope, primKeyId, roleIds,
4779                            viewActionId, start, end, null);
4780            }
4781    
4782            /**
4783             * Returns an ordered range of all the resource permissions where companyId = &#63; and name = &#63; and scope = &#63; and primKeyId = &#63; and roleId = any &#63; and viewActionId = &#63;.
4784             *
4785             * <p>
4786             * 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 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 QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link 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.
4787             * </p>
4788             *
4789             * @param companyId the company ID
4790             * @param name the name
4791             * @param scope the scope
4792             * @param primKeyId the prim key ID
4793             * @param roleIds the role IDs
4794             * @param viewActionId the view action ID
4795             * @param start the lower bound of the range of resource permissions
4796             * @param end the upper bound of the range of resource permissions (not inclusive)
4797             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
4798             * @return the ordered range of matching resource permissions
4799             */
4800            @Override
4801            public List<ResourcePermission> findByC_N_S_P_R_V(long companyId,
4802                    String name, int scope, long primKeyId, long[] roleIds,
4803                    boolean viewActionId, int start, int end,
4804                    OrderByComparator<ResourcePermission> orderByComparator) {
4805                    return findByC_N_S_P_R_V(companyId, name, scope, primKeyId, roleIds,
4806                            viewActionId, start, end, orderByComparator, true);
4807            }
4808    
4809            /**
4810             * Returns an ordered range of all the resource permissions where companyId = &#63; and name = &#63; and scope = &#63; and primKeyId = &#63; and roleId = &#63; and viewActionId = &#63;, optionally using the finder cache.
4811             *
4812             * <p>
4813             * 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 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 QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link 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.
4814             * </p>
4815             *
4816             * @param companyId the company ID
4817             * @param name the name
4818             * @param scope the scope
4819             * @param primKeyId the prim key ID
4820             * @param roleId the role ID
4821             * @param viewActionId the view action ID
4822             * @param start the lower bound of the range of resource permissions
4823             * @param end the upper bound of the range of resource permissions (not inclusive)
4824             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
4825             * @param retrieveFromCache whether to retrieve from the finder cache
4826             * @return the ordered range of matching resource permissions
4827             */
4828            @Override
4829            public List<ResourcePermission> findByC_N_S_P_R_V(long companyId,
4830                    String name, int scope, long primKeyId, long[] roleIds,
4831                    boolean viewActionId, int start, int end,
4832                    OrderByComparator<ResourcePermission> orderByComparator,
4833                    boolean retrieveFromCache) {
4834                    if (roleIds == null) {
4835                            roleIds = new long[0];
4836                    }
4837                    else if (roleIds.length > 1) {
4838                            roleIds = ArrayUtil.unique(roleIds);
4839    
4840                            Arrays.sort(roleIds);
4841                    }
4842    
4843                    if (roleIds.length == 1) {
4844                            return findByC_N_S_P_R_V(companyId, name, scope, primKeyId,
4845                                    roleIds[0], viewActionId, start, end, orderByComparator);
4846                    }
4847    
4848                    boolean pagination = true;
4849                    Object[] finderArgs = null;
4850    
4851                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
4852                                    (orderByComparator == null)) {
4853                            pagination = false;
4854                            finderArgs = new Object[] {
4855                                            companyId, name, scope, primKeyId, StringUtil.merge(roleIds),
4856                                            viewActionId
4857                                    };
4858                    }
4859                    else {
4860                            finderArgs = new Object[] {
4861                                            companyId, name, scope, primKeyId, StringUtil.merge(roleIds),
4862                                            viewActionId,
4863                                            
4864                                            start, end, orderByComparator
4865                                    };
4866                    }
4867    
4868                    List<ResourcePermission> list = null;
4869    
4870                    if (retrieveFromCache) {
4871                            list = (List<ResourcePermission>)finderCache.getResult(FINDER_PATH_WITH_PAGINATION_FIND_BY_C_N_S_P_R_V,
4872                                            finderArgs, this);
4873    
4874                            if ((list != null) && !list.isEmpty()) {
4875                                    for (ResourcePermission resourcePermission : list) {
4876                                            if ((companyId != resourcePermission.getCompanyId()) ||
4877                                                            !Validator.equals(name, resourcePermission.getName()) ||
4878                                                            (scope != resourcePermission.getScope()) ||
4879                                                            (primKeyId != resourcePermission.getPrimKeyId()) ||
4880                                                            !ArrayUtil.contains(roleIds,
4881                                                                    resourcePermission.getRoleId()) ||
4882                                                            (viewActionId != resourcePermission.getViewActionId())) {
4883                                                    list = null;
4884    
4885                                                    break;
4886                                            }
4887                                    }
4888                            }
4889                    }
4890    
4891                    if (list == null) {
4892                            StringBundler query = new StringBundler();
4893    
4894                            query.append(_SQL_SELECT_RESOURCEPERMISSION_WHERE);
4895    
4896                            query.append(_FINDER_COLUMN_C_N_S_P_R_V_COMPANYID_2);
4897    
4898                            boolean bindName = false;
4899    
4900                            if (name == null) {
4901                                    query.append(_FINDER_COLUMN_C_N_S_P_R_V_NAME_1);
4902                            }
4903                            else if (name.equals(StringPool.BLANK)) {
4904                                    query.append(_FINDER_COLUMN_C_N_S_P_R_V_NAME_3);
4905                            }
4906                            else {
4907                                    bindName = true;
4908    
4909                                    query.append(_FINDER_COLUMN_C_N_S_P_R_V_NAME_2);
4910                            }
4911    
4912                            query.append(_FINDER_COLUMN_C_N_S_P_R_V_SCOPE_2);
4913    
4914                            query.append(_FINDER_COLUMN_C_N_S_P_R_V_PRIMKEYID_2);
4915    
4916                            if (roleIds.length > 0) {
4917                                    query.append(StringPool.OPEN_PARENTHESIS);
4918    
4919                                    query.append(_FINDER_COLUMN_C_N_S_P_R_V_ROLEID_7);
4920    
4921                                    query.append(StringUtil.merge(roleIds));
4922    
4923                                    query.append(StringPool.CLOSE_PARENTHESIS);
4924    
4925                                    query.append(StringPool.CLOSE_PARENTHESIS);
4926    
4927                                    query.append(WHERE_AND);
4928                            }
4929    
4930                            query.append(_FINDER_COLUMN_C_N_S_P_R_V_VIEWACTIONID_2);
4931    
4932                            query.setStringAt(removeConjunction(query.stringAt(query.index() -
4933                                                    1)), query.index() - 1);
4934    
4935                            if (orderByComparator != null) {
4936                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
4937                                            orderByComparator);
4938                            }
4939                            else
4940                             if (pagination) {
4941                                    query.append(ResourcePermissionModelImpl.ORDER_BY_JPQL);
4942                            }
4943    
4944                            String sql = query.toString();
4945    
4946                            Session session = null;
4947    
4948                            try {
4949                                    session = openSession();
4950    
4951                                    Query q = session.createQuery(sql);
4952    
4953                                    QueryPos qPos = QueryPos.getInstance(q);
4954    
4955                                    qPos.add(companyId);
4956    
4957                                    if (bindName) {
4958                                            qPos.add(name);
4959                                    }
4960    
4961                                    qPos.add(scope);
4962    
4963                                    qPos.add(primKeyId);
4964    
4965                                    qPos.add(viewActionId);
4966    
4967                                    if (!pagination) {
4968                                            list = (List<ResourcePermission>)QueryUtil.list(q,
4969                                                            getDialect(), start, end, false);
4970    
4971                                            Collections.sort(list);
4972    
4973                                            list = Collections.unmodifiableList(list);
4974                                    }
4975                                    else {
4976                                            list = (List<ResourcePermission>)QueryUtil.list(q,
4977                                                            getDialect(), start, end);
4978                                    }
4979    
4980                                    cacheResult(list);
4981    
4982                                    finderCache.putResult(FINDER_PATH_WITH_PAGINATION_FIND_BY_C_N_S_P_R_V,
4983                                            finderArgs, list);
4984                            }
4985                            catch (Exception e) {
4986                                    finderCache.removeResult(FINDER_PATH_WITH_PAGINATION_FIND_BY_C_N_S_P_R_V,
4987                                            finderArgs);
4988    
4989                                    throw processException(e);
4990                            }
4991                            finally {
4992                                    closeSession(session);
4993                            }
4994                    }
4995    
4996                    return list;
4997            }
4998    
4999            /**
5000             * Removes all the resource permissions where companyId = &#63; and name = &#63; and scope = &#63; and primKeyId = &#63; and roleId = &#63; and viewActionId = &#63; from the database.
5001             *
5002             * @param companyId the company ID
5003             * @param name the name
5004             * @param scope the scope
5005             * @param primKeyId the prim key ID
5006             * @param roleId the role ID
5007             * @param viewActionId the view action ID
5008             */
5009            @Override
5010            public void removeByC_N_S_P_R_V(long companyId, String name, int scope,
5011                    long primKeyId, long roleId, boolean viewActionId) {
5012                    for (ResourcePermission resourcePermission : findByC_N_S_P_R_V(
5013                                    companyId, name, scope, primKeyId, roleId, viewActionId,
5014                                    QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
5015                            remove(resourcePermission);
5016                    }
5017            }
5018    
5019            /**
5020             * Returns the number of resource permissions where companyId = &#63; and name = &#63; and scope = &#63; and primKeyId = &#63; and roleId = &#63; and viewActionId = &#63;.
5021             *
5022             * @param companyId the company ID
5023             * @param name the name
5024             * @param scope the scope
5025             * @param primKeyId the prim key ID
5026             * @param roleId the role ID
5027             * @param viewActionId the view action ID
5028             * @return the number of matching resource permissions
5029             */
5030            @Override
5031            public int countByC_N_S_P_R_V(long companyId, String name, int scope,
5032                    long primKeyId, long roleId, boolean viewActionId) {
5033                    FinderPath finderPath = FINDER_PATH_COUNT_BY_C_N_S_P_R_V;
5034    
5035                    Object[] finderArgs = new Object[] {
5036                                    companyId, name, scope, primKeyId, roleId, viewActionId
5037                            };
5038    
5039                    Long count = (Long)finderCache.getResult(finderPath, finderArgs, this);
5040    
5041                    if (count == null) {
5042                            StringBundler query = new StringBundler(7);
5043    
5044                            query.append(_SQL_COUNT_RESOURCEPERMISSION_WHERE);
5045    
5046                            query.append(_FINDER_COLUMN_C_N_S_P_R_V_COMPANYID_2);
5047    
5048                            boolean bindName = false;
5049    
5050                            if (name == null) {
5051                                    query.append(_FINDER_COLUMN_C_N_S_P_R_V_NAME_1);
5052                            }
5053                            else if (name.equals(StringPool.BLANK)) {
5054                                    query.append(_FINDER_COLUMN_C_N_S_P_R_V_NAME_3);
5055                            }
5056                            else {
5057                                    bindName = true;
5058    
5059                                    query.append(_FINDER_COLUMN_C_N_S_P_R_V_NAME_2);
5060                            }
5061    
5062                            query.append(_FINDER_COLUMN_C_N_S_P_R_V_SCOPE_2);
5063    
5064                            query.append(_FINDER_COLUMN_C_N_S_P_R_V_PRIMKEYID_2);
5065    
5066                            query.append(_FINDER_COLUMN_C_N_S_P_R_V_ROLEID_2);
5067    
5068                            query.append(_FINDER_COLUMN_C_N_S_P_R_V_VIEWACTIONID_2);
5069    
5070                            String sql = query.toString();
5071    
5072                            Session session = null;
5073    
5074                            try {
5075                                    session = openSession();
5076    
5077                                    Query q = session.createQuery(sql);
5078    
5079                                    QueryPos qPos = QueryPos.getInstance(q);
5080    
5081                                    qPos.add(companyId);
5082    
5083                                    if (bindName) {
5084                                            qPos.add(name);
5085                                    }
5086    
5087                                    qPos.add(scope);
5088    
5089                                    qPos.add(primKeyId);
5090    
5091                                    qPos.add(roleId);
5092    
5093                                    qPos.add(viewActionId);
5094    
5095                                    count = (Long)q.uniqueResult();
5096    
5097                                    finderCache.putResult(finderPath, finderArgs, count);
5098                            }
5099                            catch (Exception e) {
5100                                    finderCache.removeResult(finderPath, finderArgs);
5101    
5102                                    throw processException(e);
5103                            }
5104                            finally {
5105                                    closeSession(session);
5106                            }
5107                    }
5108    
5109                    return count.intValue();
5110            }
5111    
5112            /**
5113             * Returns the number of resource permissions where companyId = &#63; and name = &#63; and scope = &#63; and primKeyId = &#63; and roleId = any &#63; and viewActionId = &#63;.
5114             *
5115             * @param companyId the company ID
5116             * @param name the name
5117             * @param scope the scope
5118             * @param primKeyId the prim key ID
5119             * @param roleIds the role IDs
5120             * @param viewActionId the view action ID
5121             * @return the number of matching resource permissions
5122             */
5123            @Override
5124            public int countByC_N_S_P_R_V(long companyId, String name, int scope,
5125                    long primKeyId, long[] roleIds, boolean viewActionId) {
5126                    if (roleIds == null) {
5127                            roleIds = new long[0];
5128                    }
5129                    else if (roleIds.length > 1) {
5130                            roleIds = ArrayUtil.unique(roleIds);
5131    
5132                            Arrays.sort(roleIds);
5133                    }
5134    
5135                    Object[] finderArgs = new Object[] {
5136                                    companyId, name, scope, primKeyId, StringUtil.merge(roleIds),
5137                                    viewActionId
5138                            };
5139    
5140                    Long count = (Long)finderCache.getResult(FINDER_PATH_WITH_PAGINATION_COUNT_BY_C_N_S_P_R_V,
5141                                    finderArgs, this);
5142    
5143                    if (count == null) {
5144                            StringBundler query = new StringBundler();
5145    
5146                            query.append(_SQL_COUNT_RESOURCEPERMISSION_WHERE);
5147    
5148                            query.append(_FINDER_COLUMN_C_N_S_P_R_V_COMPANYID_2);
5149    
5150                            boolean bindName = false;
5151    
5152                            if (name == null) {
5153                                    query.append(_FINDER_COLUMN_C_N_S_P_R_V_NAME_1);
5154                            }
5155                            else if (name.equals(StringPool.BLANK)) {
5156                                    query.append(_FINDER_COLUMN_C_N_S_P_R_V_NAME_3);
5157                            }
5158                            else {
5159                                    bindName = true;
5160    
5161                                    query.append(_FINDER_COLUMN_C_N_S_P_R_V_NAME_2);
5162                            }
5163    
5164                            query.append(_FINDER_COLUMN_C_N_S_P_R_V_SCOPE_2);
5165    
5166                            query.append(_FINDER_COLUMN_C_N_S_P_R_V_PRIMKEYID_2);
5167    
5168                            if (roleIds.length > 0) {
5169                                    query.append(StringPool.OPEN_PARENTHESIS);
5170    
5171                                    query.append(_FINDER_COLUMN_C_N_S_P_R_V_ROLEID_7);
5172    
5173                                    query.append(StringUtil.merge(roleIds));
5174    
5175                                    query.append(StringPool.CLOSE_PARENTHESIS);
5176    
5177                                    query.append(StringPool.CLOSE_PARENTHESIS);
5178    
5179                                    query.append(WHERE_AND);
5180                            }
5181    
5182                            query.append(_FINDER_COLUMN_C_N_S_P_R_V_VIEWACTIONID_2);
5183    
5184                            query.setStringAt(removeConjunction(query.stringAt(query.index() -
5185                                                    1)), query.index() - 1);
5186    
5187                            String sql = query.toString();
5188    
5189                            Session session = null;
5190    
5191                            try {
5192                                    session = openSession();
5193    
5194                                    Query q = session.createQuery(sql);
5195    
5196                                    QueryPos qPos = QueryPos.getInstance(q);
5197    
5198                                    qPos.add(companyId);
5199    
5200                                    if (bindName) {
5201                                            qPos.add(name);
5202                                    }
5203    
5204                                    qPos.add(scope);
5205    
5206                                    qPos.add(primKeyId);
5207    
5208                                    qPos.add(viewActionId);
5209    
5210                                    count = (Long)q.uniqueResult();
5211    
5212                                    finderCache.putResult(FINDER_PATH_WITH_PAGINATION_COUNT_BY_C_N_S_P_R_V,
5213                                            finderArgs, count);
5214                            }
5215                            catch (Exception e) {
5216                                    finderCache.removeResult(FINDER_PATH_WITH_PAGINATION_COUNT_BY_C_N_S_P_R_V,
5217                                            finderArgs);
5218    
5219                                    throw processException(e);
5220                            }
5221                            finally {
5222                                    closeSession(session);
5223                            }
5224                    }
5225    
5226                    return count.intValue();
5227            }
5228    
5229            private static final String _FINDER_COLUMN_C_N_S_P_R_V_COMPANYID_2 = "resourcePermission.companyId = ? AND ";
5230            private static final String _FINDER_COLUMN_C_N_S_P_R_V_NAME_1 = "resourcePermission.name IS NULL AND ";
5231            private static final String _FINDER_COLUMN_C_N_S_P_R_V_NAME_2 = "resourcePermission.name = ? AND ";
5232            private static final String _FINDER_COLUMN_C_N_S_P_R_V_NAME_3 = "(resourcePermission.name IS NULL OR resourcePermission.name = '') AND ";
5233            private static final String _FINDER_COLUMN_C_N_S_P_R_V_SCOPE_2 = "resourcePermission.scope = ? AND ";
5234            private static final String _FINDER_COLUMN_C_N_S_P_R_V_PRIMKEYID_2 = "resourcePermission.primKeyId = ? AND ";
5235            private static final String _FINDER_COLUMN_C_N_S_P_R_V_ROLEID_2 = "resourcePermission.roleId = ? AND ";
5236            private static final String _FINDER_COLUMN_C_N_S_P_R_V_ROLEID_7 = "resourcePermission.roleId IN (";
5237            private static final String _FINDER_COLUMN_C_N_S_P_R_V_VIEWACTIONID_2 = "resourcePermission.viewActionId = ?";
5238    
5239            public ResourcePermissionPersistenceImpl() {
5240                    setModelClass(ResourcePermission.class);
5241            }
5242    
5243            /**
5244             * Caches the resource permission in the entity cache if it is enabled.
5245             *
5246             * @param resourcePermission the resource permission
5247             */
5248            @Override
5249            public void cacheResult(ResourcePermission resourcePermission) {
5250                    entityCache.putResult(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
5251                            ResourcePermissionImpl.class, resourcePermission.getPrimaryKey(),
5252                            resourcePermission);
5253    
5254                    finderCache.putResult(FINDER_PATH_FETCH_BY_C_N_S_P_R,
5255                            new Object[] {
5256                                    resourcePermission.getCompanyId(), resourcePermission.getName(),
5257                                    resourcePermission.getScope(), resourcePermission.getPrimKey(),
5258                                    resourcePermission.getRoleId()
5259                            }, resourcePermission);
5260    
5261                    resourcePermission.resetOriginalValues();
5262            }
5263    
5264            /**
5265             * Caches the resource permissions in the entity cache if it is enabled.
5266             *
5267             * @param resourcePermissions the resource permissions
5268             */
5269            @Override
5270            public void cacheResult(List<ResourcePermission> resourcePermissions) {
5271                    for (ResourcePermission resourcePermission : resourcePermissions) {
5272                            if (entityCache.getResult(
5273                                                    ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
5274                                                    ResourcePermissionImpl.class,
5275                                                    resourcePermission.getPrimaryKey()) == null) {
5276                                    cacheResult(resourcePermission);
5277                            }
5278                            else {
5279                                    resourcePermission.resetOriginalValues();
5280                            }
5281                    }
5282            }
5283    
5284            /**
5285             * Clears the cache for all resource permissions.
5286             *
5287             * <p>
5288             * The {@link EntityCache} and {@link FinderCache} are both cleared by this method.
5289             * </p>
5290             */
5291            @Override
5292            public void clearCache() {
5293                    entityCache.clearCache(ResourcePermissionImpl.class);
5294    
5295                    finderCache.clearCache(FINDER_CLASS_NAME_ENTITY);
5296                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
5297                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
5298            }
5299    
5300            /**
5301             * Clears the cache for the resource permission.
5302             *
5303             * <p>
5304             * The {@link EntityCache} and {@link FinderCache} are both cleared by this method.
5305             * </p>
5306             */
5307            @Override
5308            public void clearCache(ResourcePermission resourcePermission) {
5309                    entityCache.removeResult(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
5310                            ResourcePermissionImpl.class, resourcePermission.getPrimaryKey());
5311    
5312                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
5313                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
5314    
5315                    clearUniqueFindersCache((ResourcePermissionModelImpl)resourcePermission);
5316            }
5317    
5318            @Override
5319            public void clearCache(List<ResourcePermission> resourcePermissions) {
5320                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
5321                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
5322    
5323                    for (ResourcePermission resourcePermission : resourcePermissions) {
5324                            entityCache.removeResult(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
5325                                    ResourcePermissionImpl.class, resourcePermission.getPrimaryKey());
5326    
5327                            clearUniqueFindersCache((ResourcePermissionModelImpl)resourcePermission);
5328                    }
5329            }
5330    
5331            protected void cacheUniqueFindersCache(
5332                    ResourcePermissionModelImpl resourcePermissionModelImpl, boolean isNew) {
5333                    if (isNew) {
5334                            Object[] args = new Object[] {
5335                                            resourcePermissionModelImpl.getCompanyId(),
5336                                            resourcePermissionModelImpl.getName(),
5337                                            resourcePermissionModelImpl.getScope(),
5338                                            resourcePermissionModelImpl.getPrimKey(),
5339                                            resourcePermissionModelImpl.getRoleId()
5340                                    };
5341    
5342                            finderCache.putResult(FINDER_PATH_COUNT_BY_C_N_S_P_R, args,
5343                                    Long.valueOf(1));
5344                            finderCache.putResult(FINDER_PATH_FETCH_BY_C_N_S_P_R, args,
5345                                    resourcePermissionModelImpl);
5346                    }
5347                    else {
5348                            if ((resourcePermissionModelImpl.getColumnBitmask() &
5349                                            FINDER_PATH_FETCH_BY_C_N_S_P_R.getColumnBitmask()) != 0) {
5350                                    Object[] args = new Object[] {
5351                                                    resourcePermissionModelImpl.getCompanyId(),
5352                                                    resourcePermissionModelImpl.getName(),
5353                                                    resourcePermissionModelImpl.getScope(),
5354                                                    resourcePermissionModelImpl.getPrimKey(),
5355                                                    resourcePermissionModelImpl.getRoleId()
5356                                            };
5357    
5358                                    finderCache.putResult(FINDER_PATH_COUNT_BY_C_N_S_P_R, args,
5359                                            Long.valueOf(1));
5360                                    finderCache.putResult(FINDER_PATH_FETCH_BY_C_N_S_P_R, args,
5361                                            resourcePermissionModelImpl);
5362                            }
5363                    }
5364            }
5365    
5366            protected void clearUniqueFindersCache(
5367                    ResourcePermissionModelImpl resourcePermissionModelImpl) {
5368                    Object[] args = new Object[] {
5369                                    resourcePermissionModelImpl.getCompanyId(),
5370                                    resourcePermissionModelImpl.getName(),
5371                                    resourcePermissionModelImpl.getScope(),
5372                                    resourcePermissionModelImpl.getPrimKey(),
5373                                    resourcePermissionModelImpl.getRoleId()
5374                            };
5375    
5376                    finderCache.removeResult(FINDER_PATH_COUNT_BY_C_N_S_P_R, args);
5377                    finderCache.removeResult(FINDER_PATH_FETCH_BY_C_N_S_P_R, args);
5378    
5379                    if ((resourcePermissionModelImpl.getColumnBitmask() &
5380                                    FINDER_PATH_FETCH_BY_C_N_S_P_R.getColumnBitmask()) != 0) {
5381                            args = new Object[] {
5382                                            resourcePermissionModelImpl.getOriginalCompanyId(),
5383                                            resourcePermissionModelImpl.getOriginalName(),
5384                                            resourcePermissionModelImpl.getOriginalScope(),
5385                                            resourcePermissionModelImpl.getOriginalPrimKey(),
5386                                            resourcePermissionModelImpl.getOriginalRoleId()
5387                                    };
5388    
5389                            finderCache.removeResult(FINDER_PATH_COUNT_BY_C_N_S_P_R, args);
5390                            finderCache.removeResult(FINDER_PATH_FETCH_BY_C_N_S_P_R, args);
5391                    }
5392            }
5393    
5394            /**
5395             * Creates a new resource permission with the primary key. Does not add the resource permission to the database.
5396             *
5397             * @param resourcePermissionId the primary key for the new resource permission
5398             * @return the new resource permission
5399             */
5400            @Override
5401            public ResourcePermission create(long resourcePermissionId) {
5402                    ResourcePermission resourcePermission = new ResourcePermissionImpl();
5403    
5404                    resourcePermission.setNew(true);
5405                    resourcePermission.setPrimaryKey(resourcePermissionId);
5406    
5407                    return resourcePermission;
5408            }
5409    
5410            /**
5411             * Removes the resource permission with the primary key from the database. Also notifies the appropriate model listeners.
5412             *
5413             * @param resourcePermissionId the primary key of the resource permission
5414             * @return the resource permission that was removed
5415             * @throws NoSuchResourcePermissionException if a resource permission with the primary key could not be found
5416             */
5417            @Override
5418            public ResourcePermission remove(long resourcePermissionId)
5419                    throws NoSuchResourcePermissionException {
5420                    return remove((Serializable)resourcePermissionId);
5421            }
5422    
5423            /**
5424             * Removes the resource permission with the primary key from the database. Also notifies the appropriate model listeners.
5425             *
5426             * @param primaryKey the primary key of the resource permission
5427             * @return the resource permission that was removed
5428             * @throws NoSuchResourcePermissionException if a resource permission with the primary key could not be found
5429             */
5430            @Override
5431            public ResourcePermission remove(Serializable primaryKey)
5432                    throws NoSuchResourcePermissionException {
5433                    Session session = null;
5434    
5435                    try {
5436                            session = openSession();
5437    
5438                            ResourcePermission resourcePermission = (ResourcePermission)session.get(ResourcePermissionImpl.class,
5439                                            primaryKey);
5440    
5441                            if (resourcePermission == null) {
5442                                    if (_log.isWarnEnabled()) {
5443                                            _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
5444                                    }
5445    
5446                                    throw new NoSuchResourcePermissionException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
5447                                            primaryKey);
5448                            }
5449    
5450                            return remove(resourcePermission);
5451                    }
5452                    catch (NoSuchResourcePermissionException nsee) {
5453                            throw nsee;
5454                    }
5455                    catch (Exception e) {
5456                            throw processException(e);
5457                    }
5458                    finally {
5459                            closeSession(session);
5460                    }
5461            }
5462    
5463            @Override
5464            protected ResourcePermission removeImpl(
5465                    ResourcePermission resourcePermission) {
5466                    resourcePermission = toUnwrappedModel(resourcePermission);
5467    
5468                    Session session = null;
5469    
5470                    try {
5471                            session = openSession();
5472    
5473                            if (!session.contains(resourcePermission)) {
5474                                    resourcePermission = (ResourcePermission)session.get(ResourcePermissionImpl.class,
5475                                                    resourcePermission.getPrimaryKeyObj());
5476                            }
5477    
5478                            if (resourcePermission != null) {
5479                                    session.delete(resourcePermission);
5480                            }
5481                    }
5482                    catch (Exception e) {
5483                            throw processException(e);
5484                    }
5485                    finally {
5486                            closeSession(session);
5487                    }
5488    
5489                    if (resourcePermission != null) {
5490                            clearCache(resourcePermission);
5491                    }
5492    
5493                    return resourcePermission;
5494            }
5495    
5496            @Override
5497            public ResourcePermission updateImpl(ResourcePermission resourcePermission) {
5498                    resourcePermission = toUnwrappedModel(resourcePermission);
5499    
5500                    boolean isNew = resourcePermission.isNew();
5501    
5502                    ResourcePermissionModelImpl resourcePermissionModelImpl = (ResourcePermissionModelImpl)resourcePermission;
5503    
5504                    Session session = null;
5505    
5506                    try {
5507                            session = openSession();
5508    
5509                            if (resourcePermission.isNew()) {
5510                                    session.save(resourcePermission);
5511    
5512                                    resourcePermission.setNew(false);
5513                            }
5514                            else {
5515                                    resourcePermission = (ResourcePermission)session.merge(resourcePermission);
5516                            }
5517                    }
5518                    catch (Exception e) {
5519                            throw processException(e);
5520                    }
5521                    finally {
5522                            closeSession(session);
5523                    }
5524    
5525                    finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
5526    
5527                    if (isNew || !ResourcePermissionModelImpl.COLUMN_BITMASK_ENABLED) {
5528                            finderCache.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
5529                    }
5530    
5531                    else {
5532                            if ((resourcePermissionModelImpl.getColumnBitmask() &
5533                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_SCOPE.getColumnBitmask()) != 0) {
5534                                    Object[] args = new Object[] {
5535                                                    resourcePermissionModelImpl.getOriginalScope()
5536                                            };
5537    
5538                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_SCOPE, args);
5539                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_SCOPE,
5540                                            args);
5541    
5542                                    args = new Object[] { resourcePermissionModelImpl.getScope() };
5543    
5544                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_SCOPE, args);
5545                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_SCOPE,
5546                                            args);
5547                            }
5548    
5549                            if ((resourcePermissionModelImpl.getColumnBitmask() &
5550                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_ROLEID.getColumnBitmask()) != 0) {
5551                                    Object[] args = new Object[] {
5552                                                    resourcePermissionModelImpl.getOriginalRoleId()
5553                                            };
5554    
5555                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_ROLEID, args);
5556                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_ROLEID,
5557                                            args);
5558    
5559                                    args = new Object[] { resourcePermissionModelImpl.getRoleId() };
5560    
5561                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_ROLEID, args);
5562                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_ROLEID,
5563                                            args);
5564                            }
5565    
5566                            if ((resourcePermissionModelImpl.getColumnBitmask() &
5567                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_N_S.getColumnBitmask()) != 0) {
5568                                    Object[] args = new Object[] {
5569                                                    resourcePermissionModelImpl.getOriginalCompanyId(),
5570                                                    resourcePermissionModelImpl.getOriginalName(),
5571                                                    resourcePermissionModelImpl.getOriginalScope()
5572                                            };
5573    
5574                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_C_N_S, args);
5575                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_N_S,
5576                                            args);
5577    
5578                                    args = new Object[] {
5579                                                    resourcePermissionModelImpl.getCompanyId(),
5580                                                    resourcePermissionModelImpl.getName(),
5581                                                    resourcePermissionModelImpl.getScope()
5582                                            };
5583    
5584                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_C_N_S, args);
5585                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_N_S,
5586                                            args);
5587                            }
5588    
5589                            if ((resourcePermissionModelImpl.getColumnBitmask() &
5590                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_N_S_P.getColumnBitmask()) != 0) {
5591                                    Object[] args = new Object[] {
5592                                                    resourcePermissionModelImpl.getOriginalCompanyId(),
5593                                                    resourcePermissionModelImpl.getOriginalName(),
5594                                                    resourcePermissionModelImpl.getOriginalScope(),
5595                                                    resourcePermissionModelImpl.getOriginalPrimKey()
5596                                            };
5597    
5598                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_C_N_S_P, args);
5599                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_N_S_P,
5600                                            args);
5601    
5602                                    args = new Object[] {
5603                                                    resourcePermissionModelImpl.getCompanyId(),
5604                                                    resourcePermissionModelImpl.getName(),
5605                                                    resourcePermissionModelImpl.getScope(),
5606                                                    resourcePermissionModelImpl.getPrimKey()
5607                                            };
5608    
5609                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_C_N_S_P, args);
5610                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_N_S_P,
5611                                            args);
5612                            }
5613    
5614                            if ((resourcePermissionModelImpl.getColumnBitmask() &
5615                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_N_S_P_R.getColumnBitmask()) != 0) {
5616                                    Object[] args = new Object[] {
5617                                                    resourcePermissionModelImpl.getOriginalCompanyId(),
5618                                                    resourcePermissionModelImpl.getOriginalName(),
5619                                                    resourcePermissionModelImpl.getOriginalScope(),
5620                                                    resourcePermissionModelImpl.getOriginalPrimKey(),
5621                                                    resourcePermissionModelImpl.getOriginalRoleId()
5622                                            };
5623    
5624                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_C_N_S_P_R, args);
5625                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_N_S_P_R,
5626                                            args);
5627    
5628                                    args = new Object[] {
5629                                                    resourcePermissionModelImpl.getCompanyId(),
5630                                                    resourcePermissionModelImpl.getName(),
5631                                                    resourcePermissionModelImpl.getScope(),
5632                                                    resourcePermissionModelImpl.getPrimKey(),
5633                                                    resourcePermissionModelImpl.getRoleId()
5634                                            };
5635    
5636                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_C_N_S_P_R, args);
5637                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_N_S_P_R,
5638                                            args);
5639                            }
5640    
5641                            if ((resourcePermissionModelImpl.getColumnBitmask() &
5642                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_N_S_P_R_V.getColumnBitmask()) != 0) {
5643                                    Object[] args = new Object[] {
5644                                                    resourcePermissionModelImpl.getOriginalCompanyId(),
5645                                                    resourcePermissionModelImpl.getOriginalName(),
5646                                                    resourcePermissionModelImpl.getOriginalScope(),
5647                                                    resourcePermissionModelImpl.getOriginalPrimKeyId(),
5648                                                    resourcePermissionModelImpl.getOriginalRoleId(),
5649                                                    resourcePermissionModelImpl.getOriginalViewActionId()
5650                                            };
5651    
5652                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_C_N_S_P_R_V, args);
5653                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_N_S_P_R_V,
5654                                            args);
5655    
5656                                    args = new Object[] {
5657                                                    resourcePermissionModelImpl.getCompanyId(),
5658                                                    resourcePermissionModelImpl.getName(),
5659                                                    resourcePermissionModelImpl.getScope(),
5660                                                    resourcePermissionModelImpl.getPrimKeyId(),
5661                                                    resourcePermissionModelImpl.getRoleId(),
5662                                                    resourcePermissionModelImpl.getViewActionId()
5663                                            };
5664    
5665                                    finderCache.removeResult(FINDER_PATH_COUNT_BY_C_N_S_P_R_V, args);
5666                                    finderCache.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_N_S_P_R_V,
5667                                            args);
5668                            }
5669                    }
5670    
5671                    entityCache.putResult(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
5672                            ResourcePermissionImpl.class, resourcePermission.getPrimaryKey(),
5673                            resourcePermission, false);
5674    
5675                    clearUniqueFindersCache(resourcePermissionModelImpl);
5676                    cacheUniqueFindersCache(resourcePermissionModelImpl, isNew);
5677    
5678                    resourcePermission.resetOriginalValues();
5679    
5680                    return resourcePermission;
5681            }
5682    
5683            protected ResourcePermission toUnwrappedModel(
5684                    ResourcePermission resourcePermission) {
5685                    if (resourcePermission instanceof ResourcePermissionImpl) {
5686                            return resourcePermission;
5687                    }
5688    
5689                    ResourcePermissionImpl resourcePermissionImpl = new ResourcePermissionImpl();
5690    
5691                    resourcePermissionImpl.setNew(resourcePermission.isNew());
5692                    resourcePermissionImpl.setPrimaryKey(resourcePermission.getPrimaryKey());
5693    
5694                    resourcePermissionImpl.setMvccVersion(resourcePermission.getMvccVersion());
5695                    resourcePermissionImpl.setResourcePermissionId(resourcePermission.getResourcePermissionId());
5696                    resourcePermissionImpl.setCompanyId(resourcePermission.getCompanyId());
5697                    resourcePermissionImpl.setName(resourcePermission.getName());
5698                    resourcePermissionImpl.setScope(resourcePermission.getScope());
5699                    resourcePermissionImpl.setPrimKey(resourcePermission.getPrimKey());
5700                    resourcePermissionImpl.setPrimKeyId(resourcePermission.getPrimKeyId());
5701                    resourcePermissionImpl.setRoleId(resourcePermission.getRoleId());
5702                    resourcePermissionImpl.setOwnerId(resourcePermission.getOwnerId());
5703                    resourcePermissionImpl.setActionIds(resourcePermission.getActionIds());
5704                    resourcePermissionImpl.setViewActionId(resourcePermission.isViewActionId());
5705    
5706                    return resourcePermissionImpl;
5707            }
5708    
5709            /**
5710             * Returns the resource permission with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
5711             *
5712             * @param primaryKey the primary key of the resource permission
5713             * @return the resource permission
5714             * @throws NoSuchResourcePermissionException if a resource permission with the primary key could not be found
5715             */
5716            @Override
5717            public ResourcePermission findByPrimaryKey(Serializable primaryKey)
5718                    throws NoSuchResourcePermissionException {
5719                    ResourcePermission resourcePermission = fetchByPrimaryKey(primaryKey);
5720    
5721                    if (resourcePermission == null) {
5722                            if (_log.isWarnEnabled()) {
5723                                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
5724                            }
5725    
5726                            throw new NoSuchResourcePermissionException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
5727                                    primaryKey);
5728                    }
5729    
5730                    return resourcePermission;
5731            }
5732    
5733            /**
5734             * Returns the resource permission with the primary key or throws a {@link NoSuchResourcePermissionException} if it could not be found.
5735             *
5736             * @param resourcePermissionId the primary key of the resource permission
5737             * @return the resource permission
5738             * @throws NoSuchResourcePermissionException if a resource permission with the primary key could not be found
5739             */
5740            @Override
5741            public ResourcePermission findByPrimaryKey(long resourcePermissionId)
5742                    throws NoSuchResourcePermissionException {
5743                    return findByPrimaryKey((Serializable)resourcePermissionId);
5744            }
5745    
5746            /**
5747             * Returns the resource permission with the primary key or returns <code>null</code> if it could not be found.
5748             *
5749             * @param primaryKey the primary key of the resource permission
5750             * @return the resource permission, or <code>null</code> if a resource permission with the primary key could not be found
5751             */
5752            @Override
5753            public ResourcePermission fetchByPrimaryKey(Serializable primaryKey) {
5754                    ResourcePermission resourcePermission = (ResourcePermission)entityCache.getResult(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
5755                                    ResourcePermissionImpl.class, primaryKey);
5756    
5757                    if (resourcePermission == _nullResourcePermission) {
5758                            return null;
5759                    }
5760    
5761                    if (resourcePermission == null) {
5762                            Session session = null;
5763    
5764                            try {
5765                                    session = openSession();
5766    
5767                                    resourcePermission = (ResourcePermission)session.get(ResourcePermissionImpl.class,
5768                                                    primaryKey);
5769    
5770                                    if (resourcePermission != null) {
5771                                            cacheResult(resourcePermission);
5772                                    }
5773                                    else {
5774                                            entityCache.putResult(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
5775                                                    ResourcePermissionImpl.class, primaryKey,
5776                                                    _nullResourcePermission);
5777                                    }
5778                            }
5779                            catch (Exception e) {
5780                                    entityCache.removeResult(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
5781                                            ResourcePermissionImpl.class, primaryKey);
5782    
5783                                    throw processException(e);
5784                            }
5785                            finally {
5786                                    closeSession(session);
5787                            }
5788                    }
5789    
5790                    return resourcePermission;
5791            }
5792    
5793            /**
5794             * Returns the resource permission with the primary key or returns <code>null</code> if it could not be found.
5795             *
5796             * @param resourcePermissionId the primary key of the resource permission
5797             * @return the resource permission, or <code>null</code> if a resource permission with the primary key could not be found
5798             */
5799            @Override
5800            public ResourcePermission fetchByPrimaryKey(long resourcePermissionId) {
5801                    return fetchByPrimaryKey((Serializable)resourcePermissionId);
5802            }
5803    
5804            @Override
5805            public Map<Serializable, ResourcePermission> fetchByPrimaryKeys(
5806                    Set<Serializable> primaryKeys) {
5807                    if (primaryKeys.isEmpty()) {
5808                            return Collections.emptyMap();
5809                    }
5810    
5811                    Map<Serializable, ResourcePermission> map = new HashMap<Serializable, ResourcePermission>();
5812    
5813                    if (primaryKeys.size() == 1) {
5814                            Iterator<Serializable> iterator = primaryKeys.iterator();
5815    
5816                            Serializable primaryKey = iterator.next();
5817    
5818                            ResourcePermission resourcePermission = fetchByPrimaryKey(primaryKey);
5819    
5820                            if (resourcePermission != null) {
5821                                    map.put(primaryKey, resourcePermission);
5822                            }
5823    
5824                            return map;
5825                    }
5826    
5827                    Set<Serializable> uncachedPrimaryKeys = null;
5828    
5829                    for (Serializable primaryKey : primaryKeys) {
5830                            ResourcePermission resourcePermission = (ResourcePermission)entityCache.getResult(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
5831                                            ResourcePermissionImpl.class, primaryKey);
5832    
5833                            if (resourcePermission == null) {
5834                                    if (uncachedPrimaryKeys == null) {
5835                                            uncachedPrimaryKeys = new HashSet<Serializable>();
5836                                    }
5837    
5838                                    uncachedPrimaryKeys.add(primaryKey);
5839                            }
5840                            else {
5841                                    map.put(primaryKey, resourcePermission);
5842                            }
5843                    }
5844    
5845                    if (uncachedPrimaryKeys == null) {
5846                            return map;
5847                    }
5848    
5849                    StringBundler query = new StringBundler((uncachedPrimaryKeys.size() * 2) +
5850                                    1);
5851    
5852                    query.append(_SQL_SELECT_RESOURCEPERMISSION_WHERE_PKS_IN);
5853    
5854                    for (Serializable primaryKey : uncachedPrimaryKeys) {
5855                            query.append(String.valueOf(primaryKey));
5856    
5857                            query.append(StringPool.COMMA);
5858                    }
5859    
5860                    query.setIndex(query.index() - 1);
5861    
5862                    query.append(StringPool.CLOSE_PARENTHESIS);
5863    
5864                    String sql = query.toString();
5865    
5866                    Session session = null;
5867    
5868                    try {
5869                            session = openSession();
5870    
5871                            Query q = session.createQuery(sql);
5872    
5873                            for (ResourcePermission resourcePermission : (List<ResourcePermission>)q.list()) {
5874                                    map.put(resourcePermission.getPrimaryKeyObj(),
5875                                            resourcePermission);
5876    
5877                                    cacheResult(resourcePermission);
5878    
5879                                    uncachedPrimaryKeys.remove(resourcePermission.getPrimaryKeyObj());
5880                            }
5881    
5882                            for (Serializable primaryKey : uncachedPrimaryKeys) {
5883                                    entityCache.putResult(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
5884                                            ResourcePermissionImpl.class, primaryKey,
5885                                            _nullResourcePermission);
5886                            }
5887                    }
5888                    catch (Exception e) {
5889                            throw processException(e);
5890                    }
5891                    finally {
5892                            closeSession(session);
5893                    }
5894    
5895                    return map;
5896            }
5897    
5898            /**
5899             * Returns all the resource permissions.
5900             *
5901             * @return the resource permissions
5902             */
5903            @Override
5904            public List<ResourcePermission> findAll() {
5905                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
5906            }
5907    
5908            /**
5909             * Returns a range of all the resource permissions.
5910             *
5911             * <p>
5912             * 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 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 QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link 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.
5913             * </p>
5914             *
5915             * @param start the lower bound of the range of resource permissions
5916             * @param end the upper bound of the range of resource permissions (not inclusive)
5917             * @return the range of resource permissions
5918             */
5919            @Override
5920            public List<ResourcePermission> findAll(int start, int end) {
5921                    return findAll(start, end, null);
5922            }
5923    
5924            /**
5925             * Returns an ordered range of all the resource permissions.
5926             *
5927             * <p>
5928             * 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 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 QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link 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.
5929             * </p>
5930             *
5931             * @param start the lower bound of the range of resource permissions
5932             * @param end the upper bound of the range of resource permissions (not inclusive)
5933             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
5934             * @return the ordered range of resource permissions
5935             */
5936            @Override
5937            public List<ResourcePermission> findAll(int start, int end,
5938                    OrderByComparator<ResourcePermission> orderByComparator) {
5939                    return findAll(start, end, orderByComparator, true);
5940            }
5941    
5942            /**
5943             * Returns an ordered range of all the resource permissions.
5944             *
5945             * <p>
5946             * 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 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 QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link 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.
5947             * </p>
5948             *
5949             * @param start the lower bound of the range of resource permissions
5950             * @param end the upper bound of the range of resource permissions (not inclusive)
5951             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
5952             * @param retrieveFromCache whether to retrieve from the finder cache
5953             * @return the ordered range of resource permissions
5954             */
5955            @Override
5956            public List<ResourcePermission> findAll(int start, int end,
5957                    OrderByComparator<ResourcePermission> orderByComparator,
5958                    boolean retrieveFromCache) {
5959                    boolean pagination = true;
5960                    FinderPath finderPath = null;
5961                    Object[] finderArgs = null;
5962    
5963                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
5964                                    (orderByComparator == null)) {
5965                            pagination = false;
5966                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
5967                            finderArgs = FINDER_ARGS_EMPTY;
5968                    }
5969                    else {
5970                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
5971                            finderArgs = new Object[] { start, end, orderByComparator };
5972                    }
5973    
5974                    List<ResourcePermission> list = null;
5975    
5976                    if (retrieveFromCache) {
5977                            list = (List<ResourcePermission>)finderCache.getResult(finderPath,
5978                                            finderArgs, this);
5979                    }
5980    
5981                    if (list == null) {
5982                            StringBundler query = null;
5983                            String sql = null;
5984    
5985                            if (orderByComparator != null) {
5986                                    query = new StringBundler(2 +
5987                                                    (orderByComparator.getOrderByFields().length * 3));
5988    
5989                                    query.append(_SQL_SELECT_RESOURCEPERMISSION);
5990    
5991                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
5992                                            orderByComparator);
5993    
5994                                    sql = query.toString();
5995                            }
5996                            else {
5997                                    sql = _SQL_SELECT_RESOURCEPERMISSION;
5998    
5999                                    if (pagination) {
6000                                            sql = sql.concat(ResourcePermissionModelImpl.ORDER_BY_JPQL);
6001                                    }
6002                            }
6003    
6004                            Session session = null;
6005    
6006                            try {
6007                                    session = openSession();
6008    
6009                                    Query q = session.createQuery(sql);
6010    
6011                                    if (!pagination) {
6012                                            list = (List<ResourcePermission>)QueryUtil.list(q,
6013                                                            getDialect(), start, end, false);
6014    
6015                                            Collections.sort(list);
6016    
6017                                            list = Collections.unmodifiableList(list);
6018                                    }
6019                                    else {
6020                                            list = (List<ResourcePermission>)QueryUtil.list(q,
6021                                                            getDialect(), start, end);
6022                                    }
6023    
6024                                    cacheResult(list);
6025    
6026                                    finderCache.putResult(finderPath, finderArgs, list);
6027                            }
6028                            catch (Exception e) {
6029                                    finderCache.removeResult(finderPath, finderArgs);
6030    
6031                                    throw processException(e);
6032                            }
6033                            finally {
6034                                    closeSession(session);
6035                            }
6036                    }
6037    
6038                    return list;
6039            }
6040    
6041            /**
6042             * Removes all the resource permissions from the database.
6043             *
6044             */
6045            @Override
6046            public void removeAll() {
6047                    for (ResourcePermission resourcePermission : findAll()) {
6048                            remove(resourcePermission);
6049                    }
6050            }
6051    
6052            /**
6053             * Returns the number of resource permissions.
6054             *
6055             * @return the number of resource permissions
6056             */
6057            @Override
6058            public int countAll() {
6059                    Long count = (Long)finderCache.getResult(FINDER_PATH_COUNT_ALL,
6060                                    FINDER_ARGS_EMPTY, this);
6061    
6062                    if (count == null) {
6063                            Session session = null;
6064    
6065                            try {
6066                                    session = openSession();
6067    
6068                                    Query q = session.createQuery(_SQL_COUNT_RESOURCEPERMISSION);
6069    
6070                                    count = (Long)q.uniqueResult();
6071    
6072                                    finderCache.putResult(FINDER_PATH_COUNT_ALL, FINDER_ARGS_EMPTY,
6073                                            count);
6074                            }
6075                            catch (Exception e) {
6076                                    finderCache.removeResult(FINDER_PATH_COUNT_ALL,
6077                                            FINDER_ARGS_EMPTY);
6078    
6079                                    throw processException(e);
6080                            }
6081                            finally {
6082                                    closeSession(session);
6083                            }
6084                    }
6085    
6086                    return count.intValue();
6087            }
6088    
6089            @Override
6090            protected Map<String, Integer> getTableColumnsMap() {
6091                    return ResourcePermissionModelImpl.TABLE_COLUMNS_MAP;
6092            }
6093    
6094            /**
6095             * Initializes the resource permission persistence.
6096             */
6097            public void afterPropertiesSet() {
6098            }
6099    
6100            public void destroy() {
6101                    entityCache.removeCache(ResourcePermissionImpl.class.getName());
6102                    finderCache.removeCache(FINDER_CLASS_NAME_ENTITY);
6103                    finderCache.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
6104                    finderCache.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
6105            }
6106    
6107            protected EntityCache entityCache = EntityCacheUtil.getEntityCache();
6108            protected FinderCache finderCache = FinderCacheUtil.getFinderCache();
6109            private static final String _SQL_SELECT_RESOURCEPERMISSION = "SELECT resourcePermission FROM ResourcePermission resourcePermission";
6110            private static final String _SQL_SELECT_RESOURCEPERMISSION_WHERE_PKS_IN = "SELECT resourcePermission FROM ResourcePermission resourcePermission WHERE resourcePermissionId IN (";
6111            private static final String _SQL_SELECT_RESOURCEPERMISSION_WHERE = "SELECT resourcePermission FROM ResourcePermission resourcePermission WHERE ";
6112            private static final String _SQL_COUNT_RESOURCEPERMISSION = "SELECT COUNT(resourcePermission) FROM ResourcePermission resourcePermission";
6113            private static final String _SQL_COUNT_RESOURCEPERMISSION_WHERE = "SELECT COUNT(resourcePermission) FROM ResourcePermission resourcePermission WHERE ";
6114            private static final String _ORDER_BY_ENTITY_ALIAS = "resourcePermission.";
6115            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No ResourcePermission exists with the primary key ";
6116            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No ResourcePermission exists with the key {";
6117            private static final Log _log = LogFactoryUtil.getLog(ResourcePermissionPersistenceImpl.class);
6118            private static final ResourcePermission _nullResourcePermission = new ResourcePermissionImpl() {
6119                            @Override
6120                            public Object clone() {
6121                                    return this;
6122                            }
6123    
6124                            @Override
6125                            public CacheModel<ResourcePermission> toCacheModel() {
6126                                    return _nullResourcePermissionCacheModel;
6127                            }
6128                    };
6129    
6130            private static final CacheModel<ResourcePermission> _nullResourcePermissionCacheModel =
6131                    new NullCacheModel();
6132    
6133            private static class NullCacheModel implements CacheModel<ResourcePermission>,
6134                    MVCCModel {
6135                    @Override
6136                    public long getMvccVersion() {
6137                            return -1;
6138                    }
6139    
6140                    @Override
6141                    public void setMvccVersion(long mvccVersion) {
6142                    }
6143    
6144                    @Override
6145                    public ResourcePermission toEntityModel() {
6146                            return _nullResourcePermission;
6147                    }
6148            }
6149    }