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