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