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.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     public UserGroup updateImpl(com.liferay.portal.model.UserGroup userGroup,
268         boolean merge) throws SystemException {
269         userGroup = toUnwrappedModel(userGroup);
270 
271         boolean isNew = userGroup.isNew();
272 
273         UserGroupModelImpl userGroupModelImpl = (UserGroupModelImpl)userGroup;
274 
275         Session session = null;
276 
277         try {
278             session = openSession();
279 
280             BatchSessionUtil.update(session, userGroup, merge);
281 
282             userGroup.setNew(false);
283         }
284         catch (Exception e) {
285             throw processException(e);
286         }
287         finally {
288             closeSession(session);
289         }
290 
291         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
292 
293         EntityCacheUtil.putResult(UserGroupModelImpl.ENTITY_CACHE_ENABLED,
294             UserGroupImpl.class, userGroup.getPrimaryKey(), userGroup);
295 
296         if (!isNew &&
297                 ((userGroup.getCompanyId() != userGroupModelImpl.getOriginalCompanyId()) ||
298                 !Validator.equals(userGroup.getName(),
299                     userGroupModelImpl.getOriginalName()))) {
300             FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_N,
301                 new Object[] {
302                     new Long(userGroupModelImpl.getOriginalCompanyId()),
303                     
304                 userGroupModelImpl.getOriginalName()
305                 });
306         }
307 
308         if (isNew ||
309                 ((userGroup.getCompanyId() != userGroupModelImpl.getOriginalCompanyId()) ||
310                 !Validator.equals(userGroup.getName(),
311                     userGroupModelImpl.getOriginalName()))) {
312             FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_N,
313                 new Object[] {
314                     new Long(userGroup.getCompanyId()),
315                     
316                 userGroup.getName()
317                 }, userGroup);
318         }
319 
320         return userGroup;
321     }
322 
323     protected UserGroup toUnwrappedModel(UserGroup userGroup) {
324         if (userGroup instanceof UserGroupImpl) {
325             return userGroup;
326         }
327 
328         UserGroupImpl userGroupImpl = new UserGroupImpl();
329 
330         userGroupImpl.setNew(userGroup.isNew());
331         userGroupImpl.setPrimaryKey(userGroup.getPrimaryKey());
332 
333         userGroupImpl.setUserGroupId(userGroup.getUserGroupId());
334         userGroupImpl.setCompanyId(userGroup.getCompanyId());
335         userGroupImpl.setParentUserGroupId(userGroup.getParentUserGroupId());
336         userGroupImpl.setName(userGroup.getName());
337         userGroupImpl.setDescription(userGroup.getDescription());
338 
339         return userGroupImpl;
340     }
341 
342     public UserGroup findByPrimaryKey(Serializable primaryKey)
343         throws NoSuchModelException, SystemException {
344         return findByPrimaryKey(((Long)primaryKey).longValue());
345     }
346 
347     public UserGroup findByPrimaryKey(long userGroupId)
348         throws NoSuchUserGroupException, SystemException {
349         UserGroup userGroup = fetchByPrimaryKey(userGroupId);
350 
351         if (userGroup == null) {
352             if (_log.isWarnEnabled()) {
353                 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + userGroupId);
354             }
355 
356             throw new NoSuchUserGroupException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
357                 userGroupId);
358         }
359 
360         return userGroup;
361     }
362 
363     public UserGroup fetchByPrimaryKey(Serializable primaryKey)
364         throws SystemException {
365         return fetchByPrimaryKey(((Long)primaryKey).longValue());
366     }
367 
368     public UserGroup fetchByPrimaryKey(long userGroupId)
369         throws SystemException {
370         UserGroup userGroup = (UserGroup)EntityCacheUtil.getResult(UserGroupModelImpl.ENTITY_CACHE_ENABLED,
371                 UserGroupImpl.class, userGroupId, this);
372 
373         if (userGroup == null) {
374             Session session = null;
375 
376             try {
377                 session = openSession();
378 
379                 userGroup = (UserGroup)session.get(UserGroupImpl.class,
380                         new Long(userGroupId));
381             }
382             catch (Exception e) {
383                 throw processException(e);
384             }
385             finally {
386                 if (userGroup != null) {
387                     cacheResult(userGroup);
388                 }
389 
390                 closeSession(session);
391             }
392         }
393 
394         return userGroup;
395     }
396 
397     public List<UserGroup> findByCompanyId(long companyId)
398         throws SystemException {
399         Object[] finderArgs = new Object[] { new Long(companyId) };
400 
401         List<UserGroup> list = (List<UserGroup>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_COMPANYID,
402                 finderArgs, this);
403 
404         if (list == null) {
405             Session session = null;
406 
407             try {
408                 session = openSession();
409 
410                 StringBundler query = new StringBundler(3);
411 
412                 query.append(_SQL_SELECT_USERGROUP_WHERE);
413 
414                 query.append(_FINDER_COLUMN_COMPANYID_COMPANYID_2);
415 
416                 query.append(UserGroupModelImpl.ORDER_BY_JPQL);
417 
418                 String sql = query.toString();
419 
420                 Query q = session.createQuery(sql);
421 
422                 QueryPos qPos = QueryPos.getInstance(q);
423 
424                 qPos.add(companyId);
425 
426                 list = q.list();
427             }
428             catch (Exception e) {
429                 throw processException(e);
430             }
431             finally {
432                 if (list == null) {
433                     list = new ArrayList<UserGroup>();
434                 }
435 
436                 cacheResult(list);
437 
438                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_COMPANYID,
439                     finderArgs, list);
440 
441                 closeSession(session);
442             }
443         }
444 
445         return list;
446     }
447 
448     public List<UserGroup> findByCompanyId(long companyId, int start, int end)
449         throws SystemException {
450         return findByCompanyId(companyId, start, end, null);
451     }
452 
453     public List<UserGroup> findByCompanyId(long companyId, int start, int end,
454         OrderByComparator orderByComparator) throws SystemException {
455         Object[] finderArgs = new Object[] {
456                 new Long(companyId),
457                 
458                 String.valueOf(start), String.valueOf(end),
459                 String.valueOf(orderByComparator)
460             };
461 
462         List<UserGroup> list = (List<UserGroup>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_OBC_COMPANYID,
463                 finderArgs, this);
464 
465         if (list == null) {
466             Session session = null;
467 
468             try {
469                 session = openSession();
470 
471                 StringBundler query = null;
472 
473                 if (orderByComparator != null) {
474                     query = new StringBundler(3 +
475                             (orderByComparator.getOrderByFields().length * 3));
476                 }
477                 else {
478                     query = new StringBundler(3);
479                 }
480 
481                 query.append(_SQL_SELECT_USERGROUP_WHERE);
482 
483                 query.append(_FINDER_COLUMN_COMPANYID_COMPANYID_2);
484 
485                 if (orderByComparator != null) {
486                     appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
487                         orderByComparator);
488                 }
489 
490                 else {
491                     query.append(UserGroupModelImpl.ORDER_BY_JPQL);
492                 }
493 
494                 String sql = query.toString();
495 
496                 Query q = session.createQuery(sql);
497 
498                 QueryPos qPos = QueryPos.getInstance(q);
499 
500                 qPos.add(companyId);
501 
502                 list = (List<UserGroup>)QueryUtil.list(q, getDialect(), start,
503                         end);
504             }
505             catch (Exception e) {
506                 throw processException(e);
507             }
508             finally {
509                 if (list == null) {
510                     list = new ArrayList<UserGroup>();
511                 }
512 
513                 cacheResult(list);
514 
515                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_OBC_COMPANYID,
516                     finderArgs, list);
517 
518                 closeSession(session);
519             }
520         }
521 
522         return list;
523     }
524 
525     public UserGroup findByCompanyId_First(long companyId,
526         OrderByComparator orderByComparator)
527         throws NoSuchUserGroupException, SystemException {
528         List<UserGroup> list = findByCompanyId(companyId, 0, 1,
529                 orderByComparator);
530 
531         if (list.isEmpty()) {
532             StringBundler msg = new StringBundler(4);
533 
534             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
535 
536             msg.append("companyId=");
537             msg.append(companyId);
538 
539             msg.append(StringPool.CLOSE_CURLY_BRACE);
540 
541             throw new NoSuchUserGroupException(msg.toString());
542         }
543         else {
544             return list.get(0);
545         }
546     }
547 
548     public UserGroup findByCompanyId_Last(long companyId,
549         OrderByComparator orderByComparator)
550         throws NoSuchUserGroupException, SystemException {
551         int count = countByCompanyId(companyId);
552 
553         List<UserGroup> list = findByCompanyId(companyId, count - 1, count,
554                 orderByComparator);
555 
556         if (list.isEmpty()) {
557             StringBundler msg = new StringBundler(4);
558 
559             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
560 
561             msg.append("companyId=");
562             msg.append(companyId);
563 
564             msg.append(StringPool.CLOSE_CURLY_BRACE);
565 
566             throw new NoSuchUserGroupException(msg.toString());
567         }
568         else {
569             return list.get(0);
570         }
571     }
572 
573     public UserGroup[] findByCompanyId_PrevAndNext(long userGroupId,
574         long companyId, OrderByComparator orderByComparator)
575         throws NoSuchUserGroupException, SystemException {
576         UserGroup userGroup = findByPrimaryKey(userGroupId);
577 
578         int count = countByCompanyId(companyId);
579 
580         Session session = null;
581 
582         try {
583             session = openSession();
584 
585             StringBundler query = null;
586 
587             if (orderByComparator != null) {
588                 query = new StringBundler(3 +
589                         (orderByComparator.getOrderByFields().length * 3));
590             }
591             else {
592                 query = new StringBundler(3);
593             }
594 
595             query.append(_SQL_SELECT_USERGROUP_WHERE);
596 
597             query.append(_FINDER_COLUMN_COMPANYID_COMPANYID_2);
598 
599             if (orderByComparator != null) {
600                 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
601                     orderByComparator);
602             }
603 
604             else {
605                 query.append(UserGroupModelImpl.ORDER_BY_JPQL);
606             }
607 
608             String sql = query.toString();
609 
610             Query q = session.createQuery(sql);
611 
612             QueryPos qPos = QueryPos.getInstance(q);
613 
614             qPos.add(companyId);
615 
616             Object[] objArray = QueryUtil.getPrevAndNext(q, count,
617                     orderByComparator, userGroup);
618 
619             UserGroup[] array = new UserGroupImpl[3];
620 
621             array[0] = (UserGroup)objArray[0];
622             array[1] = (UserGroup)objArray[1];
623             array[2] = (UserGroup)objArray[2];
624 
625             return array;
626         }
627         catch (Exception e) {
628             throw processException(e);
629         }
630         finally {
631             closeSession(session);
632         }
633     }
634 
635     public List<UserGroup> findByC_P(long companyId, long parentUserGroupId)
636         throws SystemException {
637         Object[] finderArgs = new Object[] {
638                 new Long(companyId), new Long(parentUserGroupId)
639             };
640 
641         List<UserGroup> list = (List<UserGroup>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_C_P,
642                 finderArgs, this);
643 
644         if (list == null) {
645             Session session = null;
646 
647             try {
648                 session = openSession();
649 
650                 StringBundler query = new StringBundler(4);
651 
652                 query.append(_SQL_SELECT_USERGROUP_WHERE);
653 
654                 query.append(_FINDER_COLUMN_C_P_COMPANYID_2);
655 
656                 query.append(_FINDER_COLUMN_C_P_PARENTUSERGROUPID_2);
657 
658                 query.append(UserGroupModelImpl.ORDER_BY_JPQL);
659 
660                 String sql = query.toString();
661 
662                 Query q = session.createQuery(sql);
663 
664                 QueryPos qPos = QueryPos.getInstance(q);
665 
666                 qPos.add(companyId);
667 
668                 qPos.add(parentUserGroupId);
669 
670                 list = q.list();
671             }
672             catch (Exception e) {
673                 throw processException(e);
674             }
675             finally {
676                 if (list == null) {
677                     list = new ArrayList<UserGroup>();
678                 }
679 
680                 cacheResult(list);
681 
682                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_C_P, finderArgs,
683                     list);
684 
685                 closeSession(session);
686             }
687         }
688 
689         return list;
690     }
691 
692     public List<UserGroup> findByC_P(long companyId, long parentUserGroupId,
693         int start, int end) throws SystemException {
694         return findByC_P(companyId, parentUserGroupId, start, end, null);
695     }
696 
697     public List<UserGroup> findByC_P(long companyId, long parentUserGroupId,
698         int start, int end, OrderByComparator orderByComparator)
699         throws SystemException {
700         Object[] finderArgs = new Object[] {
701                 new Long(companyId), new Long(parentUserGroupId),
702                 
703                 String.valueOf(start), String.valueOf(end),
704                 String.valueOf(orderByComparator)
705             };
706 
707         List<UserGroup> list = (List<UserGroup>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_OBC_C_P,
708                 finderArgs, this);
709 
710         if (list == null) {
711             Session session = null;
712 
713             try {
714                 session = openSession();
715 
716                 StringBundler query = null;
717 
718                 if (orderByComparator != null) {
719                     query = new StringBundler(4 +
720                             (orderByComparator.getOrderByFields().length * 3));
721                 }
722                 else {
723                     query = new StringBundler(4);
724                 }
725 
726                 query.append(_SQL_SELECT_USERGROUP_WHERE);
727 
728                 query.append(_FINDER_COLUMN_C_P_COMPANYID_2);
729 
730                 query.append(_FINDER_COLUMN_C_P_PARENTUSERGROUPID_2);
731 
732                 if (orderByComparator != null) {
733                     appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
734                         orderByComparator);
735                 }
736 
737                 else {
738                     query.append(UserGroupModelImpl.ORDER_BY_JPQL);
739                 }
740 
741                 String sql = query.toString();
742 
743                 Query q = session.createQuery(sql);
744 
745                 QueryPos qPos = QueryPos.getInstance(q);
746 
747                 qPos.add(companyId);
748 
749                 qPos.add(parentUserGroupId);
750 
751                 list = (List<UserGroup>)QueryUtil.list(q, getDialect(), start,
752                         end);
753             }
754             catch (Exception e) {
755                 throw processException(e);
756             }
757             finally {
758                 if (list == null) {
759                     list = new ArrayList<UserGroup>();
760                 }
761 
762                 cacheResult(list);
763 
764                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_OBC_C_P,
765                     finderArgs, list);
766 
767                 closeSession(session);
768             }
769         }
770 
771         return list;
772     }
773 
774     public UserGroup findByC_P_First(long companyId, long parentUserGroupId,
775         OrderByComparator orderByComparator)
776         throws NoSuchUserGroupException, SystemException {
777         List<UserGroup> list = findByC_P(companyId, parentUserGroupId, 0, 1,
778                 orderByComparator);
779 
780         if (list.isEmpty()) {
781             StringBundler msg = new StringBundler(6);
782 
783             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
784 
785             msg.append("companyId=");
786             msg.append(companyId);
787 
788             msg.append(", parentUserGroupId=");
789             msg.append(parentUserGroupId);
790 
791             msg.append(StringPool.CLOSE_CURLY_BRACE);
792 
793             throw new NoSuchUserGroupException(msg.toString());
794         }
795         else {
796             return list.get(0);
797         }
798     }
799 
800     public UserGroup findByC_P_Last(long companyId, long parentUserGroupId,
801         OrderByComparator orderByComparator)
802         throws NoSuchUserGroupException, SystemException {
803         int count = countByC_P(companyId, parentUserGroupId);
804 
805         List<UserGroup> list = findByC_P(companyId, parentUserGroupId,
806                 count - 1, count, orderByComparator);
807 
808         if (list.isEmpty()) {
809             StringBundler msg = new StringBundler(6);
810 
811             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
812 
813             msg.append("companyId=");
814             msg.append(companyId);
815 
816             msg.append(", parentUserGroupId=");
817             msg.append(parentUserGroupId);
818 
819             msg.append(StringPool.CLOSE_CURLY_BRACE);
820 
821             throw new NoSuchUserGroupException(msg.toString());
822         }
823         else {
824             return list.get(0);
825         }
826     }
827 
828     public UserGroup[] findByC_P_PrevAndNext(long userGroupId, long companyId,
829         long parentUserGroupId, OrderByComparator orderByComparator)
830         throws NoSuchUserGroupException, SystemException {
831         UserGroup userGroup = findByPrimaryKey(userGroupId);
832 
833         int count = countByC_P(companyId, parentUserGroupId);
834 
835         Session session = null;
836 
837         try {
838             session = openSession();
839 
840             StringBundler query = null;
841 
842             if (orderByComparator != null) {
843                 query = new StringBundler(4 +
844                         (orderByComparator.getOrderByFields().length * 3));
845             }
846             else {
847                 query = new StringBundler(4);
848             }
849 
850             query.append(_SQL_SELECT_USERGROUP_WHERE);
851 
852             query.append(_FINDER_COLUMN_C_P_COMPANYID_2);
853 
854             query.append(_FINDER_COLUMN_C_P_PARENTUSERGROUPID_2);
855 
856             if (orderByComparator != null) {
857                 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
858                     orderByComparator);
859             }
860 
861             else {
862                 query.append(UserGroupModelImpl.ORDER_BY_JPQL);
863             }
864 
865             String sql = query.toString();
866 
867             Query q = session.createQuery(sql);
868 
869             QueryPos qPos = QueryPos.getInstance(q);
870 
871             qPos.add(companyId);
872 
873             qPos.add(parentUserGroupId);
874 
875             Object[] objArray = QueryUtil.getPrevAndNext(q, count,
876                     orderByComparator, userGroup);
877 
878             UserGroup[] array = new UserGroupImpl[3];
879 
880             array[0] = (UserGroup)objArray[0];
881             array[1] = (UserGroup)objArray[1];
882             array[2] = (UserGroup)objArray[2];
883 
884             return array;
885         }
886         catch (Exception e) {
887             throw processException(e);
888         }
889         finally {
890             closeSession(session);
891         }
892     }
893 
894     public UserGroup findByC_N(long companyId, String name)
895         throws NoSuchUserGroupException, SystemException {
896         UserGroup userGroup = fetchByC_N(companyId, name);
897 
898         if (userGroup == null) {
899             StringBundler msg = new StringBundler(6);
900 
901             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
902 
903             msg.append("companyId=");
904             msg.append(companyId);
905 
906             msg.append(", name=");
907             msg.append(name);
908 
909             msg.append(StringPool.CLOSE_CURLY_BRACE);
910 
911             if (_log.isWarnEnabled()) {
912                 _log.warn(msg.toString());
913             }
914 
915             throw new NoSuchUserGroupException(msg.toString());
916         }
917 
918         return userGroup;
919     }
920 
921     public UserGroup fetchByC_N(long companyId, String name)
922         throws SystemException {
923         return fetchByC_N(companyId, name, true);
924     }
925 
926     public UserGroup fetchByC_N(long companyId, String name,
927         boolean retrieveFromCache) throws SystemException {
928         Object[] finderArgs = new Object[] { new Long(companyId), name };
929 
930         Object result = null;
931 
932         if (retrieveFromCache) {
933             result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_C_N,
934                     finderArgs, this);
935         }
936 
937         if (result == null) {
938             Session session = null;
939 
940             try {
941                 session = openSession();
942 
943                 StringBundler query = new StringBundler(4);
944 
945                 query.append(_SQL_SELECT_USERGROUP_WHERE);
946 
947                 query.append(_FINDER_COLUMN_C_N_COMPANYID_2);
948 
949                 if (name == null) {
950                     query.append(_FINDER_COLUMN_C_N_NAME_1);
951                 }
952                 else {
953                     if (name.equals(StringPool.BLANK)) {
954                         query.append(_FINDER_COLUMN_C_N_NAME_3);
955                     }
956                     else {
957                         query.append(_FINDER_COLUMN_C_N_NAME_2);
958                     }
959                 }
960 
961                 query.append(UserGroupModelImpl.ORDER_BY_JPQL);
962 
963                 String sql = query.toString();
964 
965                 Query q = session.createQuery(sql);
966 
967                 QueryPos qPos = QueryPos.getInstance(q);
968 
969                 qPos.add(companyId);
970 
971                 if (name != null) {
972                     qPos.add(name);
973                 }
974 
975                 List<UserGroup> list = q.list();
976 
977                 result = list;
978 
979                 UserGroup userGroup = null;
980 
981                 if (list.isEmpty()) {
982                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_N,
983                         finderArgs, list);
984                 }
985                 else {
986                     userGroup = list.get(0);
987 
988                     cacheResult(userGroup);
989 
990                     if ((userGroup.getCompanyId() != companyId) ||
991                             (userGroup.getName() == null) ||
992                             !userGroup.getName().equals(name)) {
993                         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_N,
994                             finderArgs, userGroup);
995                     }
996                 }
997 
998                 return userGroup;
999             }
1000            catch (Exception e) {
1001                throw processException(e);
1002            }
1003            finally {
1004                if (result == null) {
1005                    FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_N,
1006                        finderArgs, new ArrayList<UserGroup>());
1007                }
1008
1009                closeSession(session);
1010            }
1011        }
1012        else {
1013            if (result instanceof List<?>) {
1014                return null;
1015            }
1016            else {
1017                return (UserGroup)result;
1018            }
1019        }
1020    }
1021
1022    public List<UserGroup> findAll() throws SystemException {
1023        return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
1024    }
1025
1026    public List<UserGroup> findAll(int start, int end)
1027        throws SystemException {
1028        return findAll(start, end, null);
1029    }
1030
1031    public List<UserGroup> findAll(int start, int end,
1032        OrderByComparator orderByComparator) throws SystemException {
1033        Object[] finderArgs = new Object[] {
1034                String.valueOf(start), String.valueOf(end),
1035                String.valueOf(orderByComparator)
1036            };
1037
1038        List<UserGroup> list = (List<UserGroup>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
1039                finderArgs, this);
1040
1041        if (list == null) {
1042            Session session = null;
1043
1044            try {
1045                session = openSession();
1046
1047                StringBundler query = null;
1048                String sql = null;
1049
1050                if (orderByComparator != null) {
1051                    query = new StringBundler(2 +
1052                            (orderByComparator.getOrderByFields().length * 3));
1053
1054                    query.append(_SQL_SELECT_USERGROUP);
1055
1056                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1057                        orderByComparator);
1058
1059                    sql = query.toString();
1060                }
1061
1062                else {
1063                    sql = _SQL_SELECT_USERGROUP.concat(UserGroupModelImpl.ORDER_BY_JPQL);
1064                }
1065
1066                Query q = session.createQuery(sql);
1067
1068                if (orderByComparator == null) {
1069                    list = (List<UserGroup>)QueryUtil.list(q, getDialect(),
1070                            start, end, false);
1071
1072                    Collections.sort(list);
1073                }
1074                else {
1075                    list = (List<UserGroup>)QueryUtil.list(q, getDialect(),
1076                            start, end);
1077                }
1078            }
1079            catch (Exception e) {
1080                throw processException(e);
1081            }
1082            finally {
1083                if (list == null) {
1084                    list = new ArrayList<UserGroup>();
1085                }
1086
1087                cacheResult(list);
1088
1089                FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
1090
1091                closeSession(session);
1092            }
1093        }
1094
1095        return list;
1096    }
1097
1098    public void removeByCompanyId(long companyId) throws SystemException {
1099        for (UserGroup userGroup : findByCompanyId(companyId)) {
1100            remove(userGroup);
1101        }
1102    }
1103
1104    public void removeByC_P(long companyId, long parentUserGroupId)
1105        throws SystemException {
1106        for (UserGroup userGroup : findByC_P(companyId, parentUserGroupId)) {
1107            remove(userGroup);
1108        }
1109    }
1110
1111    public void removeByC_N(long companyId, String name)
1112        throws NoSuchUserGroupException, SystemException {
1113        UserGroup userGroup = findByC_N(companyId, name);
1114
1115        remove(userGroup);
1116    }
1117
1118    public void removeAll() throws SystemException {
1119        for (UserGroup userGroup : findAll()) {
1120            remove(userGroup);
1121        }
1122    }
1123
1124    public int countByCompanyId(long companyId) throws SystemException {
1125        Object[] finderArgs = new Object[] { new Long(companyId) };
1126
1127        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_COMPANYID,
1128                finderArgs, this);
1129
1130        if (count == null) {
1131            Session session = null;
1132
1133            try {
1134                session = openSession();
1135
1136                StringBundler query = new StringBundler(2);
1137
1138                query.append(_SQL_COUNT_USERGROUP_WHERE);
1139
1140                query.append(_FINDER_COLUMN_COMPANYID_COMPANYID_2);
1141
1142                String sql = query.toString();
1143
1144                Query q = session.createQuery(sql);
1145
1146                QueryPos qPos = QueryPos.getInstance(q);
1147
1148                qPos.add(companyId);
1149
1150                count = (Long)q.uniqueResult();
1151            }
1152            catch (Exception e) {
1153                throw processException(e);
1154            }
1155            finally {
1156                if (count == null) {
1157                    count = Long.valueOf(0);
1158                }
1159
1160                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_COMPANYID,
1161                    finderArgs, count);
1162
1163                closeSession(session);
1164            }
1165        }
1166
1167        return count.intValue();
1168    }
1169
1170    public int countByC_P(long companyId, long parentUserGroupId)
1171        throws SystemException {
1172        Object[] finderArgs = new Object[] {
1173                new Long(companyId), new Long(parentUserGroupId)
1174            };
1175
1176        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_C_P,
1177                finderArgs, this);
1178
1179        if (count == null) {
1180            Session session = null;
1181
1182            try {
1183                session = openSession();
1184
1185                StringBundler query = new StringBundler(3);
1186
1187                query.append(_SQL_COUNT_USERGROUP_WHERE);
1188
1189                query.append(_FINDER_COLUMN_C_P_COMPANYID_2);
1190
1191                query.append(_FINDER_COLUMN_C_P_PARENTUSERGROUPID_2);
1192
1193                String sql = query.toString();
1194
1195                Query q = session.createQuery(sql);
1196
1197                QueryPos qPos = QueryPos.getInstance(q);
1198
1199                qPos.add(companyId);
1200
1201                qPos.add(parentUserGroupId);
1202
1203                count = (Long)q.uniqueResult();
1204            }
1205            catch (Exception e) {
1206                throw processException(e);
1207            }
1208            finally {
1209                if (count == null) {
1210                    count = Long.valueOf(0);
1211                }
1212
1213                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_P, finderArgs,
1214                    count);
1215
1216                closeSession(session);
1217            }
1218        }
1219
1220        return count.intValue();
1221    }
1222
1223    public int countByC_N(long companyId, String name)
1224        throws SystemException {
1225        Object[] finderArgs = new Object[] { new Long(companyId), name };
1226
1227        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_C_N,
1228                finderArgs, this);
1229
1230        if (count == null) {
1231            Session session = null;
1232
1233            try {
1234                session = openSession();
1235
1236                StringBundler query = new StringBundler(3);
1237
1238                query.append(_SQL_COUNT_USERGROUP_WHERE);
1239
1240                query.append(_FINDER_COLUMN_C_N_COMPANYID_2);
1241
1242                if (name == null) {
1243                    query.append(_FINDER_COLUMN_C_N_NAME_1);
1244                }
1245                else {
1246                    if (name.equals(StringPool.BLANK)) {
1247                        query.append(_FINDER_COLUMN_C_N_NAME_3);
1248                    }
1249                    else {
1250                        query.append(_FINDER_COLUMN_C_N_NAME_2);
1251                    }
1252                }
1253
1254                String sql = query.toString();
1255
1256                Query q = session.createQuery(sql);
1257
1258                QueryPos qPos = QueryPos.getInstance(q);
1259
1260                qPos.add(companyId);
1261
1262                if (name != null) {
1263                    qPos.add(name);
1264                }
1265
1266                count = (Long)q.uniqueResult();
1267            }
1268            catch (Exception e) {
1269                throw processException(e);
1270            }
1271            finally {
1272                if (count == null) {
1273                    count = Long.valueOf(0);
1274                }
1275
1276                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_N, finderArgs,
1277                    count);
1278
1279                closeSession(session);
1280            }
1281        }
1282
1283        return count.intValue();
1284    }
1285
1286    public int countAll() throws SystemException {
1287        Object[] finderArgs = new Object[0];
1288
1289        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1290                finderArgs, this);
1291
1292        if (count == null) {
1293            Session session = null;
1294
1295            try {
1296                session = openSession();
1297
1298                Query q = session.createQuery(_SQL_COUNT_USERGROUP);
1299
1300                count = (Long)q.uniqueResult();
1301            }
1302            catch (Exception e) {
1303                throw processException(e);
1304            }
1305            finally {
1306                if (count == null) {
1307                    count = Long.valueOf(0);
1308                }
1309
1310                FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
1311                    count);
1312
1313                closeSession(session);
1314            }
1315        }
1316
1317        return count.intValue();
1318    }
1319
1320    public List<com.liferay.portal.model.User> getUsers(long pk)
1321        throws SystemException {
1322        return getUsers(pk, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
1323    }
1324
1325    public List<com.liferay.portal.model.User> getUsers(long pk, int start,
1326        int end) throws SystemException {
1327        return getUsers(pk, start, end, null);
1328    }
1329
1330    public static final FinderPath FINDER_PATH_GET_USERS = new FinderPath(com.liferay.portal.model.impl.UserModelImpl.ENTITY_CACHE_ENABLED,
1331            UserGroupModelImpl.FINDER_CACHE_ENABLED_USERS_USERGROUPS,
1332            UserGroupModelImpl.MAPPING_TABLE_USERS_USERGROUPS_NAME, "getUsers",
1333            new String[] {
1334                Long.class.getName(), "java.lang.Integer", "java.lang.Integer",
1335                "com.liferay.portal.kernel.util.OrderByComparator"
1336            });
1337
1338    public List<com.liferay.portal.model.User> getUsers(long pk, int start,
1339        int end, OrderByComparator orderByComparator) throws SystemException {
1340        Object[] finderArgs = new Object[] {
1341                new Long(pk), String.valueOf(start), String.valueOf(end),
1342                String.valueOf(orderByComparator)
1343            };
1344
1345        List<com.liferay.portal.model.User> list = (List<com.liferay.portal.model.User>)FinderCacheUtil.getResult(FINDER_PATH_GET_USERS,
1346                finderArgs, this);
1347
1348        if (list == null) {
1349            Session session = null;
1350
1351            try {
1352                session = openSession();
1353
1354                String sql = null;
1355
1356                if (orderByComparator != null) {
1357                    sql = _SQL_GETUSERS.concat(ORDER_BY_CLAUSE)
1358                                       .concat(orderByComparator.getOrderBy());
1359                }
1360
1361                sql = _SQL_GETUSERS;
1362
1363                SQLQuery q = session.createSQLQuery(sql);
1364
1365                q.addEntity("User_",
1366                    com.liferay.portal.model.impl.UserImpl.class);
1367
1368                QueryPos qPos = QueryPos.getInstance(q);
1369
1370                qPos.add(pk);
1371
1372                list = (List<com.liferay.portal.model.User>)QueryUtil.list(q,
1373                        getDialect(), start, end);
1374            }
1375            catch (Exception e) {
1376                throw processException(e);
1377            }
1378            finally {
1379                if (list == null) {
1380                    list = new ArrayList<com.liferay.portal.model.User>();
1381                }
1382
1383                userPersistence.cacheResult(list);
1384
1385                FinderCacheUtil.putResult(FINDER_PATH_GET_USERS, finderArgs,
1386                    list);
1387
1388                closeSession(session);
1389            }
1390        }
1391
1392        return list;
1393    }
1394
1395    public static final FinderPath FINDER_PATH_GET_USERS_SIZE = new FinderPath(com.liferay.portal.model.impl.UserModelImpl.ENTITY_CACHE_ENABLED,
1396            UserGroupModelImpl.FINDER_CACHE_ENABLED_USERS_USERGROUPS,
1397            UserGroupModelImpl.MAPPING_TABLE_USERS_USERGROUPS_NAME,
1398            "getUsersSize", new String[] { Long.class.getName() });
1399
1400    public int getUsersSize(long pk) throws SystemException {
1401        Object[] finderArgs = new Object[] { new Long(pk) };
1402
1403        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_GET_USERS_SIZE,
1404                finderArgs, this);
1405
1406        if (count == null) {
1407            Session session = null;
1408
1409            try {
1410                session = openSession();
1411
1412                SQLQuery q = session.createSQLQuery(_SQL_GETUSERSSIZE);
1413
1414                q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
1415
1416                QueryPos qPos = QueryPos.getInstance(q);
1417
1418                qPos.add(pk);
1419
1420                count = (Long)q.uniqueResult();
1421            }
1422            catch (Exception e) {
1423                throw processException(e);
1424            }
1425            finally {
1426                if (count == null) {
1427                    count = Long.valueOf(0);
1428                }
1429
1430                FinderCacheUtil.putResult(FINDER_PATH_GET_USERS_SIZE,
1431                    finderArgs, count);
1432
1433                closeSession(session);
1434            }
1435        }
1436
1437        return count.intValue();
1438    }
1439
1440    public static final FinderPath FINDER_PATH_CONTAINS_USER = new FinderPath(com.liferay.portal.model.impl.UserModelImpl.ENTITY_CACHE_ENABLED,
1441            UserGroupModelImpl.FINDER_CACHE_ENABLED_USERS_USERGROUPS,
1442            UserGroupModelImpl.MAPPING_TABLE_USERS_USERGROUPS_NAME,
1443            "containsUser",
1444            new String[] { Long.class.getName(), Long.class.getName() });
1445
1446    public boolean containsUser(long pk, long userPK) throws SystemException {
1447        Object[] finderArgs = new Object[] { new Long(pk), new Long(userPK) };
1448
1449        Boolean value = (Boolean)FinderCacheUtil.getResult(FINDER_PATH_CONTAINS_USER,
1450                finderArgs, this);
1451
1452        if (value == null) {
1453            try {
1454                value = Boolean.valueOf(containsUser.contains(pk, userPK));
1455            }
1456            catch (Exception e) {
1457                throw processException(e);
1458            }
1459            finally {
1460                if (value == null) {
1461                    value = Boolean.FALSE;
1462                }
1463
1464                FinderCacheUtil.putResult(FINDER_PATH_CONTAINS_USER,
1465                    finderArgs, value);
1466            }
1467        }
1468
1469        return value.booleanValue();
1470    }
1471
1472    public boolean containsUsers(long pk) throws SystemException {
1473        if (getUsersSize(pk) > 0) {
1474            return true;
1475        }
1476        else {
1477            return false;
1478        }
1479    }
1480
1481    public void addUser(long pk, long userPK) throws SystemException {
1482        try {
1483            addUser.add(pk, userPK);
1484        }
1485        catch (Exception e) {
1486            throw processException(e);
1487        }
1488        finally {
1489            FinderCacheUtil.clearCache(UserGroupModelImpl.MAPPING_TABLE_USERS_USERGROUPS_NAME);
1490        }
1491    }
1492
1493    public void addUser(long pk, com.liferay.portal.model.User user)
1494        throws SystemException {
1495        try {
1496            addUser.add(pk, user.getPrimaryKey());
1497        }
1498        catch (Exception e) {
1499            throw processException(e);
1500        }
1501        finally {
1502            FinderCacheUtil.clearCache(UserGroupModelImpl.MAPPING_TABLE_USERS_USERGROUPS_NAME);
1503        }
1504    }
1505
1506    public void addUsers(long pk, long[] userPKs) throws SystemException {
1507        try {
1508            for (long userPK : userPKs) {
1509                addUser.add(pk, userPK);
1510            }
1511        }
1512        catch (Exception e) {
1513            throw processException(e);
1514        }
1515        finally {
1516            FinderCacheUtil.clearCache(UserGroupModelImpl.MAPPING_TABLE_USERS_USERGROUPS_NAME);
1517        }
1518    }
1519
1520    public void addUsers(long pk, List<com.liferay.portal.model.User> users)
1521        throws SystemException {
1522        try {
1523            for (com.liferay.portal.model.User user : users) {
1524                addUser.add(pk, user.getPrimaryKey());
1525            }
1526        }
1527        catch (Exception e) {
1528            throw processException(e);
1529        }
1530        finally {
1531            FinderCacheUtil.clearCache(UserGroupModelImpl.MAPPING_TABLE_USERS_USERGROUPS_NAME);
1532        }
1533    }
1534
1535    public void clearUsers(long pk) throws SystemException {
1536        try {
1537            clearUsers.clear(pk);
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 removeUser(long pk, long userPK) throws SystemException {
1548        try {
1549            removeUser.remove(pk, userPK);
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, com.liferay.portal.model.User user)
1560        throws SystemException {
1561        try {
1562            removeUser.remove(pk, user.getPrimaryKey());
1563        }
1564        catch (Exception e) {
1565            throw processException(e);
1566        }
1567        finally {
1568            FinderCacheUtil.clearCache(UserGroupModelImpl.MAPPING_TABLE_USERS_USERGROUPS_NAME);
1569        }
1570    }
1571
1572    public void removeUsers(long pk, long[] userPKs) throws SystemException {
1573        try {
1574            for (long userPK : userPKs) {
1575                removeUser.remove(pk, userPK);
1576            }
1577        }
1578        catch (Exception e) {
1579            throw processException(e);
1580        }
1581        finally {
1582            FinderCacheUtil.clearCache(UserGroupModelImpl.MAPPING_TABLE_USERS_USERGROUPS_NAME);
1583        }
1584    }
1585
1586    public void removeUsers(long pk, List<com.liferay.portal.model.User> users)
1587        throws SystemException {
1588        try {
1589            for (com.liferay.portal.model.User user : users) {
1590                removeUser.remove(pk, user.getPrimaryKey());
1591            }
1592        }
1593        catch (Exception e) {
1594            throw processException(e);
1595        }
1596        finally {
1597            FinderCacheUtil.clearCache(UserGroupModelImpl.MAPPING_TABLE_USERS_USERGROUPS_NAME);
1598        }
1599    }
1600
1601    public void setUsers(long pk, long[] userPKs) throws SystemException {
1602        try {
1603            Set<Long> userPKSet = SetUtil.fromArray(userPKs);
1604
1605            List<com.liferay.portal.model.User> users = getUsers(pk);
1606
1607            for (com.liferay.portal.model.User user : users) {
1608                if (!userPKSet.contains(user.getPrimaryKey())) {
1609                    removeUser.remove(pk, user.getPrimaryKey());
1610                }
1611                else {
1612                    userPKSet.remove(user.getPrimaryKey());
1613                }
1614            }
1615
1616            for (Long userPK : userPKSet) {
1617                addUser.add(pk, userPK);
1618            }
1619        }
1620        catch (Exception e) {
1621            throw processException(e);
1622        }
1623        finally {
1624            FinderCacheUtil.clearCache(UserGroupModelImpl.MAPPING_TABLE_USERS_USERGROUPS_NAME);
1625        }
1626    }
1627
1628    public void setUsers(long pk, List<com.liferay.portal.model.User> users)
1629        throws SystemException {
1630        try {
1631            long[] userPKs = new long[users.size()];
1632
1633            for (int i = 0; i < users.size(); i++) {
1634                com.liferay.portal.model.User user = users.get(i);
1635
1636                userPKs[i] = user.getPrimaryKey();
1637            }
1638
1639            setUsers(pk, userPKs);
1640        }
1641        catch (Exception e) {
1642            throw processException(e);
1643        }
1644        finally {
1645            FinderCacheUtil.clearCache(UserGroupModelImpl.MAPPING_TABLE_USERS_USERGROUPS_NAME);
1646        }
1647    }
1648
1649    public void afterPropertiesSet() {
1650        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1651                    com.liferay.portal.util.PropsUtil.get(
1652                        "value.object.listener.com.liferay.portal.model.UserGroup")));
1653
1654        if (listenerClassNames.length > 0) {
1655            try {
1656                List<ModelListener<UserGroup>> listenersList = new ArrayList<ModelListener<UserGroup>>();
1657
1658                for (String listenerClassName : listenerClassNames) {
1659                    listenersList.add((ModelListener<UserGroup>)Class.forName(
1660                            listenerClassName).newInstance());
1661                }
1662
1663                listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1664            }
1665            catch (Exception e) {
1666                _log.error(e);
1667            }
1668        }
1669
1670        containsUser = new ContainsUser(this);
1671
1672        addUser = new AddUser(this);
1673        clearUsers = new ClearUsers(this);
1674        removeUser = new RemoveUser(this);
1675    }
1676
1677    @BeanReference(type = AccountPersistence.class)
1678    protected AccountPersistence accountPersistence;
1679    @BeanReference(type = AddressPersistence.class)
1680    protected AddressPersistence addressPersistence;
1681    @BeanReference(type = BrowserTrackerPersistence.class)
1682    protected BrowserTrackerPersistence browserTrackerPersistence;
1683    @BeanReference(type = ClassNamePersistence.class)
1684    protected ClassNamePersistence classNamePersistence;
1685    @BeanReference(type = CompanyPersistence.class)
1686    protected CompanyPersistence companyPersistence;
1687    @BeanReference(type = ContactPersistence.class)
1688    protected ContactPersistence contactPersistence;
1689    @BeanReference(type = CountryPersistence.class)
1690    protected CountryPersistence countryPersistence;
1691    @BeanReference(type = EmailAddressPersistence.class)
1692    protected EmailAddressPersistence emailAddressPersistence;
1693    @BeanReference(type = GroupPersistence.class)
1694    protected GroupPersistence groupPersistence;
1695    @BeanReference(type = ImagePersistence.class)
1696    protected ImagePersistence imagePersistence;
1697    @BeanReference(type = LayoutPersistence.class)
1698    protected LayoutPersistence layoutPersistence;
1699    @BeanReference(type = LayoutPrototypePersistence.class)
1700    protected LayoutPrototypePersistence layoutPrototypePersistence;
1701    @BeanReference(type = LayoutSetPersistence.class)
1702    protected LayoutSetPersistence layoutSetPersistence;
1703    @BeanReference(type = LayoutSetPrototypePersistence.class)
1704    protected LayoutSetPrototypePersistence layoutSetPrototypePersistence;
1705    @BeanReference(type = ListTypePersistence.class)
1706    protected ListTypePersistence listTypePersistence;
1707    @BeanReference(type = LockPersistence.class)
1708    protected LockPersistence lockPersistence;
1709    @BeanReference(type = MembershipRequestPersistence.class)
1710    protected MembershipRequestPersistence membershipRequestPersistence;
1711    @BeanReference(type = OrganizationPersistence.class)
1712    protected OrganizationPersistence organizationPersistence;
1713    @BeanReference(type = OrgGroupPermissionPersistence.class)
1714    protected OrgGroupPermissionPersistence orgGroupPermissionPersistence;
1715    @BeanReference(type = OrgGroupRolePersistence.class)
1716    protected OrgGroupRolePersistence orgGroupRolePersistence;
1717    @BeanReference(type = OrgLaborPersistence.class)
1718    protected OrgLaborPersistence orgLaborPersistence;
1719    @BeanReference(type = PasswordPolicyPersistence.class)
1720    protected PasswordPolicyPersistence passwordPolicyPersistence;
1721    @BeanReference(type = PasswordPolicyRelPersistence.class)
1722    protected PasswordPolicyRelPersistence passwordPolicyRelPersistence;
1723    @BeanReference(type = PasswordTrackerPersistence.class)
1724    protected PasswordTrackerPersistence passwordTrackerPersistence;
1725    @BeanReference(type = PermissionPersistence.class)
1726    protected PermissionPersistence permissionPersistence;
1727    @BeanReference(type = PhonePersistence.class)
1728    protected PhonePersistence phonePersistence;
1729    @BeanReference(type = PluginSettingPersistence.class)
1730    protected PluginSettingPersistence pluginSettingPersistence;
1731    @BeanReference(type = PortletPersistence.class)
1732    protected PortletPersistence portletPersistence;
1733    @BeanReference(type = PortletItemPersistence.class)
1734    protected PortletItemPersistence portletItemPersistence;
1735    @BeanReference(type = PortletPreferencesPersistence.class)
1736    protected PortletPreferencesPersistence portletPreferencesPersistence;
1737    @BeanReference(type = RegionPersistence.class)
1738    protected RegionPersistence regionPersistence;
1739    @BeanReference(type = ReleasePersistence.class)
1740    protected ReleasePersistence releasePersistence;
1741    @BeanReference(type = ResourcePersistence.class)
1742    protected ResourcePersistence resourcePersistence;
1743    @BeanReference(type = ResourceActionPersistence.class)
1744    protected ResourceActionPersistence resourceActionPersistence;
1745    @BeanReference(type = ResourceCodePersistence.class)
1746    protected ResourceCodePersistence resourceCodePersistence;
1747    @BeanReference(type = ResourcePermissionPersistence.class)
1748    protected ResourcePermissionPersistence resourcePermissionPersistence;
1749    @BeanReference(type = RolePersistence.class)
1750    protected RolePersistence rolePersistence;
1751    @BeanReference(type = ServiceComponentPersistence.class)
1752    protected ServiceComponentPersistence serviceComponentPersistence;
1753    @BeanReference(type = ShardPersistence.class)
1754    protected ShardPersistence shardPersistence;
1755    @BeanReference(type = SubscriptionPersistence.class)
1756    protected SubscriptionPersistence subscriptionPersistence;
1757    @BeanReference(type = TeamPersistence.class)
1758    protected TeamPersistence teamPersistence;
1759    @BeanReference(type = UserPersistence.class)
1760    protected UserPersistence userPersistence;
1761    @BeanReference(type = UserGroupPersistence.class)
1762    protected UserGroupPersistence userGroupPersistence;
1763    @BeanReference(type = UserGroupGroupRolePersistence.class)
1764    protected UserGroupGroupRolePersistence userGroupGroupRolePersistence;
1765    @BeanReference(type = UserGroupRolePersistence.class)
1766    protected UserGroupRolePersistence userGroupRolePersistence;
1767    @BeanReference(type = UserIdMapperPersistence.class)
1768    protected UserIdMapperPersistence userIdMapperPersistence;
1769    @BeanReference(type = UserTrackerPersistence.class)
1770    protected UserTrackerPersistence userTrackerPersistence;
1771    @BeanReference(type = UserTrackerPathPersistence.class)
1772    protected UserTrackerPathPersistence userTrackerPathPersistence;
1773    @BeanReference(type = WebDAVPropsPersistence.class)
1774    protected WebDAVPropsPersistence webDAVPropsPersistence;
1775    @BeanReference(type = WebsitePersistence.class)
1776    protected WebsitePersistence websitePersistence;
1777    @BeanReference(type = WorkflowDefinitionLinkPersistence.class)
1778    protected WorkflowDefinitionLinkPersistence workflowDefinitionLinkPersistence;
1779    @BeanReference(type = WorkflowInstanceLinkPersistence.class)
1780    protected WorkflowInstanceLinkPersistence workflowInstanceLinkPersistence;
1781    protected ContainsUser containsUser;
1782    protected AddUser addUser;
1783    protected ClearUsers clearUsers;
1784    protected RemoveUser removeUser;
1785
1786    protected class ContainsUser {
1787        protected ContainsUser(UserGroupPersistenceImpl persistenceImpl) {
1788            super();
1789
1790            _mappingSqlQuery = MappingSqlQueryFactoryUtil.getMappingSqlQuery(getDataSource(),
1791                    _SQL_CONTAINSUSER,
1792                    new int[] { Types.BIGINT, Types.BIGINT }, RowMapper.COUNT);
1793        }
1794
1795        protected boolean contains(long userGroupId, long userId) {
1796            List<Integer> results = _mappingSqlQuery.execute(new Object[] {
1797                        new Long(userGroupId), new Long(userId)
1798                    });
1799
1800            if (results.size() > 0) {
1801                Integer count = results.get(0);
1802
1803                if (count.intValue() > 0) {
1804                    return true;
1805                }
1806            }
1807
1808            return false;
1809        }
1810
1811        private MappingSqlQuery<Integer> _mappingSqlQuery;
1812    }
1813
1814    protected class AddUser {
1815        protected AddUser(UserGroupPersistenceImpl persistenceImpl) {
1816            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
1817                    "INSERT INTO Users_UserGroups (userGroupId, userId) VALUES (?, ?)",
1818                    new int[] { Types.BIGINT, Types.BIGINT });
1819            _persistenceImpl = persistenceImpl;
1820        }
1821
1822        protected void add(long userGroupId, long userId)
1823            throws SystemException {
1824            if (!_persistenceImpl.containsUser.contains(userGroupId, userId)) {
1825                ModelListener<com.liferay.portal.model.User>[] userListeners = userPersistence.getListeners();
1826
1827                for (ModelListener<UserGroup> listener : listeners) {
1828                    listener.onBeforeAddAssociation(userGroupId,
1829                        com.liferay.portal.model.User.class.getName(), userId);
1830                }
1831
1832                for (ModelListener<com.liferay.portal.model.User> listener : userListeners) {
1833                    listener.onBeforeAddAssociation(userId,
1834                        UserGroup.class.getName(), userGroupId);
1835                }
1836
1837                _sqlUpdate.update(new Object[] {
1838                        new Long(userGroupId), new Long(userId)
1839                    });
1840
1841                for (ModelListener<UserGroup> listener : listeners) {
1842                    listener.onAfterAddAssociation(userGroupId,
1843                        com.liferay.portal.model.User.class.getName(), userId);
1844                }
1845
1846                for (ModelListener<com.liferay.portal.model.User> listener : userListeners) {
1847                    listener.onAfterAddAssociation(userId,
1848                        UserGroup.class.getName(), userGroupId);
1849                }
1850            }
1851        }
1852
1853        private SqlUpdate _sqlUpdate;
1854        private UserGroupPersistenceImpl _persistenceImpl;
1855    }
1856
1857    protected class ClearUsers {
1858        protected ClearUsers(UserGroupPersistenceImpl persistenceImpl) {
1859            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
1860                    "DELETE FROM Users_UserGroups WHERE userGroupId = ?",
1861                    new int[] { Types.BIGINT });
1862        }
1863
1864        protected void clear(long userGroupId) throws SystemException {
1865            ModelListener<com.liferay.portal.model.User>[] userListeners = userPersistence.getListeners();
1866
1867            List<com.liferay.portal.model.User> users = null;
1868
1869            if ((listeners.length > 0) || (userListeners.length > 0)) {
1870                users = getUsers(userGroupId);
1871
1872                for (com.liferay.portal.model.User user : users) {
1873                    for (ModelListener<UserGroup> listener : listeners) {
1874                        listener.onBeforeRemoveAssociation(userGroupId,
1875                            com.liferay.portal.model.User.class.getName(),
1876                            user.getPrimaryKey());
1877                    }
1878
1879                    for (ModelListener<com.liferay.portal.model.User> listener : userListeners) {
1880                        listener.onBeforeRemoveAssociation(user.getPrimaryKey(),
1881                            UserGroup.class.getName(), userGroupId);
1882                    }
1883                }
1884            }
1885
1886            _sqlUpdate.update(new Object[] { new Long(userGroupId) });
1887
1888            if ((listeners.length > 0) || (userListeners.length > 0)) {
1889                for (com.liferay.portal.model.User user : users) {
1890                    for (ModelListener<UserGroup> listener : listeners) {
1891                        listener.onAfterRemoveAssociation(userGroupId,
1892                            com.liferay.portal.model.User.class.getName(),
1893                            user.getPrimaryKey());
1894                    }
1895
1896                    for (ModelListener<com.liferay.portal.model.User> listener : userListeners) {
1897                        listener.onAfterRemoveAssociation(user.getPrimaryKey(),
1898                            UserGroup.class.getName(), userGroupId);
1899                    }
1900                }
1901            }
1902        }
1903
1904        private SqlUpdate _sqlUpdate;
1905    }
1906
1907    protected class RemoveUser {
1908        protected RemoveUser(UserGroupPersistenceImpl persistenceImpl) {
1909            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
1910                    "DELETE FROM Users_UserGroups WHERE userGroupId = ? AND userId = ?",
1911                    new int[] { Types.BIGINT, Types.BIGINT });
1912            _persistenceImpl = persistenceImpl;
1913        }
1914
1915        protected void remove(long userGroupId, long userId)
1916            throws SystemException {
1917            if (_persistenceImpl.containsUser.contains(userGroupId, userId)) {
1918                ModelListener<com.liferay.portal.model.User>[] userListeners = userPersistence.getListeners();
1919
1920                for (ModelListener<UserGroup> listener : listeners) {
1921                    listener.onBeforeRemoveAssociation(userGroupId,
1922                        com.liferay.portal.model.User.class.getName(), userId);
1923                }
1924
1925                for (ModelListener<com.liferay.portal.model.User> listener : userListeners) {
1926                    listener.onBeforeRemoveAssociation(userId,
1927                        UserGroup.class.getName(), userGroupId);
1928                }
1929
1930                _sqlUpdate.update(new Object[] {
1931                        new Long(userGroupId), new Long(userId)
1932                    });
1933
1934                for (ModelListener<UserGroup> listener : listeners) {
1935                    listener.onAfterRemoveAssociation(userGroupId,
1936                        com.liferay.portal.model.User.class.getName(), userId);
1937                }
1938
1939                for (ModelListener<com.liferay.portal.model.User> listener : userListeners) {
1940                    listener.onAfterRemoveAssociation(userId,
1941                        UserGroup.class.getName(), userGroupId);
1942                }
1943            }
1944        }
1945
1946        private SqlUpdate _sqlUpdate;
1947        private UserGroupPersistenceImpl _persistenceImpl;
1948    }
1949
1950    private static final String _SQL_SELECT_USERGROUP = "SELECT userGroup FROM UserGroup userGroup";
1951    private static final String _SQL_SELECT_USERGROUP_WHERE = "SELECT userGroup FROM UserGroup userGroup WHERE ";
1952    private static final String _SQL_COUNT_USERGROUP = "SELECT COUNT(userGroup) FROM UserGroup userGroup";
1953    private static final String _SQL_COUNT_USERGROUP_WHERE = "SELECT COUNT(userGroup) FROM UserGroup userGroup WHERE ";
1954    private static final String _SQL_GETUSERS = "SELECT {User_.*} FROM User_ INNER JOIN Users_UserGroups ON (Users_UserGroups.userId = User_.userId) WHERE (Users_UserGroups.userGroupId = ?)";
1955    private static final String _SQL_GETUSERSSIZE = "SELECT COUNT(*) AS COUNT_VALUE FROM Users_UserGroups WHERE userGroupId = ?";
1956    private static final String _SQL_CONTAINSUSER = "SELECT COUNT(*) AS COUNT_VALUE FROM Users_UserGroups WHERE userGroupId = ? AND userId = ?";
1957    private static final String _FINDER_COLUMN_COMPANYID_COMPANYID_2 = "userGroup.companyId = ?";
1958    private static final String _FINDER_COLUMN_C_P_COMPANYID_2 = "userGroup.companyId = ? AND ";
1959    private static final String _FINDER_COLUMN_C_P_PARENTUSERGROUPID_2 = "userGroup.parentUserGroupId = ?";
1960    private static final String _FINDER_COLUMN_C_N_COMPANYID_2 = "userGroup.companyId = ? AND ";
1961    private static final String _FINDER_COLUMN_C_N_NAME_1 = "userGroup.name IS NULL";
1962    private static final String _FINDER_COLUMN_C_N_NAME_2 = "userGroup.name = ?";
1963    private static final String _FINDER_COLUMN_C_N_NAME_3 = "(userGroup.name IS NULL OR userGroup.name = ?)";
1964    private static final String _ORDER_BY_ENTITY_ALIAS = "userGroup.";
1965    private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No UserGroup exists with the primary key ";
1966    private static final String _NO_SUCH_ENTITY_WITH_KEY = "No UserGroup exists with the key {";
1967    private static Log _log = LogFactoryUtil.getLog(UserGroupPersistenceImpl.class);
1968}