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