1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portlet.ratings.service.impl;
16  
17  import com.liferay.portal.kernel.exception.PortalException;
18  import com.liferay.portal.kernel.exception.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(
79              long userId, String className, List<Long> classPKs)
80          throws SystemException {
81  
82          long classNameId = PortalUtil.getClassNameId(className);
83  
84          return ratingsEntryFinder.findByU_C_C(userId, classNameId, classPKs);
85      }
86  
87      public List<RatingsEntry> getEntries(String className, long classPK)
88          throws SystemException {
89  
90          long classNameId = PortalUtil.getClassNameId(className);
91  
92          return ratingsEntryPersistence.findByC_C(classNameId, classPK);
93      }
94  
95      public RatingsEntry getEntry(long userId, String className, long classPK)
96          throws PortalException, SystemException {
97  
98          long classNameId = PortalUtil.getClassNameId(className);
99  
100         return ratingsEntryPersistence.findByU_C_C(
101             userId, classNameId, classPK);
102     }
103 
104     public RatingsEntry updateEntry(
105             long userId, String className, long classPK, double score,
106             ServiceContext serviceContext)
107         throws PortalException, SystemException {
108 
109         // Entry
110 
111         boolean newEntry = false;
112 
113         long classNameId = PortalUtil.getClassNameId(className);
114         double oldScore = 0;
115         Date now = new Date();
116 
117         RatingsEntry entry = ratingsEntryPersistence.fetchByU_C_C(
118             userId, classNameId, classPK);
119 
120         if (entry != null) {
121             oldScore = entry.getScore();
122 
123             entry.setModifiedDate(serviceContext.getModifiedDate(now));
124             entry.setScore(score);
125 
126             ratingsEntryPersistence.update(entry, false);
127 
128             // Stats
129 
130             RatingsStats stats = ratingsStatsLocalService.getStats(
131                 className, classPK);
132 
133             stats.setTotalScore(stats.getTotalScore() - oldScore + score);
134             stats.setAverageScore(
135                 stats.getTotalScore() / stats.getTotalEntries());
136 
137             ratingsStatsPersistence.update(stats, false);
138         }
139         else {
140             newEntry = true;
141 
142             User user = userPersistence.findByPrimaryKey(userId);
143 
144             long entryId = counterLocalService.increment();
145 
146             entry = ratingsEntryPersistence.create(entryId);
147 
148             entry.setCompanyId(user.getCompanyId());
149             entry.setUserId(user.getUserId());
150             entry.setUserName(user.getFullName());
151             entry.setCreateDate(serviceContext.getCreateDate(now));
152             entry.setModifiedDate(serviceContext.getModifiedDate(now));
153             entry.setClassNameId(classNameId);
154             entry.setClassPK(classPK);
155             entry.setScore(score);
156 
157             ratingsEntryPersistence.update(entry, false);
158 
159             // Stats
160 
161             RatingsStats stats = ratingsStatsLocalService.getStats(
162                 className, classPK);
163 
164             stats.setTotalEntries(stats.getTotalEntries() + 1);
165             stats.setTotalScore(stats.getTotalScore() + score);
166             stats.setAverageScore(
167                 stats.getTotalScore() / stats.getTotalEntries());
168 
169             ratingsStatsPersistence.update(stats, false);
170         }
171 
172         // Blogs entry
173 
174         if (className.equals(BlogsEntry.class.getName())) {
175             BlogsEntry blogsEntry = blogsEntryPersistence.findByPrimaryKey(
176                 classPK);
177 
178             BlogsStatsUser blogsStatsUser =
179                 blogsStatsUserLocalService.getStatsUser(
180                     blogsEntry.getGroupId(), blogsEntry.getUserId());
181 
182             int ratingsTotalEntries = blogsStatsUser.getRatingsTotalEntries();
183             double ratingsTotalScore = blogsStatsUser.getRatingsTotalScore();
184             double ratingsAverageScore =
185                 blogsStatsUser.getRatingsAverageScore();
186 
187             if (newEntry) {
188                 ratingsTotalEntries++;
189                 ratingsTotalScore += score;
190             }
191             else {
192                 ratingsTotalScore = ratingsTotalScore - oldScore + score;
193             }
194 
195             ratingsAverageScore = ratingsTotalScore / ratingsTotalEntries;
196 
197             blogsStatsUser.setRatingsTotalEntries(ratingsTotalEntries);
198             blogsStatsUser.setRatingsTotalScore(ratingsTotalScore);
199             blogsStatsUser.setRatingsAverageScore(ratingsAverageScore);
200 
201             blogsStatsUserPersistence.update(blogsStatsUser, false);
202         }
203 
204         return entry;
205     }
206 
207 }