001: /*
002: * Copyright (c) JForum Team
003: * All rights reserved.
004: *
005: * Redistribution and use in source and binary forms,
006: * with or without modification, are permitted provided
007: * that the following conditions are met:
008: *
009: * 1) Redistributions of source code must retain the above
010: * copyright notice, this list of conditions and the
011: * following disclaimer.
012: * 2) Redistributions in binary form must reproduce the
013: * above copyright notice, this list of conditions and
014: * the following disclaimer in the documentation and/or
015: * other materials provided with the distribution.
016: * 3) Neither the name of "Rafael Steil" nor
017: * the names of its contributors may be used to endorse
018: * or promote products derived from this software without
019: * specific prior written permission.
020: *
021: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
022: * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
023: * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
024: * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
025: * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
026: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
027: * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
028: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
029: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
030: * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
031: * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
032: * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
033: * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
034: * IN CONTRACT, STRICT LIABILITY, OR TORT
035: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
036: * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
037: * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
038: *
039: * Created on Jan 11, 2005 11:44:06 PM
040: * The JForum Project
041: * http://www.jforum.net
042: */
043: package net.jforum.view.forum;
044:
045: import net.jforum.Command;
046: import net.jforum.JForumExecutionContext;
047: import net.jforum.SessionFacade;
048: import net.jforum.dao.DataAccessDriver;
049: import net.jforum.dao.KarmaDAO;
050: import net.jforum.dao.PostDAO;
051: import net.jforum.entities.Karma;
052: import net.jforum.entities.KarmaStatus;
053: import net.jforum.entities.Post;
054: import net.jforum.repository.PostRepository;
055: import net.jforum.repository.SecurityRepository;
056: import net.jforum.security.SecurityConstants;
057: import net.jforum.util.I18n;
058: import net.jforum.util.preferences.ConfigKeys;
059: import net.jforum.util.preferences.SystemGlobals;
060: import net.jforum.util.preferences.TemplateKeys;
061: import net.jforum.view.forum.common.PostCommon;
062: import net.jforum.view.forum.common.ViewCommon;
063:
064: /**
065: * @author Rafael Steil
066: * @version $Id: KarmaAction.java,v 1.21 2007/08/01 22:30:03 rafaelsteil Exp $
067: */
068: public class KarmaAction extends Command {
069: public void insert() {
070: if (!SecurityRepository
071: .canAccess(SecurityConstants.PERM_KARMA_ENABLED)) {
072: this .error("Karma.featureDisabled", null);
073: return;
074: }
075:
076: int postId = this .request.getIntParameter("post_id");
077: int fromUserId = SessionFacade.getUserSession().getUserId();
078:
079: PostDAO pm = DataAccessDriver.getInstance().newPostDAO();
080: Post p = pm.selectById(postId);
081:
082: if (fromUserId == SystemGlobals
083: .getIntValue(ConfigKeys.ANONYMOUS_USER_ID)) {
084: this .error("Karma.anonymousIsDenied", p);
085: return;
086: }
087:
088: if (p.getUserId() == fromUserId) {
089: this .error("Karma.cannotSelfVote", p);
090: return;
091: }
092:
093: KarmaDAO km = DataAccessDriver.getInstance().newKarmaDAO();
094:
095: if (!km.userCanAddKarma(fromUserId, postId)) {
096: this .error("Karma.alreadyVoted", p);
097: return;
098: }
099:
100: // Check range
101: int points = this .request.getIntParameter("points");
102:
103: if (points < SystemGlobals
104: .getIntValue(ConfigKeys.KARMA_MIN_POINTS)
105: || points > SystemGlobals
106: .getIntValue(ConfigKeys.KARMA_MAX_POINTS)) {
107: this .error("Karma.invalidRange", p);
108: return;
109: }
110:
111: Karma karma = new Karma();
112: karma.setFromUserId(fromUserId);
113: karma.setPostUserId(p.getUserId());
114: karma.setPostId(postId);
115: karma.setTopicId(p.getTopicId());
116: karma.setPoints(points);
117:
118: km.addKarma(karma);
119:
120: p.setKarma(new KarmaStatus(p.getId(), points));
121:
122: if (SystemGlobals.getBoolValue(ConfigKeys.POSTS_CACHE_ENABLED)) {
123: PostRepository.update(p.getTopicId(), PostCommon
124: .preparePostForDisplay(p));
125: }
126:
127: JForumExecutionContext.setRedirect(this .urlToTopic(p));
128: }
129:
130: private void error(String message, Post p) {
131: this .setTemplateName(TemplateKeys.KARMA_ERROR);
132:
133: if (p != null) {
134: this .context.put("message", I18n.getMessage(message,
135: new String[] { this .urlToTopic(p) }));
136: } else {
137: this .context.put("message", I18n.getMessage(message));
138: }
139: }
140:
141: private String urlToTopic(Post p) {
142: return JForumExecutionContext.getRequest().getContextPath()
143: + "/posts/list/" + ViewCommon.getStartPage() + "/"
144: + p.getTopicId()
145: + SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION)
146: + "#" + p.getId();
147: }
148:
149: /**
150: * @see net.jforum.Command#list()
151: */
152: public void list() {
153: this .setTemplateName(TemplateKeys.KARMA_LIST);
154: this .context.put("message", I18n.getMessage("invalidAction"));
155: }
156: }
|