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                     "SELECT mbDiscussion FROM MBDiscussion mbDiscussion WHERE ");
421 
422                 query.append("mbDiscussion.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                     "SELECT mbDiscussion FROM MBDiscussion mbDiscussion WHERE ");
480 
481                 query.append("mbDiscussion.classNameId = ?");
482 
483                 query.append(" ");
484 
485                 if (obc != null) {
486                     query.append("ORDER BY ");
487 
488                     String[] orderByFields = obc.getOrderByFields();
489 
490                     for (int i = 0; i < orderByFields.length; i++) {
491                         query.append("mbDiscussion.");
492                         query.append(orderByFields[i]);
493 
494                         if (obc.isAscending()) {
495                             query.append(" ASC");
496                         }
497                         else {
498                             query.append(" DESC");
499                         }
500 
501                         if ((i + 1) < orderByFields.length) {
502                             query.append(", ");
503                         }
504                     }
505                 }
506 
507                 Query q = session.createQuery(query.toString());
508 
509                 QueryPos qPos = QueryPos.getInstance(q);
510 
511                 qPos.add(classNameId);
512 
513                 list = (List<MBDiscussion>)QueryUtil.list(q, getDialect(),
514                         start, end);
515             }
516             catch (Exception e) {
517                 throw processException(e);
518             }
519             finally {
520                 if (list == null) {
521                     list = new ArrayList<MBDiscussion>();
522                 }
523 
524                 cacheResult(list);
525 
526                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_OBC_CLASSNAMEID,
527                     finderArgs, list);
528 
529                 closeSession(session);
530             }
531         }
532 
533         return list;
534     }
535 
536     public MBDiscussion findByClassNameId_First(long classNameId,
537         OrderByComparator obc)
538         throws NoSuchDiscussionException, SystemException {
539         List<MBDiscussion> list = findByClassNameId(classNameId, 0, 1, obc);
540 
541         if (list.isEmpty()) {
542             StringBuilder msg = new StringBuilder();
543 
544             msg.append("No MBDiscussion exists with the key {");
545 
546             msg.append("classNameId=" + classNameId);
547 
548             msg.append(StringPool.CLOSE_CURLY_BRACE);
549 
550             throw new NoSuchDiscussionException(msg.toString());
551         }
552         else {
553             return list.get(0);
554         }
555     }
556 
557     public MBDiscussion findByClassNameId_Last(long classNameId,
558         OrderByComparator obc)
559         throws NoSuchDiscussionException, SystemException {
560         int count = countByClassNameId(classNameId);
561 
562         List<MBDiscussion> list = findByClassNameId(classNameId, count - 1,
563                 count, obc);
564 
565         if (list.isEmpty()) {
566             StringBuilder msg = new StringBuilder();
567 
568             msg.append("No MBDiscussion exists with the key {");
569 
570             msg.append("classNameId=" + classNameId);
571 
572             msg.append(StringPool.CLOSE_CURLY_BRACE);
573 
574             throw new NoSuchDiscussionException(msg.toString());
575         }
576         else {
577             return list.get(0);
578         }
579     }
580 
581     public MBDiscussion[] findByClassNameId_PrevAndNext(long discussionId,
582         long classNameId, OrderByComparator obc)
583         throws NoSuchDiscussionException, SystemException {
584         MBDiscussion mbDiscussion = findByPrimaryKey(discussionId);
585 
586         int count = countByClassNameId(classNameId);
587 
588         Session session = null;
589 
590         try {
591             session = openSession();
592 
593             StringBuilder query = new StringBuilder();
594 
595             query.append(
596                 "SELECT mbDiscussion FROM MBDiscussion mbDiscussion WHERE ");
597 
598             query.append("mbDiscussion.classNameId = ?");
599 
600             query.append(" ");
601 
602             if (obc != null) {
603                 query.append("ORDER BY ");
604 
605                 String[] orderByFields = obc.getOrderByFields();
606 
607                 for (int i = 0; i < orderByFields.length; i++) {
608                     query.append("mbDiscussion.");
609                     query.append(orderByFields[i]);
610 
611                     if (obc.isAscending()) {
612                         query.append(" ASC");
613                     }
614                     else {
615                         query.append(" DESC");
616                     }
617 
618                     if ((i + 1) < orderByFields.length) {
619                         query.append(", ");
620                     }
621                 }
622             }
623 
624             Query q = session.createQuery(query.toString());
625 
626             QueryPos qPos = QueryPos.getInstance(q);
627 
628             qPos.add(classNameId);
629 
630             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
631                     mbDiscussion);
632 
633             MBDiscussion[] array = new MBDiscussionImpl[3];
634 
635             array[0] = (MBDiscussion)objArray[0];
636             array[1] = (MBDiscussion)objArray[1];
637             array[2] = (MBDiscussion)objArray[2];
638 
639             return array;
640         }
641         catch (Exception e) {
642             throw processException(e);
643         }
644         finally {
645             closeSession(session);
646         }
647     }
648 
649     public MBDiscussion findByThreadId(long threadId)
650         throws NoSuchDiscussionException, SystemException {
651         MBDiscussion mbDiscussion = fetchByThreadId(threadId);
652 
653         if (mbDiscussion == null) {
654             StringBuilder msg = new StringBuilder();
655 
656             msg.append("No MBDiscussion exists with the key {");
657 
658             msg.append("threadId=" + threadId);
659 
660             msg.append(StringPool.CLOSE_CURLY_BRACE);
661 
662             if (_log.isWarnEnabled()) {
663                 _log.warn(msg.toString());
664             }
665 
666             throw new NoSuchDiscussionException(msg.toString());
667         }
668 
669         return mbDiscussion;
670     }
671 
672     public MBDiscussion fetchByThreadId(long threadId)
673         throws SystemException {
674         return fetchByThreadId(threadId, true);
675     }
676 
677     public MBDiscussion fetchByThreadId(long threadId, boolean retrieveFromCache)
678         throws SystemException {
679         Object[] finderArgs = new Object[] { new Long(threadId) };
680 
681         Object result = null;
682 
683         if (retrieveFromCache) {
684             result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_THREADID,
685                     finderArgs, this);
686         }
687 
688         if (result == null) {
689             Session session = null;
690 
691             try {
692                 session = openSession();
693 
694                 StringBuilder query = new StringBuilder();
695 
696                 query.append(
697                     "SELECT mbDiscussion FROM MBDiscussion mbDiscussion WHERE ");
698 
699                 query.append("mbDiscussion.threadId = ?");
700 
701                 query.append(" ");
702 
703                 Query q = session.createQuery(query.toString());
704 
705                 QueryPos qPos = QueryPos.getInstance(q);
706 
707                 qPos.add(threadId);
708 
709                 List<MBDiscussion> list = q.list();
710 
711                 result = list;
712 
713                 MBDiscussion mbDiscussion = null;
714 
715                 if (list.isEmpty()) {
716                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_THREADID,
717                         finderArgs, list);
718                 }
719                 else {
720                     mbDiscussion = list.get(0);
721 
722                     cacheResult(mbDiscussion);
723 
724                     if ((mbDiscussion.getThreadId() != threadId)) {
725                         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_THREADID,
726                             finderArgs, mbDiscussion);
727                     }
728                 }
729 
730                 return mbDiscussion;
731             }
732             catch (Exception e) {
733                 throw processException(e);
734             }
735             finally {
736                 if (result == null) {
737                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_THREADID,
738                         finderArgs, new ArrayList<MBDiscussion>());
739                 }
740 
741                 closeSession(session);
742             }
743         }
744         else {
745             if (result instanceof List) {
746                 return null;
747             }
748             else {
749                 return (MBDiscussion)result;
750             }
751         }
752     }
753 
754     public MBDiscussion findByC_C(long classNameId, long classPK)
755         throws NoSuchDiscussionException, SystemException {
756         MBDiscussion mbDiscussion = fetchByC_C(classNameId, classPK);
757 
758         if (mbDiscussion == null) {
759             StringBuilder msg = new StringBuilder();
760 
761             msg.append("No MBDiscussion exists with the key {");
762 
763             msg.append("classNameId=" + classNameId);
764 
765             msg.append(", ");
766             msg.append("classPK=" + classPK);
767 
768             msg.append(StringPool.CLOSE_CURLY_BRACE);
769 
770             if (_log.isWarnEnabled()) {
771                 _log.warn(msg.toString());
772             }
773 
774             throw new NoSuchDiscussionException(msg.toString());
775         }
776 
777         return mbDiscussion;
778     }
779 
780     public MBDiscussion fetchByC_C(long classNameId, long classPK)
781         throws SystemException {
782         return fetchByC_C(classNameId, classPK, true);
783     }
784 
785     public MBDiscussion fetchByC_C(long classNameId, long classPK,
786         boolean retrieveFromCache) throws SystemException {
787         Object[] finderArgs = new Object[] {
788                 new Long(classNameId), new Long(classPK)
789             };
790 
791         Object result = null;
792 
793         if (retrieveFromCache) {
794             result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_C_C,
795                     finderArgs, this);
796         }
797 
798         if (result == null) {
799             Session session = null;
800 
801             try {
802                 session = openSession();
803 
804                 StringBuilder query = new StringBuilder();
805 
806                 query.append(
807                     "SELECT mbDiscussion FROM MBDiscussion mbDiscussion WHERE ");
808 
809                 query.append("mbDiscussion.classNameId = ?");
810 
811                 query.append(" AND ");
812 
813                 query.append("mbDiscussion.classPK = ?");
814 
815                 query.append(" ");
816 
817                 Query q = session.createQuery(query.toString());
818 
819                 QueryPos qPos = QueryPos.getInstance(q);
820 
821                 qPos.add(classNameId);
822 
823                 qPos.add(classPK);
824 
825                 List<MBDiscussion> list = q.list();
826 
827                 result = list;
828 
829                 MBDiscussion mbDiscussion = null;
830 
831                 if (list.isEmpty()) {
832                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
833                         finderArgs, list);
834                 }
835                 else {
836                     mbDiscussion = list.get(0);
837 
838                     cacheResult(mbDiscussion);
839 
840                     if ((mbDiscussion.getClassNameId() != classNameId) ||
841                             (mbDiscussion.getClassPK() != classPK)) {
842                         FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
843                             finderArgs, mbDiscussion);
844                     }
845                 }
846 
847                 return mbDiscussion;
848             }
849             catch (Exception e) {
850                 throw processException(e);
851             }
852             finally {
853                 if (result == null) {
854                     FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
855                         finderArgs, new ArrayList<MBDiscussion>());
856                 }
857 
858                 closeSession(session);
859             }
860         }
861         else {
862             if (result instanceof List) {
863                 return null;
864             }
865             else {
866                 return (MBDiscussion)result;
867             }
868         }
869     }
870 
871     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
872         throws SystemException {
873         Session session = null;
874 
875         try {
876             session = openSession();
877 
878             dynamicQuery.compile(session);
879 
880             return dynamicQuery.list();
881         }
882         catch (Exception e) {
883             throw processException(e);
884         }
885         finally {
886             closeSession(session);
887         }
888     }
889 
890     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
891         int start, int end) throws SystemException {
892         Session session = null;
893 
894         try {
895             session = openSession();
896 
897             dynamicQuery.setLimit(start, end);
898 
899             dynamicQuery.compile(session);
900 
901             return dynamicQuery.list();
902         }
903         catch (Exception e) {
904             throw processException(e);
905         }
906         finally {
907             closeSession(session);
908         }
909     }
910 
911     public List<MBDiscussion> findAll() throws SystemException {
912         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
913     }
914 
915     public List<MBDiscussion> findAll(int start, int end)
916         throws SystemException {
917         return findAll(start, end, null);
918     }
919 
920     public List<MBDiscussion> findAll(int start, int end, OrderByComparator obc)
921         throws SystemException {
922         Object[] finderArgs = new Object[] {
923                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
924             };
925 
926         List<MBDiscussion> list = (List<MBDiscussion>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
927                 finderArgs, this);
928 
929         if (list == null) {
930             Session session = null;
931 
932             try {
933                 session = openSession();
934 
935                 StringBuilder query = new StringBuilder();
936 
937                 query.append(
938                     "SELECT mbDiscussion FROM MBDiscussion mbDiscussion ");
939 
940                 if (obc != null) {
941                     query.append("ORDER BY ");
942 
943                     String[] orderByFields = obc.getOrderByFields();
944 
945                     for (int i = 0; i < orderByFields.length; i++) {
946                         query.append("mbDiscussion.");
947                         query.append(orderByFields[i]);
948 
949                         if (obc.isAscending()) {
950                             query.append(" ASC");
951                         }
952                         else {
953                             query.append(" DESC");
954                         }
955 
956                         if ((i + 1) < orderByFields.length) {
957                             query.append(", ");
958                         }
959                     }
960                 }
961 
962                 Query q = session.createQuery(query.toString());
963 
964                 if (obc == null) {
965                     list = (List<MBDiscussion>)QueryUtil.list(q, getDialect(),
966                             start, end, false);
967 
968                     Collections.sort(list);
969                 }
970                 else {
971                     list = (List<MBDiscussion>)QueryUtil.list(q, getDialect(),
972                             start, end);
973                 }
974             }
975             catch (Exception e) {
976                 throw processException(e);
977             }
978             finally {
979                 if (list == null) {
980                     list = new ArrayList<MBDiscussion>();
981                 }
982 
983                 cacheResult(list);
984 
985                 FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
986 
987                 closeSession(session);
988             }
989         }
990 
991         return list;
992     }
993 
994     public void removeByClassNameId(long classNameId) throws SystemException {
995         for (MBDiscussion mbDiscussion : findByClassNameId(classNameId)) {
996             remove(mbDiscussion);
997         }
998     }
999 
1000    public void removeByThreadId(long threadId)
1001        throws NoSuchDiscussionException, SystemException {
1002        MBDiscussion mbDiscussion = findByThreadId(threadId);
1003
1004        remove(mbDiscussion);
1005    }
1006
1007    public void removeByC_C(long classNameId, long classPK)
1008        throws NoSuchDiscussionException, SystemException {
1009        MBDiscussion mbDiscussion = findByC_C(classNameId, classPK);
1010
1011        remove(mbDiscussion);
1012    }
1013
1014    public void removeAll() throws SystemException {
1015        for (MBDiscussion mbDiscussion : findAll()) {
1016            remove(mbDiscussion);
1017        }
1018    }
1019
1020    public int countByClassNameId(long classNameId) throws SystemException {
1021        Object[] finderArgs = new Object[] { new Long(classNameId) };
1022
1023        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_CLASSNAMEID,
1024                finderArgs, this);
1025
1026        if (count == null) {
1027            Session session = null;
1028
1029            try {
1030                session = openSession();
1031
1032                StringBuilder query = new StringBuilder();
1033
1034                query.append("SELECT COUNT(mbDiscussion) ");
1035                query.append("FROM MBDiscussion mbDiscussion WHERE ");
1036
1037                query.append("mbDiscussion.classNameId = ?");
1038
1039                query.append(" ");
1040
1041                Query q = session.createQuery(query.toString());
1042
1043                QueryPos qPos = QueryPos.getInstance(q);
1044
1045                qPos.add(classNameId);
1046
1047                count = (Long)q.uniqueResult();
1048            }
1049            catch (Exception e) {
1050                throw processException(e);
1051            }
1052            finally {
1053                if (count == null) {
1054                    count = Long.valueOf(0);
1055                }
1056
1057                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_CLASSNAMEID,
1058                    finderArgs, count);
1059
1060                closeSession(session);
1061            }
1062        }
1063
1064        return count.intValue();
1065    }
1066
1067    public int countByThreadId(long threadId) throws SystemException {
1068        Object[] finderArgs = new Object[] { new Long(threadId) };
1069
1070        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_THREADID,
1071                finderArgs, this);
1072
1073        if (count == null) {
1074            Session session = null;
1075
1076            try {
1077                session = openSession();
1078
1079                StringBuilder query = new StringBuilder();
1080
1081                query.append("SELECT COUNT(mbDiscussion) ");
1082                query.append("FROM MBDiscussion mbDiscussion WHERE ");
1083
1084                query.append("mbDiscussion.threadId = ?");
1085
1086                query.append(" ");
1087
1088                Query q = session.createQuery(query.toString());
1089
1090                QueryPos qPos = QueryPos.getInstance(q);
1091
1092                qPos.add(threadId);
1093
1094                count = (Long)q.uniqueResult();
1095            }
1096            catch (Exception e) {
1097                throw processException(e);
1098            }
1099            finally {
1100                if (count == null) {
1101                    count = Long.valueOf(0);
1102                }
1103
1104                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_THREADID,
1105                    finderArgs, count);
1106
1107                closeSession(session);
1108            }
1109        }
1110
1111        return count.intValue();
1112    }
1113
1114    public int countByC_C(long classNameId, long classPK)
1115        throws SystemException {
1116        Object[] finderArgs = new Object[] {
1117                new Long(classNameId), new Long(classPK)
1118            };
1119
1120        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_C_C,
1121                finderArgs, this);
1122
1123        if (count == null) {
1124            Session session = null;
1125
1126            try {
1127                session = openSession();
1128
1129                StringBuilder query = new StringBuilder();
1130
1131                query.append("SELECT COUNT(mbDiscussion) ");
1132                query.append("FROM MBDiscussion mbDiscussion WHERE ");
1133
1134                query.append("mbDiscussion.classNameId = ?");
1135
1136                query.append(" AND ");
1137
1138                query.append("mbDiscussion.classPK = ?");
1139
1140                query.append(" ");
1141
1142                Query q = session.createQuery(query.toString());
1143
1144                QueryPos qPos = QueryPos.getInstance(q);
1145
1146                qPos.add(classNameId);
1147
1148                qPos.add(classPK);
1149
1150                count = (Long)q.uniqueResult();
1151            }
1152            catch (Exception e) {
1153                throw processException(e);
1154            }
1155            finally {
1156                if (count == null) {
1157                    count = Long.valueOf(0);
1158                }
1159
1160                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_C, finderArgs,
1161                    count);
1162
1163                closeSession(session);
1164            }
1165        }
1166
1167        return count.intValue();
1168    }
1169
1170    public int countAll() throws SystemException {
1171        Object[] finderArgs = new Object[0];
1172
1173        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1174                finderArgs, this);
1175
1176        if (count == null) {
1177            Session session = null;
1178
1179            try {
1180                session = openSession();
1181
1182                Query q = session.createQuery(
1183                        "SELECT COUNT(mbDiscussion) FROM MBDiscussion mbDiscussion");
1184
1185                count = (Long)q.uniqueResult();
1186            }
1187            catch (Exception e) {
1188                throw processException(e);
1189            }
1190            finally {
1191                if (count == null) {
1192                    count = Long.valueOf(0);
1193                }
1194
1195                FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
1196                    count);
1197
1198                closeSession(session);
1199            }
1200        }
1201
1202        return count.intValue();
1203    }
1204
1205    public void afterPropertiesSet() {
1206        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1207                    com.liferay.portal.util.PropsUtil.get(
1208                        "value.object.listener.com.liferay.portlet.messageboards.model.MBDiscussion")));
1209
1210        if (listenerClassNames.length > 0) {
1211            try {
1212                List<ModelListener<MBDiscussion>> listenersList = new ArrayList<ModelListener<MBDiscussion>>();
1213
1214                for (String listenerClassName : listenerClassNames) {
1215                    listenersList.add((ModelListener<MBDiscussion>)Class.forName(
1216                            listenerClassName).newInstance());
1217                }
1218
1219                listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1220            }
1221            catch (Exception e) {
1222                _log.error(e);
1223            }
1224        }
1225    }
1226
1227    @BeanReference(name = "com.liferay.portlet.messageboards.service.persistence.MBBanPersistence.impl")
1228    protected com.liferay.portlet.messageboards.service.persistence.MBBanPersistence mbBanPersistence;
1229    @BeanReference(name = "com.liferay.portlet.messageboards.service.persistence.MBCategoryPersistence.impl")
1230    protected com.liferay.portlet.messageboards.service.persistence.MBCategoryPersistence mbCategoryPersistence;
1231    @BeanReference(name = "com.liferay.portlet.messageboards.service.persistence.MBDiscussionPersistence.impl")
1232    protected com.liferay.portlet.messageboards.service.persistence.MBDiscussionPersistence mbDiscussionPersistence;
1233    @BeanReference(name = "com.liferay.portlet.messageboards.service.persistence.MBMessagePersistence.impl")
1234    protected com.liferay.portlet.messageboards.service.persistence.MBMessagePersistence mbMessagePersistence;
1235    @BeanReference(name = "com.liferay.portlet.messageboards.service.persistence.MBMessageFlagPersistence.impl")
1236    protected com.liferay.portlet.messageboards.service.persistence.MBMessageFlagPersistence mbMessageFlagPersistence;
1237    @BeanReference(name = "com.liferay.portlet.messageboards.service.persistence.MBStatsUserPersistence.impl")
1238    protected com.liferay.portlet.messageboards.service.persistence.MBStatsUserPersistence mbStatsUserPersistence;
1239    @BeanReference(name = "com.liferay.portlet.messageboards.service.persistence.MBThreadPersistence.impl")
1240    protected com.liferay.portlet.messageboards.service.persistence.MBThreadPersistence mbThreadPersistence;
1241    private static Log _log = LogFactoryUtil.getLog(MBDiscussionPersistenceImpl.class);
1242}