1
22
23 package com.liferay.portlet.ratings.service.impl;
24
25 import com.liferay.counter.service.CounterLocalServiceUtil;
26 import com.liferay.portal.PortalException;
27 import com.liferay.portal.SystemException;
28 import com.liferay.portal.model.User;
29 import com.liferay.portal.service.persistence.UserUtil;
30 import com.liferay.portal.util.PortalUtil;
31 import com.liferay.portlet.blogs.model.BlogsEntry;
32 import com.liferay.portlet.blogs.model.BlogsStatsUser;
33 import com.liferay.portlet.blogs.service.BlogsStatsUserLocalServiceUtil;
34 import com.liferay.portlet.blogs.service.persistence.BlogsEntryUtil;
35 import com.liferay.portlet.blogs.service.persistence.BlogsStatsUserUtil;
36 import com.liferay.portlet.ratings.NoSuchEntryException;
37 import com.liferay.portlet.ratings.model.RatingsEntry;
38 import com.liferay.portlet.ratings.model.RatingsStats;
39 import com.liferay.portlet.ratings.service.RatingsStatsLocalServiceUtil;
40 import com.liferay.portlet.ratings.service.base.RatingsEntryLocalServiceBaseImpl;
41 import com.liferay.portlet.ratings.service.persistence.RatingsEntryUtil;
42 import com.liferay.portlet.ratings.service.persistence.RatingsStatsUtil;
43
44 import java.util.Date;
45
46
53 public class RatingsEntryLocalServiceImpl
54 extends RatingsEntryLocalServiceBaseImpl {
55
56 public RatingsEntry getEntry(long userId, String className, long classPK)
57 throws PortalException, SystemException {
58
59 long classNameId = PortalUtil.getClassNameId(className);
60
61 return RatingsEntryUtil.findByU_C_C(userId, classNameId, classPK);
62 }
63
64 public RatingsEntry updateEntry(
65 long userId, String className, long classPK, double score)
66 throws PortalException, SystemException {
67
68 boolean newEntry = false;
69
70 long classNameId = PortalUtil.getClassNameId(className);
71 double oldScore = 0;
72 Date now = new Date();
73
74 RatingsEntry entry = null;
75
76 try {
77 entry = RatingsEntryUtil.findByU_C_C(userId, classNameId, classPK);
78
79 oldScore = entry.getScore();
80
81 entry.setModifiedDate(now);
82 entry.setScore(score);
83
84 RatingsEntryUtil.update(entry);
85
86
88 RatingsStats stats = RatingsStatsLocalServiceUtil.getStats(
89 className, classPK);
90
91 stats.setTotalScore(stats.getTotalScore() - oldScore + score);
92 stats.setAverageScore(
93 stats.getTotalScore() / stats.getTotalEntries());
94
95 RatingsStatsUtil.update(stats);
96 }
97 catch (NoSuchEntryException nsee) {
98 newEntry = true;
99
100 User user = UserUtil.findByPrimaryKey(userId);
101
102 long entryId = CounterLocalServiceUtil.increment();
103
104 entry = RatingsEntryUtil.create(entryId);
105
106 entry.setCompanyId(user.getCompanyId());
107 entry.setUserId(user.getUserId());
108 entry.setUserName(user.getFullName());
109 entry.setCreateDate(now);
110 entry.setModifiedDate(now);
111 entry.setClassNameId(classNameId);
112 entry.setClassPK(classPK);
113 entry.setScore(score);
114
115 RatingsEntryUtil.update(entry);
116
117
119 RatingsStats stats = RatingsStatsLocalServiceUtil.getStats(
120 className, classPK);
121
122 stats.setTotalEntries(stats.getTotalEntries() + 1);
123 stats.setTotalScore(stats.getTotalScore() + score);
124 stats.setAverageScore(
125 stats.getTotalScore() / stats.getTotalEntries());
126
127 RatingsStatsUtil.update(stats);
128 }
129
130
132 if (className.equals(BlogsEntry.class.getName())) {
133 BlogsEntry blogsEntry = BlogsEntryUtil.findByPrimaryKey(classPK);
134
135 BlogsStatsUser blogsStasUser =
136 BlogsStatsUserLocalServiceUtil.getStatsUser(
137 blogsEntry.getGroupId(), blogsEntry.getUserId());
138
139 int ratingsTotalEntries = blogsStasUser.getRatingsTotalEntries();
140 double ratingsTotalScore = blogsStasUser.getRatingsTotalScore();
141 double ratingsAverageScore = blogsStasUser.getRatingsAverageScore();
142
143 if (newEntry) {
144 ratingsTotalEntries++;
145 ratingsTotalScore += score;
146 }
147 else {
148 ratingsTotalScore = ratingsTotalScore - oldScore + score;
149 }
150
151 ratingsAverageScore = ratingsTotalScore / ratingsTotalEntries;
152
153 blogsStasUser.setRatingsTotalEntries(ratingsTotalEntries);
154 blogsStasUser.setRatingsTotalScore(ratingsTotalScore);
155 blogsStasUser.setRatingsAverageScore(ratingsAverageScore);
156
157 BlogsStatsUserUtil.update(blogsStasUser);
158 }
159
160 return entry;
161 }
162
163 }