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