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