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