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