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