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