/** * Returns a sum of all topic's post count for that branch. * <p/> * Value is computed only for the first time (if not set explicitly before), * so it may not take into account the posts added later * * @return sum of post count for all the topics in this branch */ public int getPostCount() { if (postsCount == null) { postsCount = Lambda.sumFrom(topics, Topic.class).getPostCount(); } return postsCount; }
/** * 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); }
/** * Check if user can answer for question with given id. Yer can answer if question has less than * {@link #LIMIT_OF_POSTS_VALUE} answers * * @param questionId id of question to be checked * * @return Success response in JSON form if user can leave answer and fail response in JSON form otherwise * @throws NotFoundException if question with given id not found */ @RequestMapping(value = "{id}/canpost", method = RequestMethod.GET) @ResponseBody public JsonResponse canPost(@PathVariable("id") Long questionId) throws NotFoundException { Topic topic = getTypeAwarePluginTopicService().get(questionId, QuestionsAndAnswersPlugin.TOPIC_TYPE); if (topic.getPostCount() - 1 >= LIMIT_OF_POSTS_VALUE) { return new JsonResponse(JsonResponseStatus.FAIL); } return new JsonResponse(JsonResponseStatus.SUCCESS); }
Topic topic = getTypeAwarePluginTopicService().get(questionId, QuestionsAndAnswersPlugin.TOPIC_TYPE); if (result.hasErrors() || LIMIT_OF_POSTS_VALUE <= topic.getPostCount() - 1) { JCUser currentUser = getUserReader().getCurrentUser(); PostDraft draft = topic.getDraftForUser(currentUser);