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