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.kernel.util.Validator;
38  import com.liferay.portal.kernel.uuid.PortalUUIDUtil;
39  import com.liferay.portal.model.ModelListener;
40  import com.liferay.portal.service.persistence.BatchSessionUtil;
41  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
42  
43  import com.liferay.portlet.messageboards.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 {
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             if (BatchSessionUtil.isEnabled()) {
136                 Object staleObject = session.get(MBMessageImpl.class,
137                         mbMessage.getPrimaryKeyObj());
138 
139                 if (staleObject != null) {
140                     session.evict(staleObject);
141                 }
142             }
143 
144             session.delete(mbMessage);
145 
146             session.flush();
147 
148             return mbMessage;
149         }
150         catch (Exception e) {
151             throw processException(e);
152         }
153         finally {
154             closeSession(session);
155 
156             FinderCacheUtil.clearCache(MBMessage.class.getName());
157         }
158     }
159 
160     /**
161      * @deprecated Use <code>update(MBMessage mbMessage, boolean merge)</code>.
162      */
163     public MBMessage update(MBMessage mbMessage) throws SystemException {
164         if (_log.isWarnEnabled()) {
165             _log.warn(
166                 "Using the deprecated update(MBMessage mbMessage) method. Use update(MBMessage mbMessage, boolean merge) instead.");
167         }
168 
169         return update(mbMessage, false);
170     }
171 
172     /**
173      * Add, update, or merge, the entity. This method also calls the model
174      * listeners to trigger the proper events associated with adding, deleting,
175      * or updating an entity.
176      *
177      * @param        mbMessage the entity to add, update, or merge
178      * @param        merge boolean value for whether to merge the entity. The
179      *                default value is false. Setting merge to true is more
180      *                expensive and should only be true when mbMessage is
181      *                transient. See LEP-5473 for a detailed discussion of this
182      *                method.
183      * @return        true if the portlet can be displayed via Ajax
184      */
185     public MBMessage update(MBMessage mbMessage, boolean merge)
186         throws SystemException {
187         boolean isNew = mbMessage.isNew();
188 
189         if (_listeners.length > 0) {
190             for (ModelListener listener : _listeners) {
191                 if (isNew) {
192                     listener.onBeforeCreate(mbMessage);
193                 }
194                 else {
195                     listener.onBeforeUpdate(mbMessage);
196                 }
197             }
198         }
199 
200         mbMessage = updateImpl(mbMessage, merge);
201 
202         if (_listeners.length > 0) {
203             for (ModelListener listener : _listeners) {
204                 if (isNew) {
205                     listener.onAfterCreate(mbMessage);
206                 }
207                 else {
208                     listener.onAfterUpdate(mbMessage);
209                 }
210             }
211         }
212 
213         return mbMessage;
214     }
215 
216     public MBMessage updateImpl(
217         com.liferay.portlet.messageboards.model.MBMessage mbMessage,
218         boolean merge) throws SystemException {
219         if (Validator.isNull(mbMessage.getUuid())) {
220             String uuid = PortalUUIDUtil.generate();
221 
222             mbMessage.setUuid(uuid);
223         }
224 
225         Session session = null;
226 
227         try {
228             session = openSession();
229 
230             BatchSessionUtil.update(session, mbMessage, merge);
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 = null;
1918
1919                if (obc == null) {
1920                    list = (List<MBMessage>)QueryUtil.list(q, getDialect(),
1921                            start, end, false);
1922
1923                    Collections.sort(list);
1924                }
1925                else {
1926                    list = (List<MBMessage>)QueryUtil.list(q, getDialect(),
1927                            start, end);
1928                }
1929
1930                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
1931                    finderClassName, finderMethodName, finderParams,
1932                    finderArgs, list);
1933
1934                return list;
1935            }
1936            catch (Exception e) {
1937                throw processException(e);
1938            }
1939            finally {
1940                closeSession(session);
1941            }
1942        }
1943        else {
1944            return (List<MBMessage>)result;
1945        }
1946    }
1947
1948    public void removeByUuid(String uuid) throws SystemException {
1949        for (MBMessage mbMessage : findByUuid(uuid)) {
1950            remove(mbMessage);
1951        }
1952    }
1953
1954    public void removeByCompanyId(long companyId) throws SystemException {
1955        for (MBMessage mbMessage : findByCompanyId(companyId)) {
1956            remove(mbMessage);
1957        }
1958    }
1959
1960    public void removeByCategoryId(long categoryId) throws SystemException {
1961        for (MBMessage mbMessage : findByCategoryId(categoryId)) {
1962            remove(mbMessage);
1963        }
1964    }
1965
1966    public void removeByThreadId(long threadId) throws SystemException {
1967        for (MBMessage mbMessage : findByThreadId(threadId)) {
1968            remove(mbMessage);
1969        }
1970    }
1971
1972    public void removeByC_T(long categoryId, long threadId)
1973        throws SystemException {
1974        for (MBMessage mbMessage : findByC_T(categoryId, threadId)) {
1975            remove(mbMessage);
1976        }
1977    }
1978
1979    public void removeByT_P(long threadId, long parentMessageId)
1980        throws SystemException {
1981        for (MBMessage mbMessage : findByT_P(threadId, parentMessageId)) {
1982            remove(mbMessage);
1983        }
1984    }
1985
1986    public void removeAll() throws SystemException {
1987        for (MBMessage mbMessage : findAll()) {
1988            remove(mbMessage);
1989        }
1990    }
1991
1992    public int countByUuid(String uuid) throws SystemException {
1993        boolean finderClassNameCacheEnabled = MBMessageModelImpl.CACHE_ENABLED;
1994        String finderClassName = MBMessage.class.getName();
1995        String finderMethodName = "countByUuid";
1996        String[] finderParams = new String[] { String.class.getName() };
1997        Object[] finderArgs = new Object[] { uuid };
1998
1999        Object result = null;
2000
2001        if (finderClassNameCacheEnabled) {
2002            result = FinderCacheUtil.getResult(finderClassName,
2003                    finderMethodName, finderParams, finderArgs, this);
2004        }
2005
2006        if (result == null) {
2007            Session session = null;
2008
2009            try {
2010                session = openSession();
2011
2012                StringBuilder query = new StringBuilder();
2013
2014                query.append("SELECT COUNT(*) ");
2015                query.append(
2016                    "FROM com.liferay.portlet.messageboards.model.MBMessage WHERE ");
2017
2018                if (uuid == null) {
2019                    query.append("uuid_ IS NULL");
2020                }
2021                else {
2022                    query.append("uuid_ = ?");
2023                }
2024
2025                query.append(" ");
2026
2027                Query q = session.createQuery(query.toString());
2028
2029                QueryPos qPos = QueryPos.getInstance(q);
2030
2031                if (uuid != null) {
2032                    qPos.add(uuid);
2033                }
2034
2035                Long count = null;
2036
2037                Iterator<Long> itr = q.list().iterator();
2038
2039                if (itr.hasNext()) {
2040                    count = itr.next();
2041                }
2042
2043                if (count == null) {
2044                    count = new Long(0);
2045                }
2046
2047                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
2048                    finderClassName, finderMethodName, finderParams,
2049                    finderArgs, count);
2050
2051                return count.intValue();
2052            }
2053            catch (Exception e) {
2054                throw processException(e);
2055            }
2056            finally {
2057                closeSession(session);
2058            }
2059        }
2060        else {
2061            return ((Long)result).intValue();
2062        }
2063    }
2064
2065    public int countByCompanyId(long companyId) throws SystemException {
2066        boolean finderClassNameCacheEnabled = MBMessageModelImpl.CACHE_ENABLED;
2067        String finderClassName = MBMessage.class.getName();
2068        String finderMethodName = "countByCompanyId";
2069        String[] finderParams = new String[] { Long.class.getName() };
2070        Object[] finderArgs = new Object[] { new Long(companyId) };
2071
2072        Object result = null;
2073
2074        if (finderClassNameCacheEnabled) {
2075            result = FinderCacheUtil.getResult(finderClassName,
2076                    finderMethodName, finderParams, finderArgs, this);
2077        }
2078
2079        if (result == null) {
2080            Session session = null;
2081
2082            try {
2083                session = openSession();
2084
2085                StringBuilder query = new StringBuilder();
2086
2087                query.append("SELECT COUNT(*) ");
2088                query.append(
2089                    "FROM com.liferay.portlet.messageboards.model.MBMessage WHERE ");
2090
2091                query.append("companyId = ?");
2092
2093                query.append(" ");
2094
2095                Query q = session.createQuery(query.toString());
2096
2097                QueryPos qPos = QueryPos.getInstance(q);
2098
2099                qPos.add(companyId);
2100
2101                Long count = null;
2102
2103                Iterator<Long> itr = q.list().iterator();
2104
2105                if (itr.hasNext()) {
2106                    count = itr.next();
2107                }
2108
2109                if (count == null) {
2110                    count = new Long(0);
2111                }
2112
2113                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
2114                    finderClassName, finderMethodName, finderParams,
2115                    finderArgs, count);
2116
2117                return count.intValue();
2118            }
2119            catch (Exception e) {
2120                throw processException(e);
2121            }
2122            finally {
2123                closeSession(session);
2124            }
2125        }
2126        else {
2127            return ((Long)result).intValue();
2128        }
2129    }
2130
2131    public int countByCategoryId(long categoryId) throws SystemException {
2132        boolean finderClassNameCacheEnabled = MBMessageModelImpl.CACHE_ENABLED;
2133        String finderClassName = MBMessage.class.getName();
2134        String finderMethodName = "countByCategoryId";
2135        String[] finderParams = new String[] { Long.class.getName() };
2136        Object[] finderArgs = new Object[] { new Long(categoryId) };
2137
2138        Object result = null;
2139
2140        if (finderClassNameCacheEnabled) {
2141            result = FinderCacheUtil.getResult(finderClassName,
2142                    finderMethodName, finderParams, finderArgs, this);
2143        }
2144
2145        if (result == null) {
2146            Session session = null;
2147
2148            try {
2149                session = openSession();
2150
2151                StringBuilder query = new StringBuilder();
2152
2153                query.append("SELECT COUNT(*) ");
2154                query.append(
2155                    "FROM com.liferay.portlet.messageboards.model.MBMessage WHERE ");
2156
2157                query.append("categoryId = ?");
2158
2159                query.append(" ");
2160
2161                Query q = session.createQuery(query.toString());
2162
2163                QueryPos qPos = QueryPos.getInstance(q);
2164
2165                qPos.add(categoryId);
2166
2167                Long count = null;
2168
2169                Iterator<Long> itr = q.list().iterator();
2170
2171                if (itr.hasNext()) {
2172                    count = itr.next();
2173                }
2174
2175                if (count == null) {
2176                    count = new Long(0);
2177                }
2178
2179                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
2180                    finderClassName, finderMethodName, finderParams,
2181                    finderArgs, count);
2182
2183                return count.intValue();
2184            }
2185            catch (Exception e) {
2186                throw processException(e);
2187            }
2188            finally {
2189                closeSession(session);
2190            }
2191        }
2192        else {
2193            return ((Long)result).intValue();
2194        }
2195    }
2196
2197    public int countByThreadId(long threadId) throws SystemException {
2198        boolean finderClassNameCacheEnabled = MBMessageModelImpl.CACHE_ENABLED;
2199        String finderClassName = MBMessage.class.getName();
2200        String finderMethodName = "countByThreadId";
2201        String[] finderParams = new String[] { Long.class.getName() };
2202        Object[] finderArgs = new Object[] { new Long(threadId) };
2203
2204        Object result = null;
2205
2206        if (finderClassNameCacheEnabled) {
2207            result = FinderCacheUtil.getResult(finderClassName,
2208                    finderMethodName, finderParams, finderArgs, this);
2209        }
2210
2211        if (result == null) {
2212            Session session = null;
2213
2214            try {
2215                session = openSession();
2216
2217                StringBuilder query = new StringBuilder();
2218
2219                query.append("SELECT COUNT(*) ");
2220                query.append(
2221                    "FROM com.liferay.portlet.messageboards.model.MBMessage WHERE ");
2222
2223                query.append("threadId = ?");
2224
2225                query.append(" ");
2226
2227                Query q = session.createQuery(query.toString());
2228
2229                QueryPos qPos = QueryPos.getInstance(q);
2230
2231                qPos.add(threadId);
2232
2233                Long count = null;
2234
2235                Iterator<Long> itr = q.list().iterator();
2236
2237                if (itr.hasNext()) {
2238                    count = itr.next();
2239                }
2240
2241                if (count == null) {
2242                    count = new Long(0);
2243                }
2244
2245                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
2246                    finderClassName, finderMethodName, finderParams,
2247                    finderArgs, count);
2248
2249                return count.intValue();
2250            }
2251            catch (Exception e) {
2252                throw processException(e);
2253            }
2254            finally {
2255                closeSession(session);
2256            }
2257        }
2258        else {
2259            return ((Long)result).intValue();
2260        }
2261    }
2262
2263    public int countByC_T(long categoryId, long threadId)
2264        throws SystemException {
2265        boolean finderClassNameCacheEnabled = MBMessageModelImpl.CACHE_ENABLED;
2266        String finderClassName = MBMessage.class.getName();
2267        String finderMethodName = "countByC_T";
2268        String[] finderParams = new String[] {
2269                Long.class.getName(), Long.class.getName()
2270            };
2271        Object[] finderArgs = new Object[] {
2272                new Long(categoryId), new Long(threadId)
2273            };
2274
2275        Object result = null;
2276
2277        if (finderClassNameCacheEnabled) {
2278            result = FinderCacheUtil.getResult(finderClassName,
2279                    finderMethodName, finderParams, finderArgs, this);
2280        }
2281
2282        if (result == null) {
2283            Session session = null;
2284
2285            try {
2286                session = openSession();
2287
2288                StringBuilder query = new StringBuilder();
2289
2290                query.append("SELECT COUNT(*) ");
2291                query.append(
2292                    "FROM com.liferay.portlet.messageboards.model.MBMessage WHERE ");
2293
2294                query.append("categoryId = ?");
2295
2296                query.append(" AND ");
2297
2298                query.append("threadId = ?");
2299
2300                query.append(" ");
2301
2302                Query q = session.createQuery(query.toString());
2303
2304                QueryPos qPos = QueryPos.getInstance(q);
2305
2306                qPos.add(categoryId);
2307
2308                qPos.add(threadId);
2309
2310                Long count = null;
2311
2312                Iterator<Long> itr = q.list().iterator();
2313
2314                if (itr.hasNext()) {
2315                    count = itr.next();
2316                }
2317
2318                if (count == null) {
2319                    count = new Long(0);
2320                }
2321
2322                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
2323                    finderClassName, finderMethodName, finderParams,
2324                    finderArgs, count);
2325
2326                return count.intValue();
2327            }
2328            catch (Exception e) {
2329                throw processException(e);
2330            }
2331            finally {
2332                closeSession(session);
2333            }
2334        }
2335        else {
2336            return ((Long)result).intValue();
2337        }
2338    }
2339
2340    public int countByT_P(long threadId, long parentMessageId)
2341        throws SystemException {
2342        boolean finderClassNameCacheEnabled = MBMessageModelImpl.CACHE_ENABLED;
2343        String finderClassName = MBMessage.class.getName();
2344        String finderMethodName = "countByT_P";
2345        String[] finderParams = new String[] {
2346                Long.class.getName(), Long.class.getName()
2347            };
2348        Object[] finderArgs = new Object[] {
2349                new Long(threadId), new Long(parentMessageId)
2350            };
2351
2352        Object result = null;
2353
2354        if (finderClassNameCacheEnabled) {
2355            result = FinderCacheUtil.getResult(finderClassName,
2356                    finderMethodName, finderParams, finderArgs, this);
2357        }
2358
2359        if (result == null) {
2360            Session session = null;
2361
2362            try {
2363                session = openSession();
2364
2365                StringBuilder query = new StringBuilder();
2366
2367                query.append("SELECT COUNT(*) ");
2368                query.append(
2369                    "FROM com.liferay.portlet.messageboards.model.MBMessage WHERE ");
2370
2371                query.append("threadId = ?");
2372
2373                query.append(" AND ");
2374
2375                query.append("parentMessageId = ?");
2376
2377                query.append(" ");
2378
2379                Query q = session.createQuery(query.toString());
2380
2381                QueryPos qPos = QueryPos.getInstance(q);
2382
2383                qPos.add(threadId);
2384
2385                qPos.add(parentMessageId);
2386
2387                Long count = null;
2388
2389                Iterator<Long> itr = q.list().iterator();
2390
2391                if (itr.hasNext()) {
2392                    count = itr.next();
2393                }
2394
2395                if (count == null) {
2396                    count = new Long(0);
2397                }
2398
2399                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
2400                    finderClassName, finderMethodName, finderParams,
2401                    finderArgs, count);
2402
2403                return count.intValue();
2404            }
2405            catch (Exception e) {
2406                throw processException(e);
2407            }
2408            finally {
2409                closeSession(session);
2410            }
2411        }
2412        else {
2413            return ((Long)result).intValue();
2414        }
2415    }
2416
2417    public int countAll() throws SystemException {
2418        boolean finderClassNameCacheEnabled = MBMessageModelImpl.CACHE_ENABLED;
2419        String finderClassName = MBMessage.class.getName();
2420        String finderMethodName = "countAll";
2421        String[] finderParams = new String[] {  };
2422        Object[] finderArgs = new Object[] {  };
2423
2424        Object result = null;
2425
2426        if (finderClassNameCacheEnabled) {
2427            result = FinderCacheUtil.getResult(finderClassName,
2428                    finderMethodName, finderParams, finderArgs, this);
2429        }
2430
2431        if (result == null) {
2432            Session session = null;
2433
2434            try {
2435                session = openSession();
2436
2437                Query q = session.createQuery(
2438                        "SELECT COUNT(*) FROM com.liferay.portlet.messageboards.model.MBMessage");
2439
2440                Long count = null;
2441
2442                Iterator<Long> itr = q.list().iterator();
2443
2444                if (itr.hasNext()) {
2445                    count = itr.next();
2446                }
2447
2448                if (count == null) {
2449                    count = new Long(0);
2450                }
2451
2452                FinderCacheUtil.putResult(finderClassNameCacheEnabled,
2453                    finderClassName, finderMethodName, finderParams,
2454                    finderArgs, count);
2455
2456                return count.intValue();
2457            }
2458            catch (Exception e) {
2459                throw processException(e);
2460            }
2461            finally {
2462                closeSession(session);
2463            }
2464        }
2465        else {
2466            return ((Long)result).intValue();
2467        }
2468    }
2469
2470    public void registerListener(ModelListener listener) {
2471        List<ModelListener> listeners = ListUtil.fromArray(_listeners);
2472
2473        listeners.add(listener);
2474
2475        _listeners = listeners.toArray(new ModelListener[listeners.size()]);
2476    }
2477
2478    public void unregisterListener(ModelListener listener) {
2479        List<ModelListener> listeners = ListUtil.fromArray(_listeners);
2480
2481        listeners.remove(listener);
2482
2483        _listeners = listeners.toArray(new ModelListener[listeners.size()]);
2484    }
2485
2486    public void afterPropertiesSet() {
2487        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
2488                    com.liferay.portal.util.PropsUtil.get(
2489                        "value.object.listener.com.liferay.portlet.messageboards.model.MBMessage")));
2490
2491        if (listenerClassNames.length > 0) {
2492            try {
2493                List<ModelListener> listeners = new ArrayList<ModelListener>();
2494
2495                for (String listenerClassName : listenerClassNames) {
2496                    listeners.add((ModelListener)Class.forName(
2497                            listenerClassName).newInstance());
2498                }
2499
2500                _listeners = listeners.toArray(new ModelListener[listeners.size()]);
2501            }
2502            catch (Exception e) {
2503                _log.error(e);
2504            }
2505        }
2506    }
2507
2508    private static Log _log = LogFactory.getLog(MBMessagePersistenceImpl.class);
2509    private ModelListener[] _listeners = new ModelListener[0];
2510}