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