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