/**
  * 处理踢出群成员
  *
  * @param request
  * @param response
  * @param user
  * @param session
  * @param groupIdInt
  * @throws IOException
  */
 private void deleteMember(
     HttpServletRequest request,
     HttpServletResponse response,
     User user,
     HttpSession session,
     int groupIdInt)
     throws IOException {
   GroupService groupService = new GroupServiceImpl();
   String deleteId = request.getParameter("deleteId");
   if (deleteId == null || deleteId.trim().equals("")) {
     // deleteId为空异常
     WebUtils.setResponseMessage(
         response,
         "error",
         "handlegroupmember_deleteid_null",
         session.getId(),
         request.getLocale());
     return;
   }
   int deleteIdInt = 0;
   try {
     deleteIdInt = Integer.parseInt(deleteId);
   } catch (Exception e) {
     // deleteId不是int
     WebUtils.setResponseMessage(
         response,
         "error",
         "handlegroupmember_deleteid_error",
         session.getId(),
         request.getLocale());
     return;
   }
   try {
     int status = groupService.dropOutGroup(user.getUid(), deleteIdInt, groupIdInt, false);
     handleStatus(status, request, response, session);
   } catch (UserNotExistException e) {
     WebUtils.setResponseMessage(
         response, "error", "error_uid_not_exist", session.getId(), request.getLocale());
     return;
   } catch (GroupNotExistException e) {
     WebUtils.setResponseMessage(
         response, "error", "error_gid_not_exist", session.getId(), request.getLocale());
     return;
   }
 }
  /**
   * The doPost method of the servlet. <br>
   * This method is called when a form has its tag value method equals to post.
   *
   * @param request the request send by the client to the server
   * @param response the response send by the server to the client
   * @throws ServletException if an error occurred
   * @throws IOException if an error occurred
   */
  public void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {

    try {
      request.setCharacterEncoding("utf-8");
      response.setCharacterEncoding("utf-8");
      response.setContentType("text/html;charset=utf-8");

      User user = null;
      // 检测登录状态,通过session拿回user对象
      // getSession(false),如果用户不登陆的话是不会有session存在的
      HttpSession session = request.getSession(false);
      if (session == null) {
        user = WebUtils.login(request);
        if (user != null) {
          session = request.getSession(false);
          session.setAttribute("user", user);
        } else {
          // 未登录状态
          WebUtils.setResponseMessage(
              response, "error", "error_not_login", null, request.getLocale());
          return;
        }
      } else user = (User) session.getAttribute("user");
      if (user == null) {
        user = WebUtils.login(request);
        if (user != null) {
          session = request.getSession(false);
          session.setAttribute("user", user);
        } else {
          // 未登录状态
          WebUtils.setResponseMessage(
              response, "error", "error_not_login", null, request.getLocale());
          return;
        }
      }

      // 处理标记
      String handleType = request.getParameter("handleType");
      if (handleType == null || handleType.trim().equals("")) {
        // handleType为空异常
        WebUtils.setResponseMessage(
            response, "error", "handlegroup_handletype_null", session.getId(), request.getLocale());
        return;
      }
      // 要处理的群id
      String groupId = request.getParameter("groupId");
      if (groupId == null || groupId.trim().equals("")) {
        // groupId为空异常
        WebUtils.setResponseMessage(
            response, "error", "userfile_gid_null", session.getId(), request.getLocale());
        return;
      }
      // 群id转换成int
      int groupIdInt = 0;
      try {
        groupIdInt = Integer.parseInt(groupId);
      } catch (Exception e) {
        // groupId不是int
        WebUtils.setResponseMessage(
            response, "error", "userfile_gid_error", session.getId(), request.getLocale());
        return;
      }

      if (handleType.equals("addmember")) {
        // 加入群
        GroupService groupService = new GroupServiceImpl();
        try {
          Group group = groupService.add2Group(user.getUid(), groupIdInt);
          if (group != null) {
            // 添加成功,返回添加后的群实体
            WebUtils.setResponseBean(
                response, "success", group, session.getId(), request.getLocale());
            return;
          } else {
            // 添加失败,返回失败信息
            WebUtils.setResponseMessage(
                response, "error", "error_exist_in_group", session.getId(), request.getLocale());
            return;
          }
        } catch (UserNotExistException e) {
          WebUtils.setResponseMessage(
              response, "error", "error_uid_not_exist", session.getId(), request.getLocale());
          return;
        } catch (GroupNotExistException e) {
          WebUtils.setResponseMessage(
              response, "error", "error_gid_not_exist", session.getId(), request.getLocale());
          return;
        }
      } else if (handleType.equals("deletemember")) {
        // 踢出群成员
        deleteMember(request, response, user, session, groupIdInt);
        return;
      } else if (handleType.equals("exitgroup")) {
        // 退出群
        GroupService groupService = new GroupServiceImpl();
        try {
          int status = groupService.dropOutGroup(user.getUid(), user.getUid(), groupIdInt, true);
          handleStatus(status, request, response, session);
        } catch (UserNotExistException e) {
          WebUtils.setResponseMessage(
              response, "error", "error_uid_not_exist", session.getId(), request.getLocale());
          return;
        } catch (GroupNotExistException e) {
          WebUtils.setResponseMessage(
              response, "error", "error_gid_not_exist", session.getId(), request.getLocale());
          return;
        }
      } else if (handleType.equals("dissolve")) {
        // 解散群
        GroupService groupService = new GroupServiceImpl();
        try {
          int status = groupService.dissolveGroup(user.getUid(), groupIdInt);
          handleStatus(status, request, response, session);
        } catch (GroupNotExistException e) {
          WebUtils.setResponseMessage(
              response, "error", "error_gid_not_exist", session.getId(), request.getLocale());
          return;
        }
      } else {
        // 没有该处理标记
        WebUtils.setResponseMessage(
            response, "error", "handlegroup_handletype_null", session.getId(), request.getLocale());
      }
    } catch (Exception e) {
      e.printStackTrace();
      // 服务器未知异常,反馈信息
      WebUtils.setResponseMessage(response, "error", "error_server", null, request.getLocale());
    }
  }