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