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.NoSuchDiscussionException;
40 import com.liferay.portlet.messageboards.model.MBDiscussion;
41 import com.liferay.portlet.messageboards.model.impl.MBDiscussionImpl;
42 import com.liferay.portlet.messageboards.model.impl.MBDiscussionModelImpl;
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 MBDiscussionPersistenceImpl extends BasePersistence
64 implements MBDiscussionPersistence {
65 public MBDiscussion create(long discussionId) {
66 MBDiscussion mbDiscussion = new MBDiscussionImpl();
67
68 mbDiscussion.setNew(true);
69 mbDiscussion.setPrimaryKey(discussionId);
70
71 return mbDiscussion;
72 }
73
74 public MBDiscussion remove(long discussionId)
75 throws NoSuchDiscussionException, SystemException {
76 Session session = null;
77
78 try {
79 session = openSession();
80
81 MBDiscussion mbDiscussion = (MBDiscussion)session.get(MBDiscussionImpl.class,
82 new Long(discussionId));
83
84 if (mbDiscussion == null) {
85 if (_log.isWarnEnabled()) {
86 _log.warn("No MBDiscussion exists with the primary key " +
87 discussionId);
88 }
89
90 throw new NoSuchDiscussionException(
91 "No MBDiscussion exists with the primary key " +
92 discussionId);
93 }
94
95 return remove(mbDiscussion);
96 }
97 catch (NoSuchDiscussionException nsee) {
98 throw nsee;
99 }
100 catch (Exception e) {
101 throw HibernateUtil.processException(e);
102 }
103 finally {
104 closeSession(session);
105 }
106 }
107
108 public MBDiscussion remove(MBDiscussion mbDiscussion)
109 throws SystemException {
110 if (_listeners != null) {
111 for (ModelListener listener : _listeners) {
112 listener.onBeforeRemove(mbDiscussion);
113 }
114 }
115
116 mbDiscussion = removeImpl(mbDiscussion);
117
118 if (_listeners != null) {
119 for (ModelListener listener : _listeners) {
120 listener.onAfterRemove(mbDiscussion);
121 }
122 }
123
124 return mbDiscussion;
125 }
126
127 protected MBDiscussion removeImpl(MBDiscussion mbDiscussion)
128 throws SystemException {
129 Session session = null;
130
131 try {
132 session = openSession();
133
134 session.delete(mbDiscussion);
135
136 session.flush();
137
138 return mbDiscussion;
139 }
140 catch (Exception e) {
141 throw HibernateUtil.processException(e);
142 }
143 finally {
144 closeSession(session);
145
146 FinderCache.clearCache(MBDiscussion.class.getName());
147 }
148 }
149
150
153 public MBDiscussion update(MBDiscussion mbDiscussion)
154 throws SystemException {
155 if (_log.isWarnEnabled()) {
156 _log.warn(
157 "Using the deprecated update(MBDiscussion mbDiscussion) method. Use update(MBDiscussion mbDiscussion, boolean merge) instead.");
158 }
159
160 return update(mbDiscussion, false);
161 }
162
163
176 public MBDiscussion update(MBDiscussion mbDiscussion, boolean merge)
177 throws SystemException {
178 boolean isNew = mbDiscussion.isNew();
179
180 if (_listeners != null) {
181 for (ModelListener listener : _listeners) {
182 if (isNew) {
183 listener.onBeforeCreate(mbDiscussion);
184 }
185 else {
186 listener.onBeforeUpdate(mbDiscussion);
187 }
188 }
189 }
190
191 mbDiscussion = updateImpl(mbDiscussion, merge);
192
193 if (_listeners != null) {
194 for (ModelListener listener : _listeners) {
195 if (isNew) {
196 listener.onAfterCreate(mbDiscussion);
197 }
198 else {
199 listener.onAfterUpdate(mbDiscussion);
200 }
201 }
202 }
203
204 return mbDiscussion;
205 }
206
207 public MBDiscussion updateImpl(
208 com.liferay.portlet.messageboards.model.MBDiscussion mbDiscussion,
209 boolean merge) throws SystemException {
210 Session session = null;
211
212 try {
213 session = openSession();
214
215 if (merge) {
216 session.merge(mbDiscussion);
217 }
218 else {
219 if (mbDiscussion.isNew()) {
220 session.save(mbDiscussion);
221 }
222 }
223
224 session.flush();
225
226 mbDiscussion.setNew(false);
227
228 return mbDiscussion;
229 }
230 catch (Exception e) {
231 throw HibernateUtil.processException(e);
232 }
233 finally {
234 closeSession(session);
235
236 FinderCache.clearCache(MBDiscussion.class.getName());
237 }
238 }
239
240 public MBDiscussion findByPrimaryKey(long discussionId)
241 throws NoSuchDiscussionException, SystemException {
242 MBDiscussion mbDiscussion = fetchByPrimaryKey(discussionId);
243
244 if (mbDiscussion == null) {
245 if (_log.isWarnEnabled()) {
246 _log.warn("No MBDiscussion exists with the primary key " +
247 discussionId);
248 }
249
250 throw new NoSuchDiscussionException(
251 "No MBDiscussion exists with the primary key " + discussionId);
252 }
253
254 return mbDiscussion;
255 }
256
257 public MBDiscussion fetchByPrimaryKey(long discussionId)
258 throws SystemException {
259 Session session = null;
260
261 try {
262 session = openSession();
263
264 return (MBDiscussion)session.get(MBDiscussionImpl.class,
265 new Long(discussionId));
266 }
267 catch (Exception e) {
268 throw HibernateUtil.processException(e);
269 }
270 finally {
271 closeSession(session);
272 }
273 }
274
275 public MBDiscussion findByC_C(long classNameId, long classPK)
276 throws NoSuchDiscussionException, SystemException {
277 MBDiscussion mbDiscussion = fetchByC_C(classNameId, classPK);
278
279 if (mbDiscussion == null) {
280 StringMaker msg = new StringMaker();
281
282 msg.append("No MBDiscussion exists with the key {");
283
284 msg.append("classNameId=" + classNameId);
285
286 msg.append(", ");
287 msg.append("classPK=" + classPK);
288
289 msg.append(StringPool.CLOSE_CURLY_BRACE);
290
291 if (_log.isWarnEnabled()) {
292 _log.warn(msg.toString());
293 }
294
295 throw new NoSuchDiscussionException(msg.toString());
296 }
297
298 return mbDiscussion;
299 }
300
301 public MBDiscussion fetchByC_C(long classNameId, long classPK)
302 throws SystemException {
303 boolean finderClassNameCacheEnabled = MBDiscussionModelImpl.CACHE_ENABLED;
304 String finderClassName = MBDiscussion.class.getName();
305 String finderMethodName = "fetchByC_C";
306 String[] finderParams = new String[] {
307 Long.class.getName(), Long.class.getName()
308 };
309 Object[] finderArgs = new Object[] {
310 new Long(classNameId), new Long(classPK)
311 };
312
313 Object result = null;
314
315 if (finderClassNameCacheEnabled) {
316 result = FinderCache.getResult(finderClassName, finderMethodName,
317 finderParams, finderArgs, getSessionFactory());
318 }
319
320 if (result == null) {
321 Session session = null;
322
323 try {
324 session = openSession();
325
326 StringMaker query = new StringMaker();
327
328 query.append(
329 "FROM com.liferay.portlet.messageboards.model.MBDiscussion WHERE ");
330
331 query.append("classNameId = ?");
332
333 query.append(" AND ");
334
335 query.append("classPK = ?");
336
337 query.append(" ");
338
339 Query q = session.createQuery(query.toString());
340
341 int queryPos = 0;
342
343 q.setLong(queryPos++, classNameId);
344
345 q.setLong(queryPos++, classPK);
346
347 List<MBDiscussion> list = q.list();
348
349 FinderCache.putResult(finderClassNameCacheEnabled,
350 finderClassName, finderMethodName, finderParams,
351 finderArgs, list);
352
353 if (list.size() == 0) {
354 return null;
355 }
356 else {
357 return list.get(0);
358 }
359 }
360 catch (Exception e) {
361 throw HibernateUtil.processException(e);
362 }
363 finally {
364 closeSession(session);
365 }
366 }
367 else {
368 List<MBDiscussion> list = (List<MBDiscussion>)result;
369
370 if (list.size() == 0) {
371 return null;
372 }
373 else {
374 return list.get(0);
375 }
376 }
377 }
378
379 public List<MBDiscussion> findWithDynamicQuery(
380 DynamicQueryInitializer queryInitializer) throws SystemException {
381 Session session = null;
382
383 try {
384 session = openSession();
385
386 DynamicQuery query = queryInitializer.initialize(session);
387
388 return query.list();
389 }
390 catch (Exception e) {
391 throw HibernateUtil.processException(e);
392 }
393 finally {
394 closeSession(session);
395 }
396 }
397
398 public List<MBDiscussion> findWithDynamicQuery(
399 DynamicQueryInitializer queryInitializer, int begin, int end)
400 throws SystemException {
401 Session session = null;
402
403 try {
404 session = openSession();
405
406 DynamicQuery query = queryInitializer.initialize(session);
407
408 query.setLimit(begin, end);
409
410 return query.list();
411 }
412 catch (Exception e) {
413 throw HibernateUtil.processException(e);
414 }
415 finally {
416 closeSession(session);
417 }
418 }
419
420 public List<MBDiscussion> findAll() throws SystemException {
421 return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
422 }
423
424 public List<MBDiscussion> findAll(int begin, int end)
425 throws SystemException {
426 return findAll(begin, end, null);
427 }
428
429 public List<MBDiscussion> findAll(int begin, int end, OrderByComparator obc)
430 throws SystemException {
431 boolean finderClassNameCacheEnabled = MBDiscussionModelImpl.CACHE_ENABLED;
432 String finderClassName = MBDiscussion.class.getName();
433 String finderMethodName = "findAll";
434 String[] finderParams = new String[] {
435 "java.lang.Integer", "java.lang.Integer",
436 "com.liferay.portal.kernel.util.OrderByComparator"
437 };
438 Object[] finderArgs = new Object[] {
439 String.valueOf(begin), String.valueOf(end), String.valueOf(obc)
440 };
441
442 Object result = null;
443
444 if (finderClassNameCacheEnabled) {
445 result = FinderCache.getResult(finderClassName, finderMethodName,
446 finderParams, finderArgs, getSessionFactory());
447 }
448
449 if (result == null) {
450 Session session = null;
451
452 try {
453 session = openSession();
454
455 StringMaker query = new StringMaker();
456
457 query.append(
458 "FROM com.liferay.portlet.messageboards.model.MBDiscussion ");
459
460 if (obc != null) {
461 query.append("ORDER BY ");
462 query.append(obc.getOrderBy());
463 }
464
465 Query q = session.createQuery(query.toString());
466
467 List<MBDiscussion> list = (List<MBDiscussion>)QueryUtil.list(q,
468 getDialect(), begin, end);
469
470 if (obc == null) {
471 Collections.sort(list);
472 }
473
474 FinderCache.putResult(finderClassNameCacheEnabled,
475 finderClassName, finderMethodName, finderParams,
476 finderArgs, list);
477
478 return list;
479 }
480 catch (Exception e) {
481 throw HibernateUtil.processException(e);
482 }
483 finally {
484 closeSession(session);
485 }
486 }
487 else {
488 return (List<MBDiscussion>)result;
489 }
490 }
491
492 public void removeByC_C(long classNameId, long classPK)
493 throws NoSuchDiscussionException, SystemException {
494 MBDiscussion mbDiscussion = findByC_C(classNameId, classPK);
495
496 remove(mbDiscussion);
497 }
498
499 public void removeAll() throws SystemException {
500 for (MBDiscussion mbDiscussion : findAll()) {
501 remove(mbDiscussion);
502 }
503 }
504
505 public int countByC_C(long classNameId, long classPK)
506 throws SystemException {
507 boolean finderClassNameCacheEnabled = MBDiscussionModelImpl.CACHE_ENABLED;
508 String finderClassName = MBDiscussion.class.getName();
509 String finderMethodName = "countByC_C";
510 String[] finderParams = new String[] {
511 Long.class.getName(), Long.class.getName()
512 };
513 Object[] finderArgs = new Object[] {
514 new Long(classNameId), new Long(classPK)
515 };
516
517 Object result = null;
518
519 if (finderClassNameCacheEnabled) {
520 result = FinderCache.getResult(finderClassName, finderMethodName,
521 finderParams, finderArgs, getSessionFactory());
522 }
523
524 if (result == null) {
525 Session session = null;
526
527 try {
528 session = openSession();
529
530 StringMaker query = new StringMaker();
531
532 query.append("SELECT COUNT(*) ");
533 query.append(
534 "FROM com.liferay.portlet.messageboards.model.MBDiscussion WHERE ");
535
536 query.append("classNameId = ?");
537
538 query.append(" AND ");
539
540 query.append("classPK = ?");
541
542 query.append(" ");
543
544 Query q = session.createQuery(query.toString());
545
546 int queryPos = 0;
547
548 q.setLong(queryPos++, classNameId);
549
550 q.setLong(queryPos++, classPK);
551
552 Long count = null;
553
554 Iterator<Long> itr = q.list().iterator();
555
556 if (itr.hasNext()) {
557 count = itr.next();
558 }
559
560 if (count == null) {
561 count = new Long(0);
562 }
563
564 FinderCache.putResult(finderClassNameCacheEnabled,
565 finderClassName, finderMethodName, finderParams,
566 finderArgs, count);
567
568 return count.intValue();
569 }
570 catch (Exception e) {
571 throw HibernateUtil.processException(e);
572 }
573 finally {
574 closeSession(session);
575 }
576 }
577 else {
578 return ((Long)result).intValue();
579 }
580 }
581
582 public int countAll() throws SystemException {
583 boolean finderClassNameCacheEnabled = MBDiscussionModelImpl.CACHE_ENABLED;
584 String finderClassName = MBDiscussion.class.getName();
585 String finderMethodName = "countAll";
586 String[] finderParams = new String[] { };
587 Object[] finderArgs = new Object[] { };
588
589 Object result = null;
590
591 if (finderClassNameCacheEnabled) {
592 result = FinderCache.getResult(finderClassName, finderMethodName,
593 finderParams, finderArgs, getSessionFactory());
594 }
595
596 if (result == null) {
597 Session session = null;
598
599 try {
600 session = openSession();
601
602 Query q = session.createQuery(
603 "SELECT COUNT(*) FROM com.liferay.portlet.messageboards.model.MBDiscussion");
604
605 Long count = null;
606
607 Iterator<Long> itr = q.list().iterator();
608
609 if (itr.hasNext()) {
610 count = itr.next();
611 }
612
613 if (count == null) {
614 count = new Long(0);
615 }
616
617 FinderCache.putResult(finderClassNameCacheEnabled,
618 finderClassName, finderMethodName, finderParams,
619 finderArgs, count);
620
621 return count.intValue();
622 }
623 catch (Exception e) {
624 throw HibernateUtil.processException(e);
625 }
626 finally {
627 closeSession(session);
628 }
629 }
630 else {
631 return ((Long)result).intValue();
632 }
633 }
634
635 protected void initDao() {
636 String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
637 PropsUtil.get(
638 "value.object.listener.com.liferay.portlet.messageboards.model.MBDiscussion")));
639
640 if (listenerClassNames.length > 0) {
641 try {
642 List<ModelListener> listeners = new ArrayList<ModelListener>();
643
644 for (String listenerClassName : listenerClassNames) {
645 listeners.add((ModelListener)Class.forName(
646 listenerClassName).newInstance());
647 }
648
649 _listeners = listeners.toArray(new ModelListener[listeners.size()]);
650 }
651 catch (Exception e) {
652 _log.error(e);
653 }
654 }
655 }
656
657 private static Log _log = LogFactory.getLog(MBDiscussionPersistenceImpl.class);
658 private ModelListener[] _listeners;
659 }