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