1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   * 
13   */
14  
15  package com.liferay.portlet.messageboards.service.persistence;
16  
17  import com.liferay.portal.NoSuchModelException;
18  import com.liferay.portal.SystemException;
19  import com.liferay.portal.kernel.annotation.BeanReference;
20  import com.liferay.portal.kernel.cache.CacheRegistry;
21  import com.liferay.portal.kernel.dao.orm.DynamicQuery;
22  import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
23  import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
24  import com.liferay.portal.kernel.dao.orm.FinderPath;
25  import com.liferay.portal.kernel.dao.orm.Query;
26  import com.liferay.portal.kernel.dao.orm.QueryPos;
27  import com.liferay.portal.kernel.dao.orm.QueryUtil;
28  import com.liferay.portal.kernel.dao.orm.Session;
29  import com.liferay.portal.kernel.log.Log;
30  import com.liferay.portal.kernel.log.LogFactoryUtil;
31  import com.liferay.portal.kernel.util.GetterUtil;
32  import com.liferay.portal.kernel.util.OrderByComparator;
33  import com.liferay.portal.kernel.util.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.impl.BasePersistenceImpl;
39  
40  import com.liferay.portlet.messageboards.NoSuchDiscussionException;
41  import com.liferay.portlet.messageboards.model.MBDiscussion;
42  import com.liferay.portlet.messageboards.model.impl.MBDiscussionImpl;
43  import com.liferay.portlet.messageboards.model.impl.MBDiscussionModelImpl;
44  
45  import java.io.Serializable;
46  
47  import java.util.ArrayList;
48  import java.util.Collections;
49  import java.util.List;
50  
51  /**
52   * <a href="MBDiscussionPersistenceImpl.java.html"><b><i>View Source</i></b></a>
53   *
54   * <p>
55   * ServiceBuilder generated this class. Modifications in this class will be
56   * overwritten the next time is generated.
57   * </p>
58   *
59   * @author    Brian Wing Shun Chan
60   * @see       MBDiscussionPersistence
61   * @see       MBDiscussionUtil
62   * @generated
63   */
64  public class MBDiscussionPersistenceImpl extends BasePersistenceImpl<MBDiscussion>
65      implements MBDiscussionPersistence {
66      public static final String FINDER_CLASS_NAME_ENTITY = MBDiscussionImpl.class.getName();
67      public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
68          ".List";
69      public static final FinderPath FINDER_PATH_FIND_BY_CLASSNAMEID = new FinderPath(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
70              MBDiscussionModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
71              "findByClassNameId", new String[] { Long.class.getName() });
72      public static final FinderPath FINDER_PATH_FIND_BY_OBC_CLASSNAMEID = new FinderPath(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
73              MBDiscussionModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
74              "findByClassNameId",
75              new String[] {
76                  Long.class.getName(),
77                  
78              "java.lang.Integer", "java.lang.Integer",
79                  "com.liferay.portal.kernel.util.OrderByComparator"
80              });
81      public static final FinderPath FINDER_PATH_COUNT_BY_CLASSNAMEID = new FinderPath(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
82              MBDiscussionModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
83              "countByClassNameId", new String[] { Long.class.getName() });
84      public static final FinderPath FINDER_PATH_FETCH_BY_THREADID = new FinderPath(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
85              MBDiscussionModelImpl.FINDER_CACHE_ENABLED,
86              FINDER_CLASS_NAME_ENTITY, "fetchByThreadId",
87              new String[] { Long.class.getName() });
88      public static final FinderPath FINDER_PATH_COUNT_BY_THREADID = new FinderPath(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
89              MBDiscussionModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
90              "countByThreadId", new String[] { Long.class.getName() });
91      public static final FinderPath FINDER_PATH_FETCH_BY_C_C = new FinderPath(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
92              MBDiscussionModelImpl.FINDER_CACHE_ENABLED,
93              FINDER_CLASS_NAME_ENTITY, "fetchByC_C",
94              new String[] { Long.class.getName(), Long.class.getName() });
95      public static final FinderPath FINDER_PATH_COUNT_BY_C_C = new FinderPath(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
96              MBDiscussionModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
97              "countByC_C",
98              new String[] { Long.class.getName(), Long.class.getName() });
99      public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
100             MBDiscussionModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
101             "findAll", new String[0]);
102     public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
103             MBDiscussionModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
104             "countAll", new String[0]);
105 
106     public void cacheResult(MBDiscussion mbDiscussion) {
107         EntityCacheUtil.putResult(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
108             MBDiscussionImpl.class, mbDiscussion.getPrimaryKey(), mbDiscussion);
109 
110         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_THREADID,
111             new Object[] { new Long(mbDiscussion.getThreadId()) }, mbDiscussion);
112 
113         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
114             new Object[] {
115                 new Long(mbDiscussion.getClassNameId()),
116                 new Long(mbDiscussion.getClassPK())
117             }, mbDiscussion);
118     }
119 
120     public void cacheResult(List<MBDiscussion> mbDiscussions) {
121         for (MBDiscussion mbDiscussion : mbDiscussions) {
122             if (EntityCacheUtil.getResult(
123                         MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
124                         MBDiscussionImpl.class, mbDiscussion.getPrimaryKey(),
125                         this) == null) {
126                 cacheResult(mbDiscussion);
127             }
128         }
129     }
130 
131     public void clearCache() {
132         CacheRegistry.clear(MBDiscussionImpl.class.getName());
133         EntityCacheUtil.clearCache(MBDiscussionImpl.class.getName());
134         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
135         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
136     }
137 
138     public MBDiscussion create(long discussionId) {
139         MBDiscussion mbDiscussion = new MBDiscussionImpl();
140 
141         mbDiscussion.setNew(true);
142         mbDiscussion.setPrimaryKey(discussionId);
143 
144         return mbDiscussion;
145     }
146 
147     public MBDiscussion remove(Serializable primaryKey)
148         throws NoSuchModelException, SystemException {
149         return remove(((Long)primaryKey).longValue());
150     }
151 
152     public MBDiscussion remove(long discussionId)
153         throws NoSuchDiscussionException, SystemException {
154         Session session = null;
155 
156         try {
157             session = openSession();
158 
159             MBDiscussion mbDiscussion = (MBDiscussion)session.get(MBDiscussionImpl.class,
160                     new Long(discussionId));
161 
162             if (mbDiscussion == null) {
163                 if (_log.isWarnEnabled()) {
164                     _log.warn(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY + discussionId);
165                 }
166 
167                 throw new NoSuchDiscussionException(_NO_SUCH_ENTITY_WITH_PRIMARY_KEY +
168                     discussionId);
169             }
170 
171             return remove(mbDiscussion);
172         }
173         catch (NoSuchDiscussionException nsee) {
174             throw nsee;
175         }
176         catch (Exception e) {
177             throw processException(e);
178         }
179         finally {
180             closeSession(session);
181         }
182     }
183 
184     public MBDiscussion remove(MBDiscussion mbDiscussion)
185         throws SystemException {
186         for (ModelListener<MBDiscussion> listener : listeners) {
187             listener.onBeforeRemove(mbDiscussion);
188         }
189 
190         mbDiscussion = removeImpl(mbDiscussion);
191 
192         for (ModelListener<MBDiscussion> listener : listeners) {
193             listener.onAfterRemove(mbDiscussion);
194         }
195 
196         return mbDiscussion;
197     }
198 
199     protected MBDiscussion removeImpl(MBDiscussion mbDiscussion)
200         throws SystemException {
201         mbDiscussion = toUnwrappedModel(mbDiscussion);
202 
203         Session session = null;
204 
205         try {
206             session = openSession();
207 
208             if (mbDiscussion.isCachedModel() || BatchSessionUtil.isEnabled()) {
209                 Object staleObject = session.get(MBDiscussionImpl.class,
210                         mbDiscussion.getPrimaryKeyObj());
211 
212                 if (staleObject != null) {
213                     session.evict(staleObject);
214                 }
215             }
216 
217             session.delete(mbDiscussion);
218 
219             session.flush();
220         }
221         catch (Exception e) {
222             throw processException(e);
223         }
224         finally {
225             closeSession(session);
226         }
227 
228         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
229 
230         MBDiscussionModelImpl mbDiscussionModelImpl = (MBDiscussionModelImpl)mbDiscussion;
231 
232         FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_THREADID,
233             new Object[] { new Long(mbDiscussionModelImpl.getOriginalThreadId()) });
234 
235         FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_C,
236             new Object[] {
237                 new Long(mbDiscussionModelImpl.getOriginalClassNameId()),
238                 new Long(mbDiscussionModelImpl.getOriginalClassPK())
239             });
240 
241         EntityCacheUtil.removeResult(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
242             MBDiscussionImpl.class, mbDiscussion.getPrimaryKey());
243 
244         return mbDiscussion;
245     }
246 
247     /**
248      * @deprecated Use {@link BasePersistence#update(com.liferay.portal.model.BaseModel, boolean)}.
249      */
250     public MBDiscussion update(MBDiscussion mbDiscussion)
251         throws SystemException {
252         if (_log.isWarnEnabled()) {
253             _log.warn(
254                 "Using the deprecated update(MBDiscussion mbDiscussion) method. Use update(MBDiscussion mbDiscussion, boolean merge) instead.");
255         }
256 
257         return update(mbDiscussion, false);
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         Object[] finderArgs = new Object[] { new Long(classNameId) };
404 
405         List<MBDiscussion> list = (List<MBDiscussion>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_CLASSNAMEID,
406                 finderArgs, this);
407 
408         if (list == null) {
409             Session session = null;
410 
411             try {
412                 session = openSession();
413 
414                 StringBundler query = new StringBundler(2);
415 
416                 query.append(_SQL_SELECT_MBDISCUSSION_WHERE);
417 
418                 query.append(_FINDER_COLUMN_CLASSNAMEID_CLASSNAMEID_2);
419 
420                 String sql = query.toString();
421 
422                 Query q = session.createQuery(sql);
423 
424                 QueryPos qPos = QueryPos.getInstance(q);
425 
426                 qPos.add(classNameId);
427 
428                 list = q.list();
429             }
430             catch (Exception e) {
431                 throw processException(e);
432             }
433             finally {
434                 if (list == null) {
435                     list = new ArrayList<MBDiscussion>();
436                 }
437 
438                 cacheResult(list);
439 
440                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_CLASSNAMEID,
441                     finderArgs, list);
442 
443                 closeSession(session);
444             }
445         }
446 
447         return list;
448     }
449 
450     public List<MBDiscussion> findByClassNameId(long classNameId, int start,
451         int end) throws SystemException {
452         return findByClassNameId(classNameId, start, end, null);
453     }
454 
455     public List<MBDiscussion> findByClassNameId(long classNameId, int start,
456         int end, OrderByComparator obc) throws SystemException {
457         Object[] finderArgs = new Object[] {
458                 new Long(classNameId),
459                 
460                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
461             };
462 
463         List<MBDiscussion> list = (List<MBDiscussion>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_OBC_CLASSNAMEID,
464                 finderArgs, this);
465 
466         if (list == null) {
467             Session session = null;
468 
469             try {
470                 session = openSession();
471 
472                 StringBundler query = null;
473 
474                 if (obc != null) {
475                     query = new StringBundler(3 +
476                             (obc.getOrderByFields().length * 3));
477                 }
478                 else {
479                     query = new StringBundler(2);
480                 }
481 
482                 query.append(_SQL_SELECT_MBDISCUSSION_WHERE);
483 
484                 query.append(_FINDER_COLUMN_CLASSNAMEID_CLASSNAMEID_2);
485 
486                 if (obc != null) {
487                     appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS, obc);
488                 }
489 
490                 String sql = query.toString();
491 
492                 Query q = session.createQuery(sql);
493 
494                 QueryPos qPos = QueryPos.getInstance(q);
495 
496                 qPos.add(classNameId);
497 
498                 list = (List<MBDiscussion>)QueryUtil.list(q, getDialect(),
499                         start, end);
500             }
501             catch (Exception e) {
502                 throw processException(e);
503             }
504             finally {
505                 if (list == null) {
506                     list = new ArrayList<MBDiscussion>();
507                 }
508 
509                 cacheResult(list);
510 
511                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_OBC_CLASSNAMEID,
512                     finderArgs, list);
513 
514                 closeSession(session);
515             }
516         }
517 
518         return list;
519     }
520 
521     public MBDiscussion findByClassNameId_First(long classNameId,
522         OrderByComparator obc)
523         throws NoSuchDiscussionException, SystemException {
524         List<MBDiscussion> list = findByClassNameId(classNameId, 0, 1, obc);
525 
526         if (list.isEmpty()) {
527             StringBundler msg = new StringBundler(4);
528 
529             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
530 
531             msg.append("classNameId=");
532             msg.append(classNameId);
533 
534             msg.append(StringPool.CLOSE_CURLY_BRACE);
535 
536             throw new NoSuchDiscussionException(msg.toString());
537         }
538         else {
539             return list.get(0);
540         }
541     }
542 
543     public MBDiscussion findByClassNameId_Last(long classNameId,
544         OrderByComparator obc)
545         throws NoSuchDiscussionException, SystemException {
546         int count = countByClassNameId(classNameId);
547 
548         List<MBDiscussion> list = findByClassNameId(classNameId, count - 1,
549                 count, obc);
550 
551         if (list.isEmpty()) {
552             StringBundler msg = new StringBundler(4);
553 
554             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
555 
556             msg.append("classNameId=");
557             msg.append(classNameId);
558 
559             msg.append(StringPool.CLOSE_CURLY_BRACE);
560 
561             throw new NoSuchDiscussionException(msg.toString());
562         }
563         else {
564             return list.get(0);
565         }
566     }
567 
568     public MBDiscussion[] findByClassNameId_PrevAndNext(long discussionId,
569         long classNameId, OrderByComparator obc)
570         throws NoSuchDiscussionException, SystemException {
571         MBDiscussion mbDiscussion = findByPrimaryKey(discussionId);
572 
573         int count = countByClassNameId(classNameId);
574 
575         Session session = null;
576 
577         try {
578             session = openSession();
579 
580             StringBundler query = null;
581 
582             if (obc != null) {
583                 query = new StringBundler(3 +
584                         (obc.getOrderByFields().length * 3));
585             }
586             else {
587                 query = new StringBundler(2);
588             }
589 
590             query.append(_SQL_SELECT_MBDISCUSSION_WHERE);
591 
592             query.append(_FINDER_COLUMN_CLASSNAMEID_CLASSNAMEID_2);
593 
594             if (obc != null) {
595                 appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS, obc);
596             }
597 
598             String sql = query.toString();
599 
600             Query q = session.createQuery(sql);
601 
602             QueryPos qPos = QueryPos.getInstance(q);
603 
604             qPos.add(classNameId);
605 
606             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
607                     mbDiscussion);
608 
609             MBDiscussion[] array = new MBDiscussionImpl[3];
610 
611             array[0] = (MBDiscussion)objArray[0];
612             array[1] = (MBDiscussion)objArray[1];
613             array[2] = (MBDiscussion)objArray[2];
614 
615             return array;
616         }
617         catch (Exception e) {
618             throw processException(e);
619         }
620         finally {
621             closeSession(session);
622         }
623     }
624 
625     public MBDiscussion findByThreadId(long threadId)
626         throws NoSuchDiscussionException, SystemException {
627         MBDiscussion mbDiscussion = fetchByThreadId(threadId);
628 
629         if (mbDiscussion == null) {
630             StringBundler msg = new StringBundler(4);
631 
632             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
633 
634             msg.append("threadId=");
635             msg.append(threadId);
636 
637             msg.append(StringPool.CLOSE_CURLY_BRACE);
638 
639             if (_log.isWarnEnabled()) {
640                 _log.warn(msg.toString());
641             }
642 
643             throw new NoSuchDiscussionException(msg.toString());
644         }
645 
646         return mbDiscussion;
647     }
648 
649     public MBDiscussion fetchByThreadId(long threadId)
650         throws SystemException {
651         return fetchByThreadId(threadId, true);
652     }
653 
654     public MBDiscussion fetchByThreadId(long threadId, boolean retrieveFromCache)
655         throws SystemException {
656         Object[] finderArgs = new Object[] { new Long(threadId) };
657 
658         Object result = null;
659 
660         if (retrieveFromCache) {
661             result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_THREADID,
662                     finderArgs, this);
663         }
664 
665         if (result == null) {
666             Session session = null;
667 
668             try {
669                 session = openSession();
670 
671                 StringBundler query = new StringBundler(2);
672 
673                 query.append(_SQL_SELECT_MBDISCUSSION_WHERE);
674 
675                 query.append(_FINDER_COLUMN_THREADID_THREADID_2);
676 
677                 String sql = query.toString();
678 
679                 Query q = session.createQuery(sql);
680 
681                 QueryPos qPos = QueryPos.getInstance(q);
682 
683                 qPos.add(threadId);
684 
685                 List<MBDiscussion> list = q.list();
686 
687                 result = list;
688 
689                 MBDiscussion mbDiscussion = null;
690 
691                 if (list.isEmpty()) {
692                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_THREADID,
693                         finderArgs, list);
694                 }
695                 else {
696                     mbDiscussion = list.get(0);
697 
698                     cacheResult(mbDiscussion);
699 
700                     if ((mbDiscussion.getThreadId() != threadId)) {
701                         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_THREADID,
702                             finderArgs, mbDiscussion);
703                     }
704                 }
705 
706                 return mbDiscussion;
707             }
708             catch (Exception e) {
709                 throw processException(e);
710             }
711             finally {
712                 if (result == null) {
713                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_THREADID,
714                         finderArgs, new ArrayList<MBDiscussion>());
715                 }
716 
717                 closeSession(session);
718             }
719         }
720         else {
721             if (result instanceof List<?>) {
722                 return null;
723             }
724             else {
725                 return (MBDiscussion)result;
726             }
727         }
728     }
729 
730     public MBDiscussion findByC_C(long classNameId, long classPK)
731         throws NoSuchDiscussionException, SystemException {
732         MBDiscussion mbDiscussion = fetchByC_C(classNameId, classPK);
733 
734         if (mbDiscussion == null) {
735             StringBundler msg = new StringBundler(6);
736 
737             msg.append(_NO_SUCH_ENTITY_WITH_KEY);
738 
739             msg.append("classNameId=");
740             msg.append(classNameId);
741 
742             msg.append(", classPK=");
743             msg.append(classPK);
744 
745             msg.append(StringPool.CLOSE_CURLY_BRACE);
746 
747             if (_log.isWarnEnabled()) {
748                 _log.warn(msg.toString());
749             }
750 
751             throw new NoSuchDiscussionException(msg.toString());
752         }
753 
754         return mbDiscussion;
755     }
756 
757     public MBDiscussion fetchByC_C(long classNameId, long classPK)
758         throws SystemException {
759         return fetchByC_C(classNameId, classPK, true);
760     }
761 
762     public MBDiscussion fetchByC_C(long classNameId, long classPK,
763         boolean retrieveFromCache) throws SystemException {
764         Object[] finderArgs = new Object[] {
765                 new Long(classNameId), new Long(classPK)
766             };
767 
768         Object result = null;
769 
770         if (retrieveFromCache) {
771             result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_C_C,
772                     finderArgs, this);
773         }
774 
775         if (result == null) {
776             Session session = null;
777 
778             try {
779                 session = openSession();
780 
781                 StringBundler query = new StringBundler(3);
782 
783                 query.append(_SQL_SELECT_MBDISCUSSION_WHERE);
784 
785                 query.append(_FINDER_COLUMN_C_C_CLASSNAMEID_2);
786 
787                 query.append(_FINDER_COLUMN_C_C_CLASSPK_2);
788 
789                 String sql = query.toString();
790 
791                 Query q = session.createQuery(sql);
792 
793                 QueryPos qPos = QueryPos.getInstance(q);
794 
795                 qPos.add(classNameId);
796 
797                 qPos.add(classPK);
798 
799                 List<MBDiscussion> list = q.list();
800 
801                 result = list;
802 
803                 MBDiscussion mbDiscussion = null;
804 
805                 if (list.isEmpty()) {
806                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
807                         finderArgs, list);
808                 }
809                 else {
810                     mbDiscussion = list.get(0);
811 
812                     cacheResult(mbDiscussion);
813 
814                     if ((mbDiscussion.getClassNameId() != classNameId) ||
815                             (mbDiscussion.getClassPK() != classPK)) {
816                         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
817                             finderArgs, mbDiscussion);
818                     }
819                 }
820 
821                 return mbDiscussion;
822             }
823             catch (Exception e) {
824                 throw processException(e);
825             }
826             finally {
827                 if (result == null) {
828                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
829                         finderArgs, new ArrayList<MBDiscussion>());
830                 }
831 
832                 closeSession(session);
833             }
834         }
835         else {
836             if (result instanceof List<?>) {
837                 return null;
838             }
839             else {
840                 return (MBDiscussion)result;
841             }
842         }
843     }
844 
845     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
846         throws SystemException {
847         Session session = null;
848 
849         try {
850             session = openSession();
851 
852             dynamicQuery.compile(session);
853 
854             return dynamicQuery.list();
855         }
856         catch (Exception e) {
857             throw processException(e);
858         }
859         finally {
860             closeSession(session);
861         }
862     }
863 
864     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
865         int start, int end) throws SystemException {
866         Session session = null;
867 
868         try {
869             session = openSession();
870 
871             dynamicQuery.setLimit(start, end);
872 
873             dynamicQuery.compile(session);
874 
875             return dynamicQuery.list();
876         }
877         catch (Exception e) {
878             throw processException(e);
879         }
880         finally {
881             closeSession(session);
882         }
883     }
884 
885     public List<MBDiscussion> findAll() throws SystemException {
886         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
887     }
888 
889     public List<MBDiscussion> findAll(int start, int end)
890         throws SystemException {
891         return findAll(start, end, null);
892     }
893 
894     public List<MBDiscussion> findAll(int start, int end, OrderByComparator obc)
895         throws SystemException {
896         Object[] finderArgs = new Object[] {
897                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
898             };
899 
900         List<MBDiscussion> list = (List<MBDiscussion>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
901                 finderArgs, this);
902 
903         if (list == null) {
904             Session session = null;
905 
906             try {
907                 session = openSession();
908 
909                 StringBundler query = null;
910                 String sql = null;
911 
912                 if (obc != null) {
913                     query = new StringBundler(2 +
914                             (obc.getOrderByFields().length * 3));
915 
916                     query.append(_SQL_SELECT_MBDISCUSSION);
917 
918                     appendOrderByComparator(query, _ORDER_BY_ENTITY_ALIAS, obc);
919 
920                     sql = query.toString();
921                 }
922 
923                 sql = _SQL_SELECT_MBDISCUSSION;
924 
925                 Query q = session.createQuery(sql);
926 
927                 if (obc == null) {
928                     list = (List<MBDiscussion>)QueryUtil.list(q, getDialect(),
929                             start, end, false);
930 
931                     Collections.sort(list);
932                 }
933                 else {
934                     list = (List<MBDiscussion>)QueryUtil.list(q, getDialect(),
935                             start, end);
936                 }
937             }
938             catch (Exception e) {
939                 throw processException(e);
940             }
941             finally {
942                 if (list == null) {
943                     list = new ArrayList<MBDiscussion>();
944                 }
945 
946                 cacheResult(list);
947 
948                 FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
949 
950                 closeSession(session);
951             }
952         }
953 
954         return list;
955     }
956 
957     public void removeByClassNameId(long classNameId) throws SystemException {
958         for (MBDiscussion mbDiscussion : findByClassNameId(classNameId)) {
959             remove(mbDiscussion);
960         }
961     }
962 
963     public void removeByThreadId(long threadId)
964         throws NoSuchDiscussionException, SystemException {
965         MBDiscussion mbDiscussion = findByThreadId(threadId);
966 
967         remove(mbDiscussion);
968     }
969 
970     public void removeByC_C(long classNameId, long classPK)
971         throws NoSuchDiscussionException, SystemException {
972         MBDiscussion mbDiscussion = findByC_C(classNameId, classPK);
973 
974         remove(mbDiscussion);
975     }
976 
977     public void removeAll() throws SystemException {
978         for (MBDiscussion mbDiscussion : findAll()) {
979             remove(mbDiscussion);
980         }
981     }
982 
983     public int countByClassNameId(long classNameId) throws SystemException {
984         Object[] finderArgs = new Object[] { new Long(classNameId) };
985 
986         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_CLASSNAMEID,
987                 finderArgs, this);
988 
989         if (count == null) {
990             Session session = null;
991 
992             try {
993                 session = openSession();
994 
995                 StringBundler query = new StringBundler(2);
996 
997                 query.append(_SQL_COUNT_MBDISCUSSION_WHERE);
998 
999                 query.append(_FINDER_COLUMN_CLASSNAMEID_CLASSNAMEID_2);
1000
1001                String sql = query.toString();
1002
1003                Query q = session.createQuery(sql);
1004
1005                QueryPos qPos = QueryPos.getInstance(q);
1006
1007                qPos.add(classNameId);
1008
1009                count = (Long)q.uniqueResult();
1010            }
1011            catch (Exception e) {
1012                throw processException(e);
1013            }
1014            finally {
1015                if (count == null) {
1016                    count = Long.valueOf(0);
1017                }
1018
1019                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_CLASSNAMEID,
1020                    finderArgs, count);
1021
1022                closeSession(session);
1023            }
1024        }
1025
1026        return count.intValue();
1027    }
1028
1029    public int countByThreadId(long threadId) throws SystemException {
1030        Object[] finderArgs = new Object[] { new Long(threadId) };
1031
1032        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_THREADID,
1033                finderArgs, this);
1034
1035        if (count == null) {
1036            Session session = null;
1037
1038            try {
1039                session = openSession();
1040
1041                StringBundler query = new StringBundler(2);
1042
1043                query.append(_SQL_COUNT_MBDISCUSSION_WHERE);
1044
1045                query.append(_FINDER_COLUMN_THREADID_THREADID_2);
1046
1047                String sql = query.toString();
1048
1049                Query q = session.createQuery(sql);
1050
1051                QueryPos qPos = QueryPos.getInstance(q);
1052
1053                qPos.add(threadId);
1054
1055                count = (Long)q.uniqueResult();
1056            }
1057            catch (Exception e) {
1058                throw processException(e);
1059            }
1060            finally {
1061                if (count == null) {
1062                    count = Long.valueOf(0);
1063                }
1064
1065                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_THREADID,
1066                    finderArgs, count);
1067
1068                closeSession(session);
1069            }
1070        }
1071
1072        return count.intValue();
1073    }
1074
1075    public int countByC_C(long classNameId, long classPK)
1076        throws SystemException {
1077        Object[] finderArgs = new Object[] {
1078                new Long(classNameId), new Long(classPK)
1079            };
1080
1081        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_C_C,
1082                finderArgs, this);
1083
1084        if (count == null) {
1085            Session session = null;
1086
1087            try {
1088                session = openSession();
1089
1090                StringBundler query = new StringBundler(3);
1091
1092                query.append(_SQL_COUNT_MBDISCUSSION_WHERE);
1093
1094                query.append(_FINDER_COLUMN_C_C_CLASSNAMEID_2);
1095
1096                query.append(_FINDER_COLUMN_C_C_CLASSPK_2);
1097
1098                String sql = query.toString();
1099
1100                Query q = session.createQuery(sql);
1101
1102                QueryPos qPos = QueryPos.getInstance(q);
1103
1104                qPos.add(classNameId);
1105
1106                qPos.add(classPK);
1107
1108                count = (Long)q.uniqueResult();
1109            }
1110            catch (Exception e) {
1111                throw processException(e);
1112            }
1113            finally {
1114                if (count == null) {
1115                    count = Long.valueOf(0);
1116                }
1117
1118                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_C, finderArgs,
1119                    count);
1120
1121                closeSession(session);
1122            }
1123        }
1124
1125        return count.intValue();
1126    }
1127
1128    public int countAll() throws SystemException {
1129        Object[] finderArgs = new Object[0];
1130
1131        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1132                finderArgs, this);
1133
1134        if (count == null) {
1135            Session session = null;
1136
1137            try {
1138                session = openSession();
1139
1140                Query q = session.createQuery(_SQL_COUNT_MBDISCUSSION);
1141
1142                count = (Long)q.uniqueResult();
1143            }
1144            catch (Exception e) {
1145                throw processException(e);
1146            }
1147            finally {
1148                if (count == null) {
1149                    count = Long.valueOf(0);
1150                }
1151
1152                FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
1153                    count);
1154
1155                closeSession(session);
1156            }
1157        }
1158
1159        return count.intValue();
1160    }
1161
1162    public void afterPropertiesSet() {
1163        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1164                    com.liferay.portal.util.PropsUtil.get(
1165                        "value.object.listener.com.liferay.portlet.messageboards.model.MBDiscussion")));
1166
1167        if (listenerClassNames.length > 0) {
1168            try {
1169                List<ModelListener<MBDiscussion>> listenersList = new ArrayList<ModelListener<MBDiscussion>>();
1170
1171                for (String listenerClassName : listenerClassNames) {
1172                    listenersList.add((ModelListener<MBDiscussion>)Class.forName(
1173                            listenerClassName).newInstance());
1174                }
1175
1176                listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1177            }
1178            catch (Exception e) {
1179                _log.error(e);
1180            }
1181        }
1182    }
1183
1184    @BeanReference(name = "com.liferay.portlet.messageboards.service.persistence.MBBanPersistence")
1185    protected com.liferay.portlet.messageboards.service.persistence.MBBanPersistence mbBanPersistence;
1186    @BeanReference(name = "com.liferay.portlet.messageboards.service.persistence.MBCategoryPersistence")
1187    protected com.liferay.portlet.messageboards.service.persistence.MBCategoryPersistence mbCategoryPersistence;
1188    @BeanReference(name = "com.liferay.portlet.messageboards.service.persistence.MBDiscussionPersistence")
1189    protected com.liferay.portlet.messageboards.service.persistence.MBDiscussionPersistence mbDiscussionPersistence;
1190    @BeanReference(name = "com.liferay.portlet.messageboards.service.persistence.MBMessagePersistence")
1191    protected com.liferay.portlet.messageboards.service.persistence.MBMessagePersistence mbMessagePersistence;
1192    @BeanReference(name = "com.liferay.portlet.messageboards.service.persistence.MBMessageFlagPersistence")
1193    protected com.liferay.portlet.messageboards.service.persistence.MBMessageFlagPersistence mbMessageFlagPersistence;
1194    @BeanReference(name = "com.liferay.portlet.messageboards.service.persistence.MBStatsUserPersistence")
1195    protected com.liferay.portlet.messageboards.service.persistence.MBStatsUserPersistence mbStatsUserPersistence;
1196    @BeanReference(name = "com.liferay.portlet.messageboards.service.persistence.MBThreadPersistence")
1197    protected com.liferay.portlet.messageboards.service.persistence.MBThreadPersistence mbThreadPersistence;
1198    @BeanReference(name = "com.liferay.portal.service.persistence.ResourcePersistence")
1199    protected com.liferay.portal.service.persistence.ResourcePersistence resourcePersistence;
1200    @BeanReference(name = "com.liferay.portal.service.persistence.UserPersistence")
1201    protected com.liferay.portal.service.persistence.UserPersistence userPersistence;
1202    private static final String _SQL_SELECT_MBDISCUSSION = "SELECT mbDiscussion FROM MBDiscussion mbDiscussion";
1203    private static final String _SQL_SELECT_MBDISCUSSION_WHERE = "SELECT mbDiscussion FROM MBDiscussion mbDiscussion WHERE ";
1204    private static final String _SQL_COUNT_MBDISCUSSION = "SELECT COUNT(mbDiscussion) FROM MBDiscussion mbDiscussion";
1205    private static final String _SQL_COUNT_MBDISCUSSION_WHERE = "SELECT COUNT(mbDiscussion) FROM MBDiscussion mbDiscussion WHERE ";
1206    private static final String _FINDER_COLUMN_CLASSNAMEID_CLASSNAMEID_2 = "mbDiscussion.classNameId = ?";
1207    private static final String _FINDER_COLUMN_THREADID_THREADID_2 = "mbDiscussion.threadId = ?";
1208    private static final String _FINDER_COLUMN_C_C_CLASSNAMEID_2 = "mbDiscussion.classNameId = ? AND ";
1209    private static final String _FINDER_COLUMN_C_C_CLASSPK_2 = "mbDiscussion.classPK = ?";
1210    private static final String _ORDER_BY_ENTITY_ALIAS = "mbDiscussion.";
1211    private static final String _NO_SUCH_ENTITY_WITH_PRIMARY_KEY = "No MBDiscussion exists with the primary key ";
1212    private static final String _NO_SUCH_ENTITY_WITH_KEY = "No MBDiscussion exists with the key {";
1213    private static Log _log = LogFactoryUtil.getLog(MBDiscussionPersistenceImpl.class);
1214}