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