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