001    /**
002     * Copyright (c) 2000-present Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.service.persistence.impl;
016    
017    import aQute.bnd.annotation.ProviderType;
018    
019    import com.liferay.portal.NoSuchUserGroupRoleException;
020    import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
021    import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
022    import com.liferay.portal.kernel.dao.orm.FinderPath;
023    import com.liferay.portal.kernel.dao.orm.Query;
024    import com.liferay.portal.kernel.dao.orm.QueryPos;
025    import com.liferay.portal.kernel.dao.orm.QueryUtil;
026    import com.liferay.portal.kernel.dao.orm.Session;
027    import com.liferay.portal.kernel.log.Log;
028    import com.liferay.portal.kernel.log.LogFactoryUtil;
029    import com.liferay.portal.kernel.util.OrderByComparator;
030    import com.liferay.portal.kernel.util.StringBundler;
031    import com.liferay.portal.kernel.util.StringPool;
032    import com.liferay.portal.model.CacheModel;
033    import com.liferay.portal.model.MVCCModel;
034    import com.liferay.portal.model.UserGroupRole;
035    import com.liferay.portal.model.impl.UserGroupRoleImpl;
036    import com.liferay.portal.model.impl.UserGroupRoleModelImpl;
037    import com.liferay.portal.service.persistence.UserGroupRolePK;
038    import com.liferay.portal.service.persistence.UserGroupRolePersistence;
039    
040    import java.io.Serializable;
041    
042    import java.util.Collections;
043    import java.util.HashMap;
044    import java.util.List;
045    import java.util.Map;
046    import java.util.Set;
047    
048    /**
049     * The persistence implementation for the user group role service.
050     *
051     * <p>
052     * Caching information and settings can be found in <code>portal.properties</code>
053     * </p>
054     *
055     * @author Brian Wing Shun Chan
056     * @see UserGroupRolePersistence
057     * @see com.liferay.portal.service.persistence.UserGroupRoleUtil
058     * @generated
059     */
060    @ProviderType
061    public class UserGroupRolePersistenceImpl extends BasePersistenceImpl<UserGroupRole>
062            implements UserGroupRolePersistence {
063            /*
064             * NOTE FOR DEVELOPERS:
065             *
066             * Never modify or reference this class directly. Always use {@link UserGroupRoleUtil} to access the user group role persistence. Modify <code>service.xml</code> and rerun ServiceBuilder to regenerate this class.
067             */
068            public static final String FINDER_CLASS_NAME_ENTITY = UserGroupRoleImpl.class.getName();
069            public static final String FINDER_CLASS_NAME_LIST_WITH_PAGINATION = FINDER_CLASS_NAME_ENTITY +
070                    ".List1";
071            public static final String FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION = FINDER_CLASS_NAME_ENTITY +
072                    ".List2";
073            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_ALL = new FinderPath(UserGroupRoleModelImpl.ENTITY_CACHE_ENABLED,
074                            UserGroupRoleModelImpl.FINDER_CACHE_ENABLED,
075                            UserGroupRoleImpl.class, FINDER_CLASS_NAME_LIST_WITH_PAGINATION,
076                            "findAll", new String[0]);
077            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL = new FinderPath(UserGroupRoleModelImpl.ENTITY_CACHE_ENABLED,
078                            UserGroupRoleModelImpl.FINDER_CACHE_ENABLED,
079                            UserGroupRoleImpl.class, FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION,
080                            "findAll", new String[0]);
081            public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(UserGroupRoleModelImpl.ENTITY_CACHE_ENABLED,
082                            UserGroupRoleModelImpl.FINDER_CACHE_ENABLED, Long.class,
083                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countAll", new String[0]);
084            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_USERID = new FinderPath(UserGroupRoleModelImpl.ENTITY_CACHE_ENABLED,
085                            UserGroupRoleModelImpl.FINDER_CACHE_ENABLED,
086                            UserGroupRoleImpl.class, FINDER_CLASS_NAME_LIST_WITH_PAGINATION,
087                            "findByUserId",
088                            new String[] {
089                                    Long.class.getName(),
090                                    
091                            Integer.class.getName(), Integer.class.getName(),
092                                    OrderByComparator.class.getName()
093                            });
094            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_USERID =
095                    new FinderPath(UserGroupRoleModelImpl.ENTITY_CACHE_ENABLED,
096                            UserGroupRoleModelImpl.FINDER_CACHE_ENABLED,
097                            UserGroupRoleImpl.class, FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION,
098                            "findByUserId", new String[] { Long.class.getName() },
099                            UserGroupRoleModelImpl.USERID_COLUMN_BITMASK);
100            public static final FinderPath FINDER_PATH_COUNT_BY_USERID = new FinderPath(UserGroupRoleModelImpl.ENTITY_CACHE_ENABLED,
101                            UserGroupRoleModelImpl.FINDER_CACHE_ENABLED, Long.class,
102                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByUserId",
103                            new String[] { Long.class.getName() });
104    
105            /**
106             * Returns all the user group roles where userId = &#63;.
107             *
108             * @param userId the user ID
109             * @return the matching user group roles
110             */
111            @Override
112            public List<UserGroupRole> findByUserId(long userId) {
113                    return findByUserId(userId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
114            }
115    
116            /**
117             * Returns a range of all the user group roles where userId = &#63;.
118             *
119             * <p>
120             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link UserGroupRoleModelImpl}. 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.
121             * </p>
122             *
123             * @param userId the user ID
124             * @param start the lower bound of the range of user group roles
125             * @param end the upper bound of the range of user group roles (not inclusive)
126             * @return the range of matching user group roles
127             */
128            @Override
129            public List<UserGroupRole> findByUserId(long userId, int start, int end) {
130                    return findByUserId(userId, start, end, null);
131            }
132    
133            /**
134             * Returns an ordered range of all the user group roles where userId = &#63;.
135             *
136             * <p>
137             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link UserGroupRoleModelImpl}. If both <code>orderByComparator</code> and pagination are absent, for performance reasons, the query will not have an ORDER BY clause and the returned result set will be sorted on by the primary key in an ascending order.
138             * </p>
139             *
140             * @param userId the user ID
141             * @param start the lower bound of the range of user group roles
142             * @param end the upper bound of the range of user group roles (not inclusive)
143             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
144             * @return the ordered range of matching user group roles
145             */
146            @Override
147            public List<UserGroupRole> findByUserId(long userId, int start, int end,
148                    OrderByComparator<UserGroupRole> orderByComparator) {
149                    boolean pagination = true;
150                    FinderPath finderPath = null;
151                    Object[] finderArgs = null;
152    
153                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
154                                    (orderByComparator == null)) {
155                            pagination = false;
156                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_USERID;
157                            finderArgs = new Object[] { userId };
158                    }
159                    else {
160                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_USERID;
161                            finderArgs = new Object[] { userId, start, end, orderByComparator };
162                    }
163    
164                    List<UserGroupRole> list = (List<UserGroupRole>)FinderCacheUtil.getResult(finderPath,
165                                    finderArgs, this);
166    
167                    if ((list != null) && !list.isEmpty()) {
168                            for (UserGroupRole userGroupRole : list) {
169                                    if ((userId != userGroupRole.getUserId())) {
170                                            list = null;
171    
172                                            break;
173                                    }
174                            }
175                    }
176    
177                    if (list == null) {
178                            StringBundler query = null;
179    
180                            if (orderByComparator != null) {
181                                    query = new StringBundler(3 +
182                                                    (orderByComparator.getOrderByFields().length * 3));
183                            }
184                            else {
185                                    query = new StringBundler(3);
186                            }
187    
188                            query.append(_SQL_SELECT_USERGROUPROLE_WHERE);
189    
190                            query.append(_FINDER_COLUMN_USERID_USERID_2);
191    
192                            if (orderByComparator != null) {
193                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
194                                            orderByComparator);
195                            }
196                            else
197                             if (pagination) {
198                                    query.append(UserGroupRoleModelImpl.ORDER_BY_JPQL);
199                            }
200    
201                            String sql = query.toString();
202    
203                            Session session = null;
204    
205                            try {
206                                    session = openSession();
207    
208                                    Query q = session.createQuery(sql);
209    
210                                    QueryPos qPos = QueryPos.getInstance(q);
211    
212                                    qPos.add(userId);
213    
214                                    if (!pagination) {
215                                            list = (List<UserGroupRole>)QueryUtil.list(q, getDialect(),
216                                                            start, end, false);
217    
218                                            Collections.sort(list);
219    
220                                            list = Collections.unmodifiableList(list);
221                                    }
222                                    else {
223                                            list = (List<UserGroupRole>)QueryUtil.list(q, getDialect(),
224                                                            start, end);
225                                    }
226    
227                                    cacheResult(list);
228    
229                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
230                            }
231                            catch (Exception e) {
232                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
233    
234                                    throw processException(e);
235                            }
236                            finally {
237                                    closeSession(session);
238                            }
239                    }
240    
241                    return list;
242            }
243    
244            /**
245             * Returns the first user group role in the ordered set where userId = &#63;.
246             *
247             * @param userId the user ID
248             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
249             * @return the first matching user group role
250             * @throws NoSuchUserGroupRoleException if a matching user group role could not be found
251             */
252            @Override
253            public UserGroupRole findByUserId_First(long userId,
254                    OrderByComparator<UserGroupRole> orderByComparator)
255                    throws NoSuchUserGroupRoleException {
256                    UserGroupRole userGroupRole = fetchByUserId_First(userId,
257                                    orderByComparator);
258    
259                    if (userGroupRole != null) {
260                            return userGroupRole;
261                    }
262    
263                    StringBundler msg = new StringBundler(4);
264    
265                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
266    
267                    msg.append("userId=");
268                    msg.append(userId);
269    
270                    msg.append(StringPool.CLOSE_CURLY_BRACE);
271    
272                    throw new NoSuchUserGroupRoleException(msg.toString());
273            }
274    
275            /**
276             * Returns the first user group role in the ordered set where userId = &#63;.
277             *
278             * @param userId the user ID
279             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
280             * @return the first matching user group role, or <code>null</code> if a matching user group role could not be found
281             */
282            @Override
283            public UserGroupRole fetchByUserId_First(long userId,
284                    OrderByComparator<UserGroupRole> orderByComparator) {
285                    List<UserGroupRole> list = findByUserId(userId, 0, 1, orderByComparator);
286    
287                    if (!list.isEmpty()) {
288                            return list.get(0);
289                    }
290    
291                    return null;
292            }
293    
294            /**
295             * Returns the last user group role in the ordered set where userId = &#63;.
296             *
297             * @param userId the user ID
298             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
299             * @return the last matching user group role
300             * @throws NoSuchUserGroupRoleException if a matching user group role could not be found
301             */
302            @Override
303            public UserGroupRole findByUserId_Last(long userId,
304                    OrderByComparator<UserGroupRole> orderByComparator)
305                    throws NoSuchUserGroupRoleException {
306                    UserGroupRole userGroupRole = fetchByUserId_Last(userId,
307                                    orderByComparator);
308    
309                    if (userGroupRole != null) {
310                            return userGroupRole;
311                    }
312    
313                    StringBundler msg = new StringBundler(4);
314    
315                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
316    
317                    msg.append("userId=");
318                    msg.append(userId);
319    
320                    msg.append(StringPool.CLOSE_CURLY_BRACE);
321    
322                    throw new NoSuchUserGroupRoleException(msg.toString());
323            }
324    
325            /**
326             * Returns the last user group role in the ordered set where userId = &#63;.
327             *
328             * @param userId the user ID
329             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
330             * @return the last matching user group role, or <code>null</code> if a matching user group role could not be found
331             */
332            @Override
333            public UserGroupRole fetchByUserId_Last(long userId,
334                    OrderByComparator<UserGroupRole> orderByComparator) {
335                    int count = countByUserId(userId);
336    
337                    if (count == 0) {
338                            return null;
339                    }
340    
341                    List<UserGroupRole> list = findByUserId(userId, count - 1, count,
342                                    orderByComparator);
343    
344                    if (!list.isEmpty()) {
345                            return list.get(0);
346                    }
347    
348                    return null;
349            }
350    
351            /**
352             * Returns the user group roles before and after the current user group role in the ordered set where userId = &#63;.
353             *
354             * @param userGroupRolePK the primary key of the current user group role
355             * @param userId the user ID
356             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
357             * @return the previous, current, and next user group role
358             * @throws NoSuchUserGroupRoleException if a user group role with the primary key could not be found
359             */
360            @Override
361            public UserGroupRole[] findByUserId_PrevAndNext(
362                    UserGroupRolePK userGroupRolePK, long userId,
363                    OrderByComparator<UserGroupRole> orderByComparator)
364                    throws NoSuchUserGroupRoleException {
365                    UserGroupRole userGroupRole = findByPrimaryKey(userGroupRolePK);
366    
367                    Session session = null;
368    
369                    try {
370                            session = openSession();
371    
372                            UserGroupRole[] array = new UserGroupRoleImpl[3];
373    
374                            array[0] = getByUserId_PrevAndNext(session, userGroupRole, userId,
375                                            orderByComparator, true);
376    
377                            array[1] = userGroupRole;
378    
379                            array[2] = getByUserId_PrevAndNext(session, userGroupRole, userId,
380                                            orderByComparator, false);
381    
382                            return array;
383                    }
384                    catch (Exception e) {
385                            throw processException(e);
386                    }
387                    finally {
388                            closeSession(session);
389                    }
390            }
391    
392            protected UserGroupRole getByUserId_PrevAndNext(Session session,
393                    UserGroupRole userGroupRole, long userId,
394                    OrderByComparator<UserGroupRole> orderByComparator, boolean previous) {
395                    StringBundler query = null;
396    
397                    if (orderByComparator != null) {
398                            query = new StringBundler(6 +
399                                            (orderByComparator.getOrderByFields().length * 6));
400                    }
401                    else {
402                            query = new StringBundler(3);
403                    }
404    
405                    query.append(_SQL_SELECT_USERGROUPROLE_WHERE);
406    
407                    query.append(_FINDER_COLUMN_USERID_USERID_2);
408    
409                    if (orderByComparator != null) {
410                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
411    
412                            if (orderByConditionFields.length > 0) {
413                                    query.append(WHERE_AND);
414                            }
415    
416                            for (int i = 0; i < orderByConditionFields.length; i++) {
417                                    query.append(_ORDER_BY_ENTITY_ALIAS);
418                                    query.append(orderByConditionFields[i]);
419    
420                                    if ((i + 1) < orderByConditionFields.length) {
421                                            if (orderByComparator.isAscending() ^ previous) {
422                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
423                                            }
424                                            else {
425                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
426                                            }
427                                    }
428                                    else {
429                                            if (orderByComparator.isAscending() ^ previous) {
430                                                    query.append(WHERE_GREATER_THAN);
431                                            }
432                                            else {
433                                                    query.append(WHERE_LESSER_THAN);
434                                            }
435                                    }
436                            }
437    
438                            query.append(ORDER_BY_CLAUSE);
439    
440                            String[] orderByFields = orderByComparator.getOrderByFields();
441    
442                            for (int i = 0; i < orderByFields.length; i++) {
443                                    query.append(_ORDER_BY_ENTITY_ALIAS);
444                                    query.append(orderByFields[i]);
445    
446                                    if ((i + 1) < orderByFields.length) {
447                                            if (orderByComparator.isAscending() ^ previous) {
448                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
449                                            }
450                                            else {
451                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
452                                            }
453                                    }
454                                    else {
455                                            if (orderByComparator.isAscending() ^ previous) {
456                                                    query.append(ORDER_BY_ASC);
457                                            }
458                                            else {
459                                                    query.append(ORDER_BY_DESC);
460                                            }
461                                    }
462                            }
463                    }
464                    else {
465                            query.append(UserGroupRoleModelImpl.ORDER_BY_JPQL);
466                    }
467    
468                    String sql = query.toString();
469    
470                    Query q = session.createQuery(sql);
471    
472                    q.setFirstResult(0);
473                    q.setMaxResults(2);
474    
475                    QueryPos qPos = QueryPos.getInstance(q);
476    
477                    qPos.add(userId);
478    
479                    if (orderByComparator != null) {
480                            Object[] values = orderByComparator.getOrderByConditionValues(userGroupRole);
481    
482                            for (Object value : values) {
483                                    qPos.add(value);
484                            }
485                    }
486    
487                    List<UserGroupRole> list = q.list();
488    
489                    if (list.size() == 2) {
490                            return list.get(1);
491                    }
492                    else {
493                            return null;
494                    }
495            }
496    
497            /**
498             * Removes all the user group roles where userId = &#63; from the database.
499             *
500             * @param userId the user ID
501             */
502            @Override
503            public void removeByUserId(long userId) {
504                    for (UserGroupRole userGroupRole : findByUserId(userId,
505                                    QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
506                            remove(userGroupRole);
507                    }
508            }
509    
510            /**
511             * Returns the number of user group roles where userId = &#63;.
512             *
513             * @param userId the user ID
514             * @return the number of matching user group roles
515             */
516            @Override
517            public int countByUserId(long userId) {
518                    FinderPath finderPath = FINDER_PATH_COUNT_BY_USERID;
519    
520                    Object[] finderArgs = new Object[] { userId };
521    
522                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
523                                    this);
524    
525                    if (count == null) {
526                            StringBundler query = new StringBundler(2);
527    
528                            query.append(_SQL_COUNT_USERGROUPROLE_WHERE);
529    
530                            query.append(_FINDER_COLUMN_USERID_USERID_2);
531    
532                            String sql = query.toString();
533    
534                            Session session = null;
535    
536                            try {
537                                    session = openSession();
538    
539                                    Query q = session.createQuery(sql);
540    
541                                    QueryPos qPos = QueryPos.getInstance(q);
542    
543                                    qPos.add(userId);
544    
545                                    count = (Long)q.uniqueResult();
546    
547                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
548                            }
549                            catch (Exception e) {
550                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
551    
552                                    throw processException(e);
553                            }
554                            finally {
555                                    closeSession(session);
556                            }
557                    }
558    
559                    return count.intValue();
560            }
561    
562            private static final String _FINDER_COLUMN_USERID_USERID_2 = "userGroupRole.id.userId = ?";
563            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_GROUPID = new FinderPath(UserGroupRoleModelImpl.ENTITY_CACHE_ENABLED,
564                            UserGroupRoleModelImpl.FINDER_CACHE_ENABLED,
565                            UserGroupRoleImpl.class, FINDER_CLASS_NAME_LIST_WITH_PAGINATION,
566                            "findByGroupId",
567                            new String[] {
568                                    Long.class.getName(),
569                                    
570                            Integer.class.getName(), Integer.class.getName(),
571                                    OrderByComparator.class.getName()
572                            });
573            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_GROUPID =
574                    new FinderPath(UserGroupRoleModelImpl.ENTITY_CACHE_ENABLED,
575                            UserGroupRoleModelImpl.FINDER_CACHE_ENABLED,
576                            UserGroupRoleImpl.class, FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION,
577                            "findByGroupId", new String[] { Long.class.getName() },
578                            UserGroupRoleModelImpl.GROUPID_COLUMN_BITMASK);
579            public static final FinderPath FINDER_PATH_COUNT_BY_GROUPID = new FinderPath(UserGroupRoleModelImpl.ENTITY_CACHE_ENABLED,
580                            UserGroupRoleModelImpl.FINDER_CACHE_ENABLED, Long.class,
581                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByGroupId",
582                            new String[] { Long.class.getName() });
583    
584            /**
585             * Returns all the user group roles where groupId = &#63;.
586             *
587             * @param groupId the group ID
588             * @return the matching user group roles
589             */
590            @Override
591            public List<UserGroupRole> findByGroupId(long groupId) {
592                    return findByGroupId(groupId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
593            }
594    
595            /**
596             * Returns a range of all the user group roles where groupId = &#63;.
597             *
598             * <p>
599             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link UserGroupRoleModelImpl}. 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.
600             * </p>
601             *
602             * @param groupId the group ID
603             * @param start the lower bound of the range of user group roles
604             * @param end the upper bound of the range of user group roles (not inclusive)
605             * @return the range of matching user group roles
606             */
607            @Override
608            public List<UserGroupRole> findByGroupId(long groupId, int start, int end) {
609                    return findByGroupId(groupId, start, end, null);
610            }
611    
612            /**
613             * Returns an ordered range of all the user group roles where groupId = &#63;.
614             *
615             * <p>
616             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link UserGroupRoleModelImpl}. 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.
617             * </p>
618             *
619             * @param groupId the group ID
620             * @param start the lower bound of the range of user group roles
621             * @param end the upper bound of the range of user group roles (not inclusive)
622             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
623             * @return the ordered range of matching user group roles
624             */
625            @Override
626            public List<UserGroupRole> findByGroupId(long groupId, int start, int end,
627                    OrderByComparator<UserGroupRole> orderByComparator) {
628                    boolean pagination = true;
629                    FinderPath finderPath = null;
630                    Object[] finderArgs = null;
631    
632                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
633                                    (orderByComparator == null)) {
634                            pagination = false;
635                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_GROUPID;
636                            finderArgs = new Object[] { groupId };
637                    }
638                    else {
639                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_GROUPID;
640                            finderArgs = new Object[] { groupId, start, end, orderByComparator };
641                    }
642    
643                    List<UserGroupRole> list = (List<UserGroupRole>)FinderCacheUtil.getResult(finderPath,
644                                    finderArgs, this);
645    
646                    if ((list != null) && !list.isEmpty()) {
647                            for (UserGroupRole userGroupRole : list) {
648                                    if ((groupId != userGroupRole.getGroupId())) {
649                                            list = null;
650    
651                                            break;
652                                    }
653                            }
654                    }
655    
656                    if (list == null) {
657                            StringBundler query = null;
658    
659                            if (orderByComparator != null) {
660                                    query = new StringBundler(3 +
661                                                    (orderByComparator.getOrderByFields().length * 3));
662                            }
663                            else {
664                                    query = new StringBundler(3);
665                            }
666    
667                            query.append(_SQL_SELECT_USERGROUPROLE_WHERE);
668    
669                            query.append(_FINDER_COLUMN_GROUPID_GROUPID_2);
670    
671                            if (orderByComparator != null) {
672                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
673                                            orderByComparator);
674                            }
675                            else
676                             if (pagination) {
677                                    query.append(UserGroupRoleModelImpl.ORDER_BY_JPQL);
678                            }
679    
680                            String sql = query.toString();
681    
682                            Session session = null;
683    
684                            try {
685                                    session = openSession();
686    
687                                    Query q = session.createQuery(sql);
688    
689                                    QueryPos qPos = QueryPos.getInstance(q);
690    
691                                    qPos.add(groupId);
692    
693                                    if (!pagination) {
694                                            list = (List<UserGroupRole>)QueryUtil.list(q, getDialect(),
695                                                            start, end, false);
696    
697                                            Collections.sort(list);
698    
699                                            list = Collections.unmodifiableList(list);
700                                    }
701                                    else {
702                                            list = (List<UserGroupRole>)QueryUtil.list(q, getDialect(),
703                                                            start, end);
704                                    }
705    
706                                    cacheResult(list);
707    
708                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
709                            }
710                            catch (Exception e) {
711                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
712    
713                                    throw processException(e);
714                            }
715                            finally {
716                                    closeSession(session);
717                            }
718                    }
719    
720                    return list;
721            }
722    
723            /**
724             * Returns the first user group role in the ordered set where groupId = &#63;.
725             *
726             * @param groupId the group ID
727             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
728             * @return the first matching user group role
729             * @throws NoSuchUserGroupRoleException if a matching user group role could not be found
730             */
731            @Override
732            public UserGroupRole findByGroupId_First(long groupId,
733                    OrderByComparator<UserGroupRole> orderByComparator)
734                    throws NoSuchUserGroupRoleException {
735                    UserGroupRole userGroupRole = fetchByGroupId_First(groupId,
736                                    orderByComparator);
737    
738                    if (userGroupRole != null) {
739                            return userGroupRole;
740                    }
741    
742                    StringBundler msg = new StringBundler(4);
743    
744                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
745    
746                    msg.append("groupId=");
747                    msg.append(groupId);
748    
749                    msg.append(StringPool.CLOSE_CURLY_BRACE);
750    
751                    throw new NoSuchUserGroupRoleException(msg.toString());
752            }
753    
754            /**
755             * Returns the first user group role in the ordered set where groupId = &#63;.
756             *
757             * @param groupId the group ID
758             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
759             * @return the first matching user group role, or <code>null</code> if a matching user group role could not be found
760             */
761            @Override
762            public UserGroupRole fetchByGroupId_First(long groupId,
763                    OrderByComparator<UserGroupRole> orderByComparator) {
764                    List<UserGroupRole> list = findByGroupId(groupId, 0, 1,
765                                    orderByComparator);
766    
767                    if (!list.isEmpty()) {
768                            return list.get(0);
769                    }
770    
771                    return null;
772            }
773    
774            /**
775             * Returns the last user group role in the ordered set where groupId = &#63;.
776             *
777             * @param groupId the group ID
778             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
779             * @return the last matching user group role
780             * @throws NoSuchUserGroupRoleException if a matching user group role could not be found
781             */
782            @Override
783            public UserGroupRole findByGroupId_Last(long groupId,
784                    OrderByComparator<UserGroupRole> orderByComparator)
785                    throws NoSuchUserGroupRoleException {
786                    UserGroupRole userGroupRole = fetchByGroupId_Last(groupId,
787                                    orderByComparator);
788    
789                    if (userGroupRole != null) {
790                            return userGroupRole;
791                    }
792    
793                    StringBundler msg = new StringBundler(4);
794    
795                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
796    
797                    msg.append("groupId=");
798                    msg.append(groupId);
799    
800                    msg.append(StringPool.CLOSE_CURLY_BRACE);
801    
802                    throw new NoSuchUserGroupRoleException(msg.toString());
803            }
804    
805            /**
806             * Returns the last user group role in the ordered set where groupId = &#63;.
807             *
808             * @param groupId the group ID
809             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
810             * @return the last matching user group role, or <code>null</code> if a matching user group role could not be found
811             */
812            @Override
813            public UserGroupRole fetchByGroupId_Last(long groupId,
814                    OrderByComparator<UserGroupRole> orderByComparator) {
815                    int count = countByGroupId(groupId);
816    
817                    if (count == 0) {
818                            return null;
819                    }
820    
821                    List<UserGroupRole> list = findByGroupId(groupId, count - 1, count,
822                                    orderByComparator);
823    
824                    if (!list.isEmpty()) {
825                            return list.get(0);
826                    }
827    
828                    return null;
829            }
830    
831            /**
832             * Returns the user group roles before and after the current user group role in the ordered set where groupId = &#63;.
833             *
834             * @param userGroupRolePK the primary key of the current user group role
835             * @param groupId the group ID
836             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
837             * @return the previous, current, and next user group role
838             * @throws NoSuchUserGroupRoleException if a user group role with the primary key could not be found
839             */
840            @Override
841            public UserGroupRole[] findByGroupId_PrevAndNext(
842                    UserGroupRolePK userGroupRolePK, long groupId,
843                    OrderByComparator<UserGroupRole> orderByComparator)
844                    throws NoSuchUserGroupRoleException {
845                    UserGroupRole userGroupRole = findByPrimaryKey(userGroupRolePK);
846    
847                    Session session = null;
848    
849                    try {
850                            session = openSession();
851    
852                            UserGroupRole[] array = new UserGroupRoleImpl[3];
853    
854                            array[0] = getByGroupId_PrevAndNext(session, userGroupRole,
855                                            groupId, orderByComparator, true);
856    
857                            array[1] = userGroupRole;
858    
859                            array[2] = getByGroupId_PrevAndNext(session, userGroupRole,
860                                            groupId, orderByComparator, false);
861    
862                            return array;
863                    }
864                    catch (Exception e) {
865                            throw processException(e);
866                    }
867                    finally {
868                            closeSession(session);
869                    }
870            }
871    
872            protected UserGroupRole getByGroupId_PrevAndNext(Session session,
873                    UserGroupRole userGroupRole, long groupId,
874                    OrderByComparator<UserGroupRole> orderByComparator, boolean previous) {
875                    StringBundler query = null;
876    
877                    if (orderByComparator != null) {
878                            query = new StringBundler(6 +
879                                            (orderByComparator.getOrderByFields().length * 6));
880                    }
881                    else {
882                            query = new StringBundler(3);
883                    }
884    
885                    query.append(_SQL_SELECT_USERGROUPROLE_WHERE);
886    
887                    query.append(_FINDER_COLUMN_GROUPID_GROUPID_2);
888    
889                    if (orderByComparator != null) {
890                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
891    
892                            if (orderByConditionFields.length > 0) {
893                                    query.append(WHERE_AND);
894                            }
895    
896                            for (int i = 0; i < orderByConditionFields.length; i++) {
897                                    query.append(_ORDER_BY_ENTITY_ALIAS);
898                                    query.append(orderByConditionFields[i]);
899    
900                                    if ((i + 1) < orderByConditionFields.length) {
901                                            if (orderByComparator.isAscending() ^ previous) {
902                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
903                                            }
904                                            else {
905                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
906                                            }
907                                    }
908                                    else {
909                                            if (orderByComparator.isAscending() ^ previous) {
910                                                    query.append(WHERE_GREATER_THAN);
911                                            }
912                                            else {
913                                                    query.append(WHERE_LESSER_THAN);
914                                            }
915                                    }
916                            }
917    
918                            query.append(ORDER_BY_CLAUSE);
919    
920                            String[] orderByFields = orderByComparator.getOrderByFields();
921    
922                            for (int i = 0; i < orderByFields.length; i++) {
923                                    query.append(_ORDER_BY_ENTITY_ALIAS);
924                                    query.append(orderByFields[i]);
925    
926                                    if ((i + 1) < orderByFields.length) {
927                                            if (orderByComparator.isAscending() ^ previous) {
928                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
929                                            }
930                                            else {
931                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
932                                            }
933                                    }
934                                    else {
935                                            if (orderByComparator.isAscending() ^ previous) {
936                                                    query.append(ORDER_BY_ASC);
937                                            }
938                                            else {
939                                                    query.append(ORDER_BY_DESC);
940                                            }
941                                    }
942                            }
943                    }
944                    else {
945                            query.append(UserGroupRoleModelImpl.ORDER_BY_JPQL);
946                    }
947    
948                    String sql = query.toString();
949    
950                    Query q = session.createQuery(sql);
951    
952                    q.setFirstResult(0);
953                    q.setMaxResults(2);
954    
955                    QueryPos qPos = QueryPos.getInstance(q);
956    
957                    qPos.add(groupId);
958    
959                    if (orderByComparator != null) {
960                            Object[] values = orderByComparator.getOrderByConditionValues(userGroupRole);
961    
962                            for (Object value : values) {
963                                    qPos.add(value);
964                            }
965                    }
966    
967                    List<UserGroupRole> list = q.list();
968    
969                    if (list.size() == 2) {
970                            return list.get(1);
971                    }
972                    else {
973                            return null;
974                    }
975            }
976    
977            /**
978             * Removes all the user group roles where groupId = &#63; from the database.
979             *
980             * @param groupId the group ID
981             */
982            @Override
983            public void removeByGroupId(long groupId) {
984                    for (UserGroupRole userGroupRole : findByGroupId(groupId,
985                                    QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
986                            remove(userGroupRole);
987                    }
988            }
989    
990            /**
991             * Returns the number of user group roles where groupId = &#63;.
992             *
993             * @param groupId the group ID
994             * @return the number of matching user group roles
995             */
996            @Override
997            public int countByGroupId(long groupId) {
998                    FinderPath finderPath = FINDER_PATH_COUNT_BY_GROUPID;
999    
1000                    Object[] finderArgs = new Object[] { groupId };
1001    
1002                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
1003                                    this);
1004    
1005                    if (count == null) {
1006                            StringBundler query = new StringBundler(2);
1007    
1008                            query.append(_SQL_COUNT_USERGROUPROLE_WHERE);
1009    
1010                            query.append(_FINDER_COLUMN_GROUPID_GROUPID_2);
1011    
1012                            String sql = query.toString();
1013    
1014                            Session session = null;
1015    
1016                            try {
1017                                    session = openSession();
1018    
1019                                    Query q = session.createQuery(sql);
1020    
1021                                    QueryPos qPos = QueryPos.getInstance(q);
1022    
1023                                    qPos.add(groupId);
1024    
1025                                    count = (Long)q.uniqueResult();
1026    
1027                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
1028                            }
1029                            catch (Exception e) {
1030                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
1031    
1032                                    throw processException(e);
1033                            }
1034                            finally {
1035                                    closeSession(session);
1036                            }
1037                    }
1038    
1039                    return count.intValue();
1040            }
1041    
1042            private static final String _FINDER_COLUMN_GROUPID_GROUPID_2 = "userGroupRole.id.groupId = ?";
1043            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_ROLEID = new FinderPath(UserGroupRoleModelImpl.ENTITY_CACHE_ENABLED,
1044                            UserGroupRoleModelImpl.FINDER_CACHE_ENABLED,
1045                            UserGroupRoleImpl.class, FINDER_CLASS_NAME_LIST_WITH_PAGINATION,
1046                            "findByRoleId",
1047                            new String[] {
1048                                    Long.class.getName(),
1049                                    
1050                            Integer.class.getName(), Integer.class.getName(),
1051                                    OrderByComparator.class.getName()
1052                            });
1053            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_ROLEID =
1054                    new FinderPath(UserGroupRoleModelImpl.ENTITY_CACHE_ENABLED,
1055                            UserGroupRoleModelImpl.FINDER_CACHE_ENABLED,
1056                            UserGroupRoleImpl.class, FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION,
1057                            "findByRoleId", new String[] { Long.class.getName() },
1058                            UserGroupRoleModelImpl.ROLEID_COLUMN_BITMASK);
1059            public static final FinderPath FINDER_PATH_COUNT_BY_ROLEID = new FinderPath(UserGroupRoleModelImpl.ENTITY_CACHE_ENABLED,
1060                            UserGroupRoleModelImpl.FINDER_CACHE_ENABLED, Long.class,
1061                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByRoleId",
1062                            new String[] { Long.class.getName() });
1063    
1064            /**
1065             * Returns all the user group roles where roleId = &#63;.
1066             *
1067             * @param roleId the role ID
1068             * @return the matching user group roles
1069             */
1070            @Override
1071            public List<UserGroupRole> findByRoleId(long roleId) {
1072                    return findByRoleId(roleId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
1073            }
1074    
1075            /**
1076             * Returns a range of all the user group roles where roleId = &#63;.
1077             *
1078             * <p>
1079             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link UserGroupRoleModelImpl}. 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.
1080             * </p>
1081             *
1082             * @param roleId the role ID
1083             * @param start the lower bound of the range of user group roles
1084             * @param end the upper bound of the range of user group roles (not inclusive)
1085             * @return the range of matching user group roles
1086             */
1087            @Override
1088            public List<UserGroupRole> findByRoleId(long roleId, int start, int end) {
1089                    return findByRoleId(roleId, start, end, null);
1090            }
1091    
1092            /**
1093             * Returns an ordered range of all the user group roles where roleId = &#63;.
1094             *
1095             * <p>
1096             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link UserGroupRoleModelImpl}. 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.
1097             * </p>
1098             *
1099             * @param roleId the role ID
1100             * @param start the lower bound of the range of user group roles
1101             * @param end the upper bound of the range of user group roles (not inclusive)
1102             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
1103             * @return the ordered range of matching user group roles
1104             */
1105            @Override
1106            public List<UserGroupRole> findByRoleId(long roleId, int start, int end,
1107                    OrderByComparator<UserGroupRole> orderByComparator) {
1108                    boolean pagination = true;
1109                    FinderPath finderPath = null;
1110                    Object[] finderArgs = null;
1111    
1112                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
1113                                    (orderByComparator == null)) {
1114                            pagination = false;
1115                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_ROLEID;
1116                            finderArgs = new Object[] { roleId };
1117                    }
1118                    else {
1119                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_ROLEID;
1120                            finderArgs = new Object[] { roleId, start, end, orderByComparator };
1121                    }
1122    
1123                    List<UserGroupRole> list = (List<UserGroupRole>)FinderCacheUtil.getResult(finderPath,
1124                                    finderArgs, this);
1125    
1126                    if ((list != null) && !list.isEmpty()) {
1127                            for (UserGroupRole userGroupRole : list) {
1128                                    if ((roleId != userGroupRole.getRoleId())) {
1129                                            list = null;
1130    
1131                                            break;
1132                                    }
1133                            }
1134                    }
1135    
1136                    if (list == null) {
1137                            StringBundler query = null;
1138    
1139                            if (orderByComparator != null) {
1140                                    query = new StringBundler(3 +
1141                                                    (orderByComparator.getOrderByFields().length * 3));
1142                            }
1143                            else {
1144                                    query = new StringBundler(3);
1145                            }
1146    
1147                            query.append(_SQL_SELECT_USERGROUPROLE_WHERE);
1148    
1149                            query.append(_FINDER_COLUMN_ROLEID_ROLEID_2);
1150    
1151                            if (orderByComparator != null) {
1152                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1153                                            orderByComparator);
1154                            }
1155                            else
1156                             if (pagination) {
1157                                    query.append(UserGroupRoleModelImpl.ORDER_BY_JPQL);
1158                            }
1159    
1160                            String sql = query.toString();
1161    
1162                            Session session = null;
1163    
1164                            try {
1165                                    session = openSession();
1166    
1167                                    Query q = session.createQuery(sql);
1168    
1169                                    QueryPos qPos = QueryPos.getInstance(q);
1170    
1171                                    qPos.add(roleId);
1172    
1173                                    if (!pagination) {
1174                                            list = (List<UserGroupRole>)QueryUtil.list(q, getDialect(),
1175                                                            start, end, false);
1176    
1177                                            Collections.sort(list);
1178    
1179                                            list = Collections.unmodifiableList(list);
1180                                    }
1181                                    else {
1182                                            list = (List<UserGroupRole>)QueryUtil.list(q, getDialect(),
1183                                                            start, end);
1184                                    }
1185    
1186                                    cacheResult(list);
1187    
1188                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
1189                            }
1190                            catch (Exception e) {
1191                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
1192    
1193                                    throw processException(e);
1194                            }
1195                            finally {
1196                                    closeSession(session);
1197                            }
1198                    }
1199    
1200                    return list;
1201            }
1202    
1203            /**
1204             * Returns the first user group role in the ordered set where roleId = &#63;.
1205             *
1206             * @param roleId the role ID
1207             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1208             * @return the first matching user group role
1209             * @throws NoSuchUserGroupRoleException if a matching user group role could not be found
1210             */
1211            @Override
1212            public UserGroupRole findByRoleId_First(long roleId,
1213                    OrderByComparator<UserGroupRole> orderByComparator)
1214                    throws NoSuchUserGroupRoleException {
1215                    UserGroupRole userGroupRole = fetchByRoleId_First(roleId,
1216                                    orderByComparator);
1217    
1218                    if (userGroupRole != null) {
1219                            return userGroupRole;
1220                    }
1221    
1222                    StringBundler msg = new StringBundler(4);
1223    
1224                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1225    
1226                    msg.append("roleId=");
1227                    msg.append(roleId);
1228    
1229                    msg.append(StringPool.CLOSE_CURLY_BRACE);
1230    
1231                    throw new NoSuchUserGroupRoleException(msg.toString());
1232            }
1233    
1234            /**
1235             * Returns the first user group role in the ordered set where roleId = &#63;.
1236             *
1237             * @param roleId the role ID
1238             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1239             * @return the first matching user group role, or <code>null</code> if a matching user group role could not be found
1240             */
1241            @Override
1242            public UserGroupRole fetchByRoleId_First(long roleId,
1243                    OrderByComparator<UserGroupRole> orderByComparator) {
1244                    List<UserGroupRole> list = findByRoleId(roleId, 0, 1, orderByComparator);
1245    
1246                    if (!list.isEmpty()) {
1247                            return list.get(0);
1248                    }
1249    
1250                    return null;
1251            }
1252    
1253            /**
1254             * Returns the last user group role in the ordered set where roleId = &#63;.
1255             *
1256             * @param roleId the role ID
1257             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1258             * @return the last matching user group role
1259             * @throws NoSuchUserGroupRoleException if a matching user group role could not be found
1260             */
1261            @Override
1262            public UserGroupRole findByRoleId_Last(long roleId,
1263                    OrderByComparator<UserGroupRole> orderByComparator)
1264                    throws NoSuchUserGroupRoleException {
1265                    UserGroupRole userGroupRole = fetchByRoleId_Last(roleId,
1266                                    orderByComparator);
1267    
1268                    if (userGroupRole != null) {
1269                            return userGroupRole;
1270                    }
1271    
1272                    StringBundler msg = new StringBundler(4);
1273    
1274                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1275    
1276                    msg.append("roleId=");
1277                    msg.append(roleId);
1278    
1279                    msg.append(StringPool.CLOSE_CURLY_BRACE);
1280    
1281                    throw new NoSuchUserGroupRoleException(msg.toString());
1282            }
1283    
1284            /**
1285             * Returns the last user group role in the ordered set where roleId = &#63;.
1286             *
1287             * @param roleId the role ID
1288             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1289             * @return the last matching user group role, or <code>null</code> if a matching user group role could not be found
1290             */
1291            @Override
1292            public UserGroupRole fetchByRoleId_Last(long roleId,
1293                    OrderByComparator<UserGroupRole> orderByComparator) {
1294                    int count = countByRoleId(roleId);
1295    
1296                    if (count == 0) {
1297                            return null;
1298                    }
1299    
1300                    List<UserGroupRole> list = findByRoleId(roleId, count - 1, count,
1301                                    orderByComparator);
1302    
1303                    if (!list.isEmpty()) {
1304                            return list.get(0);
1305                    }
1306    
1307                    return null;
1308            }
1309    
1310            /**
1311             * Returns the user group roles before and after the current user group role in the ordered set where roleId = &#63;.
1312             *
1313             * @param userGroupRolePK the primary key of the current user group role
1314             * @param roleId the role ID
1315             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1316             * @return the previous, current, and next user group role
1317             * @throws NoSuchUserGroupRoleException if a user group role with the primary key could not be found
1318             */
1319            @Override
1320            public UserGroupRole[] findByRoleId_PrevAndNext(
1321                    UserGroupRolePK userGroupRolePK, long roleId,
1322                    OrderByComparator<UserGroupRole> orderByComparator)
1323                    throws NoSuchUserGroupRoleException {
1324                    UserGroupRole userGroupRole = findByPrimaryKey(userGroupRolePK);
1325    
1326                    Session session = null;
1327    
1328                    try {
1329                            session = openSession();
1330    
1331                            UserGroupRole[] array = new UserGroupRoleImpl[3];
1332    
1333                            array[0] = getByRoleId_PrevAndNext(session, userGroupRole, roleId,
1334                                            orderByComparator, true);
1335    
1336                            array[1] = userGroupRole;
1337    
1338                            array[2] = getByRoleId_PrevAndNext(session, userGroupRole, roleId,
1339                                            orderByComparator, false);
1340    
1341                            return array;
1342                    }
1343                    catch (Exception e) {
1344                            throw processException(e);
1345                    }
1346                    finally {
1347                            closeSession(session);
1348                    }
1349            }
1350    
1351            protected UserGroupRole getByRoleId_PrevAndNext(Session session,
1352                    UserGroupRole userGroupRole, long roleId,
1353                    OrderByComparator<UserGroupRole> orderByComparator, boolean previous) {
1354                    StringBundler query = null;
1355    
1356                    if (orderByComparator != null) {
1357                            query = new StringBundler(6 +
1358                                            (orderByComparator.getOrderByFields().length * 6));
1359                    }
1360                    else {
1361                            query = new StringBundler(3);
1362                    }
1363    
1364                    query.append(_SQL_SELECT_USERGROUPROLE_WHERE);
1365    
1366                    query.append(_FINDER_COLUMN_ROLEID_ROLEID_2);
1367    
1368                    if (orderByComparator != null) {
1369                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
1370    
1371                            if (orderByConditionFields.length > 0) {
1372                                    query.append(WHERE_AND);
1373                            }
1374    
1375                            for (int i = 0; i < orderByConditionFields.length; i++) {
1376                                    query.append(_ORDER_BY_ENTITY_ALIAS);
1377                                    query.append(orderByConditionFields[i]);
1378    
1379                                    if ((i + 1) < orderByConditionFields.length) {
1380                                            if (orderByComparator.isAscending() ^ previous) {
1381                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
1382                                            }
1383                                            else {
1384                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
1385                                            }
1386                                    }
1387                                    else {
1388                                            if (orderByComparator.isAscending() ^ previous) {
1389                                                    query.append(WHERE_GREATER_THAN);
1390                                            }
1391                                            else {
1392                                                    query.append(WHERE_LESSER_THAN);
1393                                            }
1394                                    }
1395                            }
1396    
1397                            query.append(ORDER_BY_CLAUSE);
1398    
1399                            String[] orderByFields = orderByComparator.getOrderByFields();
1400    
1401                            for (int i = 0; i < orderByFields.length; i++) {
1402                                    query.append(_ORDER_BY_ENTITY_ALIAS);
1403                                    query.append(orderByFields[i]);
1404    
1405                                    if ((i + 1) < orderByFields.length) {
1406                                            if (orderByComparator.isAscending() ^ previous) {
1407                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
1408                                            }
1409                                            else {
1410                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
1411                                            }
1412                                    }
1413                                    else {
1414                                            if (orderByComparator.isAscending() ^ previous) {
1415                                                    query.append(ORDER_BY_ASC);
1416                                            }
1417                                            else {
1418                                                    query.append(ORDER_BY_DESC);
1419                                            }
1420                                    }
1421                            }
1422                    }
1423                    else {
1424                            query.append(UserGroupRoleModelImpl.ORDER_BY_JPQL);
1425                    }
1426    
1427                    String sql = query.toString();
1428    
1429                    Query q = session.createQuery(sql);
1430    
1431                    q.setFirstResult(0);
1432                    q.setMaxResults(2);
1433    
1434                    QueryPos qPos = QueryPos.getInstance(q);
1435    
1436                    qPos.add(roleId);
1437    
1438                    if (orderByComparator != null) {
1439                            Object[] values = orderByComparator.getOrderByConditionValues(userGroupRole);
1440    
1441                            for (Object value : values) {
1442                                    qPos.add(value);
1443                            }
1444                    }
1445    
1446                    List<UserGroupRole> list = q.list();
1447    
1448                    if (list.size() == 2) {
1449                            return list.get(1);
1450                    }
1451                    else {
1452                            return null;
1453                    }
1454            }
1455    
1456            /**
1457             * Removes all the user group roles where roleId = &#63; from the database.
1458             *
1459             * @param roleId the role ID
1460             */
1461            @Override
1462            public void removeByRoleId(long roleId) {
1463                    for (UserGroupRole userGroupRole : findByRoleId(roleId,
1464                                    QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
1465                            remove(userGroupRole);
1466                    }
1467            }
1468    
1469            /**
1470             * Returns the number of user group roles where roleId = &#63;.
1471             *
1472             * @param roleId the role ID
1473             * @return the number of matching user group roles
1474             */
1475            @Override
1476            public int countByRoleId(long roleId) {
1477                    FinderPath finderPath = FINDER_PATH_COUNT_BY_ROLEID;
1478    
1479                    Object[] finderArgs = new Object[] { roleId };
1480    
1481                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
1482                                    this);
1483    
1484                    if (count == null) {
1485                            StringBundler query = new StringBundler(2);
1486    
1487                            query.append(_SQL_COUNT_USERGROUPROLE_WHERE);
1488    
1489                            query.append(_FINDER_COLUMN_ROLEID_ROLEID_2);
1490    
1491                            String sql = query.toString();
1492    
1493                            Session session = null;
1494    
1495                            try {
1496                                    session = openSession();
1497    
1498                                    Query q = session.createQuery(sql);
1499    
1500                                    QueryPos qPos = QueryPos.getInstance(q);
1501    
1502                                    qPos.add(roleId);
1503    
1504                                    count = (Long)q.uniqueResult();
1505    
1506                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
1507                            }
1508                            catch (Exception e) {
1509                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
1510    
1511                                    throw processException(e);
1512                            }
1513                            finally {
1514                                    closeSession(session);
1515                            }
1516                    }
1517    
1518                    return count.intValue();
1519            }
1520    
1521            private static final String _FINDER_COLUMN_ROLEID_ROLEID_2 = "userGroupRole.id.roleId = ?";
1522            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_U_G = new FinderPath(UserGroupRoleModelImpl.ENTITY_CACHE_ENABLED,
1523                            UserGroupRoleModelImpl.FINDER_CACHE_ENABLED,
1524                            UserGroupRoleImpl.class, FINDER_CLASS_NAME_LIST_WITH_PAGINATION,
1525                            "findByU_G",
1526                            new String[] {
1527                                    Long.class.getName(), Long.class.getName(),
1528                                    
1529                            Integer.class.getName(), Integer.class.getName(),
1530                                    OrderByComparator.class.getName()
1531                            });
1532            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_U_G = new FinderPath(UserGroupRoleModelImpl.ENTITY_CACHE_ENABLED,
1533                            UserGroupRoleModelImpl.FINDER_CACHE_ENABLED,
1534                            UserGroupRoleImpl.class, FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION,
1535                            "findByU_G",
1536                            new String[] { Long.class.getName(), Long.class.getName() },
1537                            UserGroupRoleModelImpl.USERID_COLUMN_BITMASK |
1538                            UserGroupRoleModelImpl.GROUPID_COLUMN_BITMASK);
1539            public static final FinderPath FINDER_PATH_COUNT_BY_U_G = new FinderPath(UserGroupRoleModelImpl.ENTITY_CACHE_ENABLED,
1540                            UserGroupRoleModelImpl.FINDER_CACHE_ENABLED, Long.class,
1541                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByU_G",
1542                            new String[] { Long.class.getName(), Long.class.getName() });
1543    
1544            /**
1545             * Returns all the user group roles where userId = &#63; and groupId = &#63;.
1546             *
1547             * @param userId the user ID
1548             * @param groupId the group ID
1549             * @return the matching user group roles
1550             */
1551            @Override
1552            public List<UserGroupRole> findByU_G(long userId, long groupId) {
1553                    return findByU_G(userId, groupId, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
1554                            null);
1555            }
1556    
1557            /**
1558             * Returns a range of all the user group roles where userId = &#63; and groupId = &#63;.
1559             *
1560             * <p>
1561             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link UserGroupRoleModelImpl}. 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.
1562             * </p>
1563             *
1564             * @param userId the user ID
1565             * @param groupId the group ID
1566             * @param start the lower bound of the range of user group roles
1567             * @param end the upper bound of the range of user group roles (not inclusive)
1568             * @return the range of matching user group roles
1569             */
1570            @Override
1571            public List<UserGroupRole> findByU_G(long userId, long groupId, int start,
1572                    int end) {
1573                    return findByU_G(userId, groupId, start, end, null);
1574            }
1575    
1576            /**
1577             * Returns an ordered range of all the user group roles where userId = &#63; and groupId = &#63;.
1578             *
1579             * <p>
1580             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link UserGroupRoleModelImpl}. 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.
1581             * </p>
1582             *
1583             * @param userId the user ID
1584             * @param groupId the group ID
1585             * @param start the lower bound of the range of user group roles
1586             * @param end the upper bound of the range of user group roles (not inclusive)
1587             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
1588             * @return the ordered range of matching user group roles
1589             */
1590            @Override
1591            public List<UserGroupRole> findByU_G(long userId, long groupId, int start,
1592                    int end, OrderByComparator<UserGroupRole> orderByComparator) {
1593                    boolean pagination = true;
1594                    FinderPath finderPath = null;
1595                    Object[] finderArgs = null;
1596    
1597                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
1598                                    (orderByComparator == null)) {
1599                            pagination = false;
1600                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_U_G;
1601                            finderArgs = new Object[] { userId, groupId };
1602                    }
1603                    else {
1604                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_U_G;
1605                            finderArgs = new Object[] {
1606                                            userId, groupId,
1607                                            
1608                                            start, end, orderByComparator
1609                                    };
1610                    }
1611    
1612                    List<UserGroupRole> list = (List<UserGroupRole>)FinderCacheUtil.getResult(finderPath,
1613                                    finderArgs, this);
1614    
1615                    if ((list != null) && !list.isEmpty()) {
1616                            for (UserGroupRole userGroupRole : list) {
1617                                    if ((userId != userGroupRole.getUserId()) ||
1618                                                    (groupId != userGroupRole.getGroupId())) {
1619                                            list = null;
1620    
1621                                            break;
1622                                    }
1623                            }
1624                    }
1625    
1626                    if (list == null) {
1627                            StringBundler query = null;
1628    
1629                            if (orderByComparator != null) {
1630                                    query = new StringBundler(4 +
1631                                                    (orderByComparator.getOrderByFields().length * 3));
1632                            }
1633                            else {
1634                                    query = new StringBundler(4);
1635                            }
1636    
1637                            query.append(_SQL_SELECT_USERGROUPROLE_WHERE);
1638    
1639                            query.append(_FINDER_COLUMN_U_G_USERID_2);
1640    
1641                            query.append(_FINDER_COLUMN_U_G_GROUPID_2);
1642    
1643                            if (orderByComparator != null) {
1644                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1645                                            orderByComparator);
1646                            }
1647                            else
1648                             if (pagination) {
1649                                    query.append(UserGroupRoleModelImpl.ORDER_BY_JPQL);
1650                            }
1651    
1652                            String sql = query.toString();
1653    
1654                            Session session = null;
1655    
1656                            try {
1657                                    session = openSession();
1658    
1659                                    Query q = session.createQuery(sql);
1660    
1661                                    QueryPos qPos = QueryPos.getInstance(q);
1662    
1663                                    qPos.add(userId);
1664    
1665                                    qPos.add(groupId);
1666    
1667                                    if (!pagination) {
1668                                            list = (List<UserGroupRole>)QueryUtil.list(q, getDialect(),
1669                                                            start, end, false);
1670    
1671                                            Collections.sort(list);
1672    
1673                                            list = Collections.unmodifiableList(list);
1674                                    }
1675                                    else {
1676                                            list = (List<UserGroupRole>)QueryUtil.list(q, getDialect(),
1677                                                            start, end);
1678                                    }
1679    
1680                                    cacheResult(list);
1681    
1682                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
1683                            }
1684                            catch (Exception e) {
1685                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
1686    
1687                                    throw processException(e);
1688                            }
1689                            finally {
1690                                    closeSession(session);
1691                            }
1692                    }
1693    
1694                    return list;
1695            }
1696    
1697            /**
1698             * Returns the first user group role in the ordered set where userId = &#63; and groupId = &#63;.
1699             *
1700             * @param userId the user ID
1701             * @param groupId the group ID
1702             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1703             * @return the first matching user group role
1704             * @throws NoSuchUserGroupRoleException if a matching user group role could not be found
1705             */
1706            @Override
1707            public UserGroupRole findByU_G_First(long userId, long groupId,
1708                    OrderByComparator<UserGroupRole> orderByComparator)
1709                    throws NoSuchUserGroupRoleException {
1710                    UserGroupRole userGroupRole = fetchByU_G_First(userId, groupId,
1711                                    orderByComparator);
1712    
1713                    if (userGroupRole != null) {
1714                            return userGroupRole;
1715                    }
1716    
1717                    StringBundler msg = new StringBundler(6);
1718    
1719                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1720    
1721                    msg.append("userId=");
1722                    msg.append(userId);
1723    
1724                    msg.append(", groupId=");
1725                    msg.append(groupId);
1726    
1727                    msg.append(StringPool.CLOSE_CURLY_BRACE);
1728    
1729                    throw new NoSuchUserGroupRoleException(msg.toString());
1730            }
1731    
1732            /**
1733             * Returns the first user group role in the ordered set where userId = &#63; and groupId = &#63;.
1734             *
1735             * @param userId the user ID
1736             * @param groupId the group ID
1737             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1738             * @return the first matching user group role, or <code>null</code> if a matching user group role could not be found
1739             */
1740            @Override
1741            public UserGroupRole fetchByU_G_First(long userId, long groupId,
1742                    OrderByComparator<UserGroupRole> orderByComparator) {
1743                    List<UserGroupRole> list = findByU_G(userId, groupId, 0, 1,
1744                                    orderByComparator);
1745    
1746                    if (!list.isEmpty()) {
1747                            return list.get(0);
1748                    }
1749    
1750                    return null;
1751            }
1752    
1753            /**
1754             * Returns the last user group role in the ordered set where userId = &#63; and groupId = &#63;.
1755             *
1756             * @param userId the user ID
1757             * @param groupId the group ID
1758             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1759             * @return the last matching user group role
1760             * @throws NoSuchUserGroupRoleException if a matching user group role could not be found
1761             */
1762            @Override
1763            public UserGroupRole findByU_G_Last(long userId, long groupId,
1764                    OrderByComparator<UserGroupRole> orderByComparator)
1765                    throws NoSuchUserGroupRoleException {
1766                    UserGroupRole userGroupRole = fetchByU_G_Last(userId, groupId,
1767                                    orderByComparator);
1768    
1769                    if (userGroupRole != null) {
1770                            return userGroupRole;
1771                    }
1772    
1773                    StringBundler msg = new StringBundler(6);
1774    
1775                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1776    
1777                    msg.append("userId=");
1778                    msg.append(userId);
1779    
1780                    msg.append(", groupId=");
1781                    msg.append(groupId);
1782    
1783                    msg.append(StringPool.CLOSE_CURLY_BRACE);
1784    
1785                    throw new NoSuchUserGroupRoleException(msg.toString());
1786            }
1787    
1788            /**
1789             * Returns the last user group role in the ordered set where userId = &#63; and groupId = &#63;.
1790             *
1791             * @param userId the user ID
1792             * @param groupId the group ID
1793             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1794             * @return the last matching user group role, or <code>null</code> if a matching user group role could not be found
1795             */
1796            @Override
1797            public UserGroupRole fetchByU_G_Last(long userId, long groupId,
1798                    OrderByComparator<UserGroupRole> orderByComparator) {
1799                    int count = countByU_G(userId, groupId);
1800    
1801                    if (count == 0) {
1802                            return null;
1803                    }
1804    
1805                    List<UserGroupRole> list = findByU_G(userId, groupId, count - 1, count,
1806                                    orderByComparator);
1807    
1808                    if (!list.isEmpty()) {
1809                            return list.get(0);
1810                    }
1811    
1812                    return null;
1813            }
1814    
1815            /**
1816             * Returns the user group roles before and after the current user group role in the ordered set where userId = &#63; and groupId = &#63;.
1817             *
1818             * @param userGroupRolePK the primary key of the current user group role
1819             * @param userId the user ID
1820             * @param groupId the group ID
1821             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
1822             * @return the previous, current, and next user group role
1823             * @throws NoSuchUserGroupRoleException if a user group role with the primary key could not be found
1824             */
1825            @Override
1826            public UserGroupRole[] findByU_G_PrevAndNext(
1827                    UserGroupRolePK userGroupRolePK, long userId, long groupId,
1828                    OrderByComparator<UserGroupRole> orderByComparator)
1829                    throws NoSuchUserGroupRoleException {
1830                    UserGroupRole userGroupRole = findByPrimaryKey(userGroupRolePK);
1831    
1832                    Session session = null;
1833    
1834                    try {
1835                            session = openSession();
1836    
1837                            UserGroupRole[] array = new UserGroupRoleImpl[3];
1838    
1839                            array[0] = getByU_G_PrevAndNext(session, userGroupRole, userId,
1840                                            groupId, orderByComparator, true);
1841    
1842                            array[1] = userGroupRole;
1843    
1844                            array[2] = getByU_G_PrevAndNext(session, userGroupRole, userId,
1845                                            groupId, orderByComparator, false);
1846    
1847                            return array;
1848                    }
1849                    catch (Exception e) {
1850                            throw processException(e);
1851                    }
1852                    finally {
1853                            closeSession(session);
1854                    }
1855            }
1856    
1857            protected UserGroupRole getByU_G_PrevAndNext(Session session,
1858                    UserGroupRole userGroupRole, long userId, long groupId,
1859                    OrderByComparator<UserGroupRole> orderByComparator, boolean previous) {
1860                    StringBundler query = null;
1861    
1862                    if (orderByComparator != null) {
1863                            query = new StringBundler(6 +
1864                                            (orderByComparator.getOrderByFields().length * 6));
1865                    }
1866                    else {
1867                            query = new StringBundler(3);
1868                    }
1869    
1870                    query.append(_SQL_SELECT_USERGROUPROLE_WHERE);
1871    
1872                    query.append(_FINDER_COLUMN_U_G_USERID_2);
1873    
1874                    query.append(_FINDER_COLUMN_U_G_GROUPID_2);
1875    
1876                    if (orderByComparator != null) {
1877                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
1878    
1879                            if (orderByConditionFields.length > 0) {
1880                                    query.append(WHERE_AND);
1881                            }
1882    
1883                            for (int i = 0; i < orderByConditionFields.length; i++) {
1884                                    query.append(_ORDER_BY_ENTITY_ALIAS);
1885                                    query.append(orderByConditionFields[i]);
1886    
1887                                    if ((i + 1) < orderByConditionFields.length) {
1888                                            if (orderByComparator.isAscending() ^ previous) {
1889                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
1890                                            }
1891                                            else {
1892                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
1893                                            }
1894                                    }
1895                                    else {
1896                                            if (orderByComparator.isAscending() ^ previous) {
1897                                                    query.append(WHERE_GREATER_THAN);
1898                                            }
1899                                            else {
1900                                                    query.append(WHERE_LESSER_THAN);
1901                                            }
1902                                    }
1903                            }
1904    
1905                            query.append(ORDER_BY_CLAUSE);
1906    
1907                            String[] orderByFields = orderByComparator.getOrderByFields();
1908    
1909                            for (int i = 0; i < orderByFields.length; i++) {
1910                                    query.append(_ORDER_BY_ENTITY_ALIAS);
1911                                    query.append(orderByFields[i]);
1912    
1913                                    if ((i + 1) < orderByFields.length) {
1914                                            if (orderByComparator.isAscending() ^ previous) {
1915                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
1916                                            }
1917                                            else {
1918                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
1919                                            }
1920                                    }
1921                                    else {
1922                                            if (orderByComparator.isAscending() ^ previous) {
1923                                                    query.append(ORDER_BY_ASC);
1924                                            }
1925                                            else {
1926                                                    query.append(ORDER_BY_DESC);
1927                                            }
1928                                    }
1929                            }
1930                    }
1931                    else {
1932                            query.append(UserGroupRoleModelImpl.ORDER_BY_JPQL);
1933                    }
1934    
1935                    String sql = query.toString();
1936    
1937                    Query q = session.createQuery(sql);
1938    
1939                    q.setFirstResult(0);
1940                    q.setMaxResults(2);
1941    
1942                    QueryPos qPos = QueryPos.getInstance(q);
1943    
1944                    qPos.add(userId);
1945    
1946                    qPos.add(groupId);
1947    
1948                    if (orderByComparator != null) {
1949                            Object[] values = orderByComparator.getOrderByConditionValues(userGroupRole);
1950    
1951                            for (Object value : values) {
1952                                    qPos.add(value);
1953                            }
1954                    }
1955    
1956                    List<UserGroupRole> list = q.list();
1957    
1958                    if (list.size() == 2) {
1959                            return list.get(1);
1960                    }
1961                    else {
1962                            return null;
1963                    }
1964            }
1965    
1966            /**
1967             * Removes all the user group roles where userId = &#63; and groupId = &#63; from the database.
1968             *
1969             * @param userId the user ID
1970             * @param groupId the group ID
1971             */
1972            @Override
1973            public void removeByU_G(long userId, long groupId) {
1974                    for (UserGroupRole userGroupRole : findByU_G(userId, groupId,
1975                                    QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
1976                            remove(userGroupRole);
1977                    }
1978            }
1979    
1980            /**
1981             * Returns the number of user group roles where userId = &#63; and groupId = &#63;.
1982             *
1983             * @param userId the user ID
1984             * @param groupId the group ID
1985             * @return the number of matching user group roles
1986             */
1987            @Override
1988            public int countByU_G(long userId, long groupId) {
1989                    FinderPath finderPath = FINDER_PATH_COUNT_BY_U_G;
1990    
1991                    Object[] finderArgs = new Object[] { userId, groupId };
1992    
1993                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
1994                                    this);
1995    
1996                    if (count == null) {
1997                            StringBundler query = new StringBundler(3);
1998    
1999                            query.append(_SQL_COUNT_USERGROUPROLE_WHERE);
2000    
2001                            query.append(_FINDER_COLUMN_U_G_USERID_2);
2002    
2003                            query.append(_FINDER_COLUMN_U_G_GROUPID_2);
2004    
2005                            String sql = query.toString();
2006    
2007                            Session session = null;
2008    
2009                            try {
2010                                    session = openSession();
2011    
2012                                    Query q = session.createQuery(sql);
2013    
2014                                    QueryPos qPos = QueryPos.getInstance(q);
2015    
2016                                    qPos.add(userId);
2017    
2018                                    qPos.add(groupId);
2019    
2020                                    count = (Long)q.uniqueResult();
2021    
2022                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
2023                            }
2024                            catch (Exception e) {
2025                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
2026    
2027                                    throw processException(e);
2028                            }
2029                            finally {
2030                                    closeSession(session);
2031                            }
2032                    }
2033    
2034                    return count.intValue();
2035            }
2036    
2037            private static final String _FINDER_COLUMN_U_G_USERID_2 = "userGroupRole.id.userId = ? AND ";
2038            private static final String _FINDER_COLUMN_U_G_GROUPID_2 = "userGroupRole.id.groupId = ?";
2039            public static final FinderPath FINDER_PATH_WITH_PAGINATION_FIND_BY_G_R = new FinderPath(UserGroupRoleModelImpl.ENTITY_CACHE_ENABLED,
2040                            UserGroupRoleModelImpl.FINDER_CACHE_ENABLED,
2041                            UserGroupRoleImpl.class, FINDER_CLASS_NAME_LIST_WITH_PAGINATION,
2042                            "findByG_R",
2043                            new String[] {
2044                                    Long.class.getName(), Long.class.getName(),
2045                                    
2046                            Integer.class.getName(), Integer.class.getName(),
2047                                    OrderByComparator.class.getName()
2048                            });
2049            public static final FinderPath FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_G_R = new FinderPath(UserGroupRoleModelImpl.ENTITY_CACHE_ENABLED,
2050                            UserGroupRoleModelImpl.FINDER_CACHE_ENABLED,
2051                            UserGroupRoleImpl.class, FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION,
2052                            "findByG_R",
2053                            new String[] { Long.class.getName(), Long.class.getName() },
2054                            UserGroupRoleModelImpl.GROUPID_COLUMN_BITMASK |
2055                            UserGroupRoleModelImpl.ROLEID_COLUMN_BITMASK);
2056            public static final FinderPath FINDER_PATH_COUNT_BY_G_R = new FinderPath(UserGroupRoleModelImpl.ENTITY_CACHE_ENABLED,
2057                            UserGroupRoleModelImpl.FINDER_CACHE_ENABLED, Long.class,
2058                            FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION, "countByG_R",
2059                            new String[] { Long.class.getName(), Long.class.getName() });
2060    
2061            /**
2062             * Returns all the user group roles where groupId = &#63; and roleId = &#63;.
2063             *
2064             * @param groupId the group ID
2065             * @param roleId the role ID
2066             * @return the matching user group roles
2067             */
2068            @Override
2069            public List<UserGroupRole> findByG_R(long groupId, long roleId) {
2070                    return findByG_R(groupId, roleId, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
2071                            null);
2072            }
2073    
2074            /**
2075             * Returns a range of all the user group roles where groupId = &#63; and roleId = &#63;.
2076             *
2077             * <p>
2078             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link UserGroupRoleModelImpl}. 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.
2079             * </p>
2080             *
2081             * @param groupId the group ID
2082             * @param roleId the role ID
2083             * @param start the lower bound of the range of user group roles
2084             * @param end the upper bound of the range of user group roles (not inclusive)
2085             * @return the range of matching user group roles
2086             */
2087            @Override
2088            public List<UserGroupRole> findByG_R(long groupId, long roleId, int start,
2089                    int end) {
2090                    return findByG_R(groupId, roleId, start, end, null);
2091            }
2092    
2093            /**
2094             * Returns an ordered range of all the user group roles where groupId = &#63; and roleId = &#63;.
2095             *
2096             * <p>
2097             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link UserGroupRoleModelImpl}. 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.
2098             * </p>
2099             *
2100             * @param groupId the group ID
2101             * @param roleId the role ID
2102             * @param start the lower bound of the range of user group roles
2103             * @param end the upper bound of the range of user group roles (not inclusive)
2104             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
2105             * @return the ordered range of matching user group roles
2106             */
2107            @Override
2108            public List<UserGroupRole> findByG_R(long groupId, long roleId, int start,
2109                    int end, OrderByComparator<UserGroupRole> orderByComparator) {
2110                    boolean pagination = true;
2111                    FinderPath finderPath = null;
2112                    Object[] finderArgs = null;
2113    
2114                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
2115                                    (orderByComparator == null)) {
2116                            pagination = false;
2117                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_G_R;
2118                            finderArgs = new Object[] { groupId, roleId };
2119                    }
2120                    else {
2121                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_BY_G_R;
2122                            finderArgs = new Object[] {
2123                                            groupId, roleId,
2124                                            
2125                                            start, end, orderByComparator
2126                                    };
2127                    }
2128    
2129                    List<UserGroupRole> list = (List<UserGroupRole>)FinderCacheUtil.getResult(finderPath,
2130                                    finderArgs, this);
2131    
2132                    if ((list != null) && !list.isEmpty()) {
2133                            for (UserGroupRole userGroupRole : list) {
2134                                    if ((groupId != userGroupRole.getGroupId()) ||
2135                                                    (roleId != userGroupRole.getRoleId())) {
2136                                            list = null;
2137    
2138                                            break;
2139                                    }
2140                            }
2141                    }
2142    
2143                    if (list == null) {
2144                            StringBundler query = null;
2145    
2146                            if (orderByComparator != null) {
2147                                    query = new StringBundler(4 +
2148                                                    (orderByComparator.getOrderByFields().length * 3));
2149                            }
2150                            else {
2151                                    query = new StringBundler(4);
2152                            }
2153    
2154                            query.append(_SQL_SELECT_USERGROUPROLE_WHERE);
2155    
2156                            query.append(_FINDER_COLUMN_G_R_GROUPID_2);
2157    
2158                            query.append(_FINDER_COLUMN_G_R_ROLEID_2);
2159    
2160                            if (orderByComparator != null) {
2161                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
2162                                            orderByComparator);
2163                            }
2164                            else
2165                             if (pagination) {
2166                                    query.append(UserGroupRoleModelImpl.ORDER_BY_JPQL);
2167                            }
2168    
2169                            String sql = query.toString();
2170    
2171                            Session session = null;
2172    
2173                            try {
2174                                    session = openSession();
2175    
2176                                    Query q = session.createQuery(sql);
2177    
2178                                    QueryPos qPos = QueryPos.getInstance(q);
2179    
2180                                    qPos.add(groupId);
2181    
2182                                    qPos.add(roleId);
2183    
2184                                    if (!pagination) {
2185                                            list = (List<UserGroupRole>)QueryUtil.list(q, getDialect(),
2186                                                            start, end, false);
2187    
2188                                            Collections.sort(list);
2189    
2190                                            list = Collections.unmodifiableList(list);
2191                                    }
2192                                    else {
2193                                            list = (List<UserGroupRole>)QueryUtil.list(q, getDialect(),
2194                                                            start, end);
2195                                    }
2196    
2197                                    cacheResult(list);
2198    
2199                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
2200                            }
2201                            catch (Exception e) {
2202                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
2203    
2204                                    throw processException(e);
2205                            }
2206                            finally {
2207                                    closeSession(session);
2208                            }
2209                    }
2210    
2211                    return list;
2212            }
2213    
2214            /**
2215             * Returns the first user group role in the ordered set where groupId = &#63; and roleId = &#63;.
2216             *
2217             * @param groupId the group ID
2218             * @param roleId the role ID
2219             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2220             * @return the first matching user group role
2221             * @throws NoSuchUserGroupRoleException if a matching user group role could not be found
2222             */
2223            @Override
2224            public UserGroupRole findByG_R_First(long groupId, long roleId,
2225                    OrderByComparator<UserGroupRole> orderByComparator)
2226                    throws NoSuchUserGroupRoleException {
2227                    UserGroupRole userGroupRole = fetchByG_R_First(groupId, roleId,
2228                                    orderByComparator);
2229    
2230                    if (userGroupRole != null) {
2231                            return userGroupRole;
2232                    }
2233    
2234                    StringBundler msg = new StringBundler(6);
2235    
2236                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
2237    
2238                    msg.append("groupId=");
2239                    msg.append(groupId);
2240    
2241                    msg.append(", roleId=");
2242                    msg.append(roleId);
2243    
2244                    msg.append(StringPool.CLOSE_CURLY_BRACE);
2245    
2246                    throw new NoSuchUserGroupRoleException(msg.toString());
2247            }
2248    
2249            /**
2250             * Returns the first user group role in the ordered set where groupId = &#63; and roleId = &#63;.
2251             *
2252             * @param groupId the group ID
2253             * @param roleId the role ID
2254             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2255             * @return the first matching user group role, or <code>null</code> if a matching user group role could not be found
2256             */
2257            @Override
2258            public UserGroupRole fetchByG_R_First(long groupId, long roleId,
2259                    OrderByComparator<UserGroupRole> orderByComparator) {
2260                    List<UserGroupRole> list = findByG_R(groupId, roleId, 0, 1,
2261                                    orderByComparator);
2262    
2263                    if (!list.isEmpty()) {
2264                            return list.get(0);
2265                    }
2266    
2267                    return null;
2268            }
2269    
2270            /**
2271             * Returns the last user group role in the ordered set where groupId = &#63; and roleId = &#63;.
2272             *
2273             * @param groupId the group ID
2274             * @param roleId the role ID
2275             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2276             * @return the last matching user group role
2277             * @throws NoSuchUserGroupRoleException if a matching user group role could not be found
2278             */
2279            @Override
2280            public UserGroupRole findByG_R_Last(long groupId, long roleId,
2281                    OrderByComparator<UserGroupRole> orderByComparator)
2282                    throws NoSuchUserGroupRoleException {
2283                    UserGroupRole userGroupRole = fetchByG_R_Last(groupId, roleId,
2284                                    orderByComparator);
2285    
2286                    if (userGroupRole != null) {
2287                            return userGroupRole;
2288                    }
2289    
2290                    StringBundler msg = new StringBundler(6);
2291    
2292                    msg.append(_NO_SUCH_ENTITY_WITH_KEY);
2293    
2294                    msg.append("groupId=");
2295                    msg.append(groupId);
2296    
2297                    msg.append(", roleId=");
2298                    msg.append(roleId);
2299    
2300                    msg.append(StringPool.CLOSE_CURLY_BRACE);
2301    
2302                    throw new NoSuchUserGroupRoleException(msg.toString());
2303            }
2304    
2305            /**
2306             * Returns the last user group role in the ordered set where groupId = &#63; and roleId = &#63;.
2307             *
2308             * @param groupId the group ID
2309             * @param roleId the role ID
2310             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2311             * @return the last matching user group role, or <code>null</code> if a matching user group role could not be found
2312             */
2313            @Override
2314            public UserGroupRole fetchByG_R_Last(long groupId, long roleId,
2315                    OrderByComparator<UserGroupRole> orderByComparator) {
2316                    int count = countByG_R(groupId, roleId);
2317    
2318                    if (count == 0) {
2319                            return null;
2320                    }
2321    
2322                    List<UserGroupRole> list = findByG_R(groupId, roleId, count - 1, count,
2323                                    orderByComparator);
2324    
2325                    if (!list.isEmpty()) {
2326                            return list.get(0);
2327                    }
2328    
2329                    return null;
2330            }
2331    
2332            /**
2333             * Returns the user group roles before and after the current user group role in the ordered set where groupId = &#63; and roleId = &#63;.
2334             *
2335             * @param userGroupRolePK the primary key of the current user group role
2336             * @param groupId the group ID
2337             * @param roleId the role ID
2338             * @param orderByComparator the comparator to order the set by (optionally <code>null</code>)
2339             * @return the previous, current, and next user group role
2340             * @throws NoSuchUserGroupRoleException if a user group role with the primary key could not be found
2341             */
2342            @Override
2343            public UserGroupRole[] findByG_R_PrevAndNext(
2344                    UserGroupRolePK userGroupRolePK, long groupId, long roleId,
2345                    OrderByComparator<UserGroupRole> orderByComparator)
2346                    throws NoSuchUserGroupRoleException {
2347                    UserGroupRole userGroupRole = findByPrimaryKey(userGroupRolePK);
2348    
2349                    Session session = null;
2350    
2351                    try {
2352                            session = openSession();
2353    
2354                            UserGroupRole[] array = new UserGroupRoleImpl[3];
2355    
2356                            array[0] = getByG_R_PrevAndNext(session, userGroupRole, groupId,
2357                                            roleId, orderByComparator, true);
2358    
2359                            array[1] = userGroupRole;
2360    
2361                            array[2] = getByG_R_PrevAndNext(session, userGroupRole, groupId,
2362                                            roleId, orderByComparator, false);
2363    
2364                            return array;
2365                    }
2366                    catch (Exception e) {
2367                            throw processException(e);
2368                    }
2369                    finally {
2370                            closeSession(session);
2371                    }
2372            }
2373    
2374            protected UserGroupRole getByG_R_PrevAndNext(Session session,
2375                    UserGroupRole userGroupRole, long groupId, long roleId,
2376                    OrderByComparator<UserGroupRole> orderByComparator, boolean previous) {
2377                    StringBundler query = null;
2378    
2379                    if (orderByComparator != null) {
2380                            query = new StringBundler(6 +
2381                                            (orderByComparator.getOrderByFields().length * 6));
2382                    }
2383                    else {
2384                            query = new StringBundler(3);
2385                    }
2386    
2387                    query.append(_SQL_SELECT_USERGROUPROLE_WHERE);
2388    
2389                    query.append(_FINDER_COLUMN_G_R_GROUPID_2);
2390    
2391                    query.append(_FINDER_COLUMN_G_R_ROLEID_2);
2392    
2393                    if (orderByComparator != null) {
2394                            String[] orderByConditionFields = orderByComparator.getOrderByConditionFields();
2395    
2396                            if (orderByConditionFields.length > 0) {
2397                                    query.append(WHERE_AND);
2398                            }
2399    
2400                            for (int i = 0; i < orderByConditionFields.length; i++) {
2401                                    query.append(_ORDER_BY_ENTITY_ALIAS);
2402                                    query.append(orderByConditionFields[i]);
2403    
2404                                    if ((i + 1) < orderByConditionFields.length) {
2405                                            if (orderByComparator.isAscending() ^ previous) {
2406                                                    query.append(WHERE_GREATER_THAN_HAS_NEXT);
2407                                            }
2408                                            else {
2409                                                    query.append(WHERE_LESSER_THAN_HAS_NEXT);
2410                                            }
2411                                    }
2412                                    else {
2413                                            if (orderByComparator.isAscending() ^ previous) {
2414                                                    query.append(WHERE_GREATER_THAN);
2415                                            }
2416                                            else {
2417                                                    query.append(WHERE_LESSER_THAN);
2418                                            }
2419                                    }
2420                            }
2421    
2422                            query.append(ORDER_BY_CLAUSE);
2423    
2424                            String[] orderByFields = orderByComparator.getOrderByFields();
2425    
2426                            for (int i = 0; i < orderByFields.length; i++) {
2427                                    query.append(_ORDER_BY_ENTITY_ALIAS);
2428                                    query.append(orderByFields[i]);
2429    
2430                                    if ((i + 1) < orderByFields.length) {
2431                                            if (orderByComparator.isAscending() ^ previous) {
2432                                                    query.append(ORDER_BY_ASC_HAS_NEXT);
2433                                            }
2434                                            else {
2435                                                    query.append(ORDER_BY_DESC_HAS_NEXT);
2436                                            }
2437                                    }
2438                                    else {
2439                                            if (orderByComparator.isAscending() ^ previous) {
2440                                                    query.append(ORDER_BY_ASC);
2441                                            }
2442                                            else {
2443                                                    query.append(ORDER_BY_DESC);
2444                                            }
2445                                    }
2446                            }
2447                    }
2448                    else {
2449                            query.append(UserGroupRoleModelImpl.ORDER_BY_JPQL);
2450                    }
2451    
2452                    String sql = query.toString();
2453    
2454                    Query q = session.createQuery(sql);
2455    
2456                    q.setFirstResult(0);
2457                    q.setMaxResults(2);
2458    
2459                    QueryPos qPos = QueryPos.getInstance(q);
2460    
2461                    qPos.add(groupId);
2462    
2463                    qPos.add(roleId);
2464    
2465                    if (orderByComparator != null) {
2466                            Object[] values = orderByComparator.getOrderByConditionValues(userGroupRole);
2467    
2468                            for (Object value : values) {
2469                                    qPos.add(value);
2470                            }
2471                    }
2472    
2473                    List<UserGroupRole> list = q.list();
2474    
2475                    if (list.size() == 2) {
2476                            return list.get(1);
2477                    }
2478                    else {
2479                            return null;
2480                    }
2481            }
2482    
2483            /**
2484             * Removes all the user group roles where groupId = &#63; and roleId = &#63; from the database.
2485             *
2486             * @param groupId the group ID
2487             * @param roleId the role ID
2488             */
2489            @Override
2490            public void removeByG_R(long groupId, long roleId) {
2491                    for (UserGroupRole userGroupRole : findByG_R(groupId, roleId,
2492                                    QueryUtil.ALL_POS, QueryUtil.ALL_POS, null)) {
2493                            remove(userGroupRole);
2494                    }
2495            }
2496    
2497            /**
2498             * Returns the number of user group roles where groupId = &#63; and roleId = &#63;.
2499             *
2500             * @param groupId the group ID
2501             * @param roleId the role ID
2502             * @return the number of matching user group roles
2503             */
2504            @Override
2505            public int countByG_R(long groupId, long roleId) {
2506                    FinderPath finderPath = FINDER_PATH_COUNT_BY_G_R;
2507    
2508                    Object[] finderArgs = new Object[] { groupId, roleId };
2509    
2510                    Long count = (Long)FinderCacheUtil.getResult(finderPath, finderArgs,
2511                                    this);
2512    
2513                    if (count == null) {
2514                            StringBundler query = new StringBundler(3);
2515    
2516                            query.append(_SQL_COUNT_USERGROUPROLE_WHERE);
2517    
2518                            query.append(_FINDER_COLUMN_G_R_GROUPID_2);
2519    
2520                            query.append(_FINDER_COLUMN_G_R_ROLEID_2);
2521    
2522                            String sql = query.toString();
2523    
2524                            Session session = null;
2525    
2526                            try {
2527                                    session = openSession();
2528    
2529                                    Query q = session.createQuery(sql);
2530    
2531                                    QueryPos qPos = QueryPos.getInstance(q);
2532    
2533                                    qPos.add(groupId);
2534    
2535                                    qPos.add(roleId);
2536    
2537                                    count = (Long)q.uniqueResult();
2538    
2539                                    FinderCacheUtil.putResult(finderPath, finderArgs, count);
2540                            }
2541                            catch (Exception e) {
2542                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
2543    
2544                                    throw processException(e);
2545                            }
2546                            finally {
2547                                    closeSession(session);
2548                            }
2549                    }
2550    
2551                    return count.intValue();
2552            }
2553    
2554            private static final String _FINDER_COLUMN_G_R_GROUPID_2 = "userGroupRole.id.groupId = ? AND ";
2555            private static final String _FINDER_COLUMN_G_R_ROLEID_2 = "userGroupRole.id.roleId = ?";
2556    
2557            public UserGroupRolePersistenceImpl() {
2558                    setModelClass(UserGroupRole.class);
2559            }
2560    
2561            /**
2562             * Caches the user group role in the entity cache if it is enabled.
2563             *
2564             * @param userGroupRole the user group role
2565             */
2566            @Override
2567            public void cacheResult(UserGroupRole userGroupRole) {
2568                    EntityCacheUtil.putResult(UserGroupRoleModelImpl.ENTITY_CACHE_ENABLED,
2569                            UserGroupRoleImpl.class, userGroupRole.getPrimaryKey(),
2570                            userGroupRole);
2571    
2572                    userGroupRole.resetOriginalValues();
2573            }
2574    
2575            /**
2576             * Caches the user group roles in the entity cache if it is enabled.
2577             *
2578             * @param userGroupRoles the user group roles
2579             */
2580            @Override
2581            public void cacheResult(List<UserGroupRole> userGroupRoles) {
2582                    for (UserGroupRole userGroupRole : userGroupRoles) {
2583                            if (EntityCacheUtil.getResult(
2584                                                    UserGroupRoleModelImpl.ENTITY_CACHE_ENABLED,
2585                                                    UserGroupRoleImpl.class, userGroupRole.getPrimaryKey()) == null) {
2586                                    cacheResult(userGroupRole);
2587                            }
2588                            else {
2589                                    userGroupRole.resetOriginalValues();
2590                            }
2591                    }
2592            }
2593    
2594            /**
2595             * Clears the cache for all user group roles.
2596             *
2597             * <p>
2598             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
2599             * </p>
2600             */
2601            @Override
2602            public void clearCache() {
2603                    EntityCacheUtil.clearCache(UserGroupRoleImpl.class);
2604    
2605                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
2606                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
2607                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
2608            }
2609    
2610            /**
2611             * Clears the cache for the user group role.
2612             *
2613             * <p>
2614             * The {@link com.liferay.portal.kernel.dao.orm.EntityCache} and {@link com.liferay.portal.kernel.dao.orm.FinderCache} are both cleared by this method.
2615             * </p>
2616             */
2617            @Override
2618            public void clearCache(UserGroupRole userGroupRole) {
2619                    EntityCacheUtil.removeResult(UserGroupRoleModelImpl.ENTITY_CACHE_ENABLED,
2620                            UserGroupRoleImpl.class, userGroupRole.getPrimaryKey());
2621    
2622                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
2623                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
2624            }
2625    
2626            @Override
2627            public void clearCache(List<UserGroupRole> userGroupRoles) {
2628                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
2629                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
2630    
2631                    for (UserGroupRole userGroupRole : userGroupRoles) {
2632                            EntityCacheUtil.removeResult(UserGroupRoleModelImpl.ENTITY_CACHE_ENABLED,
2633                                    UserGroupRoleImpl.class, userGroupRole.getPrimaryKey());
2634                    }
2635            }
2636    
2637            /**
2638             * Creates a new user group role with the primary key. Does not add the user group role to the database.
2639             *
2640             * @param userGroupRolePK the primary key for the new user group role
2641             * @return the new user group role
2642             */
2643            @Override
2644            public UserGroupRole create(UserGroupRolePK userGroupRolePK) {
2645                    UserGroupRole userGroupRole = new UserGroupRoleImpl();
2646    
2647                    userGroupRole.setNew(true);
2648                    userGroupRole.setPrimaryKey(userGroupRolePK);
2649    
2650                    return userGroupRole;
2651            }
2652    
2653            /**
2654             * Removes the user group role with the primary key from the database. Also notifies the appropriate model listeners.
2655             *
2656             * @param userGroupRolePK the primary key of the user group role
2657             * @return the user group role that was removed
2658             * @throws NoSuchUserGroupRoleException if a user group role with the primary key could not be found
2659             */
2660            @Override
2661            public UserGroupRole remove(UserGroupRolePK userGroupRolePK)
2662                    throws NoSuchUserGroupRoleException {
2663                    return remove((Serializable)userGroupRolePK);
2664            }
2665    
2666            /**
2667             * Removes the user group role with the primary key from the database. Also notifies the appropriate model listeners.
2668             *
2669             * @param primaryKey the primary key of the user group role
2670             * @return the user group role that was removed
2671             * @throws NoSuchUserGroupRoleException if a user group role with the primary key could not be found
2672             */
2673            @Override
2674            public UserGroupRole remove(Serializable primaryKey)
2675                    throws NoSuchUserGroupRoleException {
2676                    Session session = null;
2677    
2678                    try {
2679                            session = openSession();
2680    
2681                            UserGroupRole userGroupRole = (UserGroupRole)session.get(UserGroupRoleImpl.class,
2682                                            primaryKey);
2683    
2684                            if (userGroupRole == null) {
2685                                    if (_log.isWarnEnabled()) {
2686                                            _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
2687                                    }
2688    
2689                                    throw new NoSuchUserGroupRoleException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
2690                                            primaryKey);
2691                            }
2692    
2693                            return remove(userGroupRole);
2694                    }
2695                    catch (NoSuchUserGroupRoleException nsee) {
2696                            throw nsee;
2697                    }
2698                    catch (Exception e) {
2699                            throw processException(e);
2700                    }
2701                    finally {
2702                            closeSession(session);
2703                    }
2704            }
2705    
2706            @Override
2707            protected UserGroupRole removeImpl(UserGroupRole userGroupRole) {
2708                    userGroupRole = toUnwrappedModel(userGroupRole);
2709    
2710                    Session session = null;
2711    
2712                    try {
2713                            session = openSession();
2714    
2715                            if (!session.contains(userGroupRole)) {
2716                                    userGroupRole = (UserGroupRole)session.get(UserGroupRoleImpl.class,
2717                                                    userGroupRole.getPrimaryKeyObj());
2718                            }
2719    
2720                            if (userGroupRole != null) {
2721                                    session.delete(userGroupRole);
2722                            }
2723                    }
2724                    catch (Exception e) {
2725                            throw processException(e);
2726                    }
2727                    finally {
2728                            closeSession(session);
2729                    }
2730    
2731                    if (userGroupRole != null) {
2732                            clearCache(userGroupRole);
2733                    }
2734    
2735                    return userGroupRole;
2736            }
2737    
2738            @Override
2739            public UserGroupRole updateImpl(UserGroupRole userGroupRole) {
2740                    userGroupRole = toUnwrappedModel(userGroupRole);
2741    
2742                    boolean isNew = userGroupRole.isNew();
2743    
2744                    UserGroupRoleModelImpl userGroupRoleModelImpl = (UserGroupRoleModelImpl)userGroupRole;
2745    
2746                    Session session = null;
2747    
2748                    try {
2749                            session = openSession();
2750    
2751                            if (userGroupRole.isNew()) {
2752                                    session.save(userGroupRole);
2753    
2754                                    userGroupRole.setNew(false);
2755                            }
2756                            else {
2757                                    session.merge(userGroupRole);
2758                            }
2759                    }
2760                    catch (Exception e) {
2761                            throw processException(e);
2762                    }
2763                    finally {
2764                            closeSession(session);
2765                    }
2766    
2767                    FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
2768    
2769                    if (isNew || !UserGroupRoleModelImpl.COLUMN_BITMASK_ENABLED) {
2770                            FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
2771                    }
2772    
2773                    else {
2774                            if ((userGroupRoleModelImpl.getColumnBitmask() &
2775                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_USERID.getColumnBitmask()) != 0) {
2776                                    Object[] args = new Object[] {
2777                                                    userGroupRoleModelImpl.getOriginalUserId()
2778                                            };
2779    
2780                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_USERID, args);
2781                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_USERID,
2782                                            args);
2783    
2784                                    args = new Object[] { userGroupRoleModelImpl.getUserId() };
2785    
2786                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_USERID, args);
2787                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_USERID,
2788                                            args);
2789                            }
2790    
2791                            if ((userGroupRoleModelImpl.getColumnBitmask() &
2792                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_GROUPID.getColumnBitmask()) != 0) {
2793                                    Object[] args = new Object[] {
2794                                                    userGroupRoleModelImpl.getOriginalGroupId()
2795                                            };
2796    
2797                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_GROUPID, args);
2798                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_GROUPID,
2799                                            args);
2800    
2801                                    args = new Object[] { userGroupRoleModelImpl.getGroupId() };
2802    
2803                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_GROUPID, args);
2804                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_GROUPID,
2805                                            args);
2806                            }
2807    
2808                            if ((userGroupRoleModelImpl.getColumnBitmask() &
2809                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_ROLEID.getColumnBitmask()) != 0) {
2810                                    Object[] args = new Object[] {
2811                                                    userGroupRoleModelImpl.getOriginalRoleId()
2812                                            };
2813    
2814                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_ROLEID, args);
2815                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_ROLEID,
2816                                            args);
2817    
2818                                    args = new Object[] { userGroupRoleModelImpl.getRoleId() };
2819    
2820                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_ROLEID, args);
2821                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_ROLEID,
2822                                            args);
2823                            }
2824    
2825                            if ((userGroupRoleModelImpl.getColumnBitmask() &
2826                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_U_G.getColumnBitmask()) != 0) {
2827                                    Object[] args = new Object[] {
2828                                                    userGroupRoleModelImpl.getOriginalUserId(),
2829                                                    userGroupRoleModelImpl.getOriginalGroupId()
2830                                            };
2831    
2832                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_U_G, args);
2833                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_U_G,
2834                                            args);
2835    
2836                                    args = new Object[] {
2837                                                    userGroupRoleModelImpl.getUserId(),
2838                                                    userGroupRoleModelImpl.getGroupId()
2839                                            };
2840    
2841                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_U_G, args);
2842                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_U_G,
2843                                            args);
2844                            }
2845    
2846                            if ((userGroupRoleModelImpl.getColumnBitmask() &
2847                                            FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_G_R.getColumnBitmask()) != 0) {
2848                                    Object[] args = new Object[] {
2849                                                    userGroupRoleModelImpl.getOriginalGroupId(),
2850                                                    userGroupRoleModelImpl.getOriginalRoleId()
2851                                            };
2852    
2853                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_G_R, args);
2854                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_G_R,
2855                                            args);
2856    
2857                                    args = new Object[] {
2858                                                    userGroupRoleModelImpl.getGroupId(),
2859                                                    userGroupRoleModelImpl.getRoleId()
2860                                            };
2861    
2862                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_BY_G_R, args);
2863                                    FinderCacheUtil.removeResult(FINDER_PATH_WITHOUT_PAGINATION_FIND_BY_G_R,
2864                                            args);
2865                            }
2866                    }
2867    
2868                    EntityCacheUtil.putResult(UserGroupRoleModelImpl.ENTITY_CACHE_ENABLED,
2869                            UserGroupRoleImpl.class, userGroupRole.getPrimaryKey(),
2870                            userGroupRole, false);
2871    
2872                    userGroupRole.resetOriginalValues();
2873    
2874                    return userGroupRole;
2875            }
2876    
2877            protected UserGroupRole toUnwrappedModel(UserGroupRole userGroupRole) {
2878                    if (userGroupRole instanceof UserGroupRoleImpl) {
2879                            return userGroupRole;
2880                    }
2881    
2882                    UserGroupRoleImpl userGroupRoleImpl = new UserGroupRoleImpl();
2883    
2884                    userGroupRoleImpl.setNew(userGroupRole.isNew());
2885                    userGroupRoleImpl.setPrimaryKey(userGroupRole.getPrimaryKey());
2886    
2887                    userGroupRoleImpl.setMvccVersion(userGroupRole.getMvccVersion());
2888                    userGroupRoleImpl.setUserId(userGroupRole.getUserId());
2889                    userGroupRoleImpl.setGroupId(userGroupRole.getGroupId());
2890                    userGroupRoleImpl.setRoleId(userGroupRole.getRoleId());
2891    
2892                    return userGroupRoleImpl;
2893            }
2894    
2895            /**
2896             * Returns the user group role with the primary key or throws a {@link com.liferay.portal.NoSuchModelException} if it could not be found.
2897             *
2898             * @param primaryKey the primary key of the user group role
2899             * @return the user group role
2900             * @throws NoSuchUserGroupRoleException if a user group role with the primary key could not be found
2901             */
2902            @Override
2903            public UserGroupRole findByPrimaryKey(Serializable primaryKey)
2904                    throws NoSuchUserGroupRoleException {
2905                    UserGroupRole userGroupRole = fetchByPrimaryKey(primaryKey);
2906    
2907                    if (userGroupRole == null) {
2908                            if (_log.isWarnEnabled()) {
2909                                    _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + primaryKey);
2910                            }
2911    
2912                            throw new NoSuchUserGroupRoleException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
2913                                    primaryKey);
2914                    }
2915    
2916                    return userGroupRole;
2917            }
2918    
2919            /**
2920             * Returns the user group role with the primary key or throws a {@link NoSuchUserGroupRoleException} if it could not be found.
2921             *
2922             * @param userGroupRolePK the primary key of the user group role
2923             * @return the user group role
2924             * @throws NoSuchUserGroupRoleException if a user group role with the primary key could not be found
2925             */
2926            @Override
2927            public UserGroupRole findByPrimaryKey(UserGroupRolePK userGroupRolePK)
2928                    throws NoSuchUserGroupRoleException {
2929                    return findByPrimaryKey((Serializable)userGroupRolePK);
2930            }
2931    
2932            /**
2933             * Returns the user group role with the primary key or returns <code>null</code> if it could not be found.
2934             *
2935             * @param primaryKey the primary key of the user group role
2936             * @return the user group role, or <code>null</code> if a user group role with the primary key could not be found
2937             */
2938            @Override
2939            public UserGroupRole fetchByPrimaryKey(Serializable primaryKey) {
2940                    UserGroupRole userGroupRole = (UserGroupRole)EntityCacheUtil.getResult(UserGroupRoleModelImpl.ENTITY_CACHE_ENABLED,
2941                                    UserGroupRoleImpl.class, primaryKey);
2942    
2943                    if (userGroupRole == _nullUserGroupRole) {
2944                            return null;
2945                    }
2946    
2947                    if (userGroupRole == null) {
2948                            Session session = null;
2949    
2950                            try {
2951                                    session = openSession();
2952    
2953                                    userGroupRole = (UserGroupRole)session.get(UserGroupRoleImpl.class,
2954                                                    primaryKey);
2955    
2956                                    if (userGroupRole != null) {
2957                                            cacheResult(userGroupRole);
2958                                    }
2959                                    else {
2960                                            EntityCacheUtil.putResult(UserGroupRoleModelImpl.ENTITY_CACHE_ENABLED,
2961                                                    UserGroupRoleImpl.class, primaryKey, _nullUserGroupRole);
2962                                    }
2963                            }
2964                            catch (Exception e) {
2965                                    EntityCacheUtil.removeResult(UserGroupRoleModelImpl.ENTITY_CACHE_ENABLED,
2966                                            UserGroupRoleImpl.class, primaryKey);
2967    
2968                                    throw processException(e);
2969                            }
2970                            finally {
2971                                    closeSession(session);
2972                            }
2973                    }
2974    
2975                    return userGroupRole;
2976            }
2977    
2978            /**
2979             * Returns the user group role with the primary key or returns <code>null</code> if it could not be found.
2980             *
2981             * @param userGroupRolePK the primary key of the user group role
2982             * @return the user group role, or <code>null</code> if a user group role with the primary key could not be found
2983             */
2984            @Override
2985            public UserGroupRole fetchByPrimaryKey(UserGroupRolePK userGroupRolePK) {
2986                    return fetchByPrimaryKey((Serializable)userGroupRolePK);
2987            }
2988    
2989            @Override
2990            public Map<Serializable, UserGroupRole> fetchByPrimaryKeys(
2991                    Set<Serializable> primaryKeys) {
2992                    if (primaryKeys.isEmpty()) {
2993                            return Collections.emptyMap();
2994                    }
2995    
2996                    Map<Serializable, UserGroupRole> map = new HashMap<Serializable, UserGroupRole>();
2997    
2998                    for (Serializable primaryKey : primaryKeys) {
2999                            UserGroupRole userGroupRole = fetchByPrimaryKey(primaryKey);
3000    
3001                            if (userGroupRole != null) {
3002                                    map.put(primaryKey, userGroupRole);
3003                            }
3004                    }
3005    
3006                    return map;
3007            }
3008    
3009            /**
3010             * Returns all the user group roles.
3011             *
3012             * @return the user group roles
3013             */
3014            @Override
3015            public List<UserGroupRole> findAll() {
3016                    return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
3017            }
3018    
3019            /**
3020             * Returns a range of all the user group roles.
3021             *
3022             * <p>
3023             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link UserGroupRoleModelImpl}. 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.
3024             * </p>
3025             *
3026             * @param start the lower bound of the range of user group roles
3027             * @param end the upper bound of the range of user group roles (not inclusive)
3028             * @return the range of user group roles
3029             */
3030            @Override
3031            public List<UserGroupRole> findAll(int start, int end) {
3032                    return findAll(start, end, null);
3033            }
3034    
3035            /**
3036             * Returns an ordered range of all the user group roles.
3037             *
3038             * <p>
3039             * Useful when paginating results. Returns a maximum of <code>end - start</code> instances. <code>start</code> and <code>end</code> are not primary keys, they are indexes in the result set. Thus, <code>0</code> refers to the first result in the set. Setting both <code>start</code> and <code>end</code> to {@link QueryUtil#ALL_POS} will return the full result set. If <code>orderByComparator</code> is specified, then the query will include the given ORDER BY logic. If <code>orderByComparator</code> is absent and pagination is required (<code>start</code> and <code>end</code> are not {@link QueryUtil#ALL_POS}), then the query will include the default ORDER BY logic from {@link UserGroupRoleModelImpl}. 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.
3040             * </p>
3041             *
3042             * @param start the lower bound of the range of user group roles
3043             * @param end the upper bound of the range of user group roles (not inclusive)
3044             * @param orderByComparator the comparator to order the results by (optionally <code>null</code>)
3045             * @return the ordered range of user group roles
3046             */
3047            @Override
3048            public List<UserGroupRole> findAll(int start, int end,
3049                    OrderByComparator<UserGroupRole> orderByComparator) {
3050                    boolean pagination = true;
3051                    FinderPath finderPath = null;
3052                    Object[] finderArgs = null;
3053    
3054                    if ((start == QueryUtil.ALL_POS) && (end == QueryUtil.ALL_POS) &&
3055                                    (orderByComparator == null)) {
3056                            pagination = false;
3057                            finderPath = FINDER_PATH_WITHOUT_PAGINATION_FIND_ALL;
3058                            finderArgs = FINDER_ARGS_EMPTY;
3059                    }
3060                    else {
3061                            finderPath = FINDER_PATH_WITH_PAGINATION_FIND_ALL;
3062                            finderArgs = new Object[] { start, end, orderByComparator };
3063                    }
3064    
3065                    List<UserGroupRole> list = (List<UserGroupRole>)FinderCacheUtil.getResult(finderPath,
3066                                    finderArgs, this);
3067    
3068                    if (list == null) {
3069                            StringBundler query = null;
3070                            String sql = null;
3071    
3072                            if (orderByComparator != null) {
3073                                    query = new StringBundler(2 +
3074                                                    (orderByComparator.getOrderByFields().length * 3));
3075    
3076                                    query.append(_SQL_SELECT_USERGROUPROLE);
3077    
3078                                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
3079                                            orderByComparator);
3080    
3081                                    sql = query.toString();
3082                            }
3083                            else {
3084                                    sql = _SQL_SELECT_USERGROUPROLE;
3085    
3086                                    if (pagination) {
3087                                            sql = sql.concat(UserGroupRoleModelImpl.ORDER_BY_JPQL);
3088                                    }
3089                            }
3090    
3091                            Session session = null;
3092    
3093                            try {
3094                                    session = openSession();
3095    
3096                                    Query q = session.createQuery(sql);
3097    
3098                                    if (!pagination) {
3099                                            list = (List<UserGroupRole>)QueryUtil.list(q, getDialect(),
3100                                                            start, end, false);
3101    
3102                                            Collections.sort(list);
3103    
3104                                            list = Collections.unmodifiableList(list);
3105                                    }
3106                                    else {
3107                                            list = (List<UserGroupRole>)QueryUtil.list(q, getDialect(),
3108                                                            start, end);
3109                                    }
3110    
3111                                    cacheResult(list);
3112    
3113                                    FinderCacheUtil.putResult(finderPath, finderArgs, list);
3114                            }
3115                            catch (Exception e) {
3116                                    FinderCacheUtil.removeResult(finderPath, finderArgs);
3117    
3118                                    throw processException(e);
3119                            }
3120                            finally {
3121                                    closeSession(session);
3122                            }
3123                    }
3124    
3125                    return list;
3126            }
3127    
3128            /**
3129             * Removes all the user group roles from the database.
3130             *
3131             */
3132            @Override
3133            public void removeAll() {
3134                    for (UserGroupRole userGroupRole : findAll()) {
3135                            remove(userGroupRole);
3136                    }
3137            }
3138    
3139            /**
3140             * Returns the number of user group roles.
3141             *
3142             * @return the number of user group roles
3143             */
3144            @Override
3145            public int countAll() {
3146                    Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
3147                                    FINDER_ARGS_EMPTY, this);
3148    
3149                    if (count == null) {
3150                            Session session = null;
3151    
3152                            try {
3153                                    session = openSession();
3154    
3155                                    Query q = session.createQuery(_SQL_COUNT_USERGROUPROLE);
3156    
3157                                    count = (Long)q.uniqueResult();
3158    
3159                                    FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL,
3160                                            FINDER_ARGS_EMPTY, count);
3161                            }
3162                            catch (Exception e) {
3163                                    FinderCacheUtil.removeResult(FINDER_PATH_COUNT_ALL,
3164                                            FINDER_ARGS_EMPTY);
3165    
3166                                    throw processException(e);
3167                            }
3168                            finally {
3169                                    closeSession(session);
3170                            }
3171                    }
3172    
3173                    return count.intValue();
3174            }
3175    
3176            /**
3177             * Initializes the user group role persistence.
3178             */
3179            public void afterPropertiesSet() {
3180            }
3181    
3182            public void destroy() {
3183                    EntityCacheUtil.removeCache(UserGroupRoleImpl.class.getName());
3184                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_ENTITY);
3185                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITH_PAGINATION);
3186                    FinderCacheUtil.removeCache(FINDER_CLASS_NAME_LIST_WITHOUT_PAGINATION);
3187            }
3188    
3189            private static final String _SQL_SELECT_USERGROUPROLE = "SELECT userGroupRole FROM UserGroupRole userGroupRole";
3190            private static final String _SQL_SELECT_USERGROUPROLE_WHERE = "SELECT userGroupRole FROM UserGroupRole userGroupRole WHERE ";
3191            private static final String _SQL_COUNT_USERGROUPROLE = "SELECT COUNT(userGroupRole) FROM UserGroupRole userGroupRole";
3192            private static final String _SQL_COUNT_USERGROUPROLE_WHERE = "SELECT COUNT(userGroupRole) FROM UserGroupRole userGroupRole WHERE ";
3193            private static final String _ORDER_BY_ENTITY_ALIAS = "userGroupRole.";
3194            private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No UserGroupRole exists with the primary key ";
3195            private static final String _NO_SUCH_ENTITY_WITH_KEY = "No UserGroupRole exists with the key {";
3196            private static final Log _log = LogFactoryUtil.getLog(UserGroupRolePersistenceImpl.class);
3197            private static final UserGroupRole _nullUserGroupRole = new UserGroupRoleImpl() {
3198                            @Override
3199                            public Object clone() {
3200                                    return this;
3201                            }
3202    
3203                            @Override
3204                            public CacheModel<UserGroupRole> toCacheModel() {
3205                                    return _nullUserGroupRoleCacheModel;
3206                            }
3207                    };
3208    
3209            private static final CacheModel<UserGroupRole> _nullUserGroupRoleCacheModel = new NullCacheModel();
3210    
3211            private static class NullCacheModel implements CacheModel<UserGroupRole>,
3212                    MVCCModel {
3213                    @Override
3214                    public long getMvccVersion() {
3215                            return -1;
3216                    }
3217    
3218                    @Override
3219                    public void setMvccVersion(long mvccVersion) {
3220                    }
3221    
3222                    @Override
3223                    public UserGroupRole toEntityModel() {
3224                            return _nullUserGroupRole;
3225                    }
3226            }
3227    }