1
22
23 package com.liferay.portlet.ratings.service.persistence;
24
25 import com.liferay.portal.SystemException;
26 import com.liferay.portal.kernel.annotation.BeanReference;
27 import com.liferay.portal.kernel.cache.CacheRegistry;
28 import com.liferay.portal.kernel.dao.orm.DynamicQuery;
29 import com.liferay.portal.kernel.dao.orm.EntityCacheUtil;
30 import com.liferay.portal.kernel.dao.orm.FinderCacheUtil;
31 import com.liferay.portal.kernel.dao.orm.FinderPath;
32 import com.liferay.portal.kernel.dao.orm.Query;
33 import com.liferay.portal.kernel.dao.orm.QueryPos;
34 import com.liferay.portal.kernel.dao.orm.QueryUtil;
35 import com.liferay.portal.kernel.dao.orm.Session;
36 import com.liferay.portal.kernel.log.Log;
37 import com.liferay.portal.kernel.log.LogFactoryUtil;
38 import com.liferay.portal.kernel.util.GetterUtil;
39 import com.liferay.portal.kernel.util.OrderByComparator;
40 import com.liferay.portal.kernel.util.StringPool;
41 import com.liferay.portal.kernel.util.StringUtil;
42 import com.liferay.portal.model.ModelListener;
43 import com.liferay.portal.service.persistence.BatchSessionUtil;
44 import com.liferay.portal.service.persistence.impl.BasePersistenceImpl;
45
46 import com.liferay.portlet.ratings.NoSuchStatsException;
47 import com.liferay.portlet.ratings.model.RatingsStats;
48 import com.liferay.portlet.ratings.model.impl.RatingsStatsImpl;
49 import com.liferay.portlet.ratings.model.impl.RatingsStatsModelImpl;
50
51 import java.util.ArrayList;
52 import java.util.Collections;
53 import java.util.List;
54
55
68 public class RatingsStatsPersistenceImpl extends BasePersistenceImpl
69 implements RatingsStatsPersistence {
70 public static final String FINDER_CLASS_NAME_ENTITY = RatingsStatsImpl.class.getName();
71 public static final String FINDER_CLASS_NAME_LIST = FINDER_CLASS_NAME_ENTITY +
72 ".List";
73 public static final FinderPath FINDER_PATH_FETCH_BY_C_C = new FinderPath(RatingsStatsModelImpl.ENTITY_CACHE_ENABLED,
74 RatingsStatsModelImpl.FINDER_CACHE_ENABLED,
75 FINDER_CLASS_NAME_ENTITY, "fetchByC_C",
76 new String[] { Long.class.getName(), Long.class.getName() });
77 public static final FinderPath FINDER_PATH_COUNT_BY_C_C = new FinderPath(RatingsStatsModelImpl.ENTITY_CACHE_ENABLED,
78 RatingsStatsModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
79 "countByC_C",
80 new String[] { Long.class.getName(), Long.class.getName() });
81 public static final FinderPath FINDER_PATH_FIND_ALL = new FinderPath(RatingsStatsModelImpl.ENTITY_CACHE_ENABLED,
82 RatingsStatsModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
83 "findAll", new String[0]);
84 public static final FinderPath FINDER_PATH_COUNT_ALL = new FinderPath(RatingsStatsModelImpl.ENTITY_CACHE_ENABLED,
85 RatingsStatsModelImpl.FINDER_CACHE_ENABLED, FINDER_CLASS_NAME_LIST,
86 "countAll", new String[0]);
87
88 public void cacheResult(RatingsStats ratingsStats) {
89 EntityCacheUtil.putResult(RatingsStatsModelImpl.ENTITY_CACHE_ENABLED,
90 RatingsStatsImpl.class, ratingsStats.getPrimaryKey(), ratingsStats);
91
92 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
93 new Object[] {
94 new Long(ratingsStats.getClassNameId()),
95 new Long(ratingsStats.getClassPK())
96 }, ratingsStats);
97 }
98
99 public void cacheResult(List<RatingsStats> ratingsStatses) {
100 for (RatingsStats ratingsStats : ratingsStatses) {
101 if (EntityCacheUtil.getResult(
102 RatingsStatsModelImpl.ENTITY_CACHE_ENABLED,
103 RatingsStatsImpl.class, ratingsStats.getPrimaryKey(),
104 this) == null) {
105 cacheResult(ratingsStats);
106 }
107 }
108 }
109
110 public void clearCache() {
111 CacheRegistry.clear(RatingsStatsImpl.class.getName());
112 EntityCacheUtil.clearCache(RatingsStatsImpl.class.getName());
113 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_ENTITY);
114 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
115 }
116
117 public RatingsStats create(long statsId) {
118 RatingsStats ratingsStats = new RatingsStatsImpl();
119
120 ratingsStats.setNew(true);
121 ratingsStats.setPrimaryKey(statsId);
122
123 return ratingsStats;
124 }
125
126 public RatingsStats remove(long statsId)
127 throws NoSuchStatsException, SystemException {
128 Session session = null;
129
130 try {
131 session = openSession();
132
133 RatingsStats ratingsStats = (RatingsStats)session.get(RatingsStatsImpl.class,
134 new Long(statsId));
135
136 if (ratingsStats == null) {
137 if (_log.isWarnEnabled()) {
138 _log.warn("No RatingsStats exists with the primary key " +
139 statsId);
140 }
141
142 throw new NoSuchStatsException(
143 "No RatingsStats exists with the primary key " + statsId);
144 }
145
146 return remove(ratingsStats);
147 }
148 catch (NoSuchStatsException nsee) {
149 throw nsee;
150 }
151 catch (Exception e) {
152 throw processException(e);
153 }
154 finally {
155 closeSession(session);
156 }
157 }
158
159 public RatingsStats remove(RatingsStats ratingsStats)
160 throws SystemException {
161 for (ModelListener<RatingsStats> listener : listeners) {
162 listener.onBeforeRemove(ratingsStats);
163 }
164
165 ratingsStats = removeImpl(ratingsStats);
166
167 for (ModelListener<RatingsStats> listener : listeners) {
168 listener.onAfterRemove(ratingsStats);
169 }
170
171 return ratingsStats;
172 }
173
174 protected RatingsStats removeImpl(RatingsStats ratingsStats)
175 throws SystemException {
176 ratingsStats = toUnwrappedModel(ratingsStats);
177
178 Session session = null;
179
180 try {
181 session = openSession();
182
183 if (ratingsStats.isCachedModel() || BatchSessionUtil.isEnabled()) {
184 Object staleObject = session.get(RatingsStatsImpl.class,
185 ratingsStats.getPrimaryKeyObj());
186
187 if (staleObject != null) {
188 session.evict(staleObject);
189 }
190 }
191
192 session.delete(ratingsStats);
193
194 session.flush();
195 }
196 catch (Exception e) {
197 throw processException(e);
198 }
199 finally {
200 closeSession(session);
201 }
202
203 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
204
205 RatingsStatsModelImpl ratingsStatsModelImpl = (RatingsStatsModelImpl)ratingsStats;
206
207 FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_C,
208 new Object[] {
209 new Long(ratingsStatsModelImpl.getOriginalClassNameId()),
210 new Long(ratingsStatsModelImpl.getOriginalClassPK())
211 });
212
213 EntityCacheUtil.removeResult(RatingsStatsModelImpl.ENTITY_CACHE_ENABLED,
214 RatingsStatsImpl.class, ratingsStats.getPrimaryKey());
215
216 return ratingsStats;
217 }
218
219
222 public RatingsStats update(RatingsStats ratingsStats)
223 throws SystemException {
224 if (_log.isWarnEnabled()) {
225 _log.warn(
226 "Using the deprecated update(RatingsStats ratingsStats) method. Use update(RatingsStats ratingsStats, boolean merge) instead.");
227 }
228
229 return update(ratingsStats, false);
230 }
231
232
244 public RatingsStats update(RatingsStats ratingsStats, boolean merge)
245 throws SystemException {
246 boolean isNew = ratingsStats.isNew();
247
248 for (ModelListener<RatingsStats> listener : listeners) {
249 if (isNew) {
250 listener.onBeforeCreate(ratingsStats);
251 }
252 else {
253 listener.onBeforeUpdate(ratingsStats);
254 }
255 }
256
257 ratingsStats = updateImpl(ratingsStats, merge);
258
259 for (ModelListener<RatingsStats> listener : listeners) {
260 if (isNew) {
261 listener.onAfterCreate(ratingsStats);
262 }
263 else {
264 listener.onAfterUpdate(ratingsStats);
265 }
266 }
267
268 return ratingsStats;
269 }
270
271 public RatingsStats updateImpl(
272 com.liferay.portlet.ratings.model.RatingsStats ratingsStats,
273 boolean merge) throws SystemException {
274 ratingsStats = toUnwrappedModel(ratingsStats);
275
276 boolean isNew = ratingsStats.isNew();
277
278 RatingsStatsModelImpl ratingsStatsModelImpl = (RatingsStatsModelImpl)ratingsStats;
279
280 Session session = null;
281
282 try {
283 session = openSession();
284
285 BatchSessionUtil.update(session, ratingsStats, merge);
286
287 ratingsStats.setNew(false);
288 }
289 catch (Exception e) {
290 throw processException(e);
291 }
292 finally {
293 closeSession(session);
294 }
295
296 FinderCacheUtil.clearCache(FINDER_CLASS_NAME_LIST);
297
298 EntityCacheUtil.putResult(RatingsStatsModelImpl.ENTITY_CACHE_ENABLED,
299 RatingsStatsImpl.class, ratingsStats.getPrimaryKey(), ratingsStats);
300
301 if (!isNew &&
302 ((ratingsStats.getClassNameId() != ratingsStatsModelImpl.getOriginalClassNameId()) ||
303 (ratingsStats.getClassPK() != ratingsStatsModelImpl.getOriginalClassPK()))) {
304 FinderCacheUtil.removeResult(FINDER_PATH_FETCH_BY_C_C,
305 new Object[] {
306 new Long(ratingsStatsModelImpl.getOriginalClassNameId()),
307 new Long(ratingsStatsModelImpl.getOriginalClassPK())
308 });
309 }
310
311 if (isNew ||
312 ((ratingsStats.getClassNameId() != ratingsStatsModelImpl.getOriginalClassNameId()) ||
313 (ratingsStats.getClassPK() != ratingsStatsModelImpl.getOriginalClassPK()))) {
314 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
315 new Object[] {
316 new Long(ratingsStats.getClassNameId()),
317 new Long(ratingsStats.getClassPK())
318 }, ratingsStats);
319 }
320
321 return ratingsStats;
322 }
323
324 protected RatingsStats toUnwrappedModel(RatingsStats ratingsStats) {
325 if (ratingsStats instanceof RatingsStatsImpl) {
326 return ratingsStats;
327 }
328
329 RatingsStatsImpl ratingsStatsImpl = new RatingsStatsImpl();
330
331 ratingsStatsImpl.setNew(ratingsStats.isNew());
332 ratingsStatsImpl.setPrimaryKey(ratingsStats.getPrimaryKey());
333
334 ratingsStatsImpl.setStatsId(ratingsStats.getStatsId());
335 ratingsStatsImpl.setClassNameId(ratingsStats.getClassNameId());
336 ratingsStatsImpl.setClassPK(ratingsStats.getClassPK());
337 ratingsStatsImpl.setTotalEntries(ratingsStats.getTotalEntries());
338 ratingsStatsImpl.setTotalScore(ratingsStats.getTotalScore());
339 ratingsStatsImpl.setAverageScore(ratingsStats.getAverageScore());
340
341 return ratingsStatsImpl;
342 }
343
344 public RatingsStats findByPrimaryKey(long statsId)
345 throws NoSuchStatsException, SystemException {
346 RatingsStats ratingsStats = fetchByPrimaryKey(statsId);
347
348 if (ratingsStats == null) {
349 if (_log.isWarnEnabled()) {
350 _log.warn("No RatingsStats exists with the primary key " +
351 statsId);
352 }
353
354 throw new NoSuchStatsException(
355 "No RatingsStats exists with the primary key " + statsId);
356 }
357
358 return ratingsStats;
359 }
360
361 public RatingsStats fetchByPrimaryKey(long statsId)
362 throws SystemException {
363 RatingsStats ratingsStats = (RatingsStats)EntityCacheUtil.getResult(RatingsStatsModelImpl.ENTITY_CACHE_ENABLED,
364 RatingsStatsImpl.class, statsId, this);
365
366 if (ratingsStats == null) {
367 Session session = null;
368
369 try {
370 session = openSession();
371
372 ratingsStats = (RatingsStats)session.get(RatingsStatsImpl.class,
373 new Long(statsId));
374 }
375 catch (Exception e) {
376 throw processException(e);
377 }
378 finally {
379 if (ratingsStats != null) {
380 cacheResult(ratingsStats);
381 }
382
383 closeSession(session);
384 }
385 }
386
387 return ratingsStats;
388 }
389
390 public RatingsStats findByC_C(long classNameId, long classPK)
391 throws NoSuchStatsException, SystemException {
392 RatingsStats ratingsStats = fetchByC_C(classNameId, classPK);
393
394 if (ratingsStats == null) {
395 StringBuilder msg = new StringBuilder();
396
397 msg.append("No RatingsStats exists with the key {");
398
399 msg.append("classNameId=" + classNameId);
400
401 msg.append(", ");
402 msg.append("classPK=" + classPK);
403
404 msg.append(StringPool.CLOSE_CURLY_BRACE);
405
406 if (_log.isWarnEnabled()) {
407 _log.warn(msg.toString());
408 }
409
410 throw new NoSuchStatsException(msg.toString());
411 }
412
413 return ratingsStats;
414 }
415
416 public RatingsStats fetchByC_C(long classNameId, long classPK)
417 throws SystemException {
418 return fetchByC_C(classNameId, classPK, true);
419 }
420
421 public RatingsStats fetchByC_C(long classNameId, long classPK,
422 boolean retrieveFromCache) throws SystemException {
423 Object[] finderArgs = new Object[] {
424 new Long(classNameId), new Long(classPK)
425 };
426
427 Object result = null;
428
429 if (retrieveFromCache) {
430 result = FinderCacheUtil.getResult(FINDER_PATH_FETCH_BY_C_C,
431 finderArgs, this);
432 }
433
434 if (result == null) {
435 Session session = null;
436
437 try {
438 session = openSession();
439
440 StringBuilder query = new StringBuilder();
441
442 query.append(
443 "SELECT ratingsStats FROM RatingsStats ratingsStats WHERE ");
444
445 query.append("ratingsStats.classNameId = ?");
446
447 query.append(" AND ");
448
449 query.append("ratingsStats.classPK = ?");
450
451 query.append(" ");
452
453 Query q = session.createQuery(query.toString());
454
455 QueryPos qPos = QueryPos.getInstance(q);
456
457 qPos.add(classNameId);
458
459 qPos.add(classPK);
460
461 List<RatingsStats> list = q.list();
462
463 result = list;
464
465 RatingsStats ratingsStats = null;
466
467 if (list.isEmpty()) {
468 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
469 finderArgs, list);
470 }
471 else {
472 ratingsStats = list.get(0);
473
474 cacheResult(ratingsStats);
475
476 if ((ratingsStats.getClassNameId() != classNameId) ||
477 (ratingsStats.getClassPK() != classPK)) {
478 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
479 finderArgs, ratingsStats);
480 }
481 }
482
483 return ratingsStats;
484 }
485 catch (Exception e) {
486 throw processException(e);
487 }
488 finally {
489 if (result == null) {
490 FinderCacheUtil.putResult(FINDER_PATH_FETCH_BY_C_C,
491 finderArgs, new ArrayList<RatingsStats>());
492 }
493
494 closeSession(session);
495 }
496 }
497 else {
498 if (result instanceof List<?>) {
499 return null;
500 }
501 else {
502 return (RatingsStats)result;
503 }
504 }
505 }
506
507 public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery)
508 throws SystemException {
509 Session session = null;
510
511 try {
512 session = openSession();
513
514 dynamicQuery.compile(session);
515
516 return dynamicQuery.list();
517 }
518 catch (Exception e) {
519 throw processException(e);
520 }
521 finally {
522 closeSession(session);
523 }
524 }
525
526 public List<Object> findWithDynamicQuery(DynamicQuery dynamicQuery,
527 int start, int end) throws SystemException {
528 Session session = null;
529
530 try {
531 session = openSession();
532
533 dynamicQuery.setLimit(start, end);
534
535 dynamicQuery.compile(session);
536
537 return dynamicQuery.list();
538 }
539 catch (Exception e) {
540 throw processException(e);
541 }
542 finally {
543 closeSession(session);
544 }
545 }
546
547 public List<RatingsStats> findAll() throws SystemException {
548 return findAll(QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
549 }
550
551 public List<RatingsStats> findAll(int start, int end)
552 throws SystemException {
553 return findAll(start, end, null);
554 }
555
556 public List<RatingsStats> findAll(int start, int end, OrderByComparator obc)
557 throws SystemException {
558 Object[] finderArgs = new Object[] {
559 String.valueOf(start), String.valueOf(end), String.valueOf(obc)
560 };
561
562 List<RatingsStats> list = (List<RatingsStats>)FinderCacheUtil.getResult(FINDER_PATH_FIND_ALL,
563 finderArgs, this);
564
565 if (list == null) {
566 Session session = null;
567
568 try {
569 session = openSession();
570
571 StringBuilder query = new StringBuilder();
572
573 query.append(
574 "SELECT ratingsStats FROM RatingsStats ratingsStats ");
575
576 if (obc != null) {
577 query.append("ORDER BY ");
578
579 String[] orderByFields = obc.getOrderByFields();
580
581 for (int i = 0; i < orderByFields.length; i++) {
582 query.append("ratingsStats.");
583 query.append(orderByFields[i]);
584
585 if (obc.isAscending()) {
586 query.append(" ASC");
587 }
588 else {
589 query.append(" DESC");
590 }
591
592 if ((i + 1) < orderByFields.length) {
593 query.append(", ");
594 }
595 }
596 }
597
598 Query q = session.createQuery(query.toString());
599
600 if (obc == null) {
601 list = (List<RatingsStats>)QueryUtil.list(q, getDialect(),
602 start, end, false);
603
604 Collections.sort(list);
605 }
606 else {
607 list = (List<RatingsStats>)QueryUtil.list(q, getDialect(),
608 start, end);
609 }
610 }
611 catch (Exception e) {
612 throw processException(e);
613 }
614 finally {
615 if (list == null) {
616 list = new ArrayList<RatingsStats>();
617 }
618
619 cacheResult(list);
620
621 FinderCacheUtil.putResult(FINDER_PATH_FIND_ALL, finderArgs, list);
622
623 closeSession(session);
624 }
625 }
626
627 return list;
628 }
629
630 public void removeByC_C(long classNameId, long classPK)
631 throws NoSuchStatsException, SystemException {
632 RatingsStats ratingsStats = findByC_C(classNameId, classPK);
633
634 remove(ratingsStats);
635 }
636
637 public void removeAll() throws SystemException {
638 for (RatingsStats ratingsStats : findAll()) {
639 remove(ratingsStats);
640 }
641 }
642
643 public int countByC_C(long classNameId, long classPK)
644 throws SystemException {
645 Object[] finderArgs = new Object[] {
646 new Long(classNameId), new Long(classPK)
647 };
648
649 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_BY_C_C,
650 finderArgs, this);
651
652 if (count == null) {
653 Session session = null;
654
655 try {
656 session = openSession();
657
658 StringBuilder query = new StringBuilder();
659
660 query.append("SELECT COUNT(ratingsStats) ");
661 query.append("FROM RatingsStats ratingsStats WHERE ");
662
663 query.append("ratingsStats.classNameId = ?");
664
665 query.append(" AND ");
666
667 query.append("ratingsStats.classPK = ?");
668
669 query.append(" ");
670
671 Query q = session.createQuery(query.toString());
672
673 QueryPos qPos = QueryPos.getInstance(q);
674
675 qPos.add(classNameId);
676
677 qPos.add(classPK);
678
679 count = (Long)q.uniqueResult();
680 }
681 catch (Exception e) {
682 throw processException(e);
683 }
684 finally {
685 if (count == null) {
686 count = Long.valueOf(0);
687 }
688
689 FinderCacheUtil.putResult(FINDER_PATH_COUNT_BY_C_C, finderArgs,
690 count);
691
692 closeSession(session);
693 }
694 }
695
696 return count.intValue();
697 }
698
699 public int countAll() throws SystemException {
700 Object[] finderArgs = new Object[0];
701
702 Long count = (Long)FinderCacheUtil.getResult(FINDER_PATH_COUNT_ALL,
703 finderArgs, this);
704
705 if (count == null) {
706 Session session = null;
707
708 try {
709 session = openSession();
710
711 Query q = session.createQuery(
712 "SELECT COUNT(ratingsStats) FROM RatingsStats ratingsStats");
713
714 count = (Long)q.uniqueResult();
715 }
716 catch (Exception e) {
717 throw processException(e);
718 }
719 finally {
720 if (count == null) {
721 count = Long.valueOf(0);
722 }
723
724 FinderCacheUtil.putResult(FINDER_PATH_COUNT_ALL, finderArgs,
725 count);
726
727 closeSession(session);
728 }
729 }
730
731 return count.intValue();
732 }
733
734 public void afterPropertiesSet() {
735 String[] listenerClassNames = StringUtil.split(GetterUtil.getString(
736 com.liferay.portal.util.PropsUtil.get(
737 "value.object.listener.com.liferay.portlet.ratings.model.RatingsStats")));
738
739 if (listenerClassNames.length > 0) {
740 try {
741 List<ModelListener<RatingsStats>> listenersList = new ArrayList<ModelListener<RatingsStats>>();
742
743 for (String listenerClassName : listenerClassNames) {
744 listenersList.add((ModelListener<RatingsStats>)Class.forName(
745 listenerClassName).newInstance());
746 }
747
748 listeners = listenersList.toArray(new ModelListener[listenersList.size()]);
749 }
750 catch (Exception e) {
751 _log.error(e);
752 }
753 }
754 }
755
756 @BeanReference(name = "com.liferay.portlet.ratings.service.persistence.RatingsEntryPersistence.impl")
757 protected com.liferay.portlet.ratings.service.persistence.RatingsEntryPersistence ratingsEntryPersistence;
758 @BeanReference(name = "com.liferay.portlet.ratings.service.persistence.RatingsStatsPersistence.impl")
759 protected com.liferay.portlet.ratings.service.persistence.RatingsStatsPersistence ratingsStatsPersistence;
760 @BeanReference(name = "com.liferay.portal.service.persistence.ResourcePersistence.impl")
761 protected com.liferay.portal.service.persistence.ResourcePersistence resourcePersistence;
762 @BeanReference(name = "com.liferay.portal.service.persistence.UserPersistence.impl")
763 protected com.liferay.portal.service.persistence.UserPersistence userPersistence;
764 private static Log _log = LogFactoryUtil.getLog(RatingsStatsPersistenceImpl.class);
765 }