001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.messageboards.service.persistence;
022:
023: import com.liferay.portal.SystemException;
024: import com.liferay.portal.kernel.dao.DynamicQuery;
025: import com.liferay.portal.kernel.dao.DynamicQueryInitializer;
026: import com.liferay.portal.kernel.util.GetterUtil;
027: import com.liferay.portal.kernel.util.OrderByComparator;
028: import com.liferay.portal.kernel.util.StringMaker;
029: import com.liferay.portal.kernel.util.StringPool;
030: import com.liferay.portal.kernel.util.Validator;
031: import com.liferay.portal.model.ModelListener;
032: import com.liferay.portal.service.persistence.BasePersistence;
033: import com.liferay.portal.spring.hibernate.FinderCache;
034: import com.liferay.portal.spring.hibernate.HibernateUtil;
035: import com.liferay.portal.util.PropsUtil;
036:
037: import com.liferay.portlet.messageboards.NoSuchThreadException;
038: import com.liferay.portlet.messageboards.model.MBThread;
039: import com.liferay.portlet.messageboards.model.impl.MBThreadImpl;
040: import com.liferay.portlet.messageboards.model.impl.MBThreadModelImpl;
041:
042: import com.liferay.util.dao.hibernate.QueryUtil;
043:
044: import org.apache.commons.logging.Log;
045: import org.apache.commons.logging.LogFactory;
046:
047: import org.hibernate.Query;
048: import org.hibernate.Session;
049:
050: import java.util.Collections;
051: import java.util.Iterator;
052: import java.util.List;
053:
054: /**
055: * <a href="MBThreadPersistenceImpl.java.html"><b><i>View Source</i></b></a>
056: *
057: * @author Brian Wing Shun Chan
058: *
059: */
060: public class MBThreadPersistenceImpl extends BasePersistence implements
061: MBThreadPersistence {
062: public MBThread create(long threadId) {
063: MBThread mbThread = new MBThreadImpl();
064:
065: mbThread.setNew(true);
066: mbThread.setPrimaryKey(threadId);
067:
068: return mbThread;
069: }
070:
071: public MBThread remove(long threadId) throws NoSuchThreadException,
072: SystemException {
073: Session session = null;
074:
075: try {
076: session = openSession();
077:
078: MBThread mbThread = (MBThread) session.get(
079: MBThreadImpl.class, new Long(threadId));
080:
081: if (mbThread == null) {
082: if (_log.isWarnEnabled()) {
083: _log
084: .warn("No MBThread exists with the primary key "
085: + threadId);
086: }
087:
088: throw new NoSuchThreadException(
089: "No MBThread exists with the primary key "
090: + threadId);
091: }
092:
093: return remove(mbThread);
094: } catch (NoSuchThreadException nsee) {
095: throw nsee;
096: } catch (Exception e) {
097: throw HibernateUtil.processException(e);
098: } finally {
099: closeSession(session);
100: }
101: }
102:
103: public MBThread remove(MBThread mbThread) throws SystemException {
104: ModelListener listener = _getListener();
105:
106: if (listener != null) {
107: listener.onBeforeRemove(mbThread);
108: }
109:
110: mbThread = removeImpl(mbThread);
111:
112: if (listener != null) {
113: listener.onAfterRemove(mbThread);
114: }
115:
116: return mbThread;
117: }
118:
119: protected MBThread removeImpl(MBThread mbThread)
120: throws SystemException {
121: Session session = null;
122:
123: try {
124: session = openSession();
125:
126: session.delete(mbThread);
127:
128: session.flush();
129:
130: return mbThread;
131: } catch (Exception e) {
132: throw HibernateUtil.processException(e);
133: } finally {
134: closeSession(session);
135:
136: FinderCache.clearCache(MBThread.class.getName());
137: }
138: }
139:
140: public MBThread update(MBThread mbThread) throws SystemException {
141: return update(mbThread, false);
142: }
143:
144: public MBThread update(MBThread mbThread, boolean merge)
145: throws SystemException {
146: ModelListener listener = _getListener();
147:
148: boolean isNew = mbThread.isNew();
149:
150: if (listener != null) {
151: if (isNew) {
152: listener.onBeforeCreate(mbThread);
153: } else {
154: listener.onBeforeUpdate(mbThread);
155: }
156: }
157:
158: mbThread = updateImpl(mbThread, merge);
159:
160: if (listener != null) {
161: if (isNew) {
162: listener.onAfterCreate(mbThread);
163: } else {
164: listener.onAfterUpdate(mbThread);
165: }
166: }
167:
168: return mbThread;
169: }
170:
171: public MBThread updateImpl(
172: com.liferay.portlet.messageboards.model.MBThread mbThread,
173: boolean merge) throws SystemException {
174: Session session = null;
175:
176: try {
177: session = openSession();
178:
179: if (merge) {
180: session.merge(mbThread);
181: } else {
182: if (mbThread.isNew()) {
183: session.save(mbThread);
184: }
185: }
186:
187: session.flush();
188:
189: mbThread.setNew(false);
190:
191: return mbThread;
192: } catch (Exception e) {
193: throw HibernateUtil.processException(e);
194: } finally {
195: closeSession(session);
196:
197: FinderCache.clearCache(MBThread.class.getName());
198: }
199: }
200:
201: public MBThread findByPrimaryKey(long threadId)
202: throws NoSuchThreadException, SystemException {
203: MBThread mbThread = fetchByPrimaryKey(threadId);
204:
205: if (mbThread == null) {
206: if (_log.isWarnEnabled()) {
207: _log.warn("No MBThread exists with the primary key "
208: + threadId);
209: }
210:
211: throw new NoSuchThreadException(
212: "No MBThread exists with the primary key "
213: + threadId);
214: }
215:
216: return mbThread;
217: }
218:
219: public MBThread fetchByPrimaryKey(long threadId)
220: throws SystemException {
221: Session session = null;
222:
223: try {
224: session = openSession();
225:
226: return (MBThread) session.get(MBThreadImpl.class, new Long(
227: threadId));
228: } catch (Exception e) {
229: throw HibernateUtil.processException(e);
230: } finally {
231: closeSession(session);
232: }
233: }
234:
235: public List findByCategoryId(long categoryId)
236: throws SystemException {
237: boolean finderClassNameCacheEnabled = MBThreadModelImpl.CACHE_ENABLED;
238: String finderClassName = MBThread.class.getName();
239: String finderMethodName = "findByCategoryId";
240: String[] finderParams = new String[] { Long.class.getName() };
241: Object[] finderArgs = new Object[] { new Long(categoryId) };
242:
243: Object result = null;
244:
245: if (finderClassNameCacheEnabled) {
246: result = FinderCache.getResult(finderClassName,
247: finderMethodName, finderParams, finderArgs,
248: getSessionFactory());
249: }
250:
251: if (result == null) {
252: Session session = null;
253:
254: try {
255: session = openSession();
256:
257: StringMaker query = new StringMaker();
258:
259: query
260: .append("FROM com.liferay.portlet.messageboards.model.MBThread WHERE ");
261:
262: query.append("categoryId = ?");
263:
264: query.append(" ");
265:
266: query.append("ORDER BY ");
267:
268: query.append("priority DESC, ");
269: query.append("lastPostDate DESC");
270:
271: Query q = session.createQuery(query.toString());
272:
273: int queryPos = 0;
274:
275: q.setLong(queryPos++, categoryId);
276:
277: List list = q.list();
278:
279: FinderCache.putResult(finderClassNameCacheEnabled,
280: finderClassName, finderMethodName,
281: finderParams, finderArgs, list);
282:
283: return list;
284: } catch (Exception e) {
285: throw HibernateUtil.processException(e);
286: } finally {
287: closeSession(session);
288: }
289: } else {
290: return (List) result;
291: }
292: }
293:
294: public List findByCategoryId(long categoryId, int begin, int end)
295: throws SystemException {
296: return findByCategoryId(categoryId, begin, end, null);
297: }
298:
299: public List findByCategoryId(long categoryId, int begin, int end,
300: OrderByComparator obc) throws SystemException {
301: boolean finderClassNameCacheEnabled = MBThreadModelImpl.CACHE_ENABLED;
302: String finderClassName = MBThread.class.getName();
303: String finderMethodName = "findByCategoryId";
304: String[] finderParams = new String[] { Long.class.getName(),
305:
306: "java.lang.Integer", "java.lang.Integer",
307: "com.liferay.portal.kernel.util.OrderByComparator" };
308: Object[] finderArgs = new Object[] { new Long(categoryId),
309:
310: String.valueOf(begin), String.valueOf(end), String.valueOf(obc) };
311:
312: Object result = null;
313:
314: if (finderClassNameCacheEnabled) {
315: result = FinderCache.getResult(finderClassName,
316: finderMethodName, finderParams, finderArgs,
317: 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
329: .append("FROM com.liferay.portlet.messageboards.model.MBThread WHERE ");
330:
331: query.append("categoryId = ?");
332:
333: query.append(" ");
334:
335: if (obc != null) {
336: query.append("ORDER BY ");
337: query.append(obc.getOrderBy());
338: }
339:
340: else {
341: query.append("ORDER BY ");
342:
343: query.append("priority DESC, ");
344: query.append("lastPostDate DESC");
345: }
346:
347: Query q = session.createQuery(query.toString());
348:
349: int queryPos = 0;
350:
351: q.setLong(queryPos++, categoryId);
352:
353: List list = QueryUtil.list(q, getDialect(), begin, end);
354:
355: FinderCache.putResult(finderClassNameCacheEnabled,
356: finderClassName, finderMethodName,
357: finderParams, finderArgs, list);
358:
359: return list;
360: } catch (Exception e) {
361: throw HibernateUtil.processException(e);
362: } finally {
363: closeSession(session);
364: }
365: } else {
366: return (List) result;
367: }
368: }
369:
370: public MBThread findByCategoryId_First(long categoryId,
371: OrderByComparator obc) throws NoSuchThreadException,
372: SystemException {
373: List list = findByCategoryId(categoryId, 0, 1, obc);
374:
375: if (list.size() == 0) {
376: StringMaker msg = new StringMaker();
377:
378: msg.append("No MBThread exists with the key {");
379:
380: msg.append("categoryId=" + categoryId);
381:
382: msg.append(StringPool.CLOSE_CURLY_BRACE);
383:
384: throw new NoSuchThreadException(msg.toString());
385: } else {
386: return (MBThread) list.get(0);
387: }
388: }
389:
390: public MBThread findByCategoryId_Last(long categoryId,
391: OrderByComparator obc) throws NoSuchThreadException,
392: SystemException {
393: int count = countByCategoryId(categoryId);
394:
395: List list = findByCategoryId(categoryId, count - 1, count, obc);
396:
397: if (list.size() == 0) {
398: StringMaker msg = new StringMaker();
399:
400: msg.append("No MBThread exists with the key {");
401:
402: msg.append("categoryId=" + categoryId);
403:
404: msg.append(StringPool.CLOSE_CURLY_BRACE);
405:
406: throw new NoSuchThreadException(msg.toString());
407: } else {
408: return (MBThread) list.get(0);
409: }
410: }
411:
412: public MBThread[] findByCategoryId_PrevAndNext(long threadId,
413: long categoryId, OrderByComparator obc)
414: throws NoSuchThreadException, SystemException {
415: MBThread mbThread = findByPrimaryKey(threadId);
416:
417: int count = countByCategoryId(categoryId);
418:
419: Session session = null;
420:
421: try {
422: session = openSession();
423:
424: StringMaker query = new StringMaker();
425:
426: query
427: .append("FROM com.liferay.portlet.messageboards.model.MBThread WHERE ");
428:
429: query.append("categoryId = ?");
430:
431: query.append(" ");
432:
433: if (obc != null) {
434: query.append("ORDER BY ");
435: query.append(obc.getOrderBy());
436: }
437:
438: else {
439: query.append("ORDER BY ");
440:
441: query.append("priority DESC, ");
442: query.append("lastPostDate DESC");
443: }
444:
445: Query q = session.createQuery(query.toString());
446:
447: int queryPos = 0;
448:
449: q.setLong(queryPos++, categoryId);
450:
451: Object[] objArray = QueryUtil.getPrevAndNext(q, count, obc,
452: mbThread);
453:
454: MBThread[] array = new MBThreadImpl[3];
455:
456: array[0] = (MBThread) objArray[0];
457: array[1] = (MBThread) objArray[1];
458: array[2] = (MBThread) objArray[2];
459:
460: return array;
461: } catch (Exception e) {
462: throw HibernateUtil.processException(e);
463: } finally {
464: closeSession(session);
465: }
466: }
467:
468: public List findWithDynamicQuery(
469: DynamicQueryInitializer queryInitializer)
470: throws SystemException {
471: Session session = null;
472:
473: try {
474: session = openSession();
475:
476: DynamicQuery query = queryInitializer.initialize(session);
477:
478: return query.list();
479: } catch (Exception e) {
480: throw HibernateUtil.processException(e);
481: } finally {
482: closeSession(session);
483: }
484: }
485:
486: public List findWithDynamicQuery(
487: DynamicQueryInitializer queryInitializer, int begin, int end)
488: throws SystemException {
489: Session session = null;
490:
491: try {
492: session = openSession();
493:
494: DynamicQuery query = queryInitializer.initialize(session);
495:
496: query.setLimit(begin, end);
497:
498: return query.list();
499: } catch (Exception e) {
500: throw HibernateUtil.processException(e);
501: } finally {
502: closeSession(session);
503: }
504: }
505:
506: public List findAll() throws SystemException {
507: return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
508: }
509:
510: public List findAll(int begin, int end) throws SystemException {
511: return findAll(begin, end, null);
512: }
513:
514: public List findAll(int begin, int end, OrderByComparator obc)
515: throws SystemException {
516: boolean finderClassNameCacheEnabled = MBThreadModelImpl.CACHE_ENABLED;
517: String finderClassName = MBThread.class.getName();
518: String finderMethodName = "findAll";
519: String[] finderParams = new String[] { "java.lang.Integer",
520: "java.lang.Integer",
521: "com.liferay.portal.kernel.util.OrderByComparator" };
522: Object[] finderArgs = new Object[] { String.valueOf(begin),
523: String.valueOf(end), String.valueOf(obc) };
524:
525: Object result = null;
526:
527: if (finderClassNameCacheEnabled) {
528: result = FinderCache.getResult(finderClassName,
529: finderMethodName, finderParams, finderArgs,
530: getSessionFactory());
531: }
532:
533: if (result == null) {
534: Session session = null;
535:
536: try {
537: session = openSession();
538:
539: StringMaker query = new StringMaker();
540:
541: query
542: .append("FROM com.liferay.portlet.messageboards.model.MBThread ");
543:
544: if (obc != null) {
545: query.append("ORDER BY ");
546: query.append(obc.getOrderBy());
547: }
548:
549: else {
550: query.append("ORDER BY ");
551:
552: query.append("priority DESC, ");
553: query.append("lastPostDate DESC");
554: }
555:
556: Query q = session.createQuery(query.toString());
557:
558: List list = QueryUtil.list(q, getDialect(), begin, end);
559:
560: if (obc == null) {
561: Collections.sort(list);
562: }
563:
564: FinderCache.putResult(finderClassNameCacheEnabled,
565: finderClassName, finderMethodName,
566: finderParams, finderArgs, list);
567:
568: return list;
569: } catch (Exception e) {
570: throw HibernateUtil.processException(e);
571: } finally {
572: closeSession(session);
573: }
574: } else {
575: return (List) result;
576: }
577: }
578:
579: public void removeByCategoryId(long categoryId)
580: throws SystemException {
581: Iterator itr = findByCategoryId(categoryId).iterator();
582:
583: while (itr.hasNext()) {
584: MBThread mbThread = (MBThread) itr.next();
585:
586: remove(mbThread);
587: }
588: }
589:
590: public void removeAll() throws SystemException {
591: Iterator itr = findAll().iterator();
592:
593: while (itr.hasNext()) {
594: remove((MBThread) itr.next());
595: }
596: }
597:
598: public int countByCategoryId(long categoryId)
599: throws SystemException {
600: boolean finderClassNameCacheEnabled = MBThreadModelImpl.CACHE_ENABLED;
601: String finderClassName = MBThread.class.getName();
602: String finderMethodName = "countByCategoryId";
603: String[] finderParams = new String[] { Long.class.getName() };
604: Object[] finderArgs = new Object[] { new Long(categoryId) };
605:
606: Object result = null;
607:
608: if (finderClassNameCacheEnabled) {
609: result = FinderCache.getResult(finderClassName,
610: finderMethodName, finderParams, finderArgs,
611: getSessionFactory());
612: }
613:
614: if (result == null) {
615: Session session = null;
616:
617: try {
618: session = openSession();
619:
620: StringMaker query = new StringMaker();
621:
622: query.append("SELECT COUNT(*) ");
623: query
624: .append("FROM com.liferay.portlet.messageboards.model.MBThread WHERE ");
625:
626: query.append("categoryId = ?");
627:
628: query.append(" ");
629:
630: Query q = session.createQuery(query.toString());
631:
632: int queryPos = 0;
633:
634: q.setLong(queryPos++, categoryId);
635:
636: Long count = null;
637:
638: Iterator itr = q.list().iterator();
639:
640: if (itr.hasNext()) {
641: count = (Long) itr.next();
642: }
643:
644: if (count == null) {
645: count = new Long(0);
646: }
647:
648: FinderCache.putResult(finderClassNameCacheEnabled,
649: finderClassName, finderMethodName,
650: finderParams, finderArgs, count);
651:
652: return count.intValue();
653: } catch (Exception e) {
654: throw HibernateUtil.processException(e);
655: } finally {
656: closeSession(session);
657: }
658: } else {
659: return ((Long) result).intValue();
660: }
661: }
662:
663: public int countAll() throws SystemException {
664: boolean finderClassNameCacheEnabled = MBThreadModelImpl.CACHE_ENABLED;
665: String finderClassName = MBThread.class.getName();
666: String finderMethodName = "countAll";
667: String[] finderParams = new String[] {};
668: Object[] finderArgs = new Object[] {};
669:
670: Object result = null;
671:
672: if (finderClassNameCacheEnabled) {
673: result = FinderCache.getResult(finderClassName,
674: finderMethodName, finderParams, finderArgs,
675: getSessionFactory());
676: }
677:
678: if (result == null) {
679: Session session = null;
680:
681: try {
682: session = openSession();
683:
684: Query q = session
685: .createQuery("SELECT COUNT(*) FROM com.liferay.portlet.messageboards.model.MBThread");
686:
687: Long count = null;
688:
689: Iterator itr = q.list().iterator();
690:
691: if (itr.hasNext()) {
692: count = (Long) itr.next();
693: }
694:
695: if (count == null) {
696: count = new Long(0);
697: }
698:
699: FinderCache.putResult(finderClassNameCacheEnabled,
700: finderClassName, finderMethodName,
701: finderParams, finderArgs, count);
702:
703: return count.intValue();
704: } catch (Exception e) {
705: throw HibernateUtil.processException(e);
706: } finally {
707: closeSession(session);
708: }
709: } else {
710: return ((Long) result).intValue();
711: }
712: }
713:
714: protected void initDao() {
715: }
716:
717: private static ModelListener _getListener() {
718: if (Validator.isNotNull(_LISTENER)) {
719: try {
720: return (ModelListener) Class.forName(_LISTENER)
721: .newInstance();
722: } catch (Exception e) {
723: _log.error(e);
724: }
725: }
726:
727: return null;
728: }
729:
730: private static final String _LISTENER = GetterUtil
731: .getString(PropsUtil
732: .get("value.object.listener.com.liferay.portlet.messageboards.model.MBThread"));
733: private static Log _log = LogFactory
734: .getLog(MBThreadPersistenceImpl.class);
735: }
|