1
22
23 package com.liferay.portlet.ratings.service.impl;
24
25 import com.liferay.portal.PortalException;
26 import com.liferay.portal.SystemException;
27 import com.liferay.portal.model.User;
28 import com.liferay.portal.util.PortalUtil;
29 import com.liferay.portlet.blogs.model.BlogsEntry;
30 import com.liferay.portlet.blogs.model.BlogsStatsUser;
31 import com.liferay.portlet.ratings.model.RatingsEntry;
32 import com.liferay.portlet.ratings.model.RatingsStats;
33 import com.liferay.portlet.ratings.service.base.RatingsEntryLocalServiceBaseImpl;
34
35 import java.util.Date;
36 import java.util.List;
37
38
44 public class RatingsEntryLocalServiceImpl
45 extends RatingsEntryLocalServiceBaseImpl {
46
47 public void deleteEntry(long userId, String className, long classPK)
48 throws PortalException, SystemException {
49
50
52 long classNameId = PortalUtil.getClassNameId(className);
53
54 RatingsEntry entry = ratingsEntryPersistence.fetchByU_C_C(
55 userId, classNameId, classPK);
56
57 if (entry == null) {
58 return;
59 }
60
61 double oldScore = entry.getScore();
62
63 ratingsEntryPersistence.removeByU_C_C(userId, classNameId, classPK);
64
65
67 RatingsStats stats = ratingsStatsLocalService.getStats(
68 className, classPK);
69
70 int totalEntries = stats.getTotalEntries() - 1;
71 double totalScore = stats.getTotalScore() - oldScore;
72 double averageScore = 0;
73
74 if (totalEntries > 0) {
75 averageScore = totalScore / totalEntries;
76 }
77
78 stats.setTotalEntries(totalEntries);
79 stats.setTotalScore(totalScore);
80 stats.setAverageScore(averageScore);
81
82 ratingsStatsPersistence.update(stats, false);
83 }
84
85 public List<RatingsEntry> getEntries(String className, long classPK)
86 throws SystemException {
87
88 long classNameId = PortalUtil.getClassNameId(className);
89
90 return ratingsEntryPersistence.findByC_C(classNameId, classPK);
91 }
92
93 public RatingsEntry getEntry(long userId, String className, long classPK)
94 throws PortalException, SystemException {
95
96 long classNameId = PortalUtil.getClassNameId(className);
97
98 return ratingsEntryPersistence.findByU_C_C(
99 userId, classNameId, classPK);
100 }
101
102 public RatingsEntry updateEntry(
103 long userId, String className, long classPK, double score)
104 throws PortalException, SystemException {
105
106
108 boolean newEntry = false;
109
110 long classNameId = PortalUtil.getClassNameId(className);
111 double oldScore = 0;
112 Date now = new Date();
113
114 RatingsEntry entry = ratingsEntryPersistence.fetchByU_C_C(
115 userId, classNameId, classPK);
116
117 if (entry != null) {
118 oldScore = entry.getScore();
119
120 entry.setModifiedDate(now);
121 entry.setScore(score);
122
123 ratingsEntryPersistence.update(entry, false);
124
125
127 RatingsStats stats = ratingsStatsLocalService.getStats(
128 className, classPK);
129
130 stats.setTotalScore(stats.getTotalScore() - oldScore + score);
131 stats.setAverageScore(
132 stats.getTotalScore() / stats.getTotalEntries());
133
134 ratingsStatsPersistence.update(stats, false);
135 }
136 else {
137 newEntry = true;
138
139 User user = userPersistence.findByPrimaryKey(userId);
140
141 long entryId = counterLocalService.increment();
142
143 entry = ratingsEntryPersistence.create(entryId);
144
145 entry.setCompanyId(user.getCompanyId());
146 entry.setUserId(user.getUserId());
147 entry.setUserName(user.getFullName());
148 entry.setCreateDate(now);
149 entry.setModifiedDate(now);
150 entry.setClassNameId(classNameId);
151 entry.setClassPK(classPK);
152 entry.setScore(score);
153
154 ratingsEntryPersistence.update(entry, false);
155
156
158 RatingsStats stats = ratingsStatsLocalService.getStats(
159 className, classPK);
160
161 stats.setTotalEntries(stats.getTotalEntries() + 1);
162 stats.setTotalScore(stats.getTotalScore() + score);
163 stats.setAverageScore(
164 stats.getTotalScore() / stats.getTotalEntries());
165
166 ratingsStatsPersistence.update(stats, false);
167 }
168
169
171 if (className.equals(BlogsEntry.class.getName())) {
172 BlogsEntry blogsEntry = blogsEntryPersistence.findByPrimaryKey(
173 classPK);
174
175 BlogsStatsUser blogsStatsUser =
176 blogsStatsUserLocalService.getStatsUser(
177 blogsEntry.getGroupId(), blogsEntry.getUserId());
178
179 int ratingsTotalEntries = blogsStatsUser.getRatingsTotalEntries();
180 double ratingsTotalScore = blogsStatsUser.getRatingsTotalScore();
181 double ratingsAverageScore =
182 blogsStatsUser.getRatingsAverageScore();
183
184 if (newEntry) {
185 ratingsTotalEntries++;
186 ratingsTotalScore += score;
187 }
188 else {
189 ratingsTotalScore = ratingsTotalScore - oldScore + score;
190 }
191
192 ratingsAverageScore = ratingsTotalScore / ratingsTotalEntries;
193
194 blogsStatsUser.setRatingsTotalEntries(ratingsTotalEntries);
195 blogsStatsUser.setRatingsTotalScore(ratingsTotalScore);
196 blogsStatsUser.setRatingsAverageScore(ratingsAverageScore);
197
198 blogsStatsUserPersistence.update(blogsStatsUser, false);
199 }
200
201 return entry;
202 }
203
204 }