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