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.util.PortalUtil;
21  import com.liferay.portlet.blogs.model.BlogsEntry;
22  import com.liferay.portlet.blogs.model.BlogsStatsUser;
23  import com.liferay.portlet.ratings.model.RatingsEntry;
24  import com.liferay.portlet.ratings.model.RatingsStats;
25  import com.liferay.portlet.ratings.service.base.RatingsEntryLocalServiceBaseImpl;
26  
27  import java.util.Date;
28  import java.util.List;
29  
30  /**
31   * <a href="RatingsEntryLocalServiceImpl.java.html"><b><i>View Source</i></b>
32   * </a>
33   *
34   * @author Brian Wing Shun Chan
35   */
36  public class RatingsEntryLocalServiceImpl
37      extends RatingsEntryLocalServiceBaseImpl {
38  
39      public void deleteEntry(long userId, String className, long classPK)
40          throws PortalException, SystemException {
41  
42          // Entry
43  
44          long classNameId = PortalUtil.getClassNameId(className);
45  
46          RatingsEntry entry = ratingsEntryPersistence.fetchByU_C_C(
47              userId, classNameId, classPK);
48  
49          if (entry == null) {
50              return;
51          }
52  
53          double oldScore = entry.getScore();
54  
55          ratingsEntryPersistence.removeByU_C_C(userId, classNameId, classPK);
56  
57          // Stats
58  
59          RatingsStats stats = ratingsStatsLocalService.getStats(
60              className, classPK);
61  
62          int totalEntries = stats.getTotalEntries() - 1;
63          double totalScore = stats.getTotalScore() - oldScore;
64          double averageScore = 0;
65  
66          if (totalEntries > 0) {
67              averageScore = totalScore / totalEntries;
68          }
69  
70          stats.setTotalEntries(totalEntries);
71          stats.setTotalScore(totalScore);
72          stats.setAverageScore(averageScore);
73  
74          ratingsStatsPersistence.update(stats, false);
75      }
76  
77      public List<RatingsEntry> getEntries(String className, long classPK)
78          throws SystemException {
79  
80          long classNameId = PortalUtil.getClassNameId(className);
81  
82          return ratingsEntryPersistence.findByC_C(classNameId, classPK);
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 RatingsEntry updateEntry(
95              long userId, String className, long classPK, double score)
96          throws PortalException, SystemException {
97  
98          // Entry
99  
100         boolean newEntry = false;
101 
102         long classNameId = PortalUtil.getClassNameId(className);
103         double oldScore = 0;
104         Date now = new Date();
105 
106         RatingsEntry entry = ratingsEntryPersistence.fetchByU_C_C(
107             userId, classNameId, classPK);
108 
109         if (entry != null) {
110             oldScore = entry.getScore();
111 
112             entry.setModifiedDate(now);
113             entry.setScore(score);
114 
115             ratingsEntryPersistence.update(entry, false);
116 
117             // Stats
118 
119             RatingsStats stats = ratingsStatsLocalService.getStats(
120                 className, classPK);
121 
122             stats.setTotalScore(stats.getTotalScore() - oldScore + score);
123             stats.setAverageScore(
124                 stats.getTotalScore() / stats.getTotalEntries());
125 
126             ratingsStatsPersistence.update(stats, false);
127         }
128         else {
129             newEntry = true;
130 
131             User user = userPersistence.findByPrimaryKey(userId);
132 
133             long entryId = counterLocalService.increment();
134 
135             entry = ratingsEntryPersistence.create(entryId);
136 
137             entry.setCompanyId(user.getCompanyId());
138             entry.setUserId(user.getUserId());
139             entry.setUserName(user.getFullName());
140             entry.setCreateDate(now);
141             entry.setModifiedDate(now);
142             entry.setClassNameId(classNameId);
143             entry.setClassPK(classPK);
144             entry.setScore(score);
145 
146             ratingsEntryPersistence.update(entry, false);
147 
148             // Stats
149 
150             RatingsStats stats = ratingsStatsLocalService.getStats(
151                 className, classPK);
152 
153             stats.setTotalEntries(stats.getTotalEntries() + 1);
154             stats.setTotalScore(stats.getTotalScore() + score);
155             stats.setAverageScore(
156                 stats.getTotalScore() / stats.getTotalEntries());
157 
158             ratingsStatsPersistence.update(stats, false);
159         }
160 
161         // Blogs entry
162 
163         if (className.equals(BlogsEntry.class.getName())) {
164             BlogsEntry blogsEntry = blogsEntryPersistence.findByPrimaryKey(
165                 classPK);
166 
167             BlogsStatsUser blogsStatsUser =
168                 blogsStatsUserLocalService.getStatsUser(
169                     blogsEntry.getGroupId(), blogsEntry.getUserId());
170 
171             int ratingsTotalEntries = blogsStatsUser.getRatingsTotalEntries();
172             double ratingsTotalScore = blogsStatsUser.getRatingsTotalScore();
173             double ratingsAverageScore =
174                 blogsStatsUser.getRatingsAverageScore();
175 
176             if (newEntry) {
177                 ratingsTotalEntries++;
178                 ratingsTotalScore += score;
179             }
180             else {
181                 ratingsTotalScore = ratingsTotalScore - oldScore + score;
182             }
183 
184             ratingsAverageScore = ratingsTotalScore / ratingsTotalEntries;
185 
186             blogsStatsUser.setRatingsTotalEntries(ratingsTotalEntries);
187             blogsStatsUser.setRatingsTotalScore(ratingsTotalScore);
188             blogsStatsUser.setRatingsAverageScore(ratingsAverageScore);
189 
190             blogsStatsUserPersistence.update(blogsStatsUser, false);
191         }
192 
193         return entry;
194     }
195 
196 }