package zr.zrpower.web.servlet;

import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
import zr.zrpower.web.bean.MessageBox;
import zr.zrpower.dbmanage.service.TableService;
import zr.zrpower.dbmanage.BPIP_TABLE;
import zr.zrpower.common.util.FunctionMessage;
import zr.zrpower.ejb.sys.OperateTableService;


public class ActionTable
    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")) {
        EditTable(request, response);
      }

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

      if (strAction.equals("add")) {
        AddTable(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 EditTable(HttpServletRequest request,
                        HttpServletResponse response) throws IOException {
    FunctionMessage fm = new FunctionMessage(1);
    BPIP_TABLE TABLEID = new BPIP_TABLE();
    TABLEID.fullDataFromRequest(request);
    try {
      String stroldname = request.getParameter("OLDTABLENAME");
	  TableService Table = new TableService();
      fm = Table.EditTC(TABLEID);
       OperateTableService TableService = new OperateTableService();
      TableService.ChangeTableName(stroldname,request.getParameter("TABLENAME"));
       if (fm.getResult()) {
         Table = null;
         TableService = null;
         TABLEID = null;
         response.sendRedirect("zrsysmanage/DbManage/CloseWindow.jsp");
      }
      else {
        this.Msg = fm.getMessage();
        this.isOk = fm.getResult();
      }
    }
    catch (Exception ex) {
      this.Msg = "文件管理中间件服务失败！<br>详细错误信息：" + ex.toString();
    }

    this.out = response.getWriter();
    this.returnPath = "zrsysmanage/DbManage/CloseWindow.jsp";
    //this.GetBox();
  }

  //删除表名表
  private void DeleteTable(HttpServletRequest request,
                          HttpServletResponse response) throws IOException {
    try {
      FunctionMessage fm = new FunctionMessage(1);
	   TableService Table = new TableService();
      fm = Table.DeleteTC(request.getParameter("TABLEID"));
      OperateTableService TableService = new OperateTableService();
      TableService.DeleteTable(request.getParameter("TABLENAME"));
      if (fm.getResult()) {
        this.Msg = fm.getMessage();
        this.isOk = fm.getResult();
      }
      else {
        this.Msg = fm.getMessage();
        this.isOk = fm.getResult();
      }
      Table = null;
      TableService = null;
      response.sendRedirect("zrsysmanage/DbManage/CloseWindow.jsp");
    }
    catch (Exception ex) {
      this.Msg = "连接文件管理中间件服务失败！<br>详细错误信息：" + ex.toString();
    }
    this.out = response.getWriter();
    this.returnPath = "zrsysmanage/DbManage/CloseWindow.jsp";
    //this.GetBox();
  }

  //添加表名表
  private void AddTable(HttpServletRequest request, HttpServletResponse response) throws
      IOException {
    FunctionMessage fm = new FunctionMessage(1);
    BPIP_TABLE TABLEID = new BPIP_TABLE();
    try {
	  TableService Table = new TableService();
      TABLEID.fullDataFromRequest(request);
      fm = Table.AddTC(TABLEID);
      if (fm.getResult()) {
        TABLEID = null;
        Table = null;
        response.sendRedirect("zrsysmanage/DbManage/CloseWindow.jsp");
      }
      else {
        this.Msg = fm.getMessage();
        this.isOk = fm.getResult();
      }
    }
    catch (Exception ex2) {
      this.Msg = "连接客户端文件中间件服务失败！<br>详细错误信息：<br>   " + ex2.toString();
    }

    this.out = response.getWriter();
    this.returnPath = "zrsysmanage/DbManage/CloseWindow.jsp";

  }
}
