package com.wxpn.tutorial.servlet;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wxpn.tutorial.db.ConnectionPool;
import com.wxpn.tutorial.db.DB;

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

	/*
	 * (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 mb_title = request.getParameter("MB_TITLE");
		String mb_content = request.getParameter("MB_CONTENT");

		String newtime = request.getParameter("newtime");
		
		NewsMgr mgr = new NewsMgr();
		News msg = mgr.getMessage(Integer.parseInt(id));
		msg.setTitle(mb_title);
		msg.setContent(mb_content);
		msg.setAuthor(request.getSession().getAttribute("userName").toString());
		if ((newtime != null) && (newtime.equals("true"))) {
			SimpleDateFormat simpledate = new SimpleDateFormat(
					"yyyy-MM-dd HH:mm:ss");
			msg.setStrCompose_date(simpledate.format(new Date()));
		}

		int i = mgr.update(msg);
		if (i == 1) {
			// ³É¹¦
			Collection c = mgr.getAllMessages(5, 1);
			request.setAttribute("c", c);
			try {
				request.getRequestDispatcher("/admin_browse.jsp").forward(
						request, response);
			} catch (Throwable t) {
				getServletContext().log(t.getMessage());
			}
		}
	}
}