1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   * 
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.SystemException;
20  import com.liferay.portal.kernel.annotation.BeanReference;
21  import com.liferay.portal.kernel.cache.CacheRegistry;
22  import com.liferay.portal.kernel.dao.jdbc.MappingSqlQuery;
23  import com.liferay.portal.kernel.dao.jdbc.MappingSqlQueryFactoryUtil;
24  import com.liferay.portal.kernel.dao.jdbc.RowMapper;
25  import com.liferay.portal.kernel.dao.jdbc.SqlUpdate;
26  import com.liferay.portal.kernel.dao.jdbc.SqlUpdateFactoryUtil;
27  import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
28  import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
29  import com.liferay.portal.kernel.dao.orm.FinderPath;
30  import com.liferay.portal.kernel.dao.orm.Query;
31  import com.liferay.portal.kernel.dao.orm.QueryPos;
32  import com.liferay.portal.kernel.dao.orm.QueryUtil;
33  import com.liferay.portal.kernel.dao.orm.SQLQuery;
34  import com.liferay.portal.kernel.dao.orm.Session;
35  import com.liferay.portal.kernel.dao.orm.Type;
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.OrderByComparator;
40  import com.liferay.portal.kernel.util.SetUtil;
41  import com.liferay.portal.kernel.util.StringBundler;
42  import com.liferay.portal.kernel.util.StringPool;
43  import com.liferay.portal.kernel.util.StringUtil;
44  import com.liferay.portal.kernel.util.Validator;
45  import com.liferay.portal.model.ModelListener;
46  import com.liferay.portal.model.UserGroup;
47  import com.liferay.portal.model.impl.UserGroupImpl;
48  import com.liferay.portal.model.impl.UserGroupModelImpl;
49  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
50  
51  import java.io.Serializable;
52  
53  import java.sql.Types;
54  
55  import java.util.ArrayList;
56  import java.util.Collections;
57  import java.util.List;
58  import java.util.Set;
59  
60  /**
61   * <a href="UserGroupPersistenceImpl.java.html"><b><i>View Source</i></b></a>
62   *
63   * <p>
64   * ServiceBuilder generated this class. Modifications in this class will be
65   * overwritten the next time is generated.
66   * </p>
67   *
68   * @author    Brian Wing Shun Chan
69   * @see       UserGroupPersistence
70   * @see       UserGroupUtil
71   * @generated
72   */
73  public class UserGroupPersistenceImpl extends BasePersistenceImpl<UserGroup>
74      implements UserGroupPersistence {
75      public static final String FINDER_CLASS_NAME_ENTITY = UserGroupImpl.class.getName();
76      public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
77          ".List";
78      public static final FinderPath FINDER_PATH_FIND_BY_COMPANYID = new FinderPath(UserGroupModelImpl.ENTITY_CACHE_ENABLED,
79              UserGroupModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
80              "findByCompanyId", new String[] { Long.class.getName() });
81      public static final FinderPath FINDER_PATH_FIND_BY_OBC_COMPANYID = new FinderPath(UserGroupModelImpl.ENTITY_CACHE_ENABLED,
82              UserGroupModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
83              "findByCompanyId",
84              new String[] {
85                  Long.class.getName(),
86                  
87              "java.lang.Integer", "java.lang.Integer",
88                  "com.liferay.portal.kernel.util.OrderByComparator"
89              });
90      public static final FinderPath FINDER_PATH_COUNT_BY_COMPANYID = new FinderPath(UserGroupModelImpl.ENTITY_CACHE_ENABLED,
91              UserGroupModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
92              "countByCompanyId", new String[] { Long.class.getName() });
93      public static final FinderPath FINDER_PATH_FIND_BY_C_P = new FinderPath(UserGroupModelImpl.ENTITY_CACHE_ENABLED,
94              UserGroupModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
95              "findByC_P",
96              new String[] { Long.class.getName(), Long.class.getName() });
97      public static final FinderPath FINDER_PATH_FIND_BY_OBC_C_P = new FinderPath(UserGroupModelImpl.ENTITY_CACHE_ENABLED,
98              UserGroupModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
99              "findByC_P",
100             new String[] {
101                 Long.class.getName(), Long.class.getName(),
102                 
103             "java.lang.Integer", "java.lang.Integer",
104                 "com.liferay.portal.kernel.util.OrderByComparator"
105             });
106     public static final FinderPath FINDER_PATH_COUNT_BY_C_P = new FinderPath(UserGroupModelImpl.ENTITY_CACHE_ENABLED,
107             UserGroupModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
108             "countByC_P",
109             new String[] { Long.class.getName(), Long.class.getName() });
110     public static final FinderPath FINDER_PATH_FETCH_BY_C_N = new FinderPath(UserGroupModelImpl.ENTITY_CACHE_ENABLED,
111             UserGroupModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_ENTITY,
112             "fetchByC_N",
113             new String[] { Long.class.getName(), String.class.getName() });
114     public static final FinderPath FINDER_PATH_COUNT_BY_C_N = new FinderPath(UserGroupModelImpl.ENTITY_CACHE_ENABLED,
115             UserGroupModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
116             "countByC_N",
117             new String[] { Long.class.getName(), String.class.getName() });
118     public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(UserGroupModelImpl.ENTITY_CACHE_ENABLED,
119             UserGroupModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
120             "findAll", new String[0]);
121     public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(UserGroupModelImpl.ENTITY_CACHE_ENABLED,
122             UserGroupModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
123             "countAll", new String[0]);
124 
125     public void cacheResult(UserGroup userGroup) {
126         EntityCacheUtil.putResult(UserGroupModelImpl.ENTITY_CACHE_ENABLED,
127             UserGroupImpl.class, userGroup.getPrimaryKey(), userGroup);
128 
129         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_N,
130             new Object[] { new Long(userGroup.getCompanyId()), userGroup.getName() },
131             userGroup);
132     }
133 
134     public void cacheResult(List<UserGroup> userGroups) {
135         for (UserGroup userGroup : userGroups) {
136             if (EntityCacheUtil.getResult(
137                         UserGroupModelImpl.ENTITY_CACHE_ENABLED,
138                         UserGroupImpl.class, userGroup.getPrimaryKey(), this) == null) {
139                 cacheResult(userGroup);
140             }
141         }
142     }
143 
144     public void clearCache() {
145         CacheRegistry.clear(UserGroupImpl.class.getName());
146         EntityCacheUtil.clearCache(UserGroupImpl.class.getName());
147         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
148         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
149     }
150 
151     public UserGroup create(long userGroupId) {
152         UserGroup userGroup = new UserGroupImpl();
153 
154         userGroup.setNew(true);
155         userGroup.setPrimaryKey(userGroupId);
156 
157         return userGroup;
158     }
159 
160     public UserGroup remove(Serializable primaryKey)
161         throws NoSuchModelException, SystemException {
162         return remove(((Long)primaryKey).longValue());
163     }
164 
165     public UserGroup remove(long userGroupId)
166         throws NoSuchUserGroupException, SystemException {
167         Session session = null;
168 
169         try {
170             session = openSession();
171 
172             UserGroup userGroup = (UserGroup)session.get(UserGroupImpl.class,
173                     new Long(userGroupId));
174 
175             if (userGroup == null) {
176                 if (_log.isWarnEnabled()) {
177                     _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + userGroupId);
178                 }
179 
180                 throw new NoSuchUserGroupException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
181                     userGroupId);
182             }
183 
184             return remove(userGroup);
185         }
186         catch (NoSuchUserGroupException nsee) {
187             throw nsee;
188         }
189         catch (Exception e) {
190             throw processException(e);
191         }
192         finally {
193             closeSession(session);
194         }
195     }
196 
197     public UserGroup remove(UserGroup userGroup) throws SystemException {
198         for (ModelListener<UserGroup> listener : listeners) {
199             listener.onBeforeRemove(userGroup);
200         }
201 
202         userGroup = removeImpl(userGroup);
203 
204         for (ModelListener<UserGroup> listener : listeners) {
205             listener.onAfterRemove(userGroup);
206         }
207 
208         return userGroup;
209     }
210 
211     protected UserGroup removeImpl(UserGroup userGroup)
212         throws SystemException {
213         userGroup = toUnwrappedModel(userGroup);
214 
215         try {
216             clearUsers.clear(userGroup.getPrimaryKey());
217         }
218         catch (Exception e) {
219             throw processException(e);
220         }
221         finally {
222             FinderCacheUtil.clearCache(UserGroupModelImpl.MAPPING_TABLE_USERS_USERGROUPS_NAME);
223         }
224 
225         Session session = null;
226 
227         try {
228             session = openSession();
229 
230             if (userGroup.isCachedModel() || BatchSessionUtil.isEnabled()) {
231                 Object staleObject = session.get(UserGroupImpl.class,
232                         userGroup.getPrimaryKeyObj());
233 
234                 if (staleObject != null) {
235                     session.evict(staleObject);
236                 }
237             }
238 
239             session.delete(userGroup);
240 
241             session.flush();
242         }
243         catch (Exception e) {
244             throw processException(e);
245         }
246         finally {
247             closeSession(session);
248         }
249 
250         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
251 
252         UserGroupModelImpl userGroupModelImpl = (UserGroupModelImpl)userGroup;
253 
254         FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_N,
255             new Object[] {
256                 new Long(userGroupModelImpl.getOriginalCompanyId()),
257                 
258             userGroupModelImpl.getOriginalName()
259             });
260 
261         EntityCacheUtil.removeResult(UserGroupModelImpl.ENTITY_CACHE_ENABLED,
262             UserGroupImpl.class, userGroup.getPrimaryKey());
263 
264         return userGroup;
265     }
266 
267     /**
268      * @deprecated Use {@link BasePersistence#update(com.liferay.portal.model.BaseModel, boolean)}.
269      */
270     public UserGroup update(UserGroup userGroup) throws SystemException {
271         if (_log.isWarnEnabled()) {
272             _log.warn(
273                 "Using the deprecated update(UserGroup userGroup) method. Use update(UserGroup userGroup, boolean merge) instead.");
274         }
275 
276         return update(userGroup, false);
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         Object[] finderArgs = new Object[] { new Long(companyId) };
412 
413         List<UserGroup> list = (List<UserGroup>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_COMPANYID,
414                 finderArgs, this);
415 
416         if (list == null) {
417             Session session = null;
418 
419             try {
420                 session = openSession();
421 
422                 StringBundler query = new StringBundler(3);
423 
424                 query.append(_SQL_SELECT_USERGROUP_WHERE);
425 
426                 query.append(_FINDER_COLUMN_COMPANYID_COMPANYID_2);
427 
428                 query.append(UserGroupModelImpl.ORDER_BY_JPQL);
429 
430                 String sql = query.toString();
431 
432                 Query q = session.createQuery(sql);
433 
434                 QueryPos qPos = QueryPos.getInstance(q);
435 
436                 qPos.add(companyId);
437 
438                 list = q.list();
439             }
440             catch (Exception e) {
441                 throw processException(e);
442             }
443             finally {
444                 if (list == null) {
445                     list = new ArrayList<UserGroup>();
446                 }
447 
448                 cacheResult(list);
449 
450                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_COMPANYID,
451                     finderArgs, list);
452 
453                 closeSession(session);
454             }
455         }
456 
457         return list;
458     }
459 
460     public List<UserGroup> findByCompanyId(long companyId, int start, int end)
461         throws SystemException {
462         return findByCompanyId(companyId, start, end, null);
463     }
464 
465     public List<UserGroup> findByCompanyId(long companyId, int start, int end,
466         OrderByComparator orderByComparator) throws SystemException {
467         Object[] finderArgs = new Object[] {
468                 new Long(companyId),
469                 
470                 String.valueOf(start), String.valueOf(end),
471                 String.valueOf(orderByComparator)
472             };
473 
474         List<UserGroup> list = (List<UserGroup>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_OBC_COMPANYID,
475                 finderArgs, this);
476 
477         if (list == null) {
478             Session session = null;
479 
480             try {
481                 session = openSession();
482 
483                 StringBundler query = null;
484 
485                 if (orderByComparator != null) {
486                     query = new StringBundler(3 +
487                             (orderByComparator.getOrderByFields().length * 3));
488                 }
489                 else {
490                     query = new StringBundler(3);
491                 }
492 
493                 query.append(_SQL_SELECT_USERGROUP_WHERE);
494 
495                 query.append(_FINDER_COLUMN_COMPANYID_COMPANYID_2);
496 
497                 if (orderByComparator != null) {
498                     appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
499                         orderByComparator);
500                 }
501 
502                 else {
503                     query.append(UserGroupModelImpl.ORDER_BY_JPQL);
504                 }
505 
506                 String sql = query.toString();
507 
508                 Query q = session.createQuery(sql);
509 
510                 QueryPos qPos = QueryPos.getInstance(q);
511 
512                 qPos.add(companyId);
513 
514                 list = (List<UserGroup>)QueryUtil.list(q, getDialect(), start,
515                         end);
516             }
517             catch (Exception e) {
518                 throw processException(e);
519             }
520             finally {
521                 if (list == null) {
522                     list = new ArrayList<UserGroup>();
523                 }
524 
525                 cacheResult(list);
526 
527                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_OBC_COMPANYID,
528                     finderArgs, list);
529 
530                 closeSession(session);
531             }
532         }
533 
534         return list;
535     }
536 
537     public UserGroup findByCompanyId_First(long companyId,
538         OrderByComparator orderByComparator)
539         throws NoSuchUserGroupException, SystemException {
540         List<UserGroup> list = findByCompanyId(companyId, 0, 1,
541                 orderByComparator);
542 
543         if (list.isEmpty()) {
544             StringBundler msg = new StringBundler(4);
545 
546             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
547 
548             msg.append("companyId=");
549             msg.append(companyId);
550 
551             msg.append(StringPool.CLOSE_CURLY_BRACE);
552 
553             throw new NoSuchUserGroupException(msg.toString());
554         }
555         else {
556             return list.get(0);
557         }
558     }
559 
560     public UserGroup findByCompanyId_Last(long companyId,
561         OrderByComparator orderByComparator)
562         throws NoSuchUserGroupException, SystemException {
563         int count = countByCompanyId(companyId);
564 
565         List<UserGroup> list = findByCompanyId(companyId, count - 1, count,
566                 orderByComparator);
567 
568         if (list.isEmpty()) {
569             StringBundler msg = new StringBundler(4);
570 
571             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
572 
573             msg.append("companyId=");
574             msg.append(companyId);
575 
576             msg.append(StringPool.CLOSE_CURLY_BRACE);
577 
578             throw new NoSuchUserGroupException(msg.toString());
579         }
580         else {
581             return list.get(0);
582         }
583     }
584 
585     public UserGroup[] findByCompanyId_PrevAndNext(long userGroupId,
586         long companyId, OrderByComparator orderByComparator)
587         throws NoSuchUserGroupException, SystemException {
588         UserGroup userGroup = findByPrimaryKey(userGroupId);
589 
590         int count = countByCompanyId(companyId);
591 
592         Session session = null;
593 
594         try {
595             session = openSession();
596 
597             StringBundler query = null;
598 
599             if (orderByComparator != null) {
600                 query = new StringBundler(3 +
601                         (orderByComparator.getOrderByFields().length * 3));
602             }
603             else {
604                 query = new StringBundler(3);
605             }
606 
607             query.append(_SQL_SELECT_USERGROUP_WHERE);
608 
609             query.append(_FINDER_COLUMN_COMPANYID_COMPANYID_2);
610 
611             if (orderByComparator != null) {
612                 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
613                     orderByComparator);
614             }
615 
616             else {
617                 query.append(UserGroupModelImpl.ORDER_BY_JPQL);
618             }
619 
620             String sql = query.toString();
621 
622             Query q = session.createQuery(sql);
623 
624             QueryPos qPos = QueryPos.getInstance(q);
625 
626             qPos.add(companyId);
627 
628             Object[] objArray = QueryUtil.getPrevAndNext(q, count,
629                     orderByComparator, userGroup);
630 
631             UserGroup[] array = new UserGroupImpl[3];
632 
633             array[0] = (UserGroup)objArray[0];
634             array[1] = (UserGroup)objArray[1];
635             array[2] = (UserGroup)objArray[2];
636 
637             return array;
638         }
639         catch (Exception e) {
640             throw processException(e);
641         }
642         finally {
643             closeSession(session);
644         }
645     }
646 
647     public List<UserGroup> findByC_P(long companyId, long parentUserGroupId)
648         throws SystemException {
649         Object[] finderArgs = new Object[] {
650                 new Long(companyId), new Long(parentUserGroupId)
651             };
652 
653         List<UserGroup> list = (List<UserGroup>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_C_P,
654                 finderArgs, this);
655 
656         if (list == null) {
657             Session session = null;
658 
659             try {
660                 session = openSession();
661 
662                 StringBundler query = new StringBundler(4);
663 
664                 query.append(_SQL_SELECT_USERGROUP_WHERE);
665 
666                 query.append(_FINDER_COLUMN_C_P_COMPANYID_2);
667 
668                 query.append(_FINDER_COLUMN_C_P_PARENTUSERGROUPID_2);
669 
670                 query.append(UserGroupModelImpl.ORDER_BY_JPQL);
671 
672                 String sql = query.toString();
673 
674                 Query q = session.createQuery(sql);
675 
676                 QueryPos qPos = QueryPos.getInstance(q);
677 
678                 qPos.add(companyId);
679 
680                 qPos.add(parentUserGroupId);
681 
682                 list = q.list();
683             }
684             catch (Exception e) {
685                 throw processException(e);
686             }
687             finally {
688                 if (list == null) {
689                     list = new ArrayList<UserGroup>();
690                 }
691 
692                 cacheResult(list);
693 
694                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_C_P, finderArgs,
695                     list);
696 
697                 closeSession(session);
698             }
699         }
700 
701         return list;
702     }
703 
704     public List<UserGroup> findByC_P(long companyId, long parentUserGroupId,
705         int start, int end) throws SystemException {
706         return findByC_P(companyId, parentUserGroupId, start, end, null);
707     }
708 
709     public List<UserGroup> findByC_P(long companyId, long parentUserGroupId,
710         int start, int end, OrderByComparator orderByComparator)
711         throws SystemException {
712         Object[] finderArgs = new Object[] {
713                 new Long(companyId), new Long(parentUserGroupId),
714                 
715                 String.valueOf(start), String.valueOf(end),
716                 String.valueOf(orderByComparator)
717             };
718 
719         List<UserGroup> list = (List<UserGroup>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_OBC_C_P,
720                 finderArgs, this);
721 
722         if (list == null) {
723             Session session = null;
724 
725             try {
726                 session = openSession();
727 
728                 StringBundler query = null;
729 
730                 if (orderByComparator != null) {
731                     query = new StringBundler(4 +
732                             (orderByComparator.getOrderByFields().length * 3));
733                 }
734                 else {
735                     query = new StringBundler(4);
736                 }
737 
738                 query.append(_SQL_SELECT_USERGROUP_WHERE);
739 
740                 query.append(_FINDER_COLUMN_C_P_COMPANYID_2);
741 
742                 query.append(_FINDER_COLUMN_C_P_PARENTUSERGROUPID_2);
743 
744                 if (orderByComparator != null) {
745                     appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
746                         orderByComparator);
747                 }
748 
749                 else {
750                     query.append(UserGroupModelImpl.ORDER_BY_JPQL);
751                 }
752 
753                 String sql = query.toString();
754 
755                 Query q = session.createQuery(sql);
756 
757                 QueryPos qPos = QueryPos.getInstance(q);
758 
759                 qPos.add(companyId);
760 
761                 qPos.add(parentUserGroupId);
762 
763                 list = (List<UserGroup>)QueryUtil.list(q, getDialect(), start,
764                         end);
765             }
766             catch (Exception e) {
767                 throw processException(e);
768             }
769             finally {
770                 if (list == null) {
771                     list = new ArrayList<UserGroup>();
772                 }
773 
774                 cacheResult(list);
775 
776                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_OBC_C_P,
777                     finderArgs, list);
778 
779                 closeSession(session);
780             }
781         }
782 
783         return list;
784     }
785 
786     public UserGroup findByC_P_First(long companyId, long parentUserGroupId,
787         OrderByComparator orderByComparator)
788         throws NoSuchUserGroupException, SystemException {
789         List<UserGroup> list = findByC_P(companyId, parentUserGroupId, 0, 1,
790                 orderByComparator);
791 
792         if (list.isEmpty()) {
793             StringBundler msg = new StringBundler(6);
794 
795             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
796 
797             msg.append("companyId=");
798             msg.append(companyId);
799 
800             msg.append(", parentUserGroupId=");
801             msg.append(parentUserGroupId);
802 
803             msg.append(StringPool.CLOSE_CURLY_BRACE);
804 
805             throw new NoSuchUserGroupException(msg.toString());
806         }
807         else {
808             return list.get(0);
809         }
810     }
811 
812     public UserGroup findByC_P_Last(long companyId, long parentUserGroupId,
813         OrderByComparator orderByComparator)
814         throws NoSuchUserGroupException, SystemException {
815         int count = countByC_P(companyId, parentUserGroupId);
816 
817         List<UserGroup> list = findByC_P(companyId, parentUserGroupId,
818                 count - 1, count, orderByComparator);
819 
820         if (list.isEmpty()) {
821             StringBundler msg = new StringBundler(6);
822 
823             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
824 
825             msg.append("companyId=");
826             msg.append(companyId);
827 
828             msg.append(", parentUserGroupId=");
829             msg.append(parentUserGroupId);
830 
831             msg.append(StringPool.CLOSE_CURLY_BRACE);
832 
833             throw new NoSuchUserGroupException(msg.toString());
834         }
835         else {
836             return list.get(0);
837         }
838     }
839 
840     public UserGroup[] findByC_P_PrevAndNext(long userGroupId, long companyId,
841         long parentUserGroupId, OrderByComparator orderByComparator)
842         throws NoSuchUserGroupException, SystemException {
843         UserGroup userGroup = findByPrimaryKey(userGroupId);
844 
845         int count = countByC_P(companyId, parentUserGroupId);
846 
847         Session session = null;
848 
849         try {
850             session = openSession();
851 
852             StringBundler query = null;
853 
854             if (orderByComparator != null) {
855                 query = new StringBundler(4 +
856                         (orderByComparator.getOrderByFields().length * 3));
857             }
858             else {
859                 query = new StringBundler(4);
860             }
861 
862             query.append(_SQL_SELECT_USERGROUP_WHERE);
863 
864             query.append(_FINDER_COLUMN_C_P_COMPANYID_2);
865 
866             query.append(_FINDER_COLUMN_C_P_PARENTUSERGROUPID_2);
867 
868             if (orderByComparator != null) {
869                 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
870                     orderByComparator);
871             }
872 
873             else {
874                 query.append(UserGroupModelImpl.ORDER_BY_JPQL);
875             }
876 
877             String sql = query.toString();
878 
879             Query q = session.createQuery(sql);
880 
881             QueryPos qPos = QueryPos.getInstance(q);
882 
883             qPos.add(companyId);
884 
885             qPos.add(parentUserGroupId);
886 
887             Object[] objArray = QueryUtil.getPrevAndNext(q, count,
888                     orderByComparator, userGroup);
889 
890             UserGroup[] array = new UserGroupImpl[3];
891 
892             array[0] = (UserGroup)objArray[0];
893             array[1] = (UserGroup)objArray[1];
894             array[2] = (UserGroup)objArray[2];
895 
896             return array;
897         }
898         catch (Exception e) {
899             throw processException(e);
900         }
901         finally {
902             closeSession(session);
903         }
904     }
905 
906     public UserGroup findByC_N(long companyId, String name)
907         throws NoSuchUserGroupException, SystemException {
908         UserGroup userGroup = fetchByC_N(companyId, name);
909 
910         if (userGroup == null) {
911             StringBundler msg = new StringBundler(6);
912 
913             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
914 
915             msg.append("companyId=");
916             msg.append(companyId);
917 
918             msg.append(", name=");
919             msg.append(name);
920 
921             msg.append(StringPool.CLOSE_CURLY_BRACE);
922 
923             if (_log.isWarnEnabled()) {
924                 _log.warn(msg.toString());
925             }
926 
927             throw new NoSuchUserGroupException(msg.toString());
928         }
929 
930         return userGroup;
931     }
932 
933     public UserGroup fetchByC_N(long companyId, String name)
934         throws SystemException {
935         return fetchByC_N(companyId, name, true);
936     }
937 
938     public UserGroup fetchByC_N(long companyId, String name,
939         boolean retrieveFromCache) throws SystemException {
940         Object[] finderArgs = new Object[] { new Long(companyId), name };
941 
942         Object result = null;
943 
944         if (retrieveFromCache) {
945             result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_C_N,
946                     finderArgs, this);
947         }
948 
949         if (result == null) {
950             Session session = null;
951 
952             try {
953                 session = openSession();
954 
955                 StringBundler query = new StringBundler(4);
956 
957                 query.append(_SQL_SELECT_USERGROUP_WHERE);
958 
959                 query.append(_FINDER_COLUMN_C_N_COMPANYID_2);
960 
961                 if (name == null) {
962                     query.append(_FINDER_COLUMN_C_N_NAME_1);
963                 }
964                 else {
965                     if (name.equals(StringPool.BLANK)) {
966                         query.append(_FINDER_COLUMN_C_N_NAME_3);
967                     }
968                     else {
969                         query.append(_FINDER_COLUMN_C_N_NAME_2);
970                     }
971                 }
972 
973                 query.append(UserGroupModelImpl.ORDER_BY_JPQL);
974 
975                 String sql = query.toString();
976 
977                 Query q = session.createQuery(sql);
978 
979                 QueryPos qPos = QueryPos.getInstance(q);
980 
981                 qPos.add(companyId);
982 
983                 if (name != null) {
984                     qPos.add(name);
985                 }
986 
987                 List<UserGroup> list = q.list();
988 
989                 result = list;
990 
991                 UserGroup userGroup = null;
992 
993                 if (list.isEmpty()) {
994                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_N,
995                         finderArgs, list);
996                 }
997                 else {
998                     userGroup = list.get(0);
999 
1000                    cacheResult(userGroup);
1001
1002                    if ((userGroup.getCompanyId() != companyId) ||
1003                            (userGroup.getName() == null) ||
1004                            !userGroup.getName().equals(name)) {
1005                        FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_N,
1006                            finderArgs, userGroup);
1007                    }
1008                }
1009
1010                return userGroup;
1011            }
1012            catch (Exception e) {
1013                throw processException(e);
1014            }
1015            finally {
1016                if (result == null) {
1017                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_N,
1018                        finderArgs, new ArrayList<UserGroup>());
1019                }
1020
1021                closeSession(session);
1022            }
1023        }
1024        else {
1025            if (result instanceof List<?>) {
1026                return null;
1027            }
1028            else {
1029                return (UserGroup)result;
1030            }
1031        }
1032    }
1033
1034    public List<UserGroup> findAll() throws SystemException {
1035        return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
1036    }
1037
1038    public List<UserGroup> findAll(int start, int end)
1039        throws SystemException {
1040        return findAll(start, end, null);
1041    }
1042
1043    public List<UserGroup> findAll(int start, int end,
1044        OrderByComparator orderByComparator) throws SystemException {
1045        Object[] finderArgs = new Object[] {
1046                String.valueOf(start), String.valueOf(end),
1047                String.valueOf(orderByComparator)
1048            };
1049
1050        List<UserGroup> list = (List<UserGroup>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
1051                finderArgs, this);
1052
1053        if (list == null) {
1054            Session session = null;
1055
1056            try {
1057                session = openSession();
1058
1059                StringBundler query = null;
1060                String sql = null;
1061
1062                if (orderByComparator != null) {
1063                    query = new StringBundler(2 +
1064                            (orderByComparator.getOrderByFields().length * 3));
1065
1066                    query.append(_SQL_SELECT_USERGROUP);
1067
1068                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1069                        orderByComparator);
1070
1071                    sql = query.toString();
1072                }
1073
1074                else {
1075                    sql = _SQL_SELECT_USERGROUP.concat(UserGroupModelImpl.ORDER_BY_JPQL);
1076                }
1077
1078                Query q = session.createQuery(sql);
1079
1080                if (orderByComparator == null) {
1081                    list = (List<UserGroup>)QueryUtil.list(q, getDialect(),
1082                            start, end, false);
1083
1084                    Collections.sort(list);
1085                }
1086                else {
1087                    list = (List<UserGroup>)QueryUtil.list(q, getDialect(),
1088                            start, end);
1089                }
1090            }
1091            catch (Exception e) {
1092                throw processException(e);
1093            }
1094            finally {
1095                if (list == null) {
1096                    list = new ArrayList<UserGroup>();
1097                }
1098
1099                cacheResult(list);
1100
1101                FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
1102
1103                closeSession(session);
1104            }
1105        }
1106
1107        return list;
1108    }
1109
1110    public void removeByCompanyId(long companyId) throws SystemException {
1111        for (UserGroup userGroup : findByCompanyId(companyId)) {
1112            remove(userGroup);
1113        }
1114    }
1115
1116    public void removeByC_P(long companyId, long parentUserGroupId)
1117        throws SystemException {
1118        for (UserGroup userGroup : findByC_P(companyId, parentUserGroupId)) {
1119            remove(userGroup);
1120        }
1121    }
1122
1123    public void removeByC_N(long companyId, String name)
1124        throws NoSuchUserGroupException, SystemException {
1125        UserGroup userGroup = findByC_N(companyId, name);
1126
1127        remove(userGroup);
1128    }
1129
1130    public void removeAll() throws SystemException {
1131        for (UserGroup userGroup : findAll()) {
1132            remove(userGroup);
1133        }
1134    }
1135
1136    public int countByCompanyId(long companyId) throws SystemException {
1137        Object[] finderArgs = new Object[] { new Long(companyId) };
1138
1139        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_COMPANYID,
1140                finderArgs, this);
1141
1142        if (count == null) {
1143            Session session = null;
1144
1145            try {
1146                session = openSession();
1147
1148                StringBundler query = new StringBundler(2);
1149
1150                query.append(_SQL_COUNT_USERGROUP_WHERE);
1151
1152                query.append(_FINDER_COLUMN_COMPANYID_COMPANYID_2);
1153
1154                String sql = query.toString();
1155
1156                Query q = session.createQuery(sql);
1157
1158                QueryPos qPos = QueryPos.getInstance(q);
1159
1160                qPos.add(companyId);
1161
1162                count = (Long)q.uniqueResult();
1163            }
1164            catch (Exception e) {
1165                throw processException(e);
1166            }
1167            finally {
1168                if (count == null) {
1169                    count = Long.valueOf(0);
1170                }
1171
1172                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_COMPANYID,
1173                    finderArgs, count);
1174
1175                closeSession(session);
1176            }
1177        }
1178
1179        return count.intValue();
1180    }
1181
1182    public int countByC_P(long companyId, long parentUserGroupId)
1183        throws SystemException {
1184        Object[] finderArgs = new Object[] {
1185                new Long(companyId), new Long(parentUserGroupId)
1186            };
1187
1188        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_C_P,
1189                finderArgs, this);
1190
1191        if (count == null) {
1192            Session session = null;
1193
1194            try {
1195                session = openSession();
1196
1197                StringBundler query = new StringBundler(3);
1198
1199                query.append(_SQL_COUNT_USERGROUP_WHERE);
1200
1201                query.append(_FINDER_COLUMN_C_P_COMPANYID_2);
1202
1203                query.append(_FINDER_COLUMN_C_P_PARENTUSERGROUPID_2);
1204
1205                String sql = query.toString();
1206
1207                Query q = session.createQuery(sql);
1208
1209                QueryPos qPos = QueryPos.getInstance(q);
1210
1211                qPos.add(companyId);
1212
1213                qPos.add(parentUserGroupId);
1214
1215                count = (Long)q.uniqueResult();
1216            }
1217            catch (Exception e) {
1218                throw processException(e);
1219            }
1220            finally {
1221                if (count == null) {
1222                    count = Long.valueOf(0);
1223                }
1224
1225                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_P, finderArgs,
1226                    count);
1227
1228                closeSession(session);
1229            }
1230        }
1231
1232        return count.intValue();
1233    }
1234
1235    public int countByC_N(long companyId, String name)
1236        throws SystemException {
1237        Object[] finderArgs = new Object[] { new Long(companyId), name };
1238
1239        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_C_N,
1240                finderArgs, this);
1241
1242        if (count == null) {
1243            Session session = null;
1244
1245            try {
1246                session = openSession();
1247
1248                StringBundler query = new StringBundler(3);
1249
1250                query.append(_SQL_COUNT_USERGROUP_WHERE);
1251
1252                query.append(_FINDER_COLUMN_C_N_COMPANYID_2);
1253
1254                if (name == null) {
1255                    query.append(_FINDER_COLUMN_C_N_NAME_1);
1256                }
1257                else {
1258                    if (name.equals(StringPool.BLANK)) {
1259                        query.append(_FINDER_COLUMN_C_N_NAME_3);
1260                    }
1261                    else {
1262                        query.append(_FINDER_COLUMN_C_N_NAME_2);
1263                    }
1264                }
1265
1266                String sql = query.toString();
1267
1268                Query q = session.createQuery(sql);
1269
1270                QueryPos qPos = QueryPos.getInstance(q);
1271
1272                qPos.add(companyId);
1273
1274                if (name != null) {
1275                    qPos.add(name);
1276                }
1277
1278                count = (Long)q.uniqueResult();
1279            }
1280            catch (Exception e) {
1281                throw processException(e);
1282            }
1283            finally {
1284                if (count == null) {
1285                    count = Long.valueOf(0);
1286                }
1287
1288                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_N, finderArgs,
1289                    count);
1290
1291                closeSession(session);
1292            }
1293        }
1294
1295        return count.intValue();
1296    }
1297
1298    public int countAll() throws SystemException {
1299        Object[] finderArgs = new Object[0];
1300
1301        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1302                finderArgs, this);
1303
1304        if (count == null) {
1305            Session session = null;
1306
1307            try {
1308                session = openSession();
1309
1310                Query q = session.createQuery(_SQL_COUNT_USERGROUP);
1311
1312                count = (Long)q.uniqueResult();
1313            }
1314            catch (Exception e) {
1315                throw processException(e);
1316            }
1317            finally {
1318                if (count == null) {
1319                    count = Long.valueOf(0);
1320                }
1321
1322                FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
1323                    count);
1324
1325                closeSession(session);
1326            }
1327        }
1328
1329        return count.intValue();
1330    }
1331
1332    public List<com.liferay.portal.model.User> getUsers(long pk)
1333        throws SystemException {
1334        return getUsers(pk, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
1335    }
1336
1337    public List<com.liferay.portal.model.User> getUsers(long pk, int start,
1338        int end) throws SystemException {
1339        return getUsers(pk, start, end, null);
1340    }
1341
1342    public static final FinderPath FINDER_PATH_GET_USERS = new FinderPath(com.liferay.portal.model.impl.UserModelImpl.ENTITY_CACHE_ENABLED,
1343            UserGroupModelImpl.FINDER_CACHE_ENABLED_USERS_USERGROUPS,
1344            UserGroupModelImpl.MAPPING_TABLE_USERS_USERGROUPS_NAME, "getUsers",
1345            new String[] {
1346                Long.class.getName(), "java.lang.Integer", "java.lang.Integer",
1347                "com.liferay.portal.kernel.util.OrderByComparator"
1348            });
1349
1350    public List<com.liferay.portal.model.User> getUsers(long pk, int start,
1351        int end, OrderByComparator orderByComparator) throws SystemException {
1352        Object[] finderArgs = new Object[] {
1353                new Long(pk), String.valueOf(start), String.valueOf(end),
1354                String.valueOf(orderByComparator)
1355            };
1356
1357        List<com.liferay.portal.model.User> list = (List<com.liferay.portal.model.User>)FinderCacheUtil.getResult(FINDER_PATH_GET_USERS,
1358                finderArgs, this);
1359
1360        if (list == null) {
1361            Session session = null;
1362
1363            try {
1364                session = openSession();
1365
1366                String sql = null;
1367
1368                if (orderByComparator != null) {
1369                    sql = _SQL_GETUSERS.concat(ORDER_BY_CLAUSE)
1370                                       .concat(orderByComparator.getOrderBy());
1371                }
1372
1373                sql = _SQL_GETUSERS;
1374
1375                SQLQuery q = session.createSQLQuery(sql);
1376
1377                q.addEntity("User_",
1378                    com.liferay.portal.model.impl.UserImpl.class);
1379
1380                QueryPos qPos = QueryPos.getInstance(q);
1381
1382                qPos.add(pk);
1383
1384                list = (List<com.liferay.portal.model.User>)QueryUtil.list(q,
1385                        getDialect(), start, end);
1386            }
1387            catch (Exception e) {
1388                throw processException(e);
1389            }
1390            finally {
1391                if (list == null) {
1392                    list = new ArrayList<com.liferay.portal.model.User>();
1393                }
1394
1395                userPersistence.cacheResult(list);
1396
1397                FinderCacheUtil.putResult(FINDER_PATH_GET_USERS, finderArgs,
1398                    list);
1399
1400                closeSession(session);
1401            }
1402        }
1403
1404        return list;
1405    }
1406
1407    public static final FinderPath FINDER_PATH_GET_USERS_SIZE = new FinderPath(com.liferay.portal.model.impl.UserModelImpl.ENTITY_CACHE_ENABLED,
1408            UserGroupModelImpl.FINDER_CACHE_ENABLED_USERS_USERGROUPS,
1409            UserGroupModelImpl.MAPPING_TABLE_USERS_USERGROUPS_NAME,
1410            "getUsersSize", new String[] { Long.class.getName() });
1411
1412    public int getUsersSize(long pk) throws SystemException {
1413        Object[] finderArgs = new Object[] { new Long(pk) };
1414
1415        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_GET_USERS_SIZE,
1416                finderArgs, this);
1417
1418        if (count == null) {
1419            Session session = null;
1420
1421            try {
1422                session = openSession();
1423
1424                SQLQuery q = session.createSQLQuery(_SQL_GETUSERSSIZE);
1425
1426                q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
1427
1428                QueryPos qPos = QueryPos.getInstance(q);
1429
1430                qPos.add(pk);
1431
1432                count = (Long)q.uniqueResult();
1433            }
1434            catch (Exception e) {
1435                throw processException(e);
1436            }
1437            finally {
1438                if (count == null) {
1439                    count = Long.valueOf(0);
1440                }
1441
1442                FinderCacheUtil.putResult(FINDER_PATH_GET_USERS_SIZE,
1443                    finderArgs, count);
1444
1445                closeSession(session);
1446            }
1447        }
1448
1449        return count.intValue();
1450    }
1451
1452    public static final FinderPath FINDER_PATH_CONTAINS_USER = new FinderPath(com.liferay.portal.model.impl.UserModelImpl.ENTITY_CACHE_ENABLED,
1453            UserGroupModelImpl.FINDER_CACHE_ENABLED_USERS_USERGROUPS,
1454            UserGroupModelImpl.MAPPING_TABLE_USERS_USERGROUPS_NAME,
1455            "containsUser",
1456            new String[] { Long.class.getName(), Long.class.getName() });
1457
1458    public boolean containsUser(long pk, long userPK) throws SystemException {
1459        Object[] finderArgs = new Object[] { new Long(pk), new Long(userPK) };
1460
1461        Boolean value = (Boolean)FinderCacheUtil.getResult(FINDER_PATH_CONTAINS_USER,
1462                finderArgs, this);
1463
1464        if (value == null) {
1465            try {
1466                value = Boolean.valueOf(containsUser.contains(pk, userPK));
1467            }
1468            catch (Exception e) {
1469                throw processException(e);
1470            }
1471            finally {
1472                if (value == null) {
1473                    value = Boolean.FALSE;
1474                }
1475
1476                FinderCacheUtil.putResult(FINDER_PATH_CONTAINS_USER,
1477                    finderArgs, value);
1478            }
1479        }
1480
1481        return value.booleanValue();
1482    }
1483
1484    public boolean containsUsers(long pk) throws SystemException {
1485        if (getUsersSize(pk) > 0) {
1486            return true;
1487        }
1488        else {
1489            return false;
1490        }
1491    }
1492
1493    public void addUser(long pk, long userPK) throws SystemException {
1494        try {
1495            addUser.add(pk, userPK);
1496        }
1497        catch (Exception e) {
1498            throw processException(e);
1499        }
1500        finally {
1501            FinderCacheUtil.clearCache(UserGroupModelImpl.MAPPING_TABLE_USERS_USERGROUPS_NAME);
1502        }
1503    }
1504
1505    public void addUser(long pk, com.liferay.portal.model.User user)
1506        throws SystemException {
1507        try {
1508            addUser.add(pk, user.getPrimaryKey());
1509        }
1510        catch (Exception e) {
1511            throw processException(e);
1512        }
1513        finally {
1514            FinderCacheUtil.clearCache(UserGroupModelImpl.MAPPING_TABLE_USERS_USERGROUPS_NAME);
1515        }
1516    }
1517
1518    public void addUsers(long pk, long[] userPKs) throws SystemException {
1519        try {
1520            for (long userPK : userPKs) {
1521                addUser.add(pk, userPK);
1522            }
1523        }
1524        catch (Exception e) {
1525            throw processException(e);
1526        }
1527        finally {
1528            FinderCacheUtil.clearCache(UserGroupModelImpl.MAPPING_TABLE_USERS_USERGROUPS_NAME);
1529        }
1530    }
1531
1532    public void addUsers(long pk, List<com.liferay.portal.model.User> users)
1533        throws SystemException {
1534        try {
1535            for (com.liferay.portal.model.User user : users) {
1536                addUser.add(pk, user.getPrimaryKey());
1537            }
1538        }
1539        catch (Exception e) {
1540            throw processException(e);
1541        }
1542        finally {
1543            FinderCacheUtil.clearCache(UserGroupModelImpl.MAPPING_TABLE_USERS_USERGROUPS_NAME);
1544        }
1545    }
1546
1547    public void clearUsers(long pk) throws SystemException {
1548        try {
1549            clearUsers.clear(pk);
1550        }
1551        catch (Exception e) {
1552            throw processException(e);
1553        }
1554        finally {
1555            FinderCacheUtil.clearCache(UserGroupModelImpl.MAPPING_TABLE_USERS_USERGROUPS_NAME);
1556        }
1557    }
1558
1559    public void removeUser(long pk, long userPK) throws SystemException {
1560        try {
1561            removeUser.remove(pk, userPK);
1562        }
1563        catch (Exception e) {
1564            throw processException(e);
1565        }
1566        finally {
1567            FinderCacheUtil.clearCache(UserGroupModelImpl.MAPPING_TABLE_USERS_USERGROUPS_NAME);
1568        }
1569    }
1570
1571    public void removeUser(long pk, com.liferay.portal.model.User user)
1572        throws SystemException {
1573        try {
1574            removeUser.remove(pk, user.getPrimaryKey());
1575        }
1576        catch (Exception e) {
1577            throw processException(e);
1578        }
1579        finally {
1580            FinderCacheUtil.clearCache(UserGroupModelImpl.MAPPING_TABLE_USERS_USERGROUPS_NAME);
1581        }
1582    }
1583
1584    public void removeUsers(long pk, long[] userPKs) throws SystemException {
1585        try {
1586            for (long userPK : userPKs) {
1587                removeUser.remove(pk, userPK);
1588            }
1589        }
1590        catch (Exception e) {
1591            throw processException(e);
1592        }
1593        finally {
1594            FinderCacheUtil.clearCache(UserGroupModelImpl.MAPPING_TABLE_USERS_USERGROUPS_NAME);
1595        }
1596    }
1597
1598    public void removeUsers(long pk, List<com.liferay.portal.model.User> users)
1599        throws SystemException {
1600        try {
1601            for (com.liferay.portal.model.User user : users) {
1602                removeUser.remove(pk, user.getPrimaryKey());
1603            }
1604        }
1605        catch (Exception e) {
1606            throw processException(e);
1607        }
1608        finally {
1609            FinderCacheUtil.clearCache(UserGroupModelImpl.MAPPING_TABLE_USERS_USERGROUPS_NAME);
1610        }
1611    }
1612
1613    public void setUsers(long pk, long[] userPKs) throws SystemException {
1614        try {
1615            Set<Long> userPKSet = SetUtil.fromArray(userPKs);
1616
1617            List<com.liferay.portal.model.User> users = getUsers(pk);
1618
1619            for (com.liferay.portal.model.User user : users) {
1620                if (!userPKSet.contains(user.getPrimaryKey())) {
1621                    removeUser.remove(pk, user.getPrimaryKey());
1622                }
1623                else {
1624                    userPKSet.remove(user.getPrimaryKey());
1625                }
1626            }
1627
1628            for (Long userPK : userPKSet) {
1629                addUser.add(pk, userPK);
1630            }
1631        }
1632        catch (Exception e) {
1633            throw processException(e);
1634        }
1635        finally {
1636            FinderCacheUtil.clearCache(UserGroupModelImpl.MAPPING_TABLE_USERS_USERGROUPS_NAME);
1637        }
1638    }
1639
1640    public void setUsers(long pk, List<com.liferay.portal.model.User> users)
1641        throws SystemException {
1642        try {
1643            long[] userPKs = new long[users.size()];
1644
1645            for (int i = 0; i < users.size(); i++) {
1646                com.liferay.portal.model.User user = users.get(i);
1647
1648                userPKs[i] = user.getPrimaryKey();
1649            }
1650
1651            setUsers(pk, userPKs);
1652        }
1653        catch (Exception e) {
1654            throw processException(e);
1655        }
1656        finally {
1657            FinderCacheUtil.clearCache(UserGroupModelImpl.MAPPING_TABLE_USERS_USERGROUPS_NAME);
1658        }
1659    }
1660
1661    public void afterPropertiesSet() {
1662        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1663                    com.liferay.portal.util.PropsUtil.get(
1664                        "value.object.listener.com.liferay.portal.model.UserGroup")));
1665
1666        if (listenerClassNames.length > 0) {
1667            try {
1668                List<ModelListener<UserGroup>> listenersList = new ArrayList<ModelListener<UserGroup>>();
1669
1670                for (String listenerClassName : listenerClassNames) {
1671                    listenersList.add((ModelListener<UserGroup>)Class.forName(
1672                            listenerClassName).newInstance());
1673                }
1674
1675                listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1676            }
1677            catch (Exception e) {
1678                _log.error(e);
1679            }
1680        }
1681
1682        containsUser = new ContainsUser(this);
1683
1684        addUser = new AddUser(this);
1685        clearUsers = new ClearUsers(this);
1686        removeUser = new RemoveUser(this);
1687    }
1688
1689    @BeanReference(type = AccountPersistence.class)
1690    protected AccountPersistence accountPersistence;
1691    @BeanReference(type = AddressPersistence.class)
1692    protected AddressPersistence addressPersistence;
1693    @BeanReference(type = BrowserTrackerPersistence.class)
1694    protected BrowserTrackerPersistence browserTrackerPersistence;
1695    @BeanReference(type = ClassNamePersistence.class)
1696    protected ClassNamePersistence classNamePersistence;
1697    @BeanReference(type = CompanyPersistence.class)
1698    protected CompanyPersistence companyPersistence;
1699    @BeanReference(type = ContactPersistence.class)
1700    protected ContactPersistence contactPersistence;
1701    @BeanReference(type = CountryPersistence.class)
1702    protected CountryPersistence countryPersistence;
1703    @BeanReference(type = EmailAddressPersistence.class)
1704    protected EmailAddressPersistence emailAddressPersistence;
1705    @BeanReference(type = GroupPersistence.class)
1706    protected GroupPersistence groupPersistence;
1707    @BeanReference(type = ImagePersistence.class)
1708    protected ImagePersistence imagePersistence;
1709    @BeanReference(type = LayoutPersistence.class)
1710    protected LayoutPersistence layoutPersistence;
1711    @BeanReference(type = LayoutSetPersistence.class)
1712    protected LayoutSetPersistence layoutSetPersistence;
1713    @BeanReference(type = ListTypePersistence.class)
1714    protected ListTypePersistence listTypePersistence;
1715    @BeanReference(type = LockPersistence.class)
1716    protected LockPersistence lockPersistence;
1717    @BeanReference(type = MembershipRequestPersistence.class)
1718    protected MembershipRequestPersistence membershipRequestPersistence;
1719    @BeanReference(type = OrganizationPersistence.class)
1720    protected OrganizationPersistence organizationPersistence;
1721    @BeanReference(type = OrgGroupPermissionPersistence.class)
1722    protected OrgGroupPermissionPersistence orgGroupPermissionPersistence;
1723    @BeanReference(type = OrgGroupRolePersistence.class)
1724    protected OrgGroupRolePersistence orgGroupRolePersistence;
1725    @BeanReference(type = OrgLaborPersistence.class)
1726    protected OrgLaborPersistence orgLaborPersistence;
1727    @BeanReference(type = PasswordPolicyPersistence.class)
1728    protected PasswordPolicyPersistence passwordPolicyPersistence;
1729    @BeanReference(type = PasswordPolicyRelPersistence.class)
1730    protected PasswordPolicyRelPersistence passwordPolicyRelPersistence;
1731    @BeanReference(type = PasswordTrackerPersistence.class)
1732    protected PasswordTrackerPersistence passwordTrackerPersistence;
1733    @BeanReference(type = PermissionPersistence.class)
1734    protected PermissionPersistence permissionPersistence;
1735    @BeanReference(type = PhonePersistence.class)
1736    protected PhonePersistence phonePersistence;
1737    @BeanReference(type = PluginSettingPersistence.class)
1738    protected PluginSettingPersistence pluginSettingPersistence;
1739    @BeanReference(type = PortletPersistence.class)
1740    protected PortletPersistence portletPersistence;
1741    @BeanReference(type = PortletItemPersistence.class)
1742    protected PortletItemPersistence portletItemPersistence;
1743    @BeanReference(type = PortletPreferencesPersistence.class)
1744    protected PortletPreferencesPersistence portletPreferencesPersistence;
1745    @BeanReference(type = RegionPersistence.class)
1746    protected RegionPersistence regionPersistence;
1747    @BeanReference(type = ReleasePersistence.class)
1748    protected ReleasePersistence releasePersistence;
1749    @BeanReference(type = ResourcePersistence.class)
1750    protected ResourcePersistence resourcePersistence;
1751    @BeanReference(type = ResourceActionPersistence.class)
1752    protected ResourceActionPersistence resourceActionPersistence;
1753    @BeanReference(type = ResourceCodePersistence.class)
1754    protected ResourceCodePersistence resourceCodePersistence;
1755    @BeanReference(type = ResourcePermissionPersistence.class)
1756    protected ResourcePermissionPersistence resourcePermissionPersistence;
1757    @BeanReference(type = RolePersistence.class)
1758    protected RolePersistence rolePersistence;
1759    @BeanReference(type = ServiceComponentPersistence.class)
1760    protected ServiceComponentPersistence serviceComponentPersistence;
1761    @BeanReference(type = ShardPersistence.class)
1762    protected ShardPersistence shardPersistence;
1763    @BeanReference(type = SubscriptionPersistence.class)
1764    protected SubscriptionPersistence subscriptionPersistence;
1765    @BeanReference(type = UserPersistence.class)
1766    protected UserPersistence userPersistence;
1767    @BeanReference(type = UserGroupPersistence.class)
1768    protected UserGroupPersistence userGroupPersistence;
1769    @BeanReference(type = UserGroupGroupRolePersistence.class)
1770    protected UserGroupGroupRolePersistence userGroupGroupRolePersistence;
1771    @BeanReference(type = UserGroupRolePersistence.class)
1772    protected UserGroupRolePersistence userGroupRolePersistence;
1773    @BeanReference(type = UserIdMapperPersistence.class)
1774    protected UserIdMapperPersistence userIdMapperPersistence;
1775    @BeanReference(type = UserTrackerPersistence.class)
1776    protected UserTrackerPersistence userTrackerPersistence;
1777    @BeanReference(type = UserTrackerPathPersistence.class)
1778    protected UserTrackerPathPersistence userTrackerPathPersistence;
1779    @BeanReference(type = WebDAVPropsPersistence.class)
1780    protected WebDAVPropsPersistence webDAVPropsPersistence;
1781    @BeanReference(type = WebsitePersistence.class)
1782    protected WebsitePersistence websitePersistence;
1783    protected ContainsUser containsUser;
1784    protected AddUser addUser;
1785    protected ClearUsers clearUsers;
1786    protected RemoveUser removeUser;
1787
1788    protected class ContainsUser {
1789        protected ContainsUser(UserGroupPersistenceImpl persistenceImpl) {
1790            super();
1791
1792            _mappingSqlQuery = MappingSqlQueryFactoryUtil.getMappingSqlQuery(getDataSource(),
1793                    _SQL_CONTAINSUSER,
1794                    new int[] { Types.BIGINT, Types.BIGINT }, RowMapper.COUNT);
1795        }
1796
1797        protected boolean contains(long userGroupId, long userId) {
1798            List<Integer> results = _mappingSqlQuery.execute(new Object[] {
1799                        new Long(userGroupId), new Long(userId)
1800                    });
1801
1802            if (results.size() > 0) {
1803                Integer count = results.get(0);
1804
1805                if (count.intValue() > 0) {
1806                    return true;
1807                }
1808            }
1809
1810            return false;
1811        }
1812
1813        private MappingSqlQuery _mappingSqlQuery;
1814    }
1815
1816    protected class AddUser {
1817        protected AddUser(UserGroupPersistenceImpl persistenceImpl) {
1818            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
1819                    "INSERT INTO Users_UserGroups (userGroupId, userId) VALUES (?, ?)",
1820                    new int[] { Types.BIGINT, Types.BIGINT });
1821            _persistenceImpl = persistenceImpl;
1822        }
1823
1824        protected void add(long userGroupId, long userId)
1825            throws SystemException {
1826            if (!_persistenceImpl.containsUser.contains(userGroupId, userId)) {
1827                ModelListener<com.liferay.portal.model.User>[] userListeners = userPersistence.getListeners();
1828
1829                for (ModelListener<UserGroup> listener : listeners) {
1830                    listener.onBeforeAddAssociation(userGroupId,
1831                        com.liferay.portal.model.User.class.getName(), userId);
1832                }
1833
1834                for (ModelListener<com.liferay.portal.model.User> listener : userListeners) {
1835                    listener.onBeforeAddAssociation(userId,
1836                        UserGroup.class.getName(), userGroupId);
1837                }
1838
1839                _sqlUpdate.update(new Object[] {
1840                        new Long(userGroupId), new Long(userId)
1841                    });
1842
1843                for (ModelListener<UserGroup> listener : listeners) {
1844                    listener.onAfterAddAssociation(userGroupId,
1845                        com.liferay.portal.model.User.class.getName(), userId);
1846                }
1847
1848                for (ModelListener<com.liferay.portal.model.User> listener : userListeners) {
1849                    listener.onAfterAddAssociation(userId,
1850                        UserGroup.class.getName(), userGroupId);
1851                }
1852            }
1853        }
1854
1855        private SqlUpdate _sqlUpdate;
1856        private UserGroupPersistenceImpl _persistenceImpl;
1857    }
1858
1859    protected class ClearUsers {
1860        protected ClearUsers(UserGroupPersistenceImpl persistenceImpl) {
1861            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
1862                    "DELETE FROM Users_UserGroups WHERE userGroupId = ?",
1863                    new int[] { Types.BIGINT });
1864        }
1865
1866        protected void clear(long userGroupId) throws SystemException {
1867            ModelListener<com.liferay.portal.model.User>[] userListeners = userPersistence.getListeners();
1868
1869            List<com.liferay.portal.model.User> users = null;
1870
1871            if ((listeners.length > 0) || (userListeners.length > 0)) {
1872                users = getUsers(userGroupId);
1873
1874                for (com.liferay.portal.model.User user : users) {
1875                    for (ModelListener<UserGroup> listener : listeners) {
1876                        listener.onBeforeRemoveAssociation(userGroupId,
1877                            com.liferay.portal.model.User.class.getName(),
1878                            user.getPrimaryKey());
1879                    }
1880
1881                    for (ModelListener<com.liferay.portal.model.User> listener : userListeners) {
1882                        listener.onBeforeRemoveAssociation(user.getPrimaryKey(),
1883                            UserGroup.class.getName(), userGroupId);
1884                    }
1885                }
1886            }
1887
1888            _sqlUpdate.update(new Object[] { new Long(userGroupId) });
1889
1890            if ((listeners.length > 0) || (userListeners.length > 0)) {
1891                for (com.liferay.portal.model.User user : users) {
1892                    for (ModelListener<UserGroup> listener : listeners) {
1893                        listener.onAfterRemoveAssociation(userGroupId,
1894                            com.liferay.portal.model.User.class.getName(),
1895                            user.getPrimaryKey());
1896                    }
1897
1898                    for (ModelListener<com.liferay.portal.model.User> listener : userListeners) {
1899                        listener.onAfterRemoveAssociation(user.getPrimaryKey(),
1900                            UserGroup.class.getName(), userGroupId);
1901                    }
1902                }
1903            }
1904        }
1905
1906        private SqlUpdate _sqlUpdate;
1907    }
1908
1909    protected class RemoveUser {
1910        protected RemoveUser(UserGroupPersistenceImpl persistenceImpl) {
1911            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
1912                    "DELETE FROM Users_UserGroups WHERE userGroupId = ? AND userId = ?",
1913                    new int[] { Types.BIGINT, Types.BIGINT });
1914            _persistenceImpl = persistenceImpl;
1915        }
1916
1917        protected void remove(long userGroupId, long userId)
1918            throws SystemException {
1919            if (_persistenceImpl.containsUser.contains(userGroupId, userId)) {
1920                ModelListener<com.liferay.portal.model.User>[] userListeners = userPersistence.getListeners();
1921
1922                for (ModelListener<UserGroup> listener : listeners) {
1923                    listener.onBeforeRemoveAssociation(userGroupId,
1924                        com.liferay.portal.model.User.class.getName(), userId);
1925                }
1926
1927                for (ModelListener<com.liferay.portal.model.User> listener : userListeners) {
1928                    listener.onBeforeRemoveAssociation(userId,
1929                        UserGroup.class.getName(), userGroupId);
1930                }
1931
1932                _sqlUpdate.update(new Object[] {
1933                        new Long(userGroupId), new Long(userId)
1934                    });
1935
1936                for (ModelListener<UserGroup> listener : listeners) {
1937                    listener.onAfterRemoveAssociation(userGroupId,
1938                        com.liferay.portal.model.User.class.getName(), userId);
1939                }
1940
1941                for (ModelListener<com.liferay.portal.model.User> listener : userListeners) {
1942                    listener.onAfterRemoveAssociation(userId,
1943                        UserGroup.class.getName(), userGroupId);
1944                }
1945            }
1946        }
1947
1948        private SqlUpdate _sqlUpdate;
1949        private UserGroupPersistenceImpl _persistenceImpl;
1950    }
1951
1952    private static final String _SQL_SELECT_USERGROUP = "SELECT userGroup FROM UserGroup userGroup";
1953    private static final String _SQL_SELECT_USERGROUP_WHERE = "SELECT userGroup FROM UserGroup userGroup WHERE ";
1954    private static final String _SQL_COUNT_USERGROUP = "SELECT COUNT(userGroup) FROM UserGroup userGroup";
1955    private static final String _SQL_COUNT_USERGROUP_WHERE = "SELECT COUNT(userGroup) FROM UserGroup userGroup WHERE ";
1956    private static final String _SQL_GETUSERS = "SELECT {User_.*} FROM User_ INNER JOIN Users_UserGroups ON (Users_UserGroups.userId = User_.userId) WHERE (Users_UserGroups.userGroupId = ?)";
1957    private static final String _SQL_GETUSERSSIZE = "SELECT COUNT(*) AS COUNT_VALUE FROM Users_UserGroups WHERE userGroupId = ?";
1958    private static final String _SQL_CONTAINSUSER = "SELECT COUNT(*) AS COUNT_VALUE FROM Users_UserGroups WHERE userGroupId = ? AND userId = ?";
1959    private static final String _FINDER_COLUMN_COMPANYID_COMPANYID_2 = "userGroup.companyId = ?";
1960    private static final String _FINDER_COLUMN_C_P_COMPANYID_2 = "userGroup.companyId = ? AND ";
1961    private static final String _FINDER_COLUMN_C_P_PARENTUSERGROUPID_2 = "userGroup.parentUserGroupId = ?";
1962    private static final String _FINDER_COLUMN_C_N_COMPANYID_2 = "userGroup.companyId = ? AND ";
1963    private static final String _FINDER_COLUMN_C_N_NAME_1 = "userGroup.name IS NULL";
1964    private static final String _FINDER_COLUMN_C_N_NAME_2 = "userGroup.name = ?";
1965    private static final String _FINDER_COLUMN_C_N_NAME_3 = "(userGroup.name IS NULL OR userGroup.name = ?)";
1966    private static final String _ORDER_BY_ENTITY_ALIAS = "userGroup.";
1967    private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No UserGroup exists with the primary key ";
1968    private static final String _NO_SUCH_ENTITY_WITH_KEY = "No UserGroup exists with the key {";
1969    private static Log _log = LogFactoryUtil.getLog(UserGroupPersistenceImpl.class);
1970}