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.GetterUtil;
31  import com.liferay.portal.kernel.util.InstanceFactory;
32  import com.liferay.portal.kernel.util.OrderByComparator;
33  import com.liferay.portal.kernel.util.StringBundler;
34  import com.liferay.portal.kernel.util.StringPool;
35  import com.liferay.portal.kernel.util.StringUtil;
36  import com.liferay.portal.model.ModelListener;
37  import com.liferay.portal.service.persistence.BatchSessionUtil;
38  import com.liferay.portal.service.persistence.ResourcePersistence;
39  import com.liferay.portal.service.persistence.UserPersistence;
40  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
41  
42  import com.liferay.portlet.messageboards.NoSuchDiscussionException;
43  import com.liferay.portlet.messageboards.model.MBDiscussion;
44  import com.liferay.portlet.messageboards.model.impl.MBDiscussionImpl;
45  import com.liferay.portlet.messageboards.model.impl.MBDiscussionModelImpl;
46  
47  import java.io.Serializable;
48  
49  import java.util.ArrayList;
50  import java.util.Collections;
51  import java.util.List;
52  
53  /**
54   * <a href="MBDiscussionPersistenceImpl.java.html"><b><i>View Source</i></b></a>
55   *
56   * <p>
57   * ServiceBuilder generated this class. Modifications in this class will be
58   * overwritten the next time is generated.
59   * </p>
60   *
61   * @author    Brian Wing Shun Chan
62   * @see       MBDiscussionPersistence
63   * @see       MBDiscussionUtil
64   * @generated
65   */
66  public class MBDiscussionPersistenceImpl extends BasePersistenceImpl<MBDiscussion>
67      implements MBDiscussionPersistence {
68      public static final String FINDER_CLASS_NAME_ENTITY = MBDiscussionImpl.class.getName();
69      public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
70          ".List";
71      public static final FinderPath FINDER_PATH_FIND_BY_CLASSNAMEID = new FinderPath(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
72              MBDiscussionModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
73              "findByClassNameId",
74              new String[] {
75                  Long.class.getName(),
76                  
77              "java.lang.Integer", "java.lang.Integer",
78                  "com.liferay.portal.kernel.util.OrderByComparator"
79              });
80      public static final FinderPath FINDER_PATH_COUNT_BY_CLASSNAMEID = new FinderPath(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
81              MBDiscussionModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
82              "countByClassNameId", new String[] { Long.class.getName() });
83      public static final FinderPath FINDER_PATH_FETCH_BY_THREADID = new FinderPath(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
84              MBDiscussionModelImpl.FINDER_CACHE_ENABLED,
85              FINDER_CLASS_NAME_ENTITY, "fetchByThreadId",
86              new String[] { Long.class.getName() });
87      public static final FinderPath FINDER_PATH_COUNT_BY_THREADID = new FinderPath(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
88              MBDiscussionModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
89              "countByThreadId", new String[] { Long.class.getName() });
90      public static final FinderPath FINDER_PATH_FETCH_BY_C_C = new FinderPath(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
91              MBDiscussionModelImpl.FINDER_CACHE_ENABLED,
92              FINDER_CLASS_NAME_ENTITY, "fetchByC_C",
93              new String[] { Long.class.getName(), Long.class.getName() });
94      public static final FinderPath FINDER_PATH_COUNT_BY_C_C = new FinderPath(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
95              MBDiscussionModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
96              "countByC_C",
97              new String[] { Long.class.getName(), Long.class.getName() });
98      public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
99              MBDiscussionModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
100             "findAll", new String[0]);
101     public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
102             MBDiscussionModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
103             "countAll", new String[0]);
104 
105     public void cacheResult(MBDiscussion mbDiscussion) {
106         EntityCacheUtil.putResult(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
107             MBDiscussionImpl.class, mbDiscussion.getPrimaryKey(), mbDiscussion);
108 
109         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_THREADID,
110             new Object[] { new Long(mbDiscussion.getThreadId()) }, mbDiscussion);
111 
112         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
113             new Object[] {
114                 new Long(mbDiscussion.getClassNameId()),
115                 new Long(mbDiscussion.getClassPK())
116             }, mbDiscussion);
117     }
118 
119     public void cacheResult(List<MBDiscussion> mbDiscussions) {
120         for (MBDiscussion mbDiscussion : mbDiscussions) {
121             if (EntityCacheUtil.getResult(
122                         MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
123                         MBDiscussionImpl.class, mbDiscussion.getPrimaryKey(),
124                         this) == null) {
125                 cacheResult(mbDiscussion);
126             }
127         }
128     }
129 
130     public void clearCache() {
131         CacheRegistry.clear(MBDiscussionImpl.class.getName());
132         EntityCacheUtil.clearCache(MBDiscussionImpl.class.getName());
133         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
134         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
135     }
136 
137     public void clearCache(MBDiscussion mbDiscussion) {
138         EntityCacheUtil.removeResult(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
139             MBDiscussionImpl.class, mbDiscussion.getPrimaryKey());
140 
141         FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_THREADID,
142             new Object[] { new Long(mbDiscussion.getThreadId()) });
143 
144         FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_C,
145             new Object[] {
146                 new Long(mbDiscussion.getClassNameId()),
147                 new Long(mbDiscussion.getClassPK())
148             });
149     }
150 
151     public MBDiscussion create(long discussionId) {
152         MBDiscussion mbDiscussion = new MBDiscussionImpl();
153 
154         mbDiscussion.setNew(true);
155         mbDiscussion.setPrimaryKey(discussionId);
156 
157         return mbDiscussion;
158     }
159 
160     public MBDiscussion remove(Serializable primaryKey)
161         throws NoSuchModelException, SystemException {
162         return remove(((Long)primaryKey).longValue());
163     }
164 
165     public MBDiscussion remove(long discussionId)
166         throws NoSuchDiscussionException, SystemException {
167         Session session = null;
168 
169         try {
170             session = openSession();
171 
172             MBDiscussion mbDiscussion = (MBDiscussion)session.get(MBDiscussionImpl.class,
173                     new Long(discussionId));
174 
175             if (mbDiscussion == null) {
176                 if (_log.isWarnEnabled()) {
177                     _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + discussionId);
178                 }
179 
180                 throw new NoSuchDiscussionException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
181                     discussionId);
182             }
183 
184             return remove(mbDiscussion);
185         }
186         catch (NoSuchDiscussionException nsee) {
187             throw nsee;
188         }
189         catch (Exception e) {
190             throw processException(e);
191         }
192         finally {
193             closeSession(session);
194         }
195     }
196 
197     public MBDiscussion remove(MBDiscussion mbDiscussion)
198         throws SystemException {
199         for (ModelListener<MBDiscussion> listener : listeners) {
200             listener.onBeforeRemove(mbDiscussion);
201         }
202 
203         mbDiscussion = removeImpl(mbDiscussion);
204 
205         for (ModelListener<MBDiscussion> listener : listeners) {
206             listener.onAfterRemove(mbDiscussion);
207         }
208 
209         return mbDiscussion;
210     }
211 
212     protected MBDiscussion removeImpl(MBDiscussion mbDiscussion)
213         throws SystemException {
214         mbDiscussion = toUnwrappedModel(mbDiscussion);
215 
216         Session session = null;
217 
218         try {
219             session = openSession();
220 
221             if (mbDiscussion.isCachedModel() || BatchSessionUtil.isEnabled()) {
222                 Object staleObject = session.get(MBDiscussionImpl.class,
223                         mbDiscussion.getPrimaryKeyObj());
224 
225                 if (staleObject != null) {
226                     session.evict(staleObject);
227                 }
228             }
229 
230             session.delete(mbDiscussion);
231 
232             session.flush();
233         }
234         catch (Exception e) {
235             throw processException(e);
236         }
237         finally {
238             closeSession(session);
239         }
240 
241         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
242 
243         MBDiscussionModelImpl mbDiscussionModelImpl = (MBDiscussionModelImpl)mbDiscussion;
244 
245         FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_THREADID,
246             new Object[] { new Long(mbDiscussionModelImpl.getOriginalThreadId()) });
247 
248         FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_C,
249             new Object[] {
250                 new Long(mbDiscussionModelImpl.getOriginalClassNameId()),
251                 new Long(mbDiscussionModelImpl.getOriginalClassPK())
252             });
253 
254         EntityCacheUtil.removeResult(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
255             MBDiscussionImpl.class, mbDiscussion.getPrimaryKey());
256 
257         return mbDiscussion;
258     }
259 
260     public MBDiscussion updateImpl(
261         com.liferay.portlet.messageboards.model.MBDiscussion mbDiscussion,
262         boolean merge) throws SystemException {
263         mbDiscussion = toUnwrappedModel(mbDiscussion);
264 
265         boolean isNew = mbDiscussion.isNew();
266 
267         MBDiscussionModelImpl mbDiscussionModelImpl = (MBDiscussionModelImpl)mbDiscussion;
268 
269         Session session = null;
270 
271         try {
272             session = openSession();
273 
274             BatchSessionUtil.update(session, mbDiscussion, merge);
275 
276             mbDiscussion.setNew(false);
277         }
278         catch (Exception e) {
279             throw processException(e);
280         }
281         finally {
282             closeSession(session);
283         }
284 
285         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
286 
287         EntityCacheUtil.putResult(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
288             MBDiscussionImpl.class, mbDiscussion.getPrimaryKey(), mbDiscussion);
289 
290         if (!isNew &&
291                 (mbDiscussion.getThreadId() != mbDiscussionModelImpl.getOriginalThreadId())) {
292             FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_THREADID,
293                 new Object[] {
294                     new Long(mbDiscussionModelImpl.getOriginalThreadId())
295                 });
296         }
297 
298         if (isNew ||
299                 (mbDiscussion.getThreadId() != mbDiscussionModelImpl.getOriginalThreadId())) {
300             FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_THREADID,
301                 new Object[] { new Long(mbDiscussion.getThreadId()) },
302                 mbDiscussion);
303         }
304 
305         if (!isNew &&
306                 ((mbDiscussion.getClassNameId() != mbDiscussionModelImpl.getOriginalClassNameId()) ||
307                 (mbDiscussion.getClassPK() != mbDiscussionModelImpl.getOriginalClassPK()))) {
308             FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_C,
309                 new Object[] {
310                     new Long(mbDiscussionModelImpl.getOriginalClassNameId()),
311                     new Long(mbDiscussionModelImpl.getOriginalClassPK())
312                 });
313         }
314 
315         if (isNew ||
316                 ((mbDiscussion.getClassNameId() != mbDiscussionModelImpl.getOriginalClassNameId()) ||
317                 (mbDiscussion.getClassPK() != mbDiscussionModelImpl.getOriginalClassPK()))) {
318             FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
319                 new Object[] {
320                     new Long(mbDiscussion.getClassNameId()),
321                     new Long(mbDiscussion.getClassPK())
322                 }, mbDiscussion);
323         }
324 
325         return mbDiscussion;
326     }
327 
328     protected MBDiscussion toUnwrappedModel(MBDiscussion mbDiscussion) {
329         if (mbDiscussion instanceof MBDiscussionImpl) {
330             return mbDiscussion;
331         }
332 
333         MBDiscussionImpl mbDiscussionImpl = new MBDiscussionImpl();
334 
335         mbDiscussionImpl.setNew(mbDiscussion.isNew());
336         mbDiscussionImpl.setPrimaryKey(mbDiscussion.getPrimaryKey());
337 
338         mbDiscussionImpl.setDiscussionId(mbDiscussion.getDiscussionId());
339         mbDiscussionImpl.setClassNameId(mbDiscussion.getClassNameId());
340         mbDiscussionImpl.setClassPK(mbDiscussion.getClassPK());
341         mbDiscussionImpl.setThreadId(mbDiscussion.getThreadId());
342 
343         return mbDiscussionImpl;
344     }
345 
346     public MBDiscussion findByPrimaryKey(Serializable primaryKey)
347         throws NoSuchModelException, SystemException {
348         return findByPrimaryKey(((Long)primaryKey).longValue());
349     }
350 
351     public MBDiscussion findByPrimaryKey(long discussionId)
352         throws NoSuchDiscussionException, SystemException {
353         MBDiscussion mbDiscussion = fetchByPrimaryKey(discussionId);
354 
355         if (mbDiscussion == null) {
356             if (_log.isWarnEnabled()) {
357                 _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + discussionId);
358             }
359 
360             throw new NoSuchDiscussionException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
361                 discussionId);
362         }
363 
364         return mbDiscussion;
365     }
366 
367     public MBDiscussion fetchByPrimaryKey(Serializable primaryKey)
368         throws SystemException {
369         return fetchByPrimaryKey(((Long)primaryKey).longValue());
370     }
371 
372     public MBDiscussion fetchByPrimaryKey(long discussionId)
373         throws SystemException {
374         MBDiscussion mbDiscussion = (MBDiscussion)EntityCacheUtil.getResult(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
375                 MBDiscussionImpl.class, discussionId, this);
376 
377         if (mbDiscussion == null) {
378             Session session = null;
379 
380             try {
381                 session = openSession();
382 
383                 mbDiscussion = (MBDiscussion)session.get(MBDiscussionImpl.class,
384                         new Long(discussionId));
385             }
386             catch (Exception e) {
387                 throw processException(e);
388             }
389             finally {
390                 if (mbDiscussion != null) {
391                     cacheResult(mbDiscussion);
392                 }
393 
394                 closeSession(session);
395             }
396         }
397 
398         return mbDiscussion;
399     }
400 
401     public List<MBDiscussion> findByClassNameId(long classNameId)
402         throws SystemException {
403         return findByClassNameId(classNameId, QueryUtil.ALL_POS,
404             QueryUtil.ALL_POS, null);
405     }
406 
407     public List<MBDiscussion> findByClassNameId(long classNameId, int start,
408         int end) throws SystemException {
409         return findByClassNameId(classNameId, start, end, null);
410     }
411 
412     public List<MBDiscussion> findByClassNameId(long classNameId, int start,
413         int end, OrderByComparator orderByComparator) throws SystemException {
414         Object[] finderArgs = new Object[] {
415                 new Long(classNameId),
416                 
417                 String.valueOf(start), String.valueOf(end),
418                 String.valueOf(orderByComparator)
419             };
420 
421         List<MBDiscussion> list = (List<MBDiscussion>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_CLASSNAMEID,
422                 finderArgs, this);
423 
424         if (list == null) {
425             Session session = null;
426 
427             try {
428                 session = openSession();
429 
430                 StringBundler query = null;
431 
432                 if (orderByComparator != null) {
433                     query = new StringBundler(3 +
434                             (orderByComparator.getOrderByFields().length * 3));
435                 }
436                 else {
437                     query = new StringBundler(2);
438                 }
439 
440                 query.append(_SQL_SELECT_MBDISCUSSION_WHERE);
441 
442                 query.append(_FINDER_COLUMN_CLASSNAMEID_CLASSNAMEID_2);
443 
444                 if (orderByComparator != null) {
445                     appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
446                         orderByComparator);
447                 }
448 
449                 String sql = query.toString();
450 
451                 Query q = session.createQuery(sql);
452 
453                 QueryPos qPos = QueryPos.getInstance(q);
454 
455                 qPos.add(classNameId);
456 
457                 list = (List<MBDiscussion>)QueryUtil.list(q, getDialect(),
458                         start, end);
459             }
460             catch (Exception e) {
461                 throw processException(e);
462             }
463             finally {
464                 if (list == null) {
465                     list = new ArrayList<MBDiscussion>();
466                 }
467 
468                 cacheResult(list);
469 
470                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_CLASSNAMEID,
471                     finderArgs, list);
472 
473                 closeSession(session);
474             }
475         }
476 
477         return list;
478     }
479 
480     public MBDiscussion findByClassNameId_First(long classNameId,
481         OrderByComparator orderByComparator)
482         throws NoSuchDiscussionException, SystemException {
483         List<MBDiscussion> list = findByClassNameId(classNameId, 0, 1,
484                 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("classNameId=");
492             msg.append(classNameId);
493 
494             msg.append(StringPool.CLOSE_CURLY_BRACE);
495 
496             throw new NoSuchDiscussionException(msg.toString());
497         }
498         else {
499             return list.get(0);
500         }
501     }
502 
503     public MBDiscussion findByClassNameId_Last(long classNameId,
504         OrderByComparator orderByComparator)
505         throws NoSuchDiscussionException, SystemException {
506         int count = countByClassNameId(classNameId);
507 
508         List<MBDiscussion> list = findByClassNameId(classNameId, count - 1,
509                 count, 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("classNameId=");
517             msg.append(classNameId);
518 
519             msg.append(StringPool.CLOSE_CURLY_BRACE);
520 
521             throw new NoSuchDiscussionException(msg.toString());
522         }
523         else {
524             return list.get(0);
525         }
526     }
527 
528     public MBDiscussion[] findByClassNameId_PrevAndNext(long discussionId,
529         long classNameId, OrderByComparator orderByComparator)
530         throws NoSuchDiscussionException, SystemException {
531         MBDiscussion mbDiscussion = findByPrimaryKey(discussionId);
532 
533         Session session = null;
534 
535         try {
536             session = openSession();
537 
538             MBDiscussion[] array = new MBDiscussionImpl[3];
539 
540             array[0] = getByClassNameId_PrevAndNext(session, mbDiscussion,
541                     classNameId, orderByComparator, true);
542 
543             array[1] = mbDiscussion;
544 
545             array[2] = getByClassNameId_PrevAndNext(session, mbDiscussion,
546                     classNameId, 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 MBDiscussion getByClassNameId_PrevAndNext(Session session,
559         MBDiscussion mbDiscussion, long classNameId,
560         OrderByComparator orderByComparator, 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_MBDISCUSSION_WHERE);
572 
573         query.append(_FINDER_COLUMN_CLASSNAMEID_CLASSNAMEID_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         String sql = query.toString();
630 
631         Query q = session.createQuery(sql);
632 
633         q.setFirstResult(0);
634         q.setMaxResults(2);
635 
636         QueryPos qPos = QueryPos.getInstance(q);
637 
638         qPos.add(classNameId);
639 
640         if (orderByComparator != null) {
641             Object[] values = orderByComparator.getOrderByValues(mbDiscussion);
642 
643             for (Object value : values) {
644                 qPos.add(value);
645             }
646         }
647 
648         List<MBDiscussion> list = q.list();
649 
650         if (list.size() == 2) {
651             return list.get(1);
652         }
653         else {
654             return null;
655         }
656     }
657 
658     public MBDiscussion findByThreadId(long threadId)
659         throws NoSuchDiscussionException, SystemException {
660         MBDiscussion mbDiscussion = fetchByThreadId(threadId);
661 
662         if (mbDiscussion == null) {
663             StringBundler msg = new StringBundler(4);
664 
665             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
666 
667             msg.append("threadId=");
668             msg.append(threadId);
669 
670             msg.append(StringPool.CLOSE_CURLY_BRACE);
671 
672             if (_log.isWarnEnabled()) {
673                 _log.warn(msg.toString());
674             }
675 
676             throw new NoSuchDiscussionException(msg.toString());
677         }
678 
679         return mbDiscussion;
680     }
681 
682     public MBDiscussion fetchByThreadId(long threadId)
683         throws SystemException {
684         return fetchByThreadId(threadId, true);
685     }
686 
687     public MBDiscussion fetchByThreadId(long threadId, boolean retrieveFromCache)
688         throws SystemException {
689         Object[] finderArgs = new Object[] { new Long(threadId) };
690 
691         Object result = null;
692 
693         if (retrieveFromCache) {
694             result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_THREADID,
695                     finderArgs, this);
696         }
697 
698         if (result == null) {
699             Session session = null;
700 
701             try {
702                 session = openSession();
703 
704                 StringBundler query = new StringBundler(2);
705 
706                 query.append(_SQL_SELECT_MBDISCUSSION_WHERE);
707 
708                 query.append(_FINDER_COLUMN_THREADID_THREADID_2);
709 
710                 String sql = query.toString();
711 
712                 Query q = session.createQuery(sql);
713 
714                 QueryPos qPos = QueryPos.getInstance(q);
715 
716                 qPos.add(threadId);
717 
718                 List<MBDiscussion> list = q.list();
719 
720                 result = list;
721 
722                 MBDiscussion mbDiscussion = null;
723 
724                 if (list.isEmpty()) {
725                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_THREADID,
726                         finderArgs, list);
727                 }
728                 else {
729                     mbDiscussion = list.get(0);
730 
731                     cacheResult(mbDiscussion);
732 
733                     if ((mbDiscussion.getThreadId() != threadId)) {
734                         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_THREADID,
735                             finderArgs, mbDiscussion);
736                     }
737                 }
738 
739                 return mbDiscussion;
740             }
741             catch (Exception e) {
742                 throw processException(e);
743             }
744             finally {
745                 if (result == null) {
746                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_THREADID,
747                         finderArgs, new ArrayList<MBDiscussion>());
748                 }
749 
750                 closeSession(session);
751             }
752         }
753         else {
754             if (result instanceof List<?>) {
755                 return null;
756             }
757             else {
758                 return (MBDiscussion)result;
759             }
760         }
761     }
762 
763     public MBDiscussion findByC_C(long classNameId, long classPK)
764         throws NoSuchDiscussionException, SystemException {
765         MBDiscussion mbDiscussion = fetchByC_C(classNameId, classPK);
766 
767         if (mbDiscussion == null) {
768             StringBundler msg = new StringBundler(6);
769 
770             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
771 
772             msg.append("classNameId=");
773             msg.append(classNameId);
774 
775             msg.append(", classPK=");
776             msg.append(classPK);
777 
778             msg.append(StringPool.CLOSE_CURLY_BRACE);
779 
780             if (_log.isWarnEnabled()) {
781                 _log.warn(msg.toString());
782             }
783 
784             throw new NoSuchDiscussionException(msg.toString());
785         }
786 
787         return mbDiscussion;
788     }
789 
790     public MBDiscussion fetchByC_C(long classNameId, long classPK)
791         throws SystemException {
792         return fetchByC_C(classNameId, classPK, true);
793     }
794 
795     public MBDiscussion fetchByC_C(long classNameId, long classPK,
796         boolean retrieveFromCache) throws SystemException {
797         Object[] finderArgs = new Object[] {
798                 new Long(classNameId), new Long(classPK)
799             };
800 
801         Object result = null;
802 
803         if (retrieveFromCache) {
804             result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_C_C,
805                     finderArgs, this);
806         }
807 
808         if (result == null) {
809             Session session = null;
810 
811             try {
812                 session = openSession();
813 
814                 StringBundler query = new StringBundler(3);
815 
816                 query.append(_SQL_SELECT_MBDISCUSSION_WHERE);
817 
818                 query.append(_FINDER_COLUMN_C_C_CLASSNAMEID_2);
819 
820                 query.append(_FINDER_COLUMN_C_C_CLASSPK_2);
821 
822                 String sql = query.toString();
823 
824                 Query q = session.createQuery(sql);
825 
826                 QueryPos qPos = QueryPos.getInstance(q);
827 
828                 qPos.add(classNameId);
829 
830                 qPos.add(classPK);
831 
832                 List<MBDiscussion> list = q.list();
833 
834                 result = list;
835 
836                 MBDiscussion mbDiscussion = null;
837 
838                 if (list.isEmpty()) {
839                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
840                         finderArgs, list);
841                 }
842                 else {
843                     mbDiscussion = list.get(0);
844 
845                     cacheResult(mbDiscussion);
846 
847                     if ((mbDiscussion.getClassNameId() != classNameId) ||
848                             (mbDiscussion.getClassPK() != classPK)) {
849                         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
850                             finderArgs, mbDiscussion);
851                     }
852                 }
853 
854                 return mbDiscussion;
855             }
856             catch (Exception e) {
857                 throw processException(e);
858             }
859             finally {
860                 if (result == null) {
861                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
862                         finderArgs, new ArrayList<MBDiscussion>());
863                 }
864 
865                 closeSession(session);
866             }
867         }
868         else {
869             if (result instanceof List<?>) {
870                 return null;
871             }
872             else {
873                 return (MBDiscussion)result;
874             }
875         }
876     }
877 
878     public List<MBDiscussion> findAll() throws SystemException {
879         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
880     }
881 
882     public List<MBDiscussion> findAll(int start, int end)
883         throws SystemException {
884         return findAll(start, end, null);
885     }
886 
887     public List<MBDiscussion> findAll(int start, int end,
888         OrderByComparator orderByComparator) throws SystemException {
889         Object[] finderArgs = new Object[] {
890                 String.valueOf(start), String.valueOf(end),
891                 String.valueOf(orderByComparator)
892             };
893 
894         List<MBDiscussion> list = (List<MBDiscussion>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
895                 finderArgs, this);
896 
897         if (list == null) {
898             Session session = null;
899 
900             try {
901                 session = openSession();
902 
903                 StringBundler query = null;
904                 String sql = null;
905 
906                 if (orderByComparator != null) {
907                     query = new StringBundler(2 +
908                             (orderByComparator.getOrderByFields().length * 3));
909 
910                     query.append(_SQL_SELECT_MBDISCUSSION);
911 
912                     appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS,
913                         orderByComparator);
914 
915                     sql = query.toString();
916                 }
917 
918                 sql = _SQL_SELECT_MBDISCUSSION;
919 
920                 Query q = session.createQuery(sql);
921 
922                 if (orderByComparator == null) {
923                     list = (List<MBDiscussion>)QueryUtil.list(q, getDialect(),
924                             start, end, false);
925 
926                     Collections.sort(list);
927                 }
928                 else {
929                     list = (List<MBDiscussion>)QueryUtil.list(q, getDialect(),
930                             start, end);
931                 }
932             }
933             catch (Exception e) {
934                 throw processException(e);
935             }
936             finally {
937                 if (list == null) {
938                     list = new ArrayList<MBDiscussion>();
939                 }
940 
941                 cacheResult(list);
942 
943                 FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
944 
945                 closeSession(session);
946             }
947         }
948 
949         return list;
950     }
951 
952     public void removeByClassNameId(long classNameId) throws SystemException {
953         for (MBDiscussion mbDiscussion : findByClassNameId(classNameId)) {
954             remove(mbDiscussion);
955         }
956     }
957 
958     public void removeByThreadId(long threadId)
959         throws NoSuchDiscussionException, SystemException {
960         MBDiscussion mbDiscussion = findByThreadId(threadId);
961 
962         remove(mbDiscussion);
963     }
964 
965     public void removeByC_C(long classNameId, long classPK)
966         throws NoSuchDiscussionException, SystemException {
967         MBDiscussion mbDiscussion = findByC_C(classNameId, classPK);
968 
969         remove(mbDiscussion);
970     }
971 
972     public void removeAll() throws SystemException {
973         for (MBDiscussion mbDiscussion : findAll()) {
974             remove(mbDiscussion);
975         }
976     }
977 
978     public int countByClassNameId(long classNameId) throws SystemException {
979         Object[] finderArgs = new Object[] { new Long(classNameId) };
980 
981         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_CLASSNAMEID,
982                 finderArgs, this);
983 
984         if (count == null) {
985             Session session = null;
986 
987             try {
988                 session = openSession();
989 
990                 StringBundler query = new StringBundler(2);
991 
992                 query.append(_SQL_COUNT_MBDISCUSSION_WHERE);
993 
994                 query.append(_FINDER_COLUMN_CLASSNAMEID_CLASSNAMEID_2);
995 
996                 String sql = query.toString();
997 
998                 Query q = session.createQuery(sql);
999 
1000                QueryPos qPos = QueryPos.getInstance(q);
1001
1002                qPos.add(classNameId);
1003
1004                count = (Long)q.uniqueResult();
1005            }
1006            catch (Exception e) {
1007                throw processException(e);
1008            }
1009            finally {
1010                if (count == null) {
1011                    count = Long.valueOf(0);
1012                }
1013
1014                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_CLASSNAMEID,
1015                    finderArgs, count);
1016
1017                closeSession(session);
1018            }
1019        }
1020
1021        return count.intValue();
1022    }
1023
1024    public int countByThreadId(long threadId) throws SystemException {
1025        Object[] finderArgs = new Object[] { new Long(threadId) };
1026
1027        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_THREADID,
1028                finderArgs, this);
1029
1030        if (count == null) {
1031            Session session = null;
1032
1033            try {
1034                session = openSession();
1035
1036                StringBundler query = new StringBundler(2);
1037
1038                query.append(_SQL_COUNT_MBDISCUSSION_WHERE);
1039
1040                query.append(_FINDER_COLUMN_THREADID_THREADID_2);
1041
1042                String sql = query.toString();
1043
1044                Query q = session.createQuery(sql);
1045
1046                QueryPos qPos = QueryPos.getInstance(q);
1047
1048                qPos.add(threadId);
1049
1050                count = (Long)q.uniqueResult();
1051            }
1052            catch (Exception e) {
1053                throw processException(e);
1054            }
1055            finally {
1056                if (count == null) {
1057                    count = Long.valueOf(0);
1058                }
1059
1060                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_THREADID,
1061                    finderArgs, count);
1062
1063                closeSession(session);
1064            }
1065        }
1066
1067        return count.intValue();
1068    }
1069
1070    public int countByC_C(long classNameId, long classPK)
1071        throws SystemException {
1072        Object[] finderArgs = new Object[] {
1073                new Long(classNameId), new Long(classPK)
1074            };
1075
1076        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_C_C,
1077                finderArgs, this);
1078
1079        if (count == null) {
1080            Session session = null;
1081
1082            try {
1083                session = openSession();
1084
1085                StringBundler query = new StringBundler(3);
1086
1087                query.append(_SQL_COUNT_MBDISCUSSION_WHERE);
1088
1089                query.append(_FINDER_COLUMN_C_C_CLASSNAMEID_2);
1090
1091                query.append(_FINDER_COLUMN_C_C_CLASSPK_2);
1092
1093                String sql = query.toString();
1094
1095                Query q = session.createQuery(sql);
1096
1097                QueryPos qPos = QueryPos.getInstance(q);
1098
1099                qPos.add(classNameId);
1100
1101                qPos.add(classPK);
1102
1103                count = (Long)q.uniqueResult();
1104            }
1105            catch (Exception e) {
1106                throw processException(e);
1107            }
1108            finally {
1109                if (count == null) {
1110                    count = Long.valueOf(0);
1111                }
1112
1113                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_C, finderArgs,
1114                    count);
1115
1116                closeSession(session);
1117            }
1118        }
1119
1120        return count.intValue();
1121    }
1122
1123    public int countAll() throws SystemException {
1124        Object[] finderArgs = new Object[0];
1125
1126        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1127                finderArgs, this);
1128
1129        if (count == null) {
1130            Session session = null;
1131
1132            try {
1133                session = openSession();
1134
1135                Query q = session.createQuery(_SQL_COUNT_MBDISCUSSION);
1136
1137                count = (Long)q.uniqueResult();
1138            }
1139            catch (Exception e) {
1140                throw processException(e);
1141            }
1142            finally {
1143                if (count == null) {
1144                    count = Long.valueOf(0);
1145                }
1146
1147                FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
1148                    count);
1149
1150                closeSession(session);
1151            }
1152        }
1153
1154        return count.intValue();
1155    }
1156
1157    public void afterPropertiesSet() {
1158        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1159                    com.liferay.portal.util.PropsUtil.get(
1160                        "value.object.listener.com.liferay.portlet.messageboards.model.MBDiscussion")));
1161
1162        if (listenerClassNames.length > 0) {
1163            try {
1164                List<ModelListener<MBDiscussion>> listenersList = new ArrayList<ModelListener<MBDiscussion>>();
1165
1166                for (String listenerClassName : listenerClassNames) {
1167                    listenersList.add((ModelListener<MBDiscussion>)InstanceFactory.newInstance(
1168                            listenerClassName));
1169                }
1170
1171                listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1172            }
1173            catch (Exception e) {
1174                _log.error(e);
1175            }
1176        }
1177    }
1178
1179    @BeanReference(type = MBBanPersistence.class)
1180    protected MBBanPersistence mbBanPersistence;
1181    @BeanReference(type = MBCategoryPersistence.class)
1182    protected MBCategoryPersistence mbCategoryPersistence;
1183    @BeanReference(type = MBDiscussionPersistence.class)
1184    protected MBDiscussionPersistence mbDiscussionPersistence;
1185    @BeanReference(type = MBMailingListPersistence.class)
1186    protected MBMailingListPersistence mbMailingListPersistence;
1187    @BeanReference(type = MBMessagePersistence.class)
1188    protected MBMessagePersistence mbMessagePersistence;
1189    @BeanReference(type = MBMessageFlagPersistence.class)
1190    protected MBMessageFlagPersistence mbMessageFlagPersistence;
1191    @BeanReference(type = MBStatsUserPersistence.class)
1192    protected MBStatsUserPersistence mbStatsUserPersistence;
1193    @BeanReference(type = MBThreadPersistence.class)
1194    protected MBThreadPersistence mbThreadPersistence;
1195    @BeanReference(type = ResourcePersistence.class)
1196    protected ResourcePersistence resourcePersistence;
1197    @BeanReference(type = UserPersistence.class)
1198    protected UserPersistence userPersistence;
1199    private static final String _SQL_SELECT_MBDISCUSSION = "SELECT mbDiscussion FROM MBDiscussion mbDiscussion";
1200    private static final String _SQL_SELECT_MBDISCUSSION_WHERE = "SELECT mbDiscussion FROM MBDiscussion mbDiscussion WHERE ";
1201    private static final String _SQL_COUNT_MBDISCUSSION = "SELECT COUNT(mbDiscussion) FROM MBDiscussion mbDiscussion";
1202    private static final String _SQL_COUNT_MBDISCUSSION_WHERE = "SELECT COUNT(mbDiscussion) FROM MBDiscussion mbDiscussion WHERE ";
1203    private static final String _FINDER_COLUMN_CLASSNAMEID_CLASSNAMEID_2 = "mbDiscussion.classNameId = ?";
1204    private static final String _FINDER_COLUMN_THREADID_THREADID_2 = "mbDiscussion.threadId = ?";
1205    private static final String _FINDER_COLUMN_C_C_CLASSNAMEID_2 = "mbDiscussion.classNameId = ? AND ";
1206    private static final String _FINDER_COLUMN_C_C_CLASSPK_2 = "mbDiscussion.classPK = ?";
1207    private static final String _ORDER_BY_ENTITY_ALIAS = "mbDiscussion.";
1208    private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No MBDiscussion exists with the primary key ";
1209    private static final String _NO_SUCH_ENTITY_WITH_KEY = "No MBDiscussion exists with the key {";
1210    private static Log _log = LogFactoryUtil.getLog(MBDiscussionPersistenceImpl.class);
1211}