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