1
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.dao.orm.DynamicQuery;
28 import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
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.GetterUtil;
36 import com.liferay.portal.kernel.util.OrderByComparator;
37 import com.liferay.portal.kernel.util.StringPool;
38 import com.liferay.portal.kernel.util.StringUtil;
39 import com.liferay.portal.model.ModelListener;
40 import com.liferay.portal.service.persistence.BatchSessionUtil;
41 import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
42
43 import com.liferay.portlet.messageboards.NoSuchThreadException;
44 import com.liferay.portlet.messageboards.model.MBThread;
45 import com.liferay.portlet.messageboards.model.impl.MBThreadImpl;
46 import com.liferay.portlet.messageboards.model.impl.MBThreadModelImpl;
47
48 import java.util.ArrayList;
49 import java.util.Collections;
50 import java.util.Iterator;
51 import java.util.List;
52
53
59 public class MBThreadPersistenceImpl extends BasePersistenceImpl
60 implements MBThreadPersistence {
61 public MBThread create(long threadId) {
62 MBThread mbThread = new MBThreadImpl();
63
64 mbThread.setNew(true);
65 mbThread.setPrimaryKey(threadId);
66
67 return mbThread;
68 }
69
70 public MBThread remove(long threadId)
71 throws NoSuchThreadException, SystemException {
72 Session session = null;
73
74 try {
75 session = openSession();
76
77 MBThread mbThread = (MBThread)session.get(MBThreadImpl.class,
78 new Long(threadId));
79
80 if (mbThread == null) {
81 if (_log.isWarnEnabled()) {
82 _log.warn("No MBThread exists with the primary key " +
83 threadId);
84 }
85
86 throw new NoSuchThreadException(
87 "No MBThread exists with the primary key " + threadId);
88 }
89
90 return remove(mbThread);
91 }
92 catch (NoSuchThreadException nsee) {
93 throw nsee;
94 }
95 catch (Exception e) {
96 throw processException(e);
97 }
98 finally {
99 closeSession(session);
100 }
101 }
102
103 public MBThread remove(MBThread mbThread) throws SystemException {
104 for (ModelListener listener : listeners) {
105 listener.onBeforeRemove(mbThread);
106 }
107
108 mbThread = removeImpl(mbThread);
109
110 for (ModelListener listener : listeners) {
111 listener.onAfterRemove(mbThread);
112 }
113
114 return mbThread;
115 }
116
117 protected MBThread removeImpl(MBThread mbThread) throws SystemException {
118 Session session = null;
119
120 try {
121 session = openSession();
122
123 if (BatchSessionUtil.isEnabled()) {
124 Object staleObject = session.get(MBThreadImpl.class,
125 mbThread.getPrimaryKeyObj());
126
127 if (staleObject != null) {
128 session.evict(staleObject);
129 }
130 }
131
132 session.delete(mbThread);
133
134 session.flush();
135
136 return mbThread;
137 }
138 catch (Exception e) {
139 throw processException(e);
140 }
141 finally {
142 closeSession(session);
143
144 FinderCacheUtil.clearCache(MBThread.class.getName());
145 }
146 }
147
148
151 public MBThread update(MBThread mbThread) throws SystemException {
152 if (_log.isWarnEnabled()) {
153 _log.warn(
154 "Using the deprecated update(MBThread mbThread) method. Use update(MBThread mbThread, boolean merge) instead.");
155 }
156
157 return update(mbThread, false);
158 }
159
160
173 public MBThread update(MBThread mbThread, boolean merge)
174 throws SystemException {
175 boolean isNew = mbThread.isNew();
176
177 for (ModelListener listener : listeners) {
178 if (isNew) {
179 listener.onBeforeCreate(mbThread);
180 }
181 else {
182 listener.onBeforeUpdate(mbThread);
183 }
184 }
185
186 mbThread = updateImpl(mbThread, merge);
187
188 for (ModelListener listener : listeners) {
189 if (isNew) {
190 listener.onAfterCreate(mbThread);
191 }
192 else {
193 listener.onAfterUpdate(mbThread);
194 }
195 }
196
197 return mbThread;
198 }
199
200 public MBThread updateImpl(
201 com.liferay.portlet.messageboards.model.MBThread mbThread, boolean merge)
202 throws SystemException {
203 Session session = null;
204
205 try {
206 session = openSession();
207
208 BatchSessionUtil.update(session, mbThread, merge);
209
210 mbThread.setNew(false);
211
212 return mbThread;
213 }
214 catch (Exception e) {
215 throw processException(e);
216 }
217 finally {
218 closeSession(session);
219
220 FinderCacheUtil.clearCache(MBThread.class.getName());
221 }
222 }
223
224 public MBThread findByPrimaryKey(long threadId)
225 throws NoSuchThreadException, SystemException {
226 MBThread mbThread = fetchByPrimaryKey(threadId);
227
228 if (mbThread == null) {
229 if (_log.isWarnEnabled()) {
230 _log.warn("No MBThread exists with the primary key " +
231 threadId);
232 }
233
234 throw new NoSuchThreadException(
235 "No MBThread exists with the primary key " + threadId);
236 }
237
238 return mbThread;
239 }
240
241 public MBThread fetchByPrimaryKey(long threadId) throws SystemException {
242 Session session = null;
243
244 try {
245 session = openSession();
246
247 return (MBThread)session.get(MBThreadImpl.class, new Long(threadId));
248 }
249 catch (Exception e) {
250 throw processException(e);
251 }
252 finally {
253 closeSession(session);
254 }
255 }
256
257 public List<MBThread> findByCategoryId(long categoryId)
258 throws SystemException {
259 boolean finderClassNameCacheEnabled = MBThreadModelImpl.CACHE_ENABLED;
260 String finderClassName = MBThread.class.getName();
261 String finderMethodName = "findByCategoryId";
262 String[] finderParams = new String[] { Long.class.getName() };
263 Object[] finderArgs = new Object[] { new Long(categoryId) };
264
265 Object result = null;
266
267 if (finderClassNameCacheEnabled) {
268 result = FinderCacheUtil.getResult(finderClassName,
269 finderMethodName, finderParams, finderArgs, this);
270 }
271
272 if (result == null) {
273 Session session = null;
274
275 try {
276 session = openSession();
277
278 StringBuilder query = new StringBuilder();
279
280 query.append(
281 "FROM com.liferay.portlet.messageboards.model.MBThread WHERE ");
282
283 query.append("categoryId = ?");
284
285 query.append(" ");
286
287 query.append("ORDER BY ");
288
289 query.append("priority DESC, ");
290 query.append("lastPostDate DESC");
291
292 Query q = session.createQuery(query.toString());
293
294 QueryPos qPos = QueryPos.getInstance(q);
295
296 qPos.add(categoryId);
297
298 List<MBThread> list = q.list();
299
300 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
301 finderClassName, finderMethodName, finderParams,
302 finderArgs, list);
303
304 return list;
305 }
306 catch (Exception e) {
307 throw processException(e);
308 }
309 finally {
310 closeSession(session);
311 }
312 }
313 else {
314 return (List<MBThread>)result;
315 }
316 }
317
318 public List<MBThread> findByCategoryId(long categoryId, int start, int end)
319 throws SystemException {
320 return findByCategoryId(categoryId, start, end, null);
321 }
322
323 public List<MBThread> findByCategoryId(long categoryId, int start, int end,
324 OrderByComparator obc) throws SystemException {
325 boolean finderClassNameCacheEnabled = MBThreadModelImpl.CACHE_ENABLED;
326 String finderClassName = MBThread.class.getName();
327 String finderMethodName = "findByCategoryId";
328 String[] finderParams = new String[] {
329 Long.class.getName(),
330
331 "java.lang.Integer", "java.lang.Integer",
332 "com.liferay.portal.kernel.util.OrderByComparator"
333 };
334 Object[] finderArgs = new Object[] {
335 new Long(categoryId),
336
337 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
338 };
339
340 Object result = null;
341
342 if (finderClassNameCacheEnabled) {
343 result = FinderCacheUtil.getResult(finderClassName,
344 finderMethodName, finderParams, finderArgs, this);
345 }
346
347 if (result == null) {
348 Session session = null;
349
350 try {
351 session = openSession();
352
353 StringBuilder query = new StringBuilder();
354
355 query.append(
356 "FROM com.liferay.portlet.messageboards.model.MBThread WHERE ");
357
358 query.append("categoryId = ?");
359
360 query.append(" ");
361
362 if (obc != null) {
363 query.append("ORDER BY ");
364 query.append(obc.getOrderBy());
365 }
366
367 else {
368 query.append("ORDER BY ");
369
370 query.append("priority DESC, ");
371 query.append("lastPostDate DESC");
372 }
373
374 Query q = session.createQuery(query.toString());
375
376 QueryPos qPos = QueryPos.getInstance(q);
377
378 qPos.add(categoryId);
379
380 List<MBThread> list = (List<MBThread>)QueryUtil.list(q,
381 getDialect(), start, end);
382
383 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
384 finderClassName, finderMethodName, finderParams,
385 finderArgs, list);
386
387 return list;
388 }
389 catch (Exception e) {
390 throw processException(e);
391 }
392 finally {
393 closeSession(session);
394 }
395 }
396 else {
397 return (List<MBThread>)result;
398 }
399 }
400
401 public MBThread findByCategoryId_First(long categoryId,
402 OrderByComparator obc) throws NoSuchThreadException, SystemException {
403 List<MBThread> list = findByCategoryId(categoryId, 0, 1, obc);
404
405 if (list.size() == 0) {
406 StringBuilder msg = new StringBuilder();
407
408 msg.append("No MBThread exists with the key {");
409
410 msg.append("categoryId=" + categoryId);
411
412 msg.append(StringPool.CLOSE_CURLY_BRACE);
413
414 throw new NoSuchThreadException(msg.toString());
415 }
416 else {
417 return list.get(0);
418 }
419 }
420
421 public MBThread findByCategoryId_Last(long categoryId, OrderByComparator obc)
422 throws NoSuchThreadException, SystemException {
423 int count = countByCategoryId(categoryId);
424
425 List<MBThread> list = findByCategoryId(categoryId, count - 1, count, obc);
426
427 if (list.size() == 0) {
428 StringBuilder msg = new StringBuilder();
429
430 msg.append("No MBThread exists with the key {");
431
432 msg.append("categoryId=" + categoryId);
433
434 msg.append(StringPool.CLOSE_CURLY_BRACE);
435
436 throw new NoSuchThreadException(msg.toString());
437 }
438 else {
439 return list.get(0);
440 }
441 }
442
443 public MBThread[] findByCategoryId_PrevAndNext(long threadId,
444 long categoryId, OrderByComparator obc)
445 throws NoSuchThreadException, SystemException {
446 MBThread mbThread = findByPrimaryKey(threadId);
447
448 int count = countByCategoryId(categoryId);
449
450 Session session = null;
451
452 try {
453 session = openSession();
454
455 StringBuilder query = new StringBuilder();
456
457 query.append(
458 "FROM com.liferay.portlet.messageboards.model.MBThread WHERE ");
459
460 query.append("categoryId = ?");
461
462 query.append(" ");
463
464 if (obc != null) {
465 query.append("ORDER BY ");
466 query.append(obc.getOrderBy());
467 }
468
469 else {
470 query.append("ORDER BY ");
471
472 query.append("priority DESC, ");
473 query.append("lastPostDate DESC");
474 }
475
476 Query q = session.createQuery(query.toString());
477
478 QueryPos qPos = QueryPos.getInstance(q);
479
480 qPos.add(categoryId);
481
482 Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc, mbThread);
483
484 MBThread[] array = new MBThreadImpl[3];
485
486 array[0] = (MBThread)objArray[0];
487 array[1] = (MBThread)objArray[1];
488 array[2] = (MBThread)objArray[2];
489
490 return array;
491 }
492 catch (Exception e) {
493 throw processException(e);
494 }
495 finally {
496 closeSession(session);
497 }
498 }
499
500 public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
501 throws SystemException {
502 Session session = null;
503
504 try {
505 session = openSession();
506
507 dynamicQuery.compile(session);
508
509 return dynamicQuery.list();
510 }
511 catch (Exception e) {
512 throw processException(e);
513 }
514 finally {
515 closeSession(session);
516 }
517 }
518
519 public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
520 int start, int end) throws SystemException {
521 Session session = null;
522
523 try {
524 session = openSession();
525
526 dynamicQuery.setLimit(start, end);
527
528 dynamicQuery.compile(session);
529
530 return dynamicQuery.list();
531 }
532 catch (Exception e) {
533 throw processException(e);
534 }
535 finally {
536 closeSession(session);
537 }
538 }
539
540 public List<MBThread> findAll() throws SystemException {
541 return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
542 }
543
544 public List<MBThread> findAll(int start, int end) throws SystemException {
545 return findAll(start, end, null);
546 }
547
548 public List<MBThread> findAll(int start, int end, OrderByComparator obc)
549 throws SystemException {
550 boolean finderClassNameCacheEnabled = MBThreadModelImpl.CACHE_ENABLED;
551 String finderClassName = MBThread.class.getName();
552 String finderMethodName = "findAll";
553 String[] finderParams = new String[] {
554 "java.lang.Integer", "java.lang.Integer",
555 "com.liferay.portal.kernel.util.OrderByComparator"
556 };
557 Object[] finderArgs = new Object[] {
558 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
559 };
560
561 Object result = null;
562
563 if (finderClassNameCacheEnabled) {
564 result = FinderCacheUtil.getResult(finderClassName,
565 finderMethodName, finderParams, finderArgs, this);
566 }
567
568 if (result == null) {
569 Session session = null;
570
571 try {
572 session = openSession();
573
574 StringBuilder query = new StringBuilder();
575
576 query.append(
577 "FROM com.liferay.portlet.messageboards.model.MBThread ");
578
579 if (obc != null) {
580 query.append("ORDER BY ");
581 query.append(obc.getOrderBy());
582 }
583
584 else {
585 query.append("ORDER BY ");
586
587 query.append("priority DESC, ");
588 query.append("lastPostDate DESC");
589 }
590
591 Query q = session.createQuery(query.toString());
592
593 List<MBThread> list = null;
594
595 if (obc == null) {
596 list = (List<MBThread>)QueryUtil.list(q, getDialect(),
597 start, end, false);
598
599 Collections.sort(list);
600 }
601 else {
602 list = (List<MBThread>)QueryUtil.list(q, getDialect(),
603 start, end);
604 }
605
606 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
607 finderClassName, finderMethodName, finderParams,
608 finderArgs, list);
609
610 return list;
611 }
612 catch (Exception e) {
613 throw processException(e);
614 }
615 finally {
616 closeSession(session);
617 }
618 }
619 else {
620 return (List<MBThread>)result;
621 }
622 }
623
624 public void removeByCategoryId(long categoryId) throws SystemException {
625 for (MBThread mbThread : findByCategoryId(categoryId)) {
626 remove(mbThread);
627 }
628 }
629
630 public void removeAll() throws SystemException {
631 for (MBThread mbThread : findAll()) {
632 remove(mbThread);
633 }
634 }
635
636 public int countByCategoryId(long categoryId) throws SystemException {
637 boolean finderClassNameCacheEnabled = MBThreadModelImpl.CACHE_ENABLED;
638 String finderClassName = MBThread.class.getName();
639 String finderMethodName = "countByCategoryId";
640 String[] finderParams = new String[] { Long.class.getName() };
641 Object[] finderArgs = new Object[] { new Long(categoryId) };
642
643 Object result = null;
644
645 if (finderClassNameCacheEnabled) {
646 result = FinderCacheUtil.getResult(finderClassName,
647 finderMethodName, finderParams, finderArgs, this);
648 }
649
650 if (result == null) {
651 Session session = null;
652
653 try {
654 session = openSession();
655
656 StringBuilder query = new StringBuilder();
657
658 query.append("SELECT COUNT(*) ");
659 query.append(
660 "FROM com.liferay.portlet.messageboards.model.MBThread WHERE ");
661
662 query.append("categoryId = ?");
663
664 query.append(" ");
665
666 Query q = session.createQuery(query.toString());
667
668 QueryPos qPos = QueryPos.getInstance(q);
669
670 qPos.add(categoryId);
671
672 Long count = null;
673
674 Iterator<Long> itr = q.list().iterator();
675
676 if (itr.hasNext()) {
677 count = itr.next();
678 }
679
680 if (count == null) {
681 count = new Long(0);
682 }
683
684 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
685 finderClassName, finderMethodName, finderParams,
686 finderArgs, count);
687
688 return count.intValue();
689 }
690 catch (Exception e) {
691 throw processException(e);
692 }
693 finally {
694 closeSession(session);
695 }
696 }
697 else {
698 return ((Long)result).intValue();
699 }
700 }
701
702 public int countAll() throws SystemException {
703 boolean finderClassNameCacheEnabled = MBThreadModelImpl.CACHE_ENABLED;
704 String finderClassName = MBThread.class.getName();
705 String finderMethodName = "countAll";
706 String[] finderParams = new String[] { };
707 Object[] finderArgs = new Object[] { };
708
709 Object result = null;
710
711 if (finderClassNameCacheEnabled) {
712 result = FinderCacheUtil.getResult(finderClassName,
713 finderMethodName, finderParams, finderArgs, this);
714 }
715
716 if (result == null) {
717 Session session = null;
718
719 try {
720 session = openSession();
721
722 Query q = session.createQuery(
723 "SELECT COUNT(*) FROM com.liferay.portlet.messageboards.model.MBThread");
724
725 Long count = null;
726
727 Iterator<Long> itr = q.list().iterator();
728
729 if (itr.hasNext()) {
730 count = itr.next();
731 }
732
733 if (count == null) {
734 count = new Long(0);
735 }
736
737 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
738 finderClassName, finderMethodName, finderParams,
739 finderArgs, count);
740
741 return count.intValue();
742 }
743 catch (Exception e) {
744 throw processException(e);
745 }
746 finally {
747 closeSession(session);
748 }
749 }
750 else {
751 return ((Long)result).intValue();
752 }
753 }
754
755 public void afterPropertiesSet() {
756 String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
757 com.liferay.portal.util.PropsUtil.get(
758 "value.object.listener.com.liferay.portlet.messageboards.model.MBThread")));
759
760 if (listenerClassNames.length > 0) {
761 try {
762 List<ModelListener> listenersList = new ArrayList<ModelListener>();
763
764 for (String listenerClassName : listenerClassNames) {
765 listenersList.add((ModelListener)Class.forName(
766 listenerClassName).newInstance());
767 }
768
769 listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
770 }
771 catch (Exception e) {
772 _log.error(e);
773 }
774 }
775 }
776
777 @BeanReference(name = "com.liferay.portlet.messageboards.service.persistence.MBBanPersistence.impl")
778 protected com.liferay.portlet.messageboards.service.persistence.MBBanPersistence mbBanPersistence;
779 @BeanReference(name = "com.liferay.portlet.messageboards.service.persistence.MBCategoryPersistence.impl")
780 protected com.liferay.portlet.messageboards.service.persistence.MBCategoryPersistence mbCategoryPersistence;
781 @BeanReference(name = "com.liferay.portlet.messageboards.service.persistence.MBDiscussionPersistence.impl")
782 protected com.liferay.portlet.messageboards.service.persistence.MBDiscussionPersistence mbDiscussionPersistence;
783 @BeanReference(name = "com.liferay.portlet.messageboards.service.persistence.MBMailingListPersistence.impl")
784 protected com.liferay.portlet.messageboards.service.persistence.MBMailingListPersistence mbMailingListPersistence;
785 @BeanReference(name = "com.liferay.portlet.messageboards.service.persistence.MBMessagePersistence.impl")
786 protected com.liferay.portlet.messageboards.service.persistence.MBMessagePersistence mbMessagePersistence;
787 @BeanReference(name = "com.liferay.portlet.messageboards.service.persistence.MBMessageFlagPersistence.impl")
788 protected com.liferay.portlet.messageboards.service.persistence.MBMessageFlagPersistence mbMessageFlagPersistence;
789 @BeanReference(name = "com.liferay.portlet.messageboards.service.persistence.MBStatsUserPersistence.impl")
790 protected com.liferay.portlet.messageboards.service.persistence.MBStatsUserPersistence mbStatsUserPersistence;
791 @BeanReference(name = "com.liferay.portlet.messageboards.service.persistence.MBThreadPersistence.impl")
792 protected com.liferay.portlet.messageboards.service.persistence.MBThreadPersistence mbThreadPersistence;
793 @BeanReference(name = "com.liferay.portal.service.persistence.ResourcePersistence.impl")
794 protected com.liferay.portal.service.persistence.ResourcePersistence resourcePersistence;
795 @BeanReference(name = "com.liferay.portal.service.persistence.UserPersistence.impl")
796 protected com.liferay.portal.service.persistence.UserPersistence userPersistence;
797 @BeanReference(name = "com.liferay.portlet.ratings.service.persistence.RatingsStatsPersistence.impl")
798 protected com.liferay.portlet.ratings.service.persistence.RatingsStatsPersistence ratingsStatsPersistence;
799 @BeanReference(name = "com.liferay.portlet.social.service.persistence.SocialActivityPersistence.impl")
800 protected com.liferay.portlet.social.service.persistence.SocialActivityPersistence socialActivityPersistence;
801 @BeanReference(name = "com.liferay.portlet.tags.service.persistence.TagsAssetPersistence.impl")
802 protected com.liferay.portlet.tags.service.persistence.TagsAssetPersistence tagsAssetPersistence;
803 private static Log _log = LogFactoryUtil.getLog(MBThreadPersistenceImpl.class);
804 }