1   /**
2    * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.messageboards.service.persistence;
24  
25  import com.liferay.portal.SystemException;
26  import com.liferay.portal.kernel.dao.orm.DynamicQuery;
27  import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
28  import com.liferay.portal.kernel.dao.orm.Query;
29  import com.liferay.portal.kernel.dao.orm.QueryPos;
30  import com.liferay.portal.kernel.dao.orm.QueryUtil;
31  import com.liferay.portal.kernel.dao.orm.Session;
32  import com.liferay.portal.kernel.util.GetterUtil;
33  import com.liferay.portal.kernel.util.ListUtil;
34  import com.liferay.portal.kernel.util.OrderByComparator;
35  import com.liferay.portal.kernel.util.StringPool;
36  import com.liferay.portal.kernel.util.StringUtil;
37  import com.liferay.portal.model.ModelListener;
38  import com.liferay.portal.service.persistence.BatchSessionUtil;
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 {
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             if (BatchSessionUtil.isEnabled()) {
132                 Object staleObject = session.get(MBDiscussionImpl.class,
133                         mbDiscussion.getPrimaryKeyObj());
134 
135                 if (staleObject != null) {
136                     session.evict(staleObject);
137                 }
138             }
139 
140             session.delete(mbDiscussion);
141 
142             session.flush();
143 
144             return mbDiscussion;
145         }
146         catch (Exception e) {
147             throw processException(e);
148         }
149         finally {
150             closeSession(session);
151 
152             FinderCacheUtil.clearCache(MBDiscussion.class.getName());
153         }
154     }
155 
156     /**
157      * @deprecated Use <code>update(MBDiscussion mbDiscussion, boolean merge)</code>.
158      */
159     public MBDiscussion update(MBDiscussion mbDiscussion)
160         throws SystemException {
161         if (_log.isWarnEnabled()) {
162             _log.warn(
163                 "Using the deprecated update(MBDiscussion mbDiscussion) method. Use update(MBDiscussion mbDiscussion, boolean merge) instead.");
164         }
165 
166         return update(mbDiscussion, false);
167     }
168 
169     /**
170      * Add, update, or merge, the entity. This method also calls the model
171      * listeners to trigger the proper events associated with adding, deleting,
172      * or updating an entity.
173      *
174      * @param        mbDiscussion the entity to add, update, or merge
175      * @param        merge boolean value for whether to merge the entity. The
176      *                default value is false. Setting merge to true is more
177      *                expensive and should only be true when mbDiscussion is
178      *                transient. See LEP-5473 for a detailed discussion of this
179      *                method.
180      * @return        true if the portlet can be displayed via Ajax
181      */
182     public MBDiscussion update(MBDiscussion mbDiscussion, boolean merge)
183         throws SystemException {
184         boolean isNew = mbDiscussion.isNew();
185 
186         if (_listeners.length > 0) {
187             for (ModelListener listener : _listeners) {
188                 if (isNew) {
189                     listener.onBeforeCreate(mbDiscussion);
190                 }
191                 else {
192                     listener.onBeforeUpdate(mbDiscussion);
193                 }
194             }
195         }
196 
197         mbDiscussion = updateImpl(mbDiscussion, merge);
198 
199         if (_listeners.length > 0) {
200             for (ModelListener listener : _listeners) {
201                 if (isNew) {
202                     listener.onAfterCreate(mbDiscussion);
203                 }
204                 else {
205                     listener.onAfterUpdate(mbDiscussion);
206                 }
207             }
208         }
209 
210         return mbDiscussion;
211     }
212 
213     public MBDiscussion updateImpl(
214         com.liferay.portlet.messageboards.model.MBDiscussion mbDiscussion,
215         boolean merge) throws SystemException {
216         Session session = null;
217 
218         try {
219             session = openSession();
220 
221             BatchSessionUtil.update(session, mbDiscussion, merge);
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 findByThreadId(long threadId)
501         throws NoSuchDiscussionException, SystemException {
502         MBDiscussion mbDiscussion = fetchByThreadId(threadId);
503 
504         if (mbDiscussion == null) {
505             StringBuilder msg = new StringBuilder();
506 
507             msg.append("No MBDiscussion exists with the key {");
508 
509             msg.append("threadId=" + threadId);
510 
511             msg.append(StringPool.CLOSE_CURLY_BRACE);
512 
513             if (_log.isWarnEnabled()) {
514                 _log.warn(msg.toString());
515             }
516 
517             throw new NoSuchDiscussionException(msg.toString());
518         }
519 
520         return mbDiscussion;
521     }
522 
523     public MBDiscussion fetchByThreadId(long threadId)
524         throws SystemException {
525         boolean finderClassNameCacheEnabled = MBDiscussionModelImpl.CACHE_ENABLED;
526         String finderClassName = MBDiscussion.class.getName();
527         String finderMethodName = "fetchByThreadId";
528         String[] finderParams = new String[] { Long.class.getName() };
529         Object[] finderArgs = new Object[] { new Long(threadId) };
530 
531         Object result = null;
532 
533         if (finderClassNameCacheEnabled) {
534             result = FinderCacheUtil.getResult(finderClassName,
535                     finderMethodName, finderParams, finderArgs, this);
536         }
537 
538         if (result == null) {
539             Session session = null;
540 
541             try {
542                 session = openSession();
543 
544                 StringBuilder query = new StringBuilder();
545 
546                 query.append(
547                     "FROM com.liferay.portlet.messageboards.model.MBDiscussion WHERE ");
548 
549                 query.append("threadId = ?");
550 
551                 query.append(" ");
552 
553                 Query q = session.createQuery(query.toString());
554 
555                 QueryPos qPos = QueryPos.getInstance(q);
556 
557                 qPos.add(threadId);
558 
559                 List<MBDiscussion> list = q.list();
560 
561                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
562                     finderClassName, finderMethodName, finderParams,
563                     finderArgs, list);
564 
565                 if (list.size() == 0) {
566                     return null;
567                 }
568                 else {
569                     return list.get(0);
570                 }
571             }
572             catch (Exception e) {
573                 throw processException(e);
574             }
575             finally {
576                 closeSession(session);
577             }
578         }
579         else {
580             List<MBDiscussion> list = (List<MBDiscussion>)result;
581 
582             if (list.size() == 0) {
583                 return null;
584             }
585             else {
586                 return list.get(0);
587             }
588         }
589     }
590 
591     public MBDiscussion findByC_C(long classNameId, long classPK)
592         throws NoSuchDiscussionException, SystemException {
593         MBDiscussion mbDiscussion = fetchByC_C(classNameId, classPK);
594 
595         if (mbDiscussion == null) {
596             StringBuilder msg = new StringBuilder();
597 
598             msg.append("No MBDiscussion exists with the key {");
599 
600             msg.append("classNameId=" + classNameId);
601 
602             msg.append(", ");
603             msg.append("classPK=" + classPK);
604 
605             msg.append(StringPool.CLOSE_CURLY_BRACE);
606 
607             if (_log.isWarnEnabled()) {
608                 _log.warn(msg.toString());
609             }
610 
611             throw new NoSuchDiscussionException(msg.toString());
612         }
613 
614         return mbDiscussion;
615     }
616 
617     public MBDiscussion fetchByC_C(long classNameId, long classPK)
618         throws SystemException {
619         boolean finderClassNameCacheEnabled = MBDiscussionModelImpl.CACHE_ENABLED;
620         String finderClassName = MBDiscussion.class.getName();
621         String finderMethodName = "fetchByC_C";
622         String[] finderParams = new String[] {
623                 Long.class.getName(), Long.class.getName()
624             };
625         Object[] finderArgs = new Object[] {
626                 new Long(classNameId), new Long(classPK)
627             };
628 
629         Object result = null;
630 
631         if (finderClassNameCacheEnabled) {
632             result = FinderCacheUtil.getResult(finderClassName,
633                     finderMethodName, finderParams, finderArgs, this);
634         }
635 
636         if (result == null) {
637             Session session = null;
638 
639             try {
640                 session = openSession();
641 
642                 StringBuilder query = new StringBuilder();
643 
644                 query.append(
645                     "FROM com.liferay.portlet.messageboards.model.MBDiscussion WHERE ");
646 
647                 query.append("classNameId = ?");
648 
649                 query.append(" AND ");
650 
651                 query.append("classPK = ?");
652 
653                 query.append(" ");
654 
655                 Query q = session.createQuery(query.toString());
656 
657                 QueryPos qPos = QueryPos.getInstance(q);
658 
659                 qPos.add(classNameId);
660 
661                 qPos.add(classPK);
662 
663                 List<MBDiscussion> list = q.list();
664 
665                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
666                     finderClassName, finderMethodName, finderParams,
667                     finderArgs, list);
668 
669                 if (list.size() == 0) {
670                     return null;
671                 }
672                 else {
673                     return list.get(0);
674                 }
675             }
676             catch (Exception e) {
677                 throw processException(e);
678             }
679             finally {
680                 closeSession(session);
681             }
682         }
683         else {
684             List<MBDiscussion> list = (List<MBDiscussion>)result;
685 
686             if (list.size() == 0) {
687                 return null;
688             }
689             else {
690                 return list.get(0);
691             }
692         }
693     }
694 
695     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
696         throws SystemException {
697         Session session = null;
698 
699         try {
700             session = openSession();
701 
702             dynamicQuery.compile(session);
703 
704             return dynamicQuery.list();
705         }
706         catch (Exception e) {
707             throw processException(e);
708         }
709         finally {
710             closeSession(session);
711         }
712     }
713 
714     public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
715         int start, int end) throws SystemException {
716         Session session = null;
717 
718         try {
719             session = openSession();
720 
721             dynamicQuery.setLimit(start, end);
722 
723             dynamicQuery.compile(session);
724 
725             return dynamicQuery.list();
726         }
727         catch (Exception e) {
728             throw processException(e);
729         }
730         finally {
731             closeSession(session);
732         }
733     }
734 
735     public List<MBDiscussion> findAll() throws SystemException {
736         return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
737     }
738 
739     public List<MBDiscussion> findAll(int start, int end)
740         throws SystemException {
741         return findAll(start, end, null);
742     }
743 
744     public List<MBDiscussion> findAll(int start, int end, OrderByComparator obc)
745         throws SystemException {
746         boolean finderClassNameCacheEnabled = MBDiscussionModelImpl.CACHE_ENABLED;
747         String finderClassName = MBDiscussion.class.getName();
748         String finderMethodName = "findAll";
749         String[] finderParams = new String[] {
750                 "java.lang.Integer", "java.lang.Integer",
751                 "com.liferay.portal.kernel.util.OrderByComparator"
752             };
753         Object[] finderArgs = new Object[] {
754                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
755             };
756 
757         Object result = null;
758 
759         if (finderClassNameCacheEnabled) {
760             result = FinderCacheUtil.getResult(finderClassName,
761                     finderMethodName, finderParams, finderArgs, this);
762         }
763 
764         if (result == null) {
765             Session session = null;
766 
767             try {
768                 session = openSession();
769 
770                 StringBuilder query = new StringBuilder();
771 
772                 query.append(
773                     "FROM com.liferay.portlet.messageboards.model.MBDiscussion ");
774 
775                 if (obc != null) {
776                     query.append("ORDER BY ");
777                     query.append(obc.getOrderBy());
778                 }
779 
780                 Query q = session.createQuery(query.toString());
781 
782                 List<MBDiscussion> list = null;
783 
784                 if (obc == null) {
785                     list = (List<MBDiscussion>)QueryUtil.list(q, getDialect(),
786                             start, end, false);
787 
788                     Collections.sort(list);
789                 }
790                 else {
791                     list = (List<MBDiscussion>)QueryUtil.list(q, getDialect(),
792                             start, end);
793                 }
794 
795                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
796                     finderClassName, finderMethodName, finderParams,
797                     finderArgs, list);
798 
799                 return list;
800             }
801             catch (Exception e) {
802                 throw processException(e);
803             }
804             finally {
805                 closeSession(session);
806             }
807         }
808         else {
809             return (List<MBDiscussion>)result;
810         }
811     }
812 
813     public void removeByClassNameId(long classNameId) throws SystemException {
814         for (MBDiscussion mbDiscussion : findByClassNameId(classNameId)) {
815             remove(mbDiscussion);
816         }
817     }
818 
819     public void removeByThreadId(long threadId)
820         throws NoSuchDiscussionException, SystemException {
821         MBDiscussion mbDiscussion = findByThreadId(threadId);
822 
823         remove(mbDiscussion);
824     }
825 
826     public void removeByC_C(long classNameId, long classPK)
827         throws NoSuchDiscussionException, SystemException {
828         MBDiscussion mbDiscussion = findByC_C(classNameId, classPK);
829 
830         remove(mbDiscussion);
831     }
832 
833     public void removeAll() throws SystemException {
834         for (MBDiscussion mbDiscussion : findAll()) {
835             remove(mbDiscussion);
836         }
837     }
838 
839     public int countByClassNameId(long classNameId) throws SystemException {
840         boolean finderClassNameCacheEnabled = MBDiscussionModelImpl.CACHE_ENABLED;
841         String finderClassName = MBDiscussion.class.getName();
842         String finderMethodName = "countByClassNameId";
843         String[] finderParams = new String[] { Long.class.getName() };
844         Object[] finderArgs = new Object[] { new Long(classNameId) };
845 
846         Object result = null;
847 
848         if (finderClassNameCacheEnabled) {
849             result = FinderCacheUtil.getResult(finderClassName,
850                     finderMethodName, finderParams, finderArgs, this);
851         }
852 
853         if (result == null) {
854             Session session = null;
855 
856             try {
857                 session = openSession();
858 
859                 StringBuilder query = new StringBuilder();
860 
861                 query.append("SELECT COUNT(*) ");
862                 query.append(
863                     "FROM com.liferay.portlet.messageboards.model.MBDiscussion WHERE ");
864 
865                 query.append("classNameId = ?");
866 
867                 query.append(" ");
868 
869                 Query q = session.createQuery(query.toString());
870 
871                 QueryPos qPos = QueryPos.getInstance(q);
872 
873                 qPos.add(classNameId);
874 
875                 Long count = null;
876 
877                 Iterator<Long> itr = q.list().iterator();
878 
879                 if (itr.hasNext()) {
880                     count = itr.next();
881                 }
882 
883                 if (count == null) {
884                     count = new Long(0);
885                 }
886 
887                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
888                     finderClassName, finderMethodName, finderParams,
889                     finderArgs, count);
890 
891                 return count.intValue();
892             }
893             catch (Exception e) {
894                 throw processException(e);
895             }
896             finally {
897                 closeSession(session);
898             }
899         }
900         else {
901             return ((Long)result).intValue();
902         }
903     }
904 
905     public int countByThreadId(long threadId) throws SystemException {
906         boolean finderClassNameCacheEnabled = MBDiscussionModelImpl.CACHE_ENABLED;
907         String finderClassName = MBDiscussion.class.getName();
908         String finderMethodName = "countByThreadId";
909         String[] finderParams = new String[] { Long.class.getName() };
910         Object[] finderArgs = new Object[] { new Long(threadId) };
911 
912         Object result = null;
913 
914         if (finderClassNameCacheEnabled) {
915             result = FinderCacheUtil.getResult(finderClassName,
916                     finderMethodName, finderParams, finderArgs, this);
917         }
918 
919         if (result == null) {
920             Session session = null;
921 
922             try {
923                 session = openSession();
924 
925                 StringBuilder query = new StringBuilder();
926 
927                 query.append("SELECT COUNT(*) ");
928                 query.append(
929                     "FROM com.liferay.portlet.messageboards.model.MBDiscussion WHERE ");
930 
931                 query.append("threadId = ?");
932 
933                 query.append(" ");
934 
935                 Query q = session.createQuery(query.toString());
936 
937                 QueryPos qPos = QueryPos.getInstance(q);
938 
939                 qPos.add(threadId);
940 
941                 Long count = null;
942 
943                 Iterator<Long> itr = q.list().iterator();
944 
945                 if (itr.hasNext()) {
946                     count = itr.next();
947                 }
948 
949                 if (count == null) {
950                     count = new Long(0);
951                 }
952 
953                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
954                     finderClassName, finderMethodName, finderParams,
955                     finderArgs, count);
956 
957                 return count.intValue();
958             }
959             catch (Exception e) {
960                 throw processException(e);
961             }
962             finally {
963                 closeSession(session);
964             }
965         }
966         else {
967             return ((Long)result).intValue();
968         }
969     }
970 
971     public int countByC_C(long classNameId, long classPK)
972         throws SystemException {
973         boolean finderClassNameCacheEnabled = MBDiscussionModelImpl.CACHE_ENABLED;
974         String finderClassName = MBDiscussion.class.getName();
975         String finderMethodName = "countByC_C";
976         String[] finderParams = new String[] {
977                 Long.class.getName(), Long.class.getName()
978             };
979         Object[] finderArgs = new Object[] {
980                 new Long(classNameId), new Long(classPK)
981             };
982 
983         Object result = null;
984 
985         if (finderClassNameCacheEnabled) {
986             result = FinderCacheUtil.getResult(finderClassName,
987                     finderMethodName, finderParams, finderArgs, this);
988         }
989 
990         if (result == null) {
991             Session session = null;
992 
993             try {
994                 session = openSession();
995 
996                 StringBuilder query = new StringBuilder();
997 
998                 query.append("SELECT COUNT(*) ");
999                 query.append(
1000                    "FROM com.liferay.portlet.messageboards.model.MBDiscussion WHERE ");
1001
1002                query.append("classNameId = ?");
1003
1004                query.append(" AND ");
1005
1006                query.append("classPK = ?");
1007
1008                query.append(" ");
1009
1010                Query q = session.createQuery(query.toString());
1011
1012                QueryPos qPos = QueryPos.getInstance(q);
1013
1014                qPos.add(classNameId);
1015
1016                qPos.add(classPK);
1017
1018                Long count = null;
1019
1020                Iterator<Long> itr = q.list().iterator();
1021
1022                if (itr.hasNext()) {
1023                    count = itr.next();
1024                }
1025
1026                if (count == null) {
1027                    count = new Long(0);
1028                }
1029
1030                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1031                    finderClassName, finderMethodName, finderParams,
1032                    finderArgs, count);
1033
1034                return count.intValue();
1035            }
1036            catch (Exception e) {
1037                throw processException(e);
1038            }
1039            finally {
1040                closeSession(session);
1041            }
1042        }
1043        else {
1044            return ((Long)result).intValue();
1045        }
1046    }
1047
1048    public int countAll() throws SystemException {
1049        boolean finderClassNameCacheEnabled = MBDiscussionModelImpl.CACHE_ENABLED;
1050        String finderClassName = MBDiscussion.class.getName();
1051        String finderMethodName = "countAll";
1052        String[] finderParams = new String[] {  };
1053        Object[] finderArgs = new Object[] {  };
1054
1055        Object result = null;
1056
1057        if (finderClassNameCacheEnabled) {
1058            result = FinderCacheUtil.getResult(finderClassName,
1059                    finderMethodName, finderParams, finderArgs, this);
1060        }
1061
1062        if (result == null) {
1063            Session session = null;
1064
1065            try {
1066                session = openSession();
1067
1068                Query q = session.createQuery(
1069                        "SELECT COUNT(*) FROM com.liferay.portlet.messageboards.model.MBDiscussion");
1070
1071                Long count = null;
1072
1073                Iterator<Long> itr = q.list().iterator();
1074
1075                if (itr.hasNext()) {
1076                    count = itr.next();
1077                }
1078
1079                if (count == null) {
1080                    count = new Long(0);
1081                }
1082
1083                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1084                    finderClassName, finderMethodName, finderParams,
1085                    finderArgs, count);
1086
1087                return count.intValue();
1088            }
1089            catch (Exception e) {
1090                throw processException(e);
1091            }
1092            finally {
1093                closeSession(session);
1094            }
1095        }
1096        else {
1097            return ((Long)result).intValue();
1098        }
1099    }
1100
1101    public void registerListener(ModelListener listener) {
1102        List<ModelListener> listeners = ListUtil.fromArray(_listeners);
1103
1104        listeners.add(listener);
1105
1106        _listeners = listeners.toArray(new ModelListener[listeners.size()]);
1107    }
1108
1109    public void unregisterListener(ModelListener listener) {
1110        List<ModelListener> listeners = ListUtil.fromArray(_listeners);
1111
1112        listeners.remove(listener);
1113
1114        _listeners = listeners.toArray(new ModelListener[listeners.size()]);
1115    }
1116
1117    public void afterPropertiesSet() {
1118        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1119                    com.liferay.portal.util.PropsUtil.get(
1120                        "value.object.listener.com.liferay.portlet.messageboards.model.MBDiscussion")));
1121
1122        if (listenerClassNames.length > 0) {
1123            try {
1124                List<ModelListener> listeners = new ArrayList<ModelListener>();
1125
1126                for (String listenerClassName : listenerClassNames) {
1127                    listeners.add((ModelListener)Class.forName(
1128                            listenerClassName).newInstance());
1129                }
1130
1131                _listeners = listeners.toArray(new ModelListener[listeners.size()]);
1132            }
1133            catch (Exception e) {
1134                _log.error(e);
1135            }
1136        }
1137    }
1138
1139    private static Log _log = LogFactory.getLog(MBDiscussionPersistenceImpl.class);
1140    private ModelListener[] _listeners = new ModelListener[0];
1141}