001
014
015 package com.liferay.portlet.ratings.service.impl;
016
017 import com.liferay.portal.kernel.configuration.Filter;
018 import com.liferay.portal.kernel.exception.PortalException;
019 import com.liferay.portal.kernel.exception.SystemException;
020 import com.liferay.portal.kernel.json.JSONFactoryUtil;
021 import com.liferay.portal.kernel.json.JSONObject;
022 import com.liferay.portal.kernel.search.Indexer;
023 import com.liferay.portal.kernel.search.IndexerRegistryUtil;
024 import com.liferay.portal.kernel.util.GetterUtil;
025 import com.liferay.portal.kernel.util.PropsKeys;
026 import com.liferay.portal.kernel.util.PropsUtil;
027 import com.liferay.portal.model.User;
028 import com.liferay.portal.service.ServiceContext;
029 import com.liferay.portal.util.PortalUtil;
030 import com.liferay.portal.util.PropsValues;
031 import com.liferay.portlet.asset.model.AssetEntry;
032 import com.liferay.portlet.blogs.model.BlogsEntry;
033 import com.liferay.portlet.blogs.model.BlogsStatsUser;
034 import com.liferay.portlet.ratings.EntryScoreException;
035 import com.liferay.portlet.ratings.model.RatingsEntry;
036 import com.liferay.portlet.ratings.model.RatingsStats;
037 import com.liferay.portlet.ratings.service.base.RatingsEntryLocalServiceBaseImpl;
038 import com.liferay.portlet.social.model.SocialActivityConstants;
039
040 import java.util.Date;
041 import java.util.List;
042
043
047 public class RatingsEntryLocalServiceImpl
048 extends RatingsEntryLocalServiceBaseImpl {
049
050 @Override
051 public void deleteEntry(long userId, String className, long classPK)
052 throws PortalException, SystemException {
053
054
055
056 long classNameId = PortalUtil.getClassNameId(className);
057
058 RatingsEntry entry = ratingsEntryPersistence.fetchByU_C_C(
059 userId, classNameId, classPK);
060
061 if (entry == null) {
062 return;
063 }
064
065 double oldScore = entry.getScore();
066
067 ratingsEntryPersistence.removeByU_C_C(userId, classNameId, classPK);
068
069
070
071 RatingsStats stats = ratingsStatsLocalService.getStats(
072 className, classPK);
073
074 int totalEntries = stats.getTotalEntries() - 1;
075 double totalScore = stats.getTotalScore() - oldScore;
076 double averageScore = 0;
077
078 if (totalEntries > 0) {
079 averageScore = totalScore / totalEntries;
080 }
081
082 stats.setTotalEntries(totalEntries);
083 stats.setTotalScore(totalScore);
084 stats.setAverageScore(averageScore);
085
086 ratingsStatsPersistence.update(stats);
087
088
089
090 reindex(stats);
091 }
092
093 @Override
094 public RatingsEntry fetchEntry(long userId, String className, long classPK)
095 throws SystemException {
096
097 long classNameId = PortalUtil.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 throws SystemException {
107
108 long classNameId = PortalUtil.getClassNameId(className);
109
110 return ratingsEntryFinder.findByU_C_C(userId, classNameId, classPKs);
111 }
112
113 @Override
114 public List<RatingsEntry> getEntries(String className, long classPK)
115 throws SystemException {
116
117 long classNameId = PortalUtil.getClassNameId(className);
118
119 return ratingsEntryPersistence.findByC_C(classNameId, classPK);
120 }
121
122 @Override
123 public List<RatingsEntry> getEntries(
124 String className, long classPK, double score)
125 throws SystemException {
126
127 long classNameId = PortalUtil.getClassNameId(className);
128
129 return ratingsEntryPersistence.findByC_C_S(classNameId, classPK, score);
130 }
131
132 @Override
133 public int getEntriesCount(String className, long classPK, double score)
134 throws SystemException {
135
136 long classNameId = PortalUtil.getClassNameId(className);
137
138 return ratingsEntryPersistence.countByC_C_S(
139 classNameId, classPK, score);
140 }
141
142 @Override
143 public RatingsEntry getEntry(long userId, String className, long classPK)
144 throws PortalException, SystemException {
145
146 long classNameId = PortalUtil.getClassNameId(className);
147
148 return ratingsEntryPersistence.findByU_C_C(
149 userId, classNameId, classPK);
150 }
151
152 @Override
153 public RatingsEntry updateEntry(
154 long userId, String className, long classPK, double score,
155 ServiceContext serviceContext)
156 throws PortalException, SystemException {
157
158
159
160 boolean newEntry = false;
161
162 long classNameId = PortalUtil.getClassNameId(className);
163 double oldScore = 0;
164 Date now = new Date();
165
166 validate(className, score);
167
168 RatingsEntry entry = ratingsEntryPersistence.fetchByU_C_C(
169 userId, classNameId, classPK);
170
171 if (entry != null) {
172 oldScore = entry.getScore();
173
174 entry.setModifiedDate(serviceContext.getModifiedDate(now));
175 entry.setScore(score);
176
177 ratingsEntryPersistence.update(entry);
178
179
180
181 RatingsStats stats = ratingsStatsLocalService.getStats(
182 className, classPK);
183
184 stats.setTotalScore(stats.getTotalScore() - oldScore + score);
185 stats.setAverageScore(
186 stats.getTotalScore() / stats.getTotalEntries());
187
188 ratingsStatsPersistence.update(stats);
189
190
191
192 reindex(stats);
193 }
194 else {
195 newEntry = true;
196
197 User user = userPersistence.findByPrimaryKey(userId);
198
199 long entryId = counterLocalService.increment();
200
201 entry = ratingsEntryPersistence.create(entryId);
202
203 entry.setCompanyId(user.getCompanyId());
204 entry.setUserId(user.getUserId());
205 entry.setUserName(user.getFullName());
206 entry.setCreateDate(serviceContext.getCreateDate(now));
207 entry.setModifiedDate(serviceContext.getModifiedDate(now));
208 entry.setClassNameId(classNameId);
209 entry.setClassPK(classPK);
210 entry.setScore(score);
211
212 ratingsEntryPersistence.update(entry);
213
214
215
216 RatingsStats stats = ratingsStatsLocalService.getStats(
217 className, classPK);
218
219 stats.setTotalEntries(stats.getTotalEntries() + 1);
220 stats.setTotalScore(stats.getTotalScore() + score);
221 stats.setAverageScore(
222 stats.getTotalScore() / stats.getTotalEntries());
223
224 ratingsStatsPersistence.update(stats);
225
226
227
228 reindex(stats);
229 }
230
231
232
233 if (className.equals(BlogsEntry.class.getName())) {
234 BlogsEntry blogsEntry = blogsEntryPersistence.findByPrimaryKey(
235 classPK);
236
237 BlogsStatsUser blogsStatsUser =
238 blogsStatsUserLocalService.getStatsUser(
239 blogsEntry.getGroupId(), blogsEntry.getUserId());
240
241 int ratingsTotalEntries = blogsStatsUser.getRatingsTotalEntries();
242 double ratingsTotalScore = blogsStatsUser.getRatingsTotalScore();
243 double ratingsAverageScore =
244 blogsStatsUser.getRatingsAverageScore();
245
246 if (newEntry) {
247 ratingsTotalEntries++;
248 ratingsTotalScore += score;
249 }
250 else {
251 ratingsTotalScore = ratingsTotalScore - oldScore + score;
252 }
253
254 ratingsAverageScore = ratingsTotalScore / ratingsTotalEntries;
255
256 blogsStatsUser.setRatingsTotalEntries(ratingsTotalEntries);
257 blogsStatsUser.setRatingsTotalScore(ratingsTotalScore);
258 blogsStatsUser.setRatingsAverageScore(ratingsAverageScore);
259
260 blogsStatsUserPersistence.update(blogsStatsUser);
261 }
262
263
264
265 AssetEntry assetEntry = assetEntryLocalService.fetchEntry(
266 className, classPK);
267
268 if (assetEntry != null) {
269 JSONObject extraDataJSONObject = JSONFactoryUtil.createJSONObject();
270
271 extraDataJSONObject.put("title", assetEntry.getTitle());
272
273 socialActivityLocalService.addActivity(
274 userId, assetEntry.getGroupId(), className, classPK,
275 SocialActivityConstants.TYPE_ADD_VOTE,
276 extraDataJSONObject.toString(), 0);
277 }
278
279 return entry;
280 }
281
282 protected void reindex(RatingsStats stats) throws PortalException {
283 String className = PortalUtil.getClassName(stats.getClassNameId());
284
285 Indexer indexer = IndexerRegistryUtil.nullSafeGetIndexer(className);
286
287 indexer.reindex(className, stats.getClassPK());
288 }
289
290 protected void validate(String className, double score)
291 throws PortalException {
292
293 Filter filter = new Filter(className);
294
295 double maxScore = GetterUtil.getInteger(
296 PropsUtil.get(PropsKeys.RATINGS_MAX_SCORE, filter),
297 PropsValues.RATINGS_DEFAULT_NUMBER_OF_STARS);
298 double minScore = GetterUtil.getInteger(
299 PropsUtil.get(PropsKeys.RATINGS_MIN_SCORE, filter));
300
301 if ((score < minScore) || (score > maxScore)) {
302 throw new EntryScoreException();
303 }
304 }
305
306 }