1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * The contents of this file are subject to the terms of the Liferay Enterprise
5    * Subscription License ("License"). You may not use this file except in
6    * compliance with the License. You can obtain a copy of the License by
7    * contacting Liferay, Inc. See the License for the specific language governing
8    * permissions and limitations under the License, including but not limited to
9    * distribution rights of the Software.
10   *
11   *
12   *
13   */
14  
15  package com.liferay.portlet.ratings.service.impl;
16  
17  import com.liferay.portal.PortalException;
18  import com.liferay.portal.SystemException;
19  import com.liferay.portal.model.User;
20  import com.liferay.portal.service.ServiceContext;
21  import com.liferay.portal.util.PortalUtil;
22  import com.liferay.portlet.blogs.model.BlogsEntry;
23  import com.liferay.portlet.blogs.model.BlogsStatsUser;
24  import com.liferay.portlet.ratings.model.RatingsEntry;
25  import com.liferay.portlet.ratings.model.RatingsStats;
26  import com.liferay.portlet.ratings.service.base.RatingsEntryLocalServiceBaseImpl;
27  
28  import java.util.Date;
29  import java.util.List;
30  
31  /**
32   * <a href="RatingsEntryLocalServiceImpl.java.html"><b><i>View Source</i></b>
33   * </a>
34   *
35   * @author Brian Wing Shun Chan
36   */
37  public class RatingsEntryLocalServiceImpl
38      extends RatingsEntryLocalServiceBaseImpl {
39  
40      public void deleteEntry(long userId, String className, long classPK)
41          throws PortalException, SystemException {
42  
43          // Entry
44  
45          long classNameId = PortalUtil.getClassNameId(className);
46  
47          RatingsEntry entry = ratingsEntryPersistence.fetchByU_C_C(
48              userId, classNameId, classPK);
49  
50          if (entry == null) {
51              return;
52          }
53  
54          double oldScore = entry.getScore();
55  
56          ratingsEntryPersistence.removeByU_C_C(userId, classNameId, classPK);
57  
58          // Stats
59  
60          RatingsStats stats = ratingsStatsLocalService.getStats(
61              className, classPK);
62  
63          int totalEntries = stats.getTotalEntries() - 1;
64          double totalScore = stats.getTotalScore() - oldScore;
65          double averageScore = 0;
66  
67          if (totalEntries > 0) {
68              averageScore = totalScore / totalEntries;
69          }
70  
71          stats.setTotalEntries(totalEntries);
72          stats.setTotalScore(totalScore);
73          stats.setAverageScore(averageScore);
74  
75          ratingsStatsPersistence.update(stats, false);
76      }
77  
78      public List<RatingsEntry> getEntries(String className, long classPK)
79          throws SystemException {
80  
81          long classNameId = PortalUtil.getClassNameId(className);
82  
83          return ratingsEntryPersistence.findByC_C(classNameId, classPK);
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      /**
96       * @deprecated
97       */
98      public RatingsEntry updateEntry(
99              long userId, String className, long classPK, double score)
100         throws PortalException, SystemException {
101 
102         return updateEntry(
103             userId, className, classPK, score, new ServiceContext());
104     }
105 
106     public RatingsEntry updateEntry(
107             long userId, String className, long classPK, double score,
108             ServiceContext serviceContext)
109         throws PortalException, SystemException {
110 
111         // Entry
112 
113         boolean newEntry = false;
114 
115         long classNameId = PortalUtil.getClassNameId(className);
116         double oldScore = 0;
117         Date now = new Date();
118 
119         RatingsEntry entry = ratingsEntryPersistence.fetchByU_C_C(
120             userId, classNameId, classPK);
121 
122         if (entry != null) {
123             oldScore = entry.getScore();
124 
125             entry.setModifiedDate(serviceContext.getModifiedDate(now));
126             entry.setScore(score);
127 
128             ratingsEntryPersistence.update(entry, false);
129 
130             // Stats
131 
132             RatingsStats stats = ratingsStatsLocalService.getStats(
133                 className, classPK);
134 
135             stats.setTotalScore(stats.getTotalScore() - oldScore + score);
136             stats.setAverageScore(
137                 stats.getTotalScore() / stats.getTotalEntries());
138 
139             ratingsStatsPersistence.update(stats, false);
140         }
141         else {
142             newEntry = true;
143 
144             User user = userPersistence.findByPrimaryKey(userId);
145 
146             long entryId = counterLocalService.increment();
147 
148             entry = ratingsEntryPersistence.create(entryId);
149 
150             entry.setCompanyId(user.getCompanyId());
151             entry.setUserId(user.getUserId());
152             entry.setUserName(user.getFullName());
153             entry.setCreateDate(serviceContext.getCreateDate(now));
154             entry.setModifiedDate(serviceContext.getModifiedDate(now));
155             entry.setClassNameId(classNameId);
156             entry.setClassPK(classPK);
157             entry.setScore(score);
158 
159             ratingsEntryPersistence.update(entry, false);
160 
161             // Stats
162 
163             RatingsStats stats = ratingsStatsLocalService.getStats(
164                 className, classPK);
165 
166             stats.setTotalEntries(stats.getTotalEntries() + 1);
167             stats.setTotalScore(stats.getTotalScore() + score);
168             stats.setAverageScore(
169                 stats.getTotalScore() / stats.getTotalEntries());
170 
171             ratingsStatsPersistence.update(stats, false);
172         }
173 
174         // Blogs entry
175 
176         if (className.equals(BlogsEntry.class.getName())) {
177             BlogsEntry blogsEntry = blogsEntryPersistence.findByPrimaryKey(
178                 classPK);
179 
180             BlogsStatsUser blogsStatsUser =
181                 blogsStatsUserLocalService.getStatsUser(
182                     blogsEntry.getGroupId(), blogsEntry.getUserId());
183 
184             int ratingsTotalEntries = blogsStatsUser.getRatingsTotalEntries();
185             double ratingsTotalScore = blogsStatsUser.getRatingsTotalScore();
186             double ratingsAverageScore =
187                 blogsStatsUser.getRatingsAverageScore();
188 
189             if (newEntry) {
190                 ratingsTotalEntries++;
191                 ratingsTotalScore += score;
192             }
193             else {
194                 ratingsTotalScore = ratingsTotalScore - oldScore + score;
195             }
196 
197             ratingsAverageScore = ratingsTotalScore / ratingsTotalEntries;
198 
199             blogsStatsUser.setRatingsTotalEntries(ratingsTotalEntries);
200             blogsStatsUser.setRatingsTotalScore(ratingsTotalScore);
201             blogsStatsUser.setRatingsAverageScore(ratingsAverageScore);
202 
203             blogsStatsUserPersistence.update(blogsStatsUser, false);
204         }
205 
206         return entry;
207     }
208 
209 }