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