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