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