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