package com.sino.publicclass.base;

import java.io.IOException;

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

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.sino.publicclass.util.StringTools;

public class RequestHandle extends HttpServlet {
	
	private static final long serialVersionUID = -1243187676403055994L;
	private final Log log = LogFactory.getLog(getClass().getName());
	private ServletConfig servletConfig;
	private static String default_actionName = "BaseAction";
	private static String default_action_url = ".do";
	private static String default_per_package = "com.sino.rrs.action";
	private static String default_error_page = "exception.jsp";
	private static String default_wait_page = "wait.jsp";
	private static String per_package;
	private String error_Page = "";
	private String wait_Page = "";
	private String actionName = "";
	private String action_url = "";

	public RequestHandle() {
	}
   
	@Override
	public void init() {
		log.info("");
		servletConfig = getServletConfig();
		per_package = getInitPara("perpackage");
		
		error_Page = getInitPara("errorPage");
		
		wait_Page = getInitPara("waitPage");
	
		actionName = getInitPara("actionName");
		
		action_url = getInitPara("actionUrl");

		if (StringTools.ValidateStr(per_package)) {
			per_package = default_per_package;
		}
		if (StringTools.ValidateStr(error_Page)) {
			error_Page = default_error_page;
		}
		if (StringTools.ValidateStr(wait_Page)) {
			wait_Page = default_wait_page;
		}
		if (StringTools.ValidateStr(action_url)) {
			action_url = default_action_url;
		}
		if (StringTools.ValidateStr(actionName)) {
			actionName = default_actionName;
		}
	}

	@Override
	public void service(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		BaseAction action;
		String pathinfo = "";
		try {
			if (VAR.getRealpath().trim().length() < 1) {
				VAR.setRealpath(getServletContext().getRealPath(""));
			}
			pathinfo = getPathinfo(req.getServletPath());
			action = (BaseAction) Class.forName(pathinfo).newInstance();
			action.service(req, res);
			if(action.isNext()){
				if (action.isTransition()) {
					callpage(wait_Page, req, res);
				} else {
					callpage(action.getNextPage(), req, res);
				}
			}
		} catch (ClassNotFoundException ex) {
			String em = "ClassNotFoundException    " + pathinfo;
			em = "<p align='center'><font color='#ff0000'>" + em + "</font>";
			log.error(em);
			req.setAttribute("failureMsg", em);
			callpage(error_Page, req, res);
		} catch (Exception ex) {
			String em = ex.getMessage();
			log.error("",ex);
			log.error("pathinfo=" + pathinfo);
			if(em==null)em="";
			if (em.indexOf("page:") != 0) {
				em = "<p align='center'><font color='#ff0000'>" + StringTools.convertForTextArea(em) + "</font>";
			} else {
				em = "<p align='left'><font color='#ff0000'>" + em + "</font>";
			}
			req.setAttribute("failureMsg", em);
			callpage(error_Page, req, res);
		}
	}

	private String getPathinfo(String path) {
		if (path == null)
			path = "";
		String pathinfo = per_package + path;
		pathinfo = pathinfo.replace('/', '.');
		pathinfo = pathinfo.substring(0, pathinfo.lastIndexOf(action_url));
		return pathinfo;
	}

	private void callpage(String page, HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		try {
			getServletContext().getRequestDispatcher(page).forward(req, res);
		} catch (Exception ex) {
			log.error("", ex);
			throw new ServletException("page: " + page + "  compile error <br>" + ex.getMessage());
		}
	}
    
	private String getInitPara(String name) {
		String temp = servletConfig.getInitParameter(name);
		
		if (StringTools.ValidateStr(temp)) {
			temp = Config.get(name);
		}
		return temp;
	}
	
}
