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