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.ratings.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.ratings.NoSuchStatsException;
038: import com.liferay.portlet.ratings.model.RatingsStats;
039: import com.liferay.portlet.ratings.model.impl.RatingsStatsImpl;
040: import com.liferay.portlet.ratings.model.impl.RatingsStatsModelImpl;
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="RatingsStatsPersistenceImpl.java.html"><b><i>View Source</i></b></a>
056: *
057: * @author Brian Wing Shun Chan
058: *
059: */
060: public class RatingsStatsPersistenceImpl extends BasePersistence
061: implements RatingsStatsPersistence {
062: public RatingsStats create(long statsId) {
063: RatingsStats ratingsStats = new RatingsStatsImpl();
064:
065: ratingsStats.setNew(true);
066: ratingsStats.setPrimaryKey(statsId);
067:
068: return ratingsStats;
069: }
070:
071: public RatingsStats remove(long statsId)
072: throws NoSuchStatsException, SystemException {
073: Session session = null;
074:
075: try {
076: session = openSession();
077:
078: RatingsStats ratingsStats = (RatingsStats) session.get(
079: RatingsStatsImpl.class, new Long(statsId));
080:
081: if (ratingsStats == null) {
082: if (_log.isWarnEnabled()) {
083: _log
084: .warn("No RatingsStats exists with the primary key "
085: + statsId);
086: }
087:
088: throw new NoSuchStatsException(
089: "No RatingsStats exists with the primary key "
090: + statsId);
091: }
092:
093: return remove(ratingsStats);
094: } catch (NoSuchStatsException nsee) {
095: throw nsee;
096: } catch (Exception e) {
097: throw HibernateUtil.processException(e);
098: } finally {
099: closeSession(session);
100: }
101: }
102:
103: public RatingsStats remove(RatingsStats ratingsStats)
104: throws SystemException {
105: ModelListener listener = _getListener();
106:
107: if (listener != null) {
108: listener.onBeforeRemove(ratingsStats);
109: }
110:
111: ratingsStats = removeImpl(ratingsStats);
112:
113: if (listener != null) {
114: listener.onAfterRemove(ratingsStats);
115: }
116:
117: return ratingsStats;
118: }
119:
120: protected RatingsStats removeImpl(RatingsStats ratingsStats)
121: throws SystemException {
122: Session session = null;
123:
124: try {
125: session = openSession();
126:
127: session.delete(ratingsStats);
128:
129: session.flush();
130:
131: return ratingsStats;
132: } catch (Exception e) {
133: throw HibernateUtil.processException(e);
134: } finally {
135: closeSession(session);
136:
137: FinderCache.clearCache(RatingsStats.class.getName());
138: }
139: }
140:
141: public RatingsStats update(RatingsStats ratingsStats)
142: throws SystemException {
143: return update(ratingsStats, false);
144: }
145:
146: public RatingsStats update(RatingsStats ratingsStats, boolean merge)
147: throws SystemException {
148: ModelListener listener = _getListener();
149:
150: boolean isNew = ratingsStats.isNew();
151:
152: if (listener != null) {
153: if (isNew) {
154: listener.onBeforeCreate(ratingsStats);
155: } else {
156: listener.onBeforeUpdate(ratingsStats);
157: }
158: }
159:
160: ratingsStats = updateImpl(ratingsStats, merge);
161:
162: if (listener != null) {
163: if (isNew) {
164: listener.onAfterCreate(ratingsStats);
165: } else {
166: listener.onAfterUpdate(ratingsStats);
167: }
168: }
169:
170: return ratingsStats;
171: }
172:
173: public RatingsStats updateImpl(
174: com.liferay.portlet.ratings.model.RatingsStats ratingsStats,
175: boolean merge) throws SystemException {
176: Session session = null;
177:
178: try {
179: session = openSession();
180:
181: if (merge) {
182: session.merge(ratingsStats);
183: } else {
184: if (ratingsStats.isNew()) {
185: session.save(ratingsStats);
186: }
187: }
188:
189: session.flush();
190:
191: ratingsStats.setNew(false);
192:
193: return ratingsStats;
194: } catch (Exception e) {
195: throw HibernateUtil.processException(e);
196: } finally {
197: closeSession(session);
198:
199: FinderCache.clearCache(RatingsStats.class.getName());
200: }
201: }
202:
203: public RatingsStats findByPrimaryKey(long statsId)
204: throws NoSuchStatsException, SystemException {
205: RatingsStats ratingsStats = fetchByPrimaryKey(statsId);
206:
207: if (ratingsStats == null) {
208: if (_log.isWarnEnabled()) {
209: _log
210: .warn("No RatingsStats exists with the primary key "
211: + statsId);
212: }
213:
214: throw new NoSuchStatsException(
215: "No RatingsStats exists with the primary key "
216: + statsId);
217: }
218:
219: return ratingsStats;
220: }
221:
222: public RatingsStats fetchByPrimaryKey(long statsId)
223: throws SystemException {
224: Session session = null;
225:
226: try {
227: session = openSession();
228:
229: return (RatingsStats) session.get(RatingsStatsImpl.class,
230: new Long(statsId));
231: } catch (Exception e) {
232: throw HibernateUtil.processException(e);
233: } finally {
234: closeSession(session);
235: }
236: }
237:
238: public RatingsStats findByC_C(long classNameId, long classPK)
239: throws NoSuchStatsException, SystemException {
240: RatingsStats ratingsStats = fetchByC_C(classNameId, classPK);
241:
242: if (ratingsStats == null) {
243: StringMaker msg = new StringMaker();
244:
245: msg.append("No RatingsStats exists with the key {");
246:
247: msg.append("classNameId=" + classNameId);
248:
249: msg.append(", ");
250: msg.append("classPK=" + classPK);
251:
252: msg.append(StringPool.CLOSE_CURLY_BRACE);
253:
254: if (_log.isWarnEnabled()) {
255: _log.warn(msg.toString());
256: }
257:
258: throw new NoSuchStatsException(msg.toString());
259: }
260:
261: return ratingsStats;
262: }
263:
264: public RatingsStats fetchByC_C(long classNameId, long classPK)
265: throws SystemException {
266: boolean finderClassNameCacheEnabled = RatingsStatsModelImpl.CACHE_ENABLED;
267: String finderClassName = RatingsStats.class.getName();
268: String finderMethodName = "fetchByC_C";
269: String[] finderParams = new String[] { Long.class.getName(),
270: Long.class.getName() };
271: Object[] finderArgs = new Object[] { new Long(classNameId),
272: new Long(classPK) };
273:
274: Object result = null;
275:
276: if (finderClassNameCacheEnabled) {
277: result = FinderCache.getResult(finderClassName,
278: finderMethodName, finderParams, finderArgs,
279: getSessionFactory());
280: }
281:
282: if (result == null) {
283: Session session = null;
284:
285: try {
286: session = openSession();
287:
288: StringMaker query = new StringMaker();
289:
290: query
291: .append("FROM com.liferay.portlet.ratings.model.RatingsStats WHERE ");
292:
293: query.append("classNameId = ?");
294:
295: query.append(" AND ");
296:
297: query.append("classPK = ?");
298:
299: query.append(" ");
300:
301: Query q = session.createQuery(query.toString());
302:
303: int queryPos = 0;
304:
305: q.setLong(queryPos++, classNameId);
306:
307: q.setLong(queryPos++, classPK);
308:
309: List list = q.list();
310:
311: FinderCache.putResult(finderClassNameCacheEnabled,
312: finderClassName, finderMethodName,
313: finderParams, finderArgs, list);
314:
315: if (list.size() == 0) {
316: return null;
317: } else {
318: return (RatingsStats) list.get(0);
319: }
320: } catch (Exception e) {
321: throw HibernateUtil.processException(e);
322: } finally {
323: closeSession(session);
324: }
325: } else {
326: List list = (List) result;
327:
328: if (list.size() == 0) {
329: return null;
330: } else {
331: return (RatingsStats) list.get(0);
332: }
333: }
334: }
335:
336: public List findWithDynamicQuery(
337: DynamicQueryInitializer queryInitializer)
338: throws SystemException {
339: Session session = null;
340:
341: try {
342: session = openSession();
343:
344: DynamicQuery query = queryInitializer.initialize(session);
345:
346: return query.list();
347: } catch (Exception e) {
348: throw HibernateUtil.processException(e);
349: } finally {
350: closeSession(session);
351: }
352: }
353:
354: public List findWithDynamicQuery(
355: DynamicQueryInitializer queryInitializer, int begin, int end)
356: throws SystemException {
357: Session session = null;
358:
359: try {
360: session = openSession();
361:
362: DynamicQuery query = queryInitializer.initialize(session);
363:
364: query.setLimit(begin, end);
365:
366: return query.list();
367: } catch (Exception e) {
368: throw HibernateUtil.processException(e);
369: } finally {
370: closeSession(session);
371: }
372: }
373:
374: public List findAll() throws SystemException {
375: return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
376: }
377:
378: public List findAll(int begin, int end) throws SystemException {
379: return findAll(begin, end, null);
380: }
381:
382: public List findAll(int begin, int end, OrderByComparator obc)
383: throws SystemException {
384: boolean finderClassNameCacheEnabled = RatingsStatsModelImpl.CACHE_ENABLED;
385: String finderClassName = RatingsStats.class.getName();
386: String finderMethodName = "findAll";
387: String[] finderParams = new String[] { "java.lang.Integer",
388: "java.lang.Integer",
389: "com.liferay.portal.kernel.util.OrderByComparator" };
390: Object[] finderArgs = new Object[] { String.valueOf(begin),
391: String.valueOf(end), String.valueOf(obc) };
392:
393: Object result = null;
394:
395: if (finderClassNameCacheEnabled) {
396: result = FinderCache.getResult(finderClassName,
397: finderMethodName, finderParams, finderArgs,
398: getSessionFactory());
399: }
400:
401: if (result == null) {
402: Session session = null;
403:
404: try {
405: session = openSession();
406:
407: StringMaker query = new StringMaker();
408:
409: query
410: .append("FROM com.liferay.portlet.ratings.model.RatingsStats ");
411:
412: if (obc != null) {
413: query.append("ORDER BY ");
414: query.append(obc.getOrderBy());
415: }
416:
417: Query q = session.createQuery(query.toString());
418:
419: List list = QueryUtil.list(q, getDialect(), begin, end);
420:
421: if (obc == null) {
422: Collections.sort(list);
423: }
424:
425: FinderCache.putResult(finderClassNameCacheEnabled,
426: finderClassName, finderMethodName,
427: finderParams, finderArgs, list);
428:
429: return list;
430: } catch (Exception e) {
431: throw HibernateUtil.processException(e);
432: } finally {
433: closeSession(session);
434: }
435: } else {
436: return (List) result;
437: }
438: }
439:
440: public void removeByC_C(long classNameId, long classPK)
441: throws NoSuchStatsException, SystemException {
442: RatingsStats ratingsStats = findByC_C(classNameId, classPK);
443:
444: remove(ratingsStats);
445: }
446:
447: public void removeAll() throws SystemException {
448: Iterator itr = findAll().iterator();
449:
450: while (itr.hasNext()) {
451: remove((RatingsStats) itr.next());
452: }
453: }
454:
455: public int countByC_C(long classNameId, long classPK)
456: throws SystemException {
457: boolean finderClassNameCacheEnabled = RatingsStatsModelImpl.CACHE_ENABLED;
458: String finderClassName = RatingsStats.class.getName();
459: String finderMethodName = "countByC_C";
460: String[] finderParams = new String[] { Long.class.getName(),
461: Long.class.getName() };
462: Object[] finderArgs = new Object[] { new Long(classNameId),
463: new Long(classPK) };
464:
465: Object result = null;
466:
467: if (finderClassNameCacheEnabled) {
468: result = FinderCache.getResult(finderClassName,
469: finderMethodName, finderParams, finderArgs,
470: getSessionFactory());
471: }
472:
473: if (result == null) {
474: Session session = null;
475:
476: try {
477: session = openSession();
478:
479: StringMaker query = new StringMaker();
480:
481: query.append("SELECT COUNT(*) ");
482: query
483: .append("FROM com.liferay.portlet.ratings.model.RatingsStats WHERE ");
484:
485: query.append("classNameId = ?");
486:
487: query.append(" AND ");
488:
489: query.append("classPK = ?");
490:
491: query.append(" ");
492:
493: Query q = session.createQuery(query.toString());
494:
495: int queryPos = 0;
496:
497: q.setLong(queryPos++, classNameId);
498:
499: q.setLong(queryPos++, classPK);
500:
501: Long count = null;
502:
503: Iterator itr = q.list().iterator();
504:
505: if (itr.hasNext()) {
506: count = (Long) itr.next();
507: }
508:
509: if (count == null) {
510: count = new Long(0);
511: }
512:
513: FinderCache.putResult(finderClassNameCacheEnabled,
514: finderClassName, finderMethodName,
515: finderParams, finderArgs, count);
516:
517: return count.intValue();
518: } catch (Exception e) {
519: throw HibernateUtil.processException(e);
520: } finally {
521: closeSession(session);
522: }
523: } else {
524: return ((Long) result).intValue();
525: }
526: }
527:
528: public int countAll() throws SystemException {
529: boolean finderClassNameCacheEnabled = RatingsStatsModelImpl.CACHE_ENABLED;
530: String finderClassName = RatingsStats.class.getName();
531: String finderMethodName = "countAll";
532: String[] finderParams = new String[] {};
533: Object[] finderArgs = new Object[] {};
534:
535: Object result = null;
536:
537: if (finderClassNameCacheEnabled) {
538: result = FinderCache.getResult(finderClassName,
539: finderMethodName, finderParams, finderArgs,
540: getSessionFactory());
541: }
542:
543: if (result == null) {
544: Session session = null;
545:
546: try {
547: session = openSession();
548:
549: Query q = session
550: .createQuery("SELECT COUNT(*) FROM com.liferay.portlet.ratings.model.RatingsStats");
551:
552: Long count = null;
553:
554: Iterator itr = q.list().iterator();
555:
556: if (itr.hasNext()) {
557: count = (Long) itr.next();
558: }
559:
560: if (count == null) {
561: count = new Long(0);
562: }
563:
564: FinderCache.putResult(finderClassNameCacheEnabled,
565: finderClassName, finderMethodName,
566: finderParams, finderArgs, count);
567:
568: return count.intValue();
569: } catch (Exception e) {
570: throw HibernateUtil.processException(e);
571: } finally {
572: closeSession(session);
573: }
574: } else {
575: return ((Long) result).intValue();
576: }
577: }
578:
579: protected void initDao() {
580: }
581:
582: private static ModelListener _getListener() {
583: if (Validator.isNotNull(_LISTENER)) {
584: try {
585: return (ModelListener) Class.forName(_LISTENER)
586: .newInstance();
587: } catch (Exception e) {
588: _log.error(e);
589: }
590: }
591:
592: return null;
593: }
594:
595: private static final String _LISTENER = GetterUtil
596: .getString(PropsUtil
597: .get("value.object.listener.com.liferay.portlet.ratings.model.RatingsStats"));
598: private static Log _log = LogFactory
599: .getLog(RatingsStatsPersistenceImpl.class);
600: }
|