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