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