1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.messageboards.service.persistence;
24  
25  import com.liferay.portal.SystemException;
26  import com.liferay.portal.kernel.annotation.BeanReference;
27  import com.liferay.portal.kernel.cache.CacheRegistry;
28  import com.liferay.portal.kernel.dao.orm.DynamicQuery;
29  import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
30  import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
31  import com.liferay.portal.kernel.dao.orm.FinderPath;
32  import com.liferay.portal.kernel.dao.orm.Query;
33  import com.liferay.portal.kernel.dao.orm.QueryPos;
34  import com.liferay.portal.kernel.dao.orm.QueryUtil;
35  import com.liferay.portal.kernel.dao.orm.Session;
36  import com.liferay.portal.kernel.log.Log;
37  import com.liferay.portal.kernel.log.LogFactoryUtil;
38  import com.liferay.portal.kernel.util.GetterUtil;
39  import com.liferay.portal.kernel.util.OrderByComparator;
40  import com.liferay.portal.kernel.util.StringPool;
41  import com.liferay.portal.kernel.util.StringUtil;
42  import com.liferay.portal.model.ModelListener;
43  import com.liferay.portal.service.persistence.BatchSessionUtil;
44  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
45  
46  import com.liferay.portlet.messageboards.NoSuchDiscussionException;
47  import com.liferay.portlet.messageboards.model.MBDiscussion;
48  import com.liferay.portlet.messageboards.model.impl.MBDiscussionImpl;
49  import com.liferay.portlet.messageboards.model.impl.MBDiscussionModelImpl;
50  
51  import java.util.ArrayList;
52  import java.util.Collections;
53  import java.util.List;
54  
55  /**
56   * <a href="MBDiscussionPersistenceImpl.java.html"><b><i>View Source</i></b></a>
57   *
58   * @author Brian Wing Shun Chan
59   *
60   */
61  public class MBDiscussionPersistenceImpl extends BasePersistenceImpl
62      implements MBDiscussionPersistence {
63      public static final String FINDER_CLASS_NAME_ENTITY = MBDiscussionImpl.class.getName();
64      public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
65          ".List";
66      public static final FinderPath FINDER_PATH_FIND_BY_CLASSNAMEID = new FinderPath(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
67              MBDiscussionModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
68              "findByClassNameId", new String[] { Long.class.getName() });
69      public static final FinderPath FINDER_PATH_FIND_BY_OBC_CLASSNAMEID = new FinderPath(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
70              MBDiscussionModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
71              "findByClassNameId",
72              new String[] {
73                  Long.class.getName(),
74                  
75              "java.lang.Integer", "java.lang.Integer",
76                  "com.liferay.portal.kernel.util.OrderByComparator"
77              });
78      public static final FinderPath FINDER_PATH_COUNT_BY_CLASSNAMEID = new FinderPath(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
79              MBDiscussionModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
80              "countByClassNameId", new String[] { Long.class.getName() });
81      public static final FinderPath FINDER_PATH_FETCH_BY_THREADID = new FinderPath(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
82              MBDiscussionModelImpl.FINDER_CACHE_ENABLED,
83              FINDER_CLASS_NAME_ENTITY, "fetchByThreadId",
84              new String[] { Long.class.getName() });
85      public static final FinderPath FINDER_PATH_COUNT_BY_THREADID = new FinderPath(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
86              MBDiscussionModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
87              "countByThreadId", new String[] { Long.class.getName() });
88      public static final FinderPath FINDER_PATH_FETCH_BY_C_C = new FinderPath(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
89              MBDiscussionModelImpl.FINDER_CACHE_ENABLED,
90              FINDER_CLASS_NAME_ENTITY, "fetchByC_C",
91              new String[] { Long.class.getName(), Long.class.getName() });
92      public static final FinderPath FINDER_PATH_COUNT_BY_C_C = new FinderPath(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
93              MBDiscussionModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
94              "countByC_C",
95              new String[] { Long.class.getName(), Long.class.getName() });
96      public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
97              MBDiscussionModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
98              "findAll", new String[0]);
99      public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
100             MBDiscussionModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
101             "countAll", new String[0]);
102 
103     public void cacheResult(MBDiscussion mbDiscussion) {
104         EntityCacheUtil.putResult(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
105             MBDiscussionImpl.class, mbDiscussion.getPrimaryKey(), mbDiscussion);
106 
107         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_THREADID,
108             new Object[] { new Long(mbDiscussion.getThreadId()) }, mbDiscussion);
109 
110         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
111             new Object[] {
112                 new Long(mbDiscussion.getClassNameId()),
113                 new Long(mbDiscussion.getClassPK())
114             }, mbDiscussion);
115     }
116 
117     public void cacheResult(List<MBDiscussion> mbDiscussions) {
118         for (MBDiscussion mbDiscussion : mbDiscussions) {
119             if (EntityCacheUtil.getResult(
120                         MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
121                         MBDiscussionImpl.class, mbDiscussion.getPrimaryKey(),
122                         this) == null) {
123                 cacheResult(mbDiscussion);
124             }
125         }
126     }
127 
128     public void clearCache() {
129         CacheRegistry.clear(MBDiscussionImpl.class.getName());
130         EntityCacheUtil.clearCache(MBDiscussionImpl.class.getName());
131         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
132         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
133     }
134 
135     public MBDiscussion create(long discussionId) {
136         MBDiscussion mbDiscussion = new MBDiscussionImpl();
137 
138         mbDiscussion.setNew(true);
139         mbDiscussion.setPrimaryKey(discussionId);
140 
141         return mbDiscussion;
142     }
143 
144     public MBDiscussion remove(long discussionId)
145         throws NoSuchDiscussionException, SystemException {
146         Session session = null;
147 
148         try {
149             session = openSession();
150 
151             MBDiscussion mbDiscussion = (MBDiscussion)session.get(MBDiscussionImpl.class,
152                     new Long(discussionId));
153 
154             if (mbDiscussion == null) {
155                 if (_log.isWarnEnabled()) {
156                     _log.warn("No MBDiscussion exists with the primary key " +
157                         discussionId);
158                 }
159 
160                 throw new NoSuchDiscussionException(
161                     "No MBDiscussion exists with the primary key " +
162                     discussionId);
163             }
164 
165             return remove(mbDiscussion);
166         }
167         catch (NoSuchDiscussionException nsee) {
168             throw nsee;
169         }
170         catch (Exception e) {
171             throw processException(e);
172         }
173         finally {
174             closeSession(session);
175         }
176     }
177 
178     public MBDiscussion remove(MBDiscussion mbDiscussion)
179         throws SystemException {
180         for (ModelListener<MBDiscussion> listener : listeners) {
181             listener.onBeforeRemove(mbDiscussion);
182         }
183 
184         mbDiscussion = removeImpl(mbDiscussion);
185 
186         for (ModelListener<MBDiscussion> listener : listeners) {
187             listener.onAfterRemove(mbDiscussion);
188         }
189 
190         return mbDiscussion;
191     }
192 
193     protected MBDiscussion removeImpl(MBDiscussion mbDiscussion)
194         throws SystemException {
195         Session session = null;
196 
197         try {
198             session = openSession();
199 
200             if (mbDiscussion.isCachedModel() || BatchSessionUtil.isEnabled()) {
201                 Object staleObject = session.get(MBDiscussionImpl.class,
202                         mbDiscussion.getPrimaryKeyObj());
203 
204                 if (staleObject != null) {
205                     session.evict(staleObject);
206                 }
207             }
208 
209             session.delete(mbDiscussion);
210 
211             session.flush();
212         }
213         catch (Exception e) {
214             throw processException(e);
215         }
216         finally {
217             closeSession(session);
218         }
219 
220         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
221 
222         MBDiscussionModelImpl mbDiscussionModelImpl = (MBDiscussionModelImpl)mbDiscussion;
223 
224         FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_THREADID,
225             new Object[] { new Long(mbDiscussionModelImpl.getOriginalThreadId()) });
226 
227         FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_C,
228             new Object[] {
229                 new Long(mbDiscussionModelImpl.getOriginalClassNameId()),
230                 new Long(mbDiscussionModelImpl.getOriginalClassPK())
231             });
232 
233         EntityCacheUtil.removeResult(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
234             MBDiscussionImpl.class, mbDiscussion.getPrimaryKey());
235 
236         return mbDiscussion;
237     }
238 
239     /**
240      * @deprecated Use <code>update(MBDiscussion mbDiscussion, boolean merge)</code>.
241      */
242     public MBDiscussion update(MBDiscussion mbDiscussion)
243         throws SystemException {
244         if (_log.isWarnEnabled()) {
245             _log.warn(
246                 "Using the deprecated update(MBDiscussion mbDiscussion) method. Use update(MBDiscussion mbDiscussion, boolean merge) instead.");
247         }
248 
249         return update(mbDiscussion, false);
250     }
251 
252     /**
253      * Add, update, or merge, the entity. This method also calls the model
254      * listeners to trigger the proper events associated with adding, deleting,
255      * or updating an entity.
256      *
257      * @param        mbDiscussion the entity to add, update, or merge
258      * @param        merge boolean value for whether to merge the entity. The
259      *                default value is false. Setting merge to true is more
260      *                expensive and should only be true when mbDiscussion is
261      *                transient. See LEP-5473 for a detailed discussion of this
262      *                method.
263      * @return        true if the portlet can be displayed via Ajax
264      */
265     public MBDiscussion update(MBDiscussion mbDiscussion, boolean merge)
266         throws SystemException {
267         boolean isNew = mbDiscussion.isNew();
268 
269         for (ModelListener<MBDiscussion> listener : listeners) {
270             if (isNew) {
271                 listener.onBeforeCreate(mbDiscussion);
272             }
273             else {
274                 listener.onBeforeUpdate(mbDiscussion);
275             }
276         }
277 
278         mbDiscussion = updateImpl(mbDiscussion, merge);
279 
280         for (ModelListener<MBDiscussion> listener : listeners) {
281             if (isNew) {
282                 listener.onAfterCreate(mbDiscussion);
283             }
284             else {
285                 listener.onAfterUpdate(mbDiscussion);
286             }
287         }
288 
289         return mbDiscussion;
290     }
291 
292     public MBDiscussion updateImpl(
293         com.liferay.portlet.messageboards.model.MBDiscussion mbDiscussion,
294         boolean merge) throws SystemException {
295         boolean isNew = mbDiscussion.isNew();
296 
297         MBDiscussionModelImpl mbDiscussionModelImpl = (MBDiscussionModelImpl)mbDiscussion;
298 
299         Session session = null;
300 
301         try {
302             session = openSession();
303 
304             BatchSessionUtil.update(session, mbDiscussion, merge);
305 
306             mbDiscussion.setNew(false);
307         }
308         catch (Exception e) {
309             throw processException(e);
310         }
311         finally {
312             closeSession(session);
313         }
314 
315         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
316 
317         EntityCacheUtil.putResult(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
318             MBDiscussionImpl.class, mbDiscussion.getPrimaryKey(), mbDiscussion);
319 
320         if (!isNew &&
321                 (mbDiscussion.getThreadId() != mbDiscussionModelImpl.getOriginalThreadId())) {
322             FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_THREADID,
323                 new Object[] {
324                     new Long(mbDiscussionModelImpl.getOriginalThreadId())
325                 });
326         }
327 
328         if (isNew ||
329                 (mbDiscussion.getThreadId() != mbDiscussionModelImpl.getOriginalThreadId())) {
330             FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_THREADID,
331                 new Object[] { new Long(mbDiscussion.getThreadId()) },
332                 mbDiscussion);
333         }
334 
335         if (!isNew &&
336                 ((mbDiscussion.getClassNameId() != mbDiscussionModelImpl.getOriginalClassNameId()) ||
337                 (mbDiscussion.getClassPK() != mbDiscussionModelImpl.getOriginalClassPK()))) {
338             FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_C,
339                 new Object[] {
340                     new Long(mbDiscussionModelImpl.getOriginalClassNameId()),
341                     new Long(mbDiscussionModelImpl.getOriginalClassPK())
342                 });
343         }
344 
345         if (isNew ||
346                 ((mbDiscussion.getClassNameId() != mbDiscussionModelImpl.getOriginalClassNameId()) ||
347                 (mbDiscussion.getClassPK() != mbDiscussionModelImpl.getOriginalClassPK()))) {
348             FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
349                 new Object[] {
350                     new Long(mbDiscussion.getClassNameId()),
351                     new Long(mbDiscussion.getClassPK())
352                 }, mbDiscussion);
353         }
354 
355         return mbDiscussion;
356     }
357 
358     public MBDiscussion findByPrimaryKey(long discussionId)
359         throws NoSuchDiscussionException, SystemException {
360         MBDiscussion mbDiscussion = fetchByPrimaryKey(discussionId);
361 
362         if (mbDiscussion == null) {
363             if (_log.isWarnEnabled()) {
364                 _log.warn("No MBDiscussion exists with the primary key " +
365                     discussionId);
366             }
367 
368             throw new NoSuchDiscussionException(
369                 "No MBDiscussion exists with the primary key " + discussionId);
370         }
371 
372         return mbDiscussion;
373     }
374 
375     public MBDiscussion fetchByPrimaryKey(long discussionId)
376         throws SystemException {
377         MBDiscussion mbDiscussion = (MBDiscussion)EntityCacheUtil.getResult(MBDiscussionModelImpl.ENTITY_CACHE_ENABLED,
378                 MBDiscussionImpl.class, discussionId, this);
379 
380         if (mbDiscussion == null) {
381             Session session = null;
382 
383             try {
384                 session = openSession();
385 
386                 mbDiscussion = (MBDiscussion)session.get(MBDiscussionImpl.class,
387                         new Long(discussionId));
388             }
389             catch (Exception e) {
390                 throw processException(e);
391             }
392             finally {
393                 if (mbDiscussion != null) {
394                     cacheResult(mbDiscussion);
395                 }
396 
397                 closeSession(session);
398             }
399         }
400 
401         return mbDiscussion;
402     }
403 
404     public List<MBDiscussion> findByClassNameId(long classNameId)
405         throws SystemException {
406         Object[] finderArgs = new Object[] { new Long(classNameId) };
407 
408         List<MBDiscussion> list = (List<MBDiscussion>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_CLASSNAMEID,
409                 finderArgs, this);
410 
411         if (list == null) {
412             Session session = null;
413 
414             try {
415                 session = openSession();
416 
417                 StringBuilder query = new StringBuilder();
418 
419                 query.append(
420                     "FROM com.liferay.portlet.messageboards.model.MBDiscussion WHERE ");
421 
422                 query.append("classNameId = ?");
423 
424                 query.append(" ");
425 
426                 Query q = session.createQuery(query.toString());
427 
428                 QueryPos qPos = QueryPos.getInstance(q);
429 
430                 qPos.add(classNameId);
431 
432                 list = q.list();
433             }
434             catch (Exception e) {
435                 throw processException(e);
436             }
437             finally {
438                 if (list == null) {
439                     list = new ArrayList<MBDiscussion>();
440                 }
441 
442                 cacheResult(list);
443 
444                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_CLASSNAMEID,
445                     finderArgs, list);
446 
447                 closeSession(session);
448             }
449         }
450 
451         return list;
452     }
453 
454     public List<MBDiscussion> findByClassNameId(long classNameId, int start,
455         int end) throws SystemException {
456         return findByClassNameId(classNameId, start, end, null);
457     }
458 
459     public List<MBDiscussion> findByClassNameId(long classNameId, int start,
460         int end, OrderByComparator obc) throws SystemException {
461         Object[] finderArgs = new Object[] {
462                 new Long(classNameId),
463                 
464                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
465             };
466 
467         List<MBDiscussion> list = (List<MBDiscussion>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_OBC_CLASSNAMEID,
468                 finderArgs, this);
469 
470         if (list == null) {
471             Session session = null;
472 
473             try {
474                 session = openSession();
475 
476                 StringBuilder query = new StringBuilder();
477 
478                 query.append(
479                     "FROM com.liferay.portlet.messageboards.model.MBDiscussion WHERE ");
480 
481                 query.append("classNameId = ?");
482 
483                 query.append(" ");
484 
485                 if (obc != null) {
486                     query.append("ORDER BY ");
487                     query.append(obc.getOrderBy());
488                 }
489 
490                 Query q = session.createQuery(query.toString());
491 
492                 QueryPos qPos = QueryPos.getInstance(q);
493 
494                 qPos.add(classNameId);
495 
496                 list = (List<MBDiscussion>)QueryUtil.list(q, getDialect(),
497                         start, end);
498             }
499             catch (Exception e) {
500                 throw processException(e);
501             }
502             finally {
503                 if (list == null) {
504                     list = new ArrayList<MBDiscussion>();
505                 }
506 
507                 cacheResult(list);
508 
509                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_OBC_CLASSNAMEID,
510                     finderArgs, list);
511 
512                 closeSession(session);
513             }
514         }
515 
516         return list;
517     }
518 
519     public MBDiscussion findByClassNameId_First(long classNameId,
520         OrderByComparator obc)
521         throws NoSuchDiscussionException, SystemException {
522         List<MBDiscussion> list = findByClassNameId(classNameId, 0, 1, obc);
523 
524         if (list.isEmpty()) {
525             StringBuilder msg = new StringBuilder();
526 
527             msg.append("No MBDiscussion exists with the key {");
528 
529             msg.append("classNameId=" + classNameId);
530 
531             msg.append(StringPool.CLOSE_CURLY_BRACE);
532 
533             throw new NoSuchDiscussionException(msg.toString());
534         }
535         else {
536             return list.get(0);
537         }
538     }
539 
540     public MBDiscussion findByClassNameId_Last(long classNameId,
541         OrderByComparator obc)
542         throws NoSuchDiscussionException, SystemException {
543         int count = countByClassNameId(classNameId);
544 
545         List<MBDiscussion> list = findByClassNameId(classNameId, count - 1,
546                 count, obc);
547 
548         if (list.isEmpty()) {
549             StringBuilder msg = new StringBuilder();
550 
551             msg.append("No MBDiscussion exists with the key {");
552 
553             msg.append("classNameId=" + classNameId);
554 
555             msg.append(StringPool.CLOSE_CURLY_BRACE);
556 
557             throw new NoSuchDiscussionException(msg.toString());
558         }
559         else {
560             return list.get(0);
561         }
562     }
563 
564     public MBDiscussion[] findByClassNameId_PrevAndNext(long discussionId,
565         long classNameId, OrderByComparator obc)
566         throws NoSuchDiscussionException, SystemException {
567         MBDiscussion mbDiscussion = findByPrimaryKey(discussionId);
568 
569         int count = countByClassNameId(classNameId);
570 
571         Session session = null;
572 
573         try {
574             session = openSession();
575 
576             StringBuilder query = new StringBuilder();
577 
578             query.append(
579                 "FROM com.liferay.portlet.messageboards.model.MBDiscussion WHERE ");
580 
581             query.append("classNameId = ?");
582 
583             query.append(" ");
584 
585             if (obc != null) {
586                 query.append("ORDER BY ");
587                 query.append(obc.getOrderBy());
588             }
589 
590             Query q = session.createQuery(query.toString());
591 
592             QueryPos qPos = QueryPos.getInstance(q);
593 
594             qPos.add(classNameId);
595 
596             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
597                     mbDiscussion);
598 
599             MBDiscussion[] array = new MBDiscussionImpl[3];
600 
601             array[0] = (MBDiscussion)objArray[0];
602             array[1] = (MBDiscussion)objArray[1];
603             array[2] = (MBDiscussion)objArray[2];
604 
605             return array;
606         }
607         catch (Exception e) {
608             throw processException(e);
609         }
610         finally {
611             closeSession(session);
612         }
613     }
614 
615     public MBDiscussion findByThreadId(long threadId)
616         throws NoSuchDiscussionException, SystemException {
617         MBDiscussion mbDiscussion = fetchByThreadId(threadId);
618 
619         if (mbDiscussion == null) {
620             StringBuilder msg = new StringBuilder();
621 
622             msg.append("No MBDiscussion exists with the key {");
623 
624             msg.append("threadId=" + threadId);
625 
626             msg.append(StringPool.CLOSE_CURLY_BRACE);
627 
628             if (_log.isWarnEnabled()) {
629                 _log.warn(msg.toString());
630             }
631 
632             throw new NoSuchDiscussionException(msg.toString());
633         }
634 
635         return mbDiscussion;
636     }
637 
638     public MBDiscussion fetchByThreadId(long threadId)
639         throws SystemException {
640         return fetchByThreadId(threadId, true);
641     }
642 
643     public MBDiscussion fetchByThreadId(long threadId, boolean retrieveFromCache)
644         throws SystemException {
645         Object[] finderArgs = new Object[] { new Long(threadId) };
646 
647         Object result = null;
648 
649         if (retrieveFromCache) {
650             result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_THREADID,
651                     finderArgs, this);
652         }
653 
654         if (result == null) {
655             Session session = null;
656 
657             try {
658                 session = openSession();
659 
660                 StringBuilder query = new StringBuilder();
661 
662                 query.append(
663                     "FROM com.liferay.portlet.messageboards.model.MBDiscussion WHERE ");
664 
665                 query.append("threadId = ?");
666 
667                 query.append(" ");
668 
669                 Query q = session.createQuery(query.toString());
670 
671                 QueryPos qPos = QueryPos.getInstance(q);
672 
673                 qPos.add(threadId);
674 
675                 List<MBDiscussion> list = q.list();
676 
677                 result = list;
678 
679                 MBDiscussion mbDiscussion = null;
680 
681                 if (list.isEmpty()) {
682                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_THREADID,
683                         finderArgs, list);
684                 }
685                 else {
686                     mbDiscussion = list.get(0);
687 
688                     cacheResult(mbDiscussion);
689 
690                     if ((mbDiscussion.getThreadId() != threadId)) {
691                         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_THREADID,
692                             finderArgs, mbDiscussion);
693                     }
694                 }
695 
696                 return mbDiscussion;
697             }
698             catch (Exception e) {
699                 throw processException(e);
700             }
701             finally {
702                 if (result == null) {
703                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_THREADID,
704                         finderArgs, new ArrayList<MBDiscussion>());
705                 }
706 
707                 closeSession(session);
708             }
709         }
710         else {
711             if (result instanceof List) {
712                 return null;
713             }
714             else {
715                 return (MBDiscussion)result;
716             }
717         }
718     }
719 
720     public MBDiscussion findByC_C(long classNameId, long classPK)
721         throws NoSuchDiscussionException, SystemException {
722         MBDiscussion mbDiscussion = fetchByC_C(classNameId, classPK);
723 
724         if (mbDiscussion == null) {
725             StringBuilder msg = new StringBuilder();
726 
727             msg.append("No MBDiscussion exists with the key {");
728 
729             msg.append("classNameId=" + classNameId);
730 
731             msg.append(", ");
732             msg.append("classPK=" + classPK);
733 
734             msg.append(StringPool.CLOSE_CURLY_BRACE);
735 
736             if (_log.isWarnEnabled()) {
737                 _log.warn(msg.toString());
738             }
739 
740             throw new NoSuchDiscussionException(msg.toString());
741         }
742 
743         return mbDiscussion;
744     }
745 
746     public MBDiscussion fetchByC_C(long classNameId, long classPK)
747         throws SystemException {
748         return fetchByC_C(classNameId, classPK, true);
749     }
750 
751     public MBDiscussion fetchByC_C(long classNameId, long classPK,
752         boolean retrieveFromCache) throws SystemException {
753         Object[] finderArgs = new Object[] {
754                 new Long(classNameId), new Long(classPK)
755             };
756 
757         Object result = null;
758 
759         if (retrieveFromCache) {
760             result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_C_C,
761                     finderArgs, this);
762         }
763 
764         if (result == null) {
765             Session session = null;
766 
767             try {
768                 session = openSession();
769 
770                 StringBuilder query = new StringBuilder();
771 
772                 query.append(
773                     "FROM com.liferay.portlet.messageboards.model.MBDiscussion WHERE ");
774 
775                 query.append("classNameId = ?");
776 
777                 query.append(" AND ");
778 
779                 query.append("classPK = ?");
780 
781                 query.append(" ");
782 
783                 Query q = session.createQuery(query.toString());
784 
785                 QueryPos qPos = QueryPos.getInstance(q);
786 
787                 qPos.add(classNameId);
788 
789                 qPos.add(classPK);
790 
791                 List<MBDiscussion> list = q.list();
792 
793                 result = list;
794 
795                 MBDiscussion mbDiscussion = null;
796 
797                 if (list.isEmpty()) {
798                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
799                         finderArgs, list);
800                 }
801                 else {
802                     mbDiscussion = list.get(0);
803 
804                     cacheResult(mbDiscussion);
805 
806                     if ((mbDiscussion.getClassNameId() != classNameId) ||
807                             (mbDiscussion.getClassPK() != classPK)) {
808                         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
809                             finderArgs, mbDiscussion);
810                     }
811                 }
812 
813                 return mbDiscussion;
814             }
815             catch (Exception e) {
816                 throw processException(e);
817             }
818             finally {
819                 if (result == null) {
820                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
821                         finderArgs, new ArrayList<MBDiscussion>());
822                 }
823 
824                 closeSession(session);
825             }
826         }
827         else {
828             if (result instanceof List) {
829                 return null;
830             }
831             else {
832                 return (MBDiscussion)result;
833             }
834         }
835     }
836 
837     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
838         throws SystemException {
839         Session session = null;
840 
841         try {
842             session = openSession();
843 
844             dynamicQuery.compile(session);
845 
846             return dynamicQuery.list();
847         }
848         catch (Exception e) {
849             throw processException(e);
850         }
851         finally {
852             closeSession(session);
853         }
854     }
855 
856     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
857         int start, int end) throws SystemException {
858         Session session = null;
859 
860         try {
861             session = openSession();
862 
863             dynamicQuery.setLimit(start, end);
864 
865             dynamicQuery.compile(session);
866 
867             return dynamicQuery.list();
868         }
869         catch (Exception e) {
870             throw processException(e);
871         }
872         finally {
873             closeSession(session);
874         }
875     }
876 
877     public List<MBDiscussion> findAll() throws SystemException {
878         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
879     }
880 
881     public List<MBDiscussion> findAll(int start, int end)
882         throws SystemException {
883         return findAll(start, end, null);
884     }
885 
886     public List<MBDiscussion> findAll(int start, int end, OrderByComparator obc)
887         throws SystemException {
888         Object[] finderArgs = new Object[] {
889                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
890             };
891 
892         List<MBDiscussion> list = (List<MBDiscussion>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
893                 finderArgs, this);
894 
895         if (list == null) {
896             Session session = null;
897 
898             try {
899                 session = openSession();
900 
901                 StringBuilder query = new StringBuilder();
902 
903                 query.append(
904                     "FROM com.liferay.portlet.messageboards.model.MBDiscussion ");
905 
906                 if (obc != null) {
907                     query.append("ORDER BY ");
908                     query.append(obc.getOrderBy());
909                 }
910 
911                 Query q = session.createQuery(query.toString());
912 
913                 if (obc == null) {
914                     list = (List<MBDiscussion>)QueryUtil.list(q, getDialect(),
915                             start, end, false);
916 
917                     Collections.sort(list);
918                 }
919                 else {
920                     list = (List<MBDiscussion>)QueryUtil.list(q, getDialect(),
921                             start, end);
922                 }
923             }
924             catch (Exception e) {
925                 throw processException(e);
926             }
927             finally {
928                 if (list == null) {
929                     list = new ArrayList<MBDiscussion>();
930                 }
931 
932                 cacheResult(list);
933 
934                 FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
935 
936                 closeSession(session);
937             }
938         }
939 
940         return list;
941     }
942 
943     public void removeByClassNameId(long classNameId) throws SystemException {
944         for (MBDiscussion mbDiscussion : findByClassNameId(classNameId)) {
945             remove(mbDiscussion);
946         }
947     }
948 
949     public void removeByThreadId(long threadId)
950         throws NoSuchDiscussionException, SystemException {
951         MBDiscussion mbDiscussion = findByThreadId(threadId);
952 
953         remove(mbDiscussion);
954     }
955 
956     public void removeByC_C(long classNameId, long classPK)
957         throws NoSuchDiscussionException, SystemException {
958         MBDiscussion mbDiscussion = findByC_C(classNameId, classPK);
959 
960         remove(mbDiscussion);
961     }
962 
963     public void removeAll() throws SystemException {
964         for (MBDiscussion mbDiscussion : findAll()) {
965             remove(mbDiscussion);
966         }
967     }
968 
969     public int countByClassNameId(long classNameId) throws SystemException {
970         Object[] finderArgs = new Object[] { new Long(classNameId) };
971 
972         Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_CLASSNAMEID,
973                 finderArgs, this);
974 
975         if (count == null) {
976             Session session = null;
977 
978             try {
979                 session = openSession();
980 
981                 StringBuilder query = new StringBuilder();
982 
983                 query.append("SELECT COUNT(*) ");
984                 query.append(
985                     "FROM com.liferay.portlet.messageboards.model.MBDiscussion WHERE ");
986 
987                 query.append("classNameId = ?");
988 
989                 query.append(" ");
990 
991                 Query q = session.createQuery(query.toString());
992 
993                 QueryPos qPos = QueryPos.getInstance(q);
994 
995                 qPos.add(classNameId);
996 
997                 count = (Long)q.uniqueResult();
998             }
999             catch (Exception e) {
1000                throw processException(e);
1001            }
1002            finally {
1003                if (count == null) {
1004                    count = Long.valueOf(0);
1005                }
1006
1007                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_CLASSNAMEID,
1008                    finderArgs, count);
1009
1010                closeSession(session);
1011            }
1012        }
1013
1014        return count.intValue();
1015    }
1016
1017    public int countByThreadId(long threadId) throws SystemException {
1018        Object[] finderArgs = new Object[] { new Long(threadId) };
1019
1020        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_THREADID,
1021                finderArgs, this);
1022
1023        if (count == null) {
1024            Session session = null;
1025
1026            try {
1027                session = openSession();
1028
1029                StringBuilder query = new StringBuilder();
1030
1031                query.append("SELECT COUNT(*) ");
1032                query.append(
1033                    "FROM com.liferay.portlet.messageboards.model.MBDiscussion WHERE ");
1034
1035                query.append("threadId = ?");
1036
1037                query.append(" ");
1038
1039                Query q = session.createQuery(query.toString());
1040
1041                QueryPos qPos = QueryPos.getInstance(q);
1042
1043                qPos.add(threadId);
1044
1045                count = (Long)q.uniqueResult();
1046            }
1047            catch (Exception e) {
1048                throw processException(e);
1049            }
1050            finally {
1051                if (count == null) {
1052                    count = Long.valueOf(0);
1053                }
1054
1055                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_THREADID,
1056                    finderArgs, count);
1057
1058                closeSession(session);
1059            }
1060        }
1061
1062        return count.intValue();
1063    }
1064
1065    public int countByC_C(long classNameId, long classPK)
1066        throws SystemException {
1067        Object[] finderArgs = new Object[] {
1068                new Long(classNameId), new Long(classPK)
1069            };
1070
1071        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_C_C,
1072                finderArgs, this);
1073
1074        if (count == null) {
1075            Session session = null;
1076
1077            try {
1078                session = openSession();
1079
1080                StringBuilder query = new StringBuilder();
1081
1082                query.append("SELECT COUNT(*) ");
1083                query.append(
1084                    "FROM com.liferay.portlet.messageboards.model.MBDiscussion WHERE ");
1085
1086                query.append("classNameId = ?");
1087
1088                query.append(" AND ");
1089
1090                query.append("classPK = ?");
1091
1092                query.append(" ");
1093
1094                Query q = session.createQuery(query.toString());
1095
1096                QueryPos qPos = QueryPos.getInstance(q);
1097
1098                qPos.add(classNameId);
1099
1100                qPos.add(classPK);
1101
1102                count = (Long)q.uniqueResult();
1103            }
1104            catch (Exception e) {
1105                throw processException(e);
1106            }
1107            finally {
1108                if (count == null) {
1109                    count = Long.valueOf(0);
1110                }
1111
1112                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_C, finderArgs,
1113                    count);
1114
1115                closeSession(session);
1116            }
1117        }
1118
1119        return count.intValue();
1120    }
1121
1122    public int countAll() throws SystemException {
1123        Object[] finderArgs = new Object[0];
1124
1125        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1126                finderArgs, this);
1127
1128        if (count == null) {
1129            Session session = null;
1130
1131            try {
1132                session = openSession();
1133
1134                Query q = session.createQuery(
1135                        "SELECT COUNT(*) FROM com.liferay.portlet.messageboards.model.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>)Class.forName(
1168                            listenerClassName).newInstance());
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(name = "com.liferay.portlet.messageboards.service.persistence.MBBanPersistence.impl")
1180    protected com.liferay.portlet.messageboards.service.persistence.MBBanPersistence mbBanPersistence;
1181    @BeanReference(name = "com.liferay.portlet.messageboards.service.persistence.MBCategoryPersistence.impl")
1182    protected com.liferay.portlet.messageboards.service.persistence.MBCategoryPersistence mbCategoryPersistence;
1183    @BeanReference(name = "com.liferay.portlet.messageboards.service.persistence.MBDiscussionPersistence.impl")
1184    protected com.liferay.portlet.messageboards.service.persistence.MBDiscussionPersistence mbDiscussionPersistence;
1185    @BeanReference(name = "com.liferay.portlet.messageboards.service.persistence.MBMailingListPersistence.impl")
1186    protected com.liferay.portlet.messageboards.service.persistence.MBMailingListPersistence mbMailingListPersistence;
1187    @BeanReference(name = "com.liferay.portlet.messageboards.service.persistence.MBMessagePersistence.impl")
1188    protected com.liferay.portlet.messageboards.service.persistence.MBMessagePersistence mbMessagePersistence;
1189    @BeanReference(name = "com.liferay.portlet.messageboards.service.persistence.MBMessageFlagPersistence.impl")
1190    protected com.liferay.portlet.messageboards.service.persistence.MBMessageFlagPersistence mbMessageFlagPersistence;
1191    @BeanReference(name = "com.liferay.portlet.messageboards.service.persistence.MBStatsUserPersistence.impl")
1192    protected com.liferay.portlet.messageboards.service.persistence.MBStatsUserPersistence mbStatsUserPersistence;
1193    @BeanReference(name = "com.liferay.portlet.messageboards.service.persistence.MBThreadPersistence.impl")
1194    protected com.liferay.portlet.messageboards.service.persistence.MBThreadPersistence mbThreadPersistence;
1195    private static Log _log = LogFactoryUtil.getLog(MBDiscussionPersistenceImpl.class);
1196}