/* Generated by Together */

package zr.zrpower.collectionengine;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import zr.zrpower.entity.sys.SessionUser;
import zr.zrpower.collectionengine.service.CollectionService;


public class CollectionEngine
    extends HttpServlet {

  //Initialize global variables
  public void init() throws ServletException {

  }

  //Process the HTTP Get request
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws
      ServletException, IOException {
    response.setContentType("text/html; charset=GBK");
    request.setCharacterEncoding("GBK");
    String html = "<p align=\"center\">非法调用，程序将自动返回上一界面...</p>";
    int iMode = 0;    //采集模式：0采集模版预览，1打印模版预览，2数据采集，3打印
    Request rq = new Request(request);

    if(rq!=null&&rq.getCount()>0){
      String sMode = rq.getStringItem("coll_mode");
      if(sMode!=null&&!sMode.equals("")){
         iMode = Integer.parseInt(sMode);
      }else{
        rq.appendItem("coll_mode","0");
      }
      try {
         HttpSession session = request.getSession(true);
         SessionUser userInfo = (SessionUser)session.getAttribute("userinfo");
         CollectionService cs = new CollectionService();

         //System.out.println(rq.getStringItem("coll_aid"));
         switch(iMode){
            case 0 : html = cs.CollectionPriview(rq,userInfo);
                     break;
            case 1 : html = cs.PrintPriview(rq,userInfo);
                     break;
            case 2 :html = cs.CollectionData(rq,userInfo);
                     break;
            case 3 : html = cs.PrintDocument(rq);
                     break;
         }
         userInfo = null;
         cs = null;
       }
       catch (Exception ex) {

         response.getWriter().print("<p>采集引擎发生异常，请稍后再试！</p><p>详细错误信息:</p><p>" +
                                    ex.toString() + "</p>"); ;
       }
    }


    //临时加,采集引擎没有调用到配置信息时刷新父窗口-------------
    if (html.equals("采集引擎发生异常：<br>流程配置与采集配置错误，未找到相关配置信息。") || html.equals("采集引擎发生错误，采集配置信息未找到！") )
    {
                StringBuffer sb = new StringBuffer();
                sb.append("<HTML>");
                sb.append("<script language=\"javascript\">");
                sb.append("setTimeout('show();',800);");
                sb.append("function show(){\r\n");
                sb.append("parent.window.location.reload();\r\n");
                sb.append("}\r\n");
                sb.append(" </script>");
                sb.append("</HTML>");
                response.getWriter().print(sb.toString());
    }else
    {
         response.getWriter().print(html);
    }
    //response.getWriter().print(html);
  }

  //Process the HTTP Post request
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws
      ServletException, IOException {
    doGet(request, response);
  }

  //Clean up resources
  public void destroy() {
  }

}
