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