package zr.zrpower.web.servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import zr.zrpower.web.bean.MessageBox;
import zr.zrpower.entity.sys.*;
import zr.zrpower.ejb.sys.UnitService;
import zr.zrpower.common.util.*;


public class ActionUnit
    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 strAct = request.getParameter("Act");
    this.isOk = true;
    this.Msg = "非法调用，操作不被接受，你的操作已被记录！";
    this.returnPath = "zrsysmanage/UnitMain.jsp?nodeId=" +
                                 request.getParameter("UnitID");
    if(strAct!=null){
        try {
          UnitService UnitMgr = new UnitService();
          if (strAct.equals("edit")) {
            BPIP_UNIT Unit = new BPIP_UNIT();
            Unit.fullDataFromRequest(request);
            if (UnitMgr.UpdateUnit(Unit)) {
                UnitMgr = null;
                Unit = null;
                response.sendRedirect("zrsysmanage/UnitMain.jsp?isLoad=yes&nodeId=" + request.getParameter("UnitID"));
            }
            else {
               response.sendRedirect("zrsysmanage/UnitMain.jsp?isLoad=yes&nodeId=" + request.getParameter("UnitID"));
            }
          }
          else if (strAct.equals("delete")) {
            String strID = request.getParameter("unitid");
            if (strID != null) {
               UnitMgr.DeleteUnit(strID);
            }
            response.sendRedirect("zrsysmanage/UnitMain.jsp?isLoad=yes&nodeId=deleteisLoad=yes&");
          }
          else if (strAct.equals("add")) {
            BPIP_UNIT Unit = new BPIP_UNIT();
            Unit.fullDataFromRequest(request);
            String strID = request.getParameter("UnitID");

            FunctionMessage fm = UnitMgr.AddUnit(Unit);
            this.isOk = fm.getResult();
            if(isOk){
              UnitMgr = null;
              Unit = null;
            }
            response.sendRedirect("zrsysmanage/UnitMain.jsp?isLoad=yes&nodeId=" + strID);
          }
        }
        catch (Exception ex) {
           this.Msg = "连接中间件服务器失败，请稍后再试！<br>详细错误信息： <br>"+ex.toString();
        }

    }
    this.out = response.getWriter();

  }

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

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