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