1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.messageboards.service.persistence;
16  
17  import com.liferay.portal.NoSuchModelException;
18  import com.liferay.portal.kernel.annotation.BeanReference;
19  import com.liferay.portal.kernel.cache.CacheRegistry;
20  import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
21  import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
22  import com.liferay.portal.kernel.dao.orm.FinderPath;
23  import com.liferay.portal.kernel.dao.orm.Query;
24  import com.liferay.portal.kernel.dao.orm.QueryPos;
25  import com.liferay.portal.kernel.dao.orm.QueryUtil;
26  import com.liferay.portal.kernel.dao.orm.Session;
27  import com.liferay.portal.kernel.exception.SystemException;
28  import com.liferay.portal.kernel.log.Log;
29  import com.liferay.portal.kernel.log.LogFactoryUtil;
30  import com.liferay.portal.kernel.util.CalendarUtil;
31  import com.liferay.portal.kernel.util.GetterUtil;
32  import com.liferay.portal.kernel.util.InstanceFactory;
33  import com.liferay.portal.kernel.util.OrderByComparator;
34  import com.liferay.portal.kernel.util.StringBundler;
35  import com.liferay.portal.kernel.util.StringPool;
36  import com.liferay.portal.kernel.util.StringUtil;
37  import com.liferay.portal.model.ModelListener;
38  import com.liferay.portal.service.persistence.BatchSessionUtil;
39  import com.liferay.portal.service.persistence.LockPersistence;
40  import com.liferay.portal.service.persistence.ResourcePersistence;
41  import com.liferay.portal.service.persistence.UserPersistence;
42  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
43  
44  import com.liferay.portlet.asset.service.persistence.AssetEntryPersistence;
45  import com.liferay.portlet.messageboards.NoSuchThreadException;
46  import com.liferay.portlet.messageboards.model.MBThread;
47  import com.liferay.portlet.messageboards.model.impl.MBThreadImpl;
48  import com.liferay.portlet.messageboards.model.impl.MBThreadModelImpl;
49  import com.liferay.portlet.ratings.service.persistence.RatingsStatsPersistence;
50  import com.liferay.portlet.social.service.persistence.SocialActivityPersistence;
51  
52  import java.io.Serializable;
53  
54  import java.util.ArrayList;
55  import java.util.Collections;
56  import java.util.Date;
57  import java.util.List;
58  
59  /**
60   * <a href="MBThreadPersistenceImpl.java.html"><b><i>View Source</i></b></a>
61   *
62   * <p>
63   * ServiceBuilder generated this class. Modifications in this class will be
64   * overwritten the next time is generated.
65   * </p>
66   *
67   * @author    Brian Wing Shun Chan
68   * @see       MBThreadPersistence
69   * @see       MBThreadUtil
70   * @generated
71   */
72  public class MBThreadPersistenceImpl extends BasePersistenceImpl<MBThread>
73      implements MBThreadPersistence {
74      public static final String FINDER_CLASS_NAME_ENTITY = MBThreadImpl.class.getName();
75      public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
76          ".List";
77      public static final FinderPath FINDER_PATH_FIND_BY_GROUPID = new FinderPath(MBThreadModelImpl.ENTITY_CACHE_ENABLED,
78              MBThreadModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
79              "findByGroupId",
80              new String[] {
81                  Long.class.getName(),
82                  
83              "java.lang.Integer", "java.lang.Integer",
84                  "com.liferay.portal.kernel.util.OrderByComparator"
85              });
86      public static final FinderPath FINDER_PATH_COUNT_BY_GROUPID = new FinderPath(MBThreadModelImpl.ENTITY_CACHE_ENABLED,
87              MBThreadModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
88              "countByGroupId", new String[] { Long.class.getName() });
89      public static final FinderPath FINDER_PATH_FIND_BY_G_C = new FinderPath(MBThreadModelImpl.ENTITY_CACHE_ENABLED,
90              MBThreadModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
91              "findByG_C",
92              new String[] {
93                  Long.class.getName(), Long.class.getName(),
94                  
95              "java.lang.Integer", "java.lang.Integer",
96                  "com.liferay.portal.kernel.util.OrderByComparator"
97              });
98      public static final FinderPath FINDER_PATH_COUNT_BY_G_C = new FinderPath(MBThreadModelImpl.ENTITY_CACHE_ENABLED,
99              MBThreadModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
100             "countByG_C",
101             new String[] { Long.class.getName(), Long.class.getName() });
102     public static final FinderPath FINDER_PATH_FIND_BY_G_S = new FinderPath(MBThreadModelImpl.ENTITY_CACHE_ENABLED,
103             MBThreadModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
104             "findByG_S",
105             new String[] {
106                 Long.class.getName(), Integer.class.getName(),
107                 
108             "java.lang.Integer", "java.lang.Integer",
109                 "com.liferay.portal.kernel.util.OrderByComparator"
110             });
111     public static final FinderPath FINDER_PATH_COUNT_BY_G_S = new FinderPath(MBThreadModelImpl.ENTITY_CACHE_ENABLED,
112             MBThreadModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
113             "countByG_S",
114             new String[] { Long.class.getName(), Integer.class.getName() });
115     public static final FinderPath FINDER_PATH_FIND_BY_C_P = new FinderPath(MBThreadModelImpl.ENTITY_CACHE_ENABLED,
116             MBThreadModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
117             "findByC_P",
118             new String[] {
119                 Long.class.getName(), Double.class.getName(),
120                 
121             "java.lang.Integer", "java.lang.Integer",
122                 "com.liferay.portal.kernel.util.OrderByComparator"
123             });
124     public static final FinderPath FINDER_PATH_COUNT_BY_C_P = new FinderPath(MBThreadModelImpl.ENTITY_CACHE_ENABLED,
125             MBThreadModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
126             "countByC_P",
127             new String[] { Long.class.getName(), Double.class.getName() });
128     public static final FinderPath FINDER_PATH_FIND_BY_G_C_L = new FinderPath(MBThreadModelImpl.ENTITY_CACHE_ENABLED,
129             MBThreadModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
130             "findByG_C_L",
131             new String[] {
132                 Long.class.getName(), Long.class.getName(), Date.class.getName(),
133                 
134             "java.lang.Integer", "java.lang.Integer",
135                 "com.liferay.portal.kernel.util.OrderByComparator"
136             });
137     public static final FinderPath FINDER_PATH_COUNT_BY_G_C_L = new FinderPath(MBThreadModelImpl.ENTITY_CACHE_ENABLED,
138             MBThreadModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
139             "countByG_C_L",
140             new String[] {
141                 Long.class.getName(), Long.class.getName(), Date.class.getName()
142             });
143     public static final FinderPath FINDER_PATH_FIND_BY_G_C_S = new FinderPath(MBThreadModelImpl.ENTITY_CACHE_ENABLED,
144             MBThreadModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
145             "findByG_C_S",
146             new String[] {
147                 Long.class.getName(), Long.class.getName(),
148                 Integer.class.getName(),
149                 
150             "java.lang.Integer", "java.lang.Integer",
151                 "com.liferay.portal.kernel.util.OrderByComparator"
152             });
153     public static final FinderPath FINDER_PATH_COUNT_BY_G_C_S = new FinderPath(MBThreadModelImpl.ENTITY_CACHE_ENABLED,
154             MBThreadModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
155             "countByG_C_S",
156             new String[] {
157                 Long.class.getName(), Long.class.getName(),
158                 Integer.class.getName()
159             });
160     public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(MBThreadModelImpl.ENTITY_CACHE_ENABLED,
161             MBThreadModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
162             "findAll", new String[0]);
163     public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(MBThreadModelImpl.ENTITY_CACHE_ENABLED,
164             MBThreadModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
165             "countAll", new String[0]);
166 
167     public void cacheResult(MBThread mbThread) {
168         EntityCacheUtil.putResult(MBThreadModelImpl.ENTITY_CACHE_ENABLED,
169             MBThreadImpl.class, mbThread.getPrimaryKey(), mbThread);
170     }
171 
172     public void cacheResult(List<MBThread> mbThreads) {
173         for (MBThread mbThread : mbThreads) {
174             if (EntityCacheUtil.getResult(
175                         MBThreadModelImpl.ENTITY_CACHE_ENABLED,
176                         MBThreadImpl.class, mbThread.getPrimaryKey(), this) == null) {
177                 cacheResult(mbThread);
178             }
179         }
180     }
181 
182     public void clearCache() {
183         CacheRegistry.clear(MBThreadImpl.class.getName());
184         EntityCacheUtil.clearCache(MBThreadImpl.class.getName());
185         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
186         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
187     }
188 
189     public void clearCache(MBThread mbThread) {
190         EntityCacheUtil.removeResult(MBThreadModelImpl.ENTITY_CACHE_ENABLED,
191             MBThreadImpl.class, mbThread.getPrimaryKey());
192     }
193 
194     public MBThread create(long threadId) {
195         MBThread mbThread = new MBThreadImpl();
196 
197         mbThread.setNew(true);
198         mbThread.setPrimaryKey(threadId);
199 
200         return mbThread;
201     }
202 
203     public MBThread remove(Serializable primaryKey)
204         throws NoSuchModelException, SystemException {
205         return remove(((Long)primaryKey).longValue());
206     }
207 
208     public MBThread remove(long threadId)
209         throws NoSuchThreadException, SystemException {
210         Session session = null;
211 
212         try {
213             session = openSession();
214 
215             MBThread mbThread = (MBThread)session.get(MBThreadImpl.class,
216                     new Long(threadId));
217 
218             if (mbThread == null) {
219                 if (_log.isWarnEnabled()) {
220                     _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + threadId);
221                 }
222 
223                 throw new NoSuchThreadException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
224                     threadId);
225             }
226 
227             return remove(mbThread);
228         }
229         catch (NoSuchThreadException nsee) {
230             throw nsee;
231         }
232         catch (Exception e) {
233             throw processException(e);
234         }
235         finally {
236             closeSession(session);
237         }
238     }
239 
240     public MBThread remove(MBThread mbThread) throws SystemException {
241         for (ModelListener<MBThread> listener : listeners) {
242             listener.onBeforeRemove(mbThread);
243         }
244 
245         mbThread = removeImpl(mbThread);
246 
247         for (ModelListener<MBThread> listener : listeners) {
248             listener.onAfterRemove(mbThread);
249         }
250 
251         return mbThread;
252     }
253 
254     protected MBThread removeImpl(MBThread mbThread) throws SystemException {
255         mbThread = toUnwrappedModel(mbThread);
256 
257         Session session = null;
258 
259         try {
260             session = openSession();
261 
262             if (mbThread.isCachedModel() || BatchSessionUtil.isEnabled()) {
263                 Object staleObject = session.get(MBThreadImpl.class,
264                         mbThread.getPrimaryKeyObj());
265 
266                 if (staleObject != null) {
267                     session.evict(staleObject);
268                 }
269             }
270 
271             session.delete(mbThread);
272 
273             session.flush();
274         }
275         catch (Exception e) {
276             throw processException(e);
277         }
278         finally {
279             closeSession(session);
280         }
281 
282         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
283 
284         EntityCacheUtil.removeResult(MBThreadModelImpl.ENTITY_CACHE_ENABLED,
285             MBThreadImpl.class, mbThread.getPrimaryKey());
286 
287         return mbThread;
288     }
289 
290     public MBThread updateImpl(
291         com.liferay.portlet.messageboards.model.MBThread mbThread, boolean merge)
292         throws SystemException {
293         mbThread = toUnwrappedModel(mbThread);
294 
295         Session session = null;
296 
297         try {
298             session = openSession();
299 
300             BatchSessionUtil.update(session, mbThread, merge);
301 
302             mbThread.setNew(false);
303         }
304         catch (Exception e) {
305             throw processException(e);
306         }
307         finally {
308             closeSession(session);
309         }
310 
311         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
312 
313         EntityCacheUtil.putResult(MBThreadModelImpl.ENTITY_CACHE_ENABLED,
314             MBThreadImpl.class, mbThread.getPrimaryKey(), mbThread);
315 
316         return mbThread;
317     }
318 
319     protected MBThread toUnwrappedModel(MBThread mbThread) {
320         if (mbThread instanceof MBThreadImpl) {
321             return mbThread;
322         }
323 
324         MBThreadImpl mbThreadImpl = new MBThreadImpl();
325 
326         mbThreadImpl.setNew(mbThread.isNew());
327         mbThreadImpl.setPrimaryKey(mbThread.getPrimaryKey());
328 
329         mbThreadImpl.setThreadId(mbThread.getThreadId());
330         mbThreadImpl.setGroupId(mbThread.getGroupId());
331         mbThreadImpl.setCategoryId(mbThread.getCategoryId());
332         mbThreadImpl.setRootMessageId(mbThread.getRootMessageId());
333         mbThreadImpl.setMessageCount(mbThread.getMessageCount());
334         mbThreadImpl.setViewCount(mbThread.getViewCount());
335         mbThreadImpl.setLastPostByUserId(mbThread.getLastPostByUserId());
336         mbThreadImpl.setLastPostDate(mbThread.getLastPostDate());
337         mbThreadImpl.setPriority(mbThread.getPriority());
338         mbThreadImpl.setStatus(mbThread.getStatus());
339         mbThreadImpl.setStatusByUserId(mbThread.getStatusByUserId());
340         mbThreadImpl.setStatusByUserName(mbThread.getStatusByUserName());
341         mbThreadImpl.setStatusDate(mbThread.getStatusDate());
342 
343         return mbThreadImpl;
344     }
345 
346     public MBThread findByPrimaryKey(Serializable primaryKey)
347         throws NoSuchModelException, SystemException {
348         return findByPrimaryKey(((Long)primaryKey).longValue());
349     }
350 
351     public MBThread findByPrimaryKey(long threadId)
352         throws NoSuchThreadException, SystemException {
353         MBThread mbThread = fetchByPrimaryKey(threadId);
354 
355         if (mbThread == null) {
356             if (_log.isWarnEnabled()) {
357                 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + threadId);
358             }
359 
360             throw new NoSuchThreadException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
361                 threadId);
362         }
363 
364         return mbThread;
365     }
366 
367     public MBThread fetchByPrimaryKey(Serializable primaryKey)
368         throws SystemException {
369         return fetchByPrimaryKey(((Long)primaryKey).longValue());
370     }
371 
372     public MBThread fetchByPrimaryKey(long threadId) throws SystemException {
373         MBThread mbThread = (MBThread)EntityCacheUtil.getResult(MBThreadModelImpl.ENTITY_CACHE_ENABLED,
374                 MBThreadImpl.class, threadId, this);
375 
376         if (mbThread == null) {
377             Session session = null;
378 
379             try {
380                 session = openSession();
381 
382                 mbThread = (MBThread)session.get(MBThreadImpl.class,
383                         new Long(threadId));
384             }
385             catch (Exception e) {
386                 throw processException(e);
387             }
388             finally {
389                 if (mbThread != null) {
390                     cacheResult(mbThread);
391                 }
392 
393                 closeSession(session);
394             }
395         }
396 
397         return mbThread;
398     }
399 
400     public List<MBThread> findByGroupId(long groupId) throws SystemException {
401         return findByGroupId(groupId, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
402     }
403 
404     public List<MBThread> findByGroupId(long groupId, int start, int end)
405         throws SystemException {
406         return findByGroupId(groupId, start, end, null);
407     }
408 
409     public List<MBThread> findByGroupId(long groupId, int start, int end,
410         OrderByComparator orderByComparator) throws SystemException {
411         Object[] finderArgs = new Object[] {
412                 new Long(groupId),
413                 
414                 String.valueOf(start), String.valueOf(end),
415                 String.valueOf(orderByComparator)
416             };
417 
418         List<MBThread> list = (List<MBThread>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_GROUPID,
419                 finderArgs, this);
420 
421         if (list == null) {
422             Session session = null;
423 
424             try {
425                 session = openSession();
426 
427                 StringBundler query = null;
428 
429                 if (orderByComparator != null) {
430                     query = new StringBundler(3 +
431                             (orderByComparator.getOrderByFields().length * 3));
432                 }
433                 else {
434                     query = new StringBundler(3);
435                 }
436 
437                 query.append(_SQL_SELECT_MBTHREAD_WHERE);
438 
439                 query.append(_FINDER_COLUMN_GROUPID_GROUPID_2);
440 
441                 if (orderByComparator != null) {
442                     appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
443                         orderByComparator);
444                 }
445 
446                 else {
447                     query.append(MBThreadModelImpl.ORDER_BY_JPQL);
448                 }
449 
450                 String sql = query.toString();
451 
452                 Query q = session.createQuery(sql);
453 
454                 QueryPos qPos = QueryPos.getInstance(q);
455 
456                 qPos.add(groupId);
457 
458                 list = (List<MBThread>)QueryUtil.list(q, getDialect(), start,
459                         end);
460             }
461             catch (Exception e) {
462                 throw processException(e);
463             }
464             finally {
465                 if (list == null) {
466                     list = new ArrayList<MBThread>();
467                 }
468 
469                 cacheResult(list);
470 
471                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_GROUPID,
472                     finderArgs, list);
473 
474                 closeSession(session);
475             }
476         }
477 
478         return list;
479     }
480 
481     public MBThread findByGroupId_First(long groupId,
482         OrderByComparator orderByComparator)
483         throws NoSuchThreadException, SystemException {
484         List<MBThread> list = findByGroupId(groupId, 0, 1, orderByComparator);
485 
486         if (list.isEmpty()) {
487             StringBundler msg = new StringBundler(4);
488 
489             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
490 
491             msg.append("groupId=");
492             msg.append(groupId);
493 
494             msg.append(StringPool.CLOSE_CURLY_BRACE);
495 
496             throw new NoSuchThreadException(msg.toString());
497         }
498         else {
499             return list.get(0);
500         }
501     }
502 
503     public MBThread findByGroupId_Last(long groupId,
504         OrderByComparator orderByComparator)
505         throws NoSuchThreadException, SystemException {
506         int count = countByGroupId(groupId);
507 
508         List<MBThread> list = findByGroupId(groupId, count - 1, count,
509                 orderByComparator);
510 
511         if (list.isEmpty()) {
512             StringBundler msg = new StringBundler(4);
513 
514             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
515 
516             msg.append("groupId=");
517             msg.append(groupId);
518 
519             msg.append(StringPool.CLOSE_CURLY_BRACE);
520 
521             throw new NoSuchThreadException(msg.toString());
522         }
523         else {
524             return list.get(0);
525         }
526     }
527 
528     public MBThread[] findByGroupId_PrevAndNext(long threadId, long groupId,
529         OrderByComparator orderByComparator)
530         throws NoSuchThreadException, SystemException {
531         MBThread mbThread = findByPrimaryKey(threadId);
532 
533         Session session = null;
534 
535         try {
536             session = openSession();
537 
538             MBThread[] array = new MBThreadImpl[3];
539 
540             array[0] = getByGroupId_PrevAndNext(session, mbThread, groupId,
541                     orderByComparator, true);
542 
543             array[1] = mbThread;
544 
545             array[2] = getByGroupId_PrevAndNext(session, mbThread, groupId,
546                     orderByComparator, false);
547 
548             return array;
549         }
550         catch (Exception e) {
551             throw processException(e);
552         }
553         finally {
554             closeSession(session);
555         }
556     }
557 
558     protected MBThread getByGroupId_PrevAndNext(Session session,
559         MBThread mbThread, long groupId, OrderByComparator orderByComparator,
560         boolean previous) {
561         StringBundler query = null;
562 
563         if (orderByComparator != null) {
564             query = new StringBundler(6 +
565                     (orderByComparator.getOrderByFields().length * 6));
566         }
567         else {
568             query = new StringBundler(3);
569         }
570 
571         query.append(_SQL_SELECT_MBTHREAD_WHERE);
572 
573         query.append(_FINDER_COLUMN_GROUPID_GROUPID_2);
574 
575         if (orderByComparator != null) {
576             String[] orderByFields = orderByComparator.getOrderByFields();
577 
578             if (orderByFields.length > 0) {
579                 query.append(WHERE_AND);
580             }
581 
582             for (int i = 0; i < orderByFields.length; i++) {
583                 query.append(_ORDER_BY_ENTITY_ALIAS);
584                 query.append(orderByFields[i]);
585 
586                 if ((i + 1) < orderByFields.length) {
587                     if (orderByComparator.isAscending() ^ previous) {
588                         query.append(WHERE_GREATER_THAN_HAS_NEXT);
589                     }
590                     else {
591                         query.append(WHERE_LESSER_THAN_HAS_NEXT);
592                     }
593                 }
594                 else {
595                     if (orderByComparator.isAscending() ^ previous) {
596                         query.append(WHERE_GREATER_THAN);
597                     }
598                     else {
599                         query.append(WHERE_LESSER_THAN);
600                     }
601                 }
602             }
603 
604             query.append(ORDER_BY_CLAUSE);
605 
606             for (int i = 0; i < orderByFields.length; i++) {
607                 query.append(_ORDER_BY_ENTITY_ALIAS);
608                 query.append(orderByFields[i]);
609 
610                 if ((i + 1) < orderByFields.length) {
611                     if (orderByComparator.isAscending() ^ previous) {
612                         query.append(ORDER_BY_ASC_HAS_NEXT);
613                     }
614                     else {
615                         query.append(ORDER_BY_DESC_HAS_NEXT);
616                     }
617                 }
618                 else {
619                     if (orderByComparator.isAscending() ^ previous) {
620                         query.append(ORDER_BY_ASC);
621                     }
622                     else {
623                         query.append(ORDER_BY_DESC);
624                     }
625                 }
626             }
627         }
628 
629         else {
630             query.append(MBThreadModelImpl.ORDER_BY_JPQL);
631         }
632 
633         String sql = query.toString();
634 
635         Query q = session.createQuery(sql);
636 
637         q.setFirstResult(0);
638         q.setMaxResults(2);
639 
640         QueryPos qPos = QueryPos.getInstance(q);
641 
642         qPos.add(groupId);
643 
644         if (orderByComparator != null) {
645             Object[] values = orderByComparator.getOrderByValues(mbThread);
646 
647             for (Object value : values) {
648                 qPos.add(value);
649             }
650         }
651 
652         List<MBThread> list = q.list();
653 
654         if (list.size() == 2) {
655             return list.get(1);
656         }
657         else {
658             return null;
659         }
660     }
661 
662     public List<MBThread> findByG_C(long groupId, long categoryId)
663         throws SystemException {
664         return findByG_C(groupId, categoryId, QueryUtil.ALL_POS,
665             QueryUtil.ALL_POS, null);
666     }
667 
668     public List<MBThread> findByG_C(long groupId, long categoryId, int start,
669         int end) throws SystemException {
670         return findByG_C(groupId, categoryId, start, end, null);
671     }
672 
673     public List<MBThread> findByG_C(long groupId, long categoryId, int start,
674         int end, OrderByComparator orderByComparator) throws SystemException {
675         Object[] finderArgs = new Object[] {
676                 new Long(groupId), new Long(categoryId),
677                 
678                 String.valueOf(start), String.valueOf(end),
679                 String.valueOf(orderByComparator)
680             };
681 
682         List<MBThread> list = (List<MBThread>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_G_C,
683                 finderArgs, this);
684 
685         if (list == null) {
686             Session session = null;
687 
688             try {
689                 session = openSession();
690 
691                 StringBundler query = null;
692 
693                 if (orderByComparator != null) {
694                     query = new StringBundler(4 +
695                             (orderByComparator.getOrderByFields().length * 3));
696                 }
697                 else {
698                     query = new StringBundler(4);
699                 }
700 
701                 query.append(_SQL_SELECT_MBTHREAD_WHERE);
702 
703                 query.append(_FINDER_COLUMN_G_C_GROUPID_2);
704 
705                 query.append(_FINDER_COLUMN_G_C_CATEGORYID_2);
706 
707                 if (orderByComparator != null) {
708                     appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
709                         orderByComparator);
710                 }
711 
712                 else {
713                     query.append(MBThreadModelImpl.ORDER_BY_JPQL);
714                 }
715 
716                 String sql = query.toString();
717 
718                 Query q = session.createQuery(sql);
719 
720                 QueryPos qPos = QueryPos.getInstance(q);
721 
722                 qPos.add(groupId);
723 
724                 qPos.add(categoryId);
725 
726                 list = (List<MBThread>)QueryUtil.list(q, getDialect(), start,
727                         end);
728             }
729             catch (Exception e) {
730                 throw processException(e);
731             }
732             finally {
733                 if (list == null) {
734                     list = new ArrayList<MBThread>();
735                 }
736 
737                 cacheResult(list);
738 
739                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_G_C, finderArgs,
740                     list);
741 
742                 closeSession(session);
743             }
744         }
745 
746         return list;
747     }
748 
749     public MBThread findByG_C_First(long groupId, long categoryId,
750         OrderByComparator orderByComparator)
751         throws NoSuchThreadException, SystemException {
752         List<MBThread> list = findByG_C(groupId, categoryId, 0, 1,
753                 orderByComparator);
754 
755         if (list.isEmpty()) {
756             StringBundler msg = new StringBundler(6);
757 
758             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
759 
760             msg.append("groupId=");
761             msg.append(groupId);
762 
763             msg.append(", categoryId=");
764             msg.append(categoryId);
765 
766             msg.append(StringPool.CLOSE_CURLY_BRACE);
767 
768             throw new NoSuchThreadException(msg.toString());
769         }
770         else {
771             return list.get(0);
772         }
773     }
774 
775     public MBThread findByG_C_Last(long groupId, long categoryId,
776         OrderByComparator orderByComparator)
777         throws NoSuchThreadException, SystemException {
778         int count = countByG_C(groupId, categoryId);
779 
780         List<MBThread> list = findByG_C(groupId, categoryId, count - 1, count,
781                 orderByComparator);
782 
783         if (list.isEmpty()) {
784             StringBundler msg = new StringBundler(6);
785 
786             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
787 
788             msg.append("groupId=");
789             msg.append(groupId);
790 
791             msg.append(", categoryId=");
792             msg.append(categoryId);
793 
794             msg.append(StringPool.CLOSE_CURLY_BRACE);
795 
796             throw new NoSuchThreadException(msg.toString());
797         }
798         else {
799             return list.get(0);
800         }
801     }
802 
803     public MBThread[] findByG_C_PrevAndNext(long threadId, long groupId,
804         long categoryId, OrderByComparator orderByComparator)
805         throws NoSuchThreadException, SystemException {
806         MBThread mbThread = findByPrimaryKey(threadId);
807 
808         Session session = null;
809 
810         try {
811             session = openSession();
812 
813             MBThread[] array = new MBThreadImpl[3];
814 
815             array[0] = getByG_C_PrevAndNext(session, mbThread, groupId,
816                     categoryId, orderByComparator, true);
817 
818             array[1] = mbThread;
819 
820             array[2] = getByG_C_PrevAndNext(session, mbThread, groupId,
821                     categoryId, orderByComparator, false);
822 
823             return array;
824         }
825         catch (Exception e) {
826             throw processException(e);
827         }
828         finally {
829             closeSession(session);
830         }
831     }
832 
833     protected MBThread getByG_C_PrevAndNext(Session session, MBThread mbThread,
834         long groupId, long categoryId, OrderByComparator orderByComparator,
835         boolean previous) {
836         StringBundler query = null;
837 
838         if (orderByComparator != null) {
839             query = new StringBundler(6 +
840                     (orderByComparator.getOrderByFields().length * 6));
841         }
842         else {
843             query = new StringBundler(3);
844         }
845 
846         query.append(_SQL_SELECT_MBTHREAD_WHERE);
847 
848         query.append(_FINDER_COLUMN_G_C_GROUPID_2);
849 
850         query.append(_FINDER_COLUMN_G_C_CATEGORYID_2);
851 
852         if (orderByComparator != null) {
853             String[] orderByFields = orderByComparator.getOrderByFields();
854 
855             if (orderByFields.length > 0) {
856                 query.append(WHERE_AND);
857             }
858 
859             for (int i = 0; i < orderByFields.length; i++) {
860                 query.append(_ORDER_BY_ENTITY_ALIAS);
861                 query.append(orderByFields[i]);
862 
863                 if ((i + 1) < orderByFields.length) {
864                     if (orderByComparator.isAscending() ^ previous) {
865                         query.append(WHERE_GREATER_THAN_HAS_NEXT);
866                     }
867                     else {
868                         query.append(WHERE_LESSER_THAN_HAS_NEXT);
869                     }
870                 }
871                 else {
872                     if (orderByComparator.isAscending() ^ previous) {
873                         query.append(WHERE_GREATER_THAN);
874                     }
875                     else {
876                         query.append(WHERE_LESSER_THAN);
877                     }
878                 }
879             }
880 
881             query.append(ORDER_BY_CLAUSE);
882 
883             for (int i = 0; i < orderByFields.length; i++) {
884                 query.append(_ORDER_BY_ENTITY_ALIAS);
885                 query.append(orderByFields[i]);
886 
887                 if ((i + 1) < orderByFields.length) {
888                     if (orderByComparator.isAscending() ^ previous) {
889                         query.append(ORDER_BY_ASC_HAS_NEXT);
890                     }
891                     else {
892                         query.append(ORDER_BY_DESC_HAS_NEXT);
893                     }
894                 }
895                 else {
896                     if (orderByComparator.isAscending() ^ previous) {
897                         query.append(ORDER_BY_ASC);
898                     }
899                     else {
900                         query.append(ORDER_BY_DESC);
901                     }
902                 }
903             }
904         }
905 
906         else {
907             query.append(MBThreadModelImpl.ORDER_BY_JPQL);
908         }
909 
910         String sql = query.toString();
911 
912         Query q = session.createQuery(sql);
913 
914         q.setFirstResult(0);
915         q.setMaxResults(2);
916 
917         QueryPos qPos = QueryPos.getInstance(q);
918 
919         qPos.add(groupId);
920 
921         qPos.add(categoryId);
922 
923         if (orderByComparator != null) {
924             Object[] values = orderByComparator.getOrderByValues(mbThread);
925 
926             for (Object value : values) {
927                 qPos.add(value);
928             }
929         }
930 
931         List<MBThread> list = q.list();
932 
933         if (list.size() == 2) {
934             return list.get(1);
935         }
936         else {
937             return null;
938         }
939     }
940 
941     public List<MBThread> findByG_S(long groupId, int status)
942         throws SystemException {
943         return findByG_S(groupId, status, QueryUtil.ALL_POS, QueryUtil.ALL_POS,
944             null);
945     }
946 
947     public List<MBThread> findByG_S(long groupId, int status, int start, int end)
948         throws SystemException {
949         return findByG_S(groupId, status, start, end, null);
950     }
951 
952     public List<MBThread> findByG_S(long groupId, int status, int start,
953         int end, OrderByComparator orderByComparator) throws SystemException {
954         Object[] finderArgs = new Object[] {
955                 new Long(groupId), new Integer(status),
956                 
957                 String.valueOf(start), String.valueOf(end),
958                 String.valueOf(orderByComparator)
959             };
960 
961         List<MBThread> list = (List<MBThread>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_G_S,
962                 finderArgs, this);
963 
964         if (list == null) {
965             Session session = null;
966 
967             try {
968                 session = openSession();
969 
970                 StringBundler query = null;
971 
972                 if (orderByComparator != null) {
973                     query = new StringBundler(4 +
974                             (orderByComparator.getOrderByFields().length * 3));
975                 }
976                 else {
977                     query = new StringBundler(4);
978                 }
979 
980                 query.append(_SQL_SELECT_MBTHREAD_WHERE);
981 
982                 query.append(_FINDER_COLUMN_G_S_GROUPID_2);
983 
984                 query.append(_FINDER_COLUMN_G_S_STATUS_2);
985 
986                 if (orderByComparator != null) {
987                     appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
988                         orderByComparator);
989                 }
990 
991                 else {
992                     query.append(MBThreadModelImpl.ORDER_BY_JPQL);
993                 }
994 
995                 String sql = query.toString();
996 
997                 Query q = session.createQuery(sql);
998 
999                 QueryPos qPos = QueryPos.getInstance(q);
1000
1001                qPos.add(groupId);
1002
1003                qPos.add(status);
1004
1005                list = (List<MBThread>)QueryUtil.list(q, getDialect(), start,
1006                        end);
1007            }
1008            catch (Exception e) {
1009                throw processException(e);
1010            }
1011            finally {
1012                if (list == null) {
1013                    list = new ArrayList<MBThread>();
1014                }
1015
1016                cacheResult(list);
1017
1018                FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_G_S, finderArgs,
1019                    list);
1020
1021                closeSession(session);
1022            }
1023        }
1024
1025        return list;
1026    }
1027
1028    public MBThread findByG_S_First(long groupId, int status,
1029        OrderByComparator orderByComparator)
1030        throws NoSuchThreadException, SystemException {
1031        List<MBThread> list = findByG_S(groupId, status, 0, 1, orderByComparator);
1032
1033        if (list.isEmpty()) {
1034            StringBundler msg = new StringBundler(6);
1035
1036            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1037
1038            msg.append("groupId=");
1039            msg.append(groupId);
1040
1041            msg.append(", status=");
1042            msg.append(status);
1043
1044            msg.append(StringPool.CLOSE_CURLY_BRACE);
1045
1046            throw new NoSuchThreadException(msg.toString());
1047        }
1048        else {
1049            return list.get(0);
1050        }
1051    }
1052
1053    public MBThread findByG_S_Last(long groupId, int status,
1054        OrderByComparator orderByComparator)
1055        throws NoSuchThreadException, SystemException {
1056        int count = countByG_S(groupId, status);
1057
1058        List<MBThread> list = findByG_S(groupId, status, count - 1, count,
1059                orderByComparator);
1060
1061        if (list.isEmpty()) {
1062            StringBundler msg = new StringBundler(6);
1063
1064            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1065
1066            msg.append("groupId=");
1067            msg.append(groupId);
1068
1069            msg.append(", status=");
1070            msg.append(status);
1071
1072            msg.append(StringPool.CLOSE_CURLY_BRACE);
1073
1074            throw new NoSuchThreadException(msg.toString());
1075        }
1076        else {
1077            return list.get(0);
1078        }
1079    }
1080
1081    public MBThread[] findByG_S_PrevAndNext(long threadId, long groupId,
1082        int status, OrderByComparator orderByComparator)
1083        throws NoSuchThreadException, SystemException {
1084        MBThread mbThread = findByPrimaryKey(threadId);
1085
1086        Session session = null;
1087
1088        try {
1089            session = openSession();
1090
1091            MBThread[] array = new MBThreadImpl[3];
1092
1093            array[0] = getByG_S_PrevAndNext(session, mbThread, groupId, status,
1094                    orderByComparator, true);
1095
1096            array[1] = mbThread;
1097
1098            array[2] = getByG_S_PrevAndNext(session, mbThread, groupId, status,
1099                    orderByComparator, false);
1100
1101            return array;
1102        }
1103        catch (Exception e) {
1104            throw processException(e);
1105        }
1106        finally {
1107            closeSession(session);
1108        }
1109    }
1110
1111    protected MBThread getByG_S_PrevAndNext(Session session, MBThread mbThread,
1112        long groupId, int status, OrderByComparator orderByComparator,
1113        boolean previous) {
1114        StringBundler query = null;
1115
1116        if (orderByComparator != null) {
1117            query = new StringBundler(6 +
1118                    (orderByComparator.getOrderByFields().length * 6));
1119        }
1120        else {
1121            query = new StringBundler(3);
1122        }
1123
1124        query.append(_SQL_SELECT_MBTHREAD_WHERE);
1125
1126        query.append(_FINDER_COLUMN_G_S_GROUPID_2);
1127
1128        query.append(_FINDER_COLUMN_G_S_STATUS_2);
1129
1130        if (orderByComparator != null) {
1131            String[] orderByFields = orderByComparator.getOrderByFields();
1132
1133            if (orderByFields.length > 0) {
1134                query.append(WHERE_AND);
1135            }
1136
1137            for (int i = 0; i < orderByFields.length; i++) {
1138                query.append(_ORDER_BY_ENTITY_ALIAS);
1139                query.append(orderByFields[i]);
1140
1141                if ((i + 1) < orderByFields.length) {
1142                    if (orderByComparator.isAscending() ^ previous) {
1143                        query.append(WHERE_GREATER_THAN_HAS_NEXT);
1144                    }
1145                    else {
1146                        query.append(WHERE_LESSER_THAN_HAS_NEXT);
1147                    }
1148                }
1149                else {
1150                    if (orderByComparator.isAscending() ^ previous) {
1151                        query.append(WHERE_GREATER_THAN);
1152                    }
1153                    else {
1154                        query.append(WHERE_LESSER_THAN);
1155                    }
1156                }
1157            }
1158
1159            query.append(ORDER_BY_CLAUSE);
1160
1161            for (int i = 0; i < orderByFields.length; i++) {
1162                query.append(_ORDER_BY_ENTITY_ALIAS);
1163                query.append(orderByFields[i]);
1164
1165                if ((i + 1) < orderByFields.length) {
1166                    if (orderByComparator.isAscending() ^ previous) {
1167                        query.append(ORDER_BY_ASC_HAS_NEXT);
1168                    }
1169                    else {
1170                        query.append(ORDER_BY_DESC_HAS_NEXT);
1171                    }
1172                }
1173                else {
1174                    if (orderByComparator.isAscending() ^ previous) {
1175                        query.append(ORDER_BY_ASC);
1176                    }
1177                    else {
1178                        query.append(ORDER_BY_DESC);
1179                    }
1180                }
1181            }
1182        }
1183
1184        else {
1185            query.append(MBThreadModelImpl.ORDER_BY_JPQL);
1186        }
1187
1188        String sql = query.toString();
1189
1190        Query q = session.createQuery(sql);
1191
1192        q.setFirstResult(0);
1193        q.setMaxResults(2);
1194
1195        QueryPos qPos = QueryPos.getInstance(q);
1196
1197        qPos.add(groupId);
1198
1199        qPos.add(status);
1200
1201        if (orderByComparator != null) {
1202            Object[] values = orderByComparator.getOrderByValues(mbThread);
1203
1204            for (Object value : values) {
1205                qPos.add(value);
1206            }
1207        }
1208
1209        List<MBThread> list = q.list();
1210
1211        if (list.size() == 2) {
1212            return list.get(1);
1213        }
1214        else {
1215            return null;
1216        }
1217    }
1218
1219    public List<MBThread> findByC_P(long categoryId, double priority)
1220        throws SystemException {
1221        return findByC_P(categoryId, priority, QueryUtil.ALL_POS,
1222            QueryUtil.ALL_POS, null);
1223    }
1224
1225    public List<MBThread> findByC_P(long categoryId, double priority,
1226        int start, int end) throws SystemException {
1227        return findByC_P(categoryId, priority, start, end, null);
1228    }
1229
1230    public List<MBThread> findByC_P(long categoryId, double priority,
1231        int start, int end, OrderByComparator orderByComparator)
1232        throws SystemException {
1233        Object[] finderArgs = new Object[] {
1234                new Long(categoryId), new Double(priority),
1235                
1236                String.valueOf(start), String.valueOf(end),
1237                String.valueOf(orderByComparator)
1238            };
1239
1240        List<MBThread> list = (List<MBThread>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_C_P,
1241                finderArgs, this);
1242
1243        if (list == null) {
1244            Session session = null;
1245
1246            try {
1247                session = openSession();
1248
1249                StringBundler query = null;
1250
1251                if (orderByComparator != null) {
1252                    query = new StringBundler(4 +
1253                            (orderByComparator.getOrderByFields().length * 3));
1254                }
1255                else {
1256                    query = new StringBundler(4);
1257                }
1258
1259                query.append(_SQL_SELECT_MBTHREAD_WHERE);
1260
1261                query.append(_FINDER_COLUMN_C_P_CATEGORYID_2);
1262
1263                query.append(_FINDER_COLUMN_C_P_PRIORITY_2);
1264
1265                if (orderByComparator != null) {
1266                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1267                        orderByComparator);
1268                }
1269
1270                else {
1271                    query.append(MBThreadModelImpl.ORDER_BY_JPQL);
1272                }
1273
1274                String sql = query.toString();
1275
1276                Query q = session.createQuery(sql);
1277
1278                QueryPos qPos = QueryPos.getInstance(q);
1279
1280                qPos.add(categoryId);
1281
1282                qPos.add(priority);
1283
1284                list = (List<MBThread>)QueryUtil.list(q, getDialect(), start,
1285                        end);
1286            }
1287            catch (Exception e) {
1288                throw processException(e);
1289            }
1290            finally {
1291                if (list == null) {
1292                    list = new ArrayList<MBThread>();
1293                }
1294
1295                cacheResult(list);
1296
1297                FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_C_P, finderArgs,
1298                    list);
1299
1300                closeSession(session);
1301            }
1302        }
1303
1304        return list;
1305    }
1306
1307    public MBThread findByC_P_First(long categoryId, double priority,
1308        OrderByComparator orderByComparator)
1309        throws NoSuchThreadException, SystemException {
1310        List<MBThread> list = findByC_P(categoryId, priority, 0, 1,
1311                orderByComparator);
1312
1313        if (list.isEmpty()) {
1314            StringBundler msg = new StringBundler(6);
1315
1316            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1317
1318            msg.append("categoryId=");
1319            msg.append(categoryId);
1320
1321            msg.append(", priority=");
1322            msg.append(priority);
1323
1324            msg.append(StringPool.CLOSE_CURLY_BRACE);
1325
1326            throw new NoSuchThreadException(msg.toString());
1327        }
1328        else {
1329            return list.get(0);
1330        }
1331    }
1332
1333    public MBThread findByC_P_Last(long categoryId, double priority,
1334        OrderByComparator orderByComparator)
1335        throws NoSuchThreadException, SystemException {
1336        int count = countByC_P(categoryId, priority);
1337
1338        List<MBThread> list = findByC_P(categoryId, priority, count - 1, count,
1339                orderByComparator);
1340
1341        if (list.isEmpty()) {
1342            StringBundler msg = new StringBundler(6);
1343
1344            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1345
1346            msg.append("categoryId=");
1347            msg.append(categoryId);
1348
1349            msg.append(", priority=");
1350            msg.append(priority);
1351
1352            msg.append(StringPool.CLOSE_CURLY_BRACE);
1353
1354            throw new NoSuchThreadException(msg.toString());
1355        }
1356        else {
1357            return list.get(0);
1358        }
1359    }
1360
1361    public MBThread[] findByC_P_PrevAndNext(long threadId, long categoryId,
1362        double priority, OrderByComparator orderByComparator)
1363        throws NoSuchThreadException, SystemException {
1364        MBThread mbThread = findByPrimaryKey(threadId);
1365
1366        Session session = null;
1367
1368        try {
1369            session = openSession();
1370
1371            MBThread[] array = new MBThreadImpl[3];
1372
1373            array[0] = getByC_P_PrevAndNext(session, mbThread, categoryId,
1374                    priority, orderByComparator, true);
1375
1376            array[1] = mbThread;
1377
1378            array[2] = getByC_P_PrevAndNext(session, mbThread, categoryId,
1379                    priority, orderByComparator, false);
1380
1381            return array;
1382        }
1383        catch (Exception e) {
1384            throw processException(e);
1385        }
1386        finally {
1387            closeSession(session);
1388        }
1389    }
1390
1391    protected MBThread getByC_P_PrevAndNext(Session session, MBThread mbThread,
1392        long categoryId, double priority, OrderByComparator orderByComparator,
1393        boolean previous) {
1394        StringBundler query = null;
1395
1396        if (orderByComparator != null) {
1397            query = new StringBundler(6 +
1398                    (orderByComparator.getOrderByFields().length * 6));
1399        }
1400        else {
1401            query = new StringBundler(3);
1402        }
1403
1404        query.append(_SQL_SELECT_MBTHREAD_WHERE);
1405
1406        query.append(_FINDER_COLUMN_C_P_CATEGORYID_2);
1407
1408        query.append(_FINDER_COLUMN_C_P_PRIORITY_2);
1409
1410        if (orderByComparator != null) {
1411            String[] orderByFields = orderByComparator.getOrderByFields();
1412
1413            if (orderByFields.length > 0) {
1414                query.append(WHERE_AND);
1415            }
1416
1417            for (int i = 0; i < orderByFields.length; i++) {
1418                query.append(_ORDER_BY_ENTITY_ALIAS);
1419                query.append(orderByFields[i]);
1420
1421                if ((i + 1) < orderByFields.length) {
1422                    if (orderByComparator.isAscending() ^ previous) {
1423                        query.append(WHERE_GREATER_THAN_HAS_NEXT);
1424                    }
1425                    else {
1426                        query.append(WHERE_LESSER_THAN_HAS_NEXT);
1427                    }
1428                }
1429                else {
1430                    if (orderByComparator.isAscending() ^ previous) {
1431                        query.append(WHERE_GREATER_THAN);
1432                    }
1433                    else {
1434                        query.append(WHERE_LESSER_THAN);
1435                    }
1436                }
1437            }
1438
1439            query.append(ORDER_BY_CLAUSE);
1440
1441            for (int i = 0; i < orderByFields.length; i++) {
1442                query.append(_ORDER_BY_ENTITY_ALIAS);
1443                query.append(orderByFields[i]);
1444
1445                if ((i + 1) < orderByFields.length) {
1446                    if (orderByComparator.isAscending() ^ previous) {
1447                        query.append(ORDER_BY_ASC_HAS_NEXT);
1448                    }
1449                    else {
1450                        query.append(ORDER_BY_DESC_HAS_NEXT);
1451                    }
1452                }
1453                else {
1454                    if (orderByComparator.isAscending() ^ previous) {
1455                        query.append(ORDER_BY_ASC);
1456                    }
1457                    else {
1458                        query.append(ORDER_BY_DESC);
1459                    }
1460                }
1461            }
1462        }
1463
1464        else {
1465            query.append(MBThreadModelImpl.ORDER_BY_JPQL);
1466        }
1467
1468        String sql = query.toString();
1469
1470        Query q = session.createQuery(sql);
1471
1472        q.setFirstResult(0);
1473        q.setMaxResults(2);
1474
1475        QueryPos qPos = QueryPos.getInstance(q);
1476
1477        qPos.add(categoryId);
1478
1479        qPos.add(priority);
1480
1481        if (orderByComparator != null) {
1482            Object[] values = orderByComparator.getOrderByValues(mbThread);
1483
1484            for (Object value : values) {
1485                qPos.add(value);
1486            }
1487        }
1488
1489        List<MBThread> list = q.list();
1490
1491        if (list.size() == 2) {
1492            return list.get(1);
1493        }
1494        else {
1495            return null;
1496        }
1497    }
1498
1499    public List<MBThread> findByG_C_L(long groupId, long categoryId,
1500        Date lastPostDate) throws SystemException {
1501        return findByG_C_L(groupId, categoryId, lastPostDate,
1502            QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
1503    }
1504
1505    public List<MBThread> findByG_C_L(long groupId, long categoryId,
1506        Date lastPostDate, int start, int end) throws SystemException {
1507        return findByG_C_L(groupId, categoryId, lastPostDate, start, end, null);
1508    }
1509
1510    public List<MBThread> findByG_C_L(long groupId, long categoryId,
1511        Date lastPostDate, int start, int end,
1512        OrderByComparator orderByComparator) throws SystemException {
1513        Object[] finderArgs = new Object[] {
1514                new Long(groupId), new Long(categoryId),
1515                
1516                lastPostDate,
1517                
1518                String.valueOf(start), String.valueOf(end),
1519                String.valueOf(orderByComparator)
1520            };
1521
1522        List<MBThread> list = (List<MBThread>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_G_C_L,
1523                finderArgs, this);
1524
1525        if (list == null) {
1526            Session session = null;
1527
1528            try {
1529                session = openSession();
1530
1531                StringBundler query = null;
1532
1533                if (orderByComparator != null) {
1534                    query = new StringBundler(5 +
1535                            (orderByComparator.getOrderByFields().length * 3));
1536                }
1537                else {
1538                    query = new StringBundler(5);
1539                }
1540
1541                query.append(_SQL_SELECT_MBTHREAD_WHERE);
1542
1543                query.append(_FINDER_COLUMN_G_C_L_GROUPID_2);
1544
1545                query.append(_FINDER_COLUMN_G_C_L_CATEGORYID_2);
1546
1547                if (lastPostDate == null) {
1548                    query.append(_FINDER_COLUMN_G_C_L_LASTPOSTDATE_1);
1549                }
1550                else {
1551                    query.append(_FINDER_COLUMN_G_C_L_LASTPOSTDATE_2);
1552                }
1553
1554                if (orderByComparator != null) {
1555                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1556                        orderByComparator);
1557                }
1558
1559                else {
1560                    query.append(MBThreadModelImpl.ORDER_BY_JPQL);
1561                }
1562
1563                String sql = query.toString();
1564
1565                Query q = session.createQuery(sql);
1566
1567                QueryPos qPos = QueryPos.getInstance(q);
1568
1569                qPos.add(groupId);
1570
1571                qPos.add(categoryId);
1572
1573                if (lastPostDate != null) {
1574                    qPos.add(CalendarUtil.getTimestamp(lastPostDate));
1575                }
1576
1577                list = (List<MBThread>)QueryUtil.list(q, getDialect(), start,
1578                        end);
1579            }
1580            catch (Exception e) {
1581                throw processException(e);
1582            }
1583            finally {
1584                if (list == null) {
1585                    list = new ArrayList<MBThread>();
1586                }
1587
1588                cacheResult(list);
1589
1590                FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_G_C_L,
1591                    finderArgs, list);
1592
1593                closeSession(session);
1594            }
1595        }
1596
1597        return list;
1598    }
1599
1600    public MBThread findByG_C_L_First(long groupId, long categoryId,
1601        Date lastPostDate, OrderByComparator orderByComparator)
1602        throws NoSuchThreadException, SystemException {
1603        List<MBThread> list = findByG_C_L(groupId, categoryId, lastPostDate, 0,
1604                1, orderByComparator);
1605
1606        if (list.isEmpty()) {
1607            StringBundler msg = new StringBundler(8);
1608
1609            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1610
1611            msg.append("groupId=");
1612            msg.append(groupId);
1613
1614            msg.append(", categoryId=");
1615            msg.append(categoryId);
1616
1617            msg.append(", lastPostDate=");
1618            msg.append(lastPostDate);
1619
1620            msg.append(StringPool.CLOSE_CURLY_BRACE);
1621
1622            throw new NoSuchThreadException(msg.toString());
1623        }
1624        else {
1625            return list.get(0);
1626        }
1627    }
1628
1629    public MBThread findByG_C_L_Last(long groupId, long categoryId,
1630        Date lastPostDate, OrderByComparator orderByComparator)
1631        throws NoSuchThreadException, SystemException {
1632        int count = countByG_C_L(groupId, categoryId, lastPostDate);
1633
1634        List<MBThread> list = findByG_C_L(groupId, categoryId, lastPostDate,
1635                count - 1, count, orderByComparator);
1636
1637        if (list.isEmpty()) {
1638            StringBundler msg = new StringBundler(8);
1639
1640            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1641
1642            msg.append("groupId=");
1643            msg.append(groupId);
1644
1645            msg.append(", categoryId=");
1646            msg.append(categoryId);
1647
1648            msg.append(", lastPostDate=");
1649            msg.append(lastPostDate);
1650
1651            msg.append(StringPool.CLOSE_CURLY_BRACE);
1652
1653            throw new NoSuchThreadException(msg.toString());
1654        }
1655        else {
1656            return list.get(0);
1657        }
1658    }
1659
1660    public MBThread[] findByG_C_L_PrevAndNext(long threadId, long groupId,
1661        long categoryId, Date lastPostDate, OrderByComparator orderByComparator)
1662        throws NoSuchThreadException, SystemException {
1663        MBThread mbThread = findByPrimaryKey(threadId);
1664
1665        Session session = null;
1666
1667        try {
1668            session = openSession();
1669
1670            MBThread[] array = new MBThreadImpl[3];
1671
1672            array[0] = getByG_C_L_PrevAndNext(session, mbThread, groupId,
1673                    categoryId, lastPostDate, orderByComparator, true);
1674
1675            array[1] = mbThread;
1676
1677            array[2] = getByG_C_L_PrevAndNext(session, mbThread, groupId,
1678                    categoryId, lastPostDate, orderByComparator, false);
1679
1680            return array;
1681        }
1682        catch (Exception e) {
1683            throw processException(e);
1684        }
1685        finally {
1686            closeSession(session);
1687        }
1688    }
1689
1690    protected MBThread getByG_C_L_PrevAndNext(Session session,
1691        MBThread mbThread, long groupId, long categoryId, Date lastPostDate,
1692        OrderByComparator orderByComparator, boolean previous) {
1693        StringBundler query = null;
1694
1695        if (orderByComparator != null) {
1696            query = new StringBundler(6 +
1697                    (orderByComparator.getOrderByFields().length * 6));
1698        }
1699        else {
1700            query = new StringBundler(3);
1701        }
1702
1703        query.append(_SQL_SELECT_MBTHREAD_WHERE);
1704
1705        query.append(_FINDER_COLUMN_G_C_L_GROUPID_2);
1706
1707        query.append(_FINDER_COLUMN_G_C_L_CATEGORYID_2);
1708
1709        if (lastPostDate == null) {
1710            query.append(_FINDER_COLUMN_G_C_L_LASTPOSTDATE_1);
1711        }
1712        else {
1713            query.append(_FINDER_COLUMN_G_C_L_LASTPOSTDATE_2);
1714        }
1715
1716        if (orderByComparator != null) {
1717            String[] orderByFields = orderByComparator.getOrderByFields();
1718
1719            if (orderByFields.length > 0) {
1720                query.append(WHERE_AND);
1721            }
1722
1723            for (int i = 0; i < orderByFields.length; i++) {
1724                query.append(_ORDER_BY_ENTITY_ALIAS);
1725                query.append(orderByFields[i]);
1726
1727                if ((i + 1) < orderByFields.length) {
1728                    if (orderByComparator.isAscending() ^ previous) {
1729                        query.append(WHERE_GREATER_THAN_HAS_NEXT);
1730                    }
1731                    else {
1732                        query.append(WHERE_LESSER_THAN_HAS_NEXT);
1733                    }
1734                }
1735                else {
1736                    if (orderByComparator.isAscending() ^ previous) {
1737                        query.append(WHERE_GREATER_THAN);
1738                    }
1739                    else {
1740                        query.append(WHERE_LESSER_THAN);
1741                    }
1742                }
1743            }
1744
1745            query.append(ORDER_BY_CLAUSE);
1746
1747            for (int i = 0; i < orderByFields.length; i++) {
1748                query.append(_ORDER_BY_ENTITY_ALIAS);
1749                query.append(orderByFields[i]);
1750
1751                if ((i + 1) < orderByFields.length) {
1752                    if (orderByComparator.isAscending() ^ previous) {
1753                        query.append(ORDER_BY_ASC_HAS_NEXT);
1754                    }
1755                    else {
1756                        query.append(ORDER_BY_DESC_HAS_NEXT);
1757                    }
1758                }
1759                else {
1760                    if (orderByComparator.isAscending() ^ previous) {
1761                        query.append(ORDER_BY_ASC);
1762                    }
1763                    else {
1764                        query.append(ORDER_BY_DESC);
1765                    }
1766                }
1767            }
1768        }
1769
1770        else {
1771            query.append(MBThreadModelImpl.ORDER_BY_JPQL);
1772        }
1773
1774        String sql = query.toString();
1775
1776        Query q = session.createQuery(sql);
1777
1778        q.setFirstResult(0);
1779        q.setMaxResults(2);
1780
1781        QueryPos qPos = QueryPos.getInstance(q);
1782
1783        qPos.add(groupId);
1784
1785        qPos.add(categoryId);
1786
1787        if (lastPostDate != null) {
1788            qPos.add(CalendarUtil.getTimestamp(lastPostDate));
1789        }
1790
1791        if (orderByComparator != null) {
1792            Object[] values = orderByComparator.getOrderByValues(mbThread);
1793
1794            for (Object value : values) {
1795                qPos.add(value);
1796            }
1797        }
1798
1799        List<MBThread> list = q.list();
1800
1801        if (list.size() == 2) {
1802            return list.get(1);
1803        }
1804        else {
1805            return null;
1806        }
1807    }
1808
1809    public List<MBThread> findByG_C_S(long groupId, long categoryId, int status)
1810        throws SystemException {
1811        return findByG_C_S(groupId, categoryId, status, QueryUtil.ALL_POS,
1812            QueryUtil.ALL_POS, null);
1813    }
1814
1815    public List<MBThread> findByG_C_S(long groupId, long categoryId,
1816        int status, int start, int end) throws SystemException {
1817        return findByG_C_S(groupId, categoryId, status, start, end, null);
1818    }
1819
1820    public List<MBThread> findByG_C_S(long groupId, long categoryId,
1821        int status, int start, int end, OrderByComparator orderByComparator)
1822        throws SystemException {
1823        Object[] finderArgs = new Object[] {
1824                new Long(groupId), new Long(categoryId), new Integer(status),
1825                
1826                String.valueOf(start), String.valueOf(end),
1827                String.valueOf(orderByComparator)
1828            };
1829
1830        List<MBThread> list = (List<MBThread>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_G_C_S,
1831                finderArgs, this);
1832
1833        if (list == null) {
1834            Session session = null;
1835
1836            try {
1837                session = openSession();
1838
1839                StringBundler query = null;
1840
1841                if (orderByComparator != null) {
1842                    query = new StringBundler(5 +
1843                            (orderByComparator.getOrderByFields().length * 3));
1844                }
1845                else {
1846                    query = new StringBundler(5);
1847                }
1848
1849                query.append(_SQL_SELECT_MBTHREAD_WHERE);
1850
1851                query.append(_FINDER_COLUMN_G_C_S_GROUPID_2);
1852
1853                query.append(_FINDER_COLUMN_G_C_S_CATEGORYID_2);
1854
1855                query.append(_FINDER_COLUMN_G_C_S_STATUS_2);
1856
1857                if (orderByComparator != null) {
1858                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
1859                        orderByComparator);
1860                }
1861
1862                else {
1863                    query.append(MBThreadModelImpl.ORDER_BY_JPQL);
1864                }
1865
1866                String sql = query.toString();
1867
1868                Query q = session.createQuery(sql);
1869
1870                QueryPos qPos = QueryPos.getInstance(q);
1871
1872                qPos.add(groupId);
1873
1874                qPos.add(categoryId);
1875
1876                qPos.add(status);
1877
1878                list = (List<MBThread>)QueryUtil.list(q, getDialect(), start,
1879                        end);
1880            }
1881            catch (Exception e) {
1882                throw processException(e);
1883            }
1884            finally {
1885                if (list == null) {
1886                    list = new ArrayList<MBThread>();
1887                }
1888
1889                cacheResult(list);
1890
1891                FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_G_C_S,
1892                    finderArgs, list);
1893
1894                closeSession(session);
1895            }
1896        }
1897
1898        return list;
1899    }
1900
1901    public MBThread findByG_C_S_First(long groupId, long categoryId,
1902        int status, OrderByComparator orderByComparator)
1903        throws NoSuchThreadException, SystemException {
1904        List<MBThread> list = findByG_C_S(groupId, categoryId, status, 0, 1,
1905                orderByComparator);
1906
1907        if (list.isEmpty()) {
1908            StringBundler msg = new StringBundler(8);
1909
1910            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1911
1912            msg.append("groupId=");
1913            msg.append(groupId);
1914
1915            msg.append(", categoryId=");
1916            msg.append(categoryId);
1917
1918            msg.append(", status=");
1919            msg.append(status);
1920
1921            msg.append(StringPool.CLOSE_CURLY_BRACE);
1922
1923            throw new NoSuchThreadException(msg.toString());
1924        }
1925        else {
1926            return list.get(0);
1927        }
1928    }
1929
1930    public MBThread findByG_C_S_Last(long groupId, long categoryId, int status,
1931        OrderByComparator orderByComparator)
1932        throws NoSuchThreadException, SystemException {
1933        int count = countByG_C_S(groupId, categoryId, status);
1934
1935        List<MBThread> list = findByG_C_S(groupId, categoryId, status,
1936                count - 1, count, orderByComparator);
1937
1938        if (list.isEmpty()) {
1939            StringBundler msg = new StringBundler(8);
1940
1941            msg.append(_NO_SUCH_ENTITY_WITH_KEY);
1942
1943            msg.append("groupId=");
1944            msg.append(groupId);
1945
1946            msg.append(", categoryId=");
1947            msg.append(categoryId);
1948
1949            msg.append(", status=");
1950            msg.append(status);
1951
1952            msg.append(StringPool.CLOSE_CURLY_BRACE);
1953
1954            throw new NoSuchThreadException(msg.toString());
1955        }
1956        else {
1957            return list.get(0);
1958        }
1959    }
1960
1961    public MBThread[] findByG_C_S_PrevAndNext(long threadId, long groupId,
1962        long categoryId, int status, OrderByComparator orderByComparator)
1963        throws NoSuchThreadException, SystemException {
1964        MBThread mbThread = findByPrimaryKey(threadId);
1965
1966        Session session = null;
1967
1968        try {
1969            session = openSession();
1970
1971            MBThread[] array = new MBThreadImpl[3];
1972
1973            array[0] = getByG_C_S_PrevAndNext(session, mbThread, groupId,
1974                    categoryId, status, orderByComparator, true);
1975
1976            array[1] = mbThread;
1977
1978            array[2] = getByG_C_S_PrevAndNext(session, mbThread, groupId,
1979                    categoryId, status, orderByComparator, false);
1980
1981            return array;
1982        }
1983        catch (Exception e) {
1984            throw processException(e);
1985        }
1986        finally {
1987            closeSession(session);
1988        }
1989    }
1990
1991    protected MBThread getByG_C_S_PrevAndNext(Session session,
1992        MBThread mbThread, long groupId, long categoryId, int status,
1993        OrderByComparator orderByComparator, boolean previous) {
1994        StringBundler query = null;
1995
1996        if (orderByComparator != null) {
1997            query = new StringBundler(6 +
1998                    (orderByComparator.getOrderByFields().length * 6));
1999        }
2000        else {
2001            query = new StringBundler(3);
2002        }
2003
2004        query.append(_SQL_SELECT_MBTHREAD_WHERE);
2005
2006        query.append(_FINDER_COLUMN_G_C_S_GROUPID_2);
2007
2008        query.append(_FINDER_COLUMN_G_C_S_CATEGORYID_2);
2009
2010        query.append(_FINDER_COLUMN_G_C_S_STATUS_2);
2011
2012        if (orderByComparator != null) {
2013            String[] orderByFields = orderByComparator.getOrderByFields();
2014
2015            if (orderByFields.length > 0) {
2016                query.append(WHERE_AND);
2017            }
2018
2019            for (int i = 0; i < orderByFields.length; i++) {
2020                query.append(_ORDER_BY_ENTITY_ALIAS);
2021                query.append(orderByFields[i]);
2022
2023                if ((i + 1) < orderByFields.length) {
2024                    if (orderByComparator.isAscending() ^ previous) {
2025                        query.append(WHERE_GREATER_THAN_HAS_NEXT);
2026                    }
2027                    else {
2028                        query.append(WHERE_LESSER_THAN_HAS_NEXT);
2029                    }
2030                }
2031                else {
2032                    if (orderByComparator.isAscending() ^ previous) {
2033                        query.append(WHERE_GREATER_THAN);
2034                    }
2035                    else {
2036                        query.append(WHERE_LESSER_THAN);
2037                    }
2038                }
2039            }
2040
2041            query.append(ORDER_BY_CLAUSE);
2042
2043            for (int i = 0; i < orderByFields.length; i++) {
2044                query.append(_ORDER_BY_ENTITY_ALIAS);
2045                query.append(orderByFields[i]);
2046
2047                if ((i + 1) < orderByFields.length) {
2048                    if (orderByComparator.isAscending() ^ previous) {
2049                        query.append(ORDER_BY_ASC_HAS_NEXT);
2050                    }
2051                    else {
2052                        query.append(ORDER_BY_DESC_HAS_NEXT);
2053                    }
2054                }
2055                else {
2056                    if (orderByComparator.isAscending() ^ previous) {
2057                        query.append(ORDER_BY_ASC);
2058                    }
2059                    else {
2060                        query.append(ORDER_BY_DESC);
2061                    }
2062                }
2063            }
2064        }
2065
2066        else {
2067            query.append(MBThreadModelImpl.ORDER_BY_JPQL);
2068        }
2069
2070        String sql = query.toString();
2071
2072        Query q = session.createQuery(sql);
2073
2074        q.setFirstResult(0);
2075        q.setMaxResults(2);
2076
2077        QueryPos qPos = QueryPos.getInstance(q);
2078
2079        qPos.add(groupId);
2080
2081        qPos.add(categoryId);
2082
2083        qPos.add(status);
2084
2085        if (orderByComparator != null) {
2086            Object[] values = orderByComparator.getOrderByValues(mbThread);
2087
2088            for (Object value : values) {
2089                qPos.add(value);
2090            }
2091        }
2092
2093        List<MBThread> list = q.list();
2094
2095        if (list.size() == 2) {
2096            return list.get(1);
2097        }
2098        else {
2099            return null;
2100        }
2101    }
2102
2103    public List<MBThread> findAll() throws SystemException {
2104        return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
2105    }
2106
2107    public List<MBThread> findAll(int start, int end) throws SystemException {
2108        return findAll(start, end, null);
2109    }
2110
2111    public List<MBThread> findAll(int start, int end,
2112        OrderByComparator orderByComparator) throws SystemException {
2113        Object[] finderArgs = new Object[] {
2114                String.valueOf(start), String.valueOf(end),
2115                String.valueOf(orderByComparator)
2116            };
2117
2118        List<MBThread> list = (List<MBThread>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
2119                finderArgs, this);
2120
2121        if (list == null) {
2122            Session session = null;
2123
2124            try {
2125                session = openSession();
2126
2127                StringBundler query = null;
2128                String sql = null;
2129
2130                if (orderByComparator != null) {
2131                    query = new StringBundler(2 +
2132                            (orderByComparator.getOrderByFields().length * 3));
2133
2134                    query.append(_SQL_SELECT_MBTHREAD);
2135
2136                    appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
2137                        orderByComparator);
2138
2139                    sql = query.toString();
2140                }
2141
2142                else {
2143                    sql = _SQL_SELECT_MBTHREAD.concat(MBThreadModelImpl.ORDER_BY_JPQL);
2144                }
2145
2146                Query q = session.createQuery(sql);
2147
2148                if (orderByComparator == null) {
2149                    list = (List<MBThread>)QueryUtil.list(q, getDialect(),
2150                            start, end, false);
2151
2152                    Collections.sort(list);
2153                }
2154                else {
2155                    list = (List<MBThread>)QueryUtil.list(q, getDialect(),
2156                            start, end);
2157                }
2158            }
2159            catch (Exception e) {
2160                throw processException(e);
2161            }
2162            finally {
2163                if (list == null) {
2164                    list = new ArrayList<MBThread>();
2165                }
2166
2167                cacheResult(list);
2168
2169                FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
2170
2171                closeSession(session);
2172            }
2173        }
2174
2175        return list;
2176    }
2177
2178    public void removeByGroupId(long groupId) throws SystemException {
2179        for (MBThread mbThread : findByGroupId(groupId)) {
2180            remove(mbThread);
2181        }
2182    }
2183
2184    public void removeByG_C(long groupId, long categoryId)
2185        throws SystemException {
2186        for (MBThread mbThread : findByG_C(groupId, categoryId)) {
2187            remove(mbThread);
2188        }
2189    }
2190
2191    public void removeByG_S(long groupId, int status) throws SystemException {
2192        for (MBThread mbThread : findByG_S(groupId, status)) {
2193            remove(mbThread);
2194        }
2195    }
2196
2197    public void removeByC_P(long categoryId, double priority)
2198        throws SystemException {
2199        for (MBThread mbThread : findByC_P(categoryId, priority)) {
2200            remove(mbThread);
2201        }
2202    }
2203
2204    public void removeByG_C_L(long groupId, long categoryId, Date lastPostDate)
2205        throws SystemException {
2206        for (MBThread mbThread : findByG_C_L(groupId, categoryId, lastPostDate)) {
2207            remove(mbThread);
2208        }
2209    }
2210
2211    public void removeByG_C_S(long groupId, long categoryId, int status)
2212        throws SystemException {
2213        for (MBThread mbThread : findByG_C_S(groupId, categoryId, status)) {
2214            remove(mbThread);
2215        }
2216    }
2217
2218    public void removeAll() throws SystemException {
2219        for (MBThread mbThread : findAll()) {
2220            remove(mbThread);
2221        }
2222    }
2223
2224    public int countByGroupId(long groupId) throws SystemException {
2225        Object[] finderArgs = new Object[] { new Long(groupId) };
2226
2227        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_GROUPID,
2228                finderArgs, this);
2229
2230        if (count == null) {
2231            Session session = null;
2232
2233            try {
2234                session = openSession();
2235
2236                StringBundler query = new StringBundler(2);
2237
2238                query.append(_SQL_COUNT_MBTHREAD_WHERE);
2239
2240                query.append(_FINDER_COLUMN_GROUPID_GROUPID_2);
2241
2242                String sql = query.toString();
2243
2244                Query q = session.createQuery(sql);
2245
2246                QueryPos qPos = QueryPos.getInstance(q);
2247
2248                qPos.add(groupId);
2249
2250                count = (Long)q.uniqueResult();
2251            }
2252            catch (Exception e) {
2253                throw processException(e);
2254            }
2255            finally {
2256                if (count == null) {
2257                    count = Long.valueOf(0);
2258                }
2259
2260                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_GROUPID,
2261                    finderArgs, count);
2262
2263                closeSession(session);
2264            }
2265        }
2266
2267        return count.intValue();
2268    }
2269
2270    public int countByG_C(long groupId, long categoryId)
2271        throws SystemException {
2272        Object[] finderArgs = new Object[] {
2273                new Long(groupId), new Long(categoryId)
2274            };
2275
2276        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_G_C,
2277                finderArgs, this);
2278
2279        if (count == null) {
2280            Session session = null;
2281
2282            try {
2283                session = openSession();
2284
2285                StringBundler query = new StringBundler(3);
2286
2287                query.append(_SQL_COUNT_MBTHREAD_WHERE);
2288
2289                query.append(_FINDER_COLUMN_G_C_GROUPID_2);
2290
2291                query.append(_FINDER_COLUMN_G_C_CATEGORYID_2);
2292
2293                String sql = query.toString();
2294
2295                Query q = session.createQuery(sql);
2296
2297                QueryPos qPos = QueryPos.getInstance(q);
2298
2299                qPos.add(groupId);
2300
2301                qPos.add(categoryId);
2302
2303                count = (Long)q.uniqueResult();
2304            }
2305            catch (Exception e) {
2306                throw processException(e);
2307            }
2308            finally {
2309                if (count == null) {
2310                    count = Long.valueOf(0);
2311                }
2312
2313                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_G_C, finderArgs,
2314                    count);
2315
2316                closeSession(session);
2317            }
2318        }
2319
2320        return count.intValue();
2321    }
2322
2323    public int countByG_S(long groupId, int status) throws SystemException {
2324        Object[] finderArgs = new Object[] {
2325                new Long(groupId), new Integer(status)
2326            };
2327
2328        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_G_S,
2329                finderArgs, this);
2330
2331        if (count == null) {
2332            Session session = null;
2333
2334            try {
2335                session = openSession();
2336
2337                StringBundler query = new StringBundler(3);
2338
2339                query.append(_SQL_COUNT_MBTHREAD_WHERE);
2340
2341                query.append(_FINDER_COLUMN_G_S_GROUPID_2);
2342
2343                query.append(_FINDER_COLUMN_G_S_STATUS_2);
2344
2345                String sql = query.toString();
2346
2347                Query q = session.createQuery(sql);
2348
2349                QueryPos qPos = QueryPos.getInstance(q);
2350
2351                qPos.add(groupId);
2352
2353                qPos.add(status);
2354
2355                count = (Long)q.uniqueResult();
2356            }
2357            catch (Exception e) {
2358                throw processException(e);
2359            }
2360            finally {
2361                if (count == null) {
2362                    count = Long.valueOf(0);
2363                }
2364
2365                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_G_S, finderArgs,
2366                    count);
2367
2368                closeSession(session);
2369            }
2370        }
2371
2372        return count.intValue();
2373    }
2374
2375    public int countByC_P(long categoryId, double priority)
2376        throws SystemException {
2377        Object[] finderArgs = new Object[] {
2378                new Long(categoryId), new Double(priority)
2379            };
2380
2381        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_C_P,
2382                finderArgs, this);
2383
2384        if (count == null) {
2385            Session session = null;
2386
2387            try {
2388                session = openSession();
2389
2390                StringBundler query = new StringBundler(3);
2391
2392                query.append(_SQL_COUNT_MBTHREAD_WHERE);
2393
2394                query.append(_FINDER_COLUMN_C_P_CATEGORYID_2);
2395
2396                query.append(_FINDER_COLUMN_C_P_PRIORITY_2);
2397
2398                String sql = query.toString();
2399
2400                Query q = session.createQuery(sql);
2401
2402                QueryPos qPos = QueryPos.getInstance(q);
2403
2404                qPos.add(categoryId);
2405
2406                qPos.add(priority);
2407
2408                count = (Long)q.uniqueResult();
2409            }
2410            catch (Exception e) {
2411                throw processException(e);
2412            }
2413            finally {
2414                if (count == null) {
2415                    count = Long.valueOf(0);
2416                }
2417
2418                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_P, finderArgs,
2419                    count);
2420
2421                closeSession(session);
2422            }
2423        }
2424
2425        return count.intValue();
2426    }
2427
2428    public int countByG_C_L(long groupId, long categoryId, Date lastPostDate)
2429        throws SystemException {
2430        Object[] finderArgs = new Object[] {
2431                new Long(groupId), new Long(categoryId),
2432                
2433                lastPostDate
2434            };
2435
2436        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_G_C_L,
2437                finderArgs, this);
2438
2439        if (count == null) {
2440            Session session = null;
2441
2442            try {
2443                session = openSession();
2444
2445                StringBundler query = new StringBundler(4);
2446
2447                query.append(_SQL_COUNT_MBTHREAD_WHERE);
2448
2449                query.append(_FINDER_COLUMN_G_C_L_GROUPID_2);
2450
2451                query.append(_FINDER_COLUMN_G_C_L_CATEGORYID_2);
2452
2453                if (lastPostDate == null) {
2454                    query.append(_FINDER_COLUMN_G_C_L_LASTPOSTDATE_1);
2455                }
2456                else {
2457                    query.append(_FINDER_COLUMN_G_C_L_LASTPOSTDATE_2);
2458                }
2459
2460                String sql = query.toString();
2461
2462                Query q = session.createQuery(sql);
2463
2464                QueryPos qPos = QueryPos.getInstance(q);
2465
2466                qPos.add(groupId);
2467
2468                qPos.add(categoryId);
2469
2470                if (lastPostDate != null) {
2471                    qPos.add(CalendarUtil.getTimestamp(lastPostDate));
2472                }
2473
2474                count = (Long)q.uniqueResult();
2475            }
2476            catch (Exception e) {
2477                throw processException(e);
2478            }
2479            finally {
2480                if (count == null) {
2481                    count = Long.valueOf(0);
2482                }
2483
2484                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_G_C_L,
2485                    finderArgs, count);
2486
2487                closeSession(session);
2488            }
2489        }
2490
2491        return count.intValue();
2492    }
2493
2494    public int countByG_C_S(long groupId, long categoryId, int status)
2495        throws SystemException {
2496        Object[] finderArgs = new Object[] {
2497                new Long(groupId), new Long(categoryId), new Integer(status)
2498            };
2499
2500        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_G_C_S,
2501                finderArgs, this);
2502
2503        if (count == null) {
2504            Session session = null;
2505
2506            try {
2507                session = openSession();
2508
2509                StringBundler query = new StringBundler(4);
2510
2511                query.append(_SQL_COUNT_MBTHREAD_WHERE);
2512
2513                query.append(_FINDER_COLUMN_G_C_S_GROUPID_2);
2514
2515                query.append(_FINDER_COLUMN_G_C_S_CATEGORYID_2);
2516
2517                query.append(_FINDER_COLUMN_G_C_S_STATUS_2);
2518
2519                String sql = query.toString();
2520
2521                Query q = session.createQuery(sql);
2522
2523                QueryPos qPos = QueryPos.getInstance(q);
2524
2525                qPos.add(groupId);
2526
2527                qPos.add(categoryId);
2528
2529                qPos.add(status);
2530
2531                count = (Long)q.uniqueResult();
2532            }
2533            catch (Exception e) {
2534                throw processException(e);
2535            }
2536            finally {
2537                if (count == null) {
2538                    count = Long.valueOf(0);
2539                }
2540
2541                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_G_C_S,
2542                    finderArgs, count);
2543
2544                closeSession(session);
2545            }
2546        }
2547
2548        return count.intValue();
2549    }
2550
2551    public int countAll() throws SystemException {
2552        Object[] finderArgs = new Object[0];
2553
2554        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
2555                finderArgs, this);
2556
2557        if (count == null) {
2558            Session session = null;
2559
2560            try {
2561                session = openSession();
2562
2563                Query q = session.createQuery(_SQL_COUNT_MBTHREAD);
2564
2565                count = (Long)q.uniqueResult();
2566            }
2567            catch (Exception e) {
2568                throw processException(e);
2569            }
2570            finally {
2571                if (count == null) {
2572                    count = Long.valueOf(0);
2573                }
2574
2575                FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
2576                    count);
2577
2578                closeSession(session);
2579            }
2580        }
2581
2582        return count.intValue();
2583    }
2584
2585    public void afterPropertiesSet() {
2586        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
2587                    com.liferay.portal.util.PropsUtil.get(
2588                        "value.object.listener.com.liferay.portlet.messageboards.model.MBThread")));
2589
2590        if (listenerClassNames.length > 0) {
2591            try {
2592                List<ModelListener<MBThread>> listenersList = new ArrayList<ModelListener<MBThread>>();
2593
2594                for (String listenerClassName : listenerClassNames) {
2595                    listenersList.add((ModelListener<MBThread>)InstanceFactory.newInstance(
2596                            listenerClassName));
2597                }
2598
2599                listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
2600            }
2601            catch (Exception e) {
2602                _log.error(e);
2603            }
2604        }
2605    }
2606
2607    @BeanReference(type = MBBanPersistence.class)
2608    protected MBBanPersistence mbBanPersistence;
2609    @BeanReference(type = MBCategoryPersistence.class)
2610    protected MBCategoryPersistence mbCategoryPersistence;
2611    @BeanReference(type = MBDiscussionPersistence.class)
2612    protected MBDiscussionPersistence mbDiscussionPersistence;
2613    @BeanReference(type = MBMailingListPersistence.class)
2614    protected MBMailingListPersistence mbMailingListPersistence;
2615    @BeanReference(type = MBMessagePersistence.class)
2616    protected MBMessagePersistence mbMessagePersistence;
2617    @BeanReference(type = MBMessageFlagPersistence.class)
2618    protected MBMessageFlagPersistence mbMessageFlagPersistence;
2619    @BeanReference(type = MBStatsUserPersistence.class)
2620    protected MBStatsUserPersistence mbStatsUserPersistence;
2621    @BeanReference(type = MBThreadPersistence.class)
2622    protected MBThreadPersistence mbThreadPersistence;
2623    @BeanReference(type = LockPersistence.class)
2624    protected LockPersistence lockPersistence;
2625    @BeanReference(type = ResourcePersistence.class)
2626    protected ResourcePersistence resourcePersistence;
2627    @BeanReference(type = UserPersistence.class)
2628    protected UserPersistence userPersistence;
2629    @BeanReference(type = AssetEntryPersistence.class)
2630    protected AssetEntryPersistence assetEntryPersistence;
2631    @BeanReference(type = RatingsStatsPersistence.class)
2632    protected RatingsStatsPersistence ratingsStatsPersistence;
2633    @BeanReference(type = SocialActivityPersistence.class)
2634    protected SocialActivityPersistence socialActivityPersistence;
2635    private static final String _SQL_SELECT_MBTHREAD = "SELECT mbThread FROM MBThread mbThread";
2636    private static final String _SQL_SELECT_MBTHREAD_WHERE = "SELECT mbThread FROM MBThread mbThread WHERE ";
2637    private static final String _SQL_COUNT_MBTHREAD = "SELECT COUNT(mbThread) FROM MBThread mbThread";
2638    private static final String _SQL_COUNT_MBTHREAD_WHERE = "SELECT COUNT(mbThread) FROM MBThread mbThread WHERE ";
2639    private static final String _FINDER_COLUMN_GROUPID_GROUPID_2 = "mbThread.groupId = ?";
2640    private static final String _FINDER_COLUMN_G_C_GROUPID_2 = "mbThread.groupId = ? AND ";
2641    private static final String _FINDER_COLUMN_G_C_CATEGORYID_2 = "mbThread.categoryId = ?";
2642    private static final String _FINDER_COLUMN_G_S_GROUPID_2 = "mbThread.groupId = ? AND ";
2643    private static final String _FINDER_COLUMN_G_S_STATUS_2 = "mbThread.status = ?";
2644    private static final String _FINDER_COLUMN_C_P_CATEGORYID_2 = "mbThread.categoryId = ? AND ";
2645    private static final String _FINDER_COLUMN_C_P_PRIORITY_2 = "mbThread.priority = ?";
2646    private static final String _FINDER_COLUMN_G_C_L_GROUPID_2 = "mbThread.groupId = ? AND ";
2647    private static final String _FINDER_COLUMN_G_C_L_CATEGORYID_2 = "mbThread.categoryId = ? AND ";
2648    private static final String _FINDER_COLUMN_G_C_L_LASTPOSTDATE_1 = "mbThread.lastPostDate IS NULL";
2649    private static final String _FINDER_COLUMN_G_C_L_LASTPOSTDATE_2 = "mbThread.lastPostDate = ?";
2650    private static final String _FINDER_COLUMN_G_C_S_GROUPID_2 = "mbThread.groupId = ? AND ";
2651    private static final String _FINDER_COLUMN_G_C_S_CATEGORYID_2 = "mbThread.categoryId = ? AND ";
2652    private static final String _FINDER_COLUMN_G_C_S_STATUS_2 = "mbThread.status = ?";
2653    private static final String _ORDER_BY_ENTITY_ALIAS = "mbThread.";
2654    private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No MBThread exists with the primary key ";
2655    private static final String _NO_SUCH_ENTITY_WITH_KEY = "No MBThread exists with the key {";
2656    private static Log _log = LogFactoryUtil.getLog(MBThreadPersistenceImpl.class);
2657}