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