package zr.zrpower.collengine.web.servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import java.util.*;
import zr.zrpower.web.bean.MessageBox;
import zr.zrpower.common.web.Upload;
import zr.zrpower.common.util.SysPreperty;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.ServletException;
import zr.zrpower.collectionengine.*;
import zr.zrpower.collectionengine.service.ConfigService;
import zr.zrpower.common.util.FunctionMessage;


public class ActionConfigMsg
    extends MessageBox {

  //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");
    //分析上传数据
    Upload upload = new Upload();
    upload.setSwitch(false);
    upload.setUpType(0);
    upload.setSuffix(".*");
    upload.setSize(1024*1024*20);  //上传限制为20M
    upload.setObjectPath(SysPreperty.getProperty().ShowTempletPath);

    boolean bUploadOK = false;  //上传是否成功
    try {
      upload.setSourceFile(request);
      bUploadOK = true;
    }
    catch (IOException ex1) {
      this.Msg = "分析上传数据失败！<br>详细错误信息：<br>   "+ex1.toString();
    }

    if(bUploadOK){
      Hashtable htFields = upload.getFields();
      String[] saFile = upload.getObjectFileName();
      String[] stra = upload.getDescription();
      String strAct = (String) htFields.get("Act");
      //================================主配置
      if (strAct.equals("add")) {
        if (htFields != null) {
        CollectionInfo config = new CollectionInfo();
         FunctionMessage fm = new FunctionMessage(1);
          try {

		     ConfigService configManage = new ConfigService();

            config.fullDataFromHashtable(htFields);
            if (saFile != null) {
              if (saFile.length > 0) {
                config.setTEMPLAET(saFile[0]);
              }
            }
            fm = configManage.addConfig(config);
             if (fm.getResult())
            {
              configManage = null;
              response.sendRedirect("ZrCollEngine/ConfigMsg/MainConfig/List.jsp");
            }
            else
            {
              this.Msg = fm.getMessage();
              this.isOk = fm.getResult();
            }
          }
          catch (Exception ex2) {
            this.Msg = "连接客户端插件中间件服务失败！<br>详细错误信息：<br>   " + ex2.toString();
          }
        }
      }
      if (strAct.equals("reload")) {

          if (upload.getCount() < 1) {
               this.Msg = stra[0];
             }
             else {
               CollectionInfo config = new CollectionInfo();
               try {

		          ConfigService configManage = new ConfigService();

                 config.fullDataFromHashtable(htFields);

                if (saFile != null) {
                  if (saFile.length > 0) {
                    config.setTEMPLAET(saFile[0]);
                  }
                }

                 String TempaetID =(String) htFields.get("ID");
                 configManage.UpdateTempaet(TempaetID,config);

                 configManage = null;
                 response.sendRedirect("ZrCollEngine/ConfigMsg/MainConfig/List.jsp");
               }
               catch (Exception ex) {
                 this.Msg = "连接客户端中间件服务失败！<br>详细错误信息：<br>   " + ex.toString();
               }
             }

      }



           }
          }

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

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

