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