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