1   /**
2    * Copyright (c) 2000-2009 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.annotation.BeanReference;
27  import com.liferay.portal.kernel.cache.CacheRegistry;
28  import com.liferay.portal.kernel.dao.orm.DynamicQuery;
29  import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
30  import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
31  import com.liferay.portal.kernel.dao.orm.FinderPath;
32  import com.liferay.portal.kernel.dao.orm.Query;
33  import com.liferay.portal.kernel.dao.orm.QueryPos;
34  import com.liferay.portal.kernel.dao.orm.QueryUtil;
35  import com.liferay.portal.kernel.dao.orm.Session;
36  import com.liferay.portal.kernel.log.Log;
37  import com.liferay.portal.kernel.log.LogFactoryUtil;
38  import com.liferay.portal.kernel.util.CalendarUtil;
39  import com.liferay.portal.kernel.util.GetterUtil;
40  import com.liferay.portal.kernel.util.OrderByComparator;
41  import com.liferay.portal.kernel.util.StringPool;
42  import com.liferay.portal.kernel.util.StringUtil;
43  import com.liferay.portal.model.ModelListener;
44  import com.liferay.portal.service.persistence.BatchSessionUtil;
45  import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
46  
47  import com.liferay.portlet.messageboards.NoSuchThreadException;
48  import com.liferay.portlet.messageboards.model.MBThread;
49  import com.liferay.portlet.messageboards.model.impl.MBThreadImpl;
50  import com.liferay.portlet.messageboards.model.impl.MBThreadModelImpl;
51  
52  import java.util.ArrayList;
53  import java.util.Collections;
54  import java.util.Date;
55  import java.util.List;
56  
57  /**
58   * <a href="MBThreadPersistenceImpl.java.html"><b><i>View Source</i></b></a>
59   *
60   * @author Brian Wing Shun Chan
61   *
62   */
63  public class MBThreadPersistenceImpl extends BasePersistenceImpl
64      implements MBThreadPersistence {
65      public static final String FINDER_CLASS_NAME_ENTITY = MBThreadImpl.class.getName();
66      public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
67          ".List";
68      public static final FinderPath FINDER_PATH_FIND_BY_GROUPID = new FinderPath(MBThreadModelImpl.ENTITY_CACHE_ENABLED,
69              MBThreadModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
70              "findByGroupId", new String[] { Long.class.getName() });
71      public static final FinderPath FINDER_PATH_FIND_BY_OBC_GROUPID = new FinderPath(MBThreadModelImpl.ENTITY_CACHE_ENABLED,
72              MBThreadModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
73              "findByGroupId",
74              new String[] {
75                  Long.class.getName(),
76                  
77              "java.lang.Integer", "java.lang.Integer",
78                  "com.liferay.portal.kernel.util.OrderByComparator"
79              });
80      public static final FinderPath FINDER_PATH_COUNT_BY_GROUPID = new FinderPath(MBThreadModelImpl.ENTITY_CACHE_ENABLED,
81              MBThreadModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
82              "countByGroupId", new String[] { Long.class.getName() });
83      public static final FinderPath FINDER_PATH_FIND_BY_CATEGORYID = new FinderPath(MBThreadModelImpl.ENTITY_CACHE_ENABLED,
84              MBThreadModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
85              "findByCategoryId", new String[] { Long.class.getName() });
86      public static final FinderPath FINDER_PATH_FIND_BY_OBC_CATEGORYID = new FinderPath(MBThreadModelImpl.ENTITY_CACHE_ENABLED,
87              MBThreadModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
88              "findByCategoryId",
89              new String[] {
90                  Long.class.getName(),
91                  
92              "java.lang.Integer", "java.lang.Integer",
93                  "com.liferay.portal.kernel.util.OrderByComparator"
94              });
95      public static final FinderPath FINDER_PATH_COUNT_BY_CATEGORYID = new FinderPath(MBThreadModelImpl.ENTITY_CACHE_ENABLED,
96              MBThreadModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
97              "countByCategoryId", new String[] { Long.class.getName() });
98      public static final FinderPath FINDER_PATH_FIND_BY_C_L = new FinderPath(MBThreadModelImpl.ENTITY_CACHE_ENABLED,
99              MBThreadModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
100             "findByC_L",
101             new String[] { Long.class.getName(), Date.class.getName() });
102     public static final FinderPath FINDER_PATH_FIND_BY_OBC_C_L = new FinderPath(MBThreadModelImpl.ENTITY_CACHE_ENABLED,
103             MBThreadModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
104             "findByC_L",
105             new String[] {
106                 Long.class.getName(), Date.class.getName(),
107                 
108             "java.lang.Integer", "java.lang.Integer",
109                 "com.liferay.portal.kernel.util.OrderByComparator"
110             });
111     public static final FinderPath FINDER_PATH_COUNT_BY_C_L = new FinderPath(MBThreadModelImpl.ENTITY_CACHE_ENABLED,
112             MBThreadModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
113             "countByC_L",
114             new String[] { Long.class.getName(), Date.class.getName() });
115     public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(MBThreadModelImpl.ENTITY_CACHE_ENABLED,
116             MBThreadModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
117             "findAll", new String[0]);
118     public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(MBThreadModelImpl.ENTITY_CACHE_ENABLED,
119             MBThreadModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
120             "countAll", new String[0]);
121 
122     public void cacheResult(MBThread mbThread) {
123         EntityCacheUtil.putResult(MBThreadModelImpl.ENTITY_CACHE_ENABLED,
124             MBThreadImpl.class, mbThread.getPrimaryKey(), mbThread);
125     }
126 
127     public void cacheResult(List<MBThread> mbThreads) {
128         for (MBThread mbThread : mbThreads) {
129             if (EntityCacheUtil.getResult(
130                         MBThreadModelImpl.ENTITY_CACHE_ENABLED,
131                         MBThreadImpl.class, mbThread.getPrimaryKey(), this) == null) {
132                 cacheResult(mbThread);
133             }
134         }
135     }
136 
137     public void clearCache() {
138         CacheRegistry.clear(MBThreadImpl.class.getName());
139         EntityCacheUtil.clearCache(MBThreadImpl.class.getName());
140         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
141         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
142     }
143 
144     public MBThread create(long threadId) {
145         MBThread mbThread = new MBThreadImpl();
146 
147         mbThread.setNew(true);
148         mbThread.setPrimaryKey(threadId);
149 
150         return mbThread;
151     }
152 
153     public MBThread remove(long threadId)
154         throws NoSuchThreadException, SystemException {
155         Session session = null;
156 
157         try {
158             session = openSession();
159 
160             MBThread mbThread = (MBThread)session.get(MBThreadImpl.class,
161                     new Long(threadId));
162 
163             if (mbThread == null) {
164                 if (_log.isWarnEnabled()) {
165                     _log.warn("No MBThread exists with the primary key " +
166                         threadId);
167                 }
168 
169                 throw new NoSuchThreadException(
170                     "No MBThread exists with the primary key " + threadId);
171             }
172 
173             return remove(mbThread);
174         }
175         catch (NoSuchThreadException nsee) {
176             throw nsee;
177         }
178         catch (Exception e) {
179             throw processException(e);
180         }
181         finally {
182             closeSession(session);
183         }
184     }
185 
186     public MBThread remove(MBThread mbThread) throws SystemException {
187         for (ModelListener<MBThread> listener : listeners) {
188             listener.onBeforeRemove(mbThread);
189         }
190 
191         mbThread = removeImpl(mbThread);
192 
193         for (ModelListener<MBThread> listener : listeners) {
194             listener.onAfterRemove(mbThread);
195         }
196 
197         return mbThread;
198     }
199 
200     protected MBThread removeImpl(MBThread mbThread) throws SystemException {
201         Session session = null;
202 
203         try {
204             session = openSession();
205 
206             if (mbThread.isCachedModel() || BatchSessionUtil.isEnabled()) {
207                 Object staleObject = session.get(MBThreadImpl.class,
208                         mbThread.getPrimaryKeyObj());
209 
210                 if (staleObject != null) {
211                     session.evict(staleObject);
212                 }
213             }
214 
215             session.delete(mbThread);
216 
217             session.flush();
218         }
219         catch (Exception e) {
220             throw processException(e);
221         }
222         finally {
223             closeSession(session);
224         }
225 
226         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
227 
228         EntityCacheUtil.removeResult(MBThreadModelImpl.ENTITY_CACHE_ENABLED,
229             MBThreadImpl.class, mbThread.getPrimaryKey());
230 
231         return mbThread;
232     }
233 
234     /**
235      * @deprecated Use <code>update(MBThread mbThread, boolean merge)</code>.
236      */
237     public MBThread update(MBThread mbThread) throws SystemException {
238         if (_log.isWarnEnabled()) {
239             _log.warn(
240                 "Using the deprecated update(MBThread mbThread) method. Use update(MBThread mbThread, boolean merge) instead.");
241         }
242 
243         return update(mbThread, false);
244     }
245 
246     /**
247      * Add, update, or merge, the entity. This method also calls the model
248      * listeners to trigger the proper events associated with adding, deleting,
249      * or updating an entity.
250      *
251      * @param        mbThread the entity to add, update, or merge
252      * @param        merge boolean value for whether to merge the entity. The
253      *                default value is false. Setting merge to true is more
254      *                expensive and should only be true when mbThread is
255      *                transient. See LEP-5473 for a detailed discussion of this
256      *                method.
257      * @return        true if the portlet can be displayed via Ajax
258      */
259     public MBThread update(MBThread mbThread, boolean merge)
260         throws SystemException {
261         boolean isNew = mbThread.isNew();
262 
263         for (ModelListener<MBThread> listener : listeners) {
264             if (isNew) {
265                 listener.onBeforeCreate(mbThread);
266             }
267             else {
268                 listener.onBeforeUpdate(mbThread);
269             }
270         }
271 
272         mbThread = updateImpl(mbThread, merge);
273 
274         for (ModelListener<MBThread> listener : listeners) {
275             if (isNew) {
276                 listener.onAfterCreate(mbThread);
277             }
278             else {
279                 listener.onAfterUpdate(mbThread);
280             }
281         }
282 
283         return mbThread;
284     }
285 
286     public MBThread updateImpl(
287         com.liferay.portlet.messageboards.model.MBThread mbThread, boolean merge)
288         throws SystemException {
289         Session session = null;
290 
291         try {
292             session = openSession();
293 
294             BatchSessionUtil.update(session, mbThread, merge);
295 
296             mbThread.setNew(false);
297         }
298         catch (Exception e) {
299             throw processException(e);
300         }
301         finally {
302             closeSession(session);
303         }
304 
305         FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
306 
307         EntityCacheUtil.putResult(MBThreadModelImpl.ENTITY_CACHE_ENABLED,
308             MBThreadImpl.class, mbThread.getPrimaryKey(), mbThread);
309 
310         return mbThread;
311     }
312 
313     public MBThread findByPrimaryKey(long threadId)
314         throws NoSuchThreadException, SystemException {
315         MBThread mbThread = fetchByPrimaryKey(threadId);
316 
317         if (mbThread == null) {
318             if (_log.isWarnEnabled()) {
319                 _log.warn("No MBThread exists with the primary key " +
320                     threadId);
321             }
322 
323             throw new NoSuchThreadException(
324                 "No MBThread exists with the primary key " + threadId);
325         }
326 
327         return mbThread;
328     }
329 
330     public MBThread fetchByPrimaryKey(long threadId) throws SystemException {
331         MBThread mbThread = (MBThread)EntityCacheUtil.getResult(MBThreadModelImpl.ENTITY_CACHE_ENABLED,
332                 MBThreadImpl.class, threadId, this);
333 
334         if (mbThread == null) {
335             Session session = null;
336 
337             try {
338                 session = openSession();
339 
340                 mbThread = (MBThread)session.get(MBThreadImpl.class,
341                         new Long(threadId));
342             }
343             catch (Exception e) {
344                 throw processException(e);
345             }
346             finally {
347                 if (mbThread != null) {
348                     cacheResult(mbThread);
349                 }
350 
351                 closeSession(session);
352             }
353         }
354 
355         return mbThread;
356     }
357 
358     public List<MBThread> findByGroupId(long groupId) throws SystemException {
359         Object[] finderArgs = new Object[] { new Long(groupId) };
360 
361         List<MBThread> list = (List<MBThread>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_GROUPID,
362                 finderArgs, this);
363 
364         if (list == 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.MBThread WHERE ");
374 
375                 query.append("groupId = ?");
376 
377                 query.append(" ");
378 
379                 query.append("ORDER BY ");
380 
381                 query.append("priority DESC, ");
382                 query.append("lastPostDate DESC");
383 
384                 Query q = session.createQuery(query.toString());
385 
386                 QueryPos qPos = QueryPos.getInstance(q);
387 
388                 qPos.add(groupId);
389 
390                 list = q.list();
391             }
392             catch (Exception e) {
393                 throw processException(e);
394             }
395             finally {
396                 if (list == null) {
397                     list = new ArrayList<MBThread>();
398                 }
399 
400                 cacheResult(list);
401 
402                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_GROUPID,
403                     finderArgs, list);
404 
405                 closeSession(session);
406             }
407         }
408 
409         return list;
410     }
411 
412     public List<MBThread> findByGroupId(long groupId, int start, int end)
413         throws SystemException {
414         return findByGroupId(groupId, start, end, null);
415     }
416 
417     public List<MBThread> findByGroupId(long groupId, int start, int end,
418         OrderByComparator obc) throws SystemException {
419         Object[] finderArgs = new Object[] {
420                 new Long(groupId),
421                 
422                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
423             };
424 
425         List<MBThread> list = (List<MBThread>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_OBC_GROUPID,
426                 finderArgs, this);
427 
428         if (list == null) {
429             Session session = null;
430 
431             try {
432                 session = openSession();
433 
434                 StringBuilder query = new StringBuilder();
435 
436                 query.append(
437                     "FROM com.liferay.portlet.messageboards.model.MBThread WHERE ");
438 
439                 query.append("groupId = ?");
440 
441                 query.append(" ");
442 
443                 if (obc != null) {
444                     query.append("ORDER BY ");
445                     query.append(obc.getOrderBy());
446                 }
447 
448                 else {
449                     query.append("ORDER BY ");
450 
451                     query.append("priority DESC, ");
452                     query.append("lastPostDate DESC");
453                 }
454 
455                 Query q = session.createQuery(query.toString());
456 
457                 QueryPos qPos = QueryPos.getInstance(q);
458 
459                 qPos.add(groupId);
460 
461                 list = (List<MBThread>)QueryUtil.list(q, getDialect(), start,
462                         end);
463             }
464             catch (Exception e) {
465                 throw processException(e);
466             }
467             finally {
468                 if (list == null) {
469                     list = new ArrayList<MBThread>();
470                 }
471 
472                 cacheResult(list);
473 
474                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_OBC_GROUPID,
475                     finderArgs, list);
476 
477                 closeSession(session);
478             }
479         }
480 
481         return list;
482     }
483 
484     public MBThread findByGroupId_First(long groupId, OrderByComparator obc)
485         throws NoSuchThreadException, SystemException {
486         List<MBThread> list = findByGroupId(groupId, 0, 1, obc);
487 
488         if (list.isEmpty()) {
489             StringBuilder msg = new StringBuilder();
490 
491             msg.append("No MBThread exists with the key {");
492 
493             msg.append("groupId=" + groupId);
494 
495             msg.append(StringPool.CLOSE_CURLY_BRACE);
496 
497             throw new NoSuchThreadException(msg.toString());
498         }
499         else {
500             return list.get(0);
501         }
502     }
503 
504     public MBThread findByGroupId_Last(long groupId, OrderByComparator obc)
505         throws NoSuchThreadException, SystemException {
506         int count = countByGroupId(groupId);
507 
508         List<MBThread> list = findByGroupId(groupId, count - 1, count, obc);
509 
510         if (list.isEmpty()) {
511             StringBuilder msg = new StringBuilder();
512 
513             msg.append("No MBThread exists with the key {");
514 
515             msg.append("groupId=" + groupId);
516 
517             msg.append(StringPool.CLOSE_CURLY_BRACE);
518 
519             throw new NoSuchThreadException(msg.toString());
520         }
521         else {
522             return list.get(0);
523         }
524     }
525 
526     public MBThread[] findByGroupId_PrevAndNext(long threadId, long groupId,
527         OrderByComparator obc) throws NoSuchThreadException, SystemException {
528         MBThread mbThread = findByPrimaryKey(threadId);
529 
530         int count = countByGroupId(groupId);
531 
532         Session session = null;
533 
534         try {
535             session = openSession();
536 
537             StringBuilder query = new StringBuilder();
538 
539             query.append(
540                 "FROM com.liferay.portlet.messageboards.model.MBThread WHERE ");
541 
542             query.append("groupId = ?");
543 
544             query.append(" ");
545 
546             if (obc != null) {
547                 query.append("ORDER BY ");
548                 query.append(obc.getOrderBy());
549             }
550 
551             else {
552                 query.append("ORDER BY ");
553 
554                 query.append("priority DESC, ");
555                 query.append("lastPostDate DESC");
556             }
557 
558             Query q = session.createQuery(query.toString());
559 
560             QueryPos qPos = QueryPos.getInstance(q);
561 
562             qPos.add(groupId);
563 
564             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc, mbThread);
565 
566             MBThread[] array = new MBThreadImpl[3];
567 
568             array[0] = (MBThread)objArray[0];
569             array[1] = (MBThread)objArray[1];
570             array[2] = (MBThread)objArray[2];
571 
572             return array;
573         }
574         catch (Exception e) {
575             throw processException(e);
576         }
577         finally {
578             closeSession(session);
579         }
580     }
581 
582     public List<MBThread> findByCategoryId(long categoryId)
583         throws SystemException {
584         Object[] finderArgs = new Object[] { new Long(categoryId) };
585 
586         List<MBThread> list = (List<MBThread>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_CATEGORYID,
587                 finderArgs, this);
588 
589         if (list == null) {
590             Session session = null;
591 
592             try {
593                 session = openSession();
594 
595                 StringBuilder query = new StringBuilder();
596 
597                 query.append(
598                     "FROM com.liferay.portlet.messageboards.model.MBThread WHERE ");
599 
600                 query.append("categoryId = ?");
601 
602                 query.append(" ");
603 
604                 query.append("ORDER BY ");
605 
606                 query.append("priority DESC, ");
607                 query.append("lastPostDate DESC");
608 
609                 Query q = session.createQuery(query.toString());
610 
611                 QueryPos qPos = QueryPos.getInstance(q);
612 
613                 qPos.add(categoryId);
614 
615                 list = q.list();
616             }
617             catch (Exception e) {
618                 throw processException(e);
619             }
620             finally {
621                 if (list == null) {
622                     list = new ArrayList<MBThread>();
623                 }
624 
625                 cacheResult(list);
626 
627                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_CATEGORYID,
628                     finderArgs, list);
629 
630                 closeSession(session);
631             }
632         }
633 
634         return list;
635     }
636 
637     public List<MBThread> findByCategoryId(long categoryId, int start, int end)
638         throws SystemException {
639         return findByCategoryId(categoryId, start, end, null);
640     }
641 
642     public List<MBThread> findByCategoryId(long categoryId, int start, int end,
643         OrderByComparator obc) throws SystemException {
644         Object[] finderArgs = new Object[] {
645                 new Long(categoryId),
646                 
647                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
648             };
649 
650         List<MBThread> list = (List<MBThread>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_OBC_CATEGORYID,
651                 finderArgs, this);
652 
653         if (list == null) {
654             Session session = null;
655 
656             try {
657                 session = openSession();
658 
659                 StringBuilder query = new StringBuilder();
660 
661                 query.append(
662                     "FROM com.liferay.portlet.messageboards.model.MBThread WHERE ");
663 
664                 query.append("categoryId = ?");
665 
666                 query.append(" ");
667 
668                 if (obc != null) {
669                     query.append("ORDER BY ");
670                     query.append(obc.getOrderBy());
671                 }
672 
673                 else {
674                     query.append("ORDER BY ");
675 
676                     query.append("priority DESC, ");
677                     query.append("lastPostDate DESC");
678                 }
679 
680                 Query q = session.createQuery(query.toString());
681 
682                 QueryPos qPos = QueryPos.getInstance(q);
683 
684                 qPos.add(categoryId);
685 
686                 list = (List<MBThread>)QueryUtil.list(q, getDialect(), start,
687                         end);
688             }
689             catch (Exception e) {
690                 throw processException(e);
691             }
692             finally {
693                 if (list == null) {
694                     list = new ArrayList<MBThread>();
695                 }
696 
697                 cacheResult(list);
698 
699                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_OBC_CATEGORYID,
700                     finderArgs, list);
701 
702                 closeSession(session);
703             }
704         }
705 
706         return list;
707     }
708 
709     public MBThread findByCategoryId_First(long categoryId,
710         OrderByComparator obc) throws NoSuchThreadException, SystemException {
711         List<MBThread> list = findByCategoryId(categoryId, 0, 1, obc);
712 
713         if (list.isEmpty()) {
714             StringBuilder msg = new StringBuilder();
715 
716             msg.append("No MBThread exists with the key {");
717 
718             msg.append("categoryId=" + categoryId);
719 
720             msg.append(StringPool.CLOSE_CURLY_BRACE);
721 
722             throw new NoSuchThreadException(msg.toString());
723         }
724         else {
725             return list.get(0);
726         }
727     }
728 
729     public MBThread findByCategoryId_Last(long categoryId, OrderByComparator obc)
730         throws NoSuchThreadException, SystemException {
731         int count = countByCategoryId(categoryId);
732 
733         List<MBThread> list = findByCategoryId(categoryId, count - 1, count, obc);
734 
735         if (list.isEmpty()) {
736             StringBuilder msg = new StringBuilder();
737 
738             msg.append("No MBThread exists with the key {");
739 
740             msg.append("categoryId=" + categoryId);
741 
742             msg.append(StringPool.CLOSE_CURLY_BRACE);
743 
744             throw new NoSuchThreadException(msg.toString());
745         }
746         else {
747             return list.get(0);
748         }
749     }
750 
751     public MBThread[] findByCategoryId_PrevAndNext(long threadId,
752         long categoryId, OrderByComparator obc)
753         throws NoSuchThreadException, SystemException {
754         MBThread mbThread = findByPrimaryKey(threadId);
755 
756         int count = countByCategoryId(categoryId);
757 
758         Session session = null;
759 
760         try {
761             session = openSession();
762 
763             StringBuilder query = new StringBuilder();
764 
765             query.append(
766                 "FROM com.liferay.portlet.messageboards.model.MBThread WHERE ");
767 
768             query.append("categoryId = ?");
769 
770             query.append(" ");
771 
772             if (obc != null) {
773                 query.append("ORDER BY ");
774                 query.append(obc.getOrderBy());
775             }
776 
777             else {
778                 query.append("ORDER BY ");
779 
780                 query.append("priority DESC, ");
781                 query.append("lastPostDate DESC");
782             }
783 
784             Query q = session.createQuery(query.toString());
785 
786             QueryPos qPos = QueryPos.getInstance(q);
787 
788             qPos.add(categoryId);
789 
790             Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc, mbThread);
791 
792             MBThread[] array = new MBThreadImpl[3];
793 
794             array[0] = (MBThread)objArray[0];
795             array[1] = (MBThread)objArray[1];
796             array[2] = (MBThread)objArray[2];
797 
798             return array;
799         }
800         catch (Exception e) {
801             throw processException(e);
802         }
803         finally {
804             closeSession(session);
805         }
806     }
807 
808     public List<MBThread> findByC_L(long categoryId, Date lastPostDate)
809         throws SystemException {
810         Object[] finderArgs = new Object[] { new Long(categoryId), lastPostDate };
811 
812         List<MBThread> list = (List<MBThread>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_C_L,
813                 finderArgs, this);
814 
815         if (list == null) {
816             Session session = null;
817 
818             try {
819                 session = openSession();
820 
821                 StringBuilder query = new StringBuilder();
822 
823                 query.append(
824                     "FROM com.liferay.portlet.messageboards.model.MBThread WHERE ");
825 
826                 query.append("categoryId = ?");
827 
828                 query.append(" AND ");
829 
830                 if (lastPostDate == null) {
831                     query.append("lastPostDate IS NULL");
832                 }
833                 else {
834                     query.append("lastPostDate = ?");
835                 }
836 
837                 query.append(" ");
838 
839                 query.append("ORDER BY ");
840 
841                 query.append("priority DESC, ");
842                 query.append("lastPostDate DESC");
843 
844                 Query q = session.createQuery(query.toString());
845 
846                 QueryPos qPos = QueryPos.getInstance(q);
847 
848                 qPos.add(categoryId);
849 
850                 if (lastPostDate != null) {
851                     qPos.add(CalendarUtil.getTimestamp(lastPostDate));
852                 }
853 
854                 list = q.list();
855             }
856             catch (Exception e) {
857                 throw processException(e);
858             }
859             finally {
860                 if (list == null) {
861                     list = new ArrayList<MBThread>();
862                 }
863 
864                 cacheResult(list);
865 
866                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_C_L, finderArgs,
867                     list);
868 
869                 closeSession(session);
870             }
871         }
872 
873         return list;
874     }
875 
876     public List<MBThread> findByC_L(long categoryId, Date lastPostDate,
877         int start, int end) throws SystemException {
878         return findByC_L(categoryId, lastPostDate, start, end, null);
879     }
880 
881     public List<MBThread> findByC_L(long categoryId, Date lastPostDate,
882         int start, int end, OrderByComparator obc) throws SystemException {
883         Object[] finderArgs = new Object[] {
884                 new Long(categoryId),
885                 
886                 lastPostDate,
887                 
888                 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
889             };
890 
891         List<MBThread> list = (List<MBThread>)FinderCacheUtil.getResult(FINDER_PATH_FIND_BY_OBC_C_L,
892                 finderArgs, this);
893 
894         if (list == null) {
895             Session session = null;
896 
897             try {
898                 session = openSession();
899 
900                 StringBuilder query = new StringBuilder();
901 
902                 query.append(
903                     "FROM com.liferay.portlet.messageboards.model.MBThread WHERE ");
904 
905                 query.append("categoryId = ?");
906 
907                 query.append(" AND ");
908 
909                 if (lastPostDate == null) {
910                     query.append("lastPostDate IS NULL");
911                 }
912                 else {
913                     query.append("lastPostDate = ?");
914                 }
915 
916                 query.append(" ");
917 
918                 if (obc != null) {
919                     query.append("ORDER BY ");
920                     query.append(obc.getOrderBy());
921                 }
922 
923                 else {
924                     query.append("ORDER BY ");
925 
926                     query.append("priority DESC, ");
927                     query.append("lastPostDate DESC");
928                 }
929 
930                 Query q = session.createQuery(query.toString());
931 
932                 QueryPos qPos = QueryPos.getInstance(q);
933 
934                 qPos.add(categoryId);
935 
936                 if (lastPostDate != null) {
937                     qPos.add(CalendarUtil.getTimestamp(lastPostDate));
938                 }
939 
940                 list = (List<MBThread>)QueryUtil.list(q, getDialect(), start,
941                         end);
942             }
943             catch (Exception e) {
944                 throw processException(e);
945             }
946             finally {
947                 if (list == null) {
948                     list = new ArrayList<MBThread>();
949                 }
950 
951                 cacheResult(list);
952 
953                 FinderCacheUtil.putResult(FINDER_PATH_FIND_BY_OBC_C_L,
954                     finderArgs, list);
955 
956                 closeSession(session);
957             }
958         }
959 
960         return list;
961     }
962 
963     public MBThread findByC_L_First(long categoryId, Date lastPostDate,
964         OrderByComparator obc) throws NoSuchThreadException, SystemException {
965         List<MBThread> list = findByC_L(categoryId, lastPostDate, 0, 1, obc);
966 
967         if (list.isEmpty()) {
968             StringBuilder msg = new StringBuilder();
969 
970             msg.append("No MBThread exists with the key {");
971 
972             msg.append("categoryId=" + categoryId);
973 
974             msg.append(", ");
975             msg.append("lastPostDate=" + lastPostDate);
976 
977             msg.append(StringPool.CLOSE_CURLY_BRACE);
978 
979             throw new NoSuchThreadException(msg.toString());
980         }
981         else {
982             return list.get(0);
983         }
984     }
985 
986     public MBThread findByC_L_Last(long categoryId, Date lastPostDate,
987         OrderByComparator obc) throws NoSuchThreadException, SystemException {
988         int count = countByC_L(categoryId, lastPostDate);
989 
990         List<MBThread> list = findByC_L(categoryId, lastPostDate, count - 1,
991                 count, obc);
992 
993         if (list.isEmpty()) {
994             StringBuilder msg = new StringBuilder();
995 
996             msg.append("No MBThread exists with the key {");
997 
998             msg.append("categoryId=" + categoryId);
999 
1000            msg.append(", ");
1001            msg.append("lastPostDate=" + lastPostDate);
1002
1003            msg.append(StringPool.CLOSE_CURLY_BRACE);
1004
1005            throw new NoSuchThreadException(msg.toString());
1006        }
1007        else {
1008            return list.get(0);
1009        }
1010    }
1011
1012    public MBThread[] findByC_L_PrevAndNext(long threadId, long categoryId,
1013        Date lastPostDate, OrderByComparator obc)
1014        throws NoSuchThreadException, SystemException {
1015        MBThread mbThread = findByPrimaryKey(threadId);
1016
1017        int count = countByC_L(categoryId, lastPostDate);
1018
1019        Session session = null;
1020
1021        try {
1022            session = openSession();
1023
1024            StringBuilder query = new StringBuilder();
1025
1026            query.append(
1027                "FROM com.liferay.portlet.messageboards.model.MBThread WHERE ");
1028
1029            query.append("categoryId = ?");
1030
1031            query.append(" AND ");
1032
1033            if (lastPostDate == null) {
1034                query.append("lastPostDate IS NULL");
1035            }
1036            else {
1037                query.append("lastPostDate = ?");
1038            }
1039
1040            query.append(" ");
1041
1042            if (obc != null) {
1043                query.append("ORDER BY ");
1044                query.append(obc.getOrderBy());
1045            }
1046
1047            else {
1048                query.append("ORDER BY ");
1049
1050                query.append("priority DESC, ");
1051                query.append("lastPostDate DESC");
1052            }
1053
1054            Query q = session.createQuery(query.toString());
1055
1056            QueryPos qPos = QueryPos.getInstance(q);
1057
1058            qPos.add(categoryId);
1059
1060            if (lastPostDate != null) {
1061                qPos.add(CalendarUtil.getTimestamp(lastPostDate));
1062            }
1063
1064            Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc, mbThread);
1065
1066            MBThread[] array = new MBThreadImpl[3];
1067
1068            array[0] = (MBThread)objArray[0];
1069            array[1] = (MBThread)objArray[1];
1070            array[2] = (MBThread)objArray[2];
1071
1072            return array;
1073        }
1074        catch (Exception e) {
1075            throw processException(e);
1076        }
1077        finally {
1078            closeSession(session);
1079        }
1080    }
1081
1082    public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
1083        throws SystemException {
1084        Session session = null;
1085
1086        try {
1087            session = openSession();
1088
1089            dynamicQuery.compile(session);
1090
1091            return dynamicQuery.list();
1092        }
1093        catch (Exception e) {
1094            throw processException(e);
1095        }
1096        finally {
1097            closeSession(session);
1098        }
1099    }
1100
1101    public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
1102        int start, int end) throws SystemException {
1103        Session session = null;
1104
1105        try {
1106            session = openSession();
1107
1108            dynamicQuery.setLimit(start, end);
1109
1110            dynamicQuery.compile(session);
1111
1112            return dynamicQuery.list();
1113        }
1114        catch (Exception e) {
1115            throw processException(e);
1116        }
1117        finally {
1118            closeSession(session);
1119        }
1120    }
1121
1122    public List<MBThread> findAll() throws SystemException {
1123        return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
1124    }
1125
1126    public List<MBThread> findAll(int start, int end) throws SystemException {
1127        return findAll(start, end, null);
1128    }
1129
1130    public List<MBThread> findAll(int start, int end, OrderByComparator obc)
1131        throws SystemException {
1132        Object[] finderArgs = new Object[] {
1133                String.valueOf(start), String.valueOf(end), String.valueOf(obc)
1134            };
1135
1136        List<MBThread> list = (List<MBThread>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
1137                finderArgs, this);
1138
1139        if (list == null) {
1140            Session session = null;
1141
1142            try {
1143                session = openSession();
1144
1145                StringBuilder query = new StringBuilder();
1146
1147                query.append(
1148                    "FROM com.liferay.portlet.messageboards.model.MBThread ");
1149
1150                if (obc != null) {
1151                    query.append("ORDER BY ");
1152                    query.append(obc.getOrderBy());
1153                }
1154
1155                else {
1156                    query.append("ORDER BY ");
1157
1158                    query.append("priority DESC, ");
1159                    query.append("lastPostDate DESC");
1160                }
1161
1162                Query q = session.createQuery(query.toString());
1163
1164                if (obc == null) {
1165                    list = (List<MBThread>)QueryUtil.list(q, getDialect(),
1166                            start, end, false);
1167
1168                    Collections.sort(list);
1169                }
1170                else {
1171                    list = (List<MBThread>)QueryUtil.list(q, getDialect(),
1172                            start, end);
1173                }
1174            }
1175            catch (Exception e) {
1176                throw processException(e);
1177            }
1178            finally {
1179                if (list == null) {
1180                    list = new ArrayList<MBThread>();
1181                }
1182
1183                cacheResult(list);
1184
1185                FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
1186
1187                closeSession(session);
1188            }
1189        }
1190
1191        return list;
1192    }
1193
1194    public void removeByGroupId(long groupId) throws SystemException {
1195        for (MBThread mbThread : findByGroupId(groupId)) {
1196            remove(mbThread);
1197        }
1198    }
1199
1200    public void removeByCategoryId(long categoryId) throws SystemException {
1201        for (MBThread mbThread : findByCategoryId(categoryId)) {
1202            remove(mbThread);
1203        }
1204    }
1205
1206    public void removeByC_L(long categoryId, Date lastPostDate)
1207        throws SystemException {
1208        for (MBThread mbThread : findByC_L(categoryId, lastPostDate)) {
1209            remove(mbThread);
1210        }
1211    }
1212
1213    public void removeAll() throws SystemException {
1214        for (MBThread mbThread : findAll()) {
1215            remove(mbThread);
1216        }
1217    }
1218
1219    public int countByGroupId(long groupId) throws SystemException {
1220        Object[] finderArgs = new Object[] { new Long(groupId) };
1221
1222        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_GROUPID,
1223                finderArgs, this);
1224
1225        if (count == null) {
1226            Session session = null;
1227
1228            try {
1229                session = openSession();
1230
1231                StringBuilder query = new StringBuilder();
1232
1233                query.append("SELECT COUNT(*) ");
1234                query.append(
1235                    "FROM com.liferay.portlet.messageboards.model.MBThread WHERE ");
1236
1237                query.append("groupId = ?");
1238
1239                query.append(" ");
1240
1241                Query q = session.createQuery(query.toString());
1242
1243                QueryPos qPos = QueryPos.getInstance(q);
1244
1245                qPos.add(groupId);
1246
1247                count = (Long)q.uniqueResult();
1248            }
1249            catch (Exception e) {
1250                throw processException(e);
1251            }
1252            finally {
1253                if (count == null) {
1254                    count = Long.valueOf(0);
1255                }
1256
1257                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_GROUPID,
1258                    finderArgs, count);
1259
1260                closeSession(session);
1261            }
1262        }
1263
1264        return count.intValue();
1265    }
1266
1267    public int countByCategoryId(long categoryId) throws SystemException {
1268        Object[] finderArgs = new Object[] { new Long(categoryId) };
1269
1270        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_CATEGORYID,
1271                finderArgs, this);
1272
1273        if (count == null) {
1274            Session session = null;
1275
1276            try {
1277                session = openSession();
1278
1279                StringBuilder query = new StringBuilder();
1280
1281                query.append("SELECT COUNT(*) ");
1282                query.append(
1283                    "FROM com.liferay.portlet.messageboards.model.MBThread WHERE ");
1284
1285                query.append("categoryId = ?");
1286
1287                query.append(" ");
1288
1289                Query q = session.createQuery(query.toString());
1290
1291                QueryPos qPos = QueryPos.getInstance(q);
1292
1293                qPos.add(categoryId);
1294
1295                count = (Long)q.uniqueResult();
1296            }
1297            catch (Exception e) {
1298                throw processException(e);
1299            }
1300            finally {
1301                if (count == null) {
1302                    count = Long.valueOf(0);
1303                }
1304
1305                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_CATEGORYID,
1306                    finderArgs, count);
1307
1308                closeSession(session);
1309            }
1310        }
1311
1312        return count.intValue();
1313    }
1314
1315    public int countByC_L(long categoryId, Date lastPostDate)
1316        throws SystemException {
1317        Object[] finderArgs = new Object[] { new Long(categoryId), lastPostDate };
1318
1319        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_C_L,
1320                finderArgs, this);
1321
1322        if (count == null) {
1323            Session session = null;
1324
1325            try {
1326                session = openSession();
1327
1328                StringBuilder query = new StringBuilder();
1329
1330                query.append("SELECT COUNT(*) ");
1331                query.append(
1332                    "FROM com.liferay.portlet.messageboards.model.MBThread WHERE ");
1333
1334                query.append("categoryId = ?");
1335
1336                query.append(" AND ");
1337
1338                if (lastPostDate == null) {
1339                    query.append("lastPostDate IS NULL");
1340                }
1341                else {
1342                    query.append("lastPostDate = ?");
1343                }
1344
1345                query.append(" ");
1346
1347                Query q = session.createQuery(query.toString());
1348
1349                QueryPos qPos = QueryPos.getInstance(q);
1350
1351                qPos.add(categoryId);
1352
1353                if (lastPostDate != null) {
1354                    qPos.add(CalendarUtil.getTimestamp(lastPostDate));
1355                }
1356
1357                count = (Long)q.uniqueResult();
1358            }
1359            catch (Exception e) {
1360                throw processException(e);
1361            }
1362            finally {
1363                if (count == null) {
1364                    count = Long.valueOf(0);
1365                }
1366
1367                FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_L, finderArgs,
1368                    count);
1369
1370                closeSession(session);
1371            }
1372        }
1373
1374        return count.intValue();
1375    }
1376
1377    public int countAll() throws SystemException {
1378        Object[] finderArgs = new Object[0];
1379
1380        Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
1381                finderArgs, this);
1382
1383        if (count == null) {
1384            Session session = null;
1385
1386            try {
1387                session = openSession();
1388
1389                Query q = session.createQuery(
1390                        "SELECT COUNT(*) FROM com.liferay.portlet.messageboards.model.MBThread");
1391
1392                count = (Long)q.uniqueResult();
1393            }
1394            catch (Exception e) {
1395                throw processException(e);
1396            }
1397            finally {
1398                if (count == null) {
1399                    count = Long.valueOf(0);
1400                }
1401
1402                FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
1403                    count);
1404
1405                closeSession(session);
1406            }
1407        }
1408
1409        return count.intValue();
1410    }
1411
1412    public void afterPropertiesSet() {
1413        String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
1414                    com.liferay.portal.util.PropsUtil.get(
1415                        "value.object.listener.com.liferay.portlet.messageboards.model.MBThread")));
1416
1417        if (listenerClassNames.length > 0) {
1418            try {
1419                List<ModelListener<MBThread>> listenersList = new ArrayList<ModelListener<MBThread>>();
1420
1421                for (String listenerClassName : listenerClassNames) {
1422                    listenersList.add((ModelListener<MBThread>)Class.forName(
1423                            listenerClassName).newInstance());
1424                }
1425
1426                listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
1427            }
1428            catch (Exception e) {
1429                _log.error(e);
1430            }
1431        }
1432    }
1433
1434    @BeanReference(name = "com.liferay.portlet.messageboards.service.persistence.MBBanPersistence.impl")
1435    protected com.liferay.portlet.messageboards.service.persistence.MBBanPersistence mbBanPersistence;
1436    @BeanReference(name = "com.liferay.portlet.messageboards.service.persistence.MBCategoryPersistence.impl")
1437    protected com.liferay.portlet.messageboards.service.persistence.MBCategoryPersistence mbCategoryPersistence;
1438    @BeanReference(name = "com.liferay.portlet.messageboards.service.persistence.MBDiscussionPersistence.impl")
1439    protected com.liferay.portlet.messageboards.service.persistence.MBDiscussionPersistence mbDiscussionPersistence;
1440    @BeanReference(name = "com.liferay.portlet.messageboards.service.persistence.MBMailingListPersistence.impl")
1441    protected com.liferay.portlet.messageboards.service.persistence.MBMailingListPersistence mbMailingListPersistence;
1442    @BeanReference(name = "com.liferay.portlet.messageboards.service.persistence.MBMessagePersistence.impl")
1443    protected com.liferay.portlet.messageboards.service.persistence.MBMessagePersistence mbMessagePersistence;
1444    @BeanReference(name = "com.liferay.portlet.messageboards.service.persistence.MBMessageFlagPersistence.impl")
1445    protected com.liferay.portlet.messageboards.service.persistence.MBMessageFlagPersistence mbMessageFlagPersistence;
1446    @BeanReference(name = "com.liferay.portlet.messageboards.service.persistence.MBStatsUserPersistence.impl")
1447    protected com.liferay.portlet.messageboards.service.persistence.MBStatsUserPersistence mbStatsUserPersistence;
1448    @BeanReference(name = "com.liferay.portlet.messageboards.service.persistence.MBThreadPersistence.impl")
1449    protected com.liferay.portlet.messageboards.service.persistence.MBThreadPersistence mbThreadPersistence;
1450    @BeanReference(name = "com.liferay.portal.service.persistence.ResourcePersistence.impl")
1451    protected com.liferay.portal.service.persistence.ResourcePersistence resourcePersistence;
1452    @BeanReference(name = "com.liferay.portal.service.persistence.UserPersistence.impl")
1453    protected com.liferay.portal.service.persistence.UserPersistence userPersistence;
1454    @BeanReference(name = "com.liferay.portlet.ratings.service.persistence.RatingsStatsPersistence.impl")
1455    protected com.liferay.portlet.ratings.service.persistence.RatingsStatsPersistence ratingsStatsPersistence;
1456    @BeanReference(name = "com.liferay.portlet.social.service.persistence.SocialActivityPersistence.impl")
1457    protected com.liferay.portlet.social.service.persistence.SocialActivityPersistence socialActivityPersistence;
1458    @BeanReference(name = "com.liferay.portlet.tags.service.persistence.TagsAssetPersistence.impl")
1459    protected com.liferay.portlet.tags.service.persistence.TagsAssetPersistence tagsAssetPersistence;
1460    private static Log _log = LogFactoryUtil.getLog(MBThreadPersistenceImpl.class);
1461}