001    /**
002     * Copyright (c) 2000-2012 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;
016    
017    import com.liferay.portal.NoSuchResourcePermissionException;
018    import com.liferay.portal.kernel.cache.CacheRegistryUtil;
019    import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
020    import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
021    import com.liferay.portal.kernel.dao.orm.FinderPath;
022    import com.liferay.portal.kernel.dao.orm.Query;
023    import com.liferay.portal.kernel.dao.orm.QueryPos;
024    import com.liferay.portal.kernel.dao.orm.QueryUtil;
025    import com.liferay.portal.kernel.dao.orm.Session;
026    import com.liferay.portal.kernel.exception.SystemException;
027    import com.liferay.portal.kernel.log.Log;
028    import com.liferay.portal.kernel.log.LogFactoryUtil;
029    import com.liferay.portal.kernel.util.ArrayUtil;
030    import com.liferay.portal.kernel.util.GetterUtil;
031    import com.liferay.portal.kernel.util.InstanceFactory;
032    import com.liferay.portal.kernel.util.OrderByComparator;
033    import com.liferay.portal.kernel.util.StringBundler;
034    import com.liferay.portal.kernel.util.StringPool;
035    import com.liferay.portal.kernel.util.StringUtil;
036    import com.liferay.portal.kernel.util.UnmodifiableList;
037    import com.liferay.portal.kernel.util.Validator;
038    import com.liferay.portal.model.CacheModel;
039    import com.liferay.portal.model.ModelListener;
040    import com.liferay.portal.model.ResourcePermission;
041    import com.liferay.portal.model.impl.ResourcePermissionImpl;
042    import com.liferay.portal.model.impl.ResourcePermissionModelImpl;
043    import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
044    
045    import java.io.Serializable;
046    
047    import java.util.ArrayList;
048    import java.util.Collections;
049    import java.util.List;
050    
051    /**
052     * The persistence implementation for the resource permission service.
053     *
054     * <p>
055     * Caching information and settings can be found in <code>portal.properties</code>
056     * </p>
057     *
058     * @author Brian Wing Shun Chan
059     * @see ResourcePermissionPersistence
060     * @see ResourcePermissionUtil
061     * @generated
062     */
063    public class ResourcePermissionPersistenceImpl extends BasePersistenceImpl<ResourcePermission>
064            implements ResourcePermissionPersistence {
065            /*
066             * NOTE FOR DEVELOPERS:
067             *
068             * 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.
069             */
070            public static final String FINDER_CLASS_NAME_ENTITY = ResourcePermissionImpl.class.getName();
071            public static final String FINDER_CLASS_NAME_LIST_WITH_PAGINATION = FINDER_CLASS_NAME_ENTITY +
072                    ".List1";
073            public static final String FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION = FINDER_CLASS_NAME_ENTITY +
074                    ".List2";
075            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_ALL = new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
076                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED,
077                            ResourcePermissionImpl.class,
078                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findAll", new String[0]);
079            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
080                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED,
081                            ResourcePermissionImpl.class,
082                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findAll", new String[0]);
083            public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
084                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED, Long.class,
085                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]);
086            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_SCOPE = new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
087                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED,
088                            ResourcePermissionImpl.class,
089                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByScope",
090                            new String[] {
091                                    Integer.class.getName(),
092                                    
093                            Integer.class.getName(), Integer.class.getName(),
094                                    OrderByComparator.class.getName()
095                            });
096            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_SCOPE = new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
097                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED,
098                            ResourcePermissionImpl.class,
099                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByScope",
100                            new String[] { Integer.class.getName() },
101                            ResourcePermissionModelImpl.SCOPE_COLUMN_BITMASK);
102            public static final FinderPath FINDER_PATH_COUNT_BY_SCOPE = new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
103                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED, Long.class,
104                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByScope",
105                            new String[] { Integer.class.getName() });
106            public static final FinderPath FINDER_PATH_WITH_PAGINATION_COUNT_BY_SCOPE = new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
107                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED, Long.class,
108                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "countByScope",
109                            new String[] { Integer.class.getName() });
110    
111            /**
112             * Returns all the resource permissions where scope = &#63;.
113             *
114             * @param scope the scope
115             * @return the matching resource permissions
116             * @throws SystemException if a system exception occurred
117             */
118            public List<ResourcePermission> findByScope(int scope)
119                    throws SystemException {
120                    return findByScope(scope, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
121            }
122    
123            /**
124             * Returns a range of all the resource permissions where scope = &#63;.
125             *
126             * <p>
127             * 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 com.liferay.portal.kernel.dao.orm.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 com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portal.model.impl.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.
128             * </p>
129             *
130             * @param scope the scope
131             * @param start the lower bound of the range of resource permissions
132             * @param end the upper bound of the range of resource permissions (not inclusive)
133             * @return the range of matching resource permissions
134             * @throws SystemException if a system exception occurred
135             */
136            public List<ResourcePermission> findByScope(int scope, int start, int end)
137                    throws SystemException {
138                    return findByScope(scope, start, end, null);
139            }
140    
141            /**
142             * Returns an ordered range of all the resource permissions where scope = &#63;.
143             *
144             * <p>
145             * 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 com.liferay.portal.kernel.dao.orm.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 com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portal.model.impl.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.
146             * </p>
147             *
148             * @param scope the scope
149             * @param start the lower bound of the range of resource permissions
150             * @param end the upper bound of the range of resource permissions (not inclusive)
151             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
152             * @return the ordered range of matching resource permissions
153             * @throws SystemException if a system exception occurred
154             */
155            public List<ResourcePermission> findByScope(int scope, int start, int end,
156                    OrderByComparator orderByComparator) throws SystemException {
157                    boolean pagination = true;
158                    FinderPath finderPath = null;
159                    Object[] finderArgs = null;
160    
161                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
162                                    (orderByComparator == null)) {
163                            pagination = false;
164                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_SCOPE;
165                            finderArgs = new Object[] { scope };
166                    }
167                    else {
168                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_SCOPE;
169                            finderArgs = new Object[] { scope, start, end, orderByComparator };
170                    }
171    
172                    List<ResourcePermission> list = (List<ResourcePermission>)FinderCacheUtil.getResult(finderPath,
173                                    finderArgs, this);
174    
175                    if ((list != null) && !list.isEmpty()) {
176                            for (ResourcePermission resourcePermission : list) {
177                                    if ((scope != resourcePermission.getScope())) {
178                                            list = null;
179    
180                                            break;
181                                    }
182                            }
183                    }
184    
185                    if (list == null) {
186                            StringBundler query = null;
187    
188                            if (orderByComparator != null) {
189                                    query = new StringBundler(3 +
190                                                    (orderByComparator.getOrderByFields().length * 3));
191                            }
192                            else {
193                                    query = new StringBundler(3);
194                            }
195    
196                            query.append(_SQL_SELECT_RESOURCEPERMISSION_WHERE);
197    
198                            query.append(_FINDER_COLUMN_SCOPE_SCOPE_2);
199    
200                            if (orderByComparator != null) {
201                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
202                                            orderByComparator);
203                            }
204                            else
205                             if (pagination) {
206                                    query.append(ResourcePermissionModelImpl.ORDER_BY_JPQL);
207                            }
208    
209                            String sql = query.toString();
210    
211                            Session session = null;
212    
213                            try {
214                                    session = openSession();
215    
216                                    Query q = session.createQuery(sql);
217    
218                                    QueryPos qPos = QueryPos.getInstance(q);
219    
220                                    qPos.add(scope);
221    
222                                    if (!pagination) {
223                                            list = (List<ResourcePermission>)QueryUtil.list(q,
224                                                            getDialect(), start, end, false);
225    
226                                            Collections.sort(list);
227    
228                                            list = new UnmodifiableList<ResourcePermission>(list);
229                                    }
230                                    else {
231                                            list = (List<ResourcePermission>)QueryUtil.list(q,
232                                                            getDialect(), start, end);
233                                    }
234    
235                                    cacheResult(list);
236    
237                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
238                            }
239                            catch (Exception e) {
240                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
241    
242                                    throw processException(e);
243                            }
244                            finally {
245                                    closeSession(session);
246                            }
247                    }
248    
249                    return list;
250            }
251    
252            /**
253             * Returns the first resource permission in the ordered set where scope = &#63;.
254             *
255             * @param scope the scope
256             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
257             * @return the first matching resource permission
258             * @throws com.liferay.portal.NoSuchResourcePermissionException if a matching resource permission could not be found
259             * @throws SystemException if a system exception occurred
260             */
261            public ResourcePermission findByScope_First(int scope,
262                    OrderByComparator orderByComparator)
263                    throws NoSuchResourcePermissionException, SystemException {
264                    ResourcePermission resourcePermission = fetchByScope_First(scope,
265                                    orderByComparator);
266    
267                    if (resourcePermission != null) {
268                            return resourcePermission;
269                    }
270    
271                    StringBundler msg = new StringBundler(4);
272    
273                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
274    
275                    msg.append("scope=");
276                    msg.append(scope);
277    
278                    msg.append(StringPool.CLOSE_CURLY_BRACE);
279    
280                    throw new NoSuchResourcePermissionException(msg.toString());
281            }
282    
283            /**
284             * Returns the first resource permission in the ordered set where scope = &#63;.
285             *
286             * @param scope the scope
287             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
288             * @return the first matching resource permission, or <code>null</code> if a matching resource permission could not be found
289             * @throws SystemException if a system exception occurred
290             */
291            public ResourcePermission fetchByScope_First(int scope,
292                    OrderByComparator orderByComparator) throws SystemException {
293                    List<ResourcePermission> list = findByScope(scope, 0, 1,
294                                    orderByComparator);
295    
296                    if (!list.isEmpty()) {
297                            return list.get(0);
298                    }
299    
300                    return null;
301            }
302    
303            /**
304             * Returns the last resource permission in the ordered set where scope = &#63;.
305             *
306             * @param scope the scope
307             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
308             * @return the last matching resource permission
309             * @throws com.liferay.portal.NoSuchResourcePermissionException if a matching resource permission could not be found
310             * @throws SystemException if a system exception occurred
311             */
312            public ResourcePermission findByScope_Last(int scope,
313                    OrderByComparator orderByComparator)
314                    throws NoSuchResourcePermissionException, SystemException {
315                    ResourcePermission resourcePermission = fetchByScope_Last(scope,
316                                    orderByComparator);
317    
318                    if (resourcePermission != null) {
319                            return resourcePermission;
320                    }
321    
322                    StringBundler msg = new StringBundler(4);
323    
324                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
325    
326                    msg.append("scope=");
327                    msg.append(scope);
328    
329                    msg.append(StringPool.CLOSE_CURLY_BRACE);
330    
331                    throw new NoSuchResourcePermissionException(msg.toString());
332            }
333    
334            /**
335             * Returns the last resource permission in the ordered set where scope = &#63;.
336             *
337             * @param scope the scope
338             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
339             * @return the last matching resource permission, or <code>null</code> if a matching resource permission could not be found
340             * @throws SystemException if a system exception occurred
341             */
342            public ResourcePermission fetchByScope_Last(int scope,
343                    OrderByComparator orderByComparator) throws SystemException {
344                    int count = countByScope(scope);
345    
346                    List<ResourcePermission> list = findByScope(scope, count - 1, count,
347                                    orderByComparator);
348    
349                    if (!list.isEmpty()) {
350                            return list.get(0);
351                    }
352    
353                    return null;
354            }
355    
356            /**
357             * Returns the resource permissions before and after the current resource permission in the ordered set where scope = &#63;.
358             *
359             * @param resourcePermissionId the primary key of the current resource permission
360             * @param scope the scope
361             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
362             * @return the previous, current, and next resource permission
363             * @throws com.liferay.portal.NoSuchResourcePermissionException if a resource permission with the primary key could not be found
364             * @throws SystemException if a system exception occurred
365             */
366            public ResourcePermission[] findByScope_PrevAndNext(
367                    long resourcePermissionId, int scope,
368                    OrderByComparator orderByComparator)
369                    throws NoSuchResourcePermissionException, SystemException {
370                    ResourcePermission resourcePermission = findByPrimaryKey(resourcePermissionId);
371    
372                    Session session = null;
373    
374                    try {
375                            session = openSession();
376    
377                            ResourcePermission[] array = new ResourcePermissionImpl[3];
378    
379                            array[0] = getByScope_PrevAndNext(session, resourcePermission,
380                                            scope, orderByComparator, true);
381    
382                            array[1] = resourcePermission;
383    
384                            array[2] = getByScope_PrevAndNext(session, resourcePermission,
385                                            scope, orderByComparator, false);
386    
387                            return array;
388                    }
389                    catch (Exception e) {
390                            throw processException(e);
391                    }
392                    finally {
393                            closeSession(session);
394                    }
395            }
396    
397            protected ResourcePermission getByScope_PrevAndNext(Session session,
398                    ResourcePermission resourcePermission, int scope,
399                    OrderByComparator orderByComparator, boolean previous) {
400                    StringBundler query = null;
401    
402                    if (orderByComparator != null) {
403                            query = new StringBundler(6 +
404                                            (orderByComparator.getOrderByFields().length * 6));
405                    }
406                    else {
407                            query = new StringBundler(3);
408                    }
409    
410                    query.append(_SQL_SELECT_RESOURCEPERMISSION_WHERE);
411    
412                    query.append(_FINDER_COLUMN_SCOPE_SCOPE_2);
413    
414                    if (orderByComparator != null) {
415                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
416    
417                            if (orderByConditionFields.length > 0) {
418                                    query.append(WHERE_AND);
419                            }
420    
421                            for (int i = 0; i < orderByConditionFields.length; i++) {
422                                    query.append(_ORDER_BY_ENTITY_ALIAS);
423                                    query.append(orderByConditionFields[i]);
424    
425                                    if ((i + 1) < orderByConditionFields.length) {
426                                            if (orderByComparator.isAscending() ^ previous) {
427                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
428                                            }
429                                            else {
430                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
431                                            }
432                                    }
433                                    else {
434                                            if (orderByComparator.isAscending() ^ previous) {
435                                                    query.append(WHERE_GREATER_THAN);
436                                            }
437                                            else {
438                                                    query.append(WHERE_LESSER_THAN);
439                                            }
440                                    }
441                            }
442    
443                            query.append(ORDER_BY_CLAUSE);
444    
445                            String[] orderByFields = orderByComparator.getOrderByFields();
446    
447                            for (int i = 0; i < orderByFields.length; i++) {
448                                    query.append(_ORDER_BY_ENTITY_ALIAS);
449                                    query.append(orderByFields[i]);
450    
451                                    if ((i + 1) < orderByFields.length) {
452                                            if (orderByComparator.isAscending() ^ previous) {
453                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
454                                            }
455                                            else {
456                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
457                                            }
458                                    }
459                                    else {
460                                            if (orderByComparator.isAscending() ^ previous) {
461                                                    query.append(ORDER_BY_ASC);
462                                            }
463                                            else {
464                                                    query.append(ORDER_BY_DESC);
465                                            }
466                                    }
467                            }
468                    }
469                    else {
470                            query.append(ResourcePermissionModelImpl.ORDER_BY_JPQL);
471                    }
472    
473                    String sql = query.toString();
474    
475                    Query q = session.createQuery(sql);
476    
477                    q.setFirstResult(0);
478                    q.setMaxResults(2);
479    
480                    QueryPos qPos = QueryPos.getInstance(q);
481    
482                    qPos.add(scope);
483    
484                    if (orderByComparator != null) {
485                            Object[] values = orderByComparator.getOrderByConditionValues(resourcePermission);
486    
487                            for (Object value : values) {
488                                    qPos.add(value);
489                            }
490                    }
491    
492                    List<ResourcePermission> list = q.list();
493    
494                    if (list.size() == 2) {
495                            return list.get(1);
496                    }
497                    else {
498                            return null;
499                    }
500            }
501    
502            /**
503             * Returns all the resource permissions where scope = any &#63;.
504             *
505             * <p>
506             * 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 com.liferay.portal.kernel.dao.orm.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 com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portal.model.impl.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.
507             * </p>
508             *
509             * @param scopes the scopes
510             * @return the matching resource permissions
511             * @throws SystemException if a system exception occurred
512             */
513            public List<ResourcePermission> findByScope(int[] scopes)
514                    throws SystemException {
515                    return findByScope(scopes, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
516            }
517    
518            /**
519             * Returns a range of all the resource permissions where scope = any &#63;.
520             *
521             * <p>
522             * 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 com.liferay.portal.kernel.dao.orm.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 com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portal.model.impl.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.
523             * </p>
524             *
525             * @param scopes the scopes
526             * @param start the lower bound of the range of resource permissions
527             * @param end the upper bound of the range of resource permissions (not inclusive)
528             * @return the range of matching resource permissions
529             * @throws SystemException if a system exception occurred
530             */
531            public List<ResourcePermission> findByScope(int[] scopes, int start, int end)
532                    throws SystemException {
533                    return findByScope(scopes, start, end, null);
534            }
535    
536            /**
537             * Returns an ordered range of all the resource permissions where scope = any &#63;.
538             *
539             * <p>
540             * 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 com.liferay.portal.kernel.dao.orm.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 com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portal.model.impl.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.
541             * </p>
542             *
543             * @param scopes the scopes
544             * @param start the lower bound of the range of resource permissions
545             * @param end the upper bound of the range of resource permissions (not inclusive)
546             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
547             * @return the ordered range of matching resource permissions
548             * @throws SystemException if a system exception occurred
549             */
550            public List<ResourcePermission> findByScope(int[] scopes, int start,
551                    int end, OrderByComparator orderByComparator) throws SystemException {
552                    if ((scopes != null) && (scopes.length == 1)) {
553                            return findByScope(scopes[0], start, end, orderByComparator);
554                    }
555    
556                    boolean pagination = true;
557                    Object[] finderArgs = null;
558    
559                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
560                                    (orderByComparator == null)) {
561                            pagination = false;
562                            finderArgs = new Object[] { StringUtil.merge(scopes) };
563                    }
564                    else {
565                            finderArgs = new Object[] {
566                                            StringUtil.merge(scopes),
567                                            
568                                            start, end, orderByComparator
569                                    };
570                    }
571    
572                    List<ResourcePermission> list = (List<ResourcePermission>)FinderCacheUtil.getResult(FINDER_PATH_WITH_PAGINATION_FIND_BY_SCOPE,
573                                    finderArgs, this);
574    
575                    if ((list != null) && !list.isEmpty()) {
576                            for (ResourcePermission resourcePermission : list) {
577                                    if (!ArrayUtil.contains(scopes, resourcePermission.getScope())) {
578                                            list = null;
579    
580                                            break;
581                                    }
582                            }
583                    }
584    
585                    if (list == null) {
586                            StringBundler query = new StringBundler();
587    
588                            query.append(_SQL_SELECT_RESOURCEPERMISSION_WHERE);
589    
590                            boolean conjunctionable = false;
591    
592                            if ((scopes == null) || (scopes.length > 0)) {
593                                    if (conjunctionable) {
594                                            query.append(WHERE_AND);
595                                    }
596    
597                                    query.append(StringPool.OPEN_PARENTHESIS);
598    
599                                    for (int i = 0; i < scopes.length; i++) {
600                                            query.append(_FINDER_COLUMN_SCOPE_SCOPE_5);
601    
602                                            if ((i + 1) < scopes.length) {
603                                                    query.append(WHERE_OR);
604                                            }
605                                    }
606    
607                                    query.append(StringPool.CLOSE_PARENTHESIS);
608    
609                                    conjunctionable = true;
610                            }
611    
612                            if (orderByComparator != null) {
613                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
614                                            orderByComparator);
615                            }
616                            else
617                             if (pagination) {
618                                    query.append(ResourcePermissionModelImpl.ORDER_BY_JPQL);
619                            }
620    
621                            String sql = query.toString();
622    
623                            Session session = null;
624    
625                            try {
626                                    session = openSession();
627    
628                                    Query q = session.createQuery(sql);
629    
630                                    QueryPos qPos = QueryPos.getInstance(q);
631    
632                                    if (scopes != null) {
633                                            qPos.add(scopes);
634                                    }
635    
636                                    if (!pagination) {
637                                            list = (List<ResourcePermission>)QueryUtil.list(q,
638                                                            getDialect(), start, end, false);
639    
640                                            Collections.sort(list);
641    
642                                            list = new UnmodifiableList<ResourcePermission>(list);
643                                    }
644                                    else {
645                                            list = (List<ResourcePermission>)QueryUtil.list(q,
646                                                            getDialect(), start, end);
647                                    }
648    
649                                    cacheResult(list);
650    
651                                    FinderCacheUtil.putResult(FINDER_PATH_WITH_PAGINATION_FIND_BY_SCOPE,
652                                            finderArgs, list);
653                            }
654                            catch (Exception e) {
655                                    FinderCacheUtil.removeResult(FINDER_PATH_WITH_PAGINATION_FIND_BY_SCOPE,
656                                            finderArgs);
657    
658                                    throw processException(e);
659                            }
660                            finally {
661                                    closeSession(session);
662                            }
663                    }
664    
665                    return list;
666            }
667    
668            /**
669             * Removes all the resource permissions where scope = &#63; from the database.
670             *
671             * @param scope the scope
672             * @throws SystemException if a system exception occurred
673             */
674            public void removeByScope(int scope) throws SystemException {
675                    for (ResourcePermission resourcePermission : findByScope(scope,
676                                    QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
677                            remove(resourcePermission);
678                    }
679            }
680    
681            /**
682             * Returns the number of resource permissions where scope = &#63;.
683             *
684             * @param scope the scope
685             * @return the number of matching resource permissions
686             * @throws SystemException if a system exception occurred
687             */
688            public int countByScope(int scope) throws SystemException {
689                    FinderPath finderPath = FINDER_PATH_COUNT_BY_SCOPE;
690    
691                    Object[] finderArgs = new Object[] { scope };
692    
693                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
694                                    this);
695    
696                    if (count == null) {
697                            StringBundler query = new StringBundler(2);
698    
699                            query.append(_SQL_COUNT_RESOURCEPERMISSION_WHERE);
700    
701                            query.append(_FINDER_COLUMN_SCOPE_SCOPE_2);
702    
703                            String sql = query.toString();
704    
705                            Session session = null;
706    
707                            try {
708                                    session = openSession();
709    
710                                    Query q = session.createQuery(sql);
711    
712                                    QueryPos qPos = QueryPos.getInstance(q);
713    
714                                    qPos.add(scope);
715    
716                                    count = (Long)q.uniqueResult();
717    
718                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
719                            }
720                            catch (Exception e) {
721                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
722    
723                                    throw processException(e);
724                            }
725                            finally {
726                                    closeSession(session);
727                            }
728                    }
729    
730                    return count.intValue();
731            }
732    
733            /**
734             * Returns the number of resource permissions where scope = any &#63;.
735             *
736             * @param scopes the scopes
737             * @return the number of matching resource permissions
738             * @throws SystemException if a system exception occurred
739             */
740            public int countByScope(int[] scopes) throws SystemException {
741                    Object[] finderArgs = new Object[] { StringUtil.merge(scopes) };
742    
743                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_WITH_PAGINATION_COUNT_BY_SCOPE,
744                                    finderArgs, this);
745    
746                    if (count == null) {
747                            StringBundler query = new StringBundler();
748    
749                            query.append(_SQL_COUNT_RESOURCEPERMISSION_WHERE);
750    
751                            boolean conjunctionable = false;
752    
753                            if ((scopes == null) || (scopes.length > 0)) {
754                                    if (conjunctionable) {
755                                            query.append(WHERE_AND);
756                                    }
757    
758                                    query.append(StringPool.OPEN_PARENTHESIS);
759    
760                                    for (int i = 0; i < scopes.length; i++) {
761                                            query.append(_FINDER_COLUMN_SCOPE_SCOPE_5);
762    
763                                            if ((i + 1) < scopes.length) {
764                                                    query.append(WHERE_OR);
765                                            }
766                                    }
767    
768                                    query.append(StringPool.CLOSE_PARENTHESIS);
769    
770                                    conjunctionable = true;
771                            }
772    
773                            String sql = query.toString();
774    
775                            Session session = null;
776    
777                            try {
778                                    session = openSession();
779    
780                                    Query q = session.createQuery(sql);
781    
782                                    QueryPos qPos = QueryPos.getInstance(q);
783    
784                                    if (scopes != null) {
785                                            qPos.add(scopes);
786                                    }
787    
788                                    count = (Long)q.uniqueResult();
789    
790                                    FinderCacheUtil.putResult(FINDER_PATH_WITH_PAGINATION_COUNT_BY_SCOPE,
791                                            finderArgs, count);
792                            }
793                            catch (Exception e) {
794                                    FinderCacheUtil.removeResult(FINDER_PATH_WITH_PAGINATION_COUNT_BY_SCOPE,
795                                            finderArgs);
796    
797                                    throw processException(e);
798                            }
799                            finally {
800                                    closeSession(session);
801                            }
802                    }
803    
804                    return count.intValue();
805            }
806    
807            private static final String _FINDER_COLUMN_SCOPE_SCOPE_2 = "resourcePermission.scope = ?";
808            private static final String _FINDER_COLUMN_SCOPE_SCOPE_5 = "(" +
809                    removeConjunction(_FINDER_COLUMN_SCOPE_SCOPE_2) + ")";
810            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_ROLEID = new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
811                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED,
812                            ResourcePermissionImpl.class,
813                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByRoleId",
814                            new String[] {
815                                    Long.class.getName(),
816                                    
817                            Integer.class.getName(), Integer.class.getName(),
818                                    OrderByComparator.class.getName()
819                            });
820            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_ROLEID =
821                    new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
822                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED,
823                            ResourcePermissionImpl.class,
824                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByRoleId",
825                            new String[] { Long.class.getName() },
826                            ResourcePermissionModelImpl.ROLEID_COLUMN_BITMASK);
827            public static final FinderPath FINDER_PATH_COUNT_BY_ROLEID = new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
828                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED, Long.class,
829                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByRoleId",
830                            new String[] { Long.class.getName() });
831    
832            /**
833             * Returns all the resource permissions where roleId = &#63;.
834             *
835             * @param roleId the role ID
836             * @return the matching resource permissions
837             * @throws SystemException if a system exception occurred
838             */
839            public List<ResourcePermission> findByRoleId(long roleId)
840                    throws SystemException {
841                    return findByRoleId(roleId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
842            }
843    
844            /**
845             * Returns a range of all the resource permissions where roleId = &#63;.
846             *
847             * <p>
848             * 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 com.liferay.portal.kernel.dao.orm.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 com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portal.model.impl.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.
849             * </p>
850             *
851             * @param roleId the role ID
852             * @param start the lower bound of the range of resource permissions
853             * @param end the upper bound of the range of resource permissions (not inclusive)
854             * @return the range of matching resource permissions
855             * @throws SystemException if a system exception occurred
856             */
857            public List<ResourcePermission> findByRoleId(long roleId, int start, int end)
858                    throws SystemException {
859                    return findByRoleId(roleId, start, end, null);
860            }
861    
862            /**
863             * Returns an ordered range of all the resource permissions where roleId = &#63;.
864             *
865             * <p>
866             * 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 com.liferay.portal.kernel.dao.orm.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 com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portal.model.impl.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.
867             * </p>
868             *
869             * @param roleId the role ID
870             * @param start the lower bound of the range of resource permissions
871             * @param end the upper bound of the range of resource permissions (not inclusive)
872             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
873             * @return the ordered range of matching resource permissions
874             * @throws SystemException if a system exception occurred
875             */
876            public List<ResourcePermission> findByRoleId(long roleId, int start,
877                    int end, OrderByComparator orderByComparator) throws SystemException {
878                    boolean pagination = true;
879                    FinderPath finderPath = null;
880                    Object[] finderArgs = null;
881    
882                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
883                                    (orderByComparator == null)) {
884                            pagination = false;
885                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_ROLEID;
886                            finderArgs = new Object[] { roleId };
887                    }
888                    else {
889                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_ROLEID;
890                            finderArgs = new Object[] { roleId, start, end, orderByComparator };
891                    }
892    
893                    List<ResourcePermission> list = (List<ResourcePermission>)FinderCacheUtil.getResult(finderPath,
894                                    finderArgs, this);
895    
896                    if ((list != null) && !list.isEmpty()) {
897                            for (ResourcePermission resourcePermission : list) {
898                                    if ((roleId != resourcePermission.getRoleId())) {
899                                            list = null;
900    
901                                            break;
902                                    }
903                            }
904                    }
905    
906                    if (list == null) {
907                            StringBundler query = null;
908    
909                            if (orderByComparator != null) {
910                                    query = new StringBundler(3 +
911                                                    (orderByComparator.getOrderByFields().length * 3));
912                            }
913                            else {
914                                    query = new StringBundler(3);
915                            }
916    
917                            query.append(_SQL_SELECT_RESOURCEPERMISSION_WHERE);
918    
919                            query.append(_FINDER_COLUMN_ROLEID_ROLEID_2);
920    
921                            if (orderByComparator != null) {
922                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
923                                            orderByComparator);
924                            }
925                            else
926                             if (pagination) {
927                                    query.append(ResourcePermissionModelImpl.ORDER_BY_JPQL);
928                            }
929    
930                            String sql = query.toString();
931    
932                            Session session = null;
933    
934                            try {
935                                    session = openSession();
936    
937                                    Query q = session.createQuery(sql);
938    
939                                    QueryPos qPos = QueryPos.getInstance(q);
940    
941                                    qPos.add(roleId);
942    
943                                    if (!pagination) {
944                                            list = (List<ResourcePermission>)QueryUtil.list(q,
945                                                            getDialect(), start, end, false);
946    
947                                            Collections.sort(list);
948    
949                                            list = new UnmodifiableList<ResourcePermission>(list);
950                                    }
951                                    else {
952                                            list = (List<ResourcePermission>)QueryUtil.list(q,
953                                                            getDialect(), start, end);
954                                    }
955    
956                                    cacheResult(list);
957    
958                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
959                            }
960                            catch (Exception e) {
961                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
962    
963                                    throw processException(e);
964                            }
965                            finally {
966                                    closeSession(session);
967                            }
968                    }
969    
970                    return list;
971            }
972    
973            /**
974             * Returns the first resource permission in the ordered set where roleId = &#63;.
975             *
976             * @param roleId the role ID
977             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
978             * @return the first matching resource permission
979             * @throws com.liferay.portal.NoSuchResourcePermissionException if a matching resource permission could not be found
980             * @throws SystemException if a system exception occurred
981             */
982            public ResourcePermission findByRoleId_First(long roleId,
983                    OrderByComparator orderByComparator)
984                    throws NoSuchResourcePermissionException, SystemException {
985                    ResourcePermission resourcePermission = fetchByRoleId_First(roleId,
986                                    orderByComparator);
987    
988                    if (resourcePermission != null) {
989                            return resourcePermission;
990                    }
991    
992                    StringBundler msg = new StringBundler(4);
993    
994                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
995    
996                    msg.append("roleId=");
997                    msg.append(roleId);
998    
999                    msg.append(StringPool.CLOSE_CURLY_BRACE);
1000    
1001                    throw new NoSuchResourcePermissionException(msg.toString());
1002            }
1003    
1004            /**
1005             * Returns the first resource permission in the ordered set where roleId = &#63;.
1006             *
1007             * @param roleId the role ID
1008             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1009             * @return the first matching resource permission, or <code>null</code> if a matching resource permission could not be found
1010             * @throws SystemException if a system exception occurred
1011             */
1012            public ResourcePermission fetchByRoleId_First(long roleId,
1013                    OrderByComparator orderByComparator) throws SystemException {
1014                    List<ResourcePermission> list = findByRoleId(roleId, 0, 1,
1015                                    orderByComparator);
1016    
1017                    if (!list.isEmpty()) {
1018                            return list.get(0);
1019                    }
1020    
1021                    return null;
1022            }
1023    
1024            /**
1025             * Returns the last resource permission in the ordered set where roleId = &#63;.
1026             *
1027             * @param roleId the role ID
1028             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1029             * @return the last matching resource permission
1030             * @throws com.liferay.portal.NoSuchResourcePermissionException if a matching resource permission could not be found
1031             * @throws SystemException if a system exception occurred
1032             */
1033            public ResourcePermission findByRoleId_Last(long roleId,
1034                    OrderByComparator orderByComparator)
1035                    throws NoSuchResourcePermissionException, SystemException {
1036                    ResourcePermission resourcePermission = fetchByRoleId_Last(roleId,
1037                                    orderByComparator);
1038    
1039                    if (resourcePermission != null) {
1040                            return resourcePermission;
1041                    }
1042    
1043                    StringBundler msg = new StringBundler(4);
1044    
1045                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1046    
1047                    msg.append("roleId=");
1048                    msg.append(roleId);
1049    
1050                    msg.append(StringPool.CLOSE_CURLY_BRACE);
1051    
1052                    throw new NoSuchResourcePermissionException(msg.toString());
1053            }
1054    
1055            /**
1056             * Returns the last resource permission in the ordered set where roleId = &#63;.
1057             *
1058             * @param roleId the role ID
1059             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1060             * @return the last matching resource permission, or <code>null</code> if a matching resource permission could not be found
1061             * @throws SystemException if a system exception occurred
1062             */
1063            public ResourcePermission fetchByRoleId_Last(long roleId,
1064                    OrderByComparator orderByComparator) throws SystemException {
1065                    int count = countByRoleId(roleId);
1066    
1067                    List<ResourcePermission> list = findByRoleId(roleId, count - 1, count,
1068                                    orderByComparator);
1069    
1070                    if (!list.isEmpty()) {
1071                            return list.get(0);
1072                    }
1073    
1074                    return null;
1075            }
1076    
1077            /**
1078             * Returns the resource permissions before and after the current resource permission in the ordered set where roleId = &#63;.
1079             *
1080             * @param resourcePermissionId the primary key of the current resource permission
1081             * @param roleId the role ID
1082             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1083             * @return the previous, current, and next resource permission
1084             * @throws com.liferay.portal.NoSuchResourcePermissionException if a resource permission with the primary key could not be found
1085             * @throws SystemException if a system exception occurred
1086             */
1087            public ResourcePermission[] findByRoleId_PrevAndNext(
1088                    long resourcePermissionId, long roleId,
1089                    OrderByComparator orderByComparator)
1090                    throws NoSuchResourcePermissionException, SystemException {
1091                    ResourcePermission resourcePermission = findByPrimaryKey(resourcePermissionId);
1092    
1093                    Session session = null;
1094    
1095                    try {
1096                            session = openSession();
1097    
1098                            ResourcePermission[] array = new ResourcePermissionImpl[3];
1099    
1100                            array[0] = getByRoleId_PrevAndNext(session, resourcePermission,
1101                                            roleId, orderByComparator, true);
1102    
1103                            array[1] = resourcePermission;
1104    
1105                            array[2] = getByRoleId_PrevAndNext(session, resourcePermission,
1106                                            roleId, orderByComparator, false);
1107    
1108                            return array;
1109                    }
1110                    catch (Exception e) {
1111                            throw processException(e);
1112                    }
1113                    finally {
1114                            closeSession(session);
1115                    }
1116            }
1117    
1118            protected ResourcePermission getByRoleId_PrevAndNext(Session session,
1119                    ResourcePermission resourcePermission, long roleId,
1120                    OrderByComparator orderByComparator, boolean previous) {
1121                    StringBundler query = null;
1122    
1123                    if (orderByComparator != null) {
1124                            query = new StringBundler(6 +
1125                                            (orderByComparator.getOrderByFields().length * 6));
1126                    }
1127                    else {
1128                            query = new StringBundler(3);
1129                    }
1130    
1131                    query.append(_SQL_SELECT_RESOURCEPERMISSION_WHERE);
1132    
1133                    query.append(_FINDER_COLUMN_ROLEID_ROLEID_2);
1134    
1135                    if (orderByComparator != null) {
1136                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
1137    
1138                            if (orderByConditionFields.length > 0) {
1139                                    query.append(WHERE_AND);
1140                            }
1141    
1142                            for (int i = 0; i < orderByConditionFields.length; i++) {
1143                                    query.append(_ORDER_BY_ENTITY_ALIAS);
1144                                    query.append(orderByConditionFields[i]);
1145    
1146                                    if ((i + 1) < orderByConditionFields.length) {
1147                                            if (orderByComparator.isAscending() ^ previous) {
1148                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
1149                                            }
1150                                            else {
1151                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
1152                                            }
1153                                    }
1154                                    else {
1155                                            if (orderByComparator.isAscending() ^ previous) {
1156                                                    query.append(WHERE_GREATER_THAN);
1157                                            }
1158                                            else {
1159                                                    query.append(WHERE_LESSER_THAN);
1160                                            }
1161                                    }
1162                            }
1163    
1164                            query.append(ORDER_BY_CLAUSE);
1165    
1166                            String[] orderByFields = orderByComparator.getOrderByFields();
1167    
1168                            for (int i = 0; i < orderByFields.length; i++) {
1169                                    query.append(_ORDER_BY_ENTITY_ALIAS);
1170                                    query.append(orderByFields[i]);
1171    
1172                                    if ((i + 1) < orderByFields.length) {
1173                                            if (orderByComparator.isAscending() ^ previous) {
1174                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
1175                                            }
1176                                            else {
1177                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
1178                                            }
1179                                    }
1180                                    else {
1181                                            if (orderByComparator.isAscending() ^ previous) {
1182                                                    query.append(ORDER_BY_ASC);
1183                                            }
1184                                            else {
1185                                                    query.append(ORDER_BY_DESC);
1186                                            }
1187                                    }
1188                            }
1189                    }
1190                    else {
1191                            query.append(ResourcePermissionModelImpl.ORDER_BY_JPQL);
1192                    }
1193    
1194                    String sql = query.toString();
1195    
1196                    Query q = session.createQuery(sql);
1197    
1198                    q.setFirstResult(0);
1199                    q.setMaxResults(2);
1200    
1201                    QueryPos qPos = QueryPos.getInstance(q);
1202    
1203                    qPos.add(roleId);
1204    
1205                    if (orderByComparator != null) {
1206                            Object[] values = orderByComparator.getOrderByConditionValues(resourcePermission);
1207    
1208                            for (Object value : values) {
1209                                    qPos.add(value);
1210                            }
1211                    }
1212    
1213                    List<ResourcePermission> list = q.list();
1214    
1215                    if (list.size() == 2) {
1216                            return list.get(1);
1217                    }
1218                    else {
1219                            return null;
1220                    }
1221            }
1222    
1223            /**
1224             * Removes all the resource permissions where roleId = &#63; from the database.
1225             *
1226             * @param roleId the role ID
1227             * @throws SystemException if a system exception occurred
1228             */
1229            public void removeByRoleId(long roleId) throws SystemException {
1230                    for (ResourcePermission resourcePermission : findByRoleId(roleId,
1231                                    QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
1232                            remove(resourcePermission);
1233                    }
1234            }
1235    
1236            /**
1237             * Returns the number of resource permissions where roleId = &#63;.
1238             *
1239             * @param roleId the role ID
1240             * @return the number of matching resource permissions
1241             * @throws SystemException if a system exception occurred
1242             */
1243            public int countByRoleId(long roleId) throws SystemException {
1244                    FinderPath finderPath = FINDER_PATH_COUNT_BY_ROLEID;
1245    
1246                    Object[] finderArgs = new Object[] { roleId };
1247    
1248                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
1249                                    this);
1250    
1251                    if (count == null) {
1252                            StringBundler query = new StringBundler(2);
1253    
1254                            query.append(_SQL_COUNT_RESOURCEPERMISSION_WHERE);
1255    
1256                            query.append(_FINDER_COLUMN_ROLEID_ROLEID_2);
1257    
1258                            String sql = query.toString();
1259    
1260                            Session session = null;
1261    
1262                            try {
1263                                    session = openSession();
1264    
1265                                    Query q = session.createQuery(sql);
1266    
1267                                    QueryPos qPos = QueryPos.getInstance(q);
1268    
1269                                    qPos.add(roleId);
1270    
1271                                    count = (Long)q.uniqueResult();
1272    
1273                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
1274                            }
1275                            catch (Exception e) {
1276                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
1277    
1278                                    throw processException(e);
1279                            }
1280                            finally {
1281                                    closeSession(session);
1282                            }
1283                    }
1284    
1285                    return count.intValue();
1286            }
1287    
1288            private static final String _FINDER_COLUMN_ROLEID_ROLEID_2 = "resourcePermission.roleId = ?";
1289            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_C_LIKEP = new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
1290                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED,
1291                            ResourcePermissionImpl.class,
1292                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByC_LikeP",
1293                            new String[] {
1294                                    Long.class.getName(), String.class.getName(),
1295                                    
1296                            Integer.class.getName(), Integer.class.getName(),
1297                                    OrderByComparator.class.getName()
1298                            });
1299            public static final FinderPath FINDER_PATH_WITH_PAGINATION_COUNT_BY_C_LIKEP = new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
1300                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED, Long.class,
1301                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "countByC_LikeP",
1302                            new String[] { Long.class.getName(), String.class.getName() });
1303    
1304            /**
1305             * Returns all the resource permissions where companyId = &#63; and primKey LIKE &#63;.
1306             *
1307             * @param companyId the company ID
1308             * @param primKey the prim key
1309             * @return the matching resource permissions
1310             * @throws SystemException if a system exception occurred
1311             */
1312            public List<ResourcePermission> findByC_LikeP(long companyId, String primKey)
1313                    throws SystemException {
1314                    return findByC_LikeP(companyId, primKey, QueryUtil.ALL_POS,
1315                            QueryUtil.ALL_POS, null);
1316            }
1317    
1318            /**
1319             * Returns a range of all the resource permissions where companyId = &#63; and primKey LIKE &#63;.
1320             *
1321             * <p>
1322             * 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 com.liferay.portal.kernel.dao.orm.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 com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portal.model.impl.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.
1323             * </p>
1324             *
1325             * @param companyId the company ID
1326             * @param primKey the prim key
1327             * @param start the lower bound of the range of resource permissions
1328             * @param end the upper bound of the range of resource permissions (not inclusive)
1329             * @return the range of matching resource permissions
1330             * @throws SystemException if a system exception occurred
1331             */
1332            public List<ResourcePermission> findByC_LikeP(long companyId,
1333                    String primKey, int start, int end) throws SystemException {
1334                    return findByC_LikeP(companyId, primKey, start, end, null);
1335            }
1336    
1337            /**
1338             * Returns an ordered range of all the resource permissions where companyId = &#63; and primKey LIKE &#63;.
1339             *
1340             * <p>
1341             * 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 com.liferay.portal.kernel.dao.orm.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 com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portal.model.impl.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.
1342             * </p>
1343             *
1344             * @param companyId the company ID
1345             * @param primKey the prim key
1346             * @param start the lower bound of the range of resource permissions
1347             * @param end the upper bound of the range of resource permissions (not inclusive)
1348             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
1349             * @return the ordered range of matching resource permissions
1350             * @throws SystemException if a system exception occurred
1351             */
1352            public List<ResourcePermission> findByC_LikeP(long companyId,
1353                    String primKey, int start, int end, OrderByComparator orderByComparator)
1354                    throws SystemException {
1355                    boolean pagination = true;
1356                    FinderPath finderPath = null;
1357                    Object[] finderArgs = null;
1358    
1359                    finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_C_LIKEP;
1360                    finderArgs = new Object[] {
1361                                    companyId, primKey,
1362                                    
1363                                    start, end, orderByComparator
1364                            };
1365    
1366                    List<ResourcePermission> list = (List<ResourcePermission>)FinderCacheUtil.getResult(finderPath,
1367                                    finderArgs, this);
1368    
1369                    if ((list != null) && !list.isEmpty()) {
1370                            for (ResourcePermission resourcePermission : list) {
1371                                    if ((companyId != resourcePermission.getCompanyId()) ||
1372                                                    !Validator.equals(primKey,
1373                                                            resourcePermission.getPrimKey())) {
1374                                            list = null;
1375    
1376                                            break;
1377                                    }
1378                            }
1379                    }
1380    
1381                    if (list == null) {
1382                            StringBundler query = null;
1383    
1384                            if (orderByComparator != null) {
1385                                    query = new StringBundler(4 +
1386                                                    (orderByComparator.getOrderByFields().length * 3));
1387                            }
1388                            else {
1389                                    query = new StringBundler(4);
1390                            }
1391    
1392                            query.append(_SQL_SELECT_RESOURCEPERMISSION_WHERE);
1393    
1394                            query.append(_FINDER_COLUMN_C_LIKEP_COMPANYID_2);
1395    
1396                            boolean bindPrimKey = false;
1397    
1398                            if (primKey == null) {
1399                                    query.append(_FINDER_COLUMN_C_LIKEP_PRIMKEY_1);
1400                            }
1401                            else if (primKey.equals(StringPool.BLANK)) {
1402                                    query.append(_FINDER_COLUMN_C_LIKEP_PRIMKEY_3);
1403                            }
1404                            else {
1405                                    bindPrimKey = true;
1406    
1407                                    query.append(_FINDER_COLUMN_C_LIKEP_PRIMKEY_2);
1408                            }
1409    
1410                            if (orderByComparator != null) {
1411                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1412                                            orderByComparator);
1413                            }
1414                            else
1415                             if (pagination) {
1416                                    query.append(ResourcePermissionModelImpl.ORDER_BY_JPQL);
1417                            }
1418    
1419                            String sql = query.toString();
1420    
1421                            Session session = null;
1422    
1423                            try {
1424                                    session = openSession();
1425    
1426                                    Query q = session.createQuery(sql);
1427    
1428                                    QueryPos qPos = QueryPos.getInstance(q);
1429    
1430                                    qPos.add(companyId);
1431    
1432                                    if (bindPrimKey) {
1433                                            qPos.add(primKey);
1434                                    }
1435    
1436                                    if (!pagination) {
1437                                            list = (List<ResourcePermission>)QueryUtil.list(q,
1438                                                            getDialect(), start, end, false);
1439    
1440                                            Collections.sort(list);
1441    
1442                                            list = new UnmodifiableList<ResourcePermission>(list);
1443                                    }
1444                                    else {
1445                                            list = (List<ResourcePermission>)QueryUtil.list(q,
1446                                                            getDialect(), start, end);
1447                                    }
1448    
1449                                    cacheResult(list);
1450    
1451                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
1452                            }
1453                            catch (Exception e) {
1454                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
1455    
1456                                    throw processException(e);
1457                            }
1458                            finally {
1459                                    closeSession(session);
1460                            }
1461                    }
1462    
1463                    return list;
1464            }
1465    
1466            /**
1467             * Returns the first resource permission in the ordered set where companyId = &#63; and primKey LIKE &#63;.
1468             *
1469             * @param companyId the company ID
1470             * @param primKey the prim key
1471             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1472             * @return the first matching resource permission
1473             * @throws com.liferay.portal.NoSuchResourcePermissionException if a matching resource permission could not be found
1474             * @throws SystemException if a system exception occurred
1475             */
1476            public ResourcePermission findByC_LikeP_First(long companyId,
1477                    String primKey, OrderByComparator orderByComparator)
1478                    throws NoSuchResourcePermissionException, SystemException {
1479                    ResourcePermission resourcePermission = fetchByC_LikeP_First(companyId,
1480                                    primKey, orderByComparator);
1481    
1482                    if (resourcePermission != null) {
1483                            return resourcePermission;
1484                    }
1485    
1486                    StringBundler msg = new StringBundler(6);
1487    
1488                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1489    
1490                    msg.append("companyId=");
1491                    msg.append(companyId);
1492    
1493                    msg.append(", primKey=");
1494                    msg.append(primKey);
1495    
1496                    msg.append(StringPool.CLOSE_CURLY_BRACE);
1497    
1498                    throw new NoSuchResourcePermissionException(msg.toString());
1499            }
1500    
1501            /**
1502             * Returns the first resource permission in the ordered set where companyId = &#63; and primKey LIKE &#63;.
1503             *
1504             * @param companyId the company ID
1505             * @param primKey the prim key
1506             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1507             * @return the first matching resource permission, or <code>null</code> if a matching resource permission could not be found
1508             * @throws SystemException if a system exception occurred
1509             */
1510            public ResourcePermission fetchByC_LikeP_First(long companyId,
1511                    String primKey, OrderByComparator orderByComparator)
1512                    throws SystemException {
1513                    List<ResourcePermission> list = findByC_LikeP(companyId, primKey, 0, 1,
1514                                    orderByComparator);
1515    
1516                    if (!list.isEmpty()) {
1517                            return list.get(0);
1518                    }
1519    
1520                    return null;
1521            }
1522    
1523            /**
1524             * Returns the last resource permission in the ordered set where companyId = &#63; and primKey LIKE &#63;.
1525             *
1526             * @param companyId the company ID
1527             * @param primKey the prim key
1528             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1529             * @return the last matching resource permission
1530             * @throws com.liferay.portal.NoSuchResourcePermissionException if a matching resource permission could not be found
1531             * @throws SystemException if a system exception occurred
1532             */
1533            public ResourcePermission findByC_LikeP_Last(long companyId,
1534                    String primKey, OrderByComparator orderByComparator)
1535                    throws NoSuchResourcePermissionException, SystemException {
1536                    ResourcePermission resourcePermission = fetchByC_LikeP_Last(companyId,
1537                                    primKey, orderByComparator);
1538    
1539                    if (resourcePermission != null) {
1540                            return resourcePermission;
1541                    }
1542    
1543                    StringBundler msg = new StringBundler(6);
1544    
1545                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1546    
1547                    msg.append("companyId=");
1548                    msg.append(companyId);
1549    
1550                    msg.append(", primKey=");
1551                    msg.append(primKey);
1552    
1553                    msg.append(StringPool.CLOSE_CURLY_BRACE);
1554    
1555                    throw new NoSuchResourcePermissionException(msg.toString());
1556            }
1557    
1558            /**
1559             * Returns the last resource permission in the ordered set where companyId = &#63; and primKey LIKE &#63;.
1560             *
1561             * @param companyId the company ID
1562             * @param primKey the prim key
1563             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1564             * @return the last matching resource permission, or <code>null</code> if a matching resource permission could not be found
1565             * @throws SystemException if a system exception occurred
1566             */
1567            public ResourcePermission fetchByC_LikeP_Last(long companyId,
1568                    String primKey, OrderByComparator orderByComparator)
1569                    throws SystemException {
1570                    int count = countByC_LikeP(companyId, primKey);
1571    
1572                    List<ResourcePermission> list = findByC_LikeP(companyId, primKey,
1573                                    count - 1, count, orderByComparator);
1574    
1575                    if (!list.isEmpty()) {
1576                            return list.get(0);
1577                    }
1578    
1579                    return null;
1580            }
1581    
1582            /**
1583             * Returns the resource permissions before and after the current resource permission in the ordered set where companyId = &#63; and primKey LIKE &#63;.
1584             *
1585             * @param resourcePermissionId the primary key of the current resource permission
1586             * @param companyId the company ID
1587             * @param primKey the prim key
1588             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1589             * @return the previous, current, and next resource permission
1590             * @throws com.liferay.portal.NoSuchResourcePermissionException if a resource permission with the primary key could not be found
1591             * @throws SystemException if a system exception occurred
1592             */
1593            public ResourcePermission[] findByC_LikeP_PrevAndNext(
1594                    long resourcePermissionId, long companyId, String primKey,
1595                    OrderByComparator orderByComparator)
1596                    throws NoSuchResourcePermissionException, SystemException {
1597                    ResourcePermission resourcePermission = findByPrimaryKey(resourcePermissionId);
1598    
1599                    Session session = null;
1600    
1601                    try {
1602                            session = openSession();
1603    
1604                            ResourcePermission[] array = new ResourcePermissionImpl[3];
1605    
1606                            array[0] = getByC_LikeP_PrevAndNext(session, resourcePermission,
1607                                            companyId, primKey, orderByComparator, true);
1608    
1609                            array[1] = resourcePermission;
1610    
1611                            array[2] = getByC_LikeP_PrevAndNext(session, resourcePermission,
1612                                            companyId, primKey, orderByComparator, false);
1613    
1614                            return array;
1615                    }
1616                    catch (Exception e) {
1617                            throw processException(e);
1618                    }
1619                    finally {
1620                            closeSession(session);
1621                    }
1622            }
1623    
1624            protected ResourcePermission getByC_LikeP_PrevAndNext(Session session,
1625                    ResourcePermission resourcePermission, long companyId, String primKey,
1626                    OrderByComparator orderByComparator, boolean previous) {
1627                    StringBundler query = null;
1628    
1629                    if (orderByComparator != null) {
1630                            query = new StringBundler(6 +
1631                                            (orderByComparator.getOrderByFields().length * 6));
1632                    }
1633                    else {
1634                            query = new StringBundler(3);
1635                    }
1636    
1637                    query.append(_SQL_SELECT_RESOURCEPERMISSION_WHERE);
1638    
1639                    query.append(_FINDER_COLUMN_C_LIKEP_COMPANYID_2);
1640    
1641                    boolean bindPrimKey = false;
1642    
1643                    if (primKey == null) {
1644                            query.append(_FINDER_COLUMN_C_LIKEP_PRIMKEY_1);
1645                    }
1646                    else if (primKey.equals(StringPool.BLANK)) {
1647                            query.append(_FINDER_COLUMN_C_LIKEP_PRIMKEY_3);
1648                    }
1649                    else {
1650                            bindPrimKey = true;
1651    
1652                            query.append(_FINDER_COLUMN_C_LIKEP_PRIMKEY_2);
1653                    }
1654    
1655                    if (orderByComparator != null) {
1656                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
1657    
1658                            if (orderByConditionFields.length > 0) {
1659                                    query.append(WHERE_AND);
1660                            }
1661    
1662                            for (int i = 0; i < orderByConditionFields.length; i++) {
1663                                    query.append(_ORDER_BY_ENTITY_ALIAS);
1664                                    query.append(orderByConditionFields[i]);
1665    
1666                                    if ((i + 1) < orderByConditionFields.length) {
1667                                            if (orderByComparator.isAscending() ^ previous) {
1668                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
1669                                            }
1670                                            else {
1671                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
1672                                            }
1673                                    }
1674                                    else {
1675                                            if (orderByComparator.isAscending() ^ previous) {
1676                                                    query.append(WHERE_GREATER_THAN);
1677                                            }
1678                                            else {
1679                                                    query.append(WHERE_LESSER_THAN);
1680                                            }
1681                                    }
1682                            }
1683    
1684                            query.append(ORDER_BY_CLAUSE);
1685    
1686                            String[] orderByFields = orderByComparator.getOrderByFields();
1687    
1688                            for (int i = 0; i < orderByFields.length; i++) {
1689                                    query.append(_ORDER_BY_ENTITY_ALIAS);
1690                                    query.append(orderByFields[i]);
1691    
1692                                    if ((i + 1) < orderByFields.length) {
1693                                            if (orderByComparator.isAscending() ^ previous) {
1694                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
1695                                            }
1696                                            else {
1697                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
1698                                            }
1699                                    }
1700                                    else {
1701                                            if (orderByComparator.isAscending() ^ previous) {
1702                                                    query.append(ORDER_BY_ASC);
1703                                            }
1704                                            else {
1705                                                    query.append(ORDER_BY_DESC);
1706                                            }
1707                                    }
1708                            }
1709                    }
1710                    else {
1711                            query.append(ResourcePermissionModelImpl.ORDER_BY_JPQL);
1712                    }
1713    
1714                    String sql = query.toString();
1715    
1716                    Query q = session.createQuery(sql);
1717    
1718                    q.setFirstResult(0);
1719                    q.setMaxResults(2);
1720    
1721                    QueryPos qPos = QueryPos.getInstance(q);
1722    
1723                    qPos.add(companyId);
1724    
1725                    if (bindPrimKey) {
1726                            qPos.add(primKey);
1727                    }
1728    
1729                    if (orderByComparator != null) {
1730                            Object[] values = orderByComparator.getOrderByConditionValues(resourcePermission);
1731    
1732                            for (Object value : values) {
1733                                    qPos.add(value);
1734                            }
1735                    }
1736    
1737                    List<ResourcePermission> list = q.list();
1738    
1739                    if (list.size() == 2) {
1740                            return list.get(1);
1741                    }
1742                    else {
1743                            return null;
1744                    }
1745            }
1746    
1747            /**
1748             * Removes all the resource permissions where companyId = &#63; and primKey LIKE &#63; from the database.
1749             *
1750             * @param companyId the company ID
1751             * @param primKey the prim key
1752             * @throws SystemException if a system exception occurred
1753             */
1754            public void removeByC_LikeP(long companyId, String primKey)
1755                    throws SystemException {
1756                    for (ResourcePermission resourcePermission : findByC_LikeP(companyId,
1757                                    primKey, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
1758                            remove(resourcePermission);
1759                    }
1760            }
1761    
1762            /**
1763             * Returns the number of resource permissions where companyId = &#63; and primKey LIKE &#63;.
1764             *
1765             * @param companyId the company ID
1766             * @param primKey the prim key
1767             * @return the number of matching resource permissions
1768             * @throws SystemException if a system exception occurred
1769             */
1770            public int countByC_LikeP(long companyId, String primKey)
1771                    throws SystemException {
1772                    FinderPath finderPath = FINDER_PATH_WITH_PAGINATION_COUNT_BY_C_LIKEP;
1773    
1774                    Object[] finderArgs = new Object[] { companyId, primKey };
1775    
1776                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
1777                                    this);
1778    
1779                    if (count == null) {
1780                            StringBundler query = new StringBundler(3);
1781    
1782                            query.append(_SQL_COUNT_RESOURCEPERMISSION_WHERE);
1783    
1784                            query.append(_FINDER_COLUMN_C_LIKEP_COMPANYID_2);
1785    
1786                            boolean bindPrimKey = false;
1787    
1788                            if (primKey == null) {
1789                                    query.append(_FINDER_COLUMN_C_LIKEP_PRIMKEY_1);
1790                            }
1791                            else if (primKey.equals(StringPool.BLANK)) {
1792                                    query.append(_FINDER_COLUMN_C_LIKEP_PRIMKEY_3);
1793                            }
1794                            else {
1795                                    bindPrimKey = true;
1796    
1797                                    query.append(_FINDER_COLUMN_C_LIKEP_PRIMKEY_2);
1798                            }
1799    
1800                            String sql = query.toString();
1801    
1802                            Session session = null;
1803    
1804                            try {
1805                                    session = openSession();
1806    
1807                                    Query q = session.createQuery(sql);
1808    
1809                                    QueryPos qPos = QueryPos.getInstance(q);
1810    
1811                                    qPos.add(companyId);
1812    
1813                                    if (bindPrimKey) {
1814                                            qPos.add(primKey);
1815                                    }
1816    
1817                                    count = (Long)q.uniqueResult();
1818    
1819                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
1820                            }
1821                            catch (Exception e) {
1822                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
1823    
1824                                    throw processException(e);
1825                            }
1826                            finally {
1827                                    closeSession(session);
1828                            }
1829                    }
1830    
1831                    return count.intValue();
1832            }
1833    
1834            private static final String _FINDER_COLUMN_C_LIKEP_COMPANYID_2 = "resourcePermission.companyId = ? AND ";
1835            private static final String _FINDER_COLUMN_C_LIKEP_PRIMKEY_1 = "resourcePermission.primKey LIKE NULL";
1836            private static final String _FINDER_COLUMN_C_LIKEP_PRIMKEY_2 = "resourcePermission.primKey LIKE ?";
1837            private static final String _FINDER_COLUMN_C_LIKEP_PRIMKEY_3 = "(resourcePermission.primKey IS NULL OR resourcePermission.primKey LIKE '')";
1838            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_C_N_S = new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
1839                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED,
1840                            ResourcePermissionImpl.class,
1841                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByC_N_S",
1842                            new String[] {
1843                                    Long.class.getName(), String.class.getName(),
1844                                    Integer.class.getName(),
1845                                    
1846                            Integer.class.getName(), Integer.class.getName(),
1847                                    OrderByComparator.class.getName()
1848                            });
1849            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_N_S = new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
1850                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED,
1851                            ResourcePermissionImpl.class,
1852                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByC_N_S",
1853                            new String[] {
1854                                    Long.class.getName(), String.class.getName(),
1855                                    Integer.class.getName()
1856                            },
1857                            ResourcePermissionModelImpl.COMPANYID_COLUMN_BITMASK |
1858                            ResourcePermissionModelImpl.NAME_COLUMN_BITMASK |
1859                            ResourcePermissionModelImpl.SCOPE_COLUMN_BITMASK);
1860            public static final FinderPath FINDER_PATH_COUNT_BY_C_N_S = new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
1861                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED, Long.class,
1862                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByC_N_S",
1863                            new String[] {
1864                                    Long.class.getName(), String.class.getName(),
1865                                    Integer.class.getName()
1866                            });
1867    
1868            /**
1869             * Returns all the resource permissions where companyId = &#63; and name = &#63; and scope = &#63;.
1870             *
1871             * @param companyId the company ID
1872             * @param name the name
1873             * @param scope the scope
1874             * @return the matching resource permissions
1875             * @throws SystemException if a system exception occurred
1876             */
1877            public List<ResourcePermission> findByC_N_S(long companyId, String name,
1878                    int scope) throws SystemException {
1879                    return findByC_N_S(companyId, name, scope, QueryUtil.ALL_POS,
1880                            QueryUtil.ALL_POS, null);
1881            }
1882    
1883            /**
1884             * Returns a range of all the resource permissions where companyId = &#63; and name = &#63; and scope = &#63;.
1885             *
1886             * <p>
1887             * 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 com.liferay.portal.kernel.dao.orm.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 com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portal.model.impl.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.
1888             * </p>
1889             *
1890             * @param companyId the company ID
1891             * @param name the name
1892             * @param scope the scope
1893             * @param start the lower bound of the range of resource permissions
1894             * @param end the upper bound of the range of resource permissions (not inclusive)
1895             * @return the range of matching resource permissions
1896             * @throws SystemException if a system exception occurred
1897             */
1898            public List<ResourcePermission> findByC_N_S(long companyId, String name,
1899                    int scope, int start, int end) throws SystemException {
1900                    return findByC_N_S(companyId, name, scope, start, end, null);
1901            }
1902    
1903            /**
1904             * Returns an ordered range of all the resource permissions where companyId = &#63; and name = &#63; and scope = &#63;.
1905             *
1906             * <p>
1907             * 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 com.liferay.portal.kernel.dao.orm.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 com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portal.model.impl.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.
1908             * </p>
1909             *
1910             * @param companyId the company ID
1911             * @param name the name
1912             * @param scope the scope
1913             * @param start the lower bound of the range of resource permissions
1914             * @param end the upper bound of the range of resource permissions (not inclusive)
1915             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
1916             * @return the ordered range of matching resource permissions
1917             * @throws SystemException if a system exception occurred
1918             */
1919            public List<ResourcePermission> findByC_N_S(long companyId, String name,
1920                    int scope, int start, int end, OrderByComparator orderByComparator)
1921                    throws SystemException {
1922                    boolean pagination = true;
1923                    FinderPath finderPath = null;
1924                    Object[] finderArgs = null;
1925    
1926                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
1927                                    (orderByComparator == null)) {
1928                            pagination = false;
1929                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_N_S;
1930                            finderArgs = new Object[] { companyId, name, scope };
1931                    }
1932                    else {
1933                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_C_N_S;
1934                            finderArgs = new Object[] {
1935                                            companyId, name, scope,
1936                                            
1937                                            start, end, orderByComparator
1938                                    };
1939                    }
1940    
1941                    List<ResourcePermission> list = (List<ResourcePermission>)FinderCacheUtil.getResult(finderPath,
1942                                    finderArgs, this);
1943    
1944                    if ((list != null) && !list.isEmpty()) {
1945                            for (ResourcePermission resourcePermission : list) {
1946                                    if ((companyId != resourcePermission.getCompanyId()) ||
1947                                                    !Validator.equals(name, resourcePermission.getName()) ||
1948                                                    (scope != resourcePermission.getScope())) {
1949                                            list = null;
1950    
1951                                            break;
1952                                    }
1953                            }
1954                    }
1955    
1956                    if (list == null) {
1957                            StringBundler query = null;
1958    
1959                            if (orderByComparator != null) {
1960                                    query = new StringBundler(5 +
1961                                                    (orderByComparator.getOrderByFields().length * 3));
1962                            }
1963                            else {
1964                                    query = new StringBundler(5);
1965                            }
1966    
1967                            query.append(_SQL_SELECT_RESOURCEPERMISSION_WHERE);
1968    
1969                            query.append(_FINDER_COLUMN_C_N_S_COMPANYID_2);
1970    
1971                            boolean bindName = false;
1972    
1973                            if (name == null) {
1974                                    query.append(_FINDER_COLUMN_C_N_S_NAME_1);
1975                            }
1976                            else if (name.equals(StringPool.BLANK)) {
1977                                    query.append(_FINDER_COLUMN_C_N_S_NAME_3);
1978                            }
1979                            else {
1980                                    bindName = true;
1981    
1982                                    query.append(_FINDER_COLUMN_C_N_S_NAME_2);
1983                            }
1984    
1985                            query.append(_FINDER_COLUMN_C_N_S_SCOPE_2);
1986    
1987                            if (orderByComparator != null) {
1988                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1989                                            orderByComparator);
1990                            }
1991                            else
1992                             if (pagination) {
1993                                    query.append(ResourcePermissionModelImpl.ORDER_BY_JPQL);
1994                            }
1995    
1996                            String sql = query.toString();
1997    
1998                            Session session = null;
1999    
2000                            try {
2001                                    session = openSession();
2002    
2003                                    Query q = session.createQuery(sql);
2004    
2005                                    QueryPos qPos = QueryPos.getInstance(q);
2006    
2007                                    qPos.add(companyId);
2008    
2009                                    if (bindName) {
2010                                            qPos.add(name);
2011                                    }
2012    
2013                                    qPos.add(scope);
2014    
2015                                    if (!pagination) {
2016                                            list = (List<ResourcePermission>)QueryUtil.list(q,
2017                                                            getDialect(), start, end, false);
2018    
2019                                            Collections.sort(list);
2020    
2021                                            list = new UnmodifiableList<ResourcePermission>(list);
2022                                    }
2023                                    else {
2024                                            list = (List<ResourcePermission>)QueryUtil.list(q,
2025                                                            getDialect(), start, end);
2026                                    }
2027    
2028                                    cacheResult(list);
2029    
2030                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
2031                            }
2032                            catch (Exception e) {
2033                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
2034    
2035                                    throw processException(e);
2036                            }
2037                            finally {
2038                                    closeSession(session);
2039                            }
2040                    }
2041    
2042                    return list;
2043            }
2044    
2045            /**
2046             * Returns the first resource permission in the ordered set where companyId = &#63; and name = &#63; and scope = &#63;.
2047             *
2048             * @param companyId the company ID
2049             * @param name the name
2050             * @param scope the scope
2051             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2052             * @return the first matching resource permission
2053             * @throws com.liferay.portal.NoSuchResourcePermissionException if a matching resource permission could not be found
2054             * @throws SystemException if a system exception occurred
2055             */
2056            public ResourcePermission findByC_N_S_First(long companyId, String name,
2057                    int scope, OrderByComparator orderByComparator)
2058                    throws NoSuchResourcePermissionException, SystemException {
2059                    ResourcePermission resourcePermission = fetchByC_N_S_First(companyId,
2060                                    name, scope, orderByComparator);
2061    
2062                    if (resourcePermission != null) {
2063                            return resourcePermission;
2064                    }
2065    
2066                    StringBundler msg = new StringBundler(8);
2067    
2068                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
2069    
2070                    msg.append("companyId=");
2071                    msg.append(companyId);
2072    
2073                    msg.append(", name=");
2074                    msg.append(name);
2075    
2076                    msg.append(", scope=");
2077                    msg.append(scope);
2078    
2079                    msg.append(StringPool.CLOSE_CURLY_BRACE);
2080    
2081                    throw new NoSuchResourcePermissionException(msg.toString());
2082            }
2083    
2084            /**
2085             * Returns the first resource permission in the ordered set where companyId = &#63; and name = &#63; and scope = &#63;.
2086             *
2087             * @param companyId the company ID
2088             * @param name the name
2089             * @param scope the scope
2090             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2091             * @return the first matching resource permission, or <code>null</code> if a matching resource permission could not be found
2092             * @throws SystemException if a system exception occurred
2093             */
2094            public ResourcePermission fetchByC_N_S_First(long companyId, String name,
2095                    int scope, OrderByComparator orderByComparator)
2096                    throws SystemException {
2097                    List<ResourcePermission> list = findByC_N_S(companyId, name, scope, 0,
2098                                    1, orderByComparator);
2099    
2100                    if (!list.isEmpty()) {
2101                            return list.get(0);
2102                    }
2103    
2104                    return null;
2105            }
2106    
2107            /**
2108             * Returns the last resource permission in the ordered set where companyId = &#63; and name = &#63; and scope = &#63;.
2109             *
2110             * @param companyId the company ID
2111             * @param name the name
2112             * @param scope the scope
2113             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2114             * @return the last matching resource permission
2115             * @throws com.liferay.portal.NoSuchResourcePermissionException if a matching resource permission could not be found
2116             * @throws SystemException if a system exception occurred
2117             */
2118            public ResourcePermission findByC_N_S_Last(long companyId, String name,
2119                    int scope, OrderByComparator orderByComparator)
2120                    throws NoSuchResourcePermissionException, SystemException {
2121                    ResourcePermission resourcePermission = fetchByC_N_S_Last(companyId,
2122                                    name, scope, orderByComparator);
2123    
2124                    if (resourcePermission != null) {
2125                            return resourcePermission;
2126                    }
2127    
2128                    StringBundler msg = new StringBundler(8);
2129    
2130                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
2131    
2132                    msg.append("companyId=");
2133                    msg.append(companyId);
2134    
2135                    msg.append(", name=");
2136                    msg.append(name);
2137    
2138                    msg.append(", scope=");
2139                    msg.append(scope);
2140    
2141                    msg.append(StringPool.CLOSE_CURLY_BRACE);
2142    
2143                    throw new NoSuchResourcePermissionException(msg.toString());
2144            }
2145    
2146            /**
2147             * Returns the last resource permission in the ordered set where companyId = &#63; and name = &#63; and scope = &#63;.
2148             *
2149             * @param companyId the company ID
2150             * @param name the name
2151             * @param scope the scope
2152             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2153             * @return the last matching resource permission, or <code>null</code> if a matching resource permission could not be found
2154             * @throws SystemException if a system exception occurred
2155             */
2156            public ResourcePermission fetchByC_N_S_Last(long companyId, String name,
2157                    int scope, OrderByComparator orderByComparator)
2158                    throws SystemException {
2159                    int count = countByC_N_S(companyId, name, scope);
2160    
2161                    List<ResourcePermission> list = findByC_N_S(companyId, name, scope,
2162                                    count - 1, count, orderByComparator);
2163    
2164                    if (!list.isEmpty()) {
2165                            return list.get(0);
2166                    }
2167    
2168                    return null;
2169            }
2170    
2171            /**
2172             * Returns the resource permissions before and after the current resource permission in the ordered set where companyId = &#63; and name = &#63; and scope = &#63;.
2173             *
2174             * @param resourcePermissionId the primary key of the current resource permission
2175             * @param companyId the company ID
2176             * @param name the name
2177             * @param scope the scope
2178             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2179             * @return the previous, current, and next resource permission
2180             * @throws com.liferay.portal.NoSuchResourcePermissionException if a resource permission with the primary key could not be found
2181             * @throws SystemException if a system exception occurred
2182             */
2183            public ResourcePermission[] findByC_N_S_PrevAndNext(
2184                    long resourcePermissionId, long companyId, String name, int scope,
2185                    OrderByComparator orderByComparator)
2186                    throws NoSuchResourcePermissionException, SystemException {
2187                    ResourcePermission resourcePermission = findByPrimaryKey(resourcePermissionId);
2188    
2189                    Session session = null;
2190    
2191                    try {
2192                            session = openSession();
2193    
2194                            ResourcePermission[] array = new ResourcePermissionImpl[3];
2195    
2196                            array[0] = getByC_N_S_PrevAndNext(session, resourcePermission,
2197                                            companyId, name, scope, orderByComparator, true);
2198    
2199                            array[1] = resourcePermission;
2200    
2201                            array[2] = getByC_N_S_PrevAndNext(session, resourcePermission,
2202                                            companyId, name, scope, orderByComparator, false);
2203    
2204                            return array;
2205                    }
2206                    catch (Exception e) {
2207                            throw processException(e);
2208                    }
2209                    finally {
2210                            closeSession(session);
2211                    }
2212            }
2213    
2214            protected ResourcePermission getByC_N_S_PrevAndNext(Session session,
2215                    ResourcePermission resourcePermission, long companyId, String name,
2216                    int scope, OrderByComparator orderByComparator, boolean previous) {
2217                    StringBundler query = null;
2218    
2219                    if (orderByComparator != null) {
2220                            query = new StringBundler(6 +
2221                                            (orderByComparator.getOrderByFields().length * 6));
2222                    }
2223                    else {
2224                            query = new StringBundler(3);
2225                    }
2226    
2227                    query.append(_SQL_SELECT_RESOURCEPERMISSION_WHERE);
2228    
2229                    query.append(_FINDER_COLUMN_C_N_S_COMPANYID_2);
2230    
2231                    boolean bindName = false;
2232    
2233                    if (name == null) {
2234                            query.append(_FINDER_COLUMN_C_N_S_NAME_1);
2235                    }
2236                    else if (name.equals(StringPool.BLANK)) {
2237                            query.append(_FINDER_COLUMN_C_N_S_NAME_3);
2238                    }
2239                    else {
2240                            bindName = true;
2241    
2242                            query.append(_FINDER_COLUMN_C_N_S_NAME_2);
2243                    }
2244    
2245                    query.append(_FINDER_COLUMN_C_N_S_SCOPE_2);
2246    
2247                    if (orderByComparator != null) {
2248                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
2249    
2250                            if (orderByConditionFields.length > 0) {
2251                                    query.append(WHERE_AND);
2252                            }
2253    
2254                            for (int i = 0; i < orderByConditionFields.length; i++) {
2255                                    query.append(_ORDER_BY_ENTITY_ALIAS);
2256                                    query.append(orderByConditionFields[i]);
2257    
2258                                    if ((i + 1) < orderByConditionFields.length) {
2259                                            if (orderByComparator.isAscending() ^ previous) {
2260                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
2261                                            }
2262                                            else {
2263                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
2264                                            }
2265                                    }
2266                                    else {
2267                                            if (orderByComparator.isAscending() ^ previous) {
2268                                                    query.append(WHERE_GREATER_THAN);
2269                                            }
2270                                            else {
2271                                                    query.append(WHERE_LESSER_THAN);
2272                                            }
2273                                    }
2274                            }
2275    
2276                            query.append(ORDER_BY_CLAUSE);
2277    
2278                            String[] orderByFields = orderByComparator.getOrderByFields();
2279    
2280                            for (int i = 0; i < orderByFields.length; i++) {
2281                                    query.append(_ORDER_BY_ENTITY_ALIAS);
2282                                    query.append(orderByFields[i]);
2283    
2284                                    if ((i + 1) < orderByFields.length) {
2285                                            if (orderByComparator.isAscending() ^ previous) {
2286                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
2287                                            }
2288                                            else {
2289                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
2290                                            }
2291                                    }
2292                                    else {
2293                                            if (orderByComparator.isAscending() ^ previous) {
2294                                                    query.append(ORDER_BY_ASC);
2295                                            }
2296                                            else {
2297                                                    query.append(ORDER_BY_DESC);
2298                                            }
2299                                    }
2300                            }
2301                    }
2302                    else {
2303                            query.append(ResourcePermissionModelImpl.ORDER_BY_JPQL);
2304                    }
2305    
2306                    String sql = query.toString();
2307    
2308                    Query q = session.createQuery(sql);
2309    
2310                    q.setFirstResult(0);
2311                    q.setMaxResults(2);
2312    
2313                    QueryPos qPos = QueryPos.getInstance(q);
2314    
2315                    qPos.add(companyId);
2316    
2317                    if (bindName) {
2318                            qPos.add(name);
2319                    }
2320    
2321                    qPos.add(scope);
2322    
2323                    if (orderByComparator != null) {
2324                            Object[] values = orderByComparator.getOrderByConditionValues(resourcePermission);
2325    
2326                            for (Object value : values) {
2327                                    qPos.add(value);
2328                            }
2329                    }
2330    
2331                    List<ResourcePermission> list = q.list();
2332    
2333                    if (list.size() == 2) {
2334                            return list.get(1);
2335                    }
2336                    else {
2337                            return null;
2338                    }
2339            }
2340    
2341            /**
2342             * Removes all the resource permissions where companyId = &#63; and name = &#63; and scope = &#63; from the database.
2343             *
2344             * @param companyId the company ID
2345             * @param name the name
2346             * @param scope the scope
2347             * @throws SystemException if a system exception occurred
2348             */
2349            public void removeByC_N_S(long companyId, String name, int scope)
2350                    throws SystemException {
2351                    for (ResourcePermission resourcePermission : findByC_N_S(companyId,
2352                                    name, scope, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
2353                            remove(resourcePermission);
2354                    }
2355            }
2356    
2357            /**
2358             * Returns the number of resource permissions where companyId = &#63; and name = &#63; and scope = &#63;.
2359             *
2360             * @param companyId the company ID
2361             * @param name the name
2362             * @param scope the scope
2363             * @return the number of matching resource permissions
2364             * @throws SystemException if a system exception occurred
2365             */
2366            public int countByC_N_S(long companyId, String name, int scope)
2367                    throws SystemException {
2368                    FinderPath finderPath = FINDER_PATH_COUNT_BY_C_N_S;
2369    
2370                    Object[] finderArgs = new Object[] { companyId, name, scope };
2371    
2372                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
2373                                    this);
2374    
2375                    if (count == null) {
2376                            StringBundler query = new StringBundler(4);
2377    
2378                            query.append(_SQL_COUNT_RESOURCEPERMISSION_WHERE);
2379    
2380                            query.append(_FINDER_COLUMN_C_N_S_COMPANYID_2);
2381    
2382                            boolean bindName = false;
2383    
2384                            if (name == null) {
2385                                    query.append(_FINDER_COLUMN_C_N_S_NAME_1);
2386                            }
2387                            else if (name.equals(StringPool.BLANK)) {
2388                                    query.append(_FINDER_COLUMN_C_N_S_NAME_3);
2389                            }
2390                            else {
2391                                    bindName = true;
2392    
2393                                    query.append(_FINDER_COLUMN_C_N_S_NAME_2);
2394                            }
2395    
2396                            query.append(_FINDER_COLUMN_C_N_S_SCOPE_2);
2397    
2398                            String sql = query.toString();
2399    
2400                            Session session = null;
2401    
2402                            try {
2403                                    session = openSession();
2404    
2405                                    Query q = session.createQuery(sql);
2406    
2407                                    QueryPos qPos = QueryPos.getInstance(q);
2408    
2409                                    qPos.add(companyId);
2410    
2411                                    if (bindName) {
2412                                            qPos.add(name);
2413                                    }
2414    
2415                                    qPos.add(scope);
2416    
2417                                    count = (Long)q.uniqueResult();
2418    
2419                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
2420                            }
2421                            catch (Exception e) {
2422                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
2423    
2424                                    throw processException(e);
2425                            }
2426                            finally {
2427                                    closeSession(session);
2428                            }
2429                    }
2430    
2431                    return count.intValue();
2432            }
2433    
2434            private static final String _FINDER_COLUMN_C_N_S_COMPANYID_2 = "resourcePermission.companyId = ? AND ";
2435            private static final String _FINDER_COLUMN_C_N_S_NAME_1 = "resourcePermission.name IS NULL AND ";
2436            private static final String _FINDER_COLUMN_C_N_S_NAME_2 = "resourcePermission.name = ? AND ";
2437            private static final String _FINDER_COLUMN_C_N_S_NAME_3 = "(resourcePermission.name IS NULL OR resourcePermission.name = '') AND ";
2438            private static final String _FINDER_COLUMN_C_N_S_SCOPE_2 = "resourcePermission.scope = ?";
2439            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_C_N_S_P = new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
2440                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED,
2441                            ResourcePermissionImpl.class,
2442                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByC_N_S_P",
2443                            new String[] {
2444                                    Long.class.getName(), String.class.getName(),
2445                                    Integer.class.getName(), String.class.getName(),
2446                                    
2447                            Integer.class.getName(), Integer.class.getName(),
2448                                    OrderByComparator.class.getName()
2449                            });
2450            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_N_S_P =
2451                    new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
2452                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED,
2453                            ResourcePermissionImpl.class,
2454                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByC_N_S_P",
2455                            new String[] {
2456                                    Long.class.getName(), String.class.getName(),
2457                                    Integer.class.getName(), String.class.getName()
2458                            },
2459                            ResourcePermissionModelImpl.COMPANYID_COLUMN_BITMASK |
2460                            ResourcePermissionModelImpl.NAME_COLUMN_BITMASK |
2461                            ResourcePermissionModelImpl.SCOPE_COLUMN_BITMASK |
2462                            ResourcePermissionModelImpl.PRIMKEY_COLUMN_BITMASK);
2463            public static final FinderPath FINDER_PATH_COUNT_BY_C_N_S_P = new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
2464                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED, Long.class,
2465                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByC_N_S_P",
2466                            new String[] {
2467                                    Long.class.getName(), String.class.getName(),
2468                                    Integer.class.getName(), String.class.getName()
2469                            });
2470    
2471            /**
2472             * Returns all the resource permissions where companyId = &#63; and name = &#63; and scope = &#63; and primKey = &#63;.
2473             *
2474             * @param companyId the company ID
2475             * @param name the name
2476             * @param scope the scope
2477             * @param primKey the prim key
2478             * @return the matching resource permissions
2479             * @throws SystemException if a system exception occurred
2480             */
2481            public List<ResourcePermission> findByC_N_S_P(long companyId, String name,
2482                    int scope, String primKey) throws SystemException {
2483                    return findByC_N_S_P(companyId, name, scope, primKey,
2484                            QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
2485            }
2486    
2487            /**
2488             * Returns a range of all the resource permissions where companyId = &#63; and name = &#63; and scope = &#63; and primKey = &#63;.
2489             *
2490             * <p>
2491             * 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 com.liferay.portal.kernel.dao.orm.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 com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portal.model.impl.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.
2492             * </p>
2493             *
2494             * @param companyId the company ID
2495             * @param name the name
2496             * @param scope the scope
2497             * @param primKey the prim key
2498             * @param start the lower bound of the range of resource permissions
2499             * @param end the upper bound of the range of resource permissions (not inclusive)
2500             * @return the range of matching resource permissions
2501             * @throws SystemException if a system exception occurred
2502             */
2503            public List<ResourcePermission> findByC_N_S_P(long companyId, String name,
2504                    int scope, String primKey, int start, int end)
2505                    throws SystemException {
2506                    return findByC_N_S_P(companyId, name, scope, primKey, start, end, null);
2507            }
2508    
2509            /**
2510             * Returns an ordered range of all the resource permissions where companyId = &#63; and name = &#63; and scope = &#63; and primKey = &#63;.
2511             *
2512             * <p>
2513             * 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 com.liferay.portal.kernel.dao.orm.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 com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portal.model.impl.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.
2514             * </p>
2515             *
2516             * @param companyId the company ID
2517             * @param name the name
2518             * @param scope the scope
2519             * @param primKey the prim key
2520             * @param start the lower bound of the range of resource permissions
2521             * @param end the upper bound of the range of resource permissions (not inclusive)
2522             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
2523             * @return the ordered range of matching resource permissions
2524             * @throws SystemException if a system exception occurred
2525             */
2526            public List<ResourcePermission> findByC_N_S_P(long companyId, String name,
2527                    int scope, String primKey, int start, int end,
2528                    OrderByComparator orderByComparator) throws SystemException {
2529                    boolean pagination = true;
2530                    FinderPath finderPath = null;
2531                    Object[] finderArgs = null;
2532    
2533                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
2534                                    (orderByComparator == null)) {
2535                            pagination = false;
2536                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_N_S_P;
2537                            finderArgs = new Object[] { companyId, name, scope, primKey };
2538                    }
2539                    else {
2540                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_C_N_S_P;
2541                            finderArgs = new Object[] {
2542                                            companyId, name, scope, primKey,
2543                                            
2544                                            start, end, orderByComparator
2545                                    };
2546                    }
2547    
2548                    List<ResourcePermission> list = (List<ResourcePermission>)FinderCacheUtil.getResult(finderPath,
2549                                    finderArgs, this);
2550    
2551                    if ((list != null) && !list.isEmpty()) {
2552                            for (ResourcePermission resourcePermission : list) {
2553                                    if ((companyId != resourcePermission.getCompanyId()) ||
2554                                                    !Validator.equals(name, resourcePermission.getName()) ||
2555                                                    (scope != resourcePermission.getScope()) ||
2556                                                    !Validator.equals(primKey,
2557                                                            resourcePermission.getPrimKey())) {
2558                                            list = null;
2559    
2560                                            break;
2561                                    }
2562                            }
2563                    }
2564    
2565                    if (list == null) {
2566                            StringBundler query = null;
2567    
2568                            if (orderByComparator != null) {
2569                                    query = new StringBundler(6 +
2570                                                    (orderByComparator.getOrderByFields().length * 3));
2571                            }
2572                            else {
2573                                    query = new StringBundler(6);
2574                            }
2575    
2576                            query.append(_SQL_SELECT_RESOURCEPERMISSION_WHERE);
2577    
2578                            query.append(_FINDER_COLUMN_C_N_S_P_COMPANYID_2);
2579    
2580                            boolean bindName = false;
2581    
2582                            if (name == null) {
2583                                    query.append(_FINDER_COLUMN_C_N_S_P_NAME_1);
2584                            }
2585                            else if (name.equals(StringPool.BLANK)) {
2586                                    query.append(_FINDER_COLUMN_C_N_S_P_NAME_3);
2587                            }
2588                            else {
2589                                    bindName = true;
2590    
2591                                    query.append(_FINDER_COLUMN_C_N_S_P_NAME_2);
2592                            }
2593    
2594                            query.append(_FINDER_COLUMN_C_N_S_P_SCOPE_2);
2595    
2596                            boolean bindPrimKey = false;
2597    
2598                            if (primKey == null) {
2599                                    query.append(_FINDER_COLUMN_C_N_S_P_PRIMKEY_1);
2600                            }
2601                            else if (primKey.equals(StringPool.BLANK)) {
2602                                    query.append(_FINDER_COLUMN_C_N_S_P_PRIMKEY_3);
2603                            }
2604                            else {
2605                                    bindPrimKey = true;
2606    
2607                                    query.append(_FINDER_COLUMN_C_N_S_P_PRIMKEY_2);
2608                            }
2609    
2610                            if (orderByComparator != null) {
2611                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
2612                                            orderByComparator);
2613                            }
2614                            else
2615                             if (pagination) {
2616                                    query.append(ResourcePermissionModelImpl.ORDER_BY_JPQL);
2617                            }
2618    
2619                            String sql = query.toString();
2620    
2621                            Session session = null;
2622    
2623                            try {
2624                                    session = openSession();
2625    
2626                                    Query q = session.createQuery(sql);
2627    
2628                                    QueryPos qPos = QueryPos.getInstance(q);
2629    
2630                                    qPos.add(companyId);
2631    
2632                                    if (bindName) {
2633                                            qPos.add(name);
2634                                    }
2635    
2636                                    qPos.add(scope);
2637    
2638                                    if (bindPrimKey) {
2639                                            qPos.add(primKey);
2640                                    }
2641    
2642                                    if (!pagination) {
2643                                            list = (List<ResourcePermission>)QueryUtil.list(q,
2644                                                            getDialect(), start, end, false);
2645    
2646                                            Collections.sort(list);
2647    
2648                                            list = new UnmodifiableList<ResourcePermission>(list);
2649                                    }
2650                                    else {
2651                                            list = (List<ResourcePermission>)QueryUtil.list(q,
2652                                                            getDialect(), start, end);
2653                                    }
2654    
2655                                    cacheResult(list);
2656    
2657                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
2658                            }
2659                            catch (Exception e) {
2660                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
2661    
2662                                    throw processException(e);
2663                            }
2664                            finally {
2665                                    closeSession(session);
2666                            }
2667                    }
2668    
2669                    return list;
2670            }
2671    
2672            /**
2673             * Returns the first resource permission in the ordered set where companyId = &#63; and name = &#63; and scope = &#63; and primKey = &#63;.
2674             *
2675             * @param companyId the company ID
2676             * @param name the name
2677             * @param scope the scope
2678             * @param primKey the prim key
2679             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2680             * @return the first matching resource permission
2681             * @throws com.liferay.portal.NoSuchResourcePermissionException if a matching resource permission could not be found
2682             * @throws SystemException if a system exception occurred
2683             */
2684            public ResourcePermission findByC_N_S_P_First(long companyId, String name,
2685                    int scope, String primKey, OrderByComparator orderByComparator)
2686                    throws NoSuchResourcePermissionException, SystemException {
2687                    ResourcePermission resourcePermission = fetchByC_N_S_P_First(companyId,
2688                                    name, scope, primKey, orderByComparator);
2689    
2690                    if (resourcePermission != null) {
2691                            return resourcePermission;
2692                    }
2693    
2694                    StringBundler msg = new StringBundler(10);
2695    
2696                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
2697    
2698                    msg.append("companyId=");
2699                    msg.append(companyId);
2700    
2701                    msg.append(", name=");
2702                    msg.append(name);
2703    
2704                    msg.append(", scope=");
2705                    msg.append(scope);
2706    
2707                    msg.append(", primKey=");
2708                    msg.append(primKey);
2709    
2710                    msg.append(StringPool.CLOSE_CURLY_BRACE);
2711    
2712                    throw new NoSuchResourcePermissionException(msg.toString());
2713            }
2714    
2715            /**
2716             * Returns the first resource permission in the ordered set where companyId = &#63; and name = &#63; and scope = &#63; and primKey = &#63;.
2717             *
2718             * @param companyId the company ID
2719             * @param name the name
2720             * @param scope the scope
2721             * @param primKey the prim key
2722             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2723             * @return the first matching resource permission, or <code>null</code> if a matching resource permission could not be found
2724             * @throws SystemException if a system exception occurred
2725             */
2726            public ResourcePermission fetchByC_N_S_P_First(long companyId, String name,
2727                    int scope, String primKey, OrderByComparator orderByComparator)
2728                    throws SystemException {
2729                    List<ResourcePermission> list = findByC_N_S_P(companyId, name, scope,
2730                                    primKey, 0, 1, orderByComparator);
2731    
2732                    if (!list.isEmpty()) {
2733                            return list.get(0);
2734                    }
2735    
2736                    return null;
2737            }
2738    
2739            /**
2740             * Returns the last resource permission in the ordered set where companyId = &#63; and name = &#63; and scope = &#63; and primKey = &#63;.
2741             *
2742             * @param companyId the company ID
2743             * @param name the name
2744             * @param scope the scope
2745             * @param primKey the prim key
2746             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2747             * @return the last matching resource permission
2748             * @throws com.liferay.portal.NoSuchResourcePermissionException if a matching resource permission could not be found
2749             * @throws SystemException if a system exception occurred
2750             */
2751            public ResourcePermission findByC_N_S_P_Last(long companyId, String name,
2752                    int scope, String primKey, OrderByComparator orderByComparator)
2753                    throws NoSuchResourcePermissionException, SystemException {
2754                    ResourcePermission resourcePermission = fetchByC_N_S_P_Last(companyId,
2755                                    name, scope, primKey, orderByComparator);
2756    
2757                    if (resourcePermission != null) {
2758                            return resourcePermission;
2759                    }
2760    
2761                    StringBundler msg = new StringBundler(10);
2762    
2763                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
2764    
2765                    msg.append("companyId=");
2766                    msg.append(companyId);
2767    
2768                    msg.append(", name=");
2769                    msg.append(name);
2770    
2771                    msg.append(", scope=");
2772                    msg.append(scope);
2773    
2774                    msg.append(", primKey=");
2775                    msg.append(primKey);
2776    
2777                    msg.append(StringPool.CLOSE_CURLY_BRACE);
2778    
2779                    throw new NoSuchResourcePermissionException(msg.toString());
2780            }
2781    
2782            /**
2783             * Returns the last resource permission in the ordered set where companyId = &#63; and name = &#63; and scope = &#63; and primKey = &#63;.
2784             *
2785             * @param companyId the company ID
2786             * @param name the name
2787             * @param scope the scope
2788             * @param primKey the prim key
2789             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2790             * @return the last matching resource permission, or <code>null</code> if a matching resource permission could not be found
2791             * @throws SystemException if a system exception occurred
2792             */
2793            public ResourcePermission fetchByC_N_S_P_Last(long companyId, String name,
2794                    int scope, String primKey, OrderByComparator orderByComparator)
2795                    throws SystemException {
2796                    int count = countByC_N_S_P(companyId, name, scope, primKey);
2797    
2798                    List<ResourcePermission> list = findByC_N_S_P(companyId, name, scope,
2799                                    primKey, count - 1, count, orderByComparator);
2800    
2801                    if (!list.isEmpty()) {
2802                            return list.get(0);
2803                    }
2804    
2805                    return null;
2806            }
2807    
2808            /**
2809             * 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;.
2810             *
2811             * @param resourcePermissionId the primary key of the current resource permission
2812             * @param companyId the company ID
2813             * @param name the name
2814             * @param scope the scope
2815             * @param primKey the prim key
2816             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2817             * @return the previous, current, and next resource permission
2818             * @throws com.liferay.portal.NoSuchResourcePermissionException if a resource permission with the primary key could not be found
2819             * @throws SystemException if a system exception occurred
2820             */
2821            public ResourcePermission[] findByC_N_S_P_PrevAndNext(
2822                    long resourcePermissionId, long companyId, String name, int scope,
2823                    String primKey, OrderByComparator orderByComparator)
2824                    throws NoSuchResourcePermissionException, SystemException {
2825                    ResourcePermission resourcePermission = findByPrimaryKey(resourcePermissionId);
2826    
2827                    Session session = null;
2828    
2829                    try {
2830                            session = openSession();
2831    
2832                            ResourcePermission[] array = new ResourcePermissionImpl[3];
2833    
2834                            array[0] = getByC_N_S_P_PrevAndNext(session, resourcePermission,
2835                                            companyId, name, scope, primKey, orderByComparator, true);
2836    
2837                            array[1] = resourcePermission;
2838    
2839                            array[2] = getByC_N_S_P_PrevAndNext(session, resourcePermission,
2840                                            companyId, name, scope, primKey, orderByComparator, false);
2841    
2842                            return array;
2843                    }
2844                    catch (Exception e) {
2845                            throw processException(e);
2846                    }
2847                    finally {
2848                            closeSession(session);
2849                    }
2850            }
2851    
2852            protected ResourcePermission getByC_N_S_P_PrevAndNext(Session session,
2853                    ResourcePermission resourcePermission, long companyId, String name,
2854                    int scope, String primKey, OrderByComparator orderByComparator,
2855                    boolean previous) {
2856                    StringBundler query = null;
2857    
2858                    if (orderByComparator != null) {
2859                            query = new StringBundler(6 +
2860                                            (orderByComparator.getOrderByFields().length * 6));
2861                    }
2862                    else {
2863                            query = new StringBundler(3);
2864                    }
2865    
2866                    query.append(_SQL_SELECT_RESOURCEPERMISSION_WHERE);
2867    
2868                    query.append(_FINDER_COLUMN_C_N_S_P_COMPANYID_2);
2869    
2870                    boolean bindName = false;
2871    
2872                    if (name == null) {
2873                            query.append(_FINDER_COLUMN_C_N_S_P_NAME_1);
2874                    }
2875                    else if (name.equals(StringPool.BLANK)) {
2876                            query.append(_FINDER_COLUMN_C_N_S_P_NAME_3);
2877                    }
2878                    else {
2879                            bindName = true;
2880    
2881                            query.append(_FINDER_COLUMN_C_N_S_P_NAME_2);
2882                    }
2883    
2884                    query.append(_FINDER_COLUMN_C_N_S_P_SCOPE_2);
2885    
2886                    boolean bindPrimKey = false;
2887    
2888                    if (primKey == null) {
2889                            query.append(_FINDER_COLUMN_C_N_S_P_PRIMKEY_1);
2890                    }
2891                    else if (primKey.equals(StringPool.BLANK)) {
2892                            query.append(_FINDER_COLUMN_C_N_S_P_PRIMKEY_3);
2893                    }
2894                    else {
2895                            bindPrimKey = true;
2896    
2897                            query.append(_FINDER_COLUMN_C_N_S_P_PRIMKEY_2);
2898                    }
2899    
2900                    if (orderByComparator != null) {
2901                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
2902    
2903                            if (orderByConditionFields.length > 0) {
2904                                    query.append(WHERE_AND);
2905                            }
2906    
2907                            for (int i = 0; i < orderByConditionFields.length; i++) {
2908                                    query.append(_ORDER_BY_ENTITY_ALIAS);
2909                                    query.append(orderByConditionFields[i]);
2910    
2911                                    if ((i + 1) < orderByConditionFields.length) {
2912                                            if (orderByComparator.isAscending() ^ previous) {
2913                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
2914                                            }
2915                                            else {
2916                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
2917                                            }
2918                                    }
2919                                    else {
2920                                            if (orderByComparator.isAscending() ^ previous) {
2921                                                    query.append(WHERE_GREATER_THAN);
2922                                            }
2923                                            else {
2924                                                    query.append(WHERE_LESSER_THAN);
2925                                            }
2926                                    }
2927                            }
2928    
2929                            query.append(ORDER_BY_CLAUSE);
2930    
2931                            String[] orderByFields = orderByComparator.getOrderByFields();
2932    
2933                            for (int i = 0; i < orderByFields.length; i++) {
2934                                    query.append(_ORDER_BY_ENTITY_ALIAS);
2935                                    query.append(orderByFields[i]);
2936    
2937                                    if ((i + 1) < orderByFields.length) {
2938                                            if (orderByComparator.isAscending() ^ previous) {
2939                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
2940                                            }
2941                                            else {
2942                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
2943                                            }
2944                                    }
2945                                    else {
2946                                            if (orderByComparator.isAscending() ^ previous) {
2947                                                    query.append(ORDER_BY_ASC);
2948                                            }
2949                                            else {
2950                                                    query.append(ORDER_BY_DESC);
2951                                            }
2952                                    }
2953                            }
2954                    }
2955                    else {
2956                            query.append(ResourcePermissionModelImpl.ORDER_BY_JPQL);
2957                    }
2958    
2959                    String sql = query.toString();
2960    
2961                    Query q = session.createQuery(sql);
2962    
2963                    q.setFirstResult(0);
2964                    q.setMaxResults(2);
2965    
2966                    QueryPos qPos = QueryPos.getInstance(q);
2967    
2968                    qPos.add(companyId);
2969    
2970                    if (bindName) {
2971                            qPos.add(name);
2972                    }
2973    
2974                    qPos.add(scope);
2975    
2976                    if (bindPrimKey) {
2977                            qPos.add(primKey);
2978                    }
2979    
2980                    if (orderByComparator != null) {
2981                            Object[] values = orderByComparator.getOrderByConditionValues(resourcePermission);
2982    
2983                            for (Object value : values) {
2984                                    qPos.add(value);
2985                            }
2986                    }
2987    
2988                    List<ResourcePermission> list = q.list();
2989    
2990                    if (list.size() == 2) {
2991                            return list.get(1);
2992                    }
2993                    else {
2994                            return null;
2995                    }
2996            }
2997    
2998            /**
2999             * Removes all the resource permissions where companyId = &#63; and name = &#63; and scope = &#63; and primKey = &#63; from the database.
3000             *
3001             * @param companyId the company ID
3002             * @param name the name
3003             * @param scope the scope
3004             * @param primKey the prim key
3005             * @throws SystemException if a system exception occurred
3006             */
3007            public void removeByC_N_S_P(long companyId, String name, int scope,
3008                    String primKey) throws SystemException {
3009                    for (ResourcePermission resourcePermission : findByC_N_S_P(companyId,
3010                                    name, scope, primKey, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
3011                            remove(resourcePermission);
3012                    }
3013            }
3014    
3015            /**
3016             * Returns the number of resource permissions where companyId = &#63; and name = &#63; and scope = &#63; and primKey = &#63;.
3017             *
3018             * @param companyId the company ID
3019             * @param name the name
3020             * @param scope the scope
3021             * @param primKey the prim key
3022             * @return the number of matching resource permissions
3023             * @throws SystemException if a system exception occurred
3024             */
3025            public int countByC_N_S_P(long companyId, String name, int scope,
3026                    String primKey) throws SystemException {
3027                    FinderPath finderPath = FINDER_PATH_COUNT_BY_C_N_S_P;
3028    
3029                    Object[] finderArgs = new Object[] { companyId, name, scope, primKey };
3030    
3031                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
3032                                    this);
3033    
3034                    if (count == null) {
3035                            StringBundler query = new StringBundler(5);
3036    
3037                            query.append(_SQL_COUNT_RESOURCEPERMISSION_WHERE);
3038    
3039                            query.append(_FINDER_COLUMN_C_N_S_P_COMPANYID_2);
3040    
3041                            boolean bindName = false;
3042    
3043                            if (name == null) {
3044                                    query.append(_FINDER_COLUMN_C_N_S_P_NAME_1);
3045                            }
3046                            else if (name.equals(StringPool.BLANK)) {
3047                                    query.append(_FINDER_COLUMN_C_N_S_P_NAME_3);
3048                            }
3049                            else {
3050                                    bindName = true;
3051    
3052                                    query.append(_FINDER_COLUMN_C_N_S_P_NAME_2);
3053                            }
3054    
3055                            query.append(_FINDER_COLUMN_C_N_S_P_SCOPE_2);
3056    
3057                            boolean bindPrimKey = false;
3058    
3059                            if (primKey == null) {
3060                                    query.append(_FINDER_COLUMN_C_N_S_P_PRIMKEY_1);
3061                            }
3062                            else if (primKey.equals(StringPool.BLANK)) {
3063                                    query.append(_FINDER_COLUMN_C_N_S_P_PRIMKEY_3);
3064                            }
3065                            else {
3066                                    bindPrimKey = true;
3067    
3068                                    query.append(_FINDER_COLUMN_C_N_S_P_PRIMKEY_2);
3069                            }
3070    
3071                            String sql = query.toString();
3072    
3073                            Session session = null;
3074    
3075                            try {
3076                                    session = openSession();
3077    
3078                                    Query q = session.createQuery(sql);
3079    
3080                                    QueryPos qPos = QueryPos.getInstance(q);
3081    
3082                                    qPos.add(companyId);
3083    
3084                                    if (bindName) {
3085                                            qPos.add(name);
3086                                    }
3087    
3088                                    qPos.add(scope);
3089    
3090                                    if (bindPrimKey) {
3091                                            qPos.add(primKey);
3092                                    }
3093    
3094                                    count = (Long)q.uniqueResult();
3095    
3096                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
3097                            }
3098                            catch (Exception e) {
3099                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
3100    
3101                                    throw processException(e);
3102                            }
3103                            finally {
3104                                    closeSession(session);
3105                            }
3106                    }
3107    
3108                    return count.intValue();
3109            }
3110    
3111            private static final String _FINDER_COLUMN_C_N_S_P_COMPANYID_2 = "resourcePermission.companyId = ? AND ";
3112            private static final String _FINDER_COLUMN_C_N_S_P_NAME_1 = "resourcePermission.name IS NULL AND ";
3113            private static final String _FINDER_COLUMN_C_N_S_P_NAME_2 = "resourcePermission.name = ? AND ";
3114            private static final String _FINDER_COLUMN_C_N_S_P_NAME_3 = "(resourcePermission.name IS NULL OR resourcePermission.name = '') AND ";
3115            private static final String _FINDER_COLUMN_C_N_S_P_SCOPE_2 = "resourcePermission.scope = ? AND ";
3116            private static final String _FINDER_COLUMN_C_N_S_P_PRIMKEY_1 = "resourcePermission.primKey IS NULL";
3117            private static final String _FINDER_COLUMN_C_N_S_P_PRIMKEY_2 = "resourcePermission.primKey = ?";
3118            private static final String _FINDER_COLUMN_C_N_S_P_PRIMKEY_3 = "(resourcePermission.primKey IS NULL OR resourcePermission.primKey = '')";
3119            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_C_N_S_P_R =
3120                    new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
3121                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED,
3122                            ResourcePermissionImpl.class,
3123                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "findByC_N_S_P_R",
3124                            new String[] {
3125                                    Long.class.getName(), String.class.getName(),
3126                                    Integer.class.getName(), String.class.getName(),
3127                                    Long.class.getName(),
3128                                    
3129                            Integer.class.getName(), Integer.class.getName(),
3130                                    OrderByComparator.class.getName()
3131                            });
3132            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_N_S_P_R =
3133                    new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
3134                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED,
3135                            ResourcePermissionImpl.class,
3136                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "findByC_N_S_P_R",
3137                            new String[] {
3138                                    Long.class.getName(), String.class.getName(),
3139                                    Integer.class.getName(), String.class.getName(),
3140                                    Long.class.getName()
3141                            },
3142                            ResourcePermissionModelImpl.COMPANYID_COLUMN_BITMASK |
3143                            ResourcePermissionModelImpl.NAME_COLUMN_BITMASK |
3144                            ResourcePermissionModelImpl.SCOPE_COLUMN_BITMASK |
3145                            ResourcePermissionModelImpl.PRIMKEY_COLUMN_BITMASK |
3146                            ResourcePermissionModelImpl.ROLEID_COLUMN_BITMASK);
3147            public static final FinderPath FINDER_PATH_FETCH_BY_C_N_S_P_R = new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
3148                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED,
3149                            ResourcePermissionImpl.class, FINDER_CLASS_NAME_ENTITY,
3150                            "fetchByC_N_S_P_R",
3151                            new String[] {
3152                                    Long.class.getName(), String.class.getName(),
3153                                    Integer.class.getName(), String.class.getName(),
3154                                    Long.class.getName()
3155                            },
3156                            ResourcePermissionModelImpl.COMPANYID_COLUMN_BITMASK |
3157                            ResourcePermissionModelImpl.NAME_COLUMN_BITMASK |
3158                            ResourcePermissionModelImpl.SCOPE_COLUMN_BITMASK |
3159                            ResourcePermissionModelImpl.PRIMKEY_COLUMN_BITMASK |
3160                            ResourcePermissionModelImpl.ROLEID_COLUMN_BITMASK);
3161            public static final FinderPath FINDER_PATH_COUNT_BY_C_N_S_P_R = new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
3162                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED, Long.class,
3163                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByC_N_S_P_R",
3164                            new String[] {
3165                                    Long.class.getName(), String.class.getName(),
3166                                    Integer.class.getName(), String.class.getName(),
3167                                    Long.class.getName()
3168                            });
3169            public static final FinderPath FINDER_PATH_WITH_PAGINATION_COUNT_BY_C_N_S_P_R =
3170                    new FinderPath(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
3171                            ResourcePermissionModelImpl.FINDER_CACHE_ENABLED, Long.class,
3172                            FINDER_CLASS_NAME_LIST_WITH_PAGINATION, "countByC_N_S_P_R",
3173                            new String[] {
3174                                    Long.class.getName(), String.class.getName(),
3175                                    Integer.class.getName(), String.class.getName(),
3176                                    Long.class.getName()
3177                            });
3178    
3179            /**
3180             * Returns all the resource permissions where companyId = &#63; and name = &#63; and scope = &#63; and primKey = &#63; and roleId = any &#63;.
3181             *
3182             * <p>
3183             * 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 com.liferay.portal.kernel.dao.orm.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 com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portal.model.impl.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.
3184             * </p>
3185             *
3186             * @param companyId the company ID
3187             * @param name the name
3188             * @param scope the scope
3189             * @param primKey the prim key
3190             * @param roleIds the role IDs
3191             * @return the matching resource permissions
3192             * @throws SystemException if a system exception occurred
3193             */
3194            public List<ResourcePermission> findByC_N_S_P_R(long companyId,
3195                    String name, int scope, String primKey, long[] roleIds)
3196                    throws SystemException {
3197                    return findByC_N_S_P_R(companyId, name, scope, primKey, roleIds,
3198                            QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
3199            }
3200    
3201            /**
3202             * Returns a range of all the resource permissions where companyId = &#63; and name = &#63; and scope = &#63; and primKey = &#63; and roleId = any &#63;.
3203             *
3204             * <p>
3205             * 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 com.liferay.portal.kernel.dao.orm.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 com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portal.model.impl.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.
3206             * </p>
3207             *
3208             * @param companyId the company ID
3209             * @param name the name
3210             * @param scope the scope
3211             * @param primKey the prim key
3212             * @param roleIds the role IDs
3213             * @param start the lower bound of the range of resource permissions
3214             * @param end the upper bound of the range of resource permissions (not inclusive)
3215             * @return the range of matching resource permissions
3216             * @throws SystemException if a system exception occurred
3217             */
3218            public List<ResourcePermission> findByC_N_S_P_R(long companyId,
3219                    String name, int scope, String primKey, long[] roleIds, int start,
3220                    int end) throws SystemException {
3221                    return findByC_N_S_P_R(companyId, name, scope, primKey, roleIds, start,
3222                            end, null);
3223            }
3224    
3225            /**
3226             * 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;.
3227             *
3228             * <p>
3229             * 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 com.liferay.portal.kernel.dao.orm.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 com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portal.model.impl.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.
3230             * </p>
3231             *
3232             * @param companyId the company ID
3233             * @param name the name
3234             * @param scope the scope
3235             * @param primKey the prim key
3236             * @param roleIds the role IDs
3237             * @param start the lower bound of the range of resource permissions
3238             * @param end the upper bound of the range of resource permissions (not inclusive)
3239             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
3240             * @return the ordered range of matching resource permissions
3241             * @throws SystemException if a system exception occurred
3242             */
3243            public List<ResourcePermission> findByC_N_S_P_R(long companyId,
3244                    String name, int scope, String primKey, long[] roleIds, int start,
3245                    int end, OrderByComparator orderByComparator) throws SystemException {
3246                    if ((roleIds != null) && (roleIds.length == 1)) {
3247                            ResourcePermission resourcePermission = fetchByC_N_S_P_R(companyId,
3248                                            name, scope, primKey, roleIds[0]);
3249    
3250                            if (resourcePermission == null) {
3251                                    return Collections.emptyList();
3252                            }
3253                            else {
3254                                    List<ResourcePermission> list = new ArrayList<ResourcePermission>(1);
3255    
3256                                    list.add(resourcePermission);
3257    
3258                                    return list;
3259                            }
3260                    }
3261    
3262                    boolean pagination = true;
3263                    Object[] finderArgs = null;
3264    
3265                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
3266                                    (orderByComparator == null)) {
3267                            pagination = false;
3268                            finderArgs = new Object[] {
3269                                            companyId, name, scope, primKey, StringUtil.merge(roleIds)
3270                                    };
3271                    }
3272                    else {
3273                            finderArgs = new Object[] {
3274                                            companyId, name, scope, primKey, StringUtil.merge(roleIds),
3275                                            
3276                                            start, end, orderByComparator
3277                                    };
3278                    }
3279    
3280                    List<ResourcePermission> list = (List<ResourcePermission>)FinderCacheUtil.getResult(FINDER_PATH_WITH_PAGINATION_FIND_BY_C_N_S_P_R,
3281                                    finderArgs, this);
3282    
3283                    if ((list != null) && !list.isEmpty()) {
3284                            for (ResourcePermission resourcePermission : list) {
3285                                    if ((companyId != resourcePermission.getCompanyId()) ||
3286                                                    !Validator.equals(name, resourcePermission.getName()) ||
3287                                                    (scope != resourcePermission.getScope()) ||
3288                                                    !Validator.equals(primKey,
3289                                                            resourcePermission.getPrimKey()) ||
3290                                                    !ArrayUtil.contains(roleIds,
3291                                                            resourcePermission.getRoleId())) {
3292                                            list = null;
3293    
3294                                            break;
3295                                    }
3296                            }
3297                    }
3298    
3299                    if (list == null) {
3300                            StringBundler query = new StringBundler();
3301    
3302                            query.append(_SQL_SELECT_RESOURCEPERMISSION_WHERE);
3303    
3304                            boolean conjunctionable = false;
3305    
3306                            if (conjunctionable) {
3307                                    query.append(WHERE_AND);
3308                            }
3309    
3310                            query.append(_FINDER_COLUMN_C_N_S_P_R_COMPANYID_5);
3311    
3312                            conjunctionable = true;
3313    
3314                            if (conjunctionable) {
3315                                    query.append(WHERE_AND);
3316                            }
3317    
3318                            if (name == null) {
3319                                    query.append(_FINDER_COLUMN_C_N_S_P_R_NAME_4);
3320                            }
3321                            else if (name.equals(StringPool.BLANK)) {
3322                                    query.append(_FINDER_COLUMN_C_N_S_P_R_NAME_6);
3323                            }
3324                            else {
3325                                    query.append(_FINDER_COLUMN_C_N_S_P_R_NAME_5);
3326                            }
3327    
3328                            conjunctionable = true;
3329    
3330                            if (conjunctionable) {
3331                                    query.append(WHERE_AND);
3332                            }
3333    
3334                            query.append(_FINDER_COLUMN_C_N_S_P_R_SCOPE_5);
3335    
3336                            conjunctionable = true;
3337    
3338                            if (conjunctionable) {
3339                                    query.append(WHERE_AND);
3340                            }
3341    
3342                            if (primKey == null) {
3343                                    query.append(_FINDER_COLUMN_C_N_S_P_R_PRIMKEY_4);
3344                            }
3345                            else if (primKey.equals(StringPool.BLANK)) {
3346                                    query.append(_FINDER_COLUMN_C_N_S_P_R_PRIMKEY_6);
3347                            }
3348                            else {
3349                                    query.append(_FINDER_COLUMN_C_N_S_P_R_PRIMKEY_5);
3350                            }
3351    
3352                            conjunctionable = true;
3353    
3354                            if ((roleIds == null) || (roleIds.length > 0)) {
3355                                    if (conjunctionable) {
3356                                            query.append(WHERE_AND);
3357                                    }
3358    
3359                                    query.append(StringPool.OPEN_PARENTHESIS);
3360    
3361                                    for (int i = 0; i < roleIds.length; i++) {
3362                                            query.append(_FINDER_COLUMN_C_N_S_P_R_ROLEID_5);
3363    
3364                                            if ((i + 1) < roleIds.length) {
3365                                                    query.append(WHERE_OR);
3366                                            }
3367                                    }
3368    
3369                                    query.append(StringPool.CLOSE_PARENTHESIS);
3370    
3371                                    conjunctionable = true;
3372                            }
3373    
3374                            if (orderByComparator != null) {
3375                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
3376                                            orderByComparator);
3377                            }
3378                            else
3379                             if (pagination) {
3380                                    query.append(ResourcePermissionModelImpl.ORDER_BY_JPQL);
3381                            }
3382    
3383                            String sql = query.toString();
3384    
3385                            Session session = null;
3386    
3387                            try {
3388                                    session = openSession();
3389    
3390                                    Query q = session.createQuery(sql);
3391    
3392                                    QueryPos qPos = QueryPos.getInstance(q);
3393    
3394                                    qPos.add(companyId);
3395    
3396                                    if (name != null) {
3397                                            qPos.add(name);
3398                                    }
3399    
3400                                    qPos.add(scope);
3401    
3402                                    if (primKey != null) {
3403                                            qPos.add(primKey);
3404                                    }
3405    
3406                                    if (roleIds != null) {
3407                                            qPos.add(roleIds);
3408                                    }
3409    
3410                                    if (!pagination) {
3411                                            list = (List<ResourcePermission>)QueryUtil.list(q,
3412                                                            getDialect(), start, end, false);
3413    
3414                                            Collections.sort(list);
3415    
3416                                            list = new UnmodifiableList<ResourcePermission>(list);
3417                                    }
3418                                    else {
3419                                            list = (List<ResourcePermission>)QueryUtil.list(q,
3420                                                            getDialect(), start, end);
3421                                    }
3422    
3423                                    cacheResult(list);
3424    
3425                                    FinderCacheUtil.putResult(FINDER_PATH_WITH_PAGINATION_FIND_BY_C_N_S_P_R,
3426                                            finderArgs, list);
3427                            }
3428                            catch (Exception e) {
3429                                    FinderCacheUtil.removeResult(FINDER_PATH_WITH_PAGINATION_FIND_BY_C_N_S_P_R,
3430                                            finderArgs);
3431    
3432                                    throw processException(e);
3433                            }
3434                            finally {
3435                                    closeSession(session);
3436                            }
3437                    }
3438    
3439                    return list;
3440            }
3441    
3442            /**
3443             * Returns the resource permission where companyId = &#63; and name = &#63; and scope = &#63; and primKey = &#63; and roleId = &#63; or throws a {@link com.liferay.portal.NoSuchResourcePermissionException} if it could not be found.
3444             *
3445             * @param companyId the company ID
3446             * @param name the name
3447             * @param scope the scope
3448             * @param primKey the prim key
3449             * @param roleId the role ID
3450             * @return the matching resource permission
3451             * @throws com.liferay.portal.NoSuchResourcePermissionException if a matching resource permission could not be found
3452             * @throws SystemException if a system exception occurred
3453             */
3454            public ResourcePermission findByC_N_S_P_R(long companyId, String name,
3455                    int scope, String primKey, long roleId)
3456                    throws NoSuchResourcePermissionException, SystemException {
3457                    ResourcePermission resourcePermission = fetchByC_N_S_P_R(companyId,
3458                                    name, scope, primKey, roleId);
3459    
3460                    if (resourcePermission == null) {
3461                            StringBundler msg = new StringBundler(12);
3462    
3463                            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
3464    
3465                            msg.append("companyId=");
3466                            msg.append(companyId);
3467    
3468                            msg.append(", name=");
3469                            msg.append(name);
3470    
3471                            msg.append(", scope=");
3472                            msg.append(scope);
3473    
3474                            msg.append(", primKey=");
3475                            msg.append(primKey);
3476    
3477                            msg.append(", roleId=");
3478                            msg.append(roleId);
3479    
3480                            msg.append(StringPool.CLOSE_CURLY_BRACE);
3481    
3482                            if (_log.isWarnEnabled()) {
3483                                    _log.warn(msg.toString());
3484                            }
3485    
3486                            throw new NoSuchResourcePermissionException(msg.toString());
3487                    }
3488    
3489                    return resourcePermission;
3490            }
3491    
3492            /**
3493             * 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.
3494             *
3495             * @param companyId the company ID
3496             * @param name the name
3497             * @param scope the scope
3498             * @param primKey the prim key
3499             * @param roleId the role ID
3500             * @return the matching resource permission, or <code>null</code> if a matching resource permission could not be found
3501             * @throws SystemException if a system exception occurred
3502             */
3503            public ResourcePermission fetchByC_N_S_P_R(long companyId, String name,
3504                    int scope, String primKey, long roleId) throws SystemException {
3505                    return fetchByC_N_S_P_R(companyId, name, scope, primKey, roleId, true);
3506            }
3507    
3508            /**
3509             * 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.
3510             *
3511             * @param companyId the company ID
3512             * @param name the name
3513             * @param scope the scope
3514             * @param primKey the prim key
3515             * @param roleId the role ID
3516             * @param retrieveFromCache whether to use the finder cache
3517             * @return the matching resource permission, or <code>null</code> if a matching resource permission could not be found
3518             * @throws SystemException if a system exception occurred
3519             */
3520            public ResourcePermission fetchByC_N_S_P_R(long companyId, String name,
3521                    int scope, String primKey, long roleId, boolean retrieveFromCache)
3522                    throws SystemException {
3523                    Object[] finderArgs = new Object[] {
3524                                    companyId, name, scope, primKey, roleId
3525                            };
3526    
3527                    Object result = null;
3528    
3529                    if (retrieveFromCache) {
3530                            result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_C_N_S_P_R,
3531                                            finderArgs, this);
3532                    }
3533    
3534                    if (result instanceof ResourcePermission) {
3535                            ResourcePermission resourcePermission = (ResourcePermission)result;
3536    
3537                            if ((companyId != resourcePermission.getCompanyId()) ||
3538                                            !Validator.equals(name, resourcePermission.getName()) ||
3539                                            (scope != resourcePermission.getScope()) ||
3540                                            !Validator.equals(primKey, resourcePermission.getPrimKey()) ||
3541                                            (roleId != resourcePermission.getRoleId())) {
3542                                    result = null;
3543                            }
3544                    }
3545    
3546                    if (result == null) {
3547                            StringBundler query = new StringBundler(7);
3548    
3549                            query.append(_SQL_SELECT_RESOURCEPERMISSION_WHERE);
3550    
3551                            query.append(_FINDER_COLUMN_C_N_S_P_R_COMPANYID_2);
3552    
3553                            boolean bindName = false;
3554    
3555                            if (name == null) {
3556                                    query.append(_FINDER_COLUMN_C_N_S_P_R_NAME_1);
3557                            }
3558                            else if (name.equals(StringPool.BLANK)) {
3559                                    query.append(_FINDER_COLUMN_C_N_S_P_R_NAME_3);
3560                            }
3561                            else {
3562                                    bindName = true;
3563    
3564                                    query.append(_FINDER_COLUMN_C_N_S_P_R_NAME_2);
3565                            }
3566    
3567                            query.append(_FINDER_COLUMN_C_N_S_P_R_SCOPE_2);
3568    
3569                            boolean bindPrimKey = false;
3570    
3571                            if (primKey == null) {
3572                                    query.append(_FINDER_COLUMN_C_N_S_P_R_PRIMKEY_1);
3573                            }
3574                            else if (primKey.equals(StringPool.BLANK)) {
3575                                    query.append(_FINDER_COLUMN_C_N_S_P_R_PRIMKEY_3);
3576                            }
3577                            else {
3578                                    bindPrimKey = true;
3579    
3580                                    query.append(_FINDER_COLUMN_C_N_S_P_R_PRIMKEY_2);
3581                            }
3582    
3583                            query.append(_FINDER_COLUMN_C_N_S_P_R_ROLEID_2);
3584    
3585                            String sql = query.toString();
3586    
3587                            Session session = null;
3588    
3589                            try {
3590                                    session = openSession();
3591    
3592                                    Query q = session.createQuery(sql);
3593    
3594                                    QueryPos qPos = QueryPos.getInstance(q);
3595    
3596                                    qPos.add(companyId);
3597    
3598                                    if (bindName) {
3599                                            qPos.add(name);
3600                                    }
3601    
3602                                    qPos.add(scope);
3603    
3604                                    if (bindPrimKey) {
3605                                            qPos.add(primKey);
3606                                    }
3607    
3608                                    qPos.add(roleId);
3609    
3610                                    List<ResourcePermission> list = q.list();
3611    
3612                                    if (list.isEmpty()) {
3613                                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_N_S_P_R,
3614                                                    finderArgs, list);
3615                                    }
3616                                    else {
3617                                            ResourcePermission resourcePermission = list.get(0);
3618    
3619                                            result = resourcePermission;
3620    
3621                                            cacheResult(resourcePermission);
3622    
3623                                            if ((resourcePermission.getCompanyId() != companyId) ||
3624                                                            (resourcePermission.getName() == null) ||
3625                                                            !resourcePermission.getName().equals(name) ||
3626                                                            (resourcePermission.getScope() != scope) ||
3627                                                            (resourcePermission.getPrimKey() == null) ||
3628                                                            !resourcePermission.getPrimKey().equals(primKey) ||
3629                                                            (resourcePermission.getRoleId() != roleId)) {
3630                                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_N_S_P_R,
3631                                                            finderArgs, resourcePermission);
3632                                            }
3633                                    }
3634                            }
3635                            catch (Exception e) {
3636                                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_N_S_P_R,
3637                                            finderArgs);
3638    
3639                                    throw processException(e);
3640                            }
3641                            finally {
3642                                    closeSession(session);
3643                            }
3644                    }
3645    
3646                    if (result instanceof List<?>) {
3647                            return null;
3648                    }
3649                    else {
3650                            return (ResourcePermission)result;
3651                    }
3652            }
3653    
3654            /**
3655             * Removes the resource permission where companyId = &#63; and name = &#63; and scope = &#63; and primKey = &#63; and roleId = &#63; from the database.
3656             *
3657             * @param companyId the company ID
3658             * @param name the name
3659             * @param scope the scope
3660             * @param primKey the prim key
3661             * @param roleId the role ID
3662             * @return the resource permission that was removed
3663             * @throws SystemException if a system exception occurred
3664             */
3665            public ResourcePermission removeByC_N_S_P_R(long companyId, String name,
3666                    int scope, String primKey, long roleId)
3667                    throws NoSuchResourcePermissionException, SystemException {
3668                    ResourcePermission resourcePermission = findByC_N_S_P_R(companyId,
3669                                    name, scope, primKey, roleId);
3670    
3671                    return remove(resourcePermission);
3672            }
3673    
3674            /**
3675             * Returns the number of resource permissions where companyId = &#63; and name = &#63; and scope = &#63; and primKey = &#63; and roleId = &#63;.
3676             *
3677             * @param companyId the company ID
3678             * @param name the name
3679             * @param scope the scope
3680             * @param primKey the prim key
3681             * @param roleId the role ID
3682             * @return the number of matching resource permissions
3683             * @throws SystemException if a system exception occurred
3684             */
3685            public int countByC_N_S_P_R(long companyId, String name, int scope,
3686                    String primKey, long roleId) throws SystemException {
3687                    FinderPath finderPath = FINDER_PATH_COUNT_BY_C_N_S_P_R;
3688    
3689                    Object[] finderArgs = new Object[] {
3690                                    companyId, name, scope, primKey, roleId
3691                            };
3692    
3693                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
3694                                    this);
3695    
3696                    if (count == null) {
3697                            StringBundler query = new StringBundler(6);
3698    
3699                            query.append(_SQL_COUNT_RESOURCEPERMISSION_WHERE);
3700    
3701                            query.append(_FINDER_COLUMN_C_N_S_P_R_COMPANYID_2);
3702    
3703                            boolean bindName = false;
3704    
3705                            if (name == null) {
3706                                    query.append(_FINDER_COLUMN_C_N_S_P_R_NAME_1);
3707                            }
3708                            else if (name.equals(StringPool.BLANK)) {
3709                                    query.append(_FINDER_COLUMN_C_N_S_P_R_NAME_3);
3710                            }
3711                            else {
3712                                    bindName = true;
3713    
3714                                    query.append(_FINDER_COLUMN_C_N_S_P_R_NAME_2);
3715                            }
3716    
3717                            query.append(_FINDER_COLUMN_C_N_S_P_R_SCOPE_2);
3718    
3719                            boolean bindPrimKey = false;
3720    
3721                            if (primKey == null) {
3722                                    query.append(_FINDER_COLUMN_C_N_S_P_R_PRIMKEY_1);
3723                            }
3724                            else if (primKey.equals(StringPool.BLANK)) {
3725                                    query.append(_FINDER_COLUMN_C_N_S_P_R_PRIMKEY_3);
3726                            }
3727                            else {
3728                                    bindPrimKey = true;
3729    
3730                                    query.append(_FINDER_COLUMN_C_N_S_P_R_PRIMKEY_2);
3731                            }
3732    
3733                            query.append(_FINDER_COLUMN_C_N_S_P_R_ROLEID_2);
3734    
3735                            String sql = query.toString();
3736    
3737                            Session session = null;
3738    
3739                            try {
3740                                    session = openSession();
3741    
3742                                    Query q = session.createQuery(sql);
3743    
3744                                    QueryPos qPos = QueryPos.getInstance(q);
3745    
3746                                    qPos.add(companyId);
3747    
3748                                    if (bindName) {
3749                                            qPos.add(name);
3750                                    }
3751    
3752                                    qPos.add(scope);
3753    
3754                                    if (bindPrimKey) {
3755                                            qPos.add(primKey);
3756                                    }
3757    
3758                                    qPos.add(roleId);
3759    
3760                                    count = (Long)q.uniqueResult();
3761    
3762                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
3763                            }
3764                            catch (Exception e) {
3765                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
3766    
3767                                    throw processException(e);
3768                            }
3769                            finally {
3770                                    closeSession(session);
3771                            }
3772                    }
3773    
3774                    return count.intValue();
3775            }
3776    
3777            /**
3778             * Returns the number of resource permissions where companyId = &#63; and name = &#63; and scope = &#63; and primKey = &#63; and roleId = any &#63;.
3779             *
3780             * @param companyId the company ID
3781             * @param name the name
3782             * @param scope the scope
3783             * @param primKey the prim key
3784             * @param roleIds the role IDs
3785             * @return the number of matching resource permissions
3786             * @throws SystemException if a system exception occurred
3787             */
3788            public int countByC_N_S_P_R(long companyId, String name, int scope,
3789                    String primKey, long[] roleIds) throws SystemException {
3790                    Object[] finderArgs = new Object[] {
3791                                    companyId, name, scope, primKey, StringUtil.merge(roleIds)
3792                            };
3793    
3794                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_WITH_PAGINATION_COUNT_BY_C_N_S_P_R,
3795                                    finderArgs, this);
3796    
3797                    if (count == null) {
3798                            StringBundler query = new StringBundler();
3799    
3800                            query.append(_SQL_COUNT_RESOURCEPERMISSION_WHERE);
3801    
3802                            boolean conjunctionable = false;
3803    
3804                            if (conjunctionable) {
3805                                    query.append(WHERE_AND);
3806                            }
3807    
3808                            query.append(_FINDER_COLUMN_C_N_S_P_R_COMPANYID_5);
3809    
3810                            conjunctionable = true;
3811    
3812                            if (conjunctionable) {
3813                                    query.append(WHERE_AND);
3814                            }
3815    
3816                            if (name == null) {
3817                                    query.append(_FINDER_COLUMN_C_N_S_P_R_NAME_4);
3818                            }
3819                            else if (name.equals(StringPool.BLANK)) {
3820                                    query.append(_FINDER_COLUMN_C_N_S_P_R_NAME_6);
3821                            }
3822                            else {
3823                                    query.append(_FINDER_COLUMN_C_N_S_P_R_NAME_5);
3824                            }
3825    
3826                            conjunctionable = true;
3827    
3828                            if (conjunctionable) {
3829                                    query.append(WHERE_AND);
3830                            }
3831    
3832                            query.append(_FINDER_COLUMN_C_N_S_P_R_SCOPE_5);
3833    
3834                            conjunctionable = true;
3835    
3836                            if (conjunctionable) {
3837                                    query.append(WHERE_AND);
3838                            }
3839    
3840                            if (primKey == null) {
3841                                    query.append(_FINDER_COLUMN_C_N_S_P_R_PRIMKEY_4);
3842                            }
3843                            else if (primKey.equals(StringPool.BLANK)) {
3844                                    query.append(_FINDER_COLUMN_C_N_S_P_R_PRIMKEY_6);
3845                            }
3846                            else {
3847                                    query.append(_FINDER_COLUMN_C_N_S_P_R_PRIMKEY_5);
3848                            }
3849    
3850                            conjunctionable = true;
3851    
3852                            if ((roleIds == null) || (roleIds.length > 0)) {
3853                                    if (conjunctionable) {
3854                                            query.append(WHERE_AND);
3855                                    }
3856    
3857                                    query.append(StringPool.OPEN_PARENTHESIS);
3858    
3859                                    for (int i = 0; i < roleIds.length; i++) {
3860                                            query.append(_FINDER_COLUMN_C_N_S_P_R_ROLEID_5);
3861    
3862                                            if ((i + 1) < roleIds.length) {
3863                                                    query.append(WHERE_OR);
3864                                            }
3865                                    }
3866    
3867                                    query.append(StringPool.CLOSE_PARENTHESIS);
3868    
3869                                    conjunctionable = true;
3870                            }
3871    
3872                            String sql = query.toString();
3873    
3874                            Session session = null;
3875    
3876                            try {
3877                                    session = openSession();
3878    
3879                                    Query q = session.createQuery(sql);
3880    
3881                                    QueryPos qPos = QueryPos.getInstance(q);
3882    
3883                                    qPos.add(companyId);
3884    
3885                                    if (name != null) {
3886                                            qPos.add(name);
3887                                    }
3888    
3889                                    qPos.add(scope);
3890    
3891                                    if (primKey != null) {
3892                                            qPos.add(primKey);
3893                                    }
3894    
3895                                    if (roleIds != null) {
3896                                            qPos.add(roleIds);
3897                                    }
3898    
3899                                    count = (Long)q.uniqueResult();
3900    
3901                                    FinderCacheUtil.putResult(FINDER_PATH_WITH_PAGINATION_COUNT_BY_C_N_S_P_R,
3902                                            finderArgs, count);
3903                            }
3904                            catch (Exception e) {
3905                                    FinderCacheUtil.removeResult(FINDER_PATH_WITH_PAGINATION_COUNT_BY_C_N_S_P_R,
3906                                            finderArgs);
3907    
3908                                    throw processException(e);
3909                            }
3910                            finally {
3911                                    closeSession(session);
3912                            }
3913                    }
3914    
3915                    return count.intValue();
3916            }
3917    
3918            private static final String _FINDER_COLUMN_C_N_S_P_R_COMPANYID_2 = "resourcePermission.companyId = ? AND ";
3919            private static final String _FINDER_COLUMN_C_N_S_P_R_COMPANYID_5 = "(" +
3920                    removeConjunction(_FINDER_COLUMN_C_N_S_P_R_COMPANYID_2) + ")";
3921            private static final String _FINDER_COLUMN_C_N_S_P_R_NAME_1 = "resourcePermission.name IS NULL AND ";
3922            private static final String _FINDER_COLUMN_C_N_S_P_R_NAME_2 = "resourcePermission.name = ? AND ";
3923            private static final String _FINDER_COLUMN_C_N_S_P_R_NAME_3 = "(resourcePermission.name IS NULL OR resourcePermission.name = '') AND ";
3924            private static final String _FINDER_COLUMN_C_N_S_P_R_NAME_4 = "(" +
3925                    removeConjunction(_FINDER_COLUMN_C_N_S_P_R_NAME_1) + ")";
3926            private static final String _FINDER_COLUMN_C_N_S_P_R_NAME_5 = "(" +
3927                    removeConjunction(_FINDER_COLUMN_C_N_S_P_R_NAME_2) + ")";
3928            private static final String _FINDER_COLUMN_C_N_S_P_R_NAME_6 = "(" +
3929                    removeConjunction(_FINDER_COLUMN_C_N_S_P_R_NAME_3) + ")";
3930            private static final String _FINDER_COLUMN_C_N_S_P_R_SCOPE_2 = "resourcePermission.scope = ? AND ";
3931            private static final String _FINDER_COLUMN_C_N_S_P_R_SCOPE_5 = "(" +
3932                    removeConjunction(_FINDER_COLUMN_C_N_S_P_R_SCOPE_2) + ")";
3933            private static final String _FINDER_COLUMN_C_N_S_P_R_PRIMKEY_1 = "resourcePermission.primKey IS NULL AND ";
3934            private static final String _FINDER_COLUMN_C_N_S_P_R_PRIMKEY_2 = "resourcePermission.primKey = ? AND ";
3935            private static final String _FINDER_COLUMN_C_N_S_P_R_PRIMKEY_3 = "(resourcePermission.primKey IS NULL OR resourcePermission.primKey = '') AND ";
3936            private static final String _FINDER_COLUMN_C_N_S_P_R_PRIMKEY_4 = "(" +
3937                    removeConjunction(_FINDER_COLUMN_C_N_S_P_R_PRIMKEY_1) + ")";
3938            private static final String _FINDER_COLUMN_C_N_S_P_R_PRIMKEY_5 = "(" +
3939                    removeConjunction(_FINDER_COLUMN_C_N_S_P_R_PRIMKEY_2) + ")";
3940            private static final String _FINDER_COLUMN_C_N_S_P_R_PRIMKEY_6 = "(" +
3941                    removeConjunction(_FINDER_COLUMN_C_N_S_P_R_PRIMKEY_3) + ")";
3942            private static final String _FINDER_COLUMN_C_N_S_P_R_ROLEID_2 = "resourcePermission.roleId = ?";
3943            private static final String _FINDER_COLUMN_C_N_S_P_R_ROLEID_5 = "(" +
3944                    removeConjunction(_FINDER_COLUMN_C_N_S_P_R_ROLEID_2) + ")";
3945    
3946            /**
3947             * Caches the resource permission in the entity cache if it is enabled.
3948             *
3949             * @param resourcePermission the resource permission
3950             */
3951            public void cacheResult(ResourcePermission resourcePermission) {
3952                    EntityCacheUtil.putResult(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
3953                            ResourcePermissionImpl.class, resourcePermission.getPrimaryKey(),
3954                            resourcePermission);
3955    
3956                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_N_S_P_R,
3957                            new Object[] {
3958                                    resourcePermission.getCompanyId(), resourcePermission.getName(),
3959                                    resourcePermission.getScope(), resourcePermission.getPrimKey(),
3960                                    resourcePermission.getRoleId()
3961                            }, resourcePermission);
3962    
3963                    resourcePermission.resetOriginalValues();
3964            }
3965    
3966            /**
3967             * Caches the resource permissions in the entity cache if it is enabled.
3968             *
3969             * @param resourcePermissions the resource permissions
3970             */
3971            public void cacheResult(List<ResourcePermission> resourcePermissions) {
3972                    for (ResourcePermission resourcePermission : resourcePermissions) {
3973                            if (EntityCacheUtil.getResult(
3974                                                    ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
3975                                                    ResourcePermissionImpl.class,
3976                                                    resourcePermission.getPrimaryKey()) == null) {
3977                                    cacheResult(resourcePermission);
3978                            }
3979                            else {
3980                                    resourcePermission.resetOriginalValues();
3981                            }
3982                    }
3983            }
3984    
3985            /**
3986             * Clears the cache for all resource permissions.
3987             *
3988             * <p>
3989             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
3990             * </p>
3991             */
3992            @Override
3993            public void clearCache() {
3994                    if (_HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE) {
3995                            CacheRegistryUtil.clear(ResourcePermissionImpl.class.getName());
3996                    }
3997    
3998                    EntityCacheUtil.clearCache(ResourcePermissionImpl.class.getName());
3999    
4000                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
4001                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
4002                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
4003            }
4004    
4005            /**
4006             * Clears the cache for the resource permission.
4007             *
4008             * <p>
4009             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
4010             * </p>
4011             */
4012            @Override
4013            public void clearCache(ResourcePermission resourcePermission) {
4014                    EntityCacheUtil.removeResult(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
4015                            ResourcePermissionImpl.class, resourcePermission.getPrimaryKey());
4016    
4017                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
4018                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
4019    
4020                    clearUniqueFindersCache(resourcePermission);
4021            }
4022    
4023            @Override
4024            public void clearCache(List<ResourcePermission> resourcePermissions) {
4025                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
4026                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
4027    
4028                    for (ResourcePermission resourcePermission : resourcePermissions) {
4029                            EntityCacheUtil.removeResult(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
4030                                    ResourcePermissionImpl.class, resourcePermission.getPrimaryKey());
4031    
4032                            clearUniqueFindersCache(resourcePermission);
4033                    }
4034            }
4035    
4036            protected void cacheUniqueFindersCache(
4037                    ResourcePermission resourcePermission) {
4038                    if (resourcePermission.isNew()) {
4039                            Object[] args = new Object[] {
4040                                            resourcePermission.getCompanyId(),
4041                                            resourcePermission.getName(), resourcePermission.getScope(),
4042                                            resourcePermission.getPrimKey(),
4043                                            resourcePermission.getRoleId()
4044                                    };
4045    
4046                            FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_N_S_P_R, args,
4047                                    Long.valueOf(1));
4048                            FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_N_S_P_R, args,
4049                                    resourcePermission);
4050                    }
4051                    else {
4052                            ResourcePermissionModelImpl resourcePermissionModelImpl = (ResourcePermissionModelImpl)resourcePermission;
4053    
4054                            if ((resourcePermissionModelImpl.getColumnBitmask() &
4055                                            FINDER_PATH_FETCH_BY_C_N_S_P_R.getColumnBitmask()) != 0) {
4056                                    Object[] args = new Object[] {
4057                                                    resourcePermission.getCompanyId(),
4058                                                    resourcePermission.getName(),
4059                                                    resourcePermission.getScope(),
4060                                                    resourcePermission.getPrimKey(),
4061                                                    resourcePermission.getRoleId()
4062                                            };
4063    
4064                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_N_S_P_R, args,
4065                                            Long.valueOf(1));
4066                                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_N_S_P_R, args,
4067                                            resourcePermission);
4068                            }
4069                    }
4070            }
4071    
4072            protected void clearUniqueFindersCache(
4073                    ResourcePermission resourcePermission) {
4074                    ResourcePermissionModelImpl resourcePermissionModelImpl = (ResourcePermissionModelImpl)resourcePermission;
4075    
4076                    Object[] args = new Object[] {
4077                                    resourcePermission.getCompanyId(), resourcePermission.getName(),
4078                                    resourcePermission.getScope(), resourcePermission.getPrimKey(),
4079                                    resourcePermission.getRoleId()
4080                            };
4081    
4082                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_C_N_S_P_R, args);
4083                    FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_N_S_P_R, args);
4084    
4085                    if ((resourcePermissionModelImpl.getColumnBitmask() &
4086                                    FINDER_PATH_FETCH_BY_C_N_S_P_R.getColumnBitmask()) != 0) {
4087                            args = new Object[] {
4088                                            resourcePermissionModelImpl.getOriginalCompanyId(),
4089                                            resourcePermissionModelImpl.getOriginalName(),
4090                                            resourcePermissionModelImpl.getOriginalScope(),
4091                                            resourcePermissionModelImpl.getOriginalPrimKey(),
4092                                            resourcePermissionModelImpl.getOriginalRoleId()
4093                                    };
4094    
4095                            FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_C_N_S_P_R, args);
4096                            FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_N_S_P_R, args);
4097                    }
4098            }
4099    
4100            /**
4101             * Creates a new resource permission with the primary key. Does not add the resource permission to the database.
4102             *
4103             * @param resourcePermissionId the primary key for the new resource permission
4104             * @return the new resource permission
4105             */
4106            public ResourcePermission create(long resourcePermissionId) {
4107                    ResourcePermission resourcePermission = new ResourcePermissionImpl();
4108    
4109                    resourcePermission.setNew(true);
4110                    resourcePermission.setPrimaryKey(resourcePermissionId);
4111    
4112                    return resourcePermission;
4113            }
4114    
4115            /**
4116             * Removes the resource permission with the primary key from the database. Also notifies the appropriate model listeners.
4117             *
4118             * @param resourcePermissionId the primary key of the resource permission
4119             * @return the resource permission that was removed
4120             * @throws com.liferay.portal.NoSuchResourcePermissionException if a resource permission with the primary key could not be found
4121             * @throws SystemException if a system exception occurred
4122             */
4123            public ResourcePermission remove(long resourcePermissionId)
4124                    throws NoSuchResourcePermissionException, SystemException {
4125                    return remove((Serializable)resourcePermissionId);
4126            }
4127    
4128            /**
4129             * Removes the resource permission with the primary key from the database. Also notifies the appropriate model listeners.
4130             *
4131             * @param primaryKey the primary key of the resource permission
4132             * @return the resource permission that was removed
4133             * @throws com.liferay.portal.NoSuchResourcePermissionException if a resource permission with the primary key could not be found
4134             * @throws SystemException if a system exception occurred
4135             */
4136            @Override
4137            public ResourcePermission remove(Serializable primaryKey)
4138                    throws NoSuchResourcePermissionException, SystemException {
4139                    Session session = null;
4140    
4141                    try {
4142                            session = openSession();
4143    
4144                            ResourcePermission resourcePermission = (ResourcePermission)session.get(ResourcePermissionImpl.class,
4145                                            primaryKey);
4146    
4147                            if (resourcePermission == null) {
4148                                    if (_log.isWarnEnabled()) {
4149                                            _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
4150                                    }
4151    
4152                                    throw new NoSuchResourcePermissionException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
4153                                            primaryKey);
4154                            }
4155    
4156                            return remove(resourcePermission);
4157                    }
4158                    catch (NoSuchResourcePermissionException nsee) {
4159                            throw nsee;
4160                    }
4161                    catch (Exception e) {
4162                            throw processException(e);
4163                    }
4164                    finally {
4165                            closeSession(session);
4166                    }
4167            }
4168    
4169            @Override
4170            protected ResourcePermission removeImpl(
4171                    ResourcePermission resourcePermission) throws SystemException {
4172                    resourcePermission = toUnwrappedModel(resourcePermission);
4173    
4174                    Session session = null;
4175    
4176                    try {
4177                            session = openSession();
4178    
4179                            if (!session.contains(resourcePermission)) {
4180                                    resourcePermission = (ResourcePermission)session.get(ResourcePermissionImpl.class,
4181                                                    resourcePermission.getPrimaryKeyObj());
4182                            }
4183    
4184                            if (resourcePermission != null) {
4185                                    session.delete(resourcePermission);
4186                            }
4187                    }
4188                    catch (Exception e) {
4189                            throw processException(e);
4190                    }
4191                    finally {
4192                            closeSession(session);
4193                    }
4194    
4195                    if (resourcePermission != null) {
4196                            clearCache(resourcePermission);
4197                    }
4198    
4199                    return resourcePermission;
4200            }
4201    
4202            @Override
4203            public ResourcePermission updateImpl(
4204                    com.liferay.portal.model.ResourcePermission resourcePermission)
4205                    throws SystemException {
4206                    resourcePermission = toUnwrappedModel(resourcePermission);
4207    
4208                    boolean isNew = resourcePermission.isNew();
4209    
4210                    ResourcePermissionModelImpl resourcePermissionModelImpl = (ResourcePermissionModelImpl)resourcePermission;
4211    
4212                    Session session = null;
4213    
4214                    try {
4215                            session = openSession();
4216    
4217                            if (resourcePermission.isNew()) {
4218                                    session.save(resourcePermission);
4219    
4220                                    resourcePermission.setNew(false);
4221                            }
4222                            else {
4223                                    session.merge(resourcePermission);
4224                            }
4225                    }
4226                    catch (Exception e) {
4227                            throw processException(e);
4228                    }
4229                    finally {
4230                            closeSession(session);
4231                    }
4232    
4233                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
4234    
4235                    if (isNew || !ResourcePermissionModelImpl.COLUMN_BITMASK_ENABLED) {
4236                            FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
4237                    }
4238    
4239                    else {
4240                            if ((resourcePermissionModelImpl.getColumnBitmask() &
4241                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_SCOPE.getColumnBitmask()) != 0) {
4242                                    Object[] args = new Object[] {
4243                                                    resourcePermissionModelImpl.getOriginalScope()
4244                                            };
4245    
4246                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_SCOPE, args);
4247                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_SCOPE,
4248                                            args);
4249    
4250                                    args = new Object[] { resourcePermissionModelImpl.getScope() };
4251    
4252                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_SCOPE, args);
4253                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_SCOPE,
4254                                            args);
4255                            }
4256    
4257                            if ((resourcePermissionModelImpl.getColumnBitmask() &
4258                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_ROLEID.getColumnBitmask()) != 0) {
4259                                    Object[] args = new Object[] {
4260                                                    resourcePermissionModelImpl.getOriginalRoleId()
4261                                            };
4262    
4263                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_ROLEID, args);
4264                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_ROLEID,
4265                                            args);
4266    
4267                                    args = new Object[] { resourcePermissionModelImpl.getRoleId() };
4268    
4269                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_ROLEID, args);
4270                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_ROLEID,
4271                                            args);
4272                            }
4273    
4274                            if ((resourcePermissionModelImpl.getColumnBitmask() &
4275                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_N_S.getColumnBitmask()) != 0) {
4276                                    Object[] args = new Object[] {
4277                                                    resourcePermissionModelImpl.getOriginalCompanyId(),
4278                                                    resourcePermissionModelImpl.getOriginalName(),
4279                                                    resourcePermissionModelImpl.getOriginalScope()
4280                                            };
4281    
4282                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_C_N_S, args);
4283                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_N_S,
4284                                            args);
4285    
4286                                    args = new Object[] {
4287                                                    resourcePermissionModelImpl.getCompanyId(),
4288                                                    resourcePermissionModelImpl.getName(),
4289                                                    resourcePermissionModelImpl.getScope()
4290                                            };
4291    
4292                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_C_N_S, args);
4293                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_N_S,
4294                                            args);
4295                            }
4296    
4297                            if ((resourcePermissionModelImpl.getColumnBitmask() &
4298                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_N_S_P.getColumnBitmask()) != 0) {
4299                                    Object[] args = new Object[] {
4300                                                    resourcePermissionModelImpl.getOriginalCompanyId(),
4301                                                    resourcePermissionModelImpl.getOriginalName(),
4302                                                    resourcePermissionModelImpl.getOriginalScope(),
4303                                                    resourcePermissionModelImpl.getOriginalPrimKey()
4304                                            };
4305    
4306                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_C_N_S_P, args);
4307                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_N_S_P,
4308                                            args);
4309    
4310                                    args = new Object[] {
4311                                                    resourcePermissionModelImpl.getCompanyId(),
4312                                                    resourcePermissionModelImpl.getName(),
4313                                                    resourcePermissionModelImpl.getScope(),
4314                                                    resourcePermissionModelImpl.getPrimKey()
4315                                            };
4316    
4317                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_C_N_S_P, args);
4318                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_N_S_P,
4319                                            args);
4320                            }
4321    
4322                            if ((resourcePermissionModelImpl.getColumnBitmask() &
4323                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_N_S_P_R.getColumnBitmask()) != 0) {
4324                                    Object[] args = new Object[] {
4325                                                    resourcePermissionModelImpl.getOriginalCompanyId(),
4326                                                    resourcePermissionModelImpl.getOriginalName(),
4327                                                    resourcePermissionModelImpl.getOriginalScope(),
4328                                                    resourcePermissionModelImpl.getOriginalPrimKey(),
4329                                                    resourcePermissionModelImpl.getOriginalRoleId()
4330                                            };
4331    
4332                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_C_N_S_P_R,
4333                                            args);
4334                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_N_S_P_R,
4335                                            args);
4336    
4337                                    args = new Object[] {
4338                                                    resourcePermissionModelImpl.getCompanyId(),
4339                                                    resourcePermissionModelImpl.getName(),
4340                                                    resourcePermissionModelImpl.getScope(),
4341                                                    resourcePermissionModelImpl.getPrimKey(),
4342                                                    resourcePermissionModelImpl.getRoleId()
4343                                            };
4344    
4345                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_C_N_S_P_R,
4346                                            args);
4347                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_C_N_S_P_R,
4348                                            args);
4349                            }
4350                    }
4351    
4352                    EntityCacheUtil.putResult(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
4353                            ResourcePermissionImpl.class, resourcePermission.getPrimaryKey(),
4354                            resourcePermission);
4355    
4356                    clearUniqueFindersCache(resourcePermission);
4357                    cacheUniqueFindersCache(resourcePermission);
4358    
4359                    return resourcePermission;
4360            }
4361    
4362            protected ResourcePermission toUnwrappedModel(
4363                    ResourcePermission resourcePermission) {
4364                    if (resourcePermission instanceof ResourcePermissionImpl) {
4365                            return resourcePermission;
4366                    }
4367    
4368                    ResourcePermissionImpl resourcePermissionImpl = new ResourcePermissionImpl();
4369    
4370                    resourcePermissionImpl.setNew(resourcePermission.isNew());
4371                    resourcePermissionImpl.setPrimaryKey(resourcePermission.getPrimaryKey());
4372    
4373                    resourcePermissionImpl.setResourcePermissionId(resourcePermission.getResourcePermissionId());
4374                    resourcePermissionImpl.setCompanyId(resourcePermission.getCompanyId());
4375                    resourcePermissionImpl.setName(resourcePermission.getName());
4376                    resourcePermissionImpl.setScope(resourcePermission.getScope());
4377                    resourcePermissionImpl.setPrimKey(resourcePermission.getPrimKey());
4378                    resourcePermissionImpl.setRoleId(resourcePermission.getRoleId());
4379                    resourcePermissionImpl.setOwnerId(resourcePermission.getOwnerId());
4380                    resourcePermissionImpl.setActionIds(resourcePermission.getActionIds());
4381    
4382                    return resourcePermissionImpl;
4383            }
4384    
4385            /**
4386             * Returns the resource permission with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
4387             *
4388             * @param primaryKey the primary key of the resource permission
4389             * @return the resource permission
4390             * @throws com.liferay.portal.NoSuchResourcePermissionException if a resource permission with the primary key could not be found
4391             * @throws SystemException if a system exception occurred
4392             */
4393            @Override
4394            public ResourcePermission findByPrimaryKey(Serializable primaryKey)
4395                    throws NoSuchResourcePermissionException, SystemException {
4396                    ResourcePermission resourcePermission = fetchByPrimaryKey(primaryKey);
4397    
4398                    if (resourcePermission == null) {
4399                            if (_log.isWarnEnabled()) {
4400                                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
4401                            }
4402    
4403                            throw new NoSuchResourcePermissionException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
4404                                    primaryKey);
4405                    }
4406    
4407                    return resourcePermission;
4408            }
4409    
4410            /**
4411             * Returns the resource permission with the primary key or throws a {@link com.liferay.portal.NoSuchResourcePermissionException} if it could not be found.
4412             *
4413             * @param resourcePermissionId the primary key of the resource permission
4414             * @return the resource permission
4415             * @throws com.liferay.portal.NoSuchResourcePermissionException if a resource permission with the primary key could not be found
4416             * @throws SystemException if a system exception occurred
4417             */
4418            public ResourcePermission findByPrimaryKey(long resourcePermissionId)
4419                    throws NoSuchResourcePermissionException, SystemException {
4420                    return findByPrimaryKey((Serializable)resourcePermissionId);
4421            }
4422    
4423            /**
4424             * Returns the resource permission with the primary key or returns <code>null</code> if it could not be found.
4425             *
4426             * @param primaryKey the primary key of the resource permission
4427             * @return the resource permission, or <code>null</code> if a resource permission with the primary key could not be found
4428             * @throws SystemException if a system exception occurred
4429             */
4430            @Override
4431            public ResourcePermission fetchByPrimaryKey(Serializable primaryKey)
4432                    throws SystemException {
4433                    ResourcePermission resourcePermission = (ResourcePermission)EntityCacheUtil.getResult(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
4434                                    ResourcePermissionImpl.class, primaryKey);
4435    
4436                    if (resourcePermission == _nullResourcePermission) {
4437                            return null;
4438                    }
4439    
4440                    if (resourcePermission == null) {
4441                            Session session = null;
4442    
4443                            try {
4444                                    session = openSession();
4445    
4446                                    resourcePermission = (ResourcePermission)session.get(ResourcePermissionImpl.class,
4447                                                    primaryKey);
4448    
4449                                    if (resourcePermission != null) {
4450                                            cacheResult(resourcePermission);
4451                                    }
4452                                    else {
4453                                            EntityCacheUtil.putResult(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
4454                                                    ResourcePermissionImpl.class, primaryKey,
4455                                                    _nullResourcePermission);
4456                                    }
4457                            }
4458                            catch (Exception e) {
4459                                    EntityCacheUtil.removeResult(ResourcePermissionModelImpl.ENTITY_CACHE_ENABLED,
4460                                            ResourcePermissionImpl.class, primaryKey);
4461    
4462                                    throw processException(e);
4463                            }
4464                            finally {
4465                                    closeSession(session);
4466                            }
4467                    }
4468    
4469                    return resourcePermission;
4470            }
4471    
4472            /**
4473             * Returns the resource permission with the primary key or returns <code>null</code> if it could not be found.
4474             *
4475             * @param resourcePermissionId the primary key of the resource permission
4476             * @return the resource permission, or <code>null</code> if a resource permission with the primary key could not be found
4477             * @throws SystemException if a system exception occurred
4478             */
4479            public ResourcePermission fetchByPrimaryKey(long resourcePermissionId)
4480                    throws SystemException {
4481                    return fetchByPrimaryKey((Serializable)resourcePermissionId);
4482            }
4483    
4484            /**
4485             * Returns all the resource permissions.
4486             *
4487             * @return the resource permissions
4488             * @throws SystemException if a system exception occurred
4489             */
4490            public List<ResourcePermission> findAll() throws SystemException {
4491                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
4492            }
4493    
4494            /**
4495             * Returns a range of all the resource permissions.
4496             *
4497             * <p>
4498             * 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 com.liferay.portal.kernel.dao.orm.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 com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portal.model.impl.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.
4499             * </p>
4500             *
4501             * @param start the lower bound of the range of resource permissions
4502             * @param end the upper bound of the range of resource permissions (not inclusive)
4503             * @return the range of resource permissions
4504             * @throws SystemException if a system exception occurred
4505             */
4506            public List<ResourcePermission> findAll(int start, int end)
4507                    throws SystemException {
4508                    return findAll(start, end, null);
4509            }
4510    
4511            /**
4512             * Returns an ordered range of all the resource permissions.
4513             *
4514             * <p>
4515             * 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 com.liferay.portal.kernel.dao.orm.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 com.liferay.portal.kernel.dao.orm.QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link com.liferay.portal.model.impl.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.
4516             * </p>
4517             *
4518             * @param start the lower bound of the range of resource permissions
4519             * @param end the upper bound of the range of resource permissions (not inclusive)
4520             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
4521             * @return the ordered range of resource permissions
4522             * @throws SystemException if a system exception occurred
4523             */
4524            public List<ResourcePermission> findAll(int start, int end,
4525                    OrderByComparator orderByComparator) throws SystemException {
4526                    boolean pagination = true;
4527                    FinderPath finderPath = null;
4528                    Object[] finderArgs = null;
4529    
4530                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
4531                                    (orderByComparator == null)) {
4532                            pagination = false;
4533                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
4534                            finderArgs = FINDER_ARGS_EMPTY;
4535                    }
4536                    else {
4537                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
4538                            finderArgs = new Object[] { start, end, orderByComparator };
4539                    }
4540    
4541                    List<ResourcePermission> list = (List<ResourcePermission>)FinderCacheUtil.getResult(finderPath,
4542                                    finderArgs, this);
4543    
4544                    if (list == null) {
4545                            StringBundler query = null;
4546                            String sql = null;
4547    
4548                            if (orderByComparator != null) {
4549                                    query = new StringBundler(2 +
4550                                                    (orderByComparator.getOrderByFields().length * 3));
4551    
4552                                    query.append(_SQL_SELECT_RESOURCEPERMISSION);
4553    
4554                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
4555                                            orderByComparator);
4556    
4557                                    sql = query.toString();
4558                            }
4559                            else {
4560                                    sql = _SQL_SELECT_RESOURCEPERMISSION;
4561    
4562                                    if (pagination) {
4563                                            sql = sql.concat(ResourcePermissionModelImpl.ORDER_BY_JPQL);
4564                                    }
4565                            }
4566    
4567                            Session session = null;
4568    
4569                            try {
4570                                    session = openSession();
4571    
4572                                    Query q = session.createQuery(sql);
4573    
4574                                    if (!pagination) {
4575                                            list = (List<ResourcePermission>)QueryUtil.list(q,
4576                                                            getDialect(), start, end, false);
4577    
4578                                            Collections.sort(list);
4579    
4580                                            list = new UnmodifiableList<ResourcePermission>(list);
4581                                    }
4582                                    else {
4583                                            list = (List<ResourcePermission>)QueryUtil.list(q,
4584                                                            getDialect(), start, end);
4585                                    }
4586    
4587                                    cacheResult(list);
4588    
4589                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
4590                            }
4591                            catch (Exception e) {
4592                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
4593    
4594                                    throw processException(e);
4595                            }
4596                            finally {
4597                                    closeSession(session);
4598                            }
4599                    }
4600    
4601                    return list;
4602            }
4603    
4604            /**
4605             * Removes all the resource permissions from the database.
4606             *
4607             * @throws SystemException if a system exception occurred
4608             */
4609            public void removeAll() throws SystemException {
4610                    for (ResourcePermission resourcePermission : findAll()) {
4611                            remove(resourcePermission);
4612                    }
4613            }
4614    
4615            /**
4616             * Returns the number of resource permissions.
4617             *
4618             * @return the number of resource permissions
4619             * @throws SystemException if a system exception occurred
4620             */
4621            public int countAll() throws SystemException {
4622                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
4623                                    FINDER_ARGS_EMPTY, this);
4624    
4625                    if (count == null) {
4626                            Session session = null;
4627    
4628                            try {
4629                                    session = openSession();
4630    
4631                                    Query q = session.createQuery(_SQL_COUNT_RESOURCEPERMISSION);
4632    
4633                                    count = (Long)q.uniqueResult();
4634    
4635                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
4636                                            FINDER_ARGS_EMPTY, count);
4637                            }
4638                            catch (Exception e) {
4639                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_ALL,
4640                                            FINDER_ARGS_EMPTY);
4641    
4642                                    throw processException(e);
4643                            }
4644                            finally {
4645                                    closeSession(session);
4646                            }
4647                    }
4648    
4649                    return count.intValue();
4650            }
4651    
4652            /**
4653             * Initializes the resource permission persistence.
4654             */
4655            public void afterPropertiesSet() {
4656                    String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
4657                                            com.liferay.portal.util.PropsUtil.get(
4658                                                    "value.object.listener.com.liferay.portal.model.ResourcePermission")));
4659    
4660                    if (listenerClassNames.length > 0) {
4661                            try {
4662                                    List<ModelListener<ResourcePermission>> listenersList = new ArrayList<ModelListener<ResourcePermission>>();
4663    
4664                                    for (String listenerClassName : listenerClassNames) {
4665                                            listenersList.add((ModelListener<ResourcePermission>)InstanceFactory.newInstance(
4666                                                            listenerClassName));
4667                                    }
4668    
4669                                    listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
4670                            }
4671                            catch (Exception e) {
4672                                    _log.error(e);
4673                            }
4674                    }
4675            }
4676    
4677            public void destroy() {
4678                    EntityCacheUtil.removeCache(ResourcePermissionImpl.class.getName());
4679                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
4680                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
4681                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
4682            }
4683    
4684            private static final String _SQL_SELECT_RESOURCEPERMISSION = "SELECT resourcePermission FROM ResourcePermission resourcePermission";
4685            private static final String _SQL_SELECT_RESOURCEPERMISSION_WHERE = "SELECT resourcePermission FROM ResourcePermission resourcePermission WHERE ";
4686            private static final String _SQL_COUNT_RESOURCEPERMISSION = "SELECT COUNT(resourcePermission) FROM ResourcePermission resourcePermission";
4687            private static final String _SQL_COUNT_RESOURCEPERMISSION_WHERE = "SELECT COUNT(resourcePermission) FROM ResourcePermission resourcePermission WHERE ";
4688            private static final String _ORDER_BY_ENTITY_ALIAS = "resourcePermission.";
4689            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No ResourcePermission exists with the primary key ";
4690            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No ResourcePermission exists with the key {";
4691            private static final boolean _HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE = com.liferay.portal.util.PropsValues.HIBERNATE_CACHE_USE_SECOND_LEVEL_CACHE;
4692            private static Log _log = LogFactoryUtil.getLog(ResourcePermissionPersistenceImpl.class);
4693            private static ResourcePermission _nullResourcePermission = new ResourcePermissionImpl() {
4694                            @Override
4695                            public Object clone() {
4696                                    return this;
4697                            }
4698    
4699                            @Override
4700                            public CacheModel<ResourcePermission> toCacheModel() {
4701                                    return _nullResourcePermissionCacheModel;
4702                            }
4703                    };
4704    
4705            private static CacheModel<ResourcePermission> _nullResourcePermissionCacheModel =
4706                    new CacheModel<ResourcePermission>() {
4707                            public ResourcePermission toEntityModel() {
4708                                    return _nullResourcePermission;
4709                            }
4710                    };
4711    }