001    /**
002     * Copyright (c) 2000-2012 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.configuration.Filter;
018    import com.liferay.portal.kernel.exception.PortalException;
019    import com.liferay.portal.kernel.exception.SystemException;
020    import com.liferay.portal.kernel.util.GetterUtil;
021    import com.liferay.portal.kernel.util.PropsKeys;
022    import com.liferay.portal.kernel.util.PropsUtil;
023    import com.liferay.portal.kernel.util.StringPool;
024    import com.liferay.portal.model.User;
025    import com.liferay.portal.service.ServiceContext;
026    import com.liferay.portal.util.PortalUtil;
027    import com.liferay.portal.util.PropsValues;
028    import com.liferay.portlet.asset.model.AssetEntry;
029    import com.liferay.portlet.blogs.model.BlogsEntry;
030    import com.liferay.portlet.blogs.model.BlogsStatsUser;
031    import com.liferay.portlet.ratings.EntryScoreException;
032    import com.liferay.portlet.ratings.model.RatingsEntry;
033    import com.liferay.portlet.ratings.model.RatingsStats;
034    import com.liferay.portlet.ratings.service.base.RatingsEntryLocalServiceBaseImpl;
035    import com.liferay.portlet.social.model.SocialActivityConstants;
036    
037    import java.util.Date;
038    import java.util.List;
039    
040    /**
041     * @author Brian Wing Shun Chan
042     * @author Zsolt Berentey
043     */
044    public class RatingsEntryLocalServiceImpl
045            extends RatingsEntryLocalServiceBaseImpl {
046    
047            public void deleteEntry(long userId, String className, long classPK)
048                    throws PortalException, SystemException {
049    
050                    // Entry
051    
052                    long classNameId = PortalUtil.getClassNameId(className);
053    
054                    RatingsEntry entry = ratingsEntryPersistence.fetchByU_C_C(
055                            userId, classNameId, classPK);
056    
057                    if (entry == null) {
058                            return;
059                    }
060    
061                    double oldScore = entry.getScore();
062    
063                    ratingsEntryPersistence.removeByU_C_C(userId, classNameId, classPK);
064    
065                    // Stats
066    
067                    RatingsStats stats = ratingsStatsLocalService.getStats(
068                            className, classPK);
069    
070                    int totalEntries = stats.getTotalEntries() - 1;
071                    double totalScore = stats.getTotalScore() - oldScore;
072                    double averageScore = 0;
073    
074                    if (totalEntries > 0) {
075                            averageScore = totalScore / totalEntries;
076                    }
077    
078                    stats.setTotalEntries(totalEntries);
079                    stats.setTotalScore(totalScore);
080                    stats.setAverageScore(averageScore);
081    
082                    ratingsStatsPersistence.update(stats);
083            }
084    
085            public RatingsEntry fetchEntry(long userId, String className, long classPK)
086                    throws SystemException {
087    
088                    long classNameId = PortalUtil.getClassNameId(className);
089    
090                    return ratingsEntryPersistence.fetchByU_C_C(
091                            userId, classNameId, classPK);
092            }
093    
094            public List<RatingsEntry> getEntries(
095                            long userId, String className, List<Long> classPKs)
096                    throws SystemException {
097    
098                    long classNameId = PortalUtil.getClassNameId(className);
099    
100                    return ratingsEntryFinder.findByU_C_C(userId, classNameId, classPKs);
101            }
102    
103            public List<RatingsEntry> getEntries(String className, long classPK)
104                    throws SystemException {
105    
106                    long classNameId = PortalUtil.getClassNameId(className);
107    
108                    return ratingsEntryPersistence.findByC_C(classNameId, classPK);
109            }
110    
111            public List<RatingsEntry> getEntries(
112                            String className, long classPK, double score)
113                    throws SystemException {
114    
115                    long classNameId = PortalUtil.getClassNameId(className);
116    
117                    return ratingsEntryPersistence.findByC_C_S(classNameId, classPK, score);
118            }
119    
120            public int getEntriesCount(String className, long classPK, double score)
121                    throws SystemException {
122    
123                    long classNameId = PortalUtil.getClassNameId(className);
124    
125                    return ratingsEntryPersistence.countByC_C_S(
126                            classNameId, classPK, score);
127            }
128    
129            public RatingsEntry getEntry(long userId, String className, long classPK)
130                    throws PortalException, SystemException {
131    
132                    long classNameId = PortalUtil.getClassNameId(className);
133    
134                    return ratingsEntryPersistence.findByU_C_C(
135                            userId, classNameId, classPK);
136            }
137    
138            public RatingsEntry updateEntry(
139                            long userId, String className, long classPK, double score,
140                            ServiceContext serviceContext)
141                    throws PortalException, SystemException {
142    
143                    // Entry
144    
145                    boolean newEntry = false;
146    
147                    long classNameId = PortalUtil.getClassNameId(className);
148                    double oldScore = 0;
149                    Date now = new Date();
150    
151                    validate(className, score);
152    
153                    RatingsEntry entry = ratingsEntryPersistence.fetchByU_C_C(
154                            userId, classNameId, classPK);
155    
156                    if (entry != null) {
157                            oldScore = entry.getScore();
158    
159                            entry.setModifiedDate(serviceContext.getModifiedDate(now));
160                            entry.setScore(score);
161    
162                            ratingsEntryPersistence.update(entry);
163    
164                            // Stats
165    
166                            RatingsStats stats = ratingsStatsLocalService.getStats(
167                                    className, classPK);
168    
169                            stats.setTotalScore(stats.getTotalScore() - oldScore + score);
170                            stats.setAverageScore(
171                                    stats.getTotalScore() / stats.getTotalEntries());
172    
173                            ratingsStatsPersistence.update(stats);
174                    }
175                    else {
176                            newEntry = true;
177    
178                            User user = userPersistence.findByPrimaryKey(userId);
179    
180                            long entryId = counterLocalService.increment();
181    
182                            entry = ratingsEntryPersistence.create(entryId);
183    
184                            entry.setCompanyId(user.getCompanyId());
185                            entry.setUserId(user.getUserId());
186                            entry.setUserName(user.getFullName());
187                            entry.setCreateDate(serviceContext.getCreateDate(now));
188                            entry.setModifiedDate(serviceContext.getModifiedDate(now));
189                            entry.setClassNameId(classNameId);
190                            entry.setClassPK(classPK);
191                            entry.setScore(score);
192    
193                            ratingsEntryPersistence.update(entry);
194    
195                            // Stats
196    
197                            RatingsStats stats = ratingsStatsLocalService.getStats(
198                                    className, classPK);
199    
200                            stats.setTotalEntries(stats.getTotalEntries() + 1);
201                            stats.setTotalScore(stats.getTotalScore() + score);
202                            stats.setAverageScore(
203                                    stats.getTotalScore() / stats.getTotalEntries());
204    
205                            ratingsStatsPersistence.update(stats);
206                    }
207    
208                    // Blogs entry
209    
210                    if (className.equals(BlogsEntry.class.getName())) {
211                            BlogsEntry blogsEntry = blogsEntryPersistence.findByPrimaryKey(
212                                    classPK);
213    
214                            BlogsStatsUser blogsStatsUser =
215                                    blogsStatsUserLocalService.getStatsUser(
216                                            blogsEntry.getGroupId(), blogsEntry.getUserId());
217    
218                            int ratingsTotalEntries = blogsStatsUser.getRatingsTotalEntries();
219                            double ratingsTotalScore = blogsStatsUser.getRatingsTotalScore();
220                            double ratingsAverageScore =
221                                    blogsStatsUser.getRatingsAverageScore();
222    
223                            if (newEntry) {
224                                    ratingsTotalEntries++;
225                                    ratingsTotalScore += score;
226                            }
227                            else {
228                                    ratingsTotalScore = ratingsTotalScore - oldScore + score;
229                            }
230    
231                            ratingsAverageScore = ratingsTotalScore / ratingsTotalEntries;
232    
233                            blogsStatsUser.setRatingsTotalEntries(ratingsTotalEntries);
234                            blogsStatsUser.setRatingsTotalScore(ratingsTotalScore);
235                            blogsStatsUser.setRatingsAverageScore(ratingsAverageScore);
236    
237                            blogsStatsUserPersistence.update(blogsStatsUser);
238                    }
239    
240                    // Social
241    
242                    AssetEntry assetEntry = assetEntryLocalService.fetchEntry(
243                            className, classPK);
244    
245                    if (assetEntry != null) {
246                            socialActivityLocalService.addActivity(
247                                    userId, assetEntry.getGroupId(), className, classPK,
248                                    SocialActivityConstants.TYPE_ADD_VOTE, StringPool.BLANK, 0);
249                    }
250    
251                    return entry;
252            }
253    
254            protected void validate(String className, double score)
255                    throws PortalException {
256    
257                    Filter filter = new Filter(className);
258    
259                    double maxScore = GetterUtil.getInteger(
260                            PropsUtil.get(PropsKeys.RATINGS_MAX_SCORE, filter),
261                            PropsValues.RATINGS_DEFAULT_NUMBER_OF_STARS);
262                    double minScore = GetterUtil.getInteger(
263                            PropsUtil.get(PropsKeys.RATINGS_MIN_SCORE, filter));
264    
265                    if ((score < minScore) || (score > maxScore)) {
266                            throw new EntryScoreException();
267                    }
268            }
269    
270    }