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