001
014
015 package com.liferay.portlet.ratings.service.impl;
016
017 import com.liferay.portal.kernel.exception.PortalException;
018 import com.liferay.portal.kernel.json.JSONFactoryUtil;
019 import com.liferay.portal.kernel.json.JSONObject;
020 import com.liferay.portal.kernel.social.SocialActivityManagerUtil;
021 import com.liferay.portal.kernel.systemevent.SystemEvent;
022 import com.liferay.portal.model.SystemEventConstants;
023 import com.liferay.portal.model.User;
024 import com.liferay.portal.service.ServiceContext;
025 import com.liferay.portlet.asset.model.AssetEntry;
026 import com.liferay.portlet.blogs.model.BlogsEntry;
027 import com.liferay.portlet.blogs.model.BlogsStatsUser;
028 import com.liferay.portlet.ratings.exception.EntryScoreException;
029 import com.liferay.portlet.ratings.model.RatingsEntry;
030 import com.liferay.portlet.ratings.model.RatingsStats;
031 import com.liferay.portlet.ratings.service.base.RatingsEntryLocalServiceBaseImpl;
032 import com.liferay.portlet.social.model.SocialActivityConstants;
033
034 import java.util.List;
035
036
040 public class RatingsEntryLocalServiceImpl
041 extends RatingsEntryLocalServiceBaseImpl {
042
043 @Override
044 public void deleteEntry(long userId, String className, long classPK)
045 throws PortalException {
046
047 long classNameId = classNameLocalService.getClassNameId(className);
048
049 RatingsEntry entry = ratingsEntryPersistence.fetchByU_C_C(
050 userId, classNameId, classPK);
051
052 ratingsEntryLocalService.deleteEntry(entry, userId, className, classPK);
053 }
054
055 @Override
056 @SystemEvent(type = SystemEventConstants.TYPE_DELETE)
057 public void deleteEntry(
058 RatingsEntry entry, long userId, String className, long classPK)
059 throws PortalException {
060
061
062
063 if (entry == null) {
064 return;
065 }
066
067 long classNameId = classNameLocalService.getClassNameId(className);
068
069 double oldScore = entry.getScore();
070
071 ratingsEntryPersistence.removeByU_C_C(userId, classNameId, classPK);
072
073
074
075 RatingsStats stats = ratingsStatsLocalService.getStats(
076 className, classPK);
077
078 int totalEntries = stats.getTotalEntries() - 1;
079 double totalScore = stats.getTotalScore() - oldScore;
080 double averageScore = 0;
081
082 if (totalEntries > 0) {
083 averageScore = totalScore / totalEntries;
084 }
085
086 stats.setTotalEntries(totalEntries);
087 stats.setTotalScore(totalScore);
088 stats.setAverageScore(averageScore);
089
090 ratingsStatsPersistence.update(stats);
091 }
092
093 @Override
094 public RatingsEntry fetchEntry(
095 long userId, String className, long classPK) {
096
097 long classNameId = classNameLocalService.getClassNameId(className);
098
099 return ratingsEntryPersistence.fetchByU_C_C(
100 userId, classNameId, classPK);
101 }
102
103 @Override
104 public List<RatingsEntry> getEntries(
105 long userId, String className, List<Long> classPKs) {
106
107 long classNameId = classNameLocalService.getClassNameId(className);
108
109 return ratingsEntryFinder.findByU_C_C(userId, classNameId, classPKs);
110 }
111
112 @Override
113 public List<RatingsEntry> getEntries(String className, long classPK) {
114 long classNameId = classNameLocalService.getClassNameId(className);
115
116 return ratingsEntryPersistence.findByC_C(classNameId, classPK);
117 }
118
119 @Override
120 public List<RatingsEntry> getEntries(
121 String className, long classPK, double score) {
122
123 long classNameId = classNameLocalService.getClassNameId(className);
124
125 return ratingsEntryPersistence.findByC_C_S(classNameId, classPK, score);
126 }
127
128 @Override
129 public int getEntriesCount(String className, long classPK, double score) {
130 long classNameId = classNameLocalService.getClassNameId(className);
131
132 return ratingsEntryPersistence.countByC_C_S(
133 classNameId, classPK, score);
134 }
135
136 @Override
137 public RatingsEntry getEntry(long userId, String className, long classPK)
138 throws PortalException {
139
140 long classNameId = classNameLocalService.getClassNameId(className);
141
142 return ratingsEntryPersistence.findByU_C_C(
143 userId, classNameId, classPK);
144 }
145
146 @Override
147 public RatingsEntry updateEntry(
148 long userId, String className, long classPK, double score,
149 ServiceContext serviceContext)
150 throws PortalException {
151
152
153
154 boolean newEntry = false;
155
156 long classNameId = classNameLocalService.getClassNameId(className);
157 double oldScore = 0;
158
159 validate(score);
160
161 RatingsEntry entry = ratingsEntryPersistence.fetchByU_C_C(
162 userId, classNameId, classPK);
163
164 if (entry != null) {
165 oldScore = entry.getScore();
166
167 entry.setScore(score);
168
169 ratingsEntryPersistence.update(entry);
170
171
172
173 RatingsStats stats = ratingsStatsLocalService.getStats(
174 className, classPK);
175
176 stats.setTotalScore(stats.getTotalScore() - oldScore + score);
177 stats.setAverageScore(
178 stats.getTotalScore() / stats.getTotalEntries());
179
180 ratingsStatsPersistence.update(stats);
181 }
182 else {
183 newEntry = true;
184
185 User user = userPersistence.findByPrimaryKey(userId);
186
187 long entryId = counterLocalService.increment();
188
189 entry = ratingsEntryPersistence.create(entryId);
190
191 entry.setCompanyId(user.getCompanyId());
192 entry.setUserId(user.getUserId());
193 entry.setUserName(user.getFullName());
194 entry.setClassNameId(classNameId);
195 entry.setClassPK(classPK);
196 entry.setScore(score);
197
198 ratingsEntryPersistence.update(entry);
199
200
201
202 RatingsStats stats = ratingsStatsLocalService.getStats(
203 className, classPK);
204
205 stats.setTotalEntries(stats.getTotalEntries() + 1);
206 stats.setTotalScore(stats.getTotalScore() + score);
207 stats.setAverageScore(
208 stats.getTotalScore() / stats.getTotalEntries());
209
210 ratingsStatsPersistence.update(stats);
211 }
212
213
214
215 if (className.equals(BlogsEntry.class.getName())) {
216 BlogsEntry blogsEntry = blogsEntryPersistence.findByPrimaryKey(
217 classPK);
218
219 BlogsStatsUser blogsStatsUser =
220 blogsStatsUserLocalService.getStatsUser(
221 blogsEntry.getGroupId(), blogsEntry.getUserId());
222
223 int ratingsTotalEntries = blogsStatsUser.getRatingsTotalEntries();
224 double ratingsTotalScore = blogsStatsUser.getRatingsTotalScore();
225 double ratingsAverageScore =
226 blogsStatsUser.getRatingsAverageScore();
227
228 if (newEntry) {
229 ratingsTotalEntries++;
230 ratingsTotalScore += score;
231 }
232 else {
233 ratingsTotalScore = ratingsTotalScore - oldScore + score;
234 }
235
236 ratingsAverageScore = ratingsTotalScore / ratingsTotalEntries;
237
238 blogsStatsUser.setRatingsTotalEntries(ratingsTotalEntries);
239 blogsStatsUser.setRatingsTotalScore(ratingsTotalScore);
240 blogsStatsUser.setRatingsAverageScore(ratingsAverageScore);
241
242 blogsStatsUserPersistence.update(blogsStatsUser);
243 }
244
245
246
247 AssetEntry assetEntry = assetEntryLocalService.fetchEntry(
248 className, classPK);
249
250 if (assetEntry != null) {
251 JSONObject extraDataJSONObject = JSONFactoryUtil.createJSONObject();
252
253 extraDataJSONObject.put("title", assetEntry.getTitle());
254
255 SocialActivityManagerUtil.addActivity(
256 userId, assetEntry, SocialActivityConstants.TYPE_ADD_VOTE,
257 extraDataJSONObject.toString(), 0);
258 }
259
260 return entry;
261 }
262
263 protected void validate(double score) throws PortalException {
264 if ((score > 1) || (score < 0)) {
265 throw new EntryScoreException(
266 "Score " + score + " is not a value between 0 and 1");
267 }
268 }
269
270 }