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