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