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