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