1   /**
2    * Copyright (c) 2000-2007 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portal.service.persistence;
24  
25  import com.liferay.portal.NoSuchUserGroupException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.kernel.dao.DynamicQuery;
28  import com.liferay.portal.kernel.dao.DynamicQueryInitializer;
29  import com.liferay.portal.kernel.util.OrderByComparator;
30  import com.liferay.portal.kernel.util.StringMaker;
31  import com.liferay.portal.kernel.util.StringPool;
32  import com.liferay.portal.model.UserGroup;
33  import com.liferay.portal.model.impl.UserGroupImpl;
34  import com.liferay.portal.service.persistence.BasePersistence;
35  import com.liferay.portal.spring.hibernate.FinderCache;
36  import com.liferay.portal.spring.hibernate.HibernateUtil;
37  
38  import com.liferay.util.dao.hibernate.QueryPos;
39  import com.liferay.util.dao.hibernate.QueryUtil;
40  
41  import org.apache.commons.logging.Log;
42  import org.apache.commons.logging.LogFactory;
43  
44  import org.hibernate.Hibernate;
45  import org.hibernate.Query;
46  import org.hibernate.SQLQuery;
47  import org.hibernate.Session;
48  
49  import org.springframework.dao.DataAccessException;
50  
51  import org.springframework.jdbc.core.SqlParameter;
52  import org.springframework.jdbc.object.MappingSqlQuery;
53  import org.springframework.jdbc.object.SqlUpdate;
54  
55  import java.sql.ResultSet;
56  import java.sql.SQLException;
57  import java.sql.Types;
58  
59  import java.util.Collections;
60  import java.util.Iterator;
61  import java.util.List;
62  
63  /**
64   * <a href="UserGroupPersistenceImpl.java.html"><b><i>View Source</i></b></a>
65   *
66   * @author Brian Wing Shun Chan
67   *
68   */
69  public class UserGroupPersistenceImpl extends BasePersistence
70      implements UserGroupPersistence {
71      public UserGroup create(long userGroupId) {
72          UserGroup userGroup = new UserGroupImpl();
73          userGroup.setNew(true);
74          userGroup.setPrimaryKey(userGroupId);
75  
76          return userGroup;
77      }
78  
79      public UserGroup remove(long userGroupId)
80          throws NoSuchUserGroupException, SystemException {
81          Session session = null;
82  
83          try {
84              session = openSession();
85  
86              UserGroup userGroup = (UserGroup)session.get(UserGroupImpl.class,
87                      new Long(userGroupId));
88  
89              if (userGroup == null) {
90                  if (_log.isWarnEnabled()) {
91                      _log.warn("No UserGroup exists with the primary key " +
92                          userGroupId);
93                  }
94  
95                  throw new NoSuchUserGroupException(
96                      "No UserGroup exists with the primary key " + userGroupId);
97              }
98  
99              return remove(userGroup);
100         }
101         catch (NoSuchUserGroupException nsee) {
102             throw nsee;
103         }
104         catch (Exception e) {
105             throw HibernateUtil.processException(e);
106         }
107         finally {
108             closeSession(session);
109         }
110     }
111 
112     public UserGroup remove(UserGroup userGroup) throws SystemException {
113         try {
114             clearUsers.clear(userGroup.getPrimaryKey());
115         }
116         catch (Exception e) {
117             throw HibernateUtil.processException(e);
118         }
119         finally {
120             FinderCache.clearCache("Users_UserGroups");
121         }
122 
123         Session session = null;
124 
125         try {
126             session = openSession();
127             session.delete(userGroup);
128             session.flush();
129 
130             return userGroup;
131         }
132         catch (Exception e) {
133             throw HibernateUtil.processException(e);
134         }
135         finally {
136             closeSession(session);
137             FinderCache.clearCache(UserGroup.class.getName());
138         }
139     }
140 
141     public UserGroup update(com.liferay.portal.model.UserGroup userGroup)
142         throws SystemException {
143         return update(userGroup, false);
144     }
145 
146     public UserGroup update(com.liferay.portal.model.UserGroup userGroup,
147         boolean merge) throws SystemException {
148         FinderCache.clearCache("Users_UserGroups");
149 
150         Session session = null;
151 
152         try {
153             session = openSession();
154 
155             if (merge) {
156                 session.merge(userGroup);
157             }
158             else {
159                 if (userGroup.isNew()) {
160                     session.save(userGroup);
161                 }
162             }
163 
164             session.flush();
165             userGroup.setNew(false);
166 
167             return userGroup;
168         }
169         catch (Exception e) {
170             throw HibernateUtil.processException(e);
171         }
172         finally {
173             closeSession(session);
174             FinderCache.clearCache(UserGroup.class.getName());
175         }
176     }
177 
178     public UserGroup findByPrimaryKey(long userGroupId)
179         throws NoSuchUserGroupException, SystemException {
180         UserGroup userGroup = fetchByPrimaryKey(userGroupId);
181 
182         if (userGroup == null) {
183             if (_log.isWarnEnabled()) {
184                 _log.warn("No UserGroup exists with the primary key " +
185                     userGroupId);
186             }
187 
188             throw new NoSuchUserGroupException(
189                 "No UserGroup exists with the primary key " + userGroupId);
190         }
191 
192         return userGroup;
193     }
194 
195     public UserGroup fetchByPrimaryKey(long userGroupId)
196         throws SystemException {
197         Session session = null;
198 
199         try {
200             session = openSession();
201 
202             return (UserGroup)session.get(UserGroupImpl.class,
203                 new Long(userGroupId));
204         }
205         catch (Exception e) {
206             throw HibernateUtil.processException(e);
207         }
208         finally {
209             closeSession(session);
210         }
211     }
212 
213     public List findByCompanyId(long companyId) throws SystemException {
214         String finderClassName = UserGroup.class.getName();
215         String finderMethodName = "findByCompanyId";
216         String[] finderParams = new String[] { Long.class.getName() };
217         Object[] finderArgs = new Object[] { new Long(companyId) };
218         Object result = FinderCache.getResult(finderClassName,
219                 finderMethodName, finderParams, finderArgs, getSessionFactory());
220 
221         if (result == null) {
222             Session session = null;
223 
224             try {
225                 session = openSession();
226 
227                 StringMaker query = new StringMaker();
228                 query.append("FROM com.liferay.portal.model.UserGroup WHERE ");
229                 query.append("companyId = ?");
230                 query.append(" ");
231                 query.append("ORDER BY ");
232                 query.append("name ASC");
233 
234                 Query q = session.createQuery(query.toString());
235                 int queryPos = 0;
236                 q.setLong(queryPos++, companyId);
237 
238                 List list = q.list();
239                 FinderCache.putResult(finderClassName, finderMethodName,
240                     finderParams, finderArgs, list);
241 
242                 return list;
243             }
244             catch (Exception e) {
245                 throw HibernateUtil.processException(e);
246             }
247             finally {
248                 closeSession(session);
249             }
250         }
251         else {
252             return (List)result;
253         }
254     }
255 
256     public List findByCompanyId(long companyId, int begin, int end)
257         throws SystemException {
258         return findByCompanyId(companyId, begin, end, null);
259     }
260 
261     public List findByCompanyId(long companyId, int begin, int end,
262         OrderByComparator obc) throws SystemException {
263         String finderClassName = UserGroup.class.getName();
264         String finderMethodName = "findByCompanyId";
265         String[] finderParams = new String[] {
266                 Long.class.getName(), "java.lang.Integer", "java.lang.Integer",
267                 "com.liferay.portal.kernel.util.OrderByComparator"
268             };
269         Object[] finderArgs = new Object[] {
270                 new Long(companyId), String.valueOf(begin), String.valueOf(end),
271                 String.valueOf(obc)
272             };
273         Object result = FinderCache.getResult(finderClassName,
274                 finderMethodName, finderParams, finderArgs, getSessionFactory());
275 
276         if (result == null) {
277             Session session = null;
278 
279             try {
280                 session = openSession();
281 
282                 StringMaker query = new StringMaker();
283                 query.append("FROM com.liferay.portal.model.UserGroup WHERE ");
284                 query.append("companyId = ?");
285                 query.append(" ");
286 
287                 if (obc != null) {
288                     query.append("ORDER BY ");
289                     query.append(obc.getOrderBy());
290                 }
291                 else {
292                     query.append("ORDER BY ");
293                     query.append("name ASC");
294                 }
295 
296                 Query q = session.createQuery(query.toString());
297                 int queryPos = 0;
298                 q.setLong(queryPos++, companyId);
299 
300                 List list = QueryUtil.list(q, getDialect(), begin, end);
301                 FinderCache.putResult(finderClassName, finderMethodName,
302                     finderParams, finderArgs, list);
303 
304                 return list;
305             }
306             catch (Exception e) {
307                 throw HibernateUtil.processException(e);
308             }
309             finally {
310                 closeSession(session);
311             }
312         }
313         else {
314             return (List)result;
315         }
316     }
317 
318     public UserGroup findByCompanyId_First(long companyId, OrderByComparator obc)
319         throws NoSuchUserGroupException, SystemException {
320         List list = findByCompanyId(companyId, 0, 1, obc);
321 
322         if (list.size() == 0) {
323             StringMaker msg = new StringMaker();
324             msg.append("No UserGroup exists with the key ");
325             msg.append(StringPool.OPEN_CURLY_BRACE);
326             msg.append("companyId=");
327             msg.append(companyId);
328             msg.append(StringPool.CLOSE_CURLY_BRACE);
329             throw new NoSuchUserGroupException(msg.toString());
330         }
331         else {
332             return (UserGroup)list.get(0);
333         }
334     }
335 
336     public UserGroup findByCompanyId_Last(long companyId, OrderByComparator obc)
337         throws NoSuchUserGroupException, SystemException {
338         int count = countByCompanyId(companyId);
339         List list = findByCompanyId(companyId, count - 1, count, obc);
340 
341         if (list.size() == 0) {
342             StringMaker msg = new StringMaker();
343             msg.append("No UserGroup exists with the key ");
344             msg.append(StringPool.OPEN_CURLY_BRACE);
345             msg.append("companyId=");
346             msg.append(companyId);
347             msg.append(StringPool.CLOSE_CURLY_BRACE);
348             throw new NoSuchUserGroupException(msg.toString());
349         }
350         else {
351             return (UserGroup)list.get(0);
352         }
353     }
354 
355     public UserGroup[] findByCompanyId_PrevAndNext(long userGroupId,
356         long companyId, OrderByComparator obc)
357         throws NoSuchUserGroupException, SystemException {
358         UserGroup userGroup = findByPrimaryKey(userGroupId);
359         int count = countByCompanyId(companyId);
360         Session session = null;
361 
362         try {
363             session = openSession();
364 
365             StringMaker query = new StringMaker();
366             query.append("FROM com.liferay.portal.model.UserGroup WHERE ");
367             query.append("companyId = ?");
368             query.append(" ");
369 
370             if (obc != null) {
371                 query.append("ORDER BY ");
372                 query.append(obc.getOrderBy());
373             }
374             else {
375                 query.append("ORDER BY ");
376                 query.append("name ASC");
377             }
378 
379             Query q = session.createQuery(query.toString());
380             int queryPos = 0;
381             q.setLong(queryPos++, companyId);
382 
383             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
384                     userGroup);
385             UserGroup[] array = new UserGroupImpl[3];
386             array[0] = (UserGroup)objArray[0];
387             array[1] = (UserGroup)objArray[1];
388             array[2] = (UserGroup)objArray[2];
389 
390             return array;
391         }
392         catch (Exception e) {
393             throw HibernateUtil.processException(e);
394         }
395         finally {
396             closeSession(session);
397         }
398     }
399 
400     public List findByC_P(long companyId, long parentUserGroupId)
401         throws SystemException {
402         String finderClassName = UserGroup.class.getName();
403         String finderMethodName = "findByC_P";
404         String[] finderParams = new String[] {
405                 Long.class.getName(), Long.class.getName()
406             };
407         Object[] finderArgs = new Object[] {
408                 new Long(companyId), new Long(parentUserGroupId)
409             };
410         Object result = FinderCache.getResult(finderClassName,
411                 finderMethodName, finderParams, finderArgs, getSessionFactory());
412 
413         if (result == null) {
414             Session session = null;
415 
416             try {
417                 session = openSession();
418 
419                 StringMaker query = new StringMaker();
420                 query.append("FROM com.liferay.portal.model.UserGroup WHERE ");
421                 query.append("companyId = ?");
422                 query.append(" AND ");
423                 query.append("parentUserGroupId = ?");
424                 query.append(" ");
425                 query.append("ORDER BY ");
426                 query.append("name ASC");
427 
428                 Query q = session.createQuery(query.toString());
429                 int queryPos = 0;
430                 q.setLong(queryPos++, companyId);
431                 q.setLong(queryPos++, parentUserGroupId);
432 
433                 List list = q.list();
434                 FinderCache.putResult(finderClassName, finderMethodName,
435                     finderParams, finderArgs, list);
436 
437                 return list;
438             }
439             catch (Exception e) {
440                 throw HibernateUtil.processException(e);
441             }
442             finally {
443                 closeSession(session);
444             }
445         }
446         else {
447             return (List)result;
448         }
449     }
450 
451     public List findByC_P(long companyId, long parentUserGroupId, int begin,
452         int end) throws SystemException {
453         return findByC_P(companyId, parentUserGroupId, begin, end, null);
454     }
455 
456     public List findByC_P(long companyId, long parentUserGroupId, int begin,
457         int end, OrderByComparator obc) throws SystemException {
458         String finderClassName = UserGroup.class.getName();
459         String finderMethodName = "findByC_P";
460         String[] finderParams = new String[] {
461                 Long.class.getName(), Long.class.getName(), "java.lang.Integer",
462                 "java.lang.Integer",
463                 "com.liferay.portal.kernel.util.OrderByComparator"
464             };
465         Object[] finderArgs = new Object[] {
466                 new Long(companyId), new Long(parentUserGroupId),
467                 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
468             };
469         Object result = FinderCache.getResult(finderClassName,
470                 finderMethodName, finderParams, finderArgs, getSessionFactory());
471 
472         if (result == null) {
473             Session session = null;
474 
475             try {
476                 session = openSession();
477 
478                 StringMaker query = new StringMaker();
479                 query.append("FROM com.liferay.portal.model.UserGroup WHERE ");
480                 query.append("companyId = ?");
481                 query.append(" AND ");
482                 query.append("parentUserGroupId = ?");
483                 query.append(" ");
484 
485                 if (obc != null) {
486                     query.append("ORDER BY ");
487                     query.append(obc.getOrderBy());
488                 }
489                 else {
490                     query.append("ORDER BY ");
491                     query.append("name ASC");
492                 }
493 
494                 Query q = session.createQuery(query.toString());
495                 int queryPos = 0;
496                 q.setLong(queryPos++, companyId);
497                 q.setLong(queryPos++, parentUserGroupId);
498 
499                 List list = QueryUtil.list(q, getDialect(), begin, end);
500                 FinderCache.putResult(finderClassName, finderMethodName,
501                     finderParams, finderArgs, list);
502 
503                 return list;
504             }
505             catch (Exception e) {
506                 throw HibernateUtil.processException(e);
507             }
508             finally {
509                 closeSession(session);
510             }
511         }
512         else {
513             return (List)result;
514         }
515     }
516 
517     public UserGroup findByC_P_First(long companyId, long parentUserGroupId,
518         OrderByComparator obc) throws NoSuchUserGroupException, SystemException {
519         List list = findByC_P(companyId, parentUserGroupId, 0, 1, obc);
520 
521         if (list.size() == 0) {
522             StringMaker msg = new StringMaker();
523             msg.append("No UserGroup exists with the key ");
524             msg.append(StringPool.OPEN_CURLY_BRACE);
525             msg.append("companyId=");
526             msg.append(companyId);
527             msg.append(", ");
528             msg.append("parentUserGroupId=");
529             msg.append(parentUserGroupId);
530             msg.append(StringPool.CLOSE_CURLY_BRACE);
531             throw new NoSuchUserGroupException(msg.toString());
532         }
533         else {
534             return (UserGroup)list.get(0);
535         }
536     }
537 
538     public UserGroup findByC_P_Last(long companyId, long parentUserGroupId,
539         OrderByComparator obc) throws NoSuchUserGroupException, SystemException {
540         int count = countByC_P(companyId, parentUserGroupId);
541         List list = findByC_P(companyId, parentUserGroupId, count - 1, count,
542                 obc);
543 
544         if (list.size() == 0) {
545             StringMaker msg = new StringMaker();
546             msg.append("No UserGroup exists with the key ");
547             msg.append(StringPool.OPEN_CURLY_BRACE);
548             msg.append("companyId=");
549             msg.append(companyId);
550             msg.append(", ");
551             msg.append("parentUserGroupId=");
552             msg.append(parentUserGroupId);
553             msg.append(StringPool.CLOSE_CURLY_BRACE);
554             throw new NoSuchUserGroupException(msg.toString());
555         }
556         else {
557             return (UserGroup)list.get(0);
558         }
559     }
560 
561     public UserGroup[] findByC_P_PrevAndNext(long userGroupId, long companyId,
562         long parentUserGroupId, OrderByComparator obc)
563         throws NoSuchUserGroupException, SystemException {
564         UserGroup userGroup = findByPrimaryKey(userGroupId);
565         int count = countByC_P(companyId, parentUserGroupId);
566         Session session = null;
567 
568         try {
569             session = openSession();
570 
571             StringMaker query = new StringMaker();
572             query.append("FROM com.liferay.portal.model.UserGroup WHERE ");
573             query.append("companyId = ?");
574             query.append(" AND ");
575             query.append("parentUserGroupId = ?");
576             query.append(" ");
577 
578             if (obc != null) {
579                 query.append("ORDER BY ");
580                 query.append(obc.getOrderBy());
581             }
582             else {
583                 query.append("ORDER BY ");
584                 query.append("name ASC");
585             }
586 
587             Query q = session.createQuery(query.toString());
588             int queryPos = 0;
589             q.setLong(queryPos++, companyId);
590             q.setLong(queryPos++, parentUserGroupId);
591 
592             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
593                     userGroup);
594             UserGroup[] array = new UserGroupImpl[3];
595             array[0] = (UserGroup)objArray[0];
596             array[1] = (UserGroup)objArray[1];
597             array[2] = (UserGroup)objArray[2];
598 
599             return array;
600         }
601         catch (Exception e) {
602             throw HibernateUtil.processException(e);
603         }
604         finally {
605             closeSession(session);
606         }
607     }
608 
609     public UserGroup findByC_N(long companyId, String name)
610         throws NoSuchUserGroupException, SystemException {
611         UserGroup userGroup = fetchByC_N(companyId, name);
612 
613         if (userGroup == null) {
614             StringMaker msg = new StringMaker();
615             msg.append("No UserGroup exists with the key ");
616             msg.append(StringPool.OPEN_CURLY_BRACE);
617             msg.append("companyId=");
618             msg.append(companyId);
619             msg.append(", ");
620             msg.append("name=");
621             msg.append(name);
622             msg.append(StringPool.CLOSE_CURLY_BRACE);
623 
624             if (_log.isWarnEnabled()) {
625                 _log.warn(msg.toString());
626             }
627 
628             throw new NoSuchUserGroupException(msg.toString());
629         }
630 
631         return userGroup;
632     }
633 
634     public UserGroup fetchByC_N(long companyId, String name)
635         throws SystemException {
636         String finderClassName = UserGroup.class.getName();
637         String finderMethodName = "fetchByC_N";
638         String[] finderParams = new String[] {
639                 Long.class.getName(), String.class.getName()
640             };
641         Object[] finderArgs = new Object[] { new Long(companyId), name };
642         Object result = FinderCache.getResult(finderClassName,
643                 finderMethodName, finderParams, finderArgs, getSessionFactory());
644 
645         if (result == null) {
646             Session session = null;
647 
648             try {
649                 session = openSession();
650 
651                 StringMaker query = new StringMaker();
652                 query.append("FROM com.liferay.portal.model.UserGroup WHERE ");
653                 query.append("companyId = ?");
654                 query.append(" AND ");
655 
656                 if (name == null) {
657                     query.append("name IS NULL");
658                 }
659                 else {
660                     query.append("name = ?");
661                 }
662 
663                 query.append(" ");
664                 query.append("ORDER BY ");
665                 query.append("name ASC");
666 
667                 Query q = session.createQuery(query.toString());
668                 int queryPos = 0;
669                 q.setLong(queryPos++, companyId);
670 
671                 if (name != null) {
672                     q.setString(queryPos++, name);
673                 }
674 
675                 List list = q.list();
676                 FinderCache.putResult(finderClassName, finderMethodName,
677                     finderParams, finderArgs, list);
678 
679                 if (list.size() == 0) {
680                     return null;
681                 }
682                 else {
683                     return (UserGroup)list.get(0);
684                 }
685             }
686             catch (Exception e) {
687                 throw HibernateUtil.processException(e);
688             }
689             finally {
690                 closeSession(session);
691             }
692         }
693         else {
694             List list = (List)result;
695 
696             if (list.size() == 0) {
697                 return null;
698             }
699             else {
700                 return (UserGroup)list.get(0);
701             }
702         }
703     }
704 
705     public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer)
706         throws SystemException {
707         Session session = null;
708 
709         try {
710             session = openSession();
711 
712             DynamicQuery query = queryInitializer.initialize(session);
713 
714             return query.list();
715         }
716         catch (Exception e) {
717             throw HibernateUtil.processException(e);
718         }
719         finally {
720             closeSession(session);
721         }
722     }
723 
724     public List findWithDynamicQuery(DynamicQueryInitializer queryInitializer,
725         int begin, int end) throws SystemException {
726         Session session = null;
727 
728         try {
729             session = openSession();
730 
731             DynamicQuery query = queryInitializer.initialize(session);
732             query.setLimit(begin, end);
733 
734             return query.list();
735         }
736         catch (Exception e) {
737             throw HibernateUtil.processException(e);
738         }
739         finally {
740             closeSession(session);
741         }
742     }
743 
744     public List findAll() throws SystemException {
745         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
746     }
747 
748     public List findAll(int begin, int end) throws SystemException {
749         return findAll(begin, end, null);
750     }
751 
752     public List findAll(int begin, int end, OrderByComparator obc)
753         throws SystemException {
754         String finderClassName = UserGroup.class.getName();
755         String finderMethodName = "findAll";
756         String[] finderParams = new String[] {
757                 "java.lang.Integer", "java.lang.Integer",
758                 "com.liferay.portal.kernel.util.OrderByComparator"
759             };
760         Object[] finderArgs = new Object[] {
761                 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
762             };
763         Object result = FinderCache.getResult(finderClassName,
764                 finderMethodName, finderParams, finderArgs, getSessionFactory());
765 
766         if (result == null) {
767             Session session = null;
768 
769             try {
770                 session = openSession();
771 
772                 StringMaker query = new StringMaker();
773                 query.append("FROM com.liferay.portal.model.UserGroup ");
774 
775                 if (obc != null) {
776                     query.append("ORDER BY ");
777                     query.append(obc.getOrderBy());
778                 }
779                 else {
780                     query.append("ORDER BY ");
781                     query.append("name ASC");
782                 }
783 
784                 Query q = session.createQuery(query.toString());
785                 List list = QueryUtil.list(q, getDialect(), begin, end);
786 
787                 if (obc == null) {
788                     Collections.sort(list);
789                 }
790 
791                 FinderCache.putResult(finderClassName, finderMethodName,
792                     finderParams, finderArgs, list);
793 
794                 return list;
795             }
796             catch (Exception e) {
797                 throw HibernateUtil.processException(e);
798             }
799             finally {
800                 closeSession(session);
801             }
802         }
803         else {
804             return (List)result;
805         }
806     }
807 
808     public void removeByCompanyId(long companyId) throws SystemException {
809         Iterator itr = findByCompanyId(companyId).iterator();
810 
811         while (itr.hasNext()) {
812             UserGroup userGroup = (UserGroup)itr.next();
813             remove(userGroup);
814         }
815     }
816 
817     public void removeByC_P(long companyId, long parentUserGroupId)
818         throws SystemException {
819         Iterator itr = findByC_P(companyId, parentUserGroupId).iterator();
820 
821         while (itr.hasNext()) {
822             UserGroup userGroup = (UserGroup)itr.next();
823             remove(userGroup);
824         }
825     }
826 
827     public void removeByC_N(long companyId, String name)
828         throws NoSuchUserGroupException, SystemException {
829         UserGroup userGroup = findByC_N(companyId, name);
830         remove(userGroup);
831     }
832 
833     public void removeAll() throws SystemException {
834         Iterator itr = findAll().iterator();
835 
836         while (itr.hasNext()) {
837             remove((UserGroup)itr.next());
838         }
839     }
840 
841     public int countByCompanyId(long companyId) throws SystemException {
842         String finderClassName = UserGroup.class.getName();
843         String finderMethodName = "countByCompanyId";
844         String[] finderParams = new String[] { Long.class.getName() };
845         Object[] finderArgs = new Object[] { new Long(companyId) };
846         Object result = FinderCache.getResult(finderClassName,
847                 finderMethodName, finderParams, finderArgs, getSessionFactory());
848 
849         if (result == null) {
850             Session session = null;
851 
852             try {
853                 session = openSession();
854 
855                 StringMaker query = new StringMaker();
856                 query.append("SELECT COUNT(*) ");
857                 query.append("FROM com.liferay.portal.model.UserGroup WHERE ");
858                 query.append("companyId = ?");
859                 query.append(" ");
860 
861                 Query q = session.createQuery(query.toString());
862                 int queryPos = 0;
863                 q.setLong(queryPos++, companyId);
864 
865                 Long count = null;
866                 Iterator itr = q.list().iterator();
867 
868                 if (itr.hasNext()) {
869                     count = (Long)itr.next();
870                 }
871 
872                 if (count == null) {
873                     count = new Long(0);
874                 }
875 
876                 FinderCache.putResult(finderClassName, finderMethodName,
877                     finderParams, finderArgs, count);
878 
879                 return count.intValue();
880             }
881             catch (Exception e) {
882                 throw HibernateUtil.processException(e);
883             }
884             finally {
885                 closeSession(session);
886             }
887         }
888         else {
889             return ((Long)result).intValue();
890         }
891     }
892 
893     public int countByC_P(long companyId, long parentUserGroupId)
894         throws SystemException {
895         String finderClassName = UserGroup.class.getName();
896         String finderMethodName = "countByC_P";
897         String[] finderParams = new String[] {
898                 Long.class.getName(), Long.class.getName()
899             };
900         Object[] finderArgs = new Object[] {
901                 new Long(companyId), new Long(parentUserGroupId)
902             };
903         Object result = FinderCache.getResult(finderClassName,
904                 finderMethodName, finderParams, finderArgs, getSessionFactory());
905 
906         if (result == null) {
907             Session session = null;
908 
909             try {
910                 session = openSession();
911 
912                 StringMaker query = new StringMaker();
913                 query.append("SELECT COUNT(*) ");
914                 query.append("FROM com.liferay.portal.model.UserGroup WHERE ");
915                 query.append("companyId = ?");
916                 query.append(" AND ");
917                 query.append("parentUserGroupId = ?");
918                 query.append(" ");
919 
920                 Query q = session.createQuery(query.toString());
921                 int queryPos = 0;
922                 q.setLong(queryPos++, companyId);
923                 q.setLong(queryPos++, parentUserGroupId);
924 
925                 Long count = null;
926                 Iterator itr = q.list().iterator();
927 
928                 if (itr.hasNext()) {
929                     count = (Long)itr.next();
930                 }
931 
932                 if (count == null) {
933                     count = new Long(0);
934                 }
935 
936                 FinderCache.putResult(finderClassName, finderMethodName,
937                     finderParams, finderArgs, count);
938 
939                 return count.intValue();
940             }
941             catch (Exception e) {
942                 throw HibernateUtil.processException(e);
943             }
944             finally {
945                 closeSession(session);
946             }
947         }
948         else {
949             return ((Long)result).intValue();
950         }
951     }
952 
953     public int countByC_N(long companyId, String name)
954         throws SystemException {
955         String finderClassName = UserGroup.class.getName();
956         String finderMethodName = "countByC_N";
957         String[] finderParams = new String[] {
958                 Long.class.getName(), String.class.getName()
959             };
960         Object[] finderArgs = new Object[] { new Long(companyId), name };
961         Object result = FinderCache.getResult(finderClassName,
962                 finderMethodName, finderParams, finderArgs, getSessionFactory());
963 
964         if (result == null) {
965             Session session = null;
966 
967             try {
968                 session = openSession();
969 
970                 StringMaker query = new StringMaker();
971                 query.append("SELECT COUNT(*) ");
972                 query.append("FROM com.liferay.portal.model.UserGroup WHERE ");
973                 query.append("companyId = ?");
974                 query.append(" AND ");
975 
976                 if (name == null) {
977                     query.append("name IS NULL");
978                 }
979                 else {
980                     query.append("name = ?");
981                 }
982 
983                 query.append(" ");
984 
985                 Query q = session.createQuery(query.toString());
986                 int queryPos = 0;
987                 q.setLong(queryPos++, companyId);
988 
989                 if (name != null) {
990                     q.setString(queryPos++, name);
991                 }
992 
993                 Long count = null;
994                 Iterator itr = q.list().iterator();
995 
996                 if (itr.hasNext()) {
997                     count = (Long)itr.next();
998                 }
999 
1000                if (count == null) {
1001                    count = new Long(0);
1002                }
1003
1004                FinderCache.putResult(finderClassName, finderMethodName,
1005                    finderParams, finderArgs, count);
1006
1007                return count.intValue();
1008            }
1009            catch (Exception e) {
1010                throw HibernateUtil.processException(e);
1011            }
1012            finally {
1013                closeSession(session);
1014            }
1015        }
1016        else {
1017            return ((Long)result).intValue();
1018        }
1019    }
1020
1021    public int countAll() throws SystemException {
1022        String finderClassName = UserGroup.class.getName();
1023        String finderMethodName = "countAll";
1024        String[] finderParams = new String[] {  };
1025        Object[] finderArgs = new Object[] {  };
1026        Object result = FinderCache.getResult(finderClassName,
1027                finderMethodName, finderParams, finderArgs, getSessionFactory());
1028
1029        if (result == null) {
1030            Session session = null;
1031
1032            try {
1033                session = openSession();
1034
1035                StringMaker query = new StringMaker();
1036                query.append("SELECT COUNT(*) ");
1037                query.append("FROM com.liferay.portal.model.UserGroup");
1038
1039                Query q = session.createQuery(query.toString());
1040                Long count = null;
1041                Iterator itr = q.list().iterator();
1042
1043                if (itr.hasNext()) {
1044                    count = (Long)itr.next();
1045                }
1046
1047                if (count == null) {
1048                    count = new Long(0);
1049                }
1050
1051                FinderCache.putResult(finderClassName, finderMethodName,
1052                    finderParams, finderArgs, count);
1053
1054                return count.intValue();
1055            }
1056            catch (Exception e) {
1057                throw HibernateUtil.processException(e);
1058            }
1059            finally {
1060                closeSession(session);
1061            }
1062        }
1063        else {
1064            return ((Long)result).intValue();
1065        }
1066    }
1067
1068    public List getUsers(long pk)
1069        throws NoSuchUserGroupException, SystemException {
1070        return getUsers(pk, QueryUtil.ALL_POS, QueryUtil.ALL_POS);
1071    }
1072
1073    public List getUsers(long pk, int begin, int end)
1074        throws NoSuchUserGroupException, SystemException {
1075        return getUsers(pk, begin, end, null);
1076    }
1077
1078    public List getUsers(long pk, int begin, int end, OrderByComparator obc)
1079        throws NoSuchUserGroupException, SystemException {
1080        String finderClassName = "Users_UserGroups";
1081        String finderMethodName = "getUsers";
1082        String[] finderParams = new String[] {
1083                Long.class.getName(), "java.lang.Integer", "java.lang.Integer",
1084                "com.liferay.portal.kernel.util.OrderByComparator"
1085            };
1086        Object[] finderArgs = new Object[] {
1087                new Long(pk), String.valueOf(begin), String.valueOf(end),
1088                String.valueOf(obc)
1089            };
1090        Object result = FinderCache.getResult(finderClassName,
1091                finderMethodName, finderParams, finderArgs, getSessionFactory());
1092
1093        if (result == null) {
1094            Session session = null;
1095
1096            try {
1097                session = HibernateUtil.openSession();
1098
1099                StringMaker sm = new StringMaker();
1100                sm.append(_SQL_GETUSERS);
1101
1102                if (obc != null) {
1103                    sm.append("ORDER BY ");
1104                    sm.append(obc.getOrderBy());
1105                }
1106
1107                String sql = sm.toString();
1108                SQLQuery q = session.createSQLQuery(sql);
1109                q.addEntity("User_",
1110                    com.liferay.portal.model.impl.UserImpl.class);
1111
1112                QueryPos qPos = QueryPos.getInstance(q);
1113                qPos.add(pk);
1114
1115                List list = QueryUtil.list(q, getDialect(), begin, end);
1116                FinderCache.putResult(finderClassName, finderMethodName,
1117                    finderParams, finderArgs, list);
1118
1119                return list;
1120            }
1121            catch (Exception e) {
1122                throw new SystemException(e);
1123            }
1124            finally {
1125                closeSession(session);
1126            }
1127        }
1128        else {
1129            return (List)result;
1130        }
1131    }
1132
1133    public int getUsersSize(long pk) throws SystemException {
1134        String finderClassName = "Users_UserGroups";
1135        String finderMethodName = "getUsersSize";
1136        String[] finderParams = new String[] { Long.class.getName() };
1137        Object[] finderArgs = new Object[] { new Long(pk) };
1138        Object result = FinderCache.getResult(finderClassName,
1139                finderMethodName, finderParams, finderArgs, getSessionFactory());
1140
1141        if (result == null) {
1142            Session session = null;
1143
1144            try {
1145                session = openSession();
1146
1147                SQLQuery q = session.createSQLQuery(_SQL_GETUSERSSIZE);
1148                q.addScalar(HibernateUtil.getCountColumnName(), Hibernate.LONG);
1149
1150                QueryPos qPos = QueryPos.getInstance(q);
1151                qPos.add(pk);
1152
1153                Long count = null;
1154                Iterator itr = q.list().iterator();
1155
1156                if (itr.hasNext()) {
1157                    count = (Long)itr.next();
1158                }
1159
1160                if (count == null) {
1161                    count = new Long(0);
1162                }
1163
1164                FinderCache.putResult(finderClassName, finderMethodName,
1165                    finderParams, finderArgs, count);
1166
1167                return count.intValue();
1168            }
1169            catch (Exception e) {
1170                throw HibernateUtil.processException(e);
1171            }
1172            finally {
1173                closeSession(session);
1174            }
1175        }
1176        else {
1177            return ((Long)result).intValue();
1178        }
1179    }
1180
1181    public boolean containsUser(long pk, long userPK) throws SystemException {
1182        String finderClassName = "Users_UserGroups";
1183        String finderMethodName = "containsUsers";
1184        String[] finderParams = new String[] {
1185                Long.class.getName(), Long.class.getName()
1186            };
1187        Object[] finderArgs = new Object[] { new Long(pk), new Long(userPK) };
1188        Object result = FinderCache.getResult(finderClassName,
1189                finderMethodName, finderParams, finderArgs, getSessionFactory());
1190
1191        if (result == null) {
1192            try {
1193                Boolean value = Boolean.valueOf(containsUser.contains(pk, userPK));
1194                FinderCache.putResult(finderClassName, finderMethodName,
1195                    finderParams, finderArgs, value);
1196
1197                return value.booleanValue();
1198            }
1199            catch (DataAccessException dae) {
1200                throw new SystemException(dae);
1201            }
1202        }
1203        else {
1204            return ((Boolean)result).booleanValue();
1205        }
1206    }
1207
1208    public boolean containsUsers(long pk) throws SystemException {
1209        if (getUsersSize(pk) > 0) {
1210            return true;
1211        }
1212        else {
1213            return false;
1214        }
1215    }
1216
1217    public void addUser(long pk, long userPK)
1218        throws NoSuchUserGroupException, com.liferay.portal.NoSuchUserException, 
1219            SystemException {
1220        try {
1221            addUser.add(pk, userPK);
1222        }
1223        catch (DataAccessException dae) {
1224            throw new SystemException(dae);
1225        }
1226        finally {
1227            FinderCache.clearCache("Users_UserGroups");
1228        }
1229    }
1230
1231    public void addUser(long pk, com.liferay.portal.model.User user)
1232        throws NoSuchUserGroupException, com.liferay.portal.NoSuchUserException, 
1233            SystemException {
1234        try {
1235            addUser.add(pk, user.getPrimaryKey());
1236        }
1237        catch (DataAccessException dae) {
1238            throw new SystemException(dae);
1239        }
1240        finally {
1241            FinderCache.clearCache("Users_UserGroups");
1242        }
1243    }
1244
1245    public void addUsers(long pk, long[] userPKs)
1246        throws NoSuchUserGroupException, com.liferay.portal.NoSuchUserException, 
1247            SystemException {
1248        try {
1249            for (int i = 0; i < userPKs.length; i++) {
1250                addUser.add(pk, userPKs[i]);
1251            }
1252        }
1253        catch (DataAccessException dae) {
1254            throw new SystemException(dae);
1255        }
1256        finally {
1257            FinderCache.clearCache("Users_UserGroups");
1258        }
1259    }
1260
1261    public void addUsers(long pk, List users)
1262        throws NoSuchUserGroupException, com.liferay.portal.NoSuchUserException, 
1263            SystemException {
1264        try {
1265            for (int i = 0; i < users.size(); i++) {
1266                com.liferay.portal.model.User user = (com.liferay.portal.model.User)users.get(i);
1267                addUser.add(pk, user.getPrimaryKey());
1268            }
1269        }
1270        catch (DataAccessException dae) {
1271            throw new SystemException(dae);
1272        }
1273        finally {
1274            FinderCache.clearCache("Users_UserGroups");
1275        }
1276    }
1277
1278    public void clearUsers(long pk)
1279        throws NoSuchUserGroupException, SystemException {
1280        try {
1281            clearUsers.clear(pk);
1282        }
1283        catch (DataAccessException dae) {
1284            throw new SystemException(dae);
1285        }
1286        finally {
1287            FinderCache.clearCache("Users_UserGroups");
1288        }
1289    }
1290
1291    public void removeUser(long pk, long userPK)
1292        throws NoSuchUserGroupException, com.liferay.portal.NoSuchUserException, 
1293            SystemException {
1294        try {
1295            removeUser.remove(pk, userPK);
1296        }
1297        catch (DataAccessException dae) {
1298            throw new SystemException(dae);
1299        }
1300        finally {
1301            FinderCache.clearCache("Users_UserGroups");
1302        }
1303    }
1304
1305    public void removeUser(long pk, com.liferay.portal.model.User user)
1306        throws NoSuchUserGroupException, com.liferay.portal.NoSuchUserException, 
1307            SystemException {
1308        try {
1309            removeUser.remove(pk, user.getPrimaryKey());
1310        }
1311        catch (DataAccessException dae) {
1312            throw new SystemException(dae);
1313        }
1314        finally {
1315            FinderCache.clearCache("Users_UserGroups");
1316        }
1317    }
1318
1319    public void removeUsers(long pk, long[] userPKs)
1320        throws NoSuchUserGroupException, com.liferay.portal.NoSuchUserException, 
1321            SystemException {
1322        try {
1323            for (int i = 0; i < userPKs.length; i++) {
1324                removeUser.remove(pk, userPKs[i]);
1325            }
1326        }
1327        catch (DataAccessException dae) {
1328            throw new SystemException(dae);
1329        }
1330        finally {
1331            FinderCache.clearCache("Users_UserGroups");
1332        }
1333    }
1334
1335    public void removeUsers(long pk, List users)
1336        throws NoSuchUserGroupException, com.liferay.portal.NoSuchUserException, 
1337            SystemException {
1338        try {
1339            for (int i = 0; i < users.size(); i++) {
1340                com.liferay.portal.model.User user = (com.liferay.portal.model.User)users.get(i);
1341                removeUser.remove(pk, user.getPrimaryKey());
1342            }
1343        }
1344        catch (DataAccessException dae) {
1345            throw new SystemException(dae);
1346        }
1347        finally {
1348            FinderCache.clearCache("Users_UserGroups");
1349        }
1350    }
1351
1352    public void setUsers(long pk, long[] userPKs)
1353        throws NoSuchUserGroupException, com.liferay.portal.NoSuchUserException, 
1354            SystemException {
1355        try {
1356            clearUsers.clear(pk);
1357
1358            for (int i = 0; i < userPKs.length; i++) {
1359                addUser.add(pk, userPKs[i]);
1360            }
1361        }
1362        catch (DataAccessException dae) {
1363            throw new SystemException(dae);
1364        }
1365        finally {
1366            FinderCache.clearCache("Users_UserGroups");
1367        }
1368    }
1369
1370    public void setUsers(long pk, List users)
1371        throws NoSuchUserGroupException, com.liferay.portal.NoSuchUserException, 
1372            SystemException {
1373        try {
1374            clearUsers.clear(pk);
1375
1376            for (int i = 0; i < users.size(); i++) {
1377                com.liferay.portal.model.User user = (com.liferay.portal.model.User)users.get(i);
1378                addUser.add(pk, user.getPrimaryKey());
1379            }
1380        }
1381        catch (DataAccessException dae) {
1382            throw new SystemException(dae);
1383        }
1384        finally {
1385            FinderCache.clearCache("Users_UserGroups");
1386        }
1387    }
1388
1389    protected void initDao() {
1390        containsUser = new ContainsUser(this);
1391        addUser = new AddUser(this);
1392        clearUsers = new ClearUsers(this);
1393        removeUser = new RemoveUser(this);
1394    }
1395
1396    protected ContainsUser containsUser;
1397    protected AddUser addUser;
1398    protected ClearUsers clearUsers;
1399    protected RemoveUser removeUser;
1400
1401    protected class ContainsUser extends MappingSqlQuery {
1402        protected ContainsUser(UserGroupPersistenceImpl persistenceImpl) {
1403            super(persistenceImpl.getDataSource(), _SQL_CONTAINSUSER);
1404            declareParameter(new SqlParameter(Types.BIGINT));
1405            declareParameter(new SqlParameter(Types.BIGINT));
1406            compile();
1407        }
1408
1409        protected Object mapRow(ResultSet rs, int rowNumber)
1410            throws SQLException {
1411            return new Integer(rs.getInt("COUNT_VALUE"));
1412        }
1413
1414        protected boolean contains(long userGroupId, long userId) {
1415            List results = execute(new Object[] {
1416                        new Long(userGroupId), new Long(userId)
1417                    });
1418
1419            if (results.size() > 0) {
1420                Integer count = (Integer)results.get(0);
1421
1422                if (count.intValue() > 0) {
1423                    return true;
1424                }
1425            }
1426
1427            return false;
1428        }
1429    }
1430
1431    protected class AddUser extends SqlUpdate {
1432        protected AddUser(UserGroupPersistenceImpl persistenceImpl) {
1433            super(persistenceImpl.getDataSource(),
1434                "INSERT INTO Users_UserGroups (userGroupId, userId) VALUES (?, ?)");
1435            _persistenceImpl = persistenceImpl;
1436            declareParameter(new SqlParameter(Types.BIGINT));
1437            declareParameter(new SqlParameter(Types.BIGINT));
1438            compile();
1439        }
1440
1441        protected void add(long userGroupId, long userId) {
1442            if (!_persistenceImpl.containsUser.contains(userGroupId, userId)) {
1443                update(new Object[] { new Long(userGroupId), new Long(userId) });
1444            }
1445        }
1446
1447        private UserGroupPersistenceImpl _persistenceImpl;
1448    }
1449
1450    protected class ClearUsers extends SqlUpdate {
1451        protected ClearUsers(UserGroupPersistenceImpl persistenceImpl) {
1452            super(persistenceImpl.getDataSource(),
1453                "DELETE FROM Users_UserGroups WHERE userGroupId = ?");
1454            declareParameter(new SqlParameter(Types.BIGINT));
1455            compile();
1456        }
1457
1458        protected void clear(long userGroupId) {
1459            update(new Object[] { new Long(userGroupId) });
1460        }
1461    }
1462
1463    protected class RemoveUser extends SqlUpdate {
1464        protected RemoveUser(UserGroupPersistenceImpl persistenceImpl) {
1465            super(persistenceImpl.getDataSource(),
1466                "DELETE FROM Users_UserGroups WHERE userGroupId = ? AND userId = ?");
1467            declareParameter(new SqlParameter(Types.BIGINT));
1468            declareParameter(new SqlParameter(Types.BIGINT));
1469            compile();
1470        }
1471
1472        protected void remove(long userGroupId, long userId) {
1473            update(new Object[] { new Long(userGroupId), new Long(userId) });
1474        }
1475    }
1476
1477    private static final String _SQL_GETUSERS = "SELECT {User_.*} FROM User_ INNER JOIN Users_UserGroups ON (Users_UserGroups.userId = User_.userId) WHERE (Users_UserGroups.userGroupId = ?)";
1478    private static final String _SQL_GETUSERSSIZE = "SELECT COUNT(*) AS COUNT_VALUE FROM Users_UserGroups WHERE userGroupId = ?";
1479    private static final String _SQL_CONTAINSUSER = "SELECT COUNT(*) AS COUNT_VALUE FROM Users_UserGroups WHERE userGroupId = ? AND userId = ?";
1480    private static Log _log = LogFactory.getLog(UserGroupPersistenceImpl.class);
1481}