コード例 #1
0
ファイル: RankServlet.java プロジェクト: Xabouba/BarbyBet
  /** @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */
  protected void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    UsersComponent usersComponent = new UsersComponent();
    User currentUser = usersComponent.getCurrentUser(request);

    if (!usersComponent.isCurrentUser(currentUser)) {
      response.sendRedirect(Constants.LOGIN_SERVLET);
    } else {
      /** User group */
      SQLGroupComponent sqlGroupComponent = new SQLGroupComponent();
      Map<Long, Map<String, String>> groups = sqlGroupComponent.getGroups(currentUser.getId());
      request.setAttribute("groups", groups);

      Long idGroup = null;
      if (request.getParameter("group") != null && !request.getParameter("group").equals("all")) {
        idGroup = Long.parseLong((request.getParameter("group")));
        request.setAttribute("currentGroupId", idGroup);
      }

      int index = 1;
      int indexFound = 0;
      for (Long id : groups.keySet()) {
        if (id == idGroup) {
          indexFound = index;
        }
        index++;
      }

      if (indexFound == 0) {
        request.setAttribute("currentGroupIndex", "general");
        request.setAttribute("currentGroupId", "-1");
      } else {
        request.setAttribute("currentGroupIndex", String.valueOf(indexFound - 1));
      }

      /** User info */
      SQLUsersComponent sqlUserComponent = new SQLUsersComponent();
      User user = sqlUserComponent.getUser(currentUser.getId());
      request.setAttribute("userPoint", user.getCoins());
      request.setAttribute("userRank", user.getRank());

      int rankUser = user.getRank();
      int pageNumber = 1;
      if (rankUser % nbUserByPage == 0) {
        pageNumber = rankUser / nbUserByPage;
      } else {
        pageNumber = (int) (Math.floor(rankUser / nbUserByPage) + 1);
      }

      /** Classement */
      setRankRequest(request, currentUser, idGroup, pageNumber, nbUserByPage);

      this.getServletContext()
          .getRequestDispatcher("/WEB-INF/jsp/rank.jsp")
          .forward(request, response);
    }
  }
コード例 #2
0
ファイル: RankServlet.java プロジェクト: Xabouba/BarbyBet
  /** @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */
  protected void doPost(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    UsersComponent usersComponent = new UsersComponent();
    User currentUser = usersComponent.getCurrentUser(request);

    Long idGroup = null;
    if (request.getParameter("group") != null && !request.getParameter("group").equals("all")) {
      idGroup = Long.parseLong((request.getParameter("group")));
      request.setAttribute("currentGroupId", idGroup);
    }

    int page = 1;
    if (request.getParameter("page") != null) {
      page = Integer.valueOf(request.getParameter("page"));
    } else {
      int rankUser = 0;
      if (idGroup != null) {
        SQLRankComponent rankComponent = new SQLRankComponent();
        Map<String, Map<String, String>> rank = rankComponent.getGroupRank(idGroup);
        rankUser = Integer.parseInt(rank.get(currentUser.getUsername()).get("rank"));
      } else {
        SQLUsersComponent sqlUserComponent = new SQLUsersComponent();
        User user = sqlUserComponent.getUser(currentUser.getId());

        rankUser = user.getRank();
      }
      if (rankUser % nbUserByPage == 0) {
        page = rankUser / nbUserByPage;
      } else {
        page = (int) (Math.floor(rankUser / nbUserByPage) + 1);
      }
    }

    /** Classement */
    setRankRequest(request, currentUser, idGroup, page, nbUserByPage);

    this.getServletContext()
        .getRequestDispatcher("/WEB-INF/jsp/rank-part.jsp")
        .forward(request, response);
  }