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