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.bean.InitializingBean;
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.util.GetterUtil;
34  import com.liferay.portal.kernel.util.ListUtil;
35  import com.liferay.portal.kernel.util.OrderByComparator;
36  import com.liferay.portal.kernel.util.StringPool;
37  import com.liferay.portal.kernel.util.StringUtil;
38  import com.liferay.portal.model.ModelListener;
39  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
40  
41  import com.liferay.portlet.messageboards.NoSuchDiscussionException;
42  import com.liferay.portlet.messageboards.model.MBDiscussion;
43  import com.liferay.portlet.messageboards.model.impl.MBDiscussionImpl;
44  import com.liferay.portlet.messageboards.model.impl.MBDiscussionModelImpl;
45  
46  import org.apache.commons.logging.Log;
47  import org.apache.commons.logging.LogFactory;
48  
49  import java.util.ArrayList;
50  import java.util.Collections;
51  import java.util.Iterator;
52  import java.util.List;
53  
54  /**
55   * <a href="MBDiscussionPersistenceImpl.java.html"><b><i>View Source</i></b></a>
56   *
57   * @author Brian Wing Shun Chan
58   *
59   */
60  public class MBDiscussionPersistenceImpl extends BasePersistenceImpl
61      implements MBDiscussionPersistence, InitializingBean {
62      public MBDiscussion create(long discussionId) {
63          MBDiscussion mbDiscussion = new MBDiscussionImpl();
64  
65          mbDiscussion.setNew(true);
66          mbDiscussion.setPrimaryKey(discussionId);
67  
68          return mbDiscussion;
69      }
70  
71      public MBDiscussion remove(long discussionId)
72          throws NoSuchDiscussionException, SystemException {
73          Session session = null;
74  
75          try {
76              session = openSession();
77  
78              MBDiscussion mbDiscussion = (MBDiscussion)session.get(MBDiscussionImpl.class,
79                      new Long(discussionId));
80  
81              if (mbDiscussion == null) {
82                  if (_log.isWarnEnabled()) {
83                      _log.warn("No MBDiscussion exists with the primary key " +
84                          discussionId);
85                  }
86  
87                  throw new NoSuchDiscussionException(
88                      "No MBDiscussion exists with the primary key " +
89                      discussionId);
90              }
91  
92              return remove(mbDiscussion);
93          }
94          catch (NoSuchDiscussionException nsee) {
95              throw nsee;
96          }
97          catch (Exception e) {
98              throw processException(e);
99          }
100         finally {
101             closeSession(session);
102         }
103     }
104 
105     public MBDiscussion remove(MBDiscussion mbDiscussion)
106         throws SystemException {
107         if (_listeners.length > 0) {
108             for (ModelListener listener : _listeners) {
109                 listener.onBeforeRemove(mbDiscussion);
110             }
111         }
112 
113         mbDiscussion = removeImpl(mbDiscussion);
114 
115         if (_listeners.length > 0) {
116             for (ModelListener listener : _listeners) {
117                 listener.onAfterRemove(mbDiscussion);
118             }
119         }
120 
121         return mbDiscussion;
122     }
123 
124     protected MBDiscussion removeImpl(MBDiscussion mbDiscussion)
125         throws SystemException {
126         Session session = null;
127 
128         try {
129             session = openSession();
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         if (_listeners.length > 0) {
178             for (ModelListener listener : _listeners) {
179                 if (isNew) {
180                     listener.onBeforeCreate(mbDiscussion);
181                 }
182                 else {
183                     listener.onBeforeUpdate(mbDiscussion);
184                 }
185             }
186         }
187 
188         mbDiscussion = updateImpl(mbDiscussion, merge);
189 
190         if (_listeners.length > 0) {
191             for (ModelListener listener : _listeners) {
192                 if (isNew) {
193                     listener.onAfterCreate(mbDiscussion);
194                 }
195                 else {
196                     listener.onAfterUpdate(mbDiscussion);
197                 }
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             if (merge) {
213                 session.merge(mbDiscussion);
214             }
215             else {
216                 if (mbDiscussion.isNew()) {
217                     session.save(mbDiscussion);
218                 }
219             }
220 
221             session.flush();
222 
223             mbDiscussion.setNew(false);
224 
225             return mbDiscussion;
226         }
227         catch (Exception e) {
228             throw processException(e);
229         }
230         finally {
231             closeSession(session);
232 
233             FinderCacheUtil.clearCache(MBDiscussion.class.getName());
234         }
235     }
236 
237     public MBDiscussion findByPrimaryKey(long discussionId)
238         throws NoSuchDiscussionException, SystemException {
239         MBDiscussion mbDiscussion = fetchByPrimaryKey(discussionId);
240 
241         if (mbDiscussion == null) {
242             if (_log.isWarnEnabled()) {
243                 _log.warn("No MBDiscussion exists with the primary key " +
244                     discussionId);
245             }
246 
247             throw new NoSuchDiscussionException(
248                 "No MBDiscussion exists with the primary key " + discussionId);
249         }
250 
251         return mbDiscussion;
252     }
253 
254     public MBDiscussion fetchByPrimaryKey(long discussionId)
255         throws SystemException {
256         Session session = null;
257 
258         try {
259             session = openSession();
260 
261             return (MBDiscussion)session.get(MBDiscussionImpl.class,
262                 new Long(discussionId));
263         }
264         catch (Exception e) {
265             throw processException(e);
266         }
267         finally {
268             closeSession(session);
269         }
270     }
271 
272     public List<MBDiscussion> findByClassNameId(long classNameId)
273         throws SystemException {
274         boolean finderClassNameCacheEnabled = MBDiscussionModelImpl.CACHE_ENABLED;
275         String finderClassName = MBDiscussion.class.getName();
276         String finderMethodName = "findByClassNameId";
277         String[] finderParams = new String[] { Long.class.getName() };
278         Object[] finderArgs = new Object[] { new Long(classNameId) };
279 
280         Object result = null;
281 
282         if (finderClassNameCacheEnabled) {
283             result = FinderCacheUtil.getResult(finderClassName,
284                     finderMethodName, finderParams, finderArgs, this);
285         }
286 
287         if (result == null) {
288             Session session = null;
289 
290             try {
291                 session = openSession();
292 
293                 StringBuilder query = new StringBuilder();
294 
295                 query.append(
296                     "FROM com.liferay.portlet.messageboards.model.MBDiscussion WHERE ");
297 
298                 query.append("classNameId = ?");
299 
300                 query.append(" ");
301 
302                 Query q = session.createQuery(query.toString());
303 
304                 QueryPos qPos = QueryPos.getInstance(q);
305 
306                 qPos.add(classNameId);
307 
308                 List<MBDiscussion> list = q.list();
309 
310                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
311                     finderClassName, finderMethodName, finderParams,
312                     finderArgs, list);
313 
314                 return list;
315             }
316             catch (Exception e) {
317                 throw processException(e);
318             }
319             finally {
320                 closeSession(session);
321             }
322         }
323         else {
324             return (List<MBDiscussion>)result;
325         }
326     }
327 
328     public List<MBDiscussion> findByClassNameId(long classNameId, int start,
329         int end) throws SystemException {
330         return findByClassNameId(classNameId, start, end, null);
331     }
332 
333     public List<MBDiscussion> findByClassNameId(long classNameId, int start,
334         int end, OrderByComparator obc) throws SystemException {
335         boolean finderClassNameCacheEnabled = MBDiscussionModelImpl.CACHE_ENABLED;
336         String finderClassName = MBDiscussion.class.getName();
337         String finderMethodName = "findByClassNameId";
338         String[] finderParams = new String[] {
339                 Long.class.getName(),
340                 
341                 "java.lang.Integer", "java.lang.Integer",
342                 "com.liferay.portal.kernel.util.OrderByComparator"
343             };
344         Object[] finderArgs = new Object[] {
345                 new Long(classNameId),
346                 
347                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
348             };
349 
350         Object result = null;
351 
352         if (finderClassNameCacheEnabled) {
353             result = FinderCacheUtil.getResult(finderClassName,
354                     finderMethodName, finderParams, finderArgs, this);
355         }
356 
357         if (result == null) {
358             Session session = null;
359 
360             try {
361                 session = openSession();
362 
363                 StringBuilder query = new StringBuilder();
364 
365                 query.append(
366                     "FROM com.liferay.portlet.messageboards.model.MBDiscussion WHERE ");
367 
368                 query.append("classNameId = ?");
369 
370                 query.append(" ");
371 
372                 if (obc != null) {
373                     query.append("ORDER BY ");
374                     query.append(obc.getOrderBy());
375                 }
376 
377                 Query q = session.createQuery(query.toString());
378 
379                 QueryPos qPos = QueryPos.getInstance(q);
380 
381                 qPos.add(classNameId);
382 
383                 List<MBDiscussion> list = (List<MBDiscussion>)QueryUtil.list(q,
384                         getDialect(), start, end);
385 
386                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
387                     finderClassName, finderMethodName, finderParams,
388                     finderArgs, list);
389 
390                 return list;
391             }
392             catch (Exception e) {
393                 throw processException(e);
394             }
395             finally {
396                 closeSession(session);
397             }
398         }
399         else {
400             return (List<MBDiscussion>)result;
401         }
402     }
403 
404     public MBDiscussion findByClassNameId_First(long classNameId,
405         OrderByComparator obc)
406         throws NoSuchDiscussionException, SystemException {
407         List<MBDiscussion> list = findByClassNameId(classNameId, 0, 1, obc);
408 
409         if (list.size() == 0) {
410             StringBuilder msg = new StringBuilder();
411 
412             msg.append("No MBDiscussion exists with the key {");
413 
414             msg.append("classNameId=" + classNameId);
415 
416             msg.append(StringPool.CLOSE_CURLY_BRACE);
417 
418             throw new NoSuchDiscussionException(msg.toString());
419         }
420         else {
421             return list.get(0);
422         }
423     }
424 
425     public MBDiscussion findByClassNameId_Last(long classNameId,
426         OrderByComparator obc)
427         throws NoSuchDiscussionException, SystemException {
428         int count = countByClassNameId(classNameId);
429 
430         List<MBDiscussion> list = findByClassNameId(classNameId, count - 1,
431                 count, obc);
432 
433         if (list.size() == 0) {
434             StringBuilder msg = new StringBuilder();
435 
436             msg.append("No MBDiscussion exists with the key {");
437 
438             msg.append("classNameId=" + classNameId);
439 
440             msg.append(StringPool.CLOSE_CURLY_BRACE);
441 
442             throw new NoSuchDiscussionException(msg.toString());
443         }
444         else {
445             return list.get(0);
446         }
447     }
448 
449     public MBDiscussion[] findByClassNameId_PrevAndNext(long discussionId,
450         long classNameId, OrderByComparator obc)
451         throws NoSuchDiscussionException, SystemException {
452         MBDiscussion mbDiscussion = findByPrimaryKey(discussionId);
453 
454         int count = countByClassNameId(classNameId);
455 
456         Session session = null;
457 
458         try {
459             session = openSession();
460 
461             StringBuilder query = new StringBuilder();
462 
463             query.append(
464                 "FROM com.liferay.portlet.messageboards.model.MBDiscussion WHERE ");
465 
466             query.append("classNameId = ?");
467 
468             query.append(" ");
469 
470             if (obc != null) {
471                 query.append("ORDER BY ");
472                 query.append(obc.getOrderBy());
473             }
474 
475             Query q = session.createQuery(query.toString());
476 
477             QueryPos qPos = QueryPos.getInstance(q);
478 
479             qPos.add(classNameId);
480 
481             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
482                     mbDiscussion);
483 
484             MBDiscussion[] array = new MBDiscussionImpl[3];
485 
486             array[0] = (MBDiscussion)objArray[0];
487             array[1] = (MBDiscussion)objArray[1];
488             array[2] = (MBDiscussion)objArray[2];
489 
490             return array;
491         }
492         catch (Exception e) {
493             throw processException(e);
494         }
495         finally {
496             closeSession(session);
497         }
498     }
499 
500     public MBDiscussion findByC_C(long classNameId, long classPK)
501         throws NoSuchDiscussionException, SystemException {
502         MBDiscussion mbDiscussion = fetchByC_C(classNameId, classPK);
503 
504         if (mbDiscussion == null) {
505             StringBuilder msg = new StringBuilder();
506 
507             msg.append("No MBDiscussion exists with the key {");
508 
509             msg.append("classNameId=" + classNameId);
510 
511             msg.append(", ");
512             msg.append("classPK=" + classPK);
513 
514             msg.append(StringPool.CLOSE_CURLY_BRACE);
515 
516             if (_log.isWarnEnabled()) {
517                 _log.warn(msg.toString());
518             }
519 
520             throw new NoSuchDiscussionException(msg.toString());
521         }
522 
523         return mbDiscussion;
524     }
525 
526     public MBDiscussion fetchByC_C(long classNameId, long classPK)
527         throws SystemException {
528         boolean finderClassNameCacheEnabled = MBDiscussionModelImpl.CACHE_ENABLED;
529         String finderClassName = MBDiscussion.class.getName();
530         String finderMethodName = "fetchByC_C";
531         String[] finderParams = new String[] {
532                 Long.class.getName(), Long.class.getName()
533             };
534         Object[] finderArgs = new Object[] {
535                 new Long(classNameId), new Long(classPK)
536             };
537 
538         Object result = null;
539 
540         if (finderClassNameCacheEnabled) {
541             result = FinderCacheUtil.getResult(finderClassName,
542                     finderMethodName, finderParams, finderArgs, this);
543         }
544 
545         if (result == null) {
546             Session session = null;
547 
548             try {
549                 session = openSession();
550 
551                 StringBuilder query = new StringBuilder();
552 
553                 query.append(
554                     "FROM com.liferay.portlet.messageboards.model.MBDiscussion WHERE ");
555 
556                 query.append("classNameId = ?");
557 
558                 query.append(" AND ");
559 
560                 query.append("classPK = ?");
561 
562                 query.append(" ");
563 
564                 Query q = session.createQuery(query.toString());
565 
566                 QueryPos qPos = QueryPos.getInstance(q);
567 
568                 qPos.add(classNameId);
569 
570                 qPos.add(classPK);
571 
572                 List<MBDiscussion> list = q.list();
573 
574                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
575                     finderClassName, finderMethodName, finderParams,
576                     finderArgs, list);
577 
578                 if (list.size() == 0) {
579                     return null;
580                 }
581                 else {
582                     return list.get(0);
583                 }
584             }
585             catch (Exception e) {
586                 throw processException(e);
587             }
588             finally {
589                 closeSession(session);
590             }
591         }
592         else {
593             List<MBDiscussion> list = (List<MBDiscussion>)result;
594 
595             if (list.size() == 0) {
596                 return null;
597             }
598             else {
599                 return list.get(0);
600             }
601         }
602     }
603 
604     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
605         throws SystemException {
606         Session session = null;
607 
608         try {
609             session = openSession();
610 
611             dynamicQuery.compile(session);
612 
613             return dynamicQuery.list();
614         }
615         catch (Exception e) {
616             throw processException(e);
617         }
618         finally {
619             closeSession(session);
620         }
621     }
622 
623     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
624         int start, int end) throws SystemException {
625         Session session = null;
626 
627         try {
628             session = openSession();
629 
630             dynamicQuery.setLimit(start, end);
631 
632             dynamicQuery.compile(session);
633 
634             return dynamicQuery.list();
635         }
636         catch (Exception e) {
637             throw processException(e);
638         }
639         finally {
640             closeSession(session);
641         }
642     }
643 
644     public List<MBDiscussion> findAll() throws SystemException {
645         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
646     }
647 
648     public List<MBDiscussion> findAll(int start, int end)
649         throws SystemException {
650         return findAll(start, end, null);
651     }
652 
653     public List<MBDiscussion> findAll(int start, int end, OrderByComparator obc)
654         throws SystemException {
655         boolean finderClassNameCacheEnabled = MBDiscussionModelImpl.CACHE_ENABLED;
656         String finderClassName = MBDiscussion.class.getName();
657         String finderMethodName = "findAll";
658         String[] finderParams = new String[] {
659                 "java.lang.Integer", "java.lang.Integer",
660                 "com.liferay.portal.kernel.util.OrderByComparator"
661             };
662         Object[] finderArgs = new Object[] {
663                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
664             };
665 
666         Object result = null;
667 
668         if (finderClassNameCacheEnabled) {
669             result = FinderCacheUtil.getResult(finderClassName,
670                     finderMethodName, finderParams, finderArgs, this);
671         }
672 
673         if (result == null) {
674             Session session = null;
675 
676             try {
677                 session = openSession();
678 
679                 StringBuilder query = new StringBuilder();
680 
681                 query.append(
682                     "FROM com.liferay.portlet.messageboards.model.MBDiscussion ");
683 
684                 if (obc != null) {
685                     query.append("ORDER BY ");
686                     query.append(obc.getOrderBy());
687                 }
688 
689                 Query q = session.createQuery(query.toString());
690 
691                 List<MBDiscussion> list = (List<MBDiscussion>)QueryUtil.list(q,
692                         getDialect(), start, end);
693 
694                 if (obc == null) {
695                     Collections.sort(list);
696                 }
697 
698                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
699                     finderClassName, finderMethodName, finderParams,
700                     finderArgs, list);
701 
702                 return list;
703             }
704             catch (Exception e) {
705                 throw processException(e);
706             }
707             finally {
708                 closeSession(session);
709             }
710         }
711         else {
712             return (List<MBDiscussion>)result;
713         }
714     }
715 
716     public void removeByClassNameId(long classNameId) throws SystemException {
717         for (MBDiscussion mbDiscussion : findByClassNameId(classNameId)) {
718             remove(mbDiscussion);
719         }
720     }
721 
722     public void removeByC_C(long classNameId, long classPK)
723         throws NoSuchDiscussionException, SystemException {
724         MBDiscussion mbDiscussion = findByC_C(classNameId, classPK);
725 
726         remove(mbDiscussion);
727     }
728 
729     public void removeAll() throws SystemException {
730         for (MBDiscussion mbDiscussion : findAll()) {
731             remove(mbDiscussion);
732         }
733     }
734 
735     public int countByClassNameId(long classNameId) throws SystemException {
736         boolean finderClassNameCacheEnabled = MBDiscussionModelImpl.CACHE_ENABLED;
737         String finderClassName = MBDiscussion.class.getName();
738         String finderMethodName = "countByClassNameId";
739         String[] finderParams = new String[] { Long.class.getName() };
740         Object[] finderArgs = new Object[] { new Long(classNameId) };
741 
742         Object result = null;
743 
744         if (finderClassNameCacheEnabled) {
745             result = FinderCacheUtil.getResult(finderClassName,
746                     finderMethodName, finderParams, finderArgs, this);
747         }
748 
749         if (result == null) {
750             Session session = null;
751 
752             try {
753                 session = openSession();
754 
755                 StringBuilder query = new StringBuilder();
756 
757                 query.append("SELECT COUNT(*) ");
758                 query.append(
759                     "FROM com.liferay.portlet.messageboards.model.MBDiscussion WHERE ");
760 
761                 query.append("classNameId = ?");
762 
763                 query.append(" ");
764 
765                 Query q = session.createQuery(query.toString());
766 
767                 QueryPos qPos = QueryPos.getInstance(q);
768 
769                 qPos.add(classNameId);
770 
771                 Long count = null;
772 
773                 Iterator<Long> itr = q.list().iterator();
774 
775                 if (itr.hasNext()) {
776                     count = itr.next();
777                 }
778 
779                 if (count == null) {
780                     count = new Long(0);
781                 }
782 
783                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
784                     finderClassName, finderMethodName, finderParams,
785                     finderArgs, count);
786 
787                 return count.intValue();
788             }
789             catch (Exception e) {
790                 throw processException(e);
791             }
792             finally {
793                 closeSession(session);
794             }
795         }
796         else {
797             return ((Long)result).intValue();
798         }
799     }
800 
801     public int countByC_C(long classNameId, long classPK)
802         throws SystemException {
803         boolean finderClassNameCacheEnabled = MBDiscussionModelImpl.CACHE_ENABLED;
804         String finderClassName = MBDiscussion.class.getName();
805         String finderMethodName = "countByC_C";
806         String[] finderParams = new String[] {
807                 Long.class.getName(), Long.class.getName()
808             };
809         Object[] finderArgs = new Object[] {
810                 new Long(classNameId), new Long(classPK)
811             };
812 
813         Object result = null;
814 
815         if (finderClassNameCacheEnabled) {
816             result = FinderCacheUtil.getResult(finderClassName,
817                     finderMethodName, finderParams, finderArgs, this);
818         }
819 
820         if (result == null) {
821             Session session = null;
822 
823             try {
824                 session = openSession();
825 
826                 StringBuilder query = new StringBuilder();
827 
828                 query.append("SELECT COUNT(*) ");
829                 query.append(
830                     "FROM com.liferay.portlet.messageboards.model.MBDiscussion WHERE ");
831 
832                 query.append("classNameId = ?");
833 
834                 query.append(" AND ");
835 
836                 query.append("classPK = ?");
837 
838                 query.append(" ");
839 
840                 Query q = session.createQuery(query.toString());
841 
842                 QueryPos qPos = QueryPos.getInstance(q);
843 
844                 qPos.add(classNameId);
845 
846                 qPos.add(classPK);
847 
848                 Long count = null;
849 
850                 Iterator<Long> itr = q.list().iterator();
851 
852                 if (itr.hasNext()) {
853                     count = itr.next();
854                 }
855 
856                 if (count == null) {
857                     count = new Long(0);
858                 }
859 
860                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
861                     finderClassName, finderMethodName, finderParams,
862                     finderArgs, count);
863 
864                 return count.intValue();
865             }
866             catch (Exception e) {
867                 throw processException(e);
868             }
869             finally {
870                 closeSession(session);
871             }
872         }
873         else {
874             return ((Long)result).intValue();
875         }
876     }
877 
878     public int countAll() throws SystemException {
879         boolean finderClassNameCacheEnabled = MBDiscussionModelImpl.CACHE_ENABLED;
880         String finderClassName = MBDiscussion.class.getName();
881         String finderMethodName = "countAll";
882         String[] finderParams = new String[] {  };
883         Object[] finderArgs = new Object[] {  };
884 
885         Object result = null;
886 
887         if (finderClassNameCacheEnabled) {
888             result = FinderCacheUtil.getResult(finderClassName,
889                     finderMethodName, finderParams, finderArgs, this);
890         }
891 
892         if (result == null) {
893             Session session = null;
894 
895             try {
896                 session = openSession();
897 
898                 Query q = session.createQuery(
899                         "SELECT COUNT(*) FROM com.liferay.portlet.messageboards.model.MBDiscussion");
900 
901                 Long count = null;
902 
903                 Iterator<Long> itr = q.list().iterator();
904 
905                 if (itr.hasNext()) {
906                     count = itr.next();
907                 }
908 
909                 if (count == null) {
910                     count = new Long(0);
911                 }
912 
913                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
914                     finderClassName, finderMethodName, finderParams,
915                     finderArgs, count);
916 
917                 return count.intValue();
918             }
919             catch (Exception e) {
920                 throw processException(e);
921             }
922             finally {
923                 closeSession(session);
924             }
925         }
926         else {
927             return ((Long)result).intValue();
928         }
929     }
930 
931     public void registerListener(ModelListener listener) {
932         List<ModelListener> listeners = ListUtil.fromArray(_listeners);
933 
934         listeners.add(listener);
935 
936         _listeners = listeners.toArray(new ModelListener[listeners.size()]);
937     }
938 
939     public void unregisterListener(ModelListener listener) {
940         List<ModelListener> listeners = ListUtil.fromArray(_listeners);
941 
942         listeners.remove(listener);
943 
944         _listeners = listeners.toArray(new ModelListener[listeners.size()]);
945     }
946 
947     public void afterPropertiesSet() {
948         String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
949                     com.liferay.portal.util.PropsUtil.get(
950                         "value.object.listener.com.liferay.portlet.messageboards.model.MBDiscussion")));
951 
952         if (listenerClassNames.length > 0) {
953             try {
954                 List<ModelListener> listeners = new ArrayList<ModelListener>();
955 
956                 for (String listenerClassName : listenerClassNames) {
957                     listeners.add((ModelListener)Class.forName(
958                             listenerClassName).newInstance());
959                 }
960 
961                 _listeners = listeners.toArray(new ModelListener[listeners.size()]);
962             }
963             catch (Exception e) {
964                 _log.error(e);
965             }
966         }
967     }
968 
969     private static Log _log = LogFactory.getLog(MBDiscussionPersistenceImpl.class);
970     private ModelListener[] _listeners = new ModelListener[0];
971 }