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