1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.service.persistence;
16  
17  import com.liferay.portal.NoSuchModelException;
18  import com.liferay.portal.NoSuchUserGroupException;
19  import com.liferay.portal.kernel.annotation.BeanReference;
20  import com.liferay.portal.kernel.cache.CacheRegistry;
21  import com.liferay.portal.kernel.dao.jdbc.MappingSqlQuery;
22  import com.liferay.portal.kernel.dao.jdbc.MappingSqlQueryFactoryUtil;
23  import com.liferay.portal.kernel.dao.jdbc.RowMapper;
24  import com.liferay.portal.kernel.dao.jdbc.SqlUpdate;
25  import com.liferay.portal.kernel.dao.jdbc.SqlUpdateFactoryUtil;
26  import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
27  import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
28  import com.liferay.portal.kernel.dao.orm.FinderPath;
29  import com.liferay.portal.kernel.dao.orm.Query;
30  import com.liferay.portal.kernel.dao.orm.QueryPos;
31  import com.liferay.portal.kernel.dao.orm.QueryUtil;
32  import com.liferay.portal.kernel.dao.orm.SQLQuery;
33  import com.liferay.portal.kernel.dao.orm.Session;
34  import com.liferay.portal.kernel.dao.orm.Type;
35  import com.liferay.portal.kernel.exception.SystemException;
36  import com.liferay.portal.kernel.log.Log;
37  import com.liferay.portal.kernel.log.LogFactoryUtil;
38  import com.liferay.portal.kernel.util.GetterUtil;
39  import com.liferay.portal.kernel.util.InstanceFactory;
40  import com.liferay.portal.kernel.util.OrderByComparator;
41  import com.liferay.portal.kernel.util.SetUtil;
42  import com.liferay.portal.kernel.util.StringBundler;
43  import com.liferay.portal.kernel.util.StringPool;
44  import com.liferay.portal.kernel.util.StringUtil;
45  import com.liferay.portal.kernel.util.Validator;
46  import com.liferay.portal.model.ModelListener;
47  import com.liferay.portal.model.UserGroup;
48  import com.liferay.portal.model.impl.UserGroupImpl;
49  import com.liferay.portal.model.impl.UserGroupModelImpl;
50  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
51  
52  import java.io.Serializable;
53  
54  import java.sql.Types;
55  
56  import java.util.ArrayList;
57  import java.util.Collections;
58  import java.util.List;
59  import java.util.Set;
60  
61  /**
62   * <a href="UserGroupPersistenceImpl.java.html"><b><i>View Source</i></b></a>
63   *
64   * <p>
65   * ServiceBuilder generated this class. Modifications in this class will be
66   * overwritten the next time is generated.
67   * </p>
68   *
69   * @author    Brian Wing Shun Chan
70   * @see       UserGroupPersistence
71   * @see       UserGroupUtil
72   * @generated
73   */
74  public class UserGroupPersistenceImpl extends BasePersistenceImpl<UserGroup>
75      implements UserGroupPersistence {
76      public static final String FINDER_CLASS_NAME_ENTITY = UserGroupImpl.class.getName();
77      public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
78          ".List";
79      public static final FinderPath FINDER_PATH_FIND_BY_COMPANYID = new FinderPath(UserGroupModelImpl.ENTITY_CACHE_ENABLED,
80              UserGroupModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
81              "findByCompanyId",
82              new String[] {
83                  Long.class.getName(),
84                  
85              "java.lang.Integer", "java.lang.Integer",
86                  "com.liferay.portal.kernel.util.OrderByComparator"
87              });
88      public static final FinderPath FINDER_PATH_COUNT_BY_COMPANYID = new FinderPath(UserGroupModelImpl.ENTITY_CACHE_ENABLED,
89              UserGroupModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
90              "countByCompanyId", new String[] { Long.class.getName() });
91      public static final FinderPath FINDER_PATH_FIND_BY_C_P = new FinderPath(UserGroupModelImpl.ENTITY_CACHE_ENABLED,
92              UserGroupModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
93              "findByC_P",
94              new String[] {
95                  Long.class.getName(), Long.class.getName(),
96                  
97              "java.lang.Integer", "java.lang.Integer",
98                  "com.liferay.portal.kernel.util.OrderByComparator"
99              });
100     public static final FinderPath FINDER_PATH_COUNT_BY_C_P = new FinderPath(UserGroupModelImpl.ENTITY_CACHE_ENABLED,
101             UserGroupModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
102             "countByC_P",
103             new String[] { Long.class.getName(), Long.class.getName() });
104     public static final FinderPath FINDER_PATH_FETCH_BY_C_N = new FinderPath(UserGroupModelImpl.ENTITY_CACHE_ENABLED,
105             UserGroupModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_ENTITY,
106             "fetchByC_N",
107             new String[] { Long.class.getName(), String.class.getName() });
108     public static final FinderPath FINDER_PATH_COUNT_BY_C_N = new FinderPath(UserGroupModelImpl.ENTITY_CACHE_ENABLED,
109             UserGroupModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
110             "countByC_N",
111             new String[] { Long.class.getName(), String.class.getName() });
112     public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(UserGroupModelImpl.ENTITY_CACHE_ENABLED,
113             UserGroupModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
114             "findAll", new String[0]);
115     public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(UserGroupModelImpl.ENTITY_CACHE_ENABLED,
116             UserGroupModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
117             "countAll", new String[0]);
118 
119     public void cacheResult(UserGroup userGroup) {
120         EntityCacheUtil.putResult(UserGroupModelImpl.ENTITY_CACHE_ENABLED,
121             UserGroupImpl.class, userGroup.getPrimaryKey(), userGroup);
122 
123         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_N,
124             new Object[] { new Long(userGroup.getCompanyId()), userGroup.getName() },
125             userGroup);
126     }
127 
128     public void cacheResult(List<UserGroup> userGroups) {
129         for (UserGroup userGroup : userGroups) {
130             if (EntityCacheUtil.getResult(
131                         UserGroupModelImpl.ENTITY_CACHE_ENABLED,
132                         UserGroupImpl.class, userGroup.getPrimaryKey(), this) == null) {
133                 cacheResult(userGroup);
134             }
135         }
136     }
137 
138     public void clearCache() {
139         CacheRegistry.clear(UserGroupImpl.class.getName());
140         EntityCacheUtil.clearCache(UserGroupImpl.class.getName());
141         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
142         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
143     }
144 
145     public void clearCache(UserGroup userGroup) {
146         EntityCacheUtil.removeResult(UserGroupModelImpl.ENTITY_CACHE_ENABLED,
147             UserGroupImpl.class, userGroup.getPrimaryKey());
148 
149         FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_N,
150             new Object[] { new Long(userGroup.getCompanyId()), userGroup.getName() });
151     }
152 
153     public UserGroup create(long userGroupId) {
154         UserGroup userGroup = new UserGroupImpl();
155 
156         userGroup.setNew(true);
157         userGroup.setPrimaryKey(userGroupId);
158 
159         return userGroup;
160     }
161 
162     public UserGroup remove(Serializable primaryKey)
163         throws NoSuchModelException, SystemException {
164         return remove(((Long)primaryKey).longValue());
165     }
166 
167     public UserGroup remove(long userGroupId)
168         throws NoSuchUserGroupException, SystemException {
169         Session session = null;
170 
171         try {
172             session = openSession();
173 
174             UserGroup userGroup = (UserGroup)session.get(UserGroupImpl.class,
175                     new Long(userGroupId));
176 
177             if (userGroup == null) {
178                 if (_log.isWarnEnabled()) {
179                     _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + userGroupId);
180                 }
181 
182                 throw new NoSuchUserGroupException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
183                     userGroupId);
184             }
185 
186             return remove(userGroup);
187         }
188         catch (NoSuchUserGroupException nsee) {
189             throw nsee;
190         }
191         catch (Exception e) {
192             throw processException(e);
193         }
194         finally {
195             closeSession(session);
196         }
197     }
198 
199     public UserGroup remove(UserGroup userGroup) throws SystemException {
200         for (ModelListener<UserGroup> listener : listeners) {
201             listener.onBeforeRemove(userGroup);
202         }
203 
204         userGroup = removeImpl(userGroup);
205 
206         for (ModelListener<UserGroup> listener : listeners) {
207             listener.onAfterRemove(userGroup);
208         }
209 
210         return userGroup;
211     }
212 
213     protected UserGroup removeImpl(UserGroup userGroup)
214         throws SystemException {
215         userGroup = toUnwrappedModel(userGroup);
216 
217         try {
218             clearGroups.clear(userGroup.getPrimaryKey());
219         }
220         catch (Exception e) {
221             throw processException(e);
222         }
223         finally {
224             FinderCacheUtil.clearCache(UserGroupModelImpl.MAPPING_TABLE_GROUPS_USERGROUPS_NAME);
225         }
226 
227         try {
228             clearUsers.clear(userGroup.getPrimaryKey());
229         }
230         catch (Exception e) {
231             throw processException(e);
232         }
233         finally {
234             FinderCacheUtil.clearCache(UserGroupModelImpl.MAPPING_TABLE_USERS_USERGROUPS_NAME);
235         }
236 
237         Session session = null;
238 
239         try {
240             session = openSession();
241 
242             if (userGroup.isCachedModel() || BatchSessionUtil.isEnabled()) {
243                 Object staleObject = session.get(UserGroupImpl.class,
244                         userGroup.getPrimaryKeyObj());
245 
246                 if (staleObject != null) {
247                     session.evict(staleObject);
248                 }
249             }
250 
251             session.delete(userGroup);
252 
253             session.flush();
254         }
255         catch (Exception e) {
256             throw processException(e);
257         }
258         finally {
259             closeSession(session);
260         }
261 
262         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
263 
264         UserGroupModelImpl userGroupModelImpl = (UserGroupModelImpl)userGroup;
265 
266         FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_N,
267             new Object[] {
268                 new Long(userGroupModelImpl.getOriginalCompanyId()),
269                 
270             userGroupModelImpl.getOriginalName()
271             });
272 
273         EntityCacheUtil.removeResult(UserGroupModelImpl.ENTITY_CACHE_ENABLED,
274             UserGroupImpl.class, userGroup.getPrimaryKey());
275 
276         return userGroup;
277     }
278 
279     public UserGroup updateImpl(com.liferay.portal.model.UserGroup userGroup,
280         boolean merge) throws SystemException {
281         userGroup = toUnwrappedModel(userGroup);
282 
283         boolean isNew = userGroup.isNew();
284 
285         UserGroupModelImpl userGroupModelImpl = (UserGroupModelImpl)userGroup;
286 
287         Session session = null;
288 
289         try {
290             session = openSession();
291 
292             BatchSessionUtil.update(session, userGroup, merge);
293 
294             userGroup.setNew(false);
295         }
296         catch (Exception e) {
297             throw processException(e);
298         }
299         finally {
300             closeSession(session);
301         }
302 
303         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
304 
305         EntityCacheUtil.putResult(UserGroupModelImpl.ENTITY_CACHE_ENABLED,
306             UserGroupImpl.class, userGroup.getPrimaryKey(), userGroup);
307 
308         if (!isNew &&
309                 ((userGroup.getCompanyId() != userGroupModelImpl.getOriginalCompanyId()) ||
310                 !Validator.equals(userGroup.getName(),
311                     userGroupModelImpl.getOriginalName()))) {
312             FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_N,
313                 new Object[] {
314                     new Long(userGroupModelImpl.getOriginalCompanyId()),
315                     
316                 userGroupModelImpl.getOriginalName()
317                 });
318         }
319 
320         if (isNew ||
321                 ((userGroup.getCompanyId() != userGroupModelImpl.getOriginalCompanyId()) ||
322                 !Validator.equals(userGroup.getName(),
323                     userGroupModelImpl.getOriginalName()))) {
324             FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_N,
325                 new Object[] {
326                     new Long(userGroup.getCompanyId()),
327                     
328                 userGroup.getName()
329                 }, userGroup);
330         }
331 
332         return userGroup;
333     }
334 
335     protected UserGroup toUnwrappedModel(UserGroup userGroup) {
336         if (userGroup instanceof UserGroupImpl) {
337             return userGroup;
338         }
339 
340         UserGroupImpl userGroupImpl = new UserGroupImpl();
341 
342         userGroupImpl.setNew(userGroup.isNew());
343         userGroupImpl.setPrimaryKey(userGroup.getPrimaryKey());
344 
345         userGroupImpl.setUserGroupId(userGroup.getUserGroupId());
346         userGroupImpl.setCompanyId(userGroup.getCompanyId());
347         userGroupImpl.setParentUserGroupId(userGroup.getParentUserGroupId());
348         userGroupImpl.setName(userGroup.getName());
349         userGroupImpl.setDescription(userGroup.getDescription());
350 
351         return userGroupImpl;
352     }
353 
354     public UserGroup findByPrimaryKey(Serializable primaryKey)
355         throws NoSuchModelException, SystemException {
356         return findByPrimaryKey(((Long)primaryKey).longValue());
357     }
358 
359     public UserGroup findByPrimaryKey(long userGroupId)
360         throws NoSuchUserGroupException, SystemException {
361         UserGroup userGroup = fetchByPrimaryKey(userGroupId);
362 
363         if (userGroup == null) {
364             if (_log.isWarnEnabled()) {
365                 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + userGroupId);
366             }
367 
368             throw new NoSuchUserGroupException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
369                 userGroupId);
370         }
371 
372         return userGroup;
373     }
374 
375     public UserGroup fetchByPrimaryKey(Serializable primaryKey)
376         throws SystemException {
377         return fetchByPrimaryKey(((Long)primaryKey).longValue());
378     }
379 
380     public UserGroup fetchByPrimaryKey(long userGroupId)
381         throws SystemException {
382         UserGroup userGroup = (UserGroup)EntityCacheUtil.getResult(UserGroupModelImpl.ENTITY_CACHE_ENABLED,
383                 UserGroupImpl.class, userGroupId, this);
384 
385         if (userGroup == null) {
386             Session session = null;
387 
388             try {
389                 session = openSession();
390 
391                 userGroup = (UserGroup)session.get(UserGroupImpl.class,
392                         new Long(userGroupId));
393             }
394             catch (Exception e) {
395                 throw processException(e);
396             }
397             finally {
398                 if (userGroup != null) {
399                     cacheResult(userGroup);
400                 }
401 
402                 closeSession(session);
403             }
404         }
405 
406         return userGroup;
407     }
408 
409     public List<UserGroup> findByCompanyId(long companyId)
410         throws SystemException {
411         return findByCompanyId(companyId, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
412             null);
413     }
414 
415     public List<UserGroup> findByCompanyId(long companyId, int start, int end)
416         throws SystemException {
417         return findByCompanyId(companyId, start, end, null);
418     }
419 
420     public List<UserGroup> findByCompanyId(long companyId, int start, int end,
421         OrderByComparator orderByComparator) throws SystemException {
422         Object[] finderArgs = new Object[] {
423                 new Long(companyId),
424                 
425                 String.valueOf(start), String.valueOf(end),
426                 String.valueOf(orderByComparator)
427             };
428 
429         List<UserGroup> list = (List<UserGroup>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_COMPANYID,
430                 finderArgs, this);
431 
432         if (list == null) {
433             Session session = null;
434 
435             try {
436                 session = openSession();
437 
438                 StringBundler query = null;
439 
440                 if (orderByComparator != null) {
441                     query = new StringBundler(3 +
442                             (orderByComparator.getOrderByFields().length * 3));
443                 }
444                 else {
445                     query = new StringBundler(3);
446                 }
447 
448                 query.append(_SQL_SELECT_USERGROUP_WHERE);
449 
450                 query.append(_FINDER_COLUMN_COMPANYID_COMPANYID_2);
451 
452                 if (orderByComparator != null) {
453                     appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
454                         orderByComparator);
455                 }
456 
457                 else {
458                     query.append(UserGroupModelImpl.ORDER_BY_JPQL);
459                 }
460 
461                 String sql = query.toString();
462 
463                 Query q = session.createQuery(sql);
464 
465                 QueryPos qPos = QueryPos.getInstance(q);
466 
467                 qPos.add(companyId);
468 
469                 list = (List<UserGroup>)QueryUtil.list(q, getDialect(), start,
470                         end);
471             }
472             catch (Exception e) {
473                 throw processException(e);
474             }
475             finally {
476                 if (list == null) {
477                     list = new ArrayList<UserGroup>();
478                 }
479 
480                 cacheResult(list);
481 
482                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_COMPANYID,
483                     finderArgs, list);
484 
485                 closeSession(session);
486             }
487         }
488 
489         return list;
490     }
491 
492     public UserGroup findByCompanyId_First(long companyId,
493         OrderByComparator orderByComparator)
494         throws NoSuchUserGroupException, SystemException {
495         List<UserGroup> list = findByCompanyId(companyId, 0, 1,
496                 orderByComparator);
497 
498         if (list.isEmpty()) {
499             StringBundler msg = new StringBundler(4);
500 
501             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
502 
503             msg.append("companyId=");
504             msg.append(companyId);
505 
506             msg.append(StringPool.CLOSE_CURLY_BRACE);
507 
508             throw new NoSuchUserGroupException(msg.toString());
509         }
510         else {
511             return list.get(0);
512         }
513     }
514 
515     public UserGroup findByCompanyId_Last(long companyId,
516         OrderByComparator orderByComparator)
517         throws NoSuchUserGroupException, SystemException {
518         int count = countByCompanyId(companyId);
519 
520         List<UserGroup> list = findByCompanyId(companyId, count - 1, count,
521                 orderByComparator);
522 
523         if (list.isEmpty()) {
524             StringBundler msg = new StringBundler(4);
525 
526             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
527 
528             msg.append("companyId=");
529             msg.append(companyId);
530 
531             msg.append(StringPool.CLOSE_CURLY_BRACE);
532 
533             throw new NoSuchUserGroupException(msg.toString());
534         }
535         else {
536             return list.get(0);
537         }
538     }
539 
540     public UserGroup[] findByCompanyId_PrevAndNext(long userGroupId,
541         long companyId, OrderByComparator orderByComparator)
542         throws NoSuchUserGroupException, SystemException {
543         UserGroup userGroup = findByPrimaryKey(userGroupId);
544 
545         Session session = null;
546 
547         try {
548             session = openSession();
549 
550             UserGroup[] array = new UserGroupImpl[3];
551 
552             array[0] = getByCompanyId_PrevAndNext(session, userGroup,
553                     companyId, orderByComparator, true);
554 
555             array[1] = userGroup;
556 
557             array[2] = getByCompanyId_PrevAndNext(session, userGroup,
558                     companyId, orderByComparator, false);
559 
560             return array;
561         }
562         catch (Exception e) {
563             throw processException(e);
564         }
565         finally {
566             closeSession(session);
567         }
568     }
569 
570     protected UserGroup getByCompanyId_PrevAndNext(Session session,
571         UserGroup userGroup, long companyId,
572         OrderByComparator orderByComparator, boolean previous) {
573         StringBundler query = null;
574 
575         if (orderByComparator != null) {
576             query = new StringBundler(6 +
577                     (orderByComparator.getOrderByFields().length * 6));
578         }
579         else {
580             query = new StringBundler(3);
581         }
582 
583         query.append(_SQL_SELECT_USERGROUP_WHERE);
584 
585         query.append(_FINDER_COLUMN_COMPANYID_COMPANYID_2);
586 
587         if (orderByComparator != null) {
588             String[] orderByFields = orderByComparator.getOrderByFields();
589 
590             if (orderByFields.length > 0) {
591                 query.append(WHERE_AND);
592             }
593 
594             for (int i = 0; i < orderByFields.length; i++) {
595                 query.append(_ORDER_BY_ENTITY_ALIAS);
596                 query.append(orderByFields[i]);
597 
598                 if ((i + 1) < orderByFields.length) {
599                     if (orderByComparator.isAscending() ^ previous) {
600                         query.append(WHERE_GREATER_THAN_HAS_NEXT);
601                     }
602                     else {
603                         query.append(WHERE_LESSER_THAN_HAS_NEXT);
604                     }
605                 }
606                 else {
607                     if (orderByComparator.isAscending() ^ previous) {
608                         query.append(WHERE_GREATER_THAN);
609                     }
610                     else {
611                         query.append(WHERE_LESSER_THAN);
612                     }
613                 }
614             }
615 
616             query.append(ORDER_BY_CLAUSE);
617 
618             for (int i = 0; i < orderByFields.length; i++) {
619                 query.append(_ORDER_BY_ENTITY_ALIAS);
620                 query.append(orderByFields[i]);
621 
622                 if ((i + 1) < orderByFields.length) {
623                     if (orderByComparator.isAscending() ^ previous) {
624                         query.append(ORDER_BY_ASC_HAS_NEXT);
625                     }
626                     else {
627                         query.append(ORDER_BY_DESC_HAS_NEXT);
628                     }
629                 }
630                 else {
631                     if (orderByComparator.isAscending() ^ previous) {
632                         query.append(ORDER_BY_ASC);
633                     }
634                     else {
635                         query.append(ORDER_BY_DESC);
636                     }
637                 }
638             }
639         }
640 
641         else {
642             query.append(UserGroupModelImpl.ORDER_BY_JPQL);
643         }
644 
645         String sql = query.toString();
646 
647         Query q = session.createQuery(sql);
648 
649         q.setFirstResult(0);
650         q.setMaxResults(2);
651 
652         QueryPos qPos = QueryPos.getInstance(q);
653 
654         qPos.add(companyId);
655 
656         if (orderByComparator != null) {
657             Object[] values = orderByComparator.getOrderByValues(userGroup);
658 
659             for (Object value : values) {
660                 qPos.add(value);
661             }
662         }
663 
664         List<UserGroup> list = q.list();
665 
666         if (list.size() == 2) {
667             return list.get(1);
668         }
669         else {
670             return null;
671         }
672     }
673 
674     public List<UserGroup> findByC_P(long companyId, long parentUserGroupId)
675         throws SystemException {
676         return findByC_P(companyId, parentUserGroupId, QueryUtil.ALL_POS,
677             QueryUtil.ALL_POS, null);
678     }
679 
680     public List<UserGroup> findByC_P(long companyId, long parentUserGroupId,
681         int start, int end) throws SystemException {
682         return findByC_P(companyId, parentUserGroupId, start, end, null);
683     }
684 
685     public List<UserGroup> findByC_P(long companyId, long parentUserGroupId,
686         int start, int end, OrderByComparator orderByComparator)
687         throws SystemException {
688         Object[] finderArgs = new Object[] {
689                 new Long(companyId), new Long(parentUserGroupId),
690                 
691                 String.valueOf(start), String.valueOf(end),
692                 String.valueOf(orderByComparator)
693             };
694 
695         List<UserGroup> list = (List<UserGroup>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_C_P,
696                 finderArgs, this);
697 
698         if (list == null) {
699             Session session = null;
700 
701             try {
702                 session = openSession();
703 
704                 StringBundler query = null;
705 
706                 if (orderByComparator != null) {
707                     query = new StringBundler(4 +
708                             (orderByComparator.getOrderByFields().length * 3));
709                 }
710                 else {
711                     query = new StringBundler(4);
712                 }
713 
714                 query.append(_SQL_SELECT_USERGROUP_WHERE);
715 
716                 query.append(_FINDER_COLUMN_C_P_COMPANYID_2);
717 
718                 query.append(_FINDER_COLUMN_C_P_PARENTUSERGROUPID_2);
719 
720                 if (orderByComparator != null) {
721                     appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
722                         orderByComparator);
723                 }
724 
725                 else {
726                     query.append(UserGroupModelImpl.ORDER_BY_JPQL);
727                 }
728 
729                 String sql = query.toString();
730 
731                 Query q = session.createQuery(sql);
732 
733                 QueryPos qPos = QueryPos.getInstance(q);
734 
735                 qPos.add(companyId);
736 
737                 qPos.add(parentUserGroupId);
738 
739                 list = (List<UserGroup>)QueryUtil.list(q, getDialect(), start,
740                         end);
741             }
742             catch (Exception e) {
743                 throw processException(e);
744             }
745             finally {
746                 if (list == null) {
747                     list = new ArrayList<UserGroup>();
748                 }
749 
750                 cacheResult(list);
751 
752                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_C_P, finderArgs,
753                     list);
754 
755                 closeSession(session);
756             }
757         }
758 
759         return list;
760     }
761 
762     public UserGroup findByC_P_First(long companyId, long parentUserGroupId,
763         OrderByComparator orderByComparator)
764         throws NoSuchUserGroupException, SystemException {
765         List<UserGroup> list = findByC_P(companyId, parentUserGroupId, 0, 1,
766                 orderByComparator);
767 
768         if (list.isEmpty()) {
769             StringBundler msg = new StringBundler(6);
770 
771             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
772 
773             msg.append("companyId=");
774             msg.append(companyId);
775 
776             msg.append(", parentUserGroupId=");
777             msg.append(parentUserGroupId);
778 
779             msg.append(StringPool.CLOSE_CURLY_BRACE);
780 
781             throw new NoSuchUserGroupException(msg.toString());
782         }
783         else {
784             return list.get(0);
785         }
786     }
787 
788     public UserGroup findByC_P_Last(long companyId, long parentUserGroupId,
789         OrderByComparator orderByComparator)
790         throws NoSuchUserGroupException, SystemException {
791         int count = countByC_P(companyId, parentUserGroupId);
792 
793         List<UserGroup> list = findByC_P(companyId, parentUserGroupId,
794                 count - 1, count, orderByComparator);
795 
796         if (list.isEmpty()) {
797             StringBundler msg = new StringBundler(6);
798 
799             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
800 
801             msg.append("companyId=");
802             msg.append(companyId);
803 
804             msg.append(", parentUserGroupId=");
805             msg.append(parentUserGroupId);
806 
807             msg.append(StringPool.CLOSE_CURLY_BRACE);
808 
809             throw new NoSuchUserGroupException(msg.toString());
810         }
811         else {
812             return list.get(0);
813         }
814     }
815 
816     public UserGroup[] findByC_P_PrevAndNext(long userGroupId, long companyId,
817         long parentUserGroupId, OrderByComparator orderByComparator)
818         throws NoSuchUserGroupException, SystemException {
819         UserGroup userGroup = findByPrimaryKey(userGroupId);
820 
821         Session session = null;
822 
823         try {
824             session = openSession();
825 
826             UserGroup[] array = new UserGroupImpl[3];
827 
828             array[0] = getByC_P_PrevAndNext(session, userGroup, companyId,
829                     parentUserGroupId, orderByComparator, true);
830 
831             array[1] = userGroup;
832 
833             array[2] = getByC_P_PrevAndNext(session, userGroup, companyId,
834                     parentUserGroupId, orderByComparator, false);
835 
836             return array;
837         }
838         catch (Exception e) {
839             throw processException(e);
840         }
841         finally {
842             closeSession(session);
843         }
844     }
845 
846     protected UserGroup getByC_P_PrevAndNext(Session session,
847         UserGroup userGroup, long companyId, long parentUserGroupId,
848         OrderByComparator orderByComparator, boolean previous) {
849         StringBundler query = null;
850 
851         if (orderByComparator != null) {
852             query = new StringBundler(6 +
853                     (orderByComparator.getOrderByFields().length * 6));
854         }
855         else {
856             query = new StringBundler(3);
857         }
858 
859         query.append(_SQL_SELECT_USERGROUP_WHERE);
860 
861         query.append(_FINDER_COLUMN_C_P_COMPANYID_2);
862 
863         query.append(_FINDER_COLUMN_C_P_PARENTUSERGROUPID_2);
864 
865         if (orderByComparator != null) {
866             String[] orderByFields = orderByComparator.getOrderByFields();
867 
868             if (orderByFields.length > 0) {
869                 query.append(WHERE_AND);
870             }
871 
872             for (int i = 0; i < orderByFields.length; i++) {
873                 query.append(_ORDER_BY_ENTITY_ALIAS);
874                 query.append(orderByFields[i]);
875 
876                 if ((i + 1) < orderByFields.length) {
877                     if (orderByComparator.isAscending() ^ previous) {
878                         query.append(WHERE_GREATER_THAN_HAS_NEXT);
879                     }
880                     else {
881                         query.append(WHERE_LESSER_THAN_HAS_NEXT);
882                     }
883                 }
884                 else {
885                     if (orderByComparator.isAscending() ^ previous) {
886                         query.append(WHERE_GREATER_THAN);
887                     }
888                     else {
889                         query.append(WHERE_LESSER_THAN);
890                     }
891                 }
892             }
893 
894             query.append(ORDER_BY_CLAUSE);
895 
896             for (int i = 0; i < orderByFields.length; i++) {
897                 query.append(_ORDER_BY_ENTITY_ALIAS);
898                 query.append(orderByFields[i]);
899 
900                 if ((i + 1) < orderByFields.length) {
901                     if (orderByComparator.isAscending() ^ previous) {
902                         query.append(ORDER_BY_ASC_HAS_NEXT);
903                     }
904                     else {
905                         query.append(ORDER_BY_DESC_HAS_NEXT);
906                     }
907                 }
908                 else {
909                     if (orderByComparator.isAscending() ^ previous) {
910                         query.append(ORDER_BY_ASC);
911                     }
912                     else {
913                         query.append(ORDER_BY_DESC);
914                     }
915                 }
916             }
917         }
918 
919         else {
920             query.append(UserGroupModelImpl.ORDER_BY_JPQL);
921         }
922 
923         String sql = query.toString();
924 
925         Query q = session.createQuery(sql);
926 
927         q.setFirstResult(0);
928         q.setMaxResults(2);
929 
930         QueryPos qPos = QueryPos.getInstance(q);
931 
932         qPos.add(companyId);
933 
934         qPos.add(parentUserGroupId);
935 
936         if (orderByComparator != null) {
937             Object[] values = orderByComparator.getOrderByValues(userGroup);
938 
939             for (Object value : values) {
940                 qPos.add(value);
941             }
942         }
943 
944         List<UserGroup> list = q.list();
945 
946         if (list.size() == 2) {
947             return list.get(1);
948         }
949         else {
950             return null;
951         }
952     }
953 
954     public UserGroup findByC_N(long companyId, String name)
955         throws NoSuchUserGroupException, SystemException {
956         UserGroup userGroup = fetchByC_N(companyId, name);
957 
958         if (userGroup == null) {
959             StringBundler msg = new StringBundler(6);
960 
961             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
962 
963             msg.append("companyId=");
964             msg.append(companyId);
965 
966             msg.append(", name=");
967             msg.append(name);
968 
969             msg.append(StringPool.CLOSE_CURLY_BRACE);
970 
971             if (_log.isWarnEnabled()) {
972                 _log.warn(msg.toString());
973             }
974 
975             throw new NoSuchUserGroupException(msg.toString());
976         }
977 
978         return userGroup;
979     }
980 
981     public UserGroup fetchByC_N(long companyId, String name)
982         throws SystemException {
983         return fetchByC_N(companyId, name, true);
984     }
985 
986     public UserGroup fetchByC_N(long companyId, String name,
987         boolean retrieveFromCache) throws SystemException {
988         Object[] finderArgs = new Object[] { new Long(companyId), name };
989 
990         Object result = null;
991 
992         if (retrieveFromCache) {
993             result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_C_N,
994                     finderArgs, this);
995         }
996 
997         if (result == null) {
998             Session session = null;
999 
1000            try {
1001                session = openSession();
1002
1003                StringBundler query = new StringBundler(4);
1004
1005                query.append(_SQL_SELECT_USERGROUP_WHERE);
1006
1007                query.append(_FINDER_COLUMN_C_N_COMPANYID_2);
1008
1009                if (name == null) {
1010                    query.append(_FINDER_COLUMN_C_N_NAME_1);
1011                }
1012                else {
1013                    if (name.equals(StringPool.BLANK)) {
1014                        query.append(_FINDER_COLUMN_C_N_NAME_3);
1015                    }
1016                    else {
1017                        query.append(_FINDER_COLUMN_C_N_NAME_2);
1018                    }
1019                }
1020
1021                query.append(UserGroupModelImpl.ORDER_BY_JPQL);
1022
1023                String sql = query.toString();
1024
1025                Query q = session.createQuery(sql);
1026
1027                QueryPos qPos = QueryPos.getInstance(q);
1028
1029                qPos.add(companyId);
1030
1031                if (name != null) {
1032                    qPos.add(name);
1033                }
1034
1035                List<UserGroup> list = q.list();
1036
1037                result = list;
1038
1039                UserGroup userGroup = null;
1040
1041                if (list.isEmpty()) {
1042                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_N,
1043                        finderArgs, list);
1044                }
1045                else {
1046                    userGroup = list.get(0);
1047
1048                    cacheResult(userGroup);
1049
1050                    if ((userGroup.getCompanyId() != companyId) ||
1051                            (userGroup.getName() == null) ||
1052                            !userGroup.getName().equals(name)) {
1053                        FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_N,
1054                            finderArgs, userGroup);
1055                    }
1056                }
1057
1058                return userGroup;
1059            }
1060            catch (Exception e) {
1061                throw processException(e);
1062            }
1063            finally {
1064                if (result == null) {
1065                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_N,
1066                        finderArgs, new ArrayList<UserGroup>());
1067                }
1068
1069                closeSession(session);
1070            }
1071        }
1072        else {
1073            if (result instanceof List<?>) {
1074                return null;
1075            }
1076            else {
1077                return (UserGroup)result;
1078            }
1079        }
1080    }
1081
1082    public List<UserGroup> findAll() throws SystemException {
1083        return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
1084    }
1085
1086    public List<UserGroup> findAll(int start, int end)
1087        throws SystemException {
1088        return findAll(start, end, null);
1089    }
1090
1091    public List<UserGroup> findAll(int start, int end,
1092        OrderByComparator orderByComparator) throws SystemException {
1093        Object[] finderArgs = new Object[] {
1094                String.valueOf(start), String.valueOf(end),
1095                String.valueOf(orderByComparator)
1096            };
1097
1098        List<UserGroup> list = (List<UserGroup>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
1099                finderArgs, this);
1100
1101        if (list == null) {
1102            Session session = null;
1103
1104            try {
1105                session = openSession();
1106
1107                StringBundler query = null;
1108                String sql = null;
1109
1110                if (orderByComparator != null) {
1111                    query = new StringBundler(2 +
1112                            (orderByComparator.getOrderByFields().length * 3));
1113
1114                    query.append(_SQL_SELECT_USERGROUP);
1115
1116                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1117                        orderByComparator);
1118
1119                    sql = query.toString();
1120                }
1121
1122                else {
1123                    sql = _SQL_SELECT_USERGROUP.concat(UserGroupModelImpl.ORDER_BY_JPQL);
1124                }
1125
1126                Query q = session.createQuery(sql);
1127
1128                if (orderByComparator == null) {
1129                    list = (List<UserGroup>)QueryUtil.list(q, getDialect(),
1130                            start, end, false);
1131
1132                    Collections.sort(list);
1133                }
1134                else {
1135                    list = (List<UserGroup>)QueryUtil.list(q, getDialect(),
1136                            start, end);
1137                }
1138            }
1139            catch (Exception e) {
1140                throw processException(e);
1141            }
1142            finally {
1143                if (list == null) {
1144                    list = new ArrayList<UserGroup>();
1145                }
1146
1147                cacheResult(list);
1148
1149                FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
1150
1151                closeSession(session);
1152            }
1153        }
1154
1155        return list;
1156    }
1157
1158    public void removeByCompanyId(long companyId) throws SystemException {
1159        for (UserGroup userGroup : findByCompanyId(companyId)) {
1160            remove(userGroup);
1161        }
1162    }
1163
1164    public void removeByC_P(long companyId, long parentUserGroupId)
1165        throws SystemException {
1166        for (UserGroup userGroup : findByC_P(companyId, parentUserGroupId)) {
1167            remove(userGroup);
1168        }
1169    }
1170
1171    public void removeByC_N(long companyId, String name)
1172        throws NoSuchUserGroupException, SystemException {
1173        UserGroup userGroup = findByC_N(companyId, name);
1174
1175        remove(userGroup);
1176    }
1177
1178    public void removeAll() throws SystemException {
1179        for (UserGroup userGroup : findAll()) {
1180            remove(userGroup);
1181        }
1182    }
1183
1184    public int countByCompanyId(long companyId) throws SystemException {
1185        Object[] finderArgs = new Object[] { new Long(companyId) };
1186
1187        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_COMPANYID,
1188                finderArgs, this);
1189
1190        if (count == null) {
1191            Session session = null;
1192
1193            try {
1194                session = openSession();
1195
1196                StringBundler query = new StringBundler(2);
1197
1198                query.append(_SQL_COUNT_USERGROUP_WHERE);
1199
1200                query.append(_FINDER_COLUMN_COMPANYID_COMPANYID_2);
1201
1202                String sql = query.toString();
1203
1204                Query q = session.createQuery(sql);
1205
1206                QueryPos qPos = QueryPos.getInstance(q);
1207
1208                qPos.add(companyId);
1209
1210                count = (Long)q.uniqueResult();
1211            }
1212            catch (Exception e) {
1213                throw processException(e);
1214            }
1215            finally {
1216                if (count == null) {
1217                    count = Long.valueOf(0);
1218                }
1219
1220                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_COMPANYID,
1221                    finderArgs, count);
1222
1223                closeSession(session);
1224            }
1225        }
1226
1227        return count.intValue();
1228    }
1229
1230    public int countByC_P(long companyId, long parentUserGroupId)
1231        throws SystemException {
1232        Object[] finderArgs = new Object[] {
1233                new Long(companyId), new Long(parentUserGroupId)
1234            };
1235
1236        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_C_P,
1237                finderArgs, this);
1238
1239        if (count == null) {
1240            Session session = null;
1241
1242            try {
1243                session = openSession();
1244
1245                StringBundler query = new StringBundler(3);
1246
1247                query.append(_SQL_COUNT_USERGROUP_WHERE);
1248
1249                query.append(_FINDER_COLUMN_C_P_COMPANYID_2);
1250
1251                query.append(_FINDER_COLUMN_C_P_PARENTUSERGROUPID_2);
1252
1253                String sql = query.toString();
1254
1255                Query q = session.createQuery(sql);
1256
1257                QueryPos qPos = QueryPos.getInstance(q);
1258
1259                qPos.add(companyId);
1260
1261                qPos.add(parentUserGroupId);
1262
1263                count = (Long)q.uniqueResult();
1264            }
1265            catch (Exception e) {
1266                throw processException(e);
1267            }
1268            finally {
1269                if (count == null) {
1270                    count = Long.valueOf(0);
1271                }
1272
1273                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_P, finderArgs,
1274                    count);
1275
1276                closeSession(session);
1277            }
1278        }
1279
1280        return count.intValue();
1281    }
1282
1283    public int countByC_N(long companyId, String name)
1284        throws SystemException {
1285        Object[] finderArgs = new Object[] { new Long(companyId), name };
1286
1287        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_C_N,
1288                finderArgs, this);
1289
1290        if (count == null) {
1291            Session session = null;
1292
1293            try {
1294                session = openSession();
1295
1296                StringBundler query = new StringBundler(3);
1297
1298                query.append(_SQL_COUNT_USERGROUP_WHERE);
1299
1300                query.append(_FINDER_COLUMN_C_N_COMPANYID_2);
1301
1302                if (name == null) {
1303                    query.append(_FINDER_COLUMN_C_N_NAME_1);
1304                }
1305                else {
1306                    if (name.equals(StringPool.BLANK)) {
1307                        query.append(_FINDER_COLUMN_C_N_NAME_3);
1308                    }
1309                    else {
1310                        query.append(_FINDER_COLUMN_C_N_NAME_2);
1311                    }
1312                }
1313
1314                String sql = query.toString();
1315
1316                Query q = session.createQuery(sql);
1317
1318                QueryPos qPos = QueryPos.getInstance(q);
1319
1320                qPos.add(companyId);
1321
1322                if (name != null) {
1323                    qPos.add(name);
1324                }
1325
1326                count = (Long)q.uniqueResult();
1327            }
1328            catch (Exception e) {
1329                throw processException(e);
1330            }
1331            finally {
1332                if (count == null) {
1333                    count = Long.valueOf(0);
1334                }
1335
1336                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_N, finderArgs,
1337                    count);
1338
1339                closeSession(session);
1340            }
1341        }
1342
1343        return count.intValue();
1344    }
1345
1346    public int countAll() throws SystemException {
1347        Object[] finderArgs = new Object[0];
1348
1349        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1350                finderArgs, this);
1351
1352        if (count == null) {
1353            Session session = null;
1354
1355            try {
1356                session = openSession();
1357
1358                Query q = session.createQuery(_SQL_COUNT_USERGROUP);
1359
1360                count = (Long)q.uniqueResult();
1361            }
1362            catch (Exception e) {
1363                throw processException(e);
1364            }
1365            finally {
1366                if (count == null) {
1367                    count = Long.valueOf(0);
1368                }
1369
1370                FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
1371                    count);
1372
1373                closeSession(session);
1374            }
1375        }
1376
1377        return count.intValue();
1378    }
1379
1380    public List<com.liferay.portal.model.Group> getGroups(long pk)
1381        throws SystemException {
1382        return getGroups(pk, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
1383    }
1384
1385    public List<com.liferay.portal.model.Group> getGroups(long pk, int start,
1386        int end) throws SystemException {
1387        return getGroups(pk, start, end, null);
1388    }
1389
1390    public static final FinderPath FINDER_PATH_GET_GROUPS = new FinderPath(com.liferay.portal.model.impl.GroupModelImpl.ENTITY_CACHE_ENABLED,
1391            UserGroupModelImpl.FINDER_CACHE_ENABLED_GROUPS_USERGROUPS,
1392            UserGroupModelImpl.MAPPING_TABLE_GROUPS_USERGROUPS_NAME,
1393            "getGroups",
1394            new String[] {
1395                Long.class.getName(), "java.lang.Integer", "java.lang.Integer",
1396                "com.liferay.portal.kernel.util.OrderByComparator"
1397            });
1398
1399    public List<com.liferay.portal.model.Group> getGroups(long pk, int start,
1400        int end, OrderByComparator orderByComparator) throws SystemException {
1401        Object[] finderArgs = new Object[] {
1402                new Long(pk), String.valueOf(start), String.valueOf(end),
1403                String.valueOf(orderByComparator)
1404            };
1405
1406        List<com.liferay.portal.model.Group> list = (List<com.liferay.portal.model.Group>)FinderCacheUtil.getResult(FINDER_PATH_GET_GROUPS,
1407                finderArgs, this);
1408
1409        if (list == null) {
1410            Session session = null;
1411
1412            try {
1413                session = openSession();
1414
1415                String sql = null;
1416
1417                if (orderByComparator != null) {
1418                    sql = _SQL_GETGROUPS.concat(ORDER_BY_CLAUSE)
1419                                        .concat(orderByComparator.getOrderBy());
1420                }
1421
1422                else {
1423                    sql = _SQL_GETGROUPS.concat(com.liferay.portal.model.impl.GroupModelImpl.ORDER_BY_SQL);
1424                }
1425
1426                SQLQuery q = session.createSQLQuery(sql);
1427
1428                q.addEntity("Group_",
1429                    com.liferay.portal.model.impl.GroupImpl.class);
1430
1431                QueryPos qPos = QueryPos.getInstance(q);
1432
1433                qPos.add(pk);
1434
1435                list = (List<com.liferay.portal.model.Group>)QueryUtil.list(q,
1436                        getDialect(), start, end);
1437            }
1438            catch (Exception e) {
1439                throw processException(e);
1440            }
1441            finally {
1442                if (list == null) {
1443                    list = new ArrayList<com.liferay.portal.model.Group>();
1444                }
1445
1446                groupPersistence.cacheResult(list);
1447
1448                FinderCacheUtil.putResult(FINDER_PATH_GET_GROUPS, finderArgs,
1449                    list);
1450
1451                closeSession(session);
1452            }
1453        }
1454
1455        return list;
1456    }
1457
1458    public static final FinderPath FINDER_PATH_GET_GROUPS_SIZE = new FinderPath(com.liferay.portal.model.impl.GroupModelImpl.ENTITY_CACHE_ENABLED,
1459            UserGroupModelImpl.FINDER_CACHE_ENABLED_GROUPS_USERGROUPS,
1460            UserGroupModelImpl.MAPPING_TABLE_GROUPS_USERGROUPS_NAME,
1461            "getGroupsSize", new String[] { Long.class.getName() });
1462
1463    public int getGroupsSize(long pk) throws SystemException {
1464        Object[] finderArgs = new Object[] { new Long(pk) };
1465
1466        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_GET_GROUPS_SIZE,
1467                finderArgs, this);
1468
1469        if (count == null) {
1470            Session session = null;
1471
1472            try {
1473                session = openSession();
1474
1475                SQLQuery q = session.createSQLQuery(_SQL_GETGROUPSSIZE);
1476
1477                q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
1478
1479                QueryPos qPos = QueryPos.getInstance(q);
1480
1481                qPos.add(pk);
1482
1483                count = (Long)q.uniqueResult();
1484            }
1485            catch (Exception e) {
1486                throw processException(e);
1487            }
1488            finally {
1489                if (count == null) {
1490                    count = Long.valueOf(0);
1491                }
1492
1493                FinderCacheUtil.putResult(FINDER_PATH_GET_GROUPS_SIZE,
1494                    finderArgs, count);
1495
1496                closeSession(session);
1497            }
1498        }
1499
1500        return count.intValue();
1501    }
1502
1503    public static final FinderPath FINDER_PATH_CONTAINS_GROUP = new FinderPath(com.liferay.portal.model.impl.GroupModelImpl.ENTITY_CACHE_ENABLED,
1504            UserGroupModelImpl.FINDER_CACHE_ENABLED_GROUPS_USERGROUPS,
1505            UserGroupModelImpl.MAPPING_TABLE_GROUPS_USERGROUPS_NAME,
1506            "containsGroup",
1507            new String[] { Long.class.getName(), Long.class.getName() });
1508
1509    public boolean containsGroup(long pk, long groupPK)
1510        throws SystemException {
1511        Object[] finderArgs = new Object[] { new Long(pk), new Long(groupPK) };
1512
1513        Boolean value = (Boolean)FinderCacheUtil.getResult(FINDER_PATH_CONTAINS_GROUP,
1514                finderArgs, this);
1515
1516        if (value == null) {
1517            try {
1518                value = Boolean.valueOf(containsGroup.contains(pk, groupPK));
1519            }
1520            catch (Exception e) {
1521                throw processException(e);
1522            }
1523            finally {
1524                if (value == null) {
1525                    value = Boolean.FALSE;
1526                }
1527
1528                FinderCacheUtil.putResult(FINDER_PATH_CONTAINS_GROUP,
1529                    finderArgs, value);
1530            }
1531        }
1532
1533        return value.booleanValue();
1534    }
1535
1536    public boolean containsGroups(long pk) throws SystemException {
1537        if (getGroupsSize(pk) > 0) {
1538            return true;
1539        }
1540        else {
1541            return false;
1542        }
1543    }
1544
1545    public void addGroup(long pk, long groupPK) throws SystemException {
1546        try {
1547            addGroup.add(pk, groupPK);
1548        }
1549        catch (Exception e) {
1550            throw processException(e);
1551        }
1552        finally {
1553            FinderCacheUtil.clearCache(UserGroupModelImpl.MAPPING_TABLE_GROUPS_USERGROUPS_NAME);
1554        }
1555    }
1556
1557    public void addGroup(long pk, com.liferay.portal.model.Group group)
1558        throws SystemException {
1559        try {
1560            addGroup.add(pk, group.getPrimaryKey());
1561        }
1562        catch (Exception e) {
1563            throw processException(e);
1564        }
1565        finally {
1566            FinderCacheUtil.clearCache(UserGroupModelImpl.MAPPING_TABLE_GROUPS_USERGROUPS_NAME);
1567        }
1568    }
1569
1570    public void addGroups(long pk, long[] groupPKs) throws SystemException {
1571        try {
1572            for (long groupPK : groupPKs) {
1573                addGroup.add(pk, groupPK);
1574            }
1575        }
1576        catch (Exception e) {
1577            throw processException(e);
1578        }
1579        finally {
1580            FinderCacheUtil.clearCache(UserGroupModelImpl.MAPPING_TABLE_GROUPS_USERGROUPS_NAME);
1581        }
1582    }
1583
1584    public void addGroups(long pk, List<com.liferay.portal.model.Group> groups)
1585        throws SystemException {
1586        try {
1587            for (com.liferay.portal.model.Group group : groups) {
1588                addGroup.add(pk, group.getPrimaryKey());
1589            }
1590        }
1591        catch (Exception e) {
1592            throw processException(e);
1593        }
1594        finally {
1595            FinderCacheUtil.clearCache(UserGroupModelImpl.MAPPING_TABLE_GROUPS_USERGROUPS_NAME);
1596        }
1597    }
1598
1599    public void clearGroups(long pk) throws SystemException {
1600        try {
1601            clearGroups.clear(pk);
1602        }
1603        catch (Exception e) {
1604            throw processException(e);
1605        }
1606        finally {
1607            FinderCacheUtil.clearCache(UserGroupModelImpl.MAPPING_TABLE_GROUPS_USERGROUPS_NAME);
1608        }
1609    }
1610
1611    public void removeGroup(long pk, long groupPK) throws SystemException {
1612        try {
1613            removeGroup.remove(pk, groupPK);
1614        }
1615        catch (Exception e) {
1616            throw processException(e);
1617        }
1618        finally {
1619            FinderCacheUtil.clearCache(UserGroupModelImpl.MAPPING_TABLE_GROUPS_USERGROUPS_NAME);
1620        }
1621    }
1622
1623    public void removeGroup(long pk, com.liferay.portal.model.Group group)
1624        throws SystemException {
1625        try {
1626            removeGroup.remove(pk, group.getPrimaryKey());
1627        }
1628        catch (Exception e) {
1629            throw processException(e);
1630        }
1631        finally {
1632            FinderCacheUtil.clearCache(UserGroupModelImpl.MAPPING_TABLE_GROUPS_USERGROUPS_NAME);
1633        }
1634    }
1635
1636    public void removeGroups(long pk, long[] groupPKs)
1637        throws SystemException {
1638        try {
1639            for (long groupPK : groupPKs) {
1640                removeGroup.remove(pk, groupPK);
1641            }
1642        }
1643        catch (Exception e) {
1644            throw processException(e);
1645        }
1646        finally {
1647            FinderCacheUtil.clearCache(UserGroupModelImpl.MAPPING_TABLE_GROUPS_USERGROUPS_NAME);
1648        }
1649    }
1650
1651    public void removeGroups(long pk,
1652        List<com.liferay.portal.model.Group> groups) throws SystemException {
1653        try {
1654            for (com.liferay.portal.model.Group group : groups) {
1655                removeGroup.remove(pk, group.getPrimaryKey());
1656            }
1657        }
1658        catch (Exception e) {
1659            throw processException(e);
1660        }
1661        finally {
1662            FinderCacheUtil.clearCache(UserGroupModelImpl.MAPPING_TABLE_GROUPS_USERGROUPS_NAME);
1663        }
1664    }
1665
1666    public void setGroups(long pk, long[] groupPKs) throws SystemException {
1667        try {
1668            Set<Long> groupPKSet = SetUtil.fromArray(groupPKs);
1669
1670            List<com.liferay.portal.model.Group> groups = getGroups(pk);
1671
1672            for (com.liferay.portal.model.Group group : groups) {
1673                if (!groupPKSet.contains(group.getPrimaryKey())) {
1674                    removeGroup.remove(pk, group.getPrimaryKey());
1675                }
1676                else {
1677                    groupPKSet.remove(group.getPrimaryKey());
1678                }
1679            }
1680
1681            for (Long groupPK : groupPKSet) {
1682                addGroup.add(pk, groupPK);
1683            }
1684        }
1685        catch (Exception e) {
1686            throw processException(e);
1687        }
1688        finally {
1689            FinderCacheUtil.clearCache(UserGroupModelImpl.MAPPING_TABLE_GROUPS_USERGROUPS_NAME);
1690        }
1691    }
1692
1693    public void setGroups(long pk, List<com.liferay.portal.model.Group> groups)
1694        throws SystemException {
1695        try {
1696            long[] groupPKs = new long[groups.size()];
1697
1698            for (int i = 0; i < groups.size(); i++) {
1699                com.liferay.portal.model.Group group = groups.get(i);
1700
1701                groupPKs[i] = group.getPrimaryKey();
1702            }
1703
1704            setGroups(pk, groupPKs);
1705        }
1706        catch (Exception e) {
1707            throw processException(e);
1708        }
1709        finally {
1710            FinderCacheUtil.clearCache(UserGroupModelImpl.MAPPING_TABLE_GROUPS_USERGROUPS_NAME);
1711        }
1712    }
1713
1714    public List<com.liferay.portal.model.User> getUsers(long pk)
1715        throws SystemException {
1716        return getUsers(pk, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
1717    }
1718
1719    public List<com.liferay.portal.model.User> getUsers(long pk, int start,
1720        int end) throws SystemException {
1721        return getUsers(pk, start, end, null);
1722    }
1723
1724    public static final FinderPath FINDER_PATH_GET_USERS = new FinderPath(com.liferay.portal.model.impl.UserModelImpl.ENTITY_CACHE_ENABLED,
1725            UserGroupModelImpl.FINDER_CACHE_ENABLED_USERS_USERGROUPS,
1726            UserGroupModelImpl.MAPPING_TABLE_USERS_USERGROUPS_NAME, "getUsers",
1727            new String[] {
1728                Long.class.getName(), "java.lang.Integer", "java.lang.Integer",
1729                "com.liferay.portal.kernel.util.OrderByComparator"
1730            });
1731
1732    public List<com.liferay.portal.model.User> getUsers(long pk, int start,
1733        int end, OrderByComparator orderByComparator) throws SystemException {
1734        Object[] finderArgs = new Object[] {
1735                new Long(pk), String.valueOf(start), String.valueOf(end),
1736                String.valueOf(orderByComparator)
1737            };
1738
1739        List<com.liferay.portal.model.User> list = (List<com.liferay.portal.model.User>)FinderCacheUtil.getResult(FINDER_PATH_GET_USERS,
1740                finderArgs, this);
1741
1742        if (list == null) {
1743            Session session = null;
1744
1745            try {
1746                session = openSession();
1747
1748                String sql = null;
1749
1750                if (orderByComparator != null) {
1751                    sql = _SQL_GETUSERS.concat(ORDER_BY_CLAUSE)
1752                                       .concat(orderByComparator.getOrderBy());
1753                }
1754
1755                sql = _SQL_GETUSERS;
1756
1757                SQLQuery q = session.createSQLQuery(sql);
1758
1759                q.addEntity("User_",
1760                    com.liferay.portal.model.impl.UserImpl.class);
1761
1762                QueryPos qPos = QueryPos.getInstance(q);
1763
1764                qPos.add(pk);
1765
1766                list = (List<com.liferay.portal.model.User>)QueryUtil.list(q,
1767                        getDialect(), start, end);
1768            }
1769            catch (Exception e) {
1770                throw processException(e);
1771            }
1772            finally {
1773                if (list == null) {
1774                    list = new ArrayList<com.liferay.portal.model.User>();
1775                }
1776
1777                userPersistence.cacheResult(list);
1778
1779                FinderCacheUtil.putResult(FINDER_PATH_GET_USERS, finderArgs,
1780                    list);
1781
1782                closeSession(session);
1783            }
1784        }
1785
1786        return list;
1787    }
1788
1789    public static final FinderPath FINDER_PATH_GET_USERS_SIZE = new FinderPath(com.liferay.portal.model.impl.UserModelImpl.ENTITY_CACHE_ENABLED,
1790            UserGroupModelImpl.FINDER_CACHE_ENABLED_USERS_USERGROUPS,
1791            UserGroupModelImpl.MAPPING_TABLE_USERS_USERGROUPS_NAME,
1792            "getUsersSize", new String[] { Long.class.getName() });
1793
1794    public int getUsersSize(long pk) throws SystemException {
1795        Object[] finderArgs = new Object[] { new Long(pk) };
1796
1797        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_GET_USERS_SIZE,
1798                finderArgs, this);
1799
1800        if (count == null) {
1801            Session session = null;
1802
1803            try {
1804                session = openSession();
1805
1806                SQLQuery q = session.createSQLQuery(_SQL_GETUSERSSIZE);
1807
1808                q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
1809
1810                QueryPos qPos = QueryPos.getInstance(q);
1811
1812                qPos.add(pk);
1813
1814                count = (Long)q.uniqueResult();
1815            }
1816            catch (Exception e) {
1817                throw processException(e);
1818            }
1819            finally {
1820                if (count == null) {
1821                    count = Long.valueOf(0);
1822                }
1823
1824                FinderCacheUtil.putResult(FINDER_PATH_GET_USERS_SIZE,
1825                    finderArgs, count);
1826
1827                closeSession(session);
1828            }
1829        }
1830
1831        return count.intValue();
1832    }
1833
1834    public static final FinderPath FINDER_PATH_CONTAINS_USER = new FinderPath(com.liferay.portal.model.impl.UserModelImpl.ENTITY_CACHE_ENABLED,
1835            UserGroupModelImpl.FINDER_CACHE_ENABLED_USERS_USERGROUPS,
1836            UserGroupModelImpl.MAPPING_TABLE_USERS_USERGROUPS_NAME,
1837            "containsUser",
1838            new String[] { Long.class.getName(), Long.class.getName() });
1839
1840    public boolean containsUser(long pk, long userPK) throws SystemException {
1841        Object[] finderArgs = new Object[] { new Long(pk), new Long(userPK) };
1842
1843        Boolean value = (Boolean)FinderCacheUtil.getResult(FINDER_PATH_CONTAINS_USER,
1844                finderArgs, this);
1845
1846        if (value == null) {
1847            try {
1848                value = Boolean.valueOf(containsUser.contains(pk, userPK));
1849            }
1850            catch (Exception e) {
1851                throw processException(e);
1852            }
1853            finally {
1854                if (value == null) {
1855                    value = Boolean.FALSE;
1856                }
1857
1858                FinderCacheUtil.putResult(FINDER_PATH_CONTAINS_USER,
1859                    finderArgs, value);
1860            }
1861        }
1862
1863        return value.booleanValue();
1864    }
1865
1866    public boolean containsUsers(long pk) throws SystemException {
1867        if (getUsersSize(pk) > 0) {
1868            return true;
1869        }
1870        else {
1871            return false;
1872        }
1873    }
1874
1875    public void addUser(long pk, long userPK) throws SystemException {
1876        try {
1877            addUser.add(pk, userPK);
1878        }
1879        catch (Exception e) {
1880            throw processException(e);
1881        }
1882        finally {
1883            FinderCacheUtil.clearCache(UserGroupModelImpl.MAPPING_TABLE_USERS_USERGROUPS_NAME);
1884        }
1885    }
1886
1887    public void addUser(long pk, com.liferay.portal.model.User user)
1888        throws SystemException {
1889        try {
1890            addUser.add(pk, user.getPrimaryKey());
1891        }
1892        catch (Exception e) {
1893            throw processException(e);
1894        }
1895        finally {
1896            FinderCacheUtil.clearCache(UserGroupModelImpl.MAPPING_TABLE_USERS_USERGROUPS_NAME);
1897        }
1898    }
1899
1900    public void addUsers(long pk, long[] userPKs) throws SystemException {
1901        try {
1902            for (long userPK : userPKs) {
1903                addUser.add(pk, userPK);
1904            }
1905        }
1906        catch (Exception e) {
1907            throw processException(e);
1908        }
1909        finally {
1910            FinderCacheUtil.clearCache(UserGroupModelImpl.MAPPING_TABLE_USERS_USERGROUPS_NAME);
1911        }
1912    }
1913
1914    public void addUsers(long pk, List<com.liferay.portal.model.User> users)
1915        throws SystemException {
1916        try {
1917            for (com.liferay.portal.model.User user : users) {
1918                addUser.add(pk, user.getPrimaryKey());
1919            }
1920        }
1921        catch (Exception e) {
1922            throw processException(e);
1923        }
1924        finally {
1925            FinderCacheUtil.clearCache(UserGroupModelImpl.MAPPING_TABLE_USERS_USERGROUPS_NAME);
1926        }
1927    }
1928
1929    public void clearUsers(long pk) throws SystemException {
1930        try {
1931            clearUsers.clear(pk);
1932        }
1933        catch (Exception e) {
1934            throw processException(e);
1935        }
1936        finally {
1937            FinderCacheUtil.clearCache(UserGroupModelImpl.MAPPING_TABLE_USERS_USERGROUPS_NAME);
1938        }
1939    }
1940
1941    public void removeUser(long pk, long userPK) throws SystemException {
1942        try {
1943            removeUser.remove(pk, userPK);
1944        }
1945        catch (Exception e) {
1946            throw processException(e);
1947        }
1948        finally {
1949            FinderCacheUtil.clearCache(UserGroupModelImpl.MAPPING_TABLE_USERS_USERGROUPS_NAME);
1950        }
1951    }
1952
1953    public void removeUser(long pk, com.liferay.portal.model.User user)
1954        throws SystemException {
1955        try {
1956            removeUser.remove(pk, user.getPrimaryKey());
1957        }
1958        catch (Exception e) {
1959            throw processException(e);
1960        }
1961        finally {
1962            FinderCacheUtil.clearCache(UserGroupModelImpl.MAPPING_TABLE_USERS_USERGROUPS_NAME);
1963        }
1964    }
1965
1966    public void removeUsers(long pk, long[] userPKs) throws SystemException {
1967        try {
1968            for (long userPK : userPKs) {
1969                removeUser.remove(pk, userPK);
1970            }
1971        }
1972        catch (Exception e) {
1973            throw processException(e);
1974        }
1975        finally {
1976            FinderCacheUtil.clearCache(UserGroupModelImpl.MAPPING_TABLE_USERS_USERGROUPS_NAME);
1977        }
1978    }
1979
1980    public void removeUsers(long pk, List<com.liferay.portal.model.User> users)
1981        throws SystemException {
1982        try {
1983            for (com.liferay.portal.model.User user : users) {
1984                removeUser.remove(pk, user.getPrimaryKey());
1985            }
1986        }
1987        catch (Exception e) {
1988            throw processException(e);
1989        }
1990        finally {
1991            FinderCacheUtil.clearCache(UserGroupModelImpl.MAPPING_TABLE_USERS_USERGROUPS_NAME);
1992        }
1993    }
1994
1995    public void setUsers(long pk, long[] userPKs) throws SystemException {
1996        try {
1997            Set<Long> userPKSet = SetUtil.fromArray(userPKs);
1998
1999            List<com.liferay.portal.model.User> users = getUsers(pk);
2000
2001            for (com.liferay.portal.model.User user : users) {
2002                if (!userPKSet.contains(user.getPrimaryKey())) {
2003                    removeUser.remove(pk, user.getPrimaryKey());
2004                }
2005                else {
2006                    userPKSet.remove(user.getPrimaryKey());
2007                }
2008            }
2009
2010            for (Long userPK : userPKSet) {
2011                addUser.add(pk, userPK);
2012            }
2013        }
2014        catch (Exception e) {
2015            throw processException(e);
2016        }
2017        finally {
2018            FinderCacheUtil.clearCache(UserGroupModelImpl.MAPPING_TABLE_USERS_USERGROUPS_NAME);
2019        }
2020    }
2021
2022    public void setUsers(long pk, List<com.liferay.portal.model.User> users)
2023        throws SystemException {
2024        try {
2025            long[] userPKs = new long[users.size()];
2026
2027            for (int i = 0; i < users.size(); i++) {
2028                com.liferay.portal.model.User user = users.get(i);
2029
2030                userPKs[i] = user.getPrimaryKey();
2031            }
2032
2033            setUsers(pk, userPKs);
2034        }
2035        catch (Exception e) {
2036            throw processException(e);
2037        }
2038        finally {
2039            FinderCacheUtil.clearCache(UserGroupModelImpl.MAPPING_TABLE_USERS_USERGROUPS_NAME);
2040        }
2041    }
2042
2043    public void afterPropertiesSet() {
2044        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
2045                    com.liferay.portal.util.PropsUtil.get(
2046                        "value.object.listener.com.liferay.portal.model.UserGroup")));
2047
2048        if (listenerClassNames.length > 0) {
2049            try {
2050                List<ModelListener<UserGroup>> listenersList = new ArrayList<ModelListener<UserGroup>>();
2051
2052                for (String listenerClassName : listenerClassNames) {
2053                    listenersList.add((ModelListener<UserGroup>)InstanceFactory.newInstance(
2054                            listenerClassName));
2055                }
2056
2057                listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
2058            }
2059            catch (Exception e) {
2060                _log.error(e);
2061            }
2062        }
2063
2064        containsGroup = new ContainsGroup(this);
2065
2066        addGroup = new AddGroup(this);
2067        clearGroups = new ClearGroups(this);
2068        removeGroup = new RemoveGroup(this);
2069
2070        containsUser = new ContainsUser(this);
2071
2072        addUser = new AddUser(this);
2073        clearUsers = new ClearUsers(this);
2074        removeUser = new RemoveUser(this);
2075    }
2076
2077    @BeanReference(type = AccountPersistence.class)
2078    protected AccountPersistence accountPersistence;
2079    @BeanReference(type = AddressPersistence.class)
2080    protected AddressPersistence addressPersistence;
2081    @BeanReference(type = BrowserTrackerPersistence.class)
2082    protected BrowserTrackerPersistence browserTrackerPersistence;
2083    @BeanReference(type = ClassNamePersistence.class)
2084    protected ClassNamePersistence classNamePersistence;
2085    @BeanReference(type = CompanyPersistence.class)
2086    protected CompanyPersistence companyPersistence;
2087    @BeanReference(type = ContactPersistence.class)
2088    protected ContactPersistence contactPersistence;
2089    @BeanReference(type = CountryPersistence.class)
2090    protected CountryPersistence countryPersistence;
2091    @BeanReference(type = EmailAddressPersistence.class)
2092    protected EmailAddressPersistence emailAddressPersistence;
2093    @BeanReference(type = GroupPersistence.class)
2094    protected GroupPersistence groupPersistence;
2095    @BeanReference(type = ImagePersistence.class)
2096    protected ImagePersistence imagePersistence;
2097    @BeanReference(type = LayoutPersistence.class)
2098    protected LayoutPersistence layoutPersistence;
2099    @BeanReference(type = LayoutPrototypePersistence.class)
2100    protected LayoutPrototypePersistence layoutPrototypePersistence;
2101    @BeanReference(type = LayoutSetPersistence.class)
2102    protected LayoutSetPersistence layoutSetPersistence;
2103    @BeanReference(type = LayoutSetPrototypePersistence.class)
2104    protected LayoutSetPrototypePersistence layoutSetPrototypePersistence;
2105    @BeanReference(type = ListTypePersistence.class)
2106    protected ListTypePersistence listTypePersistence;
2107    @BeanReference(type = LockPersistence.class)
2108    protected LockPersistence lockPersistence;
2109    @BeanReference(type = MembershipRequestPersistence.class)
2110    protected MembershipRequestPersistence membershipRequestPersistence;
2111    @BeanReference(type = OrganizationPersistence.class)
2112    protected OrganizationPersistence organizationPersistence;
2113    @BeanReference(type = OrgGroupPermissionPersistence.class)
2114    protected OrgGroupPermissionPersistence orgGroupPermissionPersistence;
2115    @BeanReference(type = OrgGroupRolePersistence.class)
2116    protected OrgGroupRolePersistence orgGroupRolePersistence;
2117    @BeanReference(type = OrgLaborPersistence.class)
2118    protected OrgLaborPersistence orgLaborPersistence;
2119    @BeanReference(type = PasswordPolicyPersistence.class)
2120    protected PasswordPolicyPersistence passwordPolicyPersistence;
2121    @BeanReference(type = PasswordPolicyRelPersistence.class)
2122    protected PasswordPolicyRelPersistence passwordPolicyRelPersistence;
2123    @BeanReference(type = PasswordTrackerPersistence.class)
2124    protected PasswordTrackerPersistence passwordTrackerPersistence;
2125    @BeanReference(type = PermissionPersistence.class)
2126    protected PermissionPersistence permissionPersistence;
2127    @BeanReference(type = PhonePersistence.class)
2128    protected PhonePersistence phonePersistence;
2129    @BeanReference(type = PluginSettingPersistence.class)
2130    protected PluginSettingPersistence pluginSettingPersistence;
2131    @BeanReference(type = PortletPersistence.class)
2132    protected PortletPersistence portletPersistence;
2133    @BeanReference(type = PortletItemPersistence.class)
2134    protected PortletItemPersistence portletItemPersistence;
2135    @BeanReference(type = PortletPreferencesPersistence.class)
2136    protected PortletPreferencesPersistence portletPreferencesPersistence;
2137    @BeanReference(type = RegionPersistence.class)
2138    protected RegionPersistence regionPersistence;
2139    @BeanReference(type = ReleasePersistence.class)
2140    protected ReleasePersistence releasePersistence;
2141    @BeanReference(type = ResourcePersistence.class)
2142    protected ResourcePersistence resourcePersistence;
2143    @BeanReference(type = ResourceActionPersistence.class)
2144    protected ResourceActionPersistence resourceActionPersistence;
2145    @BeanReference(type = ResourceCodePersistence.class)
2146    protected ResourceCodePersistence resourceCodePersistence;
2147    @BeanReference(type = ResourcePermissionPersistence.class)
2148    protected ResourcePermissionPersistence resourcePermissionPersistence;
2149    @BeanReference(type = RolePersistence.class)
2150    protected RolePersistence rolePersistence;
2151    @BeanReference(type = ServiceComponentPersistence.class)
2152    protected ServiceComponentPersistence serviceComponentPersistence;
2153    @BeanReference(type = ShardPersistence.class)
2154    protected ShardPersistence shardPersistence;
2155    @BeanReference(type = SubscriptionPersistence.class)
2156    protected SubscriptionPersistence subscriptionPersistence;
2157    @BeanReference(type = TicketPersistence.class)
2158    protected TicketPersistence ticketPersistence;
2159    @BeanReference(type = TeamPersistence.class)
2160    protected TeamPersistence teamPersistence;
2161    @BeanReference(type = UserPersistence.class)
2162    protected UserPersistence userPersistence;
2163    @BeanReference(type = UserGroupPersistence.class)
2164    protected UserGroupPersistence userGroupPersistence;
2165    @BeanReference(type = UserGroupGroupRolePersistence.class)
2166    protected UserGroupGroupRolePersistence userGroupGroupRolePersistence;
2167    @BeanReference(type = UserGroupRolePersistence.class)
2168    protected UserGroupRolePersistence userGroupRolePersistence;
2169    @BeanReference(type = UserIdMapperPersistence.class)
2170    protected UserIdMapperPersistence userIdMapperPersistence;
2171    @BeanReference(type = UserTrackerPersistence.class)
2172    protected UserTrackerPersistence userTrackerPersistence;
2173    @BeanReference(type = UserTrackerPathPersistence.class)
2174    protected UserTrackerPathPersistence userTrackerPathPersistence;
2175    @BeanReference(type = WebDAVPropsPersistence.class)
2176    protected WebDAVPropsPersistence webDAVPropsPersistence;
2177    @BeanReference(type = WebsitePersistence.class)
2178    protected WebsitePersistence websitePersistence;
2179    @BeanReference(type = WorkflowDefinitionLinkPersistence.class)
2180    protected WorkflowDefinitionLinkPersistence workflowDefinitionLinkPersistence;
2181    @BeanReference(type = WorkflowInstanceLinkPersistence.class)
2182    protected WorkflowInstanceLinkPersistence workflowInstanceLinkPersistence;
2183    protected ContainsGroup containsGroup;
2184    protected AddGroup addGroup;
2185    protected ClearGroups clearGroups;
2186    protected RemoveGroup removeGroup;
2187    protected ContainsUser containsUser;
2188    protected AddUser addUser;
2189    protected ClearUsers clearUsers;
2190    protected RemoveUser removeUser;
2191
2192    protected class ContainsGroup {
2193        protected ContainsGroup(UserGroupPersistenceImpl persistenceImpl) {
2194            super();
2195
2196            _mappingSqlQuery = MappingSqlQueryFactoryUtil.getMappingSqlQuery(getDataSource(),
2197                    _SQL_CONTAINSGROUP,
2198                    new int[] { Types.BIGINT, Types.BIGINT }, RowMapper.COUNT);
2199        }
2200
2201        protected boolean contains(long userGroupId, long groupId) {
2202            List<Integer> results = _mappingSqlQuery.execute(new Object[] {
2203                        new Long(userGroupId), new Long(groupId)
2204                    });
2205
2206            if (results.size() > 0) {
2207                Integer count = results.get(0);
2208
2209                if (count.intValue() > 0) {
2210                    return true;
2211                }
2212            }
2213
2214            return false;
2215        }
2216
2217        private MappingSqlQuery<Integer> _mappingSqlQuery;
2218    }
2219
2220    protected class AddGroup {
2221        protected AddGroup(UserGroupPersistenceImpl persistenceImpl) {
2222            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
2223                    "INSERT INTO Groups_UserGroups (userGroupId, groupId) VALUES (?, ?)",
2224                    new int[] { Types.BIGINT, Types.BIGINT });
2225            _persistenceImpl = persistenceImpl;
2226        }
2227
2228        protected void add(long userGroupId, long groupId)
2229            throws SystemException {
2230            if (!_persistenceImpl.containsGroup.contains(userGroupId, groupId)) {
2231                ModelListener<com.liferay.portal.model.Group>[] groupListeners = groupPersistence.getListeners();
2232
2233                for (ModelListener<UserGroup> listener : listeners) {
2234                    listener.onBeforeAddAssociation(userGroupId,
2235                        com.liferay.portal.model.Group.class.getName(), groupId);
2236                }
2237
2238                for (ModelListener<com.liferay.portal.model.Group> listener : groupListeners) {
2239                    listener.onBeforeAddAssociation(groupId,
2240                        UserGroup.class.getName(), userGroupId);
2241                }
2242
2243                _sqlUpdate.update(new Object[] {
2244                        new Long(userGroupId), new Long(groupId)
2245                    });
2246
2247                for (ModelListener<UserGroup> listener : listeners) {
2248                    listener.onAfterAddAssociation(userGroupId,
2249                        com.liferay.portal.model.Group.class.getName(), groupId);
2250                }
2251
2252                for (ModelListener<com.liferay.portal.model.Group> listener : groupListeners) {
2253                    listener.onAfterAddAssociation(groupId,
2254                        UserGroup.class.getName(), userGroupId);
2255                }
2256            }
2257        }
2258
2259        private SqlUpdate _sqlUpdate;
2260        private UserGroupPersistenceImpl _persistenceImpl;
2261    }
2262
2263    protected class ClearGroups {
2264        protected ClearGroups(UserGroupPersistenceImpl persistenceImpl) {
2265            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
2266                    "DELETE FROM Groups_UserGroups WHERE userGroupId = ?",
2267                    new int[] { Types.BIGINT });
2268        }
2269
2270        protected void clear(long userGroupId) throws SystemException {
2271            ModelListener<com.liferay.portal.model.Group>[] groupListeners = groupPersistence.getListeners();
2272
2273            List<com.liferay.portal.model.Group> groups = null;
2274
2275            if ((listeners.length > 0) || (groupListeners.length > 0)) {
2276                groups = getGroups(userGroupId);
2277
2278                for (com.liferay.portal.model.Group group : groups) {
2279                    for (ModelListener<UserGroup> listener : listeners) {
2280                        listener.onBeforeRemoveAssociation(userGroupId,
2281                            com.liferay.portal.model.Group.class.getName(),
2282                            group.getPrimaryKey());
2283                    }
2284
2285                    for (ModelListener<com.liferay.portal.model.Group> listener : groupListeners) {
2286                        listener.onBeforeRemoveAssociation(group.getPrimaryKey(),
2287                            UserGroup.class.getName(), userGroupId);
2288                    }
2289                }
2290            }
2291
2292            _sqlUpdate.update(new Object[] { new Long(userGroupId) });
2293
2294            if ((listeners.length > 0) || (groupListeners.length > 0)) {
2295                for (com.liferay.portal.model.Group group : groups) {
2296                    for (ModelListener<UserGroup> listener : listeners) {
2297                        listener.onAfterRemoveAssociation(userGroupId,
2298                            com.liferay.portal.model.Group.class.getName(),
2299                            group.getPrimaryKey());
2300                    }
2301
2302                    for (ModelListener<com.liferay.portal.model.Group> listener : groupListeners) {
2303                        listener.onAfterRemoveAssociation(group.getPrimaryKey(),
2304                            UserGroup.class.getName(), userGroupId);
2305                    }
2306                }
2307            }
2308        }
2309
2310        private SqlUpdate _sqlUpdate;
2311    }
2312
2313    protected class RemoveGroup {
2314        protected RemoveGroup(UserGroupPersistenceImpl persistenceImpl) {
2315            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
2316                    "DELETE FROM Groups_UserGroups WHERE userGroupId = ? AND groupId = ?",
2317                    new int[] { Types.BIGINT, Types.BIGINT });
2318            _persistenceImpl = persistenceImpl;
2319        }
2320
2321        protected void remove(long userGroupId, long groupId)
2322            throws SystemException {
2323            if (_persistenceImpl.containsGroup.contains(userGroupId, groupId)) {
2324                ModelListener<com.liferay.portal.model.Group>[] groupListeners = groupPersistence.getListeners();
2325
2326                for (ModelListener<UserGroup> listener : listeners) {
2327                    listener.onBeforeRemoveAssociation(userGroupId,
2328                        com.liferay.portal.model.Group.class.getName(), groupId);
2329                }
2330
2331                for (ModelListener<com.liferay.portal.model.Group> listener : groupListeners) {
2332                    listener.onBeforeRemoveAssociation(groupId,
2333                        UserGroup.class.getName(), userGroupId);
2334                }
2335
2336                _sqlUpdate.update(new Object[] {
2337                        new Long(userGroupId), new Long(groupId)
2338                    });
2339
2340                for (ModelListener<UserGroup> listener : listeners) {
2341                    listener.onAfterRemoveAssociation(userGroupId,
2342                        com.liferay.portal.model.Group.class.getName(), groupId);
2343                }
2344
2345                for (ModelListener<com.liferay.portal.model.Group> listener : groupListeners) {
2346                    listener.onAfterRemoveAssociation(groupId,
2347                        UserGroup.class.getName(), userGroupId);
2348                }
2349            }
2350        }
2351
2352        private SqlUpdate _sqlUpdate;
2353        private UserGroupPersistenceImpl _persistenceImpl;
2354    }
2355
2356    protected class ContainsUser {
2357        protected ContainsUser(UserGroupPersistenceImpl persistenceImpl) {
2358            super();
2359
2360            _mappingSqlQuery = MappingSqlQueryFactoryUtil.getMappingSqlQuery(getDataSource(),
2361                    _SQL_CONTAINSUSER,
2362                    new int[] { Types.BIGINT, Types.BIGINT }, RowMapper.COUNT);
2363        }
2364
2365        protected boolean contains(long userGroupId, long userId) {
2366            List<Integer> results = _mappingSqlQuery.execute(new Object[] {
2367                        new Long(userGroupId), new Long(userId)
2368                    });
2369
2370            if (results.size() > 0) {
2371                Integer count = results.get(0);
2372
2373                if (count.intValue() > 0) {
2374                    return true;
2375                }
2376            }
2377
2378            return false;
2379        }
2380
2381        private MappingSqlQuery<Integer> _mappingSqlQuery;
2382    }
2383
2384    protected class AddUser {
2385        protected AddUser(UserGroupPersistenceImpl persistenceImpl) {
2386            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
2387                    "INSERT INTO Users_UserGroups (userGroupId, userId) VALUES (?, ?)",
2388                    new int[] { Types.BIGINT, Types.BIGINT });
2389            _persistenceImpl = persistenceImpl;
2390        }
2391
2392        protected void add(long userGroupId, long userId)
2393            throws SystemException {
2394            if (!_persistenceImpl.containsUser.contains(userGroupId, userId)) {
2395                ModelListener<com.liferay.portal.model.User>[] userListeners = userPersistence.getListeners();
2396
2397                for (ModelListener<UserGroup> listener : listeners) {
2398                    listener.onBeforeAddAssociation(userGroupId,
2399                        com.liferay.portal.model.User.class.getName(), userId);
2400                }
2401
2402                for (ModelListener<com.liferay.portal.model.User> listener : userListeners) {
2403                    listener.onBeforeAddAssociation(userId,
2404                        UserGroup.class.getName(), userGroupId);
2405                }
2406
2407                _sqlUpdate.update(new Object[] {
2408                        new Long(userGroupId), new Long(userId)
2409                    });
2410
2411                for (ModelListener<UserGroup> listener : listeners) {
2412                    listener.onAfterAddAssociation(userGroupId,
2413                        com.liferay.portal.model.User.class.getName(), userId);
2414                }
2415
2416                for (ModelListener<com.liferay.portal.model.User> listener : userListeners) {
2417                    listener.onAfterAddAssociation(userId,
2418                        UserGroup.class.getName(), userGroupId);
2419                }
2420            }
2421        }
2422
2423        private SqlUpdate _sqlUpdate;
2424        private UserGroupPersistenceImpl _persistenceImpl;
2425    }
2426
2427    protected class ClearUsers {
2428        protected ClearUsers(UserGroupPersistenceImpl persistenceImpl) {
2429            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
2430                    "DELETE FROM Users_UserGroups WHERE userGroupId = ?",
2431                    new int[] { Types.BIGINT });
2432        }
2433
2434        protected void clear(long userGroupId) throws SystemException {
2435            ModelListener<com.liferay.portal.model.User>[] userListeners = userPersistence.getListeners();
2436
2437            List<com.liferay.portal.model.User> users = null;
2438
2439            if ((listeners.length > 0) || (userListeners.length > 0)) {
2440                users = getUsers(userGroupId);
2441
2442                for (com.liferay.portal.model.User user : users) {
2443                    for (ModelListener<UserGroup> listener : listeners) {
2444                        listener.onBeforeRemoveAssociation(userGroupId,
2445                            com.liferay.portal.model.User.class.getName(),
2446                            user.getPrimaryKey());
2447                    }
2448
2449                    for (ModelListener<com.liferay.portal.model.User> listener : userListeners) {
2450                        listener.onBeforeRemoveAssociation(user.getPrimaryKey(),
2451                            UserGroup.class.getName(), userGroupId);
2452                    }
2453                }
2454            }
2455
2456            _sqlUpdate.update(new Object[] { new Long(userGroupId) });
2457
2458            if ((listeners.length > 0) || (userListeners.length > 0)) {
2459                for (com.liferay.portal.model.User user : users) {
2460                    for (ModelListener<UserGroup> listener : listeners) {
2461                        listener.onAfterRemoveAssociation(userGroupId,
2462                            com.liferay.portal.model.User.class.getName(),
2463                            user.getPrimaryKey());
2464                    }
2465
2466                    for (ModelListener<com.liferay.portal.model.User> listener : userListeners) {
2467                        listener.onAfterRemoveAssociation(user.getPrimaryKey(),
2468                            UserGroup.class.getName(), userGroupId);
2469                    }
2470                }
2471            }
2472        }
2473
2474        private SqlUpdate _sqlUpdate;
2475    }
2476
2477    protected class RemoveUser {
2478        protected RemoveUser(UserGroupPersistenceImpl persistenceImpl) {
2479            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
2480                    "DELETE FROM Users_UserGroups WHERE userGroupId = ? AND userId = ?",
2481                    new int[] { Types.BIGINT, Types.BIGINT });
2482            _persistenceImpl = persistenceImpl;
2483        }
2484
2485        protected void remove(long userGroupId, long userId)
2486            throws SystemException {
2487            if (_persistenceImpl.containsUser.contains(userGroupId, userId)) {
2488                ModelListener<com.liferay.portal.model.User>[] userListeners = userPersistence.getListeners();
2489
2490                for (ModelListener<UserGroup> listener : listeners) {
2491                    listener.onBeforeRemoveAssociation(userGroupId,
2492                        com.liferay.portal.model.User.class.getName(), userId);
2493                }
2494
2495                for (ModelListener<com.liferay.portal.model.User> listener : userListeners) {
2496                    listener.onBeforeRemoveAssociation(userId,
2497                        UserGroup.class.getName(), userGroupId);
2498                }
2499
2500                _sqlUpdate.update(new Object[] {
2501                        new Long(userGroupId), new Long(userId)
2502                    });
2503
2504                for (ModelListener<UserGroup> listener : listeners) {
2505                    listener.onAfterRemoveAssociation(userGroupId,
2506                        com.liferay.portal.model.User.class.getName(), userId);
2507                }
2508
2509                for (ModelListener<com.liferay.portal.model.User> listener : userListeners) {
2510                    listener.onAfterRemoveAssociation(userId,
2511                        UserGroup.class.getName(), userGroupId);
2512                }
2513            }
2514        }
2515
2516        private SqlUpdate _sqlUpdate;
2517        private UserGroupPersistenceImpl _persistenceImpl;
2518    }
2519
2520    private static final String _SQL_SELECT_USERGROUP = "SELECT userGroup FROM UserGroup userGroup";
2521    private static final String _SQL_SELECT_USERGROUP_WHERE = "SELECT userGroup FROM UserGroup userGroup WHERE ";
2522    private static final String _SQL_COUNT_USERGROUP = "SELECT COUNT(userGroup) FROM UserGroup userGroup";
2523    private static final String _SQL_COUNT_USERGROUP_WHERE = "SELECT COUNT(userGroup) FROM UserGroup userGroup WHERE ";
2524    private static final String _SQL_GETGROUPS = "SELECT {Group_.*} FROM Group_ INNER JOIN Groups_UserGroups ON (Groups_UserGroups.groupId = Group_.groupId) WHERE (Groups_UserGroups.userGroupId = ?)";
2525    private static final String _SQL_GETGROUPSSIZE = "SELECT COUNT(*) AS COUNT_VALUE FROM Groups_UserGroups WHERE userGroupId = ?";
2526    private static final String _SQL_CONTAINSGROUP = "SELECT COUNT(*) AS COUNT_VALUE FROM Groups_UserGroups WHERE userGroupId = ? AND groupId = ?";
2527    private static final String _SQL_GETUSERS = "SELECT {User_.*} FROM User_ INNER JOIN Users_UserGroups ON (Users_UserGroups.userId = User_.userId) WHERE (Users_UserGroups.userGroupId = ?)";
2528    private static final String _SQL_GETUSERSSIZE = "SELECT COUNT(*) AS COUNT_VALUE FROM Users_UserGroups WHERE userGroupId = ?";
2529    private static final String _SQL_CONTAINSUSER = "SELECT COUNT(*) AS COUNT_VALUE FROM Users_UserGroups WHERE userGroupId = ? AND userId = ?";
2530    private static final String _FINDER_COLUMN_COMPANYID_COMPANYID_2 = "userGroup.companyId = ?";
2531    private static final String _FINDER_COLUMN_C_P_COMPANYID_2 = "userGroup.companyId = ? AND ";
2532    private static final String _FINDER_COLUMN_C_P_PARENTUSERGROUPID_2 = "userGroup.parentUserGroupId = ?";
2533    private static final String _FINDER_COLUMN_C_N_COMPANYID_2 = "userGroup.companyId = ? AND ";
2534    private static final String _FINDER_COLUMN_C_N_NAME_1 = "userGroup.name IS NULL";
2535    private static final String _FINDER_COLUMN_C_N_NAME_2 = "userGroup.name = ?";
2536    private static final String _FINDER_COLUMN_C_N_NAME_3 = "(userGroup.name IS NULL OR userGroup.name = ?)";
2537    private static final String _ORDER_BY_ENTITY_ALIAS = "userGroup.";
2538    private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No UserGroup exists with the primary key ";
2539    private static final String _NO_SUCH_ENTITY_WITH_KEY = "No UserGroup exists with the key {";
2540    private static Log _log = LogFactoryUtil.getLog(UserGroupPersistenceImpl.class);
2541}