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