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