1   /**
2    * Copyright (c) 2000-2007 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.counter.service.CounterLocalServiceUtil;
26  import com.liferay.portal.PortalException;
27  import com.liferay.portal.SystemException;
28  import com.liferay.portal.model.User;
29  import com.liferay.portal.service.persistence.UserUtil;
30  import com.liferay.portal.util.PortalUtil;
31  import com.liferay.portlet.blogs.model.BlogsEntry;
32  import com.liferay.portlet.blogs.model.BlogsStatsUser;
33  import com.liferay.portlet.blogs.service.BlogsStatsUserLocalServiceUtil;
34  import com.liferay.portlet.blogs.service.persistence.BlogsEntryUtil;
35  import com.liferay.portlet.blogs.service.persistence.BlogsStatsUserUtil;
36  import com.liferay.portlet.ratings.NoSuchEntryException;
37  import com.liferay.portlet.ratings.model.RatingsEntry;
38  import com.liferay.portlet.ratings.model.RatingsStats;
39  import com.liferay.portlet.ratings.service.RatingsStatsLocalServiceUtil;
40  import com.liferay.portlet.ratings.service.base.RatingsEntryLocalServiceBaseImpl;
41  import com.liferay.portlet.ratings.service.persistence.RatingsEntryUtil;
42  import com.liferay.portlet.ratings.service.persistence.RatingsStatsUtil;
43  
44  import java.util.Date;
45  
46  /**
47   * <a href="RatingsEntryLocalServiceImpl.java.html"><b><i>View Source</i></b>
48   * </a>
49   *
50   * @author Brian Wing Shun Chan
51   *
52   */
53  public class RatingsEntryLocalServiceImpl
54      extends RatingsEntryLocalServiceBaseImpl {
55  
56      public RatingsEntry getEntry(long userId, String className, long classPK)
57          throws PortalException, SystemException {
58  
59          long classNameId = PortalUtil.getClassNameId(className);
60  
61          return RatingsEntryUtil.findByU_C_C(userId, classNameId, classPK);
62      }
63  
64      public RatingsEntry updateEntry(
65              long userId, String className, long classPK, double score)
66          throws PortalException, SystemException {
67  
68          boolean newEntry = false;
69  
70          long classNameId = PortalUtil.getClassNameId(className);
71          double oldScore = 0;
72          Date now = new Date();
73  
74          RatingsEntry entry = null;
75  
76          try {
77              entry = RatingsEntryUtil.findByU_C_C(userId, classNameId, classPK);
78  
79              oldScore = entry.getScore();
80  
81              entry.setModifiedDate(now);
82              entry.setScore(score);
83  
84              RatingsEntryUtil.update(entry);
85  
86              // Stats
87  
88              RatingsStats stats = RatingsStatsLocalServiceUtil.getStats(
89                  className, classPK);
90  
91              stats.setTotalScore(stats.getTotalScore() - oldScore + score);
92              stats.setAverageScore(
93                  stats.getTotalScore() / stats.getTotalEntries());
94  
95              RatingsStatsUtil.update(stats);
96          }
97          catch (NoSuchEntryException nsee) {
98              newEntry = true;
99  
100             User user = UserUtil.findByPrimaryKey(userId);
101 
102             long entryId = CounterLocalServiceUtil.increment();
103 
104             entry = RatingsEntryUtil.create(entryId);
105 
106             entry.setCompanyId(user.getCompanyId());
107             entry.setUserId(user.getUserId());
108             entry.setUserName(user.getFullName());
109             entry.setCreateDate(now);
110             entry.setModifiedDate(now);
111             entry.setClassNameId(classNameId);
112             entry.setClassPK(classPK);
113             entry.setScore(score);
114 
115             RatingsEntryUtil.update(entry);
116 
117             // Stats
118 
119             RatingsStats stats = RatingsStatsLocalServiceUtil.getStats(
120                 className, classPK);
121 
122             stats.setTotalEntries(stats.getTotalEntries() + 1);
123             stats.setTotalScore(stats.getTotalScore() + score);
124             stats.setAverageScore(
125                 stats.getTotalScore() / stats.getTotalEntries());
126 
127             RatingsStatsUtil.update(stats);
128         }
129 
130         // Blogs entry
131 
132         if (className.equals(BlogsEntry.class.getName())) {
133             BlogsEntry blogsEntry = BlogsEntryUtil.findByPrimaryKey(classPK);
134 
135             BlogsStatsUser blogsStasUser =
136                 BlogsStatsUserLocalServiceUtil.getStatsUser(
137                     blogsEntry.getGroupId(), blogsEntry.getUserId());
138 
139             int ratingsTotalEntries = blogsStasUser.getRatingsTotalEntries();
140             double ratingsTotalScore = blogsStasUser.getRatingsTotalScore();
141             double ratingsAverageScore = blogsStasUser.getRatingsAverageScore();
142 
143             if (newEntry) {
144                 ratingsTotalEntries++;
145                 ratingsTotalScore += score;
146             }
147             else {
148                 ratingsTotalScore = ratingsTotalScore - oldScore + score;
149             }
150 
151             ratingsAverageScore = ratingsTotalScore / ratingsTotalEntries;
152 
153             blogsStasUser.setRatingsTotalEntries(ratingsTotalEntries);
154             blogsStasUser.setRatingsTotalScore(ratingsTotalScore);
155             blogsStasUser.setRatingsAverageScore(ratingsAverageScore);
156 
157             BlogsStatsUserUtil.update(blogsStasUser);
158         }
159 
160         return entry;
161     }
162 
163 }