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