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