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.kernel.util.Validator;
39  import com.liferay.portal.kernel.uuid.PortalUUIDUtil;
40  import com.liferay.portal.model.ModelListener;
41  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
42  
43  import com.liferay.portlet.messageboards.NoSuchMessageException;
44  import com.liferay.portlet.messageboards.model.MBMessage;
45  import com.liferay.portlet.messageboards.model.impl.MBMessageImpl;
46  import com.liferay.portlet.messageboards.model.impl.MBMessageModelImpl;
47  
48  import org.apache.commons.logging.Log;
49  import org.apache.commons.logging.LogFactory;
50  
51  import java.util.ArrayList;
52  import java.util.Collections;
53  import java.util.Iterator;
54  import java.util.List;
55  
56  /**
57   * <a href="MBMessagePersistenceImpl.java.html"><b><i>View Source</i></b></a>
58   *
59   * @author Brian Wing Shun Chan
60   *
61   */
62  public class MBMessagePersistenceImpl extends BasePersistenceImpl
63      implements MBMessagePersistence, InitializingBean {
64      public MBMessage create(long messageId) {
65          MBMessage mbMessage = new MBMessageImpl();
66  
67          mbMessage.setNew(true);
68          mbMessage.setPrimaryKey(messageId);
69  
70          String uuid = PortalUUIDUtil.generate();
71  
72          mbMessage.setUuid(uuid);
73  
74          return mbMessage;
75      }
76  
77      public MBMessage remove(long messageId)
78          throws NoSuchMessageException, SystemException {
79          Session session = null;
80  
81          try {
82              session = openSession();
83  
84              MBMessage mbMessage = (MBMessage)session.get(MBMessageImpl.class,
85                      new Long(messageId));
86  
87              if (mbMessage == null) {
88                  if (_log.isWarnEnabled()) {
89                      _log.warn("No MBMessage exists with the primary key " +
90                          messageId);
91                  }
92  
93                  throw new NoSuchMessageException(
94                      "No MBMessage exists with the primary key " + messageId);
95              }
96  
97              return remove(mbMessage);
98          }
99          catch (NoSuchMessageException nsee) {
100             throw nsee;
101         }
102         catch (Exception e) {
103             throw processException(e);
104         }
105         finally {
106             closeSession(session);
107         }
108     }
109 
110     public MBMessage remove(MBMessage mbMessage) throws SystemException {
111         if (_listeners.length > 0) {
112             for (ModelListener listener : _listeners) {
113                 listener.onBeforeRemove(mbMessage);
114             }
115         }
116 
117         mbMessage = removeImpl(mbMessage);
118 
119         if (_listeners.length > 0) {
120             for (ModelListener listener : _listeners) {
121                 listener.onAfterRemove(mbMessage);
122             }
123         }
124 
125         return mbMessage;
126     }
127 
128     protected MBMessage removeImpl(MBMessage mbMessage)
129         throws SystemException {
130         Session session = null;
131 
132         try {
133             session = openSession();
134 
135             session.delete(mbMessage);
136 
137             session.flush();
138 
139             return mbMessage;
140         }
141         catch (Exception e) {
142             throw processException(e);
143         }
144         finally {
145             closeSession(session);
146 
147             FinderCacheUtil.clearCache(MBMessage.class.getName());
148         }
149     }
150 
151     /**
152      * @deprecated Use <code>update(MBMessage mbMessage, boolean merge)</code>.
153      */
154     public MBMessage update(MBMessage mbMessage) throws SystemException {
155         if (_log.isWarnEnabled()) {
156             _log.warn(
157                 "Using the deprecated update(MBMessage mbMessage) method. Use update(MBMessage mbMessage, boolean merge) instead.");
158         }
159 
160         return update(mbMessage, false);
161     }
162 
163     /**
164      * Add, update, or merge, the entity. This method also calls the model
165      * listeners to trigger the proper events associated with adding, deleting,
166      * or updating an entity.
167      *
168      * @param        mbMessage the entity to add, update, or merge
169      * @param        merge boolean value for whether to merge the entity. The
170      *                default value is false. Setting merge to true is more
171      *                expensive and should only be true when mbMessage is
172      *                transient. See LEP-5473 for a detailed discussion of this
173      *                method.
174      * @return        true if the portlet can be displayed via Ajax
175      */
176     public MBMessage update(MBMessage mbMessage, boolean merge)
177         throws SystemException {
178         boolean isNew = mbMessage.isNew();
179 
180         if (_listeners.length > 0) {
181             for (ModelListener listener : _listeners) {
182                 if (isNew) {
183                     listener.onBeforeCreate(mbMessage);
184                 }
185                 else {
186                     listener.onBeforeUpdate(mbMessage);
187                 }
188             }
189         }
190 
191         mbMessage = updateImpl(mbMessage, merge);
192 
193         if (_listeners.length > 0) {
194             for (ModelListener listener : _listeners) {
195                 if (isNew) {
196                     listener.onAfterCreate(mbMessage);
197                 }
198                 else {
199                     listener.onAfterUpdate(mbMessage);
200                 }
201             }
202         }
203 
204         return mbMessage;
205     }
206 
207     public MBMessage updateImpl(
208         com.liferay.portlet.messageboards.model.MBMessage mbMessage,
209         boolean merge) throws SystemException {
210         if (Validator.isNull(mbMessage.getUuid())) {
211             String uuid = PortalUUIDUtil.generate();
212 
213             mbMessage.setUuid(uuid);
214         }
215 
216         Session session = null;
217 
218         try {
219             session = openSession();
220 
221             if (merge) {
222                 session.merge(mbMessage);
223             }
224             else {
225                 if (mbMessage.isNew()) {
226                     session.save(mbMessage);
227                 }
228             }
229 
230             session.flush();
231 
232             mbMessage.setNew(false);
233 
234             return mbMessage;
235         }
236         catch (Exception e) {
237             throw processException(e);
238         }
239         finally {
240             closeSession(session);
241 
242             FinderCacheUtil.clearCache(MBMessage.class.getName());
243         }
244     }
245 
246     public MBMessage findByPrimaryKey(long messageId)
247         throws NoSuchMessageException, SystemException {
248         MBMessage mbMessage = fetchByPrimaryKey(messageId);
249 
250         if (mbMessage == null) {
251             if (_log.isWarnEnabled()) {
252                 _log.warn("No MBMessage exists with the primary key " +
253                     messageId);
254             }
255 
256             throw new NoSuchMessageException(
257                 "No MBMessage exists with the primary key " + messageId);
258         }
259 
260         return mbMessage;
261     }
262 
263     public MBMessage fetchByPrimaryKey(long messageId)
264         throws SystemException {
265         Session session = null;
266 
267         try {
268             session = openSession();
269 
270             return (MBMessage)session.get(MBMessageImpl.class,
271                 new Long(messageId));
272         }
273         catch (Exception e) {
274             throw processException(e);
275         }
276         finally {
277             closeSession(session);
278         }
279     }
280 
281     public List<MBMessage> findByUuid(String uuid) throws SystemException {
282         boolean finderClassNameCacheEnabled = MBMessageModelImpl.CACHE_ENABLED;
283         String finderClassName = MBMessage.class.getName();
284         String finderMethodName = "findByUuid";
285         String[] finderParams = new String[] { String.class.getName() };
286         Object[] finderArgs = new Object[] { uuid };
287 
288         Object result = null;
289 
290         if (finderClassNameCacheEnabled) {
291             result = FinderCacheUtil.getResult(finderClassName,
292                     finderMethodName, finderParams, finderArgs, this);
293         }
294 
295         if (result == null) {
296             Session session = null;
297 
298             try {
299                 session = openSession();
300 
301                 StringBuilder query = new StringBuilder();
302 
303                 query.append(
304                     "FROM com.liferay.portlet.messageboards.model.MBMessage WHERE ");
305 
306                 if (uuid == null) {
307                     query.append("uuid_ IS NULL");
308                 }
309                 else {
310                     query.append("uuid_ = ?");
311                 }
312 
313                 query.append(" ");
314 
315                 query.append("ORDER BY ");
316 
317                 query.append("createDate ASC, ");
318                 query.append("messageId ASC");
319 
320                 Query q = session.createQuery(query.toString());
321 
322                 QueryPos qPos = QueryPos.getInstance(q);
323 
324                 if (uuid != null) {
325                     qPos.add(uuid);
326                 }
327 
328                 List<MBMessage> list = q.list();
329 
330                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
331                     finderClassName, finderMethodName, finderParams,
332                     finderArgs, list);
333 
334                 return list;
335             }
336             catch (Exception e) {
337                 throw processException(e);
338             }
339             finally {
340                 closeSession(session);
341             }
342         }
343         else {
344             return (List<MBMessage>)result;
345         }
346     }
347 
348     public List<MBMessage> findByUuid(String uuid, int start, int end)
349         throws SystemException {
350         return findByUuid(uuid, start, end, null);
351     }
352 
353     public List<MBMessage> findByUuid(String uuid, int start, int end,
354         OrderByComparator obc) throws SystemException {
355         boolean finderClassNameCacheEnabled = MBMessageModelImpl.CACHE_ENABLED;
356         String finderClassName = MBMessage.class.getName();
357         String finderMethodName = "findByUuid";
358         String[] finderParams = new String[] {
359                 String.class.getName(),
360                 
361                 "java.lang.Integer", "java.lang.Integer",
362                 "com.liferay.portal.kernel.util.OrderByComparator"
363             };
364         Object[] finderArgs = new Object[] {
365                 uuid,
366                 
367                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
368             };
369 
370         Object result = null;
371 
372         if (finderClassNameCacheEnabled) {
373             result = FinderCacheUtil.getResult(finderClassName,
374                     finderMethodName, finderParams, finderArgs, this);
375         }
376 
377         if (result == null) {
378             Session session = null;
379 
380             try {
381                 session = openSession();
382 
383                 StringBuilder query = new StringBuilder();
384 
385                 query.append(
386                     "FROM com.liferay.portlet.messageboards.model.MBMessage WHERE ");
387 
388                 if (uuid == null) {
389                     query.append("uuid_ IS NULL");
390                 }
391                 else {
392                     query.append("uuid_ = ?");
393                 }
394 
395                 query.append(" ");
396 
397                 if (obc != null) {
398                     query.append("ORDER BY ");
399                     query.append(obc.getOrderBy());
400                 }
401 
402                 else {
403                     query.append("ORDER BY ");
404 
405                     query.append("createDate ASC, ");
406                     query.append("messageId ASC");
407                 }
408 
409                 Query q = session.createQuery(query.toString());
410 
411                 QueryPos qPos = QueryPos.getInstance(q);
412 
413                 if (uuid != null) {
414                     qPos.add(uuid);
415                 }
416 
417                 List<MBMessage> list = (List<MBMessage>)QueryUtil.list(q,
418                         getDialect(), start, end);
419 
420                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
421                     finderClassName, finderMethodName, finderParams,
422                     finderArgs, list);
423 
424                 return list;
425             }
426             catch (Exception e) {
427                 throw processException(e);
428             }
429             finally {
430                 closeSession(session);
431             }
432         }
433         else {
434             return (List<MBMessage>)result;
435         }
436     }
437 
438     public MBMessage findByUuid_First(String uuid, OrderByComparator obc)
439         throws NoSuchMessageException, SystemException {
440         List<MBMessage> list = findByUuid(uuid, 0, 1, obc);
441 
442         if (list.size() == 0) {
443             StringBuilder msg = new StringBuilder();
444 
445             msg.append("No MBMessage exists with the key {");
446 
447             msg.append("uuid=" + uuid);
448 
449             msg.append(StringPool.CLOSE_CURLY_BRACE);
450 
451             throw new NoSuchMessageException(msg.toString());
452         }
453         else {
454             return list.get(0);
455         }
456     }
457 
458     public MBMessage findByUuid_Last(String uuid, OrderByComparator obc)
459         throws NoSuchMessageException, SystemException {
460         int count = countByUuid(uuid);
461 
462         List<MBMessage> list = findByUuid(uuid, count - 1, count, obc);
463 
464         if (list.size() == 0) {
465             StringBuilder msg = new StringBuilder();
466 
467             msg.append("No MBMessage exists with the key {");
468 
469             msg.append("uuid=" + uuid);
470 
471             msg.append(StringPool.CLOSE_CURLY_BRACE);
472 
473             throw new NoSuchMessageException(msg.toString());
474         }
475         else {
476             return list.get(0);
477         }
478     }
479 
480     public MBMessage[] findByUuid_PrevAndNext(long messageId, String uuid,
481         OrderByComparator obc) throws NoSuchMessageException, SystemException {
482         MBMessage mbMessage = findByPrimaryKey(messageId);
483 
484         int count = countByUuid(uuid);
485 
486         Session session = null;
487 
488         try {
489             session = openSession();
490 
491             StringBuilder query = new StringBuilder();
492 
493             query.append(
494                 "FROM com.liferay.portlet.messageboards.model.MBMessage WHERE ");
495 
496             if (uuid == null) {
497                 query.append("uuid_ IS NULL");
498             }
499             else {
500                 query.append("uuid_ = ?");
501             }
502 
503             query.append(" ");
504 
505             if (obc != null) {
506                 query.append("ORDER BY ");
507                 query.append(obc.getOrderBy());
508             }
509 
510             else {
511                 query.append("ORDER BY ");
512 
513                 query.append("createDate ASC, ");
514                 query.append("messageId ASC");
515             }
516 
517             Query q = session.createQuery(query.toString());
518 
519             QueryPos qPos = QueryPos.getInstance(q);
520 
521             if (uuid != null) {
522                 qPos.add(uuid);
523             }
524 
525             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
526                     mbMessage);
527 
528             MBMessage[] array = new MBMessageImpl[3];
529 
530             array[0] = (MBMessage)objArray[0];
531             array[1] = (MBMessage)objArray[1];
532             array[2] = (MBMessage)objArray[2];
533 
534             return array;
535         }
536         catch (Exception e) {
537             throw processException(e);
538         }
539         finally {
540             closeSession(session);
541         }
542     }
543 
544     public List<MBMessage> findByCompanyId(long companyId)
545         throws SystemException {
546         boolean finderClassNameCacheEnabled = MBMessageModelImpl.CACHE_ENABLED;
547         String finderClassName = MBMessage.class.getName();
548         String finderMethodName = "findByCompanyId";
549         String[] finderParams = new String[] { Long.class.getName() };
550         Object[] finderArgs = new Object[] { new Long(companyId) };
551 
552         Object result = null;
553 
554         if (finderClassNameCacheEnabled) {
555             result = FinderCacheUtil.getResult(finderClassName,
556                     finderMethodName, finderParams, finderArgs, this);
557         }
558 
559         if (result == null) {
560             Session session = null;
561 
562             try {
563                 session = openSession();
564 
565                 StringBuilder query = new StringBuilder();
566 
567                 query.append(
568                     "FROM com.liferay.portlet.messageboards.model.MBMessage WHERE ");
569 
570                 query.append("companyId = ?");
571 
572                 query.append(" ");
573 
574                 query.append("ORDER BY ");
575 
576                 query.append("createDate ASC, ");
577                 query.append("messageId ASC");
578 
579                 Query q = session.createQuery(query.toString());
580 
581                 QueryPos qPos = QueryPos.getInstance(q);
582 
583                 qPos.add(companyId);
584 
585                 List<MBMessage> list = q.list();
586 
587                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
588                     finderClassName, finderMethodName, finderParams,
589                     finderArgs, list);
590 
591                 return list;
592             }
593             catch (Exception e) {
594                 throw processException(e);
595             }
596             finally {
597                 closeSession(session);
598             }
599         }
600         else {
601             return (List<MBMessage>)result;
602         }
603     }
604 
605     public List<MBMessage> findByCompanyId(long companyId, int start, int end)
606         throws SystemException {
607         return findByCompanyId(companyId, start, end, null);
608     }
609 
610     public List<MBMessage> findByCompanyId(long companyId, int start, int end,
611         OrderByComparator obc) throws SystemException {
612         boolean finderClassNameCacheEnabled = MBMessageModelImpl.CACHE_ENABLED;
613         String finderClassName = MBMessage.class.getName();
614         String finderMethodName = "findByCompanyId";
615         String[] finderParams = new String[] {
616                 Long.class.getName(),
617                 
618                 "java.lang.Integer", "java.lang.Integer",
619                 "com.liferay.portal.kernel.util.OrderByComparator"
620             };
621         Object[] finderArgs = new Object[] {
622                 new Long(companyId),
623                 
624                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
625             };
626 
627         Object result = null;
628 
629         if (finderClassNameCacheEnabled) {
630             result = FinderCacheUtil.getResult(finderClassName,
631                     finderMethodName, finderParams, finderArgs, this);
632         }
633 
634         if (result == null) {
635             Session session = null;
636 
637             try {
638                 session = openSession();
639 
640                 StringBuilder query = new StringBuilder();
641 
642                 query.append(
643                     "FROM com.liferay.portlet.messageboards.model.MBMessage WHERE ");
644 
645                 query.append("companyId = ?");
646 
647                 query.append(" ");
648 
649                 if (obc != null) {
650                     query.append("ORDER BY ");
651                     query.append(obc.getOrderBy());
652                 }
653 
654                 else {
655                     query.append("ORDER BY ");
656 
657                     query.append("createDate ASC, ");
658                     query.append("messageId ASC");
659                 }
660 
661                 Query q = session.createQuery(query.toString());
662 
663                 QueryPos qPos = QueryPos.getInstance(q);
664 
665                 qPos.add(companyId);
666 
667                 List<MBMessage> list = (List<MBMessage>)QueryUtil.list(q,
668                         getDialect(), start, end);
669 
670                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
671                     finderClassName, finderMethodName, finderParams,
672                     finderArgs, list);
673 
674                 return list;
675             }
676             catch (Exception e) {
677                 throw processException(e);
678             }
679             finally {
680                 closeSession(session);
681             }
682         }
683         else {
684             return (List<MBMessage>)result;
685         }
686     }
687 
688     public MBMessage findByCompanyId_First(long companyId, OrderByComparator obc)
689         throws NoSuchMessageException, SystemException {
690         List<MBMessage> list = findByCompanyId(companyId, 0, 1, obc);
691 
692         if (list.size() == 0) {
693             StringBuilder msg = new StringBuilder();
694 
695             msg.append("No MBMessage exists with the key {");
696 
697             msg.append("companyId=" + companyId);
698 
699             msg.append(StringPool.CLOSE_CURLY_BRACE);
700 
701             throw new NoSuchMessageException(msg.toString());
702         }
703         else {
704             return list.get(0);
705         }
706     }
707 
708     public MBMessage findByCompanyId_Last(long companyId, OrderByComparator obc)
709         throws NoSuchMessageException, SystemException {
710         int count = countByCompanyId(companyId);
711 
712         List<MBMessage> list = findByCompanyId(companyId, count - 1, count, obc);
713 
714         if (list.size() == 0) {
715             StringBuilder msg = new StringBuilder();
716 
717             msg.append("No MBMessage exists with the key {");
718 
719             msg.append("companyId=" + companyId);
720 
721             msg.append(StringPool.CLOSE_CURLY_BRACE);
722 
723             throw new NoSuchMessageException(msg.toString());
724         }
725         else {
726             return list.get(0);
727         }
728     }
729 
730     public MBMessage[] findByCompanyId_PrevAndNext(long messageId,
731         long companyId, OrderByComparator obc)
732         throws NoSuchMessageException, SystemException {
733         MBMessage mbMessage = findByPrimaryKey(messageId);
734 
735         int count = countByCompanyId(companyId);
736 
737         Session session = null;
738 
739         try {
740             session = openSession();
741 
742             StringBuilder query = new StringBuilder();
743 
744             query.append(
745                 "FROM com.liferay.portlet.messageboards.model.MBMessage WHERE ");
746 
747             query.append("companyId = ?");
748 
749             query.append(" ");
750 
751             if (obc != null) {
752                 query.append("ORDER BY ");
753                 query.append(obc.getOrderBy());
754             }
755 
756             else {
757                 query.append("ORDER BY ");
758 
759                 query.append("createDate ASC, ");
760                 query.append("messageId ASC");
761             }
762 
763             Query q = session.createQuery(query.toString());
764 
765             QueryPos qPos = QueryPos.getInstance(q);
766 
767             qPos.add(companyId);
768 
769             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
770                     mbMessage);
771 
772             MBMessage[] array = new MBMessageImpl[3];
773 
774             array[0] = (MBMessage)objArray[0];
775             array[1] = (MBMessage)objArray[1];
776             array[2] = (MBMessage)objArray[2];
777 
778             return array;
779         }
780         catch (Exception e) {
781             throw processException(e);
782         }
783         finally {
784             closeSession(session);
785         }
786     }
787 
788     public List<MBMessage> findByCategoryId(long categoryId)
789         throws SystemException {
790         boolean finderClassNameCacheEnabled = MBMessageModelImpl.CACHE_ENABLED;
791         String finderClassName = MBMessage.class.getName();
792         String finderMethodName = "findByCategoryId";
793         String[] finderParams = new String[] { Long.class.getName() };
794         Object[] finderArgs = new Object[] { new Long(categoryId) };
795 
796         Object result = null;
797 
798         if (finderClassNameCacheEnabled) {
799             result = FinderCacheUtil.getResult(finderClassName,
800                     finderMethodName, finderParams, finderArgs, this);
801         }
802 
803         if (result == null) {
804             Session session = null;
805 
806             try {
807                 session = openSession();
808 
809                 StringBuilder query = new StringBuilder();
810 
811                 query.append(
812                     "FROM com.liferay.portlet.messageboards.model.MBMessage WHERE ");
813 
814                 query.append("categoryId = ?");
815 
816                 query.append(" ");
817 
818                 query.append("ORDER BY ");
819 
820                 query.append("createDate ASC, ");
821                 query.append("messageId ASC");
822 
823                 Query q = session.createQuery(query.toString());
824 
825                 QueryPos qPos = QueryPos.getInstance(q);
826 
827                 qPos.add(categoryId);
828 
829                 List<MBMessage> list = q.list();
830 
831                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
832                     finderClassName, finderMethodName, finderParams,
833                     finderArgs, list);
834 
835                 return list;
836             }
837             catch (Exception e) {
838                 throw processException(e);
839             }
840             finally {
841                 closeSession(session);
842             }
843         }
844         else {
845             return (List<MBMessage>)result;
846         }
847     }
848 
849     public List<MBMessage> findByCategoryId(long categoryId, int start, int end)
850         throws SystemException {
851         return findByCategoryId(categoryId, start, end, null);
852     }
853 
854     public List<MBMessage> findByCategoryId(long categoryId, int start,
855         int end, OrderByComparator obc) throws SystemException {
856         boolean finderClassNameCacheEnabled = MBMessageModelImpl.CACHE_ENABLED;
857         String finderClassName = MBMessage.class.getName();
858         String finderMethodName = "findByCategoryId";
859         String[] finderParams = new String[] {
860                 Long.class.getName(),
861                 
862                 "java.lang.Integer", "java.lang.Integer",
863                 "com.liferay.portal.kernel.util.OrderByComparator"
864             };
865         Object[] finderArgs = new Object[] {
866                 new Long(categoryId),
867                 
868                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
869             };
870 
871         Object result = null;
872 
873         if (finderClassNameCacheEnabled) {
874             result = FinderCacheUtil.getResult(finderClassName,
875                     finderMethodName, finderParams, finderArgs, this);
876         }
877 
878         if (result == null) {
879             Session session = null;
880 
881             try {
882                 session = openSession();
883 
884                 StringBuilder query = new StringBuilder();
885 
886                 query.append(
887                     "FROM com.liferay.portlet.messageboards.model.MBMessage WHERE ");
888 
889                 query.append("categoryId = ?");
890 
891                 query.append(" ");
892 
893                 if (obc != null) {
894                     query.append("ORDER BY ");
895                     query.append(obc.getOrderBy());
896                 }
897 
898                 else {
899                     query.append("ORDER BY ");
900 
901                     query.append("createDate ASC, ");
902                     query.append("messageId ASC");
903                 }
904 
905                 Query q = session.createQuery(query.toString());
906 
907                 QueryPos qPos = QueryPos.getInstance(q);
908 
909                 qPos.add(categoryId);
910 
911                 List<MBMessage> list = (List<MBMessage>)QueryUtil.list(q,
912                         getDialect(), start, end);
913 
914                 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
915                     finderClassName, finderMethodName, finderParams,
916                     finderArgs, list);
917 
918                 return list;
919             }
920             catch (Exception e) {
921                 throw processException(e);
922             }
923             finally {
924                 closeSession(session);
925             }
926         }
927         else {
928             return (List<MBMessage>)result;
929         }
930     }
931 
932     public MBMessage findByCategoryId_First(long categoryId,
933         OrderByComparator obc) throws NoSuchMessageException, SystemException {
934         List<MBMessage> list = findByCategoryId(categoryId, 0, 1, obc);
935 
936         if (list.size() == 0) {
937             StringBuilder msg = new StringBuilder();
938 
939             msg.append("No MBMessage exists with the key {");
940 
941             msg.append("categoryId=" + categoryId);
942 
943             msg.append(StringPool.CLOSE_CURLY_BRACE);
944 
945             throw new NoSuchMessageException(msg.toString());
946         }
947         else {
948             return list.get(0);
949         }
950     }
951 
952     public MBMessage findByCategoryId_Last(long categoryId,
953         OrderByComparator obc) throws NoSuchMessageException, SystemException {
954         int count = countByCategoryId(categoryId);
955 
956         List<MBMessage> list = findByCategoryId(categoryId, count - 1, count,
957                 obc);
958 
959         if (list.size() == 0) {
960             StringBuilder msg = new StringBuilder();
961 
962             msg.append("No MBMessage exists with the key {");
963 
964             msg.append("categoryId=" + categoryId);
965 
966             msg.append(StringPool.CLOSE_CURLY_BRACE);
967 
968             throw new NoSuchMessageException(msg.toString());
969         }
970         else {
971             return list.get(0);
972         }
973     }
974 
975     public MBMessage[] findByCategoryId_PrevAndNext(long messageId,
976         long categoryId, OrderByComparator obc)
977         throws NoSuchMessageException, SystemException {
978         MBMessage mbMessage = findByPrimaryKey(messageId);
979 
980         int count = countByCategoryId(categoryId);
981 
982         Session session = null;
983 
984         try {
985             session = openSession();
986 
987             StringBuilder query = new StringBuilder();
988 
989             query.append(
990                 "FROM com.liferay.portlet.messageboards.model.MBMessage WHERE ");
991 
992             query.append("categoryId = ?");
993 
994             query.append(" ");
995 
996             if (obc != null) {
997                 query.append("ORDER BY ");
998                 query.append(obc.getOrderBy());
999             }
1000
1001            else {
1002                query.append("ORDER BY ");
1003
1004                query.append("createDate ASC, ");
1005                query.append("messageId ASC");
1006            }
1007
1008            Query q = session.createQuery(query.toString());
1009
1010            QueryPos qPos = QueryPos.getInstance(q);
1011
1012            qPos.add(categoryId);
1013
1014            Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
1015                    mbMessage);
1016
1017            MBMessage[] array = new MBMessageImpl[3];
1018
1019            array[0] = (MBMessage)objArray[0];
1020            array[1] = (MBMessage)objArray[1];
1021            array[2] = (MBMessage)objArray[2];
1022
1023            return array;
1024        }
1025        catch (Exception e) {
1026            throw processException(e);
1027        }
1028        finally {
1029            closeSession(session);
1030        }
1031    }
1032
1033    public List<MBMessage> findByThreadId(long threadId)
1034        throws SystemException {
1035        boolean finderClassNameCacheEnabled = MBMessageModelImpl.CACHE_ENABLED;
1036        String finderClassName = MBMessage.class.getName();
1037        String finderMethodName = "findByThreadId";
1038        String[] finderParams = new String[] { Long.class.getName() };
1039        Object[] finderArgs = new Object[] { new Long(threadId) };
1040
1041        Object result = null;
1042
1043        if (finderClassNameCacheEnabled) {
1044            result = FinderCacheUtil.getResult(finderClassName,
1045                    finderMethodName, finderParams, finderArgs, this);
1046        }
1047
1048        if (result == null) {
1049            Session session = null;
1050
1051            try {
1052                session = openSession();
1053
1054                StringBuilder query = new StringBuilder();
1055
1056                query.append(
1057                    "FROM com.liferay.portlet.messageboards.model.MBMessage WHERE ");
1058
1059                query.append("threadId = ?");
1060
1061                query.append(" ");
1062
1063                query.append("ORDER BY ");
1064
1065                query.append("createDate ASC, ");
1066                query.append("messageId ASC");
1067
1068                Query q = session.createQuery(query.toString());
1069
1070                QueryPos qPos = QueryPos.getInstance(q);
1071
1072                qPos.add(threadId);
1073
1074                List<MBMessage> list = q.list();
1075
1076                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1077                    finderClassName, finderMethodName, finderParams,
1078                    finderArgs, list);
1079
1080                return list;
1081            }
1082            catch (Exception e) {
1083                throw processException(e);
1084            }
1085            finally {
1086                closeSession(session);
1087            }
1088        }
1089        else {
1090            return (List<MBMessage>)result;
1091        }
1092    }
1093
1094    public List<MBMessage> findByThreadId(long threadId, int start, int end)
1095        throws SystemException {
1096        return findByThreadId(threadId, start, end, null);
1097    }
1098
1099    public List<MBMessage> findByThreadId(long threadId, int start, int end,
1100        OrderByComparator obc) throws SystemException {
1101        boolean finderClassNameCacheEnabled = MBMessageModelImpl.CACHE_ENABLED;
1102        String finderClassName = MBMessage.class.getName();
1103        String finderMethodName = "findByThreadId";
1104        String[] finderParams = new String[] {
1105                Long.class.getName(),
1106                
1107                "java.lang.Integer", "java.lang.Integer",
1108                "com.liferay.portal.kernel.util.OrderByComparator"
1109            };
1110        Object[] finderArgs = new Object[] {
1111                new Long(threadId),
1112                
1113                String.valueOf(start), String.valueOf(end), String.valueOf(obc)
1114            };
1115
1116        Object result = null;
1117
1118        if (finderClassNameCacheEnabled) {
1119            result = FinderCacheUtil.getResult(finderClassName,
1120                    finderMethodName, finderParams, finderArgs, this);
1121        }
1122
1123        if (result == null) {
1124            Session session = null;
1125
1126            try {
1127                session = openSession();
1128
1129                StringBuilder query = new StringBuilder();
1130
1131                query.append(
1132                    "FROM com.liferay.portlet.messageboards.model.MBMessage WHERE ");
1133
1134                query.append("threadId = ?");
1135
1136                query.append(" ");
1137
1138                if (obc != null) {
1139                    query.append("ORDER BY ");
1140                    query.append(obc.getOrderBy());
1141                }
1142
1143                else {
1144                    query.append("ORDER BY ");
1145
1146                    query.append("createDate ASC, ");
1147                    query.append("messageId ASC");
1148                }
1149
1150                Query q = session.createQuery(query.toString());
1151
1152                QueryPos qPos = QueryPos.getInstance(q);
1153
1154                qPos.add(threadId);
1155
1156                List<MBMessage> list = (List<MBMessage>)QueryUtil.list(q,
1157                        getDialect(), start, end);
1158
1159                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1160                    finderClassName, finderMethodName, finderParams,
1161                    finderArgs, list);
1162
1163                return list;
1164            }
1165            catch (Exception e) {
1166                throw processException(e);
1167            }
1168            finally {
1169                closeSession(session);
1170            }
1171        }
1172        else {
1173            return (List<MBMessage>)result;
1174        }
1175    }
1176
1177    public MBMessage findByThreadId_First(long threadId, OrderByComparator obc)
1178        throws NoSuchMessageException, SystemException {
1179        List<MBMessage> list = findByThreadId(threadId, 0, 1, obc);
1180
1181        if (list.size() == 0) {
1182            StringBuilder msg = new StringBuilder();
1183
1184            msg.append("No MBMessage exists with the key {");
1185
1186            msg.append("threadId=" + threadId);
1187
1188            msg.append(StringPool.CLOSE_CURLY_BRACE);
1189
1190            throw new NoSuchMessageException(msg.toString());
1191        }
1192        else {
1193            return list.get(0);
1194        }
1195    }
1196
1197    public MBMessage findByThreadId_Last(long threadId, OrderByComparator obc)
1198        throws NoSuchMessageException, SystemException {
1199        int count = countByThreadId(threadId);
1200
1201        List<MBMessage> list = findByThreadId(threadId, count - 1, count, obc);
1202
1203        if (list.size() == 0) {
1204            StringBuilder msg = new StringBuilder();
1205
1206            msg.append("No MBMessage exists with the key {");
1207
1208            msg.append("threadId=" + threadId);
1209
1210            msg.append(StringPool.CLOSE_CURLY_BRACE);
1211
1212            throw new NoSuchMessageException(msg.toString());
1213        }
1214        else {
1215            return list.get(0);
1216        }
1217    }
1218
1219    public MBMessage[] findByThreadId_PrevAndNext(long messageId,
1220        long threadId, OrderByComparator obc)
1221        throws NoSuchMessageException, SystemException {
1222        MBMessage mbMessage = findByPrimaryKey(messageId);
1223
1224        int count = countByThreadId(threadId);
1225
1226        Session session = null;
1227
1228        try {
1229            session = openSession();
1230
1231            StringBuilder query = new StringBuilder();
1232
1233            query.append(
1234                "FROM com.liferay.portlet.messageboards.model.MBMessage WHERE ");
1235
1236            query.append("threadId = ?");
1237
1238            query.append(" ");
1239
1240            if (obc != null) {
1241                query.append("ORDER BY ");
1242                query.append(obc.getOrderBy());
1243            }
1244
1245            else {
1246                query.append("ORDER BY ");
1247
1248                query.append("createDate ASC, ");
1249                query.append("messageId ASC");
1250            }
1251
1252            Query q = session.createQuery(query.toString());
1253
1254            QueryPos qPos = QueryPos.getInstance(q);
1255
1256            qPos.add(threadId);
1257
1258            Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
1259                    mbMessage);
1260
1261            MBMessage[] array = new MBMessageImpl[3];
1262
1263            array[0] = (MBMessage)objArray[0];
1264            array[1] = (MBMessage)objArray[1];
1265            array[2] = (MBMessage)objArray[2];
1266
1267            return array;
1268        }
1269        catch (Exception e) {
1270            throw processException(e);
1271        }
1272        finally {
1273            closeSession(session);
1274        }
1275    }
1276
1277    public List<MBMessage> findByC_T(long categoryId, long threadId)
1278        throws SystemException {
1279        boolean finderClassNameCacheEnabled = MBMessageModelImpl.CACHE_ENABLED;
1280        String finderClassName = MBMessage.class.getName();
1281        String finderMethodName = "findByC_T";
1282        String[] finderParams = new String[] {
1283                Long.class.getName(), Long.class.getName()
1284            };
1285        Object[] finderArgs = new Object[] {
1286                new Long(categoryId), new Long(threadId)
1287            };
1288
1289        Object result = null;
1290
1291        if (finderClassNameCacheEnabled) {
1292            result = FinderCacheUtil.getResult(finderClassName,
1293                    finderMethodName, finderParams, finderArgs, this);
1294        }
1295
1296        if (result == null) {
1297            Session session = null;
1298
1299            try {
1300                session = openSession();
1301
1302                StringBuilder query = new StringBuilder();
1303
1304                query.append(
1305                    "FROM com.liferay.portlet.messageboards.model.MBMessage WHERE ");
1306
1307                query.append("categoryId = ?");
1308
1309                query.append(" AND ");
1310
1311                query.append("threadId = ?");
1312
1313                query.append(" ");
1314
1315                query.append("ORDER BY ");
1316
1317                query.append("createDate ASC, ");
1318                query.append("messageId ASC");
1319
1320                Query q = session.createQuery(query.toString());
1321
1322                QueryPos qPos = QueryPos.getInstance(q);
1323
1324                qPos.add(categoryId);
1325
1326                qPos.add(threadId);
1327
1328                List<MBMessage> list = q.list();
1329
1330                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1331                    finderClassName, finderMethodName, finderParams,
1332                    finderArgs, list);
1333
1334                return list;
1335            }
1336            catch (Exception e) {
1337                throw processException(e);
1338            }
1339            finally {
1340                closeSession(session);
1341            }
1342        }
1343        else {
1344            return (List<MBMessage>)result;
1345        }
1346    }
1347
1348    public List<MBMessage> findByC_T(long categoryId, long threadId, int start,
1349        int end) throws SystemException {
1350        return findByC_T(categoryId, threadId, start, end, null);
1351    }
1352
1353    public List<MBMessage> findByC_T(long categoryId, long threadId, int start,
1354        int end, OrderByComparator obc) throws SystemException {
1355        boolean finderClassNameCacheEnabled = MBMessageModelImpl.CACHE_ENABLED;
1356        String finderClassName = MBMessage.class.getName();
1357        String finderMethodName = "findByC_T";
1358        String[] finderParams = new String[] {
1359                Long.class.getName(), Long.class.getName(),
1360                
1361                "java.lang.Integer", "java.lang.Integer",
1362                "com.liferay.portal.kernel.util.OrderByComparator"
1363            };
1364        Object[] finderArgs = new Object[] {
1365                new Long(categoryId), new Long(threadId),
1366                
1367                String.valueOf(start), String.valueOf(end), String.valueOf(obc)
1368            };
1369
1370        Object result = null;
1371
1372        if (finderClassNameCacheEnabled) {
1373            result = FinderCacheUtil.getResult(finderClassName,
1374                    finderMethodName, finderParams, finderArgs, this);
1375        }
1376
1377        if (result == null) {
1378            Session session = null;
1379
1380            try {
1381                session = openSession();
1382
1383                StringBuilder query = new StringBuilder();
1384
1385                query.append(
1386                    "FROM com.liferay.portlet.messageboards.model.MBMessage WHERE ");
1387
1388                query.append("categoryId = ?");
1389
1390                query.append(" AND ");
1391
1392                query.append("threadId = ?");
1393
1394                query.append(" ");
1395
1396                if (obc != null) {
1397                    query.append("ORDER BY ");
1398                    query.append(obc.getOrderBy());
1399                }
1400
1401                else {
1402                    query.append("ORDER BY ");
1403
1404                    query.append("createDate ASC, ");
1405                    query.append("messageId ASC");
1406                }
1407
1408                Query q = session.createQuery(query.toString());
1409
1410                QueryPos qPos = QueryPos.getInstance(q);
1411
1412                qPos.add(categoryId);
1413
1414                qPos.add(threadId);
1415
1416                List<MBMessage> list = (List<MBMessage>)QueryUtil.list(q,
1417                        getDialect(), start, end);
1418
1419                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1420                    finderClassName, finderMethodName, finderParams,
1421                    finderArgs, list);
1422
1423                return list;
1424            }
1425            catch (Exception e) {
1426                throw processException(e);
1427            }
1428            finally {
1429                closeSession(session);
1430            }
1431        }
1432        else {
1433            return (List<MBMessage>)result;
1434        }
1435    }
1436
1437    public MBMessage findByC_T_First(long categoryId, long threadId,
1438        OrderByComparator obc) throws NoSuchMessageException, SystemException {
1439        List<MBMessage> list = findByC_T(categoryId, threadId, 0, 1, obc);
1440
1441        if (list.size() == 0) {
1442            StringBuilder msg = new StringBuilder();
1443
1444            msg.append("No MBMessage exists with the key {");
1445
1446            msg.append("categoryId=" + categoryId);
1447
1448            msg.append(", ");
1449            msg.append("threadId=" + threadId);
1450
1451            msg.append(StringPool.CLOSE_CURLY_BRACE);
1452
1453            throw new NoSuchMessageException(msg.toString());
1454        }
1455        else {
1456            return list.get(0);
1457        }
1458    }
1459
1460    public MBMessage findByC_T_Last(long categoryId, long threadId,
1461        OrderByComparator obc) throws NoSuchMessageException, SystemException {
1462        int count = countByC_T(categoryId, threadId);
1463
1464        List<MBMessage> list = findByC_T(categoryId, threadId, count - 1,
1465                count, obc);
1466
1467        if (list.size() == 0) {
1468            StringBuilder msg = new StringBuilder();
1469
1470            msg.append("No MBMessage exists with the key {");
1471
1472            msg.append("categoryId=" + categoryId);
1473
1474            msg.append(", ");
1475            msg.append("threadId=" + threadId);
1476
1477            msg.append(StringPool.CLOSE_CURLY_BRACE);
1478
1479            throw new NoSuchMessageException(msg.toString());
1480        }
1481        else {
1482            return list.get(0);
1483        }
1484    }
1485
1486    public MBMessage[] findByC_T_PrevAndNext(long messageId, long categoryId,
1487        long threadId, OrderByComparator obc)
1488        throws NoSuchMessageException, SystemException {
1489        MBMessage mbMessage = findByPrimaryKey(messageId);
1490
1491        int count = countByC_T(categoryId, threadId);
1492
1493        Session session = null;
1494
1495        try {
1496            session = openSession();
1497
1498            StringBuilder query = new StringBuilder();
1499
1500            query.append(
1501                "FROM com.liferay.portlet.messageboards.model.MBMessage WHERE ");
1502
1503            query.append("categoryId = ?");
1504
1505            query.append(" AND ");
1506
1507            query.append("threadId = ?");
1508
1509            query.append(" ");
1510
1511            if (obc != null) {
1512                query.append("ORDER BY ");
1513                query.append(obc.getOrderBy());
1514            }
1515
1516            else {
1517                query.append("ORDER BY ");
1518
1519                query.append("createDate ASC, ");
1520                query.append("messageId ASC");
1521            }
1522
1523            Query q = session.createQuery(query.toString());
1524
1525            QueryPos qPos = QueryPos.getInstance(q);
1526
1527            qPos.add(categoryId);
1528
1529            qPos.add(threadId);
1530
1531            Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
1532                    mbMessage);
1533
1534            MBMessage[] array = new MBMessageImpl[3];
1535
1536            array[0] = (MBMessage)objArray[0];
1537            array[1] = (MBMessage)objArray[1];
1538            array[2] = (MBMessage)objArray[2];
1539
1540            return array;
1541        }
1542        catch (Exception e) {
1543            throw processException(e);
1544        }
1545        finally {
1546            closeSession(session);
1547        }
1548    }
1549
1550    public List<MBMessage> findByT_P(long threadId, long parentMessageId)
1551        throws SystemException {
1552        boolean finderClassNameCacheEnabled = MBMessageModelImpl.CACHE_ENABLED;
1553        String finderClassName = MBMessage.class.getName();
1554        String finderMethodName = "findByT_P";
1555        String[] finderParams = new String[] {
1556                Long.class.getName(), Long.class.getName()
1557            };
1558        Object[] finderArgs = new Object[] {
1559                new Long(threadId), new Long(parentMessageId)
1560            };
1561
1562        Object result = null;
1563
1564        if (finderClassNameCacheEnabled) {
1565            result = FinderCacheUtil.getResult(finderClassName,
1566                    finderMethodName, finderParams, finderArgs, this);
1567        }
1568
1569        if (result == null) {
1570            Session session = null;
1571
1572            try {
1573                session = openSession();
1574
1575                StringBuilder query = new StringBuilder();
1576
1577                query.append(
1578                    "FROM com.liferay.portlet.messageboards.model.MBMessage WHERE ");
1579
1580                query.append("threadId = ?");
1581
1582                query.append(" AND ");
1583
1584                query.append("parentMessageId = ?");
1585
1586                query.append(" ");
1587
1588                query.append("ORDER BY ");
1589
1590                query.append("createDate ASC, ");
1591                query.append("messageId ASC");
1592
1593                Query q = session.createQuery(query.toString());
1594
1595                QueryPos qPos = QueryPos.getInstance(q);
1596
1597                qPos.add(threadId);
1598
1599                qPos.add(parentMessageId);
1600
1601                List<MBMessage> list = q.list();
1602
1603                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1604                    finderClassName, finderMethodName, finderParams,
1605                    finderArgs, list);
1606
1607                return list;
1608            }
1609            catch (Exception e) {
1610                throw processException(e);
1611            }
1612            finally {
1613                closeSession(session);
1614            }
1615        }
1616        else {
1617            return (List<MBMessage>)result;
1618        }
1619    }
1620
1621    public List<MBMessage> findByT_P(long threadId, long parentMessageId,
1622        int start, int end) throws SystemException {
1623        return findByT_P(threadId, parentMessageId, start, end, null);
1624    }
1625
1626    public List<MBMessage> findByT_P(long threadId, long parentMessageId,
1627        int start, int end, OrderByComparator obc) throws SystemException {
1628        boolean finderClassNameCacheEnabled = MBMessageModelImpl.CACHE_ENABLED;
1629        String finderClassName = MBMessage.class.getName();
1630        String finderMethodName = "findByT_P";
1631        String[] finderParams = new String[] {
1632                Long.class.getName(), Long.class.getName(),
1633                
1634                "java.lang.Integer", "java.lang.Integer",
1635                "com.liferay.portal.kernel.util.OrderByComparator"
1636            };
1637        Object[] finderArgs = new Object[] {
1638                new Long(threadId), new Long(parentMessageId),
1639                
1640                String.valueOf(start), String.valueOf(end), String.valueOf(obc)
1641            };
1642
1643        Object result = null;
1644
1645        if (finderClassNameCacheEnabled) {
1646            result = FinderCacheUtil.getResult(finderClassName,
1647                    finderMethodName, finderParams, finderArgs, this);
1648        }
1649
1650        if (result == null) {
1651            Session session = null;
1652
1653            try {
1654                session = openSession();
1655
1656                StringBuilder query = new StringBuilder();
1657
1658                query.append(
1659                    "FROM com.liferay.portlet.messageboards.model.MBMessage WHERE ");
1660
1661                query.append("threadId = ?");
1662
1663                query.append(" AND ");
1664
1665                query.append("parentMessageId = ?");
1666
1667                query.append(" ");
1668
1669                if (obc != null) {
1670                    query.append("ORDER BY ");
1671                    query.append(obc.getOrderBy());
1672                }
1673
1674                else {
1675                    query.append("ORDER BY ");
1676
1677                    query.append("createDate ASC, ");
1678                    query.append("messageId ASC");
1679                }
1680
1681                Query q = session.createQuery(query.toString());
1682
1683                QueryPos qPos = QueryPos.getInstance(q);
1684
1685                qPos.add(threadId);
1686
1687                qPos.add(parentMessageId);
1688
1689                List<MBMessage> list = (List<MBMessage>)QueryUtil.list(q,
1690                        getDialect(), start, end);
1691
1692                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1693                    finderClassName, finderMethodName, finderParams,
1694                    finderArgs, list);
1695
1696                return list;
1697            }
1698            catch (Exception e) {
1699                throw processException(e);
1700            }
1701            finally {
1702                closeSession(session);
1703            }
1704        }
1705        else {
1706            return (List<MBMessage>)result;
1707        }
1708    }
1709
1710    public MBMessage findByT_P_First(long threadId, long parentMessageId,
1711        OrderByComparator obc) throws NoSuchMessageException, SystemException {
1712        List<MBMessage> list = findByT_P(threadId, parentMessageId, 0, 1, obc);
1713
1714        if (list.size() == 0) {
1715            StringBuilder msg = new StringBuilder();
1716
1717            msg.append("No MBMessage exists with the key {");
1718
1719            msg.append("threadId=" + threadId);
1720
1721            msg.append(", ");
1722            msg.append("parentMessageId=" + parentMessageId);
1723
1724            msg.append(StringPool.CLOSE_CURLY_BRACE);
1725
1726            throw new NoSuchMessageException(msg.toString());
1727        }
1728        else {
1729            return list.get(0);
1730        }
1731    }
1732
1733    public MBMessage findByT_P_Last(long threadId, long parentMessageId,
1734        OrderByComparator obc) throws NoSuchMessageException, SystemException {
1735        int count = countByT_P(threadId, parentMessageId);
1736
1737        List<MBMessage> list = findByT_P(threadId, parentMessageId, count - 1,
1738                count, obc);
1739
1740        if (list.size() == 0) {
1741            StringBuilder msg = new StringBuilder();
1742
1743            msg.append("No MBMessage exists with the key {");
1744
1745            msg.append("threadId=" + threadId);
1746
1747            msg.append(", ");
1748            msg.append("parentMessageId=" + parentMessageId);
1749
1750            msg.append(StringPool.CLOSE_CURLY_BRACE);
1751
1752            throw new NoSuchMessageException(msg.toString());
1753        }
1754        else {
1755            return list.get(0);
1756        }
1757    }
1758
1759    public MBMessage[] findByT_P_PrevAndNext(long messageId, long threadId,
1760        long parentMessageId, OrderByComparator obc)
1761        throws NoSuchMessageException, SystemException {
1762        MBMessage mbMessage = findByPrimaryKey(messageId);
1763
1764        int count = countByT_P(threadId, parentMessageId);
1765
1766        Session session = null;
1767
1768        try {
1769            session = openSession();
1770
1771            StringBuilder query = new StringBuilder();
1772
1773            query.append(
1774                "FROM com.liferay.portlet.messageboards.model.MBMessage WHERE ");
1775
1776            query.append("threadId = ?");
1777
1778            query.append(" AND ");
1779
1780            query.append("parentMessageId = ?");
1781
1782            query.append(" ");
1783
1784            if (obc != null) {
1785                query.append("ORDER BY ");
1786                query.append(obc.getOrderBy());
1787            }
1788
1789            else {
1790                query.append("ORDER BY ");
1791
1792                query.append("createDate ASC, ");
1793                query.append("messageId ASC");
1794            }
1795
1796            Query q = session.createQuery(query.toString());
1797
1798            QueryPos qPos = QueryPos.getInstance(q);
1799
1800            qPos.add(threadId);
1801
1802            qPos.add(parentMessageId);
1803
1804            Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
1805                    mbMessage);
1806
1807            MBMessage[] array = new MBMessageImpl[3];
1808
1809            array[0] = (MBMessage)objArray[0];
1810            array[1] = (MBMessage)objArray[1];
1811            array[2] = (MBMessage)objArray[2];
1812
1813            return array;
1814        }
1815        catch (Exception e) {
1816            throw processException(e);
1817        }
1818        finally {
1819            closeSession(session);
1820        }
1821    }
1822
1823    public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
1824        throws SystemException {
1825        Session session = null;
1826
1827        try {
1828            session = openSession();
1829
1830            dynamicQuery.compile(session);
1831
1832            return dynamicQuery.list();
1833        }
1834        catch (Exception e) {
1835            throw processException(e);
1836        }
1837        finally {
1838            closeSession(session);
1839        }
1840    }
1841
1842    public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
1843        int start, int end) throws SystemException {
1844        Session session = null;
1845
1846        try {
1847            session = openSession();
1848
1849            dynamicQuery.setLimit(start, end);
1850
1851            dynamicQuery.compile(session);
1852
1853            return dynamicQuery.list();
1854        }
1855        catch (Exception e) {
1856            throw processException(e);
1857        }
1858        finally {
1859            closeSession(session);
1860        }
1861    }
1862
1863    public List<MBMessage> findAll() throws SystemException {
1864        return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
1865    }
1866
1867    public List<MBMessage> findAll(int start, int end)
1868        throws SystemException {
1869        return findAll(start, end, null);
1870    }
1871
1872    public List<MBMessage> findAll(int start, int end, OrderByComparator obc)
1873        throws SystemException {
1874        boolean finderClassNameCacheEnabled = MBMessageModelImpl.CACHE_ENABLED;
1875        String finderClassName = MBMessage.class.getName();
1876        String finderMethodName = "findAll";
1877        String[] finderParams = new String[] {
1878                "java.lang.Integer", "java.lang.Integer",
1879                "com.liferay.portal.kernel.util.OrderByComparator"
1880            };
1881        Object[] finderArgs = new Object[] {
1882                String.valueOf(start), String.valueOf(end), String.valueOf(obc)
1883            };
1884
1885        Object result = null;
1886
1887        if (finderClassNameCacheEnabled) {
1888            result = FinderCacheUtil.getResult(finderClassName,
1889                    finderMethodName, finderParams, finderArgs, this);
1890        }
1891
1892        if (result == null) {
1893            Session session = null;
1894
1895            try {
1896                session = openSession();
1897
1898                StringBuilder query = new StringBuilder();
1899
1900                query.append(
1901                    "FROM com.liferay.portlet.messageboards.model.MBMessage ");
1902
1903                if (obc != null) {
1904                    query.append("ORDER BY ");
1905                    query.append(obc.getOrderBy());
1906                }
1907
1908                else {
1909                    query.append("ORDER BY ");
1910
1911                    query.append("createDate ASC, ");
1912                    query.append("messageId ASC");
1913                }
1914
1915                Query q = session.createQuery(query.toString());
1916
1917                List<MBMessage> list = (List<MBMessage>)QueryUtil.list(q,
1918                        getDialect(), start, end);
1919
1920                if (obc == null) {
1921                    Collections.sort(list);
1922                }
1923
1924                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1925                    finderClassName, finderMethodName, finderParams,
1926                    finderArgs, list);
1927
1928                return list;
1929            }
1930            catch (Exception e) {
1931                throw processException(e);
1932            }
1933            finally {
1934                closeSession(session);
1935            }
1936        }
1937        else {
1938            return (List<MBMessage>)result;
1939        }
1940    }
1941
1942    public void removeByUuid(String uuid) throws SystemException {
1943        for (MBMessage mbMessage : findByUuid(uuid)) {
1944            remove(mbMessage);
1945        }
1946    }
1947
1948    public void removeByCompanyId(long companyId) throws SystemException {
1949        for (MBMessage mbMessage : findByCompanyId(companyId)) {
1950            remove(mbMessage);
1951        }
1952    }
1953
1954    public void removeByCategoryId(long categoryId) throws SystemException {
1955        for (MBMessage mbMessage : findByCategoryId(categoryId)) {
1956            remove(mbMessage);
1957        }
1958    }
1959
1960    public void removeByThreadId(long threadId) throws SystemException {
1961        for (MBMessage mbMessage : findByThreadId(threadId)) {
1962            remove(mbMessage);
1963        }
1964    }
1965
1966    public void removeByC_T(long categoryId, long threadId)
1967        throws SystemException {
1968        for (MBMessage mbMessage : findByC_T(categoryId, threadId)) {
1969            remove(mbMessage);
1970        }
1971    }
1972
1973    public void removeByT_P(long threadId, long parentMessageId)
1974        throws SystemException {
1975        for (MBMessage mbMessage : findByT_P(threadId, parentMessageId)) {
1976            remove(mbMessage);
1977        }
1978    }
1979
1980    public void removeAll() throws SystemException {
1981        for (MBMessage mbMessage : findAll()) {
1982            remove(mbMessage);
1983        }
1984    }
1985
1986    public int countByUuid(String uuid) throws SystemException {
1987        boolean finderClassNameCacheEnabled = MBMessageModelImpl.CACHE_ENABLED;
1988        String finderClassName = MBMessage.class.getName();
1989        String finderMethodName = "countByUuid";
1990        String[] finderParams = new String[] { String.class.getName() };
1991        Object[] finderArgs = new Object[] { uuid };
1992
1993        Object result = null;
1994
1995        if (finderClassNameCacheEnabled) {
1996            result = FinderCacheUtil.getResult(finderClassName,
1997                    finderMethodName, finderParams, finderArgs, this);
1998        }
1999
2000        if (result == null) {
2001            Session session = null;
2002
2003            try {
2004                session = openSession();
2005
2006                StringBuilder query = new StringBuilder();
2007
2008                query.append("SELECT COUNT(*) ");
2009                query.append(
2010                    "FROM com.liferay.portlet.messageboards.model.MBMessage WHERE ");
2011
2012                if (uuid == null) {
2013                    query.append("uuid_ IS NULL");
2014                }
2015                else {
2016                    query.append("uuid_ = ?");
2017                }
2018
2019                query.append(" ");
2020
2021                Query q = session.createQuery(query.toString());
2022
2023                QueryPos qPos = QueryPos.getInstance(q);
2024
2025                if (uuid != null) {
2026                    qPos.add(uuid);
2027                }
2028
2029                Long count = null;
2030
2031                Iterator<Long> itr = q.list().iterator();
2032
2033                if (itr.hasNext()) {
2034                    count = itr.next();
2035                }
2036
2037                if (count == null) {
2038                    count = new Long(0);
2039                }
2040
2041                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
2042                    finderClassName, finderMethodName, finderParams,
2043                    finderArgs, count);
2044
2045                return count.intValue();
2046            }
2047            catch (Exception e) {
2048                throw processException(e);
2049            }
2050            finally {
2051                closeSession(session);
2052            }
2053        }
2054        else {
2055            return ((Long)result).intValue();
2056        }
2057    }
2058
2059    public int countByCompanyId(long companyId) throws SystemException {
2060        boolean finderClassNameCacheEnabled = MBMessageModelImpl.CACHE_ENABLED;
2061        String finderClassName = MBMessage.class.getName();
2062        String finderMethodName = "countByCompanyId";
2063        String[] finderParams = new String[] { Long.class.getName() };
2064        Object[] finderArgs = new Object[] { new Long(companyId) };
2065
2066        Object result = null;
2067
2068        if (finderClassNameCacheEnabled) {
2069            result = FinderCacheUtil.getResult(finderClassName,
2070                    finderMethodName, finderParams, finderArgs, this);
2071        }
2072
2073        if (result == null) {
2074            Session session = null;
2075
2076            try {
2077                session = openSession();
2078
2079                StringBuilder query = new StringBuilder();
2080
2081                query.append("SELECT COUNT(*) ");
2082                query.append(
2083                    "FROM com.liferay.portlet.messageboards.model.MBMessage WHERE ");
2084
2085                query.append("companyId = ?");
2086
2087                query.append(" ");
2088
2089                Query q = session.createQuery(query.toString());
2090
2091                QueryPos qPos = QueryPos.getInstance(q);
2092
2093                qPos.add(companyId);
2094
2095                Long count = null;
2096
2097                Iterator<Long> itr = q.list().iterator();
2098
2099                if (itr.hasNext()) {
2100                    count = itr.next();
2101                }
2102
2103                if (count == null) {
2104                    count = new Long(0);
2105                }
2106
2107                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
2108                    finderClassName, finderMethodName, finderParams,
2109                    finderArgs, count);
2110
2111                return count.intValue();
2112            }
2113            catch (Exception e) {
2114                throw processException(e);
2115            }
2116            finally {
2117                closeSession(session);
2118            }
2119        }
2120        else {
2121            return ((Long)result).intValue();
2122        }
2123    }
2124
2125    public int countByCategoryId(long categoryId) throws SystemException {
2126        boolean finderClassNameCacheEnabled = MBMessageModelImpl.CACHE_ENABLED;
2127        String finderClassName = MBMessage.class.getName();
2128        String finderMethodName = "countByCategoryId";
2129        String[] finderParams = new String[] { Long.class.getName() };
2130        Object[] finderArgs = new Object[] { new Long(categoryId) };
2131
2132        Object result = null;
2133
2134        if (finderClassNameCacheEnabled) {
2135            result = FinderCacheUtil.getResult(finderClassName,
2136                    finderMethodName, finderParams, finderArgs, this);
2137        }
2138
2139        if (result == null) {
2140            Session session = null;
2141
2142            try {
2143                session = openSession();
2144
2145                StringBuilder query = new StringBuilder();
2146
2147                query.append("SELECT COUNT(*) ");
2148                query.append(
2149                    "FROM com.liferay.portlet.messageboards.model.MBMessage WHERE ");
2150
2151                query.append("categoryId = ?");
2152
2153                query.append(" ");
2154
2155                Query q = session.createQuery(query.toString());
2156
2157                QueryPos qPos = QueryPos.getInstance(q);
2158
2159                qPos.add(categoryId);
2160
2161                Long count = null;
2162
2163                Iterator<Long> itr = q.list().iterator();
2164
2165                if (itr.hasNext()) {
2166                    count = itr.next();
2167                }
2168
2169                if (count == null) {
2170                    count = new Long(0);
2171                }
2172
2173                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
2174                    finderClassName, finderMethodName, finderParams,
2175                    finderArgs, count);
2176
2177                return count.intValue();
2178            }
2179            catch (Exception e) {
2180                throw processException(e);
2181            }
2182            finally {
2183                closeSession(session);
2184            }
2185        }
2186        else {
2187            return ((Long)result).intValue();
2188        }
2189    }
2190
2191    public int countByThreadId(long threadId) throws SystemException {
2192        boolean finderClassNameCacheEnabled = MBMessageModelImpl.CACHE_ENABLED;
2193        String finderClassName = MBMessage.class.getName();
2194        String finderMethodName = "countByThreadId";
2195        String[] finderParams = new String[] { Long.class.getName() };
2196        Object[] finderArgs = new Object[] { new Long(threadId) };
2197
2198        Object result = null;
2199
2200        if (finderClassNameCacheEnabled) {
2201            result = FinderCacheUtil.getResult(finderClassName,
2202                    finderMethodName, finderParams, finderArgs, this);
2203        }
2204
2205        if (result == null) {
2206            Session session = null;
2207
2208            try {
2209                session = openSession();
2210
2211                StringBuilder query = new StringBuilder();
2212
2213                query.append("SELECT COUNT(*) ");
2214                query.append(
2215                    "FROM com.liferay.portlet.messageboards.model.MBMessage WHERE ");
2216
2217                query.append("threadId = ?");
2218
2219                query.append(" ");
2220
2221                Query q = session.createQuery(query.toString());
2222
2223                QueryPos qPos = QueryPos.getInstance(q);
2224
2225                qPos.add(threadId);
2226
2227                Long count = null;
2228
2229                Iterator<Long> itr = q.list().iterator();
2230
2231                if (itr.hasNext()) {
2232                    count = itr.next();
2233                }
2234
2235                if (count == null) {
2236                    count = new Long(0);
2237                }
2238
2239                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
2240                    finderClassName, finderMethodName, finderParams,
2241                    finderArgs, count);
2242
2243                return count.intValue();
2244            }
2245            catch (Exception e) {
2246                throw processException(e);
2247            }
2248            finally {
2249                closeSession(session);
2250            }
2251        }
2252        else {
2253            return ((Long)result).intValue();
2254        }
2255    }
2256
2257    public int countByC_T(long categoryId, long threadId)
2258        throws SystemException {
2259        boolean finderClassNameCacheEnabled = MBMessageModelImpl.CACHE_ENABLED;
2260        String finderClassName = MBMessage.class.getName();
2261        String finderMethodName = "countByC_T";
2262        String[] finderParams = new String[] {
2263                Long.class.getName(), Long.class.getName()
2264            };
2265        Object[] finderArgs = new Object[] {
2266                new Long(categoryId), new Long(threadId)
2267            };
2268
2269        Object result = null;
2270
2271        if (finderClassNameCacheEnabled) {
2272            result = FinderCacheUtil.getResult(finderClassName,
2273                    finderMethodName, finderParams, finderArgs, this);
2274        }
2275
2276        if (result == null) {
2277            Session session = null;
2278
2279            try {
2280                session = openSession();
2281
2282                StringBuilder query = new StringBuilder();
2283
2284                query.append("SELECT COUNT(*) ");
2285                query.append(
2286                    "FROM com.liferay.portlet.messageboards.model.MBMessage WHERE ");
2287
2288                query.append("categoryId = ?");
2289
2290                query.append(" AND ");
2291
2292                query.append("threadId = ?");
2293
2294                query.append(" ");
2295
2296                Query q = session.createQuery(query.toString());
2297
2298                QueryPos qPos = QueryPos.getInstance(q);
2299
2300                qPos.add(categoryId);
2301
2302                qPos.add(threadId);
2303
2304                Long count = null;
2305
2306                Iterator<Long> itr = q.list().iterator();
2307
2308                if (itr.hasNext()) {
2309                    count = itr.next();
2310                }
2311
2312                if (count == null) {
2313                    count = new Long(0);
2314                }
2315
2316                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
2317                    finderClassName, finderMethodName, finderParams,
2318                    finderArgs, count);
2319
2320                return count.intValue();
2321            }
2322            catch (Exception e) {
2323                throw processException(e);
2324            }
2325            finally {
2326                closeSession(session);
2327            }
2328        }
2329        else {
2330            return ((Long)result).intValue();
2331        }
2332    }
2333
2334    public int countByT_P(long threadId, long parentMessageId)
2335        throws SystemException {
2336        boolean finderClassNameCacheEnabled = MBMessageModelImpl.CACHE_ENABLED;
2337        String finderClassName = MBMessage.class.getName();
2338        String finderMethodName = "countByT_P";
2339        String[] finderParams = new String[] {
2340                Long.class.getName(), Long.class.getName()
2341            };
2342        Object[] finderArgs = new Object[] {
2343                new Long(threadId), new Long(parentMessageId)
2344            };
2345
2346        Object result = null;
2347
2348        if (finderClassNameCacheEnabled) {
2349            result = FinderCacheUtil.getResult(finderClassName,
2350                    finderMethodName, finderParams, finderArgs, this);
2351        }
2352
2353        if (result == null) {
2354            Session session = null;
2355
2356            try {
2357                session = openSession();
2358
2359                StringBuilder query = new StringBuilder();
2360
2361                query.append("SELECT COUNT(*) ");
2362                query.append(
2363                    "FROM com.liferay.portlet.messageboards.model.MBMessage WHERE ");
2364
2365                query.append("threadId = ?");
2366
2367                query.append(" AND ");
2368
2369                query.append("parentMessageId = ?");
2370
2371                query.append(" ");
2372
2373                Query q = session.createQuery(query.toString());
2374
2375                QueryPos qPos = QueryPos.getInstance(q);
2376
2377                qPos.add(threadId);
2378
2379                qPos.add(parentMessageId);
2380
2381                Long count = null;
2382
2383                Iterator<Long> itr = q.list().iterator();
2384
2385                if (itr.hasNext()) {
2386                    count = itr.next();
2387                }
2388
2389                if (count == null) {
2390                    count = new Long(0);
2391                }
2392
2393                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
2394                    finderClassName, finderMethodName, finderParams,
2395                    finderArgs, count);
2396
2397                return count.intValue();
2398            }
2399            catch (Exception e) {
2400                throw processException(e);
2401            }
2402            finally {
2403                closeSession(session);
2404            }
2405        }
2406        else {
2407            return ((Long)result).intValue();
2408        }
2409    }
2410
2411    public int countAll() throws SystemException {
2412        boolean finderClassNameCacheEnabled = MBMessageModelImpl.CACHE_ENABLED;
2413        String finderClassName = MBMessage.class.getName();
2414        String finderMethodName = "countAll";
2415        String[] finderParams = new String[] {  };
2416        Object[] finderArgs = new Object[] {  };
2417
2418        Object result = null;
2419
2420        if (finderClassNameCacheEnabled) {
2421            result = FinderCacheUtil.getResult(finderClassName,
2422                    finderMethodName, finderParams, finderArgs, this);
2423        }
2424
2425        if (result == null) {
2426            Session session = null;
2427
2428            try {
2429                session = openSession();
2430
2431                Query q = session.createQuery(
2432                        "SELECT COUNT(*) FROM com.liferay.portlet.messageboards.model.MBMessage");
2433
2434                Long count = null;
2435
2436                Iterator<Long> itr = q.list().iterator();
2437
2438                if (itr.hasNext()) {
2439                    count = itr.next();
2440                }
2441
2442                if (count == null) {
2443                    count = new Long(0);
2444                }
2445
2446                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
2447                    finderClassName, finderMethodName, finderParams,
2448                    finderArgs, count);
2449
2450                return count.intValue();
2451            }
2452            catch (Exception e) {
2453                throw processException(e);
2454            }
2455            finally {
2456                closeSession(session);
2457            }
2458        }
2459        else {
2460            return ((Long)result).intValue();
2461        }
2462    }
2463
2464    public void registerListener(ModelListener listener) {
2465        List<ModelListener> listeners = ListUtil.fromArray(_listeners);
2466
2467        listeners.add(listener);
2468
2469        _listeners = listeners.toArray(new ModelListener[listeners.size()]);
2470    }
2471
2472    public void unregisterListener(ModelListener listener) {
2473        List<ModelListener> listeners = ListUtil.fromArray(_listeners);
2474
2475        listeners.remove(listener);
2476
2477        _listeners = listeners.toArray(new ModelListener[listeners.size()]);
2478    }
2479
2480    public void afterPropertiesSet() {
2481        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
2482                    com.liferay.portal.util.PropsUtil.get(
2483                        "value.object.listener.com.liferay.portlet.messageboards.model.MBMessage")));
2484
2485        if (listenerClassNames.length > 0) {
2486            try {
2487                List<ModelListener> listeners = new ArrayList<ModelListener>();
2488
2489                for (String listenerClassName : listenerClassNames) {
2490                    listeners.add((ModelListener)Class.forName(
2491                            listenerClassName).newInstance());
2492                }
2493
2494                _listeners = listeners.toArray(new ModelListener[listeners.size()]);
2495            }
2496            catch (Exception e) {
2497                _log.error(e);
2498            }
2499        }
2500    }
2501
2502    private static Log _log = LogFactory.getLog(MBMessagePersistenceImpl.class);
2503    private ModelListener[] _listeners = new ModelListener[0];
2504}