package zr.zrpower.flowengine.web.servlet;


import zr.zrpower.flowengine.mode.monitor.FLOW_CONFIG_ENTRUST;
import zr.zrpower.flowengine.service.FlowMonitor;
import zr.zrpower.web.bean.MessageBox;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import javax.servlet.ServletException;
import zr.zrpower.common.util.FunctionMessage;
import javax.servlet.http.HttpServletResponse;
import zr.zrpower.common.util.Log;


public class FlowEntrust
    extends MessageBox {
  Log log;

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

  public void doGet(HttpServletRequest request, HttpServletResponse response) throws
      ServletException, IOException {
    request.setCharacterEncoding("GBK");
    response.setContentType("text/html; charset=GBK");
    String strAction = request.getParameter("Act");
    if (!strAction.equals(null)) {
          if (strAction.equals("add")) {
        AddFlowpu(request, response);
      }
      if (strAction.equals("del")) {
       Delete(request, response);
     }
     if (strAction.equals("edit")) {
       EditFlowpk(request, response);
     }

   }
}

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

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


  //添加委托人员
  private void AddFlowpu(HttpServletRequest request, HttpServletResponse response) throws
      IOException {
    String strUNIT=request.getParameter("UNIT");
    FunctionMessage fm = new FunctionMessage(1);
    FLOW_CONFIG_ENTRUST Entrust = new FLOW_CONFIG_ENTRUST();
    try {
      FlowMonitor Fentrust =  new FlowMonitor();
      Entrust.fullDataFromRequest(request);
      fm = Fentrust.AddEntrust(Entrust,strUNIT);

      if (fm.getResult()) {
        Entrust = null;
        response.sendRedirect("ZrWorkFlow/FlowMonitor/FlowEntrust/EntrustList.jsp");
      }
      else {
        this.Msg = fm.getMessage();
        this.isOk = fm.getResult();
      }
      Entrust = null;
    }
    catch (Exception ex2) {
      this.Msg = "连接客户端文件中间件服务失败！<br>详细错误信息：<br>   " + ex2.toString();
    }

    this.out = response.getWriter();
    this.returnPath = "ZrWorkFlow/FlowMonitor/FlowEntrust/EntrustList.jsp";
    //this.GetBox();
  }

  //删除委托人员基本属性
   private void Delete(HttpServletRequest request,
                           HttpServletResponse response) throws IOException {
     try {
       FunctionMessage fm = new FunctionMessage(1);
       FlowMonitor Fentrust =  new FlowMonitor();
       fm = Fentrust.DeleteEntrust(request.getParameter("ID"));
       if (fm.getResult()) {
         Fentrust = null;
         response.sendRedirect("ZrWorkFlow/FlowMonitor/FlowEntrust/EntrustList.jsp");
       }
       else {
         this.Msg = fm.getMessage();
         this.isOk = fm.getResult();
       }
       Fentrust = null;
     }
     catch (Exception ex) {
       this.Msg = "连接文件管理中间件服务失败！<br>详细错误信息：" + ex.toString();
     }
     this.out = response.getWriter();
     this.returnPath = "ZrWorkFlow/FlowMonitor/FlowEntrust/EntrustList.jsp";
     //this.GetBox();
   }

   //修改委托人员
   private void EditFlowpk(HttpServletRequest request,
                        HttpServletResponse response) throws IOException {
    FunctionMessage fm = new FunctionMessage(1);
    FLOW_CONFIG_ENTRUST Entrust = new FLOW_CONFIG_ENTRUST();
    Entrust.fullDataFromRequest(request);
      try {
       FlowMonitor Fentrust =  new FlowMonitor();
         fm = Fentrust.EditEntrust(Entrust);
            if (fm.getResult()) {
              Fentrust = null;
              response.sendRedirect("ZrWorkFlow/FlowMonitor/FlowEntrust/EntrustList.jsp");
            }
            else {
             this.Msg = fm.getMessage();
             this.isOk = fm.getResult();
            }
            Fentrust = null;
    }
    catch (Exception ex) {
      this.Msg = "文件管理中间件服务失败！<br>详细错误信息：" + ex.toString();
    }

    this.out = response.getWriter();
    this.returnPath = "ZrWorkFlow/FlowMonitor/FlowEntrust/EntrustList.jsp";
    //this.GetBox();
  }


}
