1.
package com.sanqing.action; import java.util.UUID; import com.opensymphony.xwork2.ActionSupport;
import com.sanqing.bean.Vote;
import com.sanqing.bean.VoteOption;
import com.sanqing.dao.VoteDAO;
import com.sanqing.dao.VoteOptionDAO;
import com.sanqing.daoFactory.VoteDAOFactory;
import com.sanqing.daoFactory.VoteOptionDAOFactory; public class AddVoteAction extends ActionSupport {
private int channel; // 封装channel参数
private String voteName; // 封装voteName参数
private String[] voteOption;// 封装voteOption参数 public int getChannel() {
return channel;
} public void setChannel(int channel) {
this.channel = channel;
} public String getVoteName() {
return voteName;
} public void setVoteName(String voteName) {
this.voteName = voteName;
} public String[] getVoteOption() {
return voteOption;
} public void setVoteOption(String[] voteOption) {
this.voteOption = voteOption;
} public String execute() throws Exception {
VoteDAO voteDAO = VoteDAOFactory.getVoteDAOInstance();//获得VoteDAO实例
VoteOptionDAO voteOptionDAO =
VoteOptionDAOFactory.getVoteOptionDAOInstance();//获得voteOption实例
//首先保存投票,然后再保存投票选项
Vote vote = new Vote();
vote.setChannelID(channel);
vote.setVoteName(voteName);
voteDAO.addVote(vote);
//查询投票ID
int voteID = voteDAO.findVoteByName(voteName).getVoteID();
//保存投票选项
for(String voteOptionName : voteOption) {
VoteOption vp = new VoteOption();
vp.setVoteID(voteID);
vp.setVoteOptionName(voteOptionName);
voteOptionDAO.addVoteOption(vp);
}
return this.SUCCESS;
} }
2.
package com.sanqing.action; import java.util.List; import com.opensymphony.xwork2.ActionSupport;
import com.sanqing.bean.VoteOption;
import com.sanqing.dao.VoteDAO;
import com.sanqing.dao.VoteOptionDAO;
import com.sanqing.daoFactory.VoteDAOFactory;
import com.sanqing.daoFactory.VoteOptionDAOFactory; public class DeleteVoteAction extends ActionSupport{
private int voteID; public int getVoteID() {
return voteID;
} public void setVoteID(int voteID) {
this.voteID = voteID;
} public String execute() throws Exception {
VoteDAO voteDAO = VoteDAOFactory.getVoteDAOInstance();
VoteOptionDAO voteOptionDAO = VoteOptionDAOFactory
.getVoteOptionDAOInstance();
//通过该投票ID查找该投票下的所有投票选项
List<VoteOption> voteOptions = voteOptionDAO.findVoteOptionByVoteID(voteID);
//循环进行删除
for(VoteOption voteOption : voteOptions) {
voteOptionDAO.deleteVoteOption(voteOption.getVoteOptionID());
}
//再删除该投票
voteDAO.deleteVote(voteID);
return this.SUCCESS;
}
}
3.
package com.sanqing.action; import javax.servlet.http.Cookie; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.sanqing.bean.VoteOption;
import com.sanqing.dao.VoteOptionDAO;
import com.sanqing.daoFactory.VoteOptionDAOFactory; public class DoVoteAction extends ActionSupport{
private int voteOptionID;
private String otherOption;
private int voteID;
private int channelID; public int getChannelID() {
return channelID;
}
public void setChannelID(int channelID) {
this.channelID = channelID;
}
public int getVoteID() {
return voteID;
} public void setVoteID(int voteID) {
this.voteID = voteID;
} public int getVoteOptionID() {
return voteOptionID;
} public void setVoteOptionID(int voteOptionID) {
this.voteOptionID = voteOptionID;
} public String getOtherOption() {
return otherOption;
} public void setOtherOption(String otherOption) {
this.otherOption = otherOption;
} public String execute() throws Exception {
VoteOptionDAO voteOptionDAO = VoteOptionDAOFactory
.getVoteOptionDAOInstance();
//首先判断用户是否能进行投票
Cookie cookies[]= ServletActionContext.getRequest().getCookies();//取出cookies for(Cookie cookie : cookies) {//遍历cookies if(cookie.getValue().equals(Integer.toString(voteID))) {//如果用户已经投过票
this.addActionError("您今天已经投过票了,请明天再来!");
return this.INPUT;
}
}
//判断是否选择其他选项
if(voteOptionID == 0) {
//添加该选项
VoteOption voteOption = new VoteOption();
voteOption.setVoteID(voteID);
voteOption.setVoteOptionName(otherOption);
voteOption.setTicketNum(1);
voteOptionDAO.addVoteOption(voteOption);
}else {
//取出以前的投票选项
VoteOption voteOption = voteOptionDAO.findVoteOptionById(voteOptionID);
int ticketNum = voteOption.getTicketNum();
//更新选项的投票数
voteOption.setTicketNum(ticketNum + 1);
voteOptionDAO.updateVoteOption(voteOption);
//更新完成后,添加cookie,防止重复投票
Cookie cookie = new Cookie("hasVote" + voteID,Integer.toString(voteID));
ServletActionContext.getResponse().addCookie(cookie);
}
return this.SUCCESS;
} }
4.
package com.sanqing.action; import java.util.ArrayList;
import java.util.List; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport;
import com.sanqing.bean.Vote;
import com.sanqing.bean.VoteOption;
import com.sanqing.bean.VoteResult;
import com.sanqing.dao.VoteDAO;
import com.sanqing.dao.VoteOptionDAO;
import com.sanqing.daoFactory.VoteDAOFactory;
import com.sanqing.daoFactory.VoteOptionDAOFactory;
import com.sanqing.util.Page;
import com.sanqing.util.PageUtil; public class ShowVoteAction extends ActionSupport {
private int currentPage; public int getCurrentPage() {
return currentPage;
} public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
} public String execute() throws Exception {
VoteDAO voteDAO = VoteDAOFactory.getVoteDAOInstance();
VoteOptionDAO voteOptionDAO = VoteOptionDAOFactory
.getVoteOptionDAOInstance();
int totalCount = voteDAO.findAllCount();
Page page = PageUtil.createPage(10, totalCount, currentPage);
List<Vote> votes = voteDAO.findAllVote(page);
List<VoteResult> voteResultList = new ArrayList<VoteResult>();
for (Vote vote : votes) {
List<VoteOption> voteOptions = voteOptionDAO
.findVoteOptionByVoteID(vote.getVoteID());
VoteResult voteResult = new VoteResult();
voteResult.setVote(vote);
voteResult.setVoteOptions(voteOptions);
voteResultList.add(voteResult);
}
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("voteResultList",voteResultList);
request.setAttribute("page",page);
return this.SUCCESS;
}
}
5.
package com.sanqing.action; import java.util.ArrayList;
import java.util.List; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.ServletActionContext; import com.opensymphony.xwork2.ActionSupport;
import com.sanqing.bean.Vote;
import com.sanqing.bean.VoteOption;
import com.sanqing.bean.VoteResult;
import com.sanqing.dao.VoteDAO;
import com.sanqing.dao.VoteOptionDAO;
import com.sanqing.daoFactory.VoteDAOFactory;
import com.sanqing.daoFactory.VoteOptionDAOFactory;
import com.sanqing.util.Page;
import com.sanqing.util.PageUtil; public class ShowVoteByChannelAction extends ActionSupport {
private int channelID;
private int currentPage; public int getChannelID() {
return channelID;
} public void setChannelID(int channelID) {
this.channelID = channelID;
} public int getCurrentPage() {
return currentPage;
} public void setCurrentPage(int currentPage) {
this.currentPage = currentPage;
} public String execute() throws Exception {
VoteDAO voteDAO = VoteDAOFactory.getVoteDAOInstance();
VoteOptionDAO voteOptionDAO = VoteOptionDAOFactory
.getVoteOptionDAOInstance();
// 获得该频道下的记录数
int totalCount = voteDAO.fintCountByChannel(channelID);
// 设置分页信息
Page page = PageUtil.createPage(3, totalCount, currentPage);
//取得该频道下的记录
List<Vote> votes = voteDAO.findVoteByChannel(page, channelID);
//存放所有投票和投票选项
List<VoteResult> voteResultList = new ArrayList<VoteResult>();
for (Vote vote : votes) {
//查询该投票下的所有投票选项
List<VoteOption> voteOptions = voteOptionDAO
.findVoteOptionByVoteID(vote.getVoteID());
VoteResult voteResult = new VoteResult();
voteResult.setVote(vote);
voteResult.setVoteOptions(voteOptions);
voteResultList.add(voteResult);
}
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("voteResultList",voteResultList);
request.setAttribute("page",page);
return this.SUCCESS;
}
}
6.
package com.sanqing.action; import java.util.List; import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset; import com.opensymphony.xwork2.ActionSupport;
import com.sanqing.bean.Vote;
import com.sanqing.bean.VoteOption;
import com.sanqing.dao.VoteDAO;
import com.sanqing.dao.VoteOptionDAO;
import com.sanqing.daoFactory.VoteDAOFactory;
import com.sanqing.daoFactory.VoteOptionDAOFactory; public class VoteResultAction extends ActionSupport {
private JFreeChart chart;
private int voteID;//投票ID public int getVoteID() {
return voteID;
} public void setVoteID(int voteID) {
this.voteID = voteID;
} public JFreeChart getChart() {
VoteDAO voteDAO = VoteDAOFactory.getVoteDAOInstance();//获得VoteDAO实例
VoteOptionDAO voteOptionDAO =
VoteOptionDAOFactory.getVoteOptionDAOInstance();//获得voteOption实例
//根据投票ID得到的投票
Vote vote = voteDAO.findVoteById(voteID);
String voteName = vote.getVoteName(); //得到投票名称 //根据投票ID得到所有的投票选项
List<VoteOption> voteOptions = voteOptionDAO.findVoteOptionByVoteID(voteID); DefaultCategoryDataset dcd = new DefaultCategoryDataset();//数据源 //设置数据
for(VoteOption voteOption : voteOptions) {
dcd.setValue(voteOption.getTicketNum(),"",voteOption.getVoteOptionName());
}
//使用工厂类创建柱状图
JFreeChart chart = ChartFactory.createBarChart3D(
voteName,
"投票选项",
"投票数",
dcd,
PlotOrientation.VERTICAL ,
false,
true,
false); return chart;
} public void setChart(JFreeChart chart) {
this.chart = chart;
} public String execute() throws Exception {
return SUCCESS;
}
}