001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portlet.ratings.service.impl;
016    
017    import com.liferay.portal.kernel.exception.PortalException;
018    import com.liferay.portal.kernel.exception.SystemException;
019    import com.liferay.portal.model.User;
020    import com.liferay.portal.security.permission.ActionKeys;
021    import com.liferay.portal.service.ServiceContext;
022    import com.liferay.portal.util.PortalUtil;
023    import com.liferay.portlet.blogs.model.BlogsEntry;
024    import com.liferay.portlet.blogs.model.BlogsStatsUser;
025    import com.liferay.portlet.ratings.model.RatingsEntry;
026    import com.liferay.portlet.ratings.model.RatingsStats;
027    import com.liferay.portlet.ratings.service.base.RatingsEntryLocalServiceBaseImpl;
028    
029    import java.util.Date;
030    import java.util.List;
031    
032    /**
033     * @author Brian Wing Shun Chan
034     * @author Zsolt Berentey
035     */
036    public class RatingsEntryLocalServiceImpl
037            extends RatingsEntryLocalServiceBaseImpl {
038    
039            public void deleteEntry(long userId, String className, long classPK)
040                    throws PortalException, SystemException {
041    
042                    // Entry
043    
044                    long classNameId = PortalUtil.getClassNameId(className);
045    
046                    RatingsEntry entry = ratingsEntryPersistence.fetchByU_C_C(
047                            userId, classNameId, classPK);
048    
049                    if (entry == null) {
050                            return;
051                    }
052    
053                    double oldScore = entry.getScore();
054    
055                    ratingsEntryPersistence.removeByU_C_C(userId, classNameId, classPK);
056    
057                    // Stats
058    
059                    RatingsStats stats = ratingsStatsLocalService.getStats(
060                            className, classPK);
061    
062                    int totalEntries = stats.getTotalEntries() - 1;
063                    double totalScore = stats.getTotalScore() - oldScore;
064                    double averageScore = 0;
065    
066                    if (totalEntries > 0) {
067                            averageScore = totalScore / totalEntries;
068                    }
069    
070                    stats.setTotalEntries(totalEntries);
071                    stats.setTotalScore(totalScore);
072                    stats.setAverageScore(averageScore);
073    
074                    ratingsStatsPersistence.update(stats, false);
075    
076                    // Social
077    
078                    socialEquityLogLocalService.deactivateEquityLogs(
079                            userId, className, classPK, ActionKeys.ADD_VOTE);
080            }
081    
082            public List<RatingsEntry> getEntries(
083                            long userId, String className, List<Long> classPKs)
084                    throws SystemException {
085    
086                    long classNameId = PortalUtil.getClassNameId(className);
087    
088                    return ratingsEntryFinder.findByU_C_C(userId, classNameId, classPKs);
089            }
090    
091            public List<RatingsEntry> getEntries(String className, long classPK)
092                    throws SystemException {
093    
094                    long classNameId = PortalUtil.getClassNameId(className);
095    
096                    return ratingsEntryPersistence.findByC_C(classNameId, classPK);
097            }
098    
099            public RatingsEntry getEntry(long userId, String className, long classPK)
100                    throws PortalException, SystemException {
101    
102                    long classNameId = PortalUtil.getClassNameId(className);
103    
104                    return ratingsEntryPersistence.findByU_C_C(
105                            userId, classNameId, classPK);
106            }
107    
108            public RatingsEntry updateEntry(
109                            long userId, String className, long classPK, double score,
110                            ServiceContext serviceContext)
111                    throws PortalException, SystemException {
112    
113                    // Entry
114    
115                    boolean newEntry = false;
116    
117                    long classNameId = PortalUtil.getClassNameId(className);
118                    double oldScore = 0;
119                    Date now = new Date();
120    
121                    RatingsEntry entry = ratingsEntryPersistence.fetchByU_C_C(
122                            userId, classNameId, classPK);
123    
124                    if (entry != null) {
125                            oldScore = entry.getScore();
126    
127                            entry.setModifiedDate(serviceContext.getModifiedDate(now));
128                            entry.setScore(score);
129    
130                            ratingsEntryPersistence.update(entry, false);
131    
132                            // Stats
133    
134                            RatingsStats stats = ratingsStatsLocalService.getStats(
135                                    className, classPK);
136    
137                            stats.setTotalScore(stats.getTotalScore() - oldScore + score);
138                            stats.setAverageScore(
139                                    stats.getTotalScore() / stats.getTotalEntries());
140    
141                            ratingsStatsPersistence.update(stats, false);
142                    }
143                    else {
144                            newEntry = true;
145    
146                            User user = userPersistence.findByPrimaryKey(userId);
147    
148                            long entryId = counterLocalService.increment();
149    
150                            entry = ratingsEntryPersistence.create(entryId);
151    
152                            entry.setCompanyId(user.getCompanyId());
153                            entry.setUserId(user.getUserId());
154                            entry.setUserName(user.getFullName());
155                            entry.setCreateDate(serviceContext.getCreateDate(now));
156                            entry.setModifiedDate(serviceContext.getModifiedDate(now));
157                            entry.setClassNameId(classNameId);
158                            entry.setClassPK(classPK);
159                            entry.setScore(score);
160    
161                            ratingsEntryPersistence.update(entry, false);
162    
163                            // Stats
164    
165                            RatingsStats stats = ratingsStatsLocalService.getStats(
166                                    className, classPK);
167    
168                            stats.setTotalEntries(stats.getTotalEntries() + 1);
169                            stats.setTotalScore(stats.getTotalScore() + score);
170                            stats.setAverageScore(
171                                    stats.getTotalScore() / stats.getTotalEntries());
172    
173                            ratingsStatsPersistence.update(stats, false);
174                    }
175    
176                    // Blogs entry
177    
178                    if (className.equals(BlogsEntry.class.getName())) {
179                            BlogsEntry blogsEntry = blogsEntryPersistence.findByPrimaryKey(
180                                    classPK);
181    
182                            BlogsStatsUser blogsStatsUser =
183                                    blogsStatsUserLocalService.getStatsUser(
184                                            blogsEntry.getGroupId(), blogsEntry.getUserId());
185    
186                            int ratingsTotalEntries = blogsStatsUser.getRatingsTotalEntries();
187                            double ratingsTotalScore = blogsStatsUser.getRatingsTotalScore();
188                            double ratingsAverageScore =
189                                    blogsStatsUser.getRatingsAverageScore();
190    
191                            if (newEntry) {
192                                    ratingsTotalEntries++;
193                                    ratingsTotalScore += score;
194                            }
195                            else {
196                                    ratingsTotalScore = ratingsTotalScore - oldScore + score;
197                            }
198    
199                            ratingsAverageScore = ratingsTotalScore / ratingsTotalEntries;
200    
201                            blogsStatsUser.setRatingsTotalEntries(ratingsTotalEntries);
202                            blogsStatsUser.setRatingsTotalScore(ratingsTotalScore);
203                            blogsStatsUser.setRatingsAverageScore(ratingsAverageScore);
204    
205                            blogsStatsUserPersistence.update(blogsStatsUser, false);
206                    }
207    
208                    // Social
209    
210                    socialEquityLogLocalService.addEquityLogs(
211                            userId, className, classPK, ActionKeys.ADD_VOTE);
212    
213                    return entry;
214            }
215    
216    }