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