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             if (BatchSessionUtil.isEnabled()) {
147                 Object staleObject = session.get(UserGroupImpl.class,
148                         userGroup.getPrimaryKeyObj());
149 
150                 if (staleObject != null) {
151                     session.evict(staleObject);
152                 }
153             }
154 
155             session.delete(userGroup);
156 
157             session.flush();
158 
159             return userGroup;
160         }
161         catch (Exception e) {
162             throw processException(e);
163         }
164         finally {
165             closeSession(session);
166 
167             FinderCacheUtil.clearCache(UserGroup.class.getName());
168         }
169     }
170 
171     /**
172      * @deprecated Use <code>update(UserGroup userGroup, boolean merge)</code>.
173      */
174     public UserGroup update(UserGroup userGroup) throws SystemException {
175         if (_log.isWarnEnabled()) {
176             _log.warn(
177                 "Using the deprecated update(UserGroup userGroup) method. Use update(UserGroup userGroup, boolean merge) instead.");
178         }
179 
180         return update(userGroup, false);
181     }
182 
183     /**
184      * Add, update, or merge, the entity. This method also calls the model
185      * listeners to trigger the proper events associated with adding, deleting,
186      * or updating an entity.
187      *
188      * @param        userGroup the entity to add, update, or merge
189      * @param        merge boolean value for whether to merge the entity. The
190      *                default value is false. Setting merge to true is more
191      *                expensive and should only be true when userGroup is
192      *                transient. See LEP-5473 for a detailed discussion of this
193      *                method.
194      * @return        true if the portlet can be displayed via Ajax
195      */
196     public UserGroup update(UserGroup userGroup, boolean merge)
197         throws SystemException {
198         boolean isNew = userGroup.isNew();
199 
200         if (_listeners.length > 0) {
201             for (ModelListener listener : _listeners) {
202                 if (isNew) {
203                     listener.onBeforeCreate(userGroup);
204                 }
205                 else {
206                     listener.onBeforeUpdate(userGroup);
207                 }
208             }
209         }
210 
211         userGroup = updateImpl(userGroup, merge);
212 
213         if (_listeners.length > 0) {
214             for (ModelListener listener : _listeners) {
215                 if (isNew) {
216                     listener.onAfterCreate(userGroup);
217                 }
218                 else {
219                     listener.onAfterUpdate(userGroup);
220                 }
221             }
222         }
223 
224         return userGroup;
225     }
226 
227     public UserGroup updateImpl(com.liferay.portal.model.UserGroup userGroup,
228         boolean merge) throws SystemException {
229         FinderCacheUtil.clearCache("Users_UserGroups");
230 
231         Session session = null;
232 
233         try {
234             session = openSession();
235 
236             BatchSessionUtil.update(session, userGroup, merge);
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 = null;
997 
998                 if (obc == null) {
999                     list = (List<UserGroup>)QueryUtil.list(q, getDialect(),
1000                            start, end, false);
1001
1002                    Collections.sort(list);
1003                }
1004                else {
1005                    list = (List<UserGroup>)QueryUtil.list(q, getDialect(),
1006                            start, end);
1007                }
1008
1009                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1010                    finderClassName, finderMethodName, finderParams,
1011                    finderArgs, list);
1012
1013                return list;
1014            }
1015            catch (Exception e) {
1016                throw processException(e);
1017            }
1018            finally {
1019                closeSession(session);
1020            }
1021        }
1022        else {
1023            return (List<UserGroup>)result;
1024        }
1025    }
1026
1027    public void removeByCompanyId(long companyId) throws SystemException {
1028        for (UserGroup userGroup : findByCompanyId(companyId)) {
1029            remove(userGroup);
1030        }
1031    }
1032
1033    public void removeByC_P(long companyId, long parentUserGroupId)
1034        throws SystemException {
1035        for (UserGroup userGroup : findByC_P(companyId, parentUserGroupId)) {
1036            remove(userGroup);
1037        }
1038    }
1039
1040    public void removeByC_N(long companyId, String name)
1041        throws NoSuchUserGroupException, SystemException {
1042        UserGroup userGroup = findByC_N(companyId, name);
1043
1044        remove(userGroup);
1045    }
1046
1047    public void removeAll() throws SystemException {
1048        for (UserGroup userGroup : findAll()) {
1049            remove(userGroup);
1050        }
1051    }
1052
1053    public int countByCompanyId(long companyId) throws SystemException {
1054        boolean finderClassNameCacheEnabled = UserGroupModelImpl.CACHE_ENABLED;
1055        String finderClassName = UserGroup.class.getName();
1056        String finderMethodName = "countByCompanyId";
1057        String[] finderParams = new String[] { Long.class.getName() };
1058        Object[] finderArgs = new Object[] { new Long(companyId) };
1059
1060        Object result = null;
1061
1062        if (finderClassNameCacheEnabled) {
1063            result = FinderCacheUtil.getResult(finderClassName,
1064                    finderMethodName, finderParams, finderArgs, this);
1065        }
1066
1067        if (result == null) {
1068            Session session = null;
1069
1070            try {
1071                session = openSession();
1072
1073                StringBuilder query = new StringBuilder();
1074
1075                query.append("SELECT COUNT(*) ");
1076                query.append("FROM com.liferay.portal.model.UserGroup WHERE ");
1077
1078                query.append("companyId = ?");
1079
1080                query.append(" ");
1081
1082                Query q = session.createQuery(query.toString());
1083
1084                QueryPos qPos = QueryPos.getInstance(q);
1085
1086                qPos.add(companyId);
1087
1088                Long count = null;
1089
1090                Iterator<Long> itr = q.list().iterator();
1091
1092                if (itr.hasNext()) {
1093                    count = itr.next();
1094                }
1095
1096                if (count == null) {
1097                    count = new Long(0);
1098                }
1099
1100                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1101                    finderClassName, finderMethodName, finderParams,
1102                    finderArgs, count);
1103
1104                return count.intValue();
1105            }
1106            catch (Exception e) {
1107                throw processException(e);
1108            }
1109            finally {
1110                closeSession(session);
1111            }
1112        }
1113        else {
1114            return ((Long)result).intValue();
1115        }
1116    }
1117
1118    public int countByC_P(long companyId, long parentUserGroupId)
1119        throws SystemException {
1120        boolean finderClassNameCacheEnabled = UserGroupModelImpl.CACHE_ENABLED;
1121        String finderClassName = UserGroup.class.getName();
1122        String finderMethodName = "countByC_P";
1123        String[] finderParams = new String[] {
1124                Long.class.getName(), Long.class.getName()
1125            };
1126        Object[] finderArgs = new Object[] {
1127                new Long(companyId), new Long(parentUserGroupId)
1128            };
1129
1130        Object result = null;
1131
1132        if (finderClassNameCacheEnabled) {
1133            result = FinderCacheUtil.getResult(finderClassName,
1134                    finderMethodName, finderParams, finderArgs, this);
1135        }
1136
1137        if (result == null) {
1138            Session session = null;
1139
1140            try {
1141                session = openSession();
1142
1143                StringBuilder query = new StringBuilder();
1144
1145                query.append("SELECT COUNT(*) ");
1146                query.append("FROM com.liferay.portal.model.UserGroup WHERE ");
1147
1148                query.append("companyId = ?");
1149
1150                query.append(" AND ");
1151
1152                query.append("parentUserGroupId = ?");
1153
1154                query.append(" ");
1155
1156                Query q = session.createQuery(query.toString());
1157
1158                QueryPos qPos = QueryPos.getInstance(q);
1159
1160                qPos.add(companyId);
1161
1162                qPos.add(parentUserGroupId);
1163
1164                Long count = null;
1165
1166                Iterator<Long> itr = q.list().iterator();
1167
1168                if (itr.hasNext()) {
1169                    count = itr.next();
1170                }
1171
1172                if (count == null) {
1173                    count = new Long(0);
1174                }
1175
1176                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1177                    finderClassName, finderMethodName, finderParams,
1178                    finderArgs, count);
1179
1180                return count.intValue();
1181            }
1182            catch (Exception e) {
1183                throw processException(e);
1184            }
1185            finally {
1186                closeSession(session);
1187            }
1188        }
1189        else {
1190            return ((Long)result).intValue();
1191        }
1192    }
1193
1194    public int countByC_N(long companyId, String name)
1195        throws SystemException {
1196        boolean finderClassNameCacheEnabled = UserGroupModelImpl.CACHE_ENABLED;
1197        String finderClassName = UserGroup.class.getName();
1198        String finderMethodName = "countByC_N";
1199        String[] finderParams = new String[] {
1200                Long.class.getName(), String.class.getName()
1201            };
1202        Object[] finderArgs = new Object[] { new Long(companyId), name };
1203
1204        Object result = null;
1205
1206        if (finderClassNameCacheEnabled) {
1207            result = FinderCacheUtil.getResult(finderClassName,
1208                    finderMethodName, finderParams, finderArgs, this);
1209        }
1210
1211        if (result == null) {
1212            Session session = null;
1213
1214            try {
1215                session = openSession();
1216
1217                StringBuilder query = new StringBuilder();
1218
1219                query.append("SELECT COUNT(*) ");
1220                query.append("FROM com.liferay.portal.model.UserGroup WHERE ");
1221
1222                query.append("companyId = ?");
1223
1224                query.append(" AND ");
1225
1226                if (name == null) {
1227                    query.append("name IS NULL");
1228                }
1229                else {
1230                    query.append("name = ?");
1231                }
1232
1233                query.append(" ");
1234
1235                Query q = session.createQuery(query.toString());
1236
1237                QueryPos qPos = QueryPos.getInstance(q);
1238
1239                qPos.add(companyId);
1240
1241                if (name != null) {
1242                    qPos.add(name);
1243                }
1244
1245                Long count = null;
1246
1247                Iterator<Long> itr = q.list().iterator();
1248
1249                if (itr.hasNext()) {
1250                    count = itr.next();
1251                }
1252
1253                if (count == null) {
1254                    count = new Long(0);
1255                }
1256
1257                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1258                    finderClassName, finderMethodName, finderParams,
1259                    finderArgs, count);
1260
1261                return count.intValue();
1262            }
1263            catch (Exception e) {
1264                throw processException(e);
1265            }
1266            finally {
1267                closeSession(session);
1268            }
1269        }
1270        else {
1271            return ((Long)result).intValue();
1272        }
1273    }
1274
1275    public int countAll() throws SystemException {
1276        boolean finderClassNameCacheEnabled = UserGroupModelImpl.CACHE_ENABLED;
1277        String finderClassName = UserGroup.class.getName();
1278        String finderMethodName = "countAll";
1279        String[] finderParams = new String[] {  };
1280        Object[] finderArgs = new Object[] {  };
1281
1282        Object result = null;
1283
1284        if (finderClassNameCacheEnabled) {
1285            result = FinderCacheUtil.getResult(finderClassName,
1286                    finderMethodName, finderParams, finderArgs, this);
1287        }
1288
1289        if (result == null) {
1290            Session session = null;
1291
1292            try {
1293                session = openSession();
1294
1295                Query q = session.createQuery(
1296                        "SELECT COUNT(*) FROM com.liferay.portal.model.UserGroup");
1297
1298                Long count = null;
1299
1300                Iterator<Long> itr = q.list().iterator();
1301
1302                if (itr.hasNext()) {
1303                    count = itr.next();
1304                }
1305
1306                if (count == null) {
1307                    count = new Long(0);
1308                }
1309
1310                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1311                    finderClassName, finderMethodName, finderParams,
1312                    finderArgs, count);
1313
1314                return count.intValue();
1315            }
1316            catch (Exception e) {
1317                throw processException(e);
1318            }
1319            finally {
1320                closeSession(session);
1321            }
1322        }
1323        else {
1324            return ((Long)result).intValue();
1325        }
1326    }
1327
1328    public List<com.liferay.portal.model.User> getUsers(long pk)
1329        throws SystemException {
1330        return getUsers(pk, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
1331    }
1332
1333    public List<com.liferay.portal.model.User> getUsers(long pk, int start,
1334        int end) throws SystemException {
1335        return getUsers(pk, start, end, null);
1336    }
1337
1338    public List<com.liferay.portal.model.User> getUsers(long pk, int start,
1339        int end, OrderByComparator obc) throws SystemException {
1340        boolean finderClassNameCacheEnabled = UserGroupModelImpl.CACHE_ENABLED_USERS_USERGROUPS;
1341
1342        String finderClassName = "Users_UserGroups";
1343
1344        String finderMethodName = "getUsers";
1345        String[] finderParams = new String[] {
1346                Long.class.getName(), "java.lang.Integer", "java.lang.Integer",
1347                "com.liferay.portal.kernel.util.OrderByComparator"
1348            };
1349        Object[] finderArgs = new Object[] {
1350                new Long(pk), String.valueOf(start), String.valueOf(end),
1351                String.valueOf(obc)
1352            };
1353
1354        Object result = null;
1355
1356        if (finderClassNameCacheEnabled) {
1357            result = FinderCacheUtil.getResult(finderClassName,
1358                    finderMethodName, finderParams, finderArgs, this);
1359        }
1360
1361        if (result == null) {
1362            Session session = null;
1363
1364            try {
1365                session = openSession();
1366
1367                StringBuilder sb = new StringBuilder();
1368
1369                sb.append(_SQL_GETUSERS);
1370
1371                if (obc != null) {
1372                    sb.append("ORDER BY ");
1373                    sb.append(obc.getOrderBy());
1374                }
1375
1376                String sql = sb.toString();
1377
1378                SQLQuery q = session.createSQLQuery(sql);
1379
1380                q.addEntity("User_",
1381                    com.liferay.portal.model.impl.UserImpl.class);
1382
1383                QueryPos qPos = QueryPos.getInstance(q);
1384
1385                qPos.add(pk);
1386
1387                List<com.liferay.portal.model.User> list = (List<com.liferay.portal.model.User>)QueryUtil.list(q,
1388                        getDialect(), start, end);
1389
1390                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1391                    finderClassName, finderMethodName, finderParams,
1392                    finderArgs, list);
1393
1394                return list;
1395            }
1396            catch (Exception e) {
1397                throw processException(e);
1398            }
1399            finally {
1400                closeSession(session);
1401            }
1402        }
1403        else {
1404            return (List<com.liferay.portal.model.User>)result;
1405        }
1406    }
1407
1408    public int getUsersSize(long pk) throws SystemException {
1409        boolean finderClassNameCacheEnabled = UserGroupModelImpl.CACHE_ENABLED_USERS_USERGROUPS;
1410
1411        String finderClassName = "Users_UserGroups";
1412
1413        String finderMethodName = "getUsersSize";
1414        String[] finderParams = new String[] { Long.class.getName() };
1415        Object[] finderArgs = new Object[] { new Long(pk) };
1416
1417        Object result = null;
1418
1419        if (finderClassNameCacheEnabled) {
1420            result = FinderCacheUtil.getResult(finderClassName,
1421                    finderMethodName, finderParams, finderArgs, this);
1422        }
1423
1424        if (result == null) {
1425            Session session = null;
1426
1427            try {
1428                session = openSession();
1429
1430                SQLQuery q = session.createSQLQuery(_SQL_GETUSERSSIZE);
1431
1432                q.addScalar(COUNT_COLUMN_NAME, Type.LONG);
1433
1434                QueryPos qPos = QueryPos.getInstance(q);
1435
1436                qPos.add(pk);
1437
1438                Long count = null;
1439
1440                Iterator<Long> itr = q.list().iterator();
1441
1442                if (itr.hasNext()) {
1443                    count = itr.next();
1444                }
1445
1446                if (count == null) {
1447                    count = new Long(0);
1448                }
1449
1450                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1451                    finderClassName, finderMethodName, finderParams,
1452                    finderArgs, count);
1453
1454                return count.intValue();
1455            }
1456            catch (Exception e) {
1457                throw processException(e);
1458            }
1459            finally {
1460                closeSession(session);
1461            }
1462        }
1463        else {
1464            return ((Long)result).intValue();
1465        }
1466    }
1467
1468    public boolean containsUser(long pk, long userPK) throws SystemException {
1469        boolean finderClassNameCacheEnabled = UserGroupModelImpl.CACHE_ENABLED_USERS_USERGROUPS;
1470
1471        String finderClassName = "Users_UserGroups";
1472
1473        String finderMethodName = "containsUsers";
1474        String[] finderParams = new String[] {
1475                Long.class.getName(),
1476                
1477                Long.class.getName()
1478            };
1479        Object[] finderArgs = new Object[] { new Long(pk), new Long(userPK) };
1480
1481        Object result = null;
1482
1483        if (finderClassNameCacheEnabled) {
1484            result = FinderCacheUtil.getResult(finderClassName,
1485                    finderMethodName, finderParams, finderArgs, this);
1486        }
1487
1488        if (result == null) {
1489            try {
1490                Boolean value = Boolean.valueOf(containsUser.contains(pk, userPK));
1491
1492                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1493                    finderClassName, finderMethodName, finderParams,
1494                    finderArgs, value);
1495
1496                return value.booleanValue();
1497            }
1498            catch (Exception e) {
1499                throw processException(e);
1500            }
1501        }
1502        else {
1503            return ((Boolean)result).booleanValue();
1504        }
1505    }
1506
1507    public boolean containsUsers(long pk) throws SystemException {
1508        if (getUsersSize(pk) > 0) {
1509            return true;
1510        }
1511        else {
1512            return false;
1513        }
1514    }
1515
1516    public void addUser(long pk, long userPK) throws SystemException {
1517        try {
1518            addUser.add(pk, userPK);
1519        }
1520        catch (Exception e) {
1521            throw processException(e);
1522        }
1523        finally {
1524            FinderCacheUtil.clearCache("Users_UserGroups");
1525        }
1526    }
1527
1528    public void addUser(long pk, com.liferay.portal.model.User user)
1529        throws SystemException {
1530        try {
1531            addUser.add(pk, user.getPrimaryKey());
1532        }
1533        catch (Exception e) {
1534            throw processException(e);
1535        }
1536        finally {
1537            FinderCacheUtil.clearCache("Users_UserGroups");
1538        }
1539    }
1540
1541    public void addUsers(long pk, long[] userPKs) throws SystemException {
1542        try {
1543            for (long userPK : userPKs) {
1544                addUser.add(pk, userPK);
1545            }
1546        }
1547        catch (Exception e) {
1548            throw processException(e);
1549        }
1550        finally {
1551            FinderCacheUtil.clearCache("Users_UserGroups");
1552        }
1553    }
1554
1555    public void addUsers(long pk, List<com.liferay.portal.model.User> users)
1556        throws SystemException {
1557        try {
1558            for (com.liferay.portal.model.User user : users) {
1559                addUser.add(pk, user.getPrimaryKey());
1560            }
1561        }
1562        catch (Exception e) {
1563            throw processException(e);
1564        }
1565        finally {
1566            FinderCacheUtil.clearCache("Users_UserGroups");
1567        }
1568    }
1569
1570    public void clearUsers(long pk) throws SystemException {
1571        try {
1572            clearUsers.clear(pk);
1573        }
1574        catch (Exception e) {
1575            throw processException(e);
1576        }
1577        finally {
1578            FinderCacheUtil.clearCache("Users_UserGroups");
1579        }
1580    }
1581
1582    public void removeUser(long pk, long userPK) throws SystemException {
1583        try {
1584            removeUser.remove(pk, userPK);
1585        }
1586        catch (Exception e) {
1587            throw processException(e);
1588        }
1589        finally {
1590            FinderCacheUtil.clearCache("Users_UserGroups");
1591        }
1592    }
1593
1594    public void removeUser(long pk, com.liferay.portal.model.User user)
1595        throws SystemException {
1596        try {
1597            removeUser.remove(pk, user.getPrimaryKey());
1598        }
1599        catch (Exception e) {
1600            throw processException(e);
1601        }
1602        finally {
1603            FinderCacheUtil.clearCache("Users_UserGroups");
1604        }
1605    }
1606
1607    public void removeUsers(long pk, long[] userPKs) throws SystemException {
1608        try {
1609            for (long userPK : userPKs) {
1610                removeUser.remove(pk, userPK);
1611            }
1612        }
1613        catch (Exception e) {
1614            throw processException(e);
1615        }
1616        finally {
1617            FinderCacheUtil.clearCache("Users_UserGroups");
1618        }
1619    }
1620
1621    public void removeUsers(long pk, List<com.liferay.portal.model.User> users)
1622        throws SystemException {
1623        try {
1624            for (com.liferay.portal.model.User user : users) {
1625                removeUser.remove(pk, user.getPrimaryKey());
1626            }
1627        }
1628        catch (Exception e) {
1629            throw processException(e);
1630        }
1631        finally {
1632            FinderCacheUtil.clearCache("Users_UserGroups");
1633        }
1634    }
1635
1636    public void setUsers(long pk, long[] userPKs) throws SystemException {
1637        try {
1638            clearUsers.clear(pk);
1639
1640            for (long userPK : userPKs) {
1641                addUser.add(pk, userPK);
1642            }
1643        }
1644        catch (Exception e) {
1645            throw processException(e);
1646        }
1647        finally {
1648            FinderCacheUtil.clearCache("Users_UserGroups");
1649        }
1650    }
1651
1652    public void setUsers(long pk, List<com.liferay.portal.model.User> users)
1653        throws SystemException {
1654        try {
1655            clearUsers.clear(pk);
1656
1657            for (com.liferay.portal.model.User user : users) {
1658                addUser.add(pk, user.getPrimaryKey());
1659            }
1660        }
1661        catch (Exception e) {
1662            throw processException(e);
1663        }
1664        finally {
1665            FinderCacheUtil.clearCache("Users_UserGroups");
1666        }
1667    }
1668
1669    public void registerListener(ModelListener listener) {
1670        List<ModelListener> listeners = ListUtil.fromArray(_listeners);
1671
1672        listeners.add(listener);
1673
1674        _listeners = listeners.toArray(new ModelListener[listeners.size()]);
1675    }
1676
1677    public void unregisterListener(ModelListener listener) {
1678        List<ModelListener> listeners = ListUtil.fromArray(_listeners);
1679
1680        listeners.remove(listener);
1681
1682        _listeners = listeners.toArray(new ModelListener[listeners.size()]);
1683    }
1684
1685    public void afterPropertiesSet() {
1686        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1687                    com.liferay.portal.util.PropsUtil.get(
1688                        "value.object.listener.com.liferay.portal.model.UserGroup")));
1689
1690        if (listenerClassNames.length > 0) {
1691            try {
1692                List<ModelListener> listeners = new ArrayList<ModelListener>();
1693
1694                for (String listenerClassName : listenerClassNames) {
1695                    listeners.add((ModelListener)Class.forName(
1696                            listenerClassName).newInstance());
1697                }
1698
1699                _listeners = listeners.toArray(new ModelListener[listeners.size()]);
1700            }
1701            catch (Exception e) {
1702                _log.error(e);
1703            }
1704        }
1705
1706        containsUser = new ContainsUser(this);
1707
1708        addUser = new AddUser(this);
1709        clearUsers = new ClearUsers(this);
1710        removeUser = new RemoveUser(this);
1711    }
1712
1713    protected ContainsUser containsUser;
1714    protected AddUser addUser;
1715    protected ClearUsers clearUsers;
1716    protected RemoveUser removeUser;
1717
1718    protected class ContainsUser {
1719        protected ContainsUser(UserGroupPersistenceImpl persistenceImpl) {
1720            super();
1721
1722            _mappingSqlQuery = MappingSqlQueryFactoryUtil.getMappingSqlQuery(getDataSource(),
1723                    _SQL_CONTAINSUSER,
1724                    new int[] { Types.BIGINT, Types.BIGINT }, RowMapper.COUNT);
1725        }
1726
1727        protected boolean contains(long userGroupId, long userId) {
1728            List<Integer> results = _mappingSqlQuery.execute(new Object[] {
1729                        new Long(userGroupId), new Long(userId)
1730                    });
1731
1732            if (results.size() > 0) {
1733                Integer count = results.get(0);
1734
1735                if (count.intValue() > 0) {
1736                    return true;
1737                }
1738            }
1739
1740            return false;
1741        }
1742
1743        private MappingSqlQuery _mappingSqlQuery;
1744    }
1745
1746    protected class AddUser {
1747        protected AddUser(UserGroupPersistenceImpl persistenceImpl) {
1748            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
1749                    "INSERT INTO Users_UserGroups (userGroupId, userId) VALUES (?, ?)",
1750                    new int[] { Types.BIGINT, Types.BIGINT });
1751            _persistenceImpl = persistenceImpl;
1752        }
1753
1754        protected void add(long userGroupId, long userId) {
1755            if (!_persistenceImpl.containsUser.contains(userGroupId, userId)) {
1756                _sqlUpdate.update(new Object[] {
1757                        new Long(userGroupId), new Long(userId)
1758                    });
1759            }
1760        }
1761
1762        private SqlUpdate _sqlUpdate;
1763        private UserGroupPersistenceImpl _persistenceImpl;
1764    }
1765
1766    protected class ClearUsers {
1767        protected ClearUsers(UserGroupPersistenceImpl persistenceImpl) {
1768            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
1769                    "DELETE FROM Users_UserGroups WHERE userGroupId = ?",
1770                    new int[] { Types.BIGINT });
1771        }
1772
1773        protected void clear(long userGroupId) {
1774            _sqlUpdate.update(new Object[] { new Long(userGroupId) });
1775        }
1776
1777        private SqlUpdate _sqlUpdate;
1778    }
1779
1780    protected class RemoveUser {
1781        protected RemoveUser(UserGroupPersistenceImpl persistenceImpl) {
1782            _sqlUpdate = SqlUpdateFactoryUtil.getSqlUpdate(getDataSource(),
1783                    "DELETE FROM Users_UserGroups WHERE userGroupId = ? AND userId = ?",
1784                    new int[] { Types.BIGINT, Types.BIGINT });
1785        }
1786
1787        protected void remove(long userGroupId, long userId) {
1788            _sqlUpdate.update(new Object[] {
1789                    new Long(userGroupId), new Long(userId)
1790                });
1791        }
1792
1793        private SqlUpdate _sqlUpdate;
1794    }
1795
1796    private static final String _SQL_GETUSERS = "SELECT {User_.*} FROM User_ INNER JOIN Users_UserGroups ON (Users_UserGroups.userId = User_.userId) WHERE (Users_UserGroups.userGroupId = ?)";
1797    private static final String _SQL_GETUSERSSIZE = "SELECT COUNT(*) AS COUNT_VALUE FROM Users_UserGroups WHERE userGroupId = ?";
1798    private static final String _SQL_CONTAINSUSER = "SELECT COUNT(*) AS COUNT_VALUE FROM Users_UserGroups WHERE userGroupId = ? AND userId = ?";
1799    private static Log _log = LogFactory.getLog(UserGroupPersistenceImpl.class);
1800    private ModelListener[] _listeners = new ModelListener[0];
1801}