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