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