/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
package cn.jack.struts;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.List;

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

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import cn.jack.hibernate.Bookcontent;
import cn.jack.service.BookcontentDao;
import cn.jack.tool.IConstants;
import cn.jack.tool.PagesBean;
import cn.jack.tool.StringTool;

/** 
 * MyEclipse Struts
 * Creation date: 05-13-2007
 * 
 * XDoclet definition:
 * @struts.action path="/gbook" name="gbookForm" input="/WEB-INF/JSP/main.jsp" scope="request"
 */
public class GbookAction extends Action {
	private BookcontentDao bdao;
	/*
	 * Generated Methods
	 */

	/** 
	 * Method execute
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws IOException 
	 * @throws ServletException 
	 */
	/*
	 * 程序分配
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String action = request.getParameter("action");
		if(action.equals("add")){return add(mapping,form,request,response);}
		if(action.equals("list")){return list(mapping,form,request,response);}
		if(action.equals("delete")){return delete(mapping,form,request,response);}
		if(action.equals("replay")){return replay(mapping,form,request,response);}
		return null;
	}
	/*
	 * 这里接收留言
	 */
	public ActionForward add(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		GbookForm gf = (GbookForm) form;// TODO Auto-generated method stub
		Bookcontent bt = new Bookcontent();
		bt.setGbcontent(StringTool.ConversionAll(gf.getGbcontent()));
		bt.setGbface(gf.getGbface().trim());
		bt.setGbip(request.getRemoteAddr());
		bt.setGbmail(gf.getGbmail().trim());
		bt.setGbtitle(gf.getGbtitle().trim());
		bt.setGbuser(gf.getGbuser().trim());
		java.text.SimpleDateFormat fors = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss");
		bt.setGbtime(fors.format(new java.util.Date()));
		bdao.newmessage(bt);
		return mapping.findForward(IConstants.INDEX_KEY);
	}
	/*
	 * 这里显示留言
	 */
	public ActionForward list(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) {
		List allmessage = bdao.allmessage();
		request.setAttribute("allmessage", allmessage);
		String pn = request.getParameter("pn");
		if(pn == null) {
			pn = "0";
		}
		int page = Integer.parseInt(pn);
		
		PagesBean pages = new PagesBean();

		pages.setEveryPage(IConstants.EVERYPAGENUMBER_KEY);
		pages.setTotalItem(allmessage.size());
		pages.setCurrentPage(page);
		
		request.setAttribute("pages", pages);
		return mapping.findForward("list");
	}
	/*
	 * 删除
	 */
	public ActionForward delete(ActionMapping mapping, ActionForm form,
				HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session =request.getSession();
		if(session.getAttribute("manager")==null ||! session.getAttribute("manager").equals(IConstants.SESSION_KEY)){
			request.getRequestDispatcher("/login.do").forward(request, response);
			return null;
		}
		String id=request.getParameter("id");
		bdao.delete(id);
		return mapping.findForward(IConstants.INDEX_KEY);
	}
		
	/*
	 * 回复
	 * 
	 */
		public ActionForward replay(ActionMapping mapping, ActionForm form,
				HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		GbookForm gf = (GbookForm) form;
		HttpSession session =request.getSession();
		if(session.getAttribute("manager")==null ||! session.getAttribute("manager").equals(IConstants.SESSION_KEY)){
			request.getRequestDispatcher("/login.do").forward(request, response);
			return null;
		}
		String id=request.getParameter("id").trim();
		String re=StringTool.ConversionAll(gf.getGbre());
		java.text.SimpleDateFormat fors = new SimpleDateFormat("MM/dd/yyyy hh:mm:ss");
		String retime=fors.format(new java.util.Date());
		bdao.replay(id, re, retime);
		return mapping.findForward(IConstants.INDEX_KEY);
	}
	
	/*
	 * 有spring注入，设定get与set。
	 */
	public BookcontentDao getBdao() {
		return bdao;
	}
	public void setBdao(BookcontentDao bdao) {
		this.bdao = bdao;
	}
}