1
14
15 package com.liferay.portlet.ratings.service.impl;
16
17 import com.liferay.portal.PortalException;
18 import com.liferay.portal.SystemException;
19 import com.liferay.portal.kernel.log.Log;
20 import com.liferay.portal.kernel.log.LogFactoryUtil;
21 import com.liferay.portal.util.PortalUtil;
22 import com.liferay.portlet.ratings.NoSuchStatsException;
23 import com.liferay.portlet.ratings.model.RatingsStats;
24 import com.liferay.portlet.ratings.service.base.RatingsStatsLocalServiceBaseImpl;
25
26
32 public class RatingsStatsLocalServiceImpl
33 extends RatingsStatsLocalServiceBaseImpl {
34
35 public RatingsStats addStats(long classNameId, long classPK)
36 throws SystemException {
37
38 long statsId = counterLocalService.increment();
39
40 RatingsStats stats = ratingsStatsPersistence.create(statsId);
41
42 stats.setClassNameId(classNameId);
43 stats.setClassPK(classPK);
44 stats.setTotalEntries(0);
45 stats.setTotalScore(0.0);
46 stats.setAverageScore(0.0);
47
48 try {
49 ratingsStatsPersistence.update(stats, false);
50 }
51 catch (SystemException se) {
52 if (_log.isWarnEnabled()) {
53 _log.warn(
54 "Add failed, fetch {classNameId=" + classNameId +
55 ", classPK=" + classPK + "}");
56 }
57
58 stats = ratingsStatsPersistence.fetchByC_C(
59 classNameId, classPK, false);
60
61 if (stats == null) {
62 throw se;
63 }
64 }
65
66 return stats;
67 }
68
69 public void deleteStats(String className, long classPK)
70 throws SystemException {
71
72 long classNameId = PortalUtil.getClassNameId(className);
73
74 try {
75 ratingsStatsPersistence.removeByC_C(classNameId, classPK);
76 }
77 catch (NoSuchStatsException nsse) {
78 _log.warn(nsse);
79 }
80
81 ratingsEntryPersistence.removeByC_C(classNameId, classPK);
82 }
83
84 public RatingsStats getStats(long statsId)
85 throws PortalException, SystemException {
86
87 return ratingsStatsPersistence.findByPrimaryKey(statsId);
88 }
89
90 public RatingsStats getStats(String className, long classPK)
91 throws SystemException {
92
93 long classNameId = PortalUtil.getClassNameId(className);
94
95 RatingsStats stats = ratingsStatsPersistence.fetchByC_C(
96 classNameId, classPK);
97
98 if (stats == null) {
99 stats = ratingsStatsLocalService.addStats(classNameId, classPK);
100 }
101
102 return stats;
103 }
104
105 private static Log _log = LogFactoryUtil.getLog(
106 RatingsStatsLocalServiceImpl.class);
107
108 }