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.service.FlowControl;
import zr.zrpower.entity.sys.SessionUser;
import zr.zrpower.flowengine.runtime.ActFlowRun;

/**
* <p>Title:流程运行</p>
*/
public class FlowRun
    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");

      HttpSession session = request.getSession(true);
      SessionUser user = (SessionUser)session.getAttribute("userinfo");

      //得到流程的基础属性
      String strOptCmd = request.getParameter("OptCmd");//记录属性按钮命令值
      String strIdentification = request.getParameter("Identification");//流程标识
      String strOptCmd_Name = request.getParameter("OptCmd_Name");//记录属性按钮命令值中文
      String strWorkflow_No = request.getParameter("Workflow_No");//流程编号
      String strNode_No_S = request.getParameter("Node_No_S");//当前步骤编号
      String strM_Node_No_S_E = request.getParameter("M_Node_No_S_E");//下一分支步骤编号
      String strS_Node_No_S_E = request.getParameter("S_Node_No_S_E");//上一分支步骤编号
      String strUserNo = request.getParameter("UserNo");//当前用户编号
      String strExecute_No = request.getParameter("Execute_No");//流程流转编号
      String strDo_User_Nos = request.getParameter("Do_User_Nos");//选择的处理用户编号
      String strFormID = request.getParameter("FormID");//业务的ID值
      String strTitleName = request.getParameter("TitleName");//业务的标题名称
      String strOtherID = request.getParameter("OtherID");//关联字段的ID值
      String strPath = request.getParameter("Path");//流程调用文件的路径
      String strDoIdea = request.getParameter("DoIdea");//处理意见
      String strUnit = request.getParameter("Unit");//单位编号
      String strParentID = request.getParameter("ParentID");//父流程ID值或父表单记录值
      String strParentID1 = request.getParameter("ParentID1");//父流程ID1值或父表单记录值
      String strRID = request.getParameter("RID");//关联流程的流转ID
      String strMsgType = request.getParameter("MsgType");//短信提示类别
      String strRECORDID = request.getParameter("RECORDID");//附件ID
      String strISBRANCH=request.getParameter("ISBRANCH");//多路分支是否表现在按钮上

      String strIs = "0";

      this.isOk = true;
      this.Msg = "非法调用，操作不被接受，你的操作已被记录！";
      if(strOptCmd!=null){
      try {
      FlowControl Control = new FlowControl();

      ActFlowRun ActFlowRun = new ActFlowRun();
      //初始化流程运转类-----------------
      ActFlowRun.FlowInit(strOptCmd,strIdentification,strOptCmd_Name,strWorkflow_No,strNode_No_S,strM_Node_No_S_E,strS_Node_No_S_E,strUserNo,strExecute_No,strDo_User_Nos,strFormID,strTitleName,strOtherID,strPath,strDoIdea,strUnit,strParentID,strParentID1,strRID,strMsgType,strRECORDID,strISBRANCH);
      //初始化流程运转类结束---------------

         //保存-------------------------------------
         if (strOptCmd.equals("SAVE")){
           //执行流程保存服务
           strExecute_No = Control.FlowSaveServer(ActFlowRun);
           strPath=strPath.substring(1);
           //返回
           //---------------
           user = null;
           Control = null;
           ActFlowRun = null;
           //---------------

           response.sendRedirect(strPath+"?FlowNo="+strIdentification+"&FormID="+strFormID+"&OtherID="+strOtherID+"&ExecuteNo="+strExecute_No+"&ParentID="+strParentID+"&ParentID1="+strParentID1+"&ISBRANCH="+strISBRANCH+"&RID="+strRID+"&ISSAVE=1");
         }
         //提交-------------------------------------
         if (strOptCmd.equals("SUBMIT")){
           //执行流程提交服务
           Control.FlowSubmitServer(ActFlowRun,user);
           strIs = "1";
         }
         //处理返回-----------------------------------
         if (strOptCmd.equals("RETURN")){
           //执行流程处理返回服务
           Control.FlowReturnServer(ActFlowRun,user);
           strIs = "1";
         }
         //退回---------------------------------------
         if (strOptCmd.equals("UNTREAD")){

           //初始化流程运转类,退回的特殊情况(下面两个参数互换)-----------------
           ActFlowRun.FlowInitUntread(strS_Node_No_S_E,strM_Node_No_S_E);
           //初始化流程运转类结束---------------
           //------------
           //执行流程提交服务(第六个参数的值为退回步骤的ID--strS_Node_No_S_E,第六和第七个参数互换)
           Control.FlowSubmitServer(ActFlowRun,user);
         }
         //完成----------------------------------------
         if (strOptCmd.equals("FINISH")){
            //执行流程提交服务
             Control.FlowFinishServer(ActFlowRun,user);
             strIs = "1";
         }
         //移交----------------------------------------
         if (strOptCmd.equals("DEVOLVE")){
            //执行流程移交服务
             Control.FlowDevolveServer(ActFlowRun,user);
         }
         //删除----------------------------------------
         if (strOptCmd.equals("DELETE")){
            //执行流程删除服务
             Control.FlowDeleteServer(ActFlowRun,user);
         }
         //初始化----------------------------------------
         if (strOptCmd.equals("INIT")){
            //执行流程初始化服务
             Control.FlowInitServer(ActFlowRun,user);
         }
         //收回----------------------------------------
         if (strOptCmd.equals("TAKEBACK")){
            //执行流程收回服务
             Control.FlowTakeBackServer(ActFlowRun,user);
         }
         //---------------
         //送部门承办----------------------------------------
         if (strOptCmd.equals("SUBDEPT")){
            //执行送部门承办服务
             Control.FlowSubDeptServer(ActFlowRun,user);
         }
         //---------------
         //分送已阅--------
         if (strOptCmd.equals("SENDFINISH")){
            //执行分送已阅服务
             Control.FlowSendFinishServer(ActFlowRun,user);
         }
         //--------------


         user = null;
         Control = null;
         ActFlowRun = null;
         //---------------

         if (!strOptCmd.equals("SAVE")){
            //关闭窗口
            response.sendRedirect("ZrWorkFlow/FlowRun/FlowCloseWindow.jsp?ID="+strNode_No_S+"&ISMESSAGE="+strIs);
         }
      }
      catch (Exception ex){
      this.Msg = "连接中间件服务器失败，请稍后再试！";
      }
      }
  }

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

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