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