1   /**
2    * Copyright (c) 2000-2009 Liferay, Inc. All rights reserved.
3    *
4    * Permission is hereby granted, free of charge, to any person obtaining a copy
5    * of this software and associated documentation files (the "Software"), to deal
6    * in the Software without restriction, including without limitation the rights
7    * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    * copies of the Software, and to permit persons to whom the Software is
9    * furnished to do so, subject to the following conditions:
10   *
11   * The above copyright notice and this permission notice shall be included in
12   * all copies or substantial portions of the Software.
13   *
14   * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20   * SOFTWARE.
21   */
22  
23  package com.liferay.portlet.ratings.service.impl;
24  
25  import com.liferay.portal.PortalException;
26  import com.liferay.portal.SystemException;
27  import com.liferay.portal.model.User;
28  import com.liferay.portal.util.PortalUtil;
29  import com.liferay.portlet.blogs.model.BlogsEntry;
30  import com.liferay.portlet.blogs.model.BlogsStatsUser;
31  import com.liferay.portlet.ratings.model.RatingsEntry;
32  import com.liferay.portlet.ratings.model.RatingsStats;
33  import com.liferay.portlet.ratings.service.base.RatingsEntryLocalServiceBaseImpl;
34  
35  import java.util.Date;
36  import java.util.List;
37  
38  /**
39   * <a href="RatingsEntryLocalServiceImpl.java.html"><b><i>View Source</i></b>
40   * </a>
41   *
42   * @author Brian Wing Shun Chan
43   *
44   */
45  public class RatingsEntryLocalServiceImpl
46      extends RatingsEntryLocalServiceBaseImpl {
47  
48      public void deleteEntry(long userId, String className, long classPK)
49          throws PortalException, SystemException {
50  
51          // Entry
52  
53          long classNameId = PortalUtil.getClassNameId(className);
54  
55          RatingsEntry entry = ratingsEntryPersistence.fetchByU_C_C(
56              userId, classNameId, classPK);
57  
58          if (entry == null) {
59              return;
60          }
61  
62          double oldScore = entry.getScore();
63  
64          ratingsEntryPersistence.removeByU_C_C(userId, classNameId, classPK);
65  
66          // Stats
67  
68          RatingsStats stats = ratingsStatsLocalService.getStats(
69              className, classPK);
70  
71          int totalEntries = stats.getTotalEntries() - 1;
72          double totalScore = stats.getTotalScore() - oldScore;
73          double averageScore = 0;
74  
75          if (totalEntries > 0) {
76              averageScore = totalScore / totalEntries;
77          }
78  
79          stats.setTotalEntries(totalEntries);
80          stats.setTotalScore(totalScore);
81          stats.setAverageScore(averageScore);
82  
83          ratingsStatsPersistence.update(stats, false);
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 List<RatingsEntry> getEntries(String className, long classPK)
96          throws SystemException {
97  
98          long classNameId = PortalUtil.getClassNameId(className);
99  
100         return ratingsEntryPersistence.findByC_C(classNameId, classPK);
101     }
102 
103     public RatingsEntry updateEntry(
104             long userId, String className, long classPK, double score)
105         throws PortalException, SystemException {
106 
107         boolean newEntry = false;
108 
109         long classNameId = PortalUtil.getClassNameId(className);
110         double oldScore = 0;
111         Date now = new Date();
112 
113         RatingsEntry entry = ratingsEntryPersistence.fetchByU_C_C(
114             userId, classNameId, classPK);
115 
116         if (entry != null) {
117             oldScore = entry.getScore();
118 
119             entry.setModifiedDate(now);
120             entry.setScore(score);
121 
122             ratingsEntryPersistence.update(entry, false);
123 
124             // Stats
125 
126             RatingsStats stats = ratingsStatsLocalService.getStats(
127                 className, classPK);
128 
129             stats.setTotalScore(stats.getTotalScore() - oldScore + score);
130             stats.setAverageScore(
131                 stats.getTotalScore() / stats.getTotalEntries());
132 
133             ratingsStatsPersistence.update(stats, false);
134         }
135         else {
136             newEntry = true;
137 
138             User user = userPersistence.findByPrimaryKey(userId);
139 
140             long entryId = counterLocalService.increment();
141 
142             entry = ratingsEntryPersistence.create(entryId);
143 
144             entry.setCompanyId(user.getCompanyId());
145             entry.setUserId(user.getUserId());
146             entry.setUserName(user.getFullName());
147             entry.setCreateDate(now);
148             entry.setModifiedDate(now);
149             entry.setClassNameId(classNameId);
150             entry.setClassPK(classPK);
151             entry.setScore(score);
152 
153             ratingsEntryPersistence.update(entry, false);
154 
155             // Stats
156 
157             RatingsStats stats = ratingsStatsLocalService.getStats(
158                 className, classPK);
159 
160             stats.setTotalEntries(stats.getTotalEntries() + 1);
161             stats.setTotalScore(stats.getTotalScore() + score);
162             stats.setAverageScore(
163                 stats.getTotalScore() / stats.getTotalEntries());
164 
165             ratingsStatsPersistence.update(stats, false);
166         }
167 
168         // Blogs entry
169 
170         if (className.equals(BlogsEntry.class.getName())) {
171             BlogsEntry blogsEntry = blogsEntryPersistence.findByPrimaryKey(
172                 classPK);
173 
174             BlogsStatsUser blogsStasUser =
175                 blogsStatsUserLocalService.getStatsUser(
176                     blogsEntry.getGroupId(), blogsEntry.getUserId());
177 
178             int ratingsTotalEntries = blogsStasUser.getRatingsTotalEntries();
179             double ratingsTotalScore = blogsStasUser.getRatingsTotalScore();
180             double ratingsAverageScore = blogsStasUser.getRatingsAverageScore();
181 
182             if (newEntry) {
183                 ratingsTotalEntries++;
184                 ratingsTotalScore += score;
185             }
186             else {
187                 ratingsTotalScore = ratingsTotalScore - oldScore + score;
188             }
189 
190             ratingsAverageScore = ratingsTotalScore / ratingsTotalEntries;
191 
192             blogsStasUser.setRatingsTotalEntries(ratingsTotalEntries);
193             blogsStasUser.setRatingsTotalScore(ratingsTotalScore);
194             blogsStasUser.setRatingsAverageScore(ratingsAverageScore);
195 
196             blogsStatsUserPersistence.update(blogsStasUser, false);
197         }
198 
199         return entry;
200     }
201 
202 }