@Override
  public Map<String, Object> defineModel(HttpServletRequest request) {
    Map<String, Object> result = new HashMap<String, Object>();

    int start = (page - 1) * resultsPerPage;
    int end = start + resultsPerPage;

    Collection<Topic> topics =
        dao.find(
            new Comparator<Topic>() {
              public int compare(Topic o1, Topic o2) {
                if (o2.getLastPostMilli().equals(o1.getLastPostMilli())) {
                  return 0;
                } else if (o2.getLastPostMilli() > o1.getLastPostMilli()) {
                  return 1;
                } else {
                  return -1;
                }
              }
            },
            start,
            end);

    result.put("topics", topics);

    hasNext = (topics.size() == resultsPerPage);

    result.put("member", request.getSession().getAttribute("user"));
    result.put("controller", this);

    return result;
  }
  @Override
  public Page processRequest(HttpServletRequest request) {
    Page result = this;

    if ("new".equals(request.getParameter("action"))) {
      Member member = (Member) request.getSession().getAttribute("user");

      result = new TopicEdit(this, new Topic(member));
    } else if ("previous".equals(request.getParameter("action"))) {
      if (page > 1) {
        page--;
      }
    } else if ("next".equals(request.getParameter("action"))) {
      if (page < getNumberOfPages()) {
        page++;
      }
    } else if (isAction("edit")) {
      long id = Long.parseLong(request.getParameter("actionValue"));

      result = new TopicEdit(this, dao.find(id));
    } else if (isAction("remove")) {
      long topicId = Long.parseLong(request.getParameter("actionValue"));

      Topic topic = dao.find(topicId);

      CommentDao commentDao = new CommentDao();

      for (Comment comment : topic.getComments()) {
        commentDao.remove(comment);
      }

      dao.remove(topicId);
    }

    return result;
  }
示例#3
0
  @RequestMapping(
      value = "/{section}/{group}/{id}/comments",
      produces = "application/json; charset=UTF-8",
      method = RequestMethod.GET)
  @ResponseBody
  public Map<String, Object> getComments(
      @PathVariable("section") String sectionName,
      @PathVariable("group") String groupName,
      @PathVariable("id") int msgid,
      @RequestParam(value = "page", defaultValue = "0") int page,
      HttpServletRequest request)
      throws Exception {
    Topic topic = topicDao.getById(msgid);
    Group group = groupDao.getGroup(topic.getGroupId());
    Section section = sectionService.getSection(group.getSectionId());

    if (!section.getUrlName().equals(sectionName)
        || !group.getUrlName().equals(groupName)
        || page < 0) {
      throw new MessageNotFoundException(msgid);
    }

    permissionService.checkView(topic, AuthUtil.getCurrentUser());

    CommentList comments = commentService.getCommentList(topic, false);

    CommentFilter cv = new CommentFilter(comments);

    int messagesPerPage = AuthUtil.getProfile().getMessages();

    List<Comment> commentsFiltered =
        cv.getCommentsForPage(false, page, messagesPerPage, ImmutableSet.<Integer>of());

    List<PreparedComment> preparedComments =
        prepareService.prepareCommentList(
            comments, commentsFiltered, request.isSecure(), Template.getTemplate(request), topic);

    return ImmutableMap.of(
        "comments",
        preparedComments,
        "topic",
        new ApiCommentTopicInfo(
            topic.getId(),
            topic.getLink(),
            permissionService.isCommentsAllowed(topic, AuthUtil.getCurrentUser())));
  }
 public int getNumberOfResults() {
   return dao.size();
 }
 public int getNumberOfPages() {
   return ((dao.size() - 1) / resultsPerPage) + 1;
 }