/** * Calculates modification date of topic taking it as last post in topic creation date. * Used after deletion of the post. It is necessary to save the sort order of topics in the future. */ public void recalculateModificationDate() { DateTime newTopicModificationDate = getFirstPost().getCreationDate(); for (Post post : getPosts()) { if (post.getCreationDate().isAfter(newTopicModificationDate.toInstant())) { newTopicModificationDate = post.getCreationDate(); } } modificationDate = newTopicModificationDate; }
/** * This method will return true if there are unread posts in that topic * for the current user. This state is NOT persisted and must be * explicitly set by calling Topic.setLastReadPostDate(). * <p/> * If setter has not been called this method will always return <code>true</code> * * @return if current topic has posts still unread by the current user */ public boolean isHasUpdates() { return (lastReadPostDate == null) || (lastReadPostDate.isBefore(getLastPost().getCreationDate())); }
/** * Add new {@link Post} to the topic. * The method sets Posts.topic field to this Topic. * * @param post post to add */ public void addPost(Post post) { setModificationDate(post.getCreationDate()); post.setTopic(this); this.posts.add(post); }
/** * Returns first post that is newer then give time * @param time time to looking for newer post * @return first post that is newer then give time or first post if there is no post that is newer */ private Post getFirstNewerPost(DateTime time) { for (Post post : getPosts()) { if (post.getCreationDate().isAfter(time)) { return post; } } return getFirstPost(); }
@Override public int compare(Post o1, Post o2) { return o1.getRating() == o2.getRating() ? o2.getCreationDate().compareTo(o1.getCreationDate()) : o2.getRating() - o1.getRating(); } }
/** * Remove the post from the topic. * * @param postToRemove post to remove */ public void removePost(Post postToRemove) { posts.remove(postToRemove); Topic topic = postToRemove.getTopic(); if (postToRemove.getCreationDate().withMillisOfSecond(0) .equals(topic.getModificationDate().withMillisOfSecond(0))) { topic.recalculateModificationDate(); } }
/** * Stores last read post info into database for the particular topic and user. * * @param user user to save last read post data for * @param topic topic to store info for * @param lastPost last post in the topic (or in the last read page of the topic) */ private void saveLastReadPost(JCUser user, Topic topic, Post lastPost) { DateTime lastTimeForumWasMarkedRead = user.getAllForumMarkedAsReadTime(); DateTime topicModifiedDate = topic.getModificationDate(); if (lastTimeForumWasMarkedRead != null && topicModifiedDate.isBefore(lastTimeForumWasMarkedRead)) { return; } LastReadPost lastReadPost = lastReadPostDao.getLastReadPost(user, topic); if (lastReadPost == null) { lastReadPost = new LastReadPost(user, topic, lastPost.getCreationDate()); } else { if (lastPost.getCreationDate().isAfter(lastReadPost.getPostCreationDate())) { lastReadPost.setPostCreationDate(lastPost.getCreationDate()); } else { return; } } lastReadPostDao.saveOrUpdate(lastReadPost); }
/** * {@inheritDoc} */ @Override public List<Topic> fillLastReadPostForTopics(List<Topic> topics) { JCUser currentUser = userService.getCurrentUser(); if (!currentUser.isAnonymous()) { List<Topic> notModifiedTopics = extractNotModifiedTopicsSinceForumMarkedAsRead( currentUser, topics); for (Topic notModifiedTopic : notModifiedTopics) { Post lastPost = notModifiedTopic.getLastPost(); notModifiedTopic.setLastReadPostDate(lastPost.getCreationDate()); } // @SuppressWarnings("unchecked") List<Topic> modifiedTopics = ListUtils.removeAll(topics, notModifiedTopics); fillLastReadPostsForModifiedTopics(modifiedTopics, currentUser); } return topics; }
/** * Create dto * * @param post post for conversion * @return dto for post */ public static PostDto getDtoFor(Post post) { PostDto dto = new PostDto(); dto.setBodyText(post.getPostContent()); dto.setId(post.getId()); dto.setTopicId(post.getTopic().getId()); dto.setCreationDate(post.getCreationDate()); dto.setModificationDate(post.getModificationDate()); return dto; }
/** * Creates feed item with information about the post * * @param post post to add to the feed * @param componentUrl base url of the forum component * @return item for the RSS feed */ private Item createFeedItem(Post post, String componentUrl) { Item item = new Item(); Content content = new Content(); content.setType(Content.TEXT); content.setValue(post.getPostContent()); item.setContent(content); item.setTitle(post.getTopic().getTitle()); item.setAuthor(post.getUserCreated().getUsername()); item.setLink(componentUrl + "/posts/" + post.getId()); item.setPubDate(post.getCreationDate().toDate()); return item; }