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