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