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