1
22
23 package com.liferay.portlet.messageboards.service.persistence;
24
25 import com.liferay.portal.SystemException;
26 import com.liferay.portal.kernel.dao.orm.DynamicQuery;
27 import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
28 import com.liferay.portal.kernel.dao.orm.Query;
29 import com.liferay.portal.kernel.dao.orm.QueryPos;
30 import com.liferay.portal.kernel.dao.orm.QueryUtil;
31 import com.liferay.portal.kernel.dao.orm.Session;
32 import com.liferay.portal.kernel.util.GetterUtil;
33 import com.liferay.portal.kernel.util.ListUtil;
34 import com.liferay.portal.kernel.util.OrderByComparator;
35 import com.liferay.portal.kernel.util.StringPool;
36 import com.liferay.portal.kernel.util.StringUtil;
37 import com.liferay.portal.model.ModelListener;
38 import com.liferay.portal.service.persistence.BatchSessionUtil;
39 import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
40
41 import com.liferay.portlet.messageboards.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 {
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 if (BatchSessionUtil.isEnabled()) {
129 Object staleObject = session.get(MBThreadImpl.class,
130 mbThread.getPrimaryKeyObj());
131
132 if (staleObject != null) {
133 session.evict(staleObject);
134 }
135 }
136
137 session.delete(mbThread);
138
139 session.flush();
140
141 return mbThread;
142 }
143 catch (Exception e) {
144 throw processException(e);
145 }
146 finally {
147 closeSession(session);
148
149 FinderCacheUtil.clearCache(MBThread.class.getName());
150 }
151 }
152
153
156 public MBThread update(MBThread mbThread) throws SystemException {
157 if (_log.isWarnEnabled()) {
158 _log.warn(
159 "Using the deprecated update(MBThread mbThread) method. Use update(MBThread mbThread, boolean merge) instead.");
160 }
161
162 return update(mbThread, false);
163 }
164
165
178 public MBThread update(MBThread mbThread, boolean merge)
179 throws SystemException {
180 boolean isNew = mbThread.isNew();
181
182 if (_listeners.length > 0) {
183 for (ModelListener listener : _listeners) {
184 if (isNew) {
185 listener.onBeforeCreate(mbThread);
186 }
187 else {
188 listener.onBeforeUpdate(mbThread);
189 }
190 }
191 }
192
193 mbThread = updateImpl(mbThread, merge);
194
195 if (_listeners.length > 0) {
196 for (ModelListener listener : _listeners) {
197 if (isNew) {
198 listener.onAfterCreate(mbThread);
199 }
200 else {
201 listener.onAfterUpdate(mbThread);
202 }
203 }
204 }
205
206 return mbThread;
207 }
208
209 public MBThread updateImpl(
210 com.liferay.portlet.messageboards.model.MBThread mbThread, boolean merge)
211 throws SystemException {
212 Session session = null;
213
214 try {
215 session = openSession();
216
217 BatchSessionUtil.update(session, mbThread, merge);
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 = null;
603
604 if (obc == null) {
605 list = (List<MBThread>)QueryUtil.list(q, getDialect(),
606 start, end, false);
607
608 Collections.sort(list);
609 }
610 else {
611 list = (List<MBThread>)QueryUtil.list(q, getDialect(),
612 start, end);
613 }
614
615 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
616 finderClassName, finderMethodName, finderParams,
617 finderArgs, list);
618
619 return list;
620 }
621 catch (Exception e) {
622 throw processException(e);
623 }
624 finally {
625 closeSession(session);
626 }
627 }
628 else {
629 return (List<MBThread>)result;
630 }
631 }
632
633 public void removeByCategoryId(long categoryId) throws SystemException {
634 for (MBThread mbThread : findByCategoryId(categoryId)) {
635 remove(mbThread);
636 }
637 }
638
639 public void removeAll() throws SystemException {
640 for (MBThread mbThread : findAll()) {
641 remove(mbThread);
642 }
643 }
644
645 public int countByCategoryId(long categoryId) throws SystemException {
646 boolean finderClassNameCacheEnabled = MBThreadModelImpl.CACHE_ENABLED;
647 String finderClassName = MBThread.class.getName();
648 String finderMethodName = "countByCategoryId";
649 String[] finderParams = new String[] { Long.class.getName() };
650 Object[] finderArgs = new Object[] { new Long(categoryId) };
651
652 Object result = null;
653
654 if (finderClassNameCacheEnabled) {
655 result = FinderCacheUtil.getResult(finderClassName,
656 finderMethodName, finderParams, finderArgs, this);
657 }
658
659 if (result == null) {
660 Session session = null;
661
662 try {
663 session = openSession();
664
665 StringBuilder query = new StringBuilder();
666
667 query.append("SELECT COUNT(*) ");
668 query.append(
669 "FROM com.liferay.portlet.messageboards.model.MBThread WHERE ");
670
671 query.append("categoryId = ?");
672
673 query.append(" ");
674
675 Query q = session.createQuery(query.toString());
676
677 QueryPos qPos = QueryPos.getInstance(q);
678
679 qPos.add(categoryId);
680
681 Long count = null;
682
683 Iterator<Long> itr = q.list().iterator();
684
685 if (itr.hasNext()) {
686 count = itr.next();
687 }
688
689 if (count == null) {
690 count = new Long(0);
691 }
692
693 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
694 finderClassName, finderMethodName, finderParams,
695 finderArgs, count);
696
697 return count.intValue();
698 }
699 catch (Exception e) {
700 throw processException(e);
701 }
702 finally {
703 closeSession(session);
704 }
705 }
706 else {
707 return ((Long)result).intValue();
708 }
709 }
710
711 public int countAll() throws SystemException {
712 boolean finderClassNameCacheEnabled = MBThreadModelImpl.CACHE_ENABLED;
713 String finderClassName = MBThread.class.getName();
714 String finderMethodName = "countAll";
715 String[] finderParams = new String[] { };
716 Object[] finderArgs = new Object[] { };
717
718 Object result = null;
719
720 if (finderClassNameCacheEnabled) {
721 result = FinderCacheUtil.getResult(finderClassName,
722 finderMethodName, finderParams, finderArgs, this);
723 }
724
725 if (result == null) {
726 Session session = null;
727
728 try {
729 session = openSession();
730
731 Query q = session.createQuery(
732 "SELECT COUNT(*) FROM com.liferay.portlet.messageboards.model.MBThread");
733
734 Long count = null;
735
736 Iterator<Long> itr = q.list().iterator();
737
738 if (itr.hasNext()) {
739 count = itr.next();
740 }
741
742 if (count == null) {
743 count = new Long(0);
744 }
745
746 FinderCacheUtil.putResult(finderClassNameCacheEnabled,
747 finderClassName, finderMethodName, finderParams,
748 finderArgs, count);
749
750 return count.intValue();
751 }
752 catch (Exception e) {
753 throw processException(e);
754 }
755 finally {
756 closeSession(session);
757 }
758 }
759 else {
760 return ((Long)result).intValue();
761 }
762 }
763
764 public void registerListener(ModelListener listener) {
765 List<ModelListener> listeners = ListUtil.fromArray(_listeners);
766
767 listeners.add(listener);
768
769 _listeners = listeners.toArray(new ModelListener[listeners.size()]);
770 }
771
772 public void unregisterListener(ModelListener listener) {
773 List<ModelListener> listeners = ListUtil.fromArray(_listeners);
774
775 listeners.remove(listener);
776
777 _listeners = listeners.toArray(new ModelListener[listeners.size()]);
778 }
779
780 public void afterPropertiesSet() {
781 String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
782 com.liferay.portal.util.PropsUtil.get(
783 "value.object.listener.com.liferay.portlet.messageboards.model.MBThread")));
784
785 if (listenerClassNames.length > 0) {
786 try {
787 List<ModelListener> listeners = new ArrayList<ModelListener>();
788
789 for (String listenerClassName : listenerClassNames) {
790 listeners.add((ModelListener)Class.forName(
791 listenerClassName).newInstance());
792 }
793
794 _listeners = listeners.toArray(new ModelListener[listeners.size()]);
795 }
796 catch (Exception e) {
797 _log.error(e);
798 }
799 }
800 }
801
802 private static Log _log = LogFactory.getLog(MBThreadPersistenceImpl.class);
803 private ModelListener[] _listeners = new ModelListener[0];
804 }