//Created by MyEclipse Struts
// XSL source (default): platform:/plugin/com.genuitec.eclipse.cross.easystruts.eclipse_4.1.1/xslt/JavaClass.xsl

package cn.lingqi.struts.action;

import java.io.BufferedOutputStream;
import java.io.BufferedWriter;
import java.io.DataOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;

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.lingqi.dao.NewsFormDAO;
import cn.lingqi.struts.form.LogonForm;
import cn.lingqi.tool.PageFunction;

/** 
 * MyEclipse Struts
 * Creation date: 12-25-2006
 * 
 * XDoclet definition:
 * @struts.action path="/logon" name="logonForm" scope="request" validate="true"
 * @struts.action-forward name="success" path="/main.jsp"
 */
public class LogonAction extends Action {

	// --------------------------------------------------------- Instance Variables

	// --------------------------------------------------------- Methods

	/** 
	 * Method execute
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 */
	public ActionForward execute(
		ActionMapping mapping,
		ActionForm form,
		HttpServletRequest request,
		HttpServletResponse response) {
		LogonForm logonForm = (LogonForm) form;
		HttpSession session = request.getSession();
		String path = request.getRealPath("/toolbar.html");
		
		if (logonForm.getUserName().equalsIgnoreCase("test")){
			String logonType="yes";
			session.setAttribute("logonType",logonType);
			PageFunction pf = new PageFunction();
			NewsFormDAO ndao = new NewsFormDAO();
			pf.setTotalCount(ndao.newsCount());
			pf.setPage(1);
			ArrayList listNews = (ArrayList) ndao.queryPage(1, pf.getPageSie());
			int currentBar = (pf.getPage()-1)/pf.getBarSize()+1;
			session.setAttribute("currentBar",currentBar); //当前bar号
			session.setAttribute("listNews", listNews); 
			session.setAttribute("pageList", pf.paging()); //当前bar中的页号列表
			session.setAttribute("nowpage",1); //当前页号
			session.setAttribute("barcount",pf.getBarCount()); //bar总页数
			session.setAttribute("pagecount",pf.getpageCount()); //总页数
			System.out.println(path);
			try {
				FileWriter fw = new FileWriter(path);
				BufferedWriter bw = new BufferedWriter(fw);
				bw.write("<table width=\"100%\"  border=\"0\" cellpadding=\"0\" cellspacing=\"0\">\n");
				bw.write("<tr>\n");
				//bw.write("<td width=\"30px\" nowrap class=\"toolbar\">&nbsp;</td>\n");
				bw.write("<td width=\"90px\" nowrap class=\"toolbar\" onMouseOver=\"OMO()\" onMouseOut=\"OMOU()\" onClick=\"add()\"><img src=\"./images/add.gif\">添加新闻</td>\n");
				bw.write("<td width=\"30px\" nowrap class=\"toolbar\">&nbsp;</td>\n");
				bw.write("<td width=\"30px\" nowrap class=\"toolbar\">&nbsp;</td>\n");
				bw.write("</tr>\n");
				bw.write("</table>");
				bw.flush();
				bw.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}else{
			String logonType="no";
			session.setAttribute("logonType",logonType);
			try {
				FileOutputStream fos = new FileOutputStream(path);
				BufferedOutputStream bos = new BufferedOutputStream(fos);
				DataOutputStream dos = new DataOutputStream(bos);
				try {
					dos.writeBytes("");
					dos.flush();
					dos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			} catch (FileNotFoundException e) {
				e.printStackTrace();
			}
		}
		return mapping.findForward("success");
	}

}

