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