/** * Used to find out the current post index on JSP page. * We can't invoke a method there so use an explicit getter. * * @return index of this post ina topic, starting from 0 */ public int getPostIndexInTopic(){ return topic.getPosts().indexOf(this); }
/** * Create dto from {@link Topic} * * @param topic topic for conversion */ public TopicDto(Topic topic) { this.topic = topic; if (!topic.getPosts().isEmpty()) { bodyText = topic.getBodyText(); } }
/** * Computes new last read post on the page based on the topic size and * current pagination settings. * * @param user user to calculate post for * @param topic topic to calculate post for * @param pageNum page number co calculate last post seen by the user * @return last post on the page */ private Post calculatePostOnPage(JCUser user, Topic topic, int pageNum) { int maxPostIndex = user.getPageSize() * pageNum - 1; maxPostIndex = Math.min(topic.getPostCount() - 1, maxPostIndex); return topic.getPosts().get(maxPostIndex); }
/** * 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(); }
public int getUserPostCount(JCUser user) { int count = 0; for (Post post : getPosts()) { if (post.getUserCreated().equals(user)) { count ++; } } return count; }
/** * Checks if this topic contains only posts added by topic Owner. * * @return <code>true</code> if condition is followed, otherwise <code>false</code> */ public boolean isContainsOwnerPostsOnly() { for (Post post : getPosts()) { if (!post.getUserCreated().equals(topicStarter)) { return false; } } return true; }
/** * {@inheritDoc} */ @Override public int calculatePageForPost(Post post) { Topic topic = post.getTopic(); int index = topic.getPosts().indexOf(post) + 1; int pageSize = userService.getCurrentUser().getPageSize(); int pageNum = index / pageSize; if (index % pageSize == 0) { return pageNum; } else { return pageNum + 1; } }
/** * Get the date of the last modification of posts in the current topic. */ public DateTime getLastModificationPostDate() { DateTime newTopicModificationDate = getFirstPost().getLastTouchedDate(); for (Post post : getPosts()) { if (post.getLastTouchedDate().isAfter(newTopicModificationDate.toInstant())) { newTopicModificationDate = post.getLastTouchedDate(); } } return newTopicModificationDate; }
/** * 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; }
data.put(POST_PAGE, new PageImpl<>(getSortedPosts(topic.getPosts()))); data.put(BREADCRUMB_LIST, breadcrumbBuilder.getForumBreadcrumb(topic)); data.put(SUBSCRIBED, false);
/** * {@inheritDoc} */ @PreAuthorize("hasPermission(#topic.branch.id, 'BRANCH', 'BranchPermission.MOVE_TOPICS')") @Override public void moveTopic(Topic topic, Long branchId) throws NotFoundException { Branch sourceBranch = topic.getBranch(); Branch targetBranch = branchDao.get(branchId); if (targetBranch == null) { throw new NotFoundException("Target branch not exist"); } targetBranch.addTopic(topic); branchDao.saveOrUpdate(targetBranch); List<Post> topicPosts = topic.getPosts(); if (topicPosts.contains(sourceBranch.getLastPost())) { branchLastPostService.refreshLastPostInBranch(sourceBranch); } branchLastPostService.refreshLastPostInBranch(targetBranch); notificationService.sendNotificationAboutTopicMoved(topic); logger.info("Moved topic \"{}\". Topic id: {}", topic.getTitle(), topic.getId()); }
engine.init(); data.put(QUESTION, topic); data.put(POST_PAGE, new PageImpl<>(getSortedPosts(topic.getPosts()))); data.put(BREADCRUMB_LIST, breadcrumbBuilder.getForumBreadcrumb(topic)); data.put(SUBSCRIBED, false);
/** * Performs update with security checking. * * @param post an instance of post, that will be updated * @param postContent new content of the post * @throws AccessDeniedException if user tries to update the first post of code review which should be impossible, * see <a href="http://jtalks.org/display/jcommune/1.1+Larks">Requirements</a> * for details */ @PreAuthorize("(hasPermission(#post.id, 'POST', 'GeneralPermission.WRITE') and " + "hasPermission(#post.topic.branch.id, 'BRANCH', 'BranchPermission.EDIT_OWN_POSTS')) or " + "(not hasPermission(#post.id, 'POST', 'GeneralPermission.WRITE') and " + "hasPermission(#post.topic.branch.id, 'BRANCH', 'BranchPermission.EDIT_OTHERS_POSTS'))") @Override public void updatePost(Post post, String postContent) { Topic postTopic = post.getTopic(); if (postTopic.isCodeReview() && postTopic.getPosts().get(0).getId() == post.getId()) { throw new AccessDeniedException("It is impossible to edit code review!"); } post.setPostContent(postContent); post.updateModificationDate(); this.getDao().saveOrUpdate(post); userService.notifyAndMarkNewlyMentionedUsers(post); logger.debug("Post id={} updated.", post.getId()); }
/** * Performs actual topic deletion. Deletes all topic related data and * recalculates user's post count. * * @param topic topic to delete * @return branch without deleted topic */ private Branch deleteTopicSilent(Topic topic) { List<Post> topicPosts = topic.getPosts(); for (Post post : topicPosts) { JCUser user = post.getUserCreated(); user.setPostCount(user.getPostCount() - 1); } Branch branch = topic.getBranch(); Post lastPostInBranch = branch.getLastPost(); boolean branchLastPostFromDeletedTopic = topicPosts.contains(lastPostInBranch); if (branchLastPostFromDeletedTopic) { branch.clearLastPost(); } branch.deleteTopic(topic); branchDao.saveOrUpdate(branch); if (branchLastPostFromDeletedTopic) { branchLastPostService.refreshLastPostInBranch(branch); } securityService.deleteFromAcl(Topic.class, topic.getId()); return branch; }