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