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.SecurityService;
import zr.zrpower.common.util.*;

public class ActionRole
    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 = "非法调用，操作不被接受，你的操作已被记录！";

    if(strAct!=null){
        try {
          SecurityService SeMgr = new SecurityService();
          if (strAct.equals("edit")) {
            String strMenu_No = request.getParameter("Menu_No");
            BPIP_ROLE Role = new BPIP_ROLE();
            Role.fullDataFromRequest(request);

            if (SeMgr.EditRole(Role,strMenu_No))
               {
                 SeMgr = null;
                 Role = null;
                 response.sendRedirect("zrsysmanage/role/List.jsp");
               }
            else
               {this.Msg = "编辑角色信息失败或角色重复或本角色已经分配用户！";}

          }
          if (strAct.equals("edituser")) {
           String strFormfields = request.getParameter("formfields");
           String strRole = request.getParameter("ROLEID");
           String strUnitId = request.getParameter("UNITID");
         if (SeMgr.EditRoleUser(strRole,strUnitId,strFormfields))
              {
                SeMgr = null;
                response.sendRedirect("zrsysmanage/role/ListRoleUser.jsp");
              }
           else
              {this.Msg = "编辑角色信息失败或角色重复！";}

         }
          else if (strAct.equals("delete")) {
            String strROLEID = request.getParameter("ROLEID");
            if (strROLEID != null) {
                if (SeMgr.Role_Del(strROLEID))
                {
                  SeMgr = null;
                  response.sendRedirect("zrsysmanage/role/List.jsp");
                }
                else
                {
                  this.isOk = false;
                  this.Msg = "删除角色出错!";
                  this.returnPath = "zrsysmanage/role/List.jsp";
                }
            }
            else {
              this.isOk = false;
              this.Msg = "删除角色出错!";
              this.returnPath = "zrsysmanage/role/List.jsp";
            }
          }
          else  if (strAct.equals("userrolemanage")) {
                    String strRoelListStr = request.getParameter("RoelListStr");
                    String strUserListStr = request.getParameter("UserListStr");
                    String strUnit = request.getParameter("UnitId");
                         if (SeMgr.UserRoleManage(strRoelListStr,strUserListStr))
                       {response.sendRedirect("zrsysmanage/UserCloseWindow.jsp");}
                    else
                       { this.Msg = "保存角色出错!";
                         this.returnPath = "zrsysmanage/UserList.jsp?nodeId=" + strUnit;}

                 }

          else if (strAct.equals("add")) {
            String strMenu_No = request.getParameter("Menu_No");

            BPIP_ROLE Role = new BPIP_ROLE();
            Role.fullDataFromRequest(request);
            if (SeMgr.AddRole(Role,strMenu_No))
               {
                 SeMgr = null;
                 response.sendRedirect("zrsysmanage/role/List.jsp");
               }
            else
               {this.Msg = "增加角色失败或角色重复！";}
          }
        }
        catch (Exception ex){
           this.Msg = "连接中间件服务器失败，请稍后再试！";
        }

    }

    this.out = response.getWriter();
    //this.GetBox();
  }

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

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