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