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