/**
  * 处理Service返回状态
  *
  * @param status
  * @param request
  * @param response
  * @param session
  * @throws IOException
  */
 private void handleStatus(
     int status, HttpServletRequest request, HttpServletResponse response, HttpSession session)
     throws IOException {
   switch (status) {
     case GroupService.OWNER_CAN_NOT_DROP_OUT:
       WebUtils.setResponseMessage(
           response,
           "error",
           "handlegroupmember_owner_not_deleted",
           session.getId(),
           request.getLocale());
       break;
     case GroupService.SUCCESS:
       WebUtils.setResponseBean(response, "success", null, session.getId(), request.getLocale());
       break;
     case GroupService.UID_NOT_OWNER:
       WebUtils.setResponseMessage(
           response, "error", "handlegroupmember_not_owner", session.getId(), request.getLocale());
       break;
     case GroupService.USER_NOT_INT_GROUP:
       WebUtils.setResponseMessage(
           response,
           "error",
           "handlegroupmember_user_not_in_group",
           session.getId(),
           request.getLocale());
       break;
     default:
       break;
   }
 }
 /**
  * 处理踢出群成员
  *
  * @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());
    }
  }