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