package com.wxpn.tutorial.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class for Servlet: EditVoteServlet
 */
public class EditVoteServlet extends javax.servlet.http.HttpServlet implements
		javax.servlet.Servlet {
	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	public EditVoteServlet() {
		super();
	}

	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request,
	 *      HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);
		response.setContentType("text/html;charset=UTF-8");
		String id = request.getParameter("id");

		VoteMgr mgr = new VoteMgr();
		Question question = mgr.getVote(Integer.parseInt(id));

		request.setAttribute("question", question);

		try {
			request.getRequestDispatcher("/admin/editVote.jsp").forward(
					request, response);
		} catch (Throwable t) {
			getServletContext().log(t.getMessage());
		}
	}

	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request,
	 *      HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String id = request.getParameter("id");
		String startTime = request.getParameter("startyear") + "-"
				+ request.getParameter("startmonth") + "-"
				+ request.getParameter("startday");
		String endTime = request.getParameter("endyear") + "-"
				+ request.getParameter("endmonth") + "-"
				+ request.getParameter("endday");
		String active = request.getParameter("active");
		VoteMgr mgr = new VoteMgr();
		mgr.updateVote(Integer.parseInt(id), startTime, endTime, Integer
				.parseInt(active));

		response.sendRedirect("/Vote/admin/index.jsp");
	}
}