001: /**
002: * Copyright (c) 2000-2008 Liferay, Inc. All rights reserved.
003: *
004: * Permission is hereby granted, free of charge, to any person obtaining a copy
005: * of this software and associated documentation files (the "Software"), to deal
006: * in the Software without restriction, including without limitation the rights
007: * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is
009: * furnished to do so, subject to the following conditions:
010: *
011: * The above copyright notice and this permission notice shall be included in
012: * all copies or substantial portions of the Software.
013: *
014: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
015: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
016: * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
017: * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
018: * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
019: * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
020: * SOFTWARE.
021: */package com.liferay.portlet.ratings.service.impl;
022:
023: import com.liferay.portal.PortalException;
024: import com.liferay.portal.SystemException;
025: import com.liferay.portal.model.User;
026: import com.liferay.portal.util.PortalUtil;
027: import com.liferay.portlet.blogs.model.BlogsEntry;
028: import com.liferay.portlet.blogs.model.BlogsStatsUser;
029: import com.liferay.portlet.ratings.NoSuchEntryException;
030: import com.liferay.portlet.ratings.model.RatingsEntry;
031: import com.liferay.portlet.ratings.model.RatingsStats;
032: import com.liferay.portlet.ratings.service.base.RatingsEntryLocalServiceBaseImpl;
033:
034: import java.util.Date;
035: import java.util.List;
036:
037: /**
038: * <a href="RatingsEntryLocalServiceImpl.java.html"><b><i>View Source</i></b>
039: * </a>
040: *
041: * @author Brian Wing Shun Chan
042: *
043: */
044: public class RatingsEntryLocalServiceImpl extends
045: RatingsEntryLocalServiceBaseImpl {
046:
047: public RatingsEntry getEntry(long userId, String className,
048: long classPK) throws PortalException, SystemException {
049:
050: long classNameId = PortalUtil.getClassNameId(className);
051:
052: return ratingsEntryPersistence.findByU_C_C(userId, classNameId,
053: classPK);
054: }
055:
056: public List getEntries(String className, long classPK)
057: throws PortalException, SystemException {
058:
059: long classNameId = PortalUtil.getClassNameId(className);
060:
061: return ratingsEntryPersistence.findByC_C(classNameId, classPK);
062: }
063:
064: public RatingsEntry updateEntry(long userId, String className,
065: long classPK, double score) throws PortalException,
066: SystemException {
067:
068: boolean newEntry = false;
069:
070: long classNameId = PortalUtil.getClassNameId(className);
071: double oldScore = 0;
072: Date now = new Date();
073:
074: RatingsEntry entry = null;
075:
076: try {
077: entry = ratingsEntryPersistence.findByU_C_C(userId,
078: classNameId, classPK);
079:
080: oldScore = entry.getScore();
081:
082: entry.setModifiedDate(now);
083: entry.setScore(score);
084:
085: ratingsEntryPersistence.update(entry);
086:
087: // Stats
088:
089: RatingsStats stats = ratingsStatsLocalService.getStats(
090: className, classPK);
091:
092: stats.setTotalScore(stats.getTotalScore() - oldScore
093: + score);
094: stats.setAverageScore(stats.getTotalScore()
095: / stats.getTotalEntries());
096:
097: ratingsStatsPersistence.update(stats);
098: } catch (NoSuchEntryException nsee) {
099: newEntry = true;
100:
101: User user = userPersistence.findByPrimaryKey(userId);
102:
103: long entryId = counterLocalService.increment();
104:
105: entry = ratingsEntryPersistence.create(entryId);
106:
107: entry.setCompanyId(user.getCompanyId());
108: entry.setUserId(user.getUserId());
109: entry.setUserName(user.getFullName());
110: entry.setCreateDate(now);
111: entry.setModifiedDate(now);
112: entry.setClassNameId(classNameId);
113: entry.setClassPK(classPK);
114: entry.setScore(score);
115:
116: ratingsEntryPersistence.update(entry);
117:
118: // Stats
119:
120: RatingsStats stats = ratingsStatsLocalService.getStats(
121: className, classPK);
122:
123: stats.setTotalEntries(stats.getTotalEntries() + 1);
124: stats.setTotalScore(stats.getTotalScore() + score);
125: stats.setAverageScore(stats.getTotalScore()
126: / stats.getTotalEntries());
127:
128: ratingsStatsPersistence.update(stats);
129: }
130:
131: // Blogs entry
132:
133: if (className.equals(BlogsEntry.class.getName())) {
134: BlogsEntry blogsEntry = blogsEntryPersistence
135: .findByPrimaryKey(classPK);
136:
137: BlogsStatsUser blogsStasUser = blogsStatsUserLocalService
138: .getStatsUser(blogsEntry.getGroupId(), blogsEntry
139: .getUserId());
140:
141: int ratingsTotalEntries = blogsStasUser
142: .getRatingsTotalEntries();
143: double ratingsTotalScore = blogsStasUser
144: .getRatingsTotalScore();
145: double ratingsAverageScore = blogsStasUser
146: .getRatingsAverageScore();
147:
148: if (newEntry) {
149: ratingsTotalEntries++;
150: ratingsTotalScore += score;
151: } else {
152: ratingsTotalScore = ratingsTotalScore - oldScore
153: + score;
154: }
155:
156: ratingsAverageScore = ratingsTotalScore
157: / ratingsTotalEntries;
158:
159: blogsStasUser.setRatingsTotalEntries(ratingsTotalEntries);
160: blogsStasUser.setRatingsTotalScore(ratingsTotalScore);
161: blogsStasUser.setRatingsAverageScore(ratingsAverageScore);
162:
163: blogsStatsUserPersistence.update(blogsStasUser);
164: }
165:
166: return entry;
167: }
168:
169: }
|