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