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