package zr.zrpower.flowengine.web.servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import zr.zrpower.web.bean.MessageBox;
import zr.zrpower.flowengine.mode.base.Button;
import zr.zrpower.flowengine.service.FlowSetup;
import zr.zrpower.common.util.FunctionMessage;


public class FlowButton
    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 {
    request.setCharacterEncoding("GBK");
    response.setContentType("text/html; charset=GBK");
    String strAction = request.getParameter("Act");
    if (!strAction.equals(null)) {
      if (strAction.equals("edit")) {
        EditButton(request, response);
      }

      if (strAction.equals("del")) {
        DeleteButton(request, response);
      }

      if (strAction.equals("add")) {
        AddButton(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 EditButton(HttpServletRequest request,
                        HttpServletResponse response) throws IOException {
    FunctionMessage fm = new FunctionMessage(1);
    Button ButtonID = new Button();
    ButtonID.fullDataFromRequest(request);
    try {
      FlowSetup FlowPackage = new FlowSetup();
      fm = FlowPackage.EditButton(ButtonID);
      if (fm.getResult()) {
        FlowPackage = null;
        response.sendRedirect("ZrWorkFlow/BaseSetup/FlowBaseButton/ButtonList.jsp");
      }
      else {
        this.Msg = fm.getMessage();
        this.isOk = fm.getResult();
      }
      FlowPackage = null;
    }
    catch (Exception ex) {
      this.Msg = "文件管理中间件服务失败！<br>详细错误信息：" + ex.toString();
    }

    this.out = response.getWriter();
    this.returnPath = "ZrWorkFlow/BaseSetup/FlowBaseButton/ButtonList.jsp";
    //this.GetBox();
  }

  //删除流程包
  private void DeleteButton(HttpServletRequest request,
                          HttpServletResponse response) throws IOException {
    try {
      FunctionMessage fm = new FunctionMessage(1);
      FlowSetup FlowPackage = new FlowSetup();
      fm = FlowPackage.DeleteButton(request.getParameter("ID"));

      if (fm.getResult()) {
        this.Msg = fm.getMessage();
        this.isOk = fm.getResult();
      }
      else {
        this.Msg = fm.getMessage();
        this.isOk = fm.getResult();
      }
      FlowPackage = null;
      response.sendRedirect("ZrWorkFlow/BaseSetup/FlowBaseButton/ButtonList.jsp");
    }
    catch (Exception ex) {
      this.Msg = "连接文件管理中间件服务失败！<br>详细错误信息：" + ex.toString();
    }
    this.out = response.getWriter();
    this.returnPath = "ZrWorkFlow/BaseSetup/FlowBaseButton/ButtonList.jsp";
    //this.GetBox();
  }

  //添加流程包
  private void AddButton(HttpServletRequest request, HttpServletResponse response) throws
      IOException {
    FunctionMessage fm = new FunctionMessage(1);
    Button ButtonID = new Button();
    try {
      FlowSetup FlowPackage = new FlowSetup();
      ButtonID.fullDataFromRequest(request);
      fm = FlowPackage.AddButton(ButtonID);

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

    this.out = response.getWriter();
    this.returnPath = "ZrWorkFlow/BaseSetup/FlowBaseButton/ButtonList.jsp";
    //this.GetBox();

  }



}
