为jive论坛加上投票功能
jive是什么就不多说了,我用过好些论坛的投票,比较喜欢动网论坛的投票。
操作很简单,选择单选还是多选,然后输入投票选项,每一行做一个选项,最多10个。
我把这个功能加进jive里面,建立两个新表:
CREATE TABLE jivevote (
voteid int not null,--投票id,跟jive的theadid保持一致
type int NOT NULL,--投票类型,单选还是多选
votenum int NOT NULL,--数目
votetxt text NOT NULL,--投票的文本内容
content1 int default 0,
content2 int default 0,
content3 int default 0,
content4 int default 0,
content5 int default 0,
content6 int default 0,
content7 int default 0,
content8 int default 0,
content9 int default 0,
content10 int default 0,
PRIMARY KEY (voteid)
);
CREATE TABLE jivevoterecord (
voteid int not null,--投票id,跟jive的theadid保持一致
userid int not null--记录投过票的用户
);
写一个Poll.java文件,为求方便都写为静态方法,
package com.jivesoftware.forum.database;//这样可以调用连接池
增加主题投票,用户提交投票内容,是否单选
public static void addVoteThread(long voteid,String votetxt,boolean single,int votenum){
String sqlstr = "insert into jivevote (voteid,type,votetxt,votenum) values(?,?,?,?)";
Connection con = null;
PreparedStatement stmt = null;
try
{
con = ConnectionManager.getConnection();
stmt = con.prepareStatement(sqlstr);
stmt.setLong(1,voteid);
stmt.setInt(2,single?0:1);
stmt.setString(3,votetxt);
stmt.setInt(4,votenum);
stmt.executeUpdate();
}
catch(SQLException sqle)
{
sqle.printStackTrace();
}
finally
{
try
{
stmt.close();
}
catch(Exception e)
{
e.printStackTrace();
}
try
{
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
执行投票操作,threadID为新建立投票的时候建立的主题id,voted数组为选项
public static boolean vote(long threadID,int[] voted,long userID){
if(isVoted(threadID,userID)){
return false;
}
Connection con = null;
PreparedStatement stmt = null;
try
{
con = ConnectionManager.getConnection();
for(int i=0;i<voted.length;i++){
if(voted[i]!=0){
stmt = con.prepareStatement("update jivevote set content" + voted[i] + "= content" + voted[i] + "+1 where voteid=?");
stmt.setLong(1,threadID);
stmt.executeUpdate();
}
}
stmt = con.prepareStatement("insert into jivevoterecord(voteid,userid) values(?,?)");
stmt.setLong(1,threadID);
stmt.setLong(2,userID);
stmt.executeUpdate();
}
catch(SQLException sqle)
{
sqle.printStackTrace();
}
finally
{
try
{
stmt.close();
}
catch(Exception e)
{
e.printStackTrace();
}
try
{
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
return true;
}
判断是否已经投过票
public static boolean isVoted(long threadID,long userID){
Connection con = null;
PreparedStatement stmt = null;
boolean ret = false;
try
{
con = ConnectionManager.getConnection();
stmt = con.prepareStatement("select voteid from jivevoterecord where voteid=? and userid=?");
stmt.setLong(1,threadID);
stmt.setLong(2,userID);
ResultSet rs = stmt.executeQuery();
ret = rs.next();
}
catch(SQLException sqle)
{
sqle.printStackTrace();
}
finally
{
try
{
stmt.close();
}
catch(Exception e)
{
e.printStackTrace();
}
try
{
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
return ret;
}
取得投票内容
public static String getVoteView(long threadID,long userID){
Connection con = null;
PreparedStatement stmt = null;
StringBuffer sb = new StringBuffer();
sb.append("<table cellpadding=\"5\" cellspacing=\"0\" border=\"0\" width=\"100%\" align=\"center\">");
boolean isVotedUser = false;
try
{
con = ConnectionManager.getConnection();
stmt = con.prepareStatement("select * from jivevote where voteid=?");
stmt.setLong(1,threadID);
ResultSet rs = stmt.executeQuery();
String s="";
if(rs.next()){
int type = rs.getInt("type");
int votenum = rs.getInt("votenum");
sb.append("<input type=hidden name=type value=\"" + type + "\">");
sb.append("<input type=hidden name=votenum value=\"" + votenum + "\">");
String votetxt = rs.getString("votetxt");
String[] opts = votetxt.split("\n");
for(int i=1;i<=opts.length;i++){
sb.append("<tr>");
if(i==1){
s = " checked";
}else{
s="";
}
if(type==1){
sb.append("<td>" + i + ".<input type=checkbox name=chk_" + i + " value=" + i + ">" + opts[i-1] + "</td><td>票数:<font color=red><b>" + rs.getInt("content"+i)
+ "</b></font></td>");
}else{
sb.append("<td>" + i + ".<input type=radio name=chk value=" + i + s +">" + opts[i-1] + "</td><td>票数:<font color=red><b>" + rs.getInt("content"+i)
+ "</b></font></td>");
}
sb.append("</tr>");
}
}
isVotedUser = isVoted(threadID,userID);
if(!isVotedUser){
sb.append("<tr><td><input type=submit name='submit' value=' 投票 '></td><td></td><td></td></tr>");
}else{
sb.append("<tr><td><font color=red>你已经投过票了</font></td><td></td><td></td></tr>");
}
}
catch(SQLException sqle)
{
sqle.printStackTrace();
}
finally
{
try
{
stmt.close();
}
catch(Exception e)
{
e.printStackTrace();
}
try
{
con.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
sb.append("</table>");
return sb.toString();
}
增加一个新的jsp:
<%
/**
* $RCSfile: addVote.jsp,v $
* $Revision: 1.13.4.2 $
* $Date: 2002/05/21 20:38:12 $
*/
%>
<%@ page import="java.net.*,
java.text.*,
java.util.*,
com.jivesoftware.util.*,
com.jivesoftware.forum.*,
com.jivesoftware.forum.util.*"
errorPage="error.jsp"
%>
<%@ include file="postUtils.jsp" %>
<%@ include file="global.jsp" %>
<%@page contentType="text/html;charset=GBK"%>
<% // Put the request URI and query string in the session as an attribute.
// This is done so the error.jsp and auth.jsp pages can figure out what
// page sent it an error and redirect appropriately.
setRedirectURL(request);
%>
<%
if(pageUser==null){
throw new UnauthorizedException("你没有权限操作.");
}
long forumID = ParamUtils.getLongParameter(request,"forum",-1L);
long threadID = ParamUtils.getLongParameter(request,"thread",-1L);
int type = ParamUtils.getIntParameter(request,"type",0);
int votenum = ParamUtils.getIntParameter(request,"votenum",0);
String opt = ParamUtils.getParameter(request,"chk");
forumFactory=com.jivesoftware.forum.database.DbForumFactory.getInstance();
// Load the forum
Forum forum = forumFactory.getForum(forumID);
ForumThread thread = forum.getThread(threadID);
int opti=1;
try{
opti = Integer.parseInt(opt);
}catch(Exception e){
}
if(type==0){
int[] opts = new int[1];
opts[0] = opti;
Helper.vote(threadID,opts,pageUser.getID());
}else{
if(votenum>0){
int[] opts = new int[votenum];
int i=0;
Enumeration e = request.getParameterNames();
while ( e.hasMoreElements() ){
String vid=(String)e.nextElement();
if ( vid.startsWith("chk_") ){
opts[i] = Integer.parseInt(request.getParameter(vid));
i++;
}
}
Helper.vote(threadID,opts,pageUser.getID());
}
}
response.sendRedirect("thread.jsp?forum=" + forumID + "&thread=" + threadID);
%>
在post.jsp里面加入投票
...
String votetxt = ParamUtils.getParameter(request,"votetxt");
int votetype = ParamUtils.getIntParameter(request,"votetype",0);
...
if (doPost && subjectOK && bodyOK) {
if(fromPreview){
newMessage = postMessage(request, reply, forumFactory, forum, thread,
messageID, pageUser, name, email, subject, body);
}else{
newMessage = postMessage(request, reply, forumFactory, forum, thread,
messageID, pageUser, name, email, subject, body);
}
thread = newMessage.getForumThread();
//增加投票
if(vote && votetxt!=null && !reply){
int votenum = 0;
String[] voteoptions = votetxt.split("\n");
if(voteoptions.length>=10){
votenum=10;
}else{
votenum = voteoptions.length;
}
votetxt="";
for (int i = 0; i < votenum;i++) {
votetxt = votetxt + voteoptions[i] + "\n";
}
Poll.addVoteThread(thread.getID(),votetxt,votetype==0,votenum);
thread.setProperty("vote","true");//设置为投票帖子
}
在显示投票内容的地方调用(我写在threadMode2.jsp里面)
<%//显示投票
String voteView = Poll.getVoteView(threadID,pageUser.getID());
out.print(voteView);
%>