KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > jforum > view > forum > KarmaAction


1 /*
2  * Copyright (c) Rafael Steil
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms,
6  * with or without modification, are permitted provided
7  * that the following conditions are met:
8  *
9  * 1) Redistributions of source code must retain the above
10  * copyright notice, this list of conditions and the
11  * following disclaimer.
12  * 2) Redistributions in binary form must reproduce the
13  * above copyright notice, this list of conditions and
14  * the following disclaimer in the documentation and/or
15  * other materials provided with the distribution.
16  * 3) Neither the name of "Rafael Steil" nor
17  * the names of its contributors may be used to endorse
18  * or promote products derived from this software without
19  * specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT
22  * HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
23  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
24  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR
26  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
27  * THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
28  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
32  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
34  * IN CONTRACT, STRICT LIABILITY, OR TORT
35  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
36  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
37  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
38  *
39  * Created on Jan 11, 2005 11:44:06 PM
40  * The JForum Project
41  * http://www.jforum.net
42  */

43 package net.jforum.view.forum;
44
45 import net.jforum.Command;
46 import net.jforum.JForumExecutionContext;
47 import net.jforum.SessionFacade;
48 import net.jforum.dao.DataAccessDriver;
49 import net.jforum.dao.KarmaDAO;
50 import net.jforum.dao.PostDAO;
51 import net.jforum.entities.Karma;
52 import net.jforum.entities.KarmaStatus;
53 import net.jforum.entities.Post;
54 import net.jforum.repository.PostRepository;
55 import net.jforum.repository.SecurityRepository;
56 import net.jforum.security.SecurityConstants;
57 import net.jforum.util.I18n;
58 import net.jforum.util.preferences.ConfigKeys;
59 import net.jforum.util.preferences.SystemGlobals;
60 import net.jforum.util.preferences.TemplateKeys;
61 import net.jforum.view.forum.common.PostCommon;
62 import net.jforum.view.forum.common.ViewCommon;
63
64 /**
65  * @author Rafael Steil
66  * @version $Id: KarmaAction.java,v 1.18 2006/01/29 15:06:57 rafaelsteil Exp $
67  */

68 public class KarmaAction extends Command
69 {
70     public void insert() throws Exception JavaDoc
71     {
72         if (!SecurityRepository.canAccess(SecurityConstants.PERM_KARMA_ENABLED)) {
73             this.error("Karma.featureDisabled", null);
74             return;
75         }
76
77         int postId = this.request.getIntParameter("post_id");
78         int fromUserId = SessionFacade.getUserSession().getUserId();
79
80         PostDAO pm = DataAccessDriver.getInstance().newPostDAO();
81         Post p = pm.selectById(postId);
82
83         if (fromUserId == SystemGlobals.getIntValue(ConfigKeys.ANONYMOUS_USER_ID)) {
84             this.error("Karma.anonymousIsDenied", p);
85             return;
86         }
87
88         if (p.getUserId() == fromUserId) {
89             this.error("Karma.cannotSelfVote", p);
90             return;
91         }
92
93         KarmaDAO km = DataAccessDriver.getInstance().newKarmaDAO();
94         
95         if (!km.userCanAddKarma(fromUserId, postId)) {
96             this.error("Karma.alreadyVoted", p);
97             return;
98         }
99         
100         // Check range
101
int points = this.request.getIntParameter("points");
102         
103         if (points < SystemGlobals.getIntValue(ConfigKeys.KARMA_MIN_POINTS)
104                 || points > SystemGlobals.getIntValue(ConfigKeys.KARMA_MAX_POINTS)) {
105             this.error("Karma.invalidRange", p);
106             return;
107         }
108
109         Karma karma = new Karma();
110         karma.setFromUserId(fromUserId);
111         karma.setPostUserId(p.getUserId());
112         karma.setPostId(postId);
113         karma.setTopicId(p.getTopicId());
114         karma.setPoints(points);
115
116         km.addKarma(karma);
117         
118         p.setKarma(new KarmaStatus(p.getId(), points));
119         
120         if (SystemGlobals.getBoolValue(ConfigKeys.POSTS_CACHE_ENABLED)) {
121             PostRepository.update(p.getTopicId(), PostCommon.preparePostForDisplay(p));
122         }
123
124         JForumExecutionContext.setRedirect(this.urlToTopic(p));
125     }
126
127     private void error(String JavaDoc message, Post p)
128     {
129         this.setTemplateName(TemplateKeys.KARMA_ERROR);
130
131         if (p != null) {
132             this.context.put("message", I18n.getMessage(message, new String JavaDoc[] { this.urlToTopic(p) }));
133         }
134         else {
135             this.context.put("message", I18n.getMessage(message));
136         }
137     }
138
139     private String JavaDoc urlToTopic(Post p)
140     {
141         return JForumExecutionContext.getRequest().getContextPath() + "/posts/list/"
142             + ViewCommon.getStartPage()
143             + "/" + p.getTopicId()
144             + SystemGlobals.getValue(ConfigKeys.SERVLET_EXTENSION)
145             + "#" + p.getId();
146     }
147
148     /**
149      * @see net.jforum.Command#list()
150      */

151     public void list() throws Exception JavaDoc
152     {
153         this.setTemplateName(TemplateKeys.KARMA_LIST);
154         this.context.put("message", I18n.getMessage("invalidAction"));
155     }
156
157     /**
158      * TODO: Make dynamic data format TODO: refactoring here to remove the duplicated code with the
159      * method above. Performs a search over the users votes between two dates.
160      * FIXME: "order_by" should not come from the HTML form representing the real db column name
161      *
162      * @throws Exception
163      */

164     /*
165     public void searchByPeriod() throws Exception
166     {
167         SimpleDateFormat formater = new SimpleDateFormat("dd/MM/yyyy");
168         Date firstPeriod, lastPeriod;
169         if ("".equals(this.request.getParameter("first_date"))) {
170             firstPeriod = formater.parse("01/01/1970");// extreme date
171         }
172         else {
173             firstPeriod = formater.parse(this.request.getParameter("first_date"));
174         }
175         if ("".equals(this.request.getParameter("last_date"))) {
176             lastPeriod = new Date();// now
177         }
178         else {
179             lastPeriod = formater.parse(this.request.getParameter("last_date"));
180         }
181
182         String orderField;
183         if ("".equals(this.request.getParameter("order_by"))) {
184             orderField = "total";
185         }
186         else {
187             orderField = this.request.getParameter("order_by");
188         }
189
190         int usersPerPage = SystemGlobals.getIntValue(ConfigKeys.USERS_PER_PAGE);
191         // Load all users with your karma
192         List users = DataAccessDriver.getInstance().newKarmaDAO().getMostRatedUserByPeriod(usersPerPage, firstPeriod,
193                 lastPeriod, orderField);
194         this.context.put("users", users);
195         this.setTemplateName(TemplateKeys.KARMA_SEARCH_BYPERIOD);
196     }
197     */

198
199     /**
200      * FIXME: The date format is not dynamic.
201      * FIXME: "order_by" should not come from the HTML form representing the real db column name
202      *
203      * Performs a search over the users votes in a specific month.
204      *
205      * @throws Exception
206      */

207     /*
208     public void searchByMonth() throws Exception
209     {
210         SimpleDateFormat formater = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
211         int month = Integer.parseInt(request.getParameter("month"));
212         int year = Integer.parseInt(request.getParameter("year"));
213
214         Calendar c = Calendar.getInstance();
215         Date firstPeriod, lastPeriod;
216         firstPeriod = formater.parse("01/" + month + "/" + year+ " 00:00:00");
217         // set the Calendar with the first day of the month
218         c.setTime(firstPeriod);
219         // Now get the last day of this month.
220         lastPeriod = formater.parse(c.getActualMaximum(Calendar.DAY_OF_MONTH) + "/" + month + "/" + year +" 23:59:59");
221
222         String orderField;
223         if ("".equals(request.getParameter("order_by")) || request.getParameter("order_by") == null) {
224             orderField = "total";
225         }
226         else {
227             orderField = this.request.getParameter("order_by");
228         }
229
230         int usersPerPage = SystemGlobals.getIntValue(ConfigKeys.USERS_PER_PAGE);
231         // Load all users with your karma
232         List users = DataAccessDriver.getInstance().newKarmaDAO().getMostRatedUserByPeriod(usersPerPage, firstPeriod,
233                 lastPeriod, orderField);
234         this.context.put("users", users);
235         this.setTemplateName(TemplateKeys.KARMA_SEARCH_BYMONTH);
236     }
237     */

238 }
239
Popular Tags