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