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