* @throws JSONException json exception
*/
@RequestProcessing(value = "/authors/**", method = HTTPRequestMethod.GET)
public void showAuthorArticles(final HTTPRequestContext context, final HttpServletRequest request, final HttpServletResponse response)
throws IOException, JSONException {
final AbstractFreeMarkerRenderer renderer = new FreeMarkerRenderer();
context.setRenderer(renderer);
renderer.setTemplateName("author-articles.ftl");
try {
String requestURI = request.getRequestURI();
if (!requestURI.endsWith("/")) {
requestURI += "/";
}
final String authorId = getAuthorId(requestURI);
LOGGER.log(Level.DEBUG, "Request author articles[requestURI={0}, authorId={1}]", new Object[] {requestURI, authorId});
final int currentPageNum = getAuthorCurrentPageNum(requestURI, authorId);
if (-1 == currentPageNum) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
LOGGER.log(Level.DEBUG, "Request author articles[authorId={0}, currentPageNum={1}]", new Object[] {authorId, currentPageNum});
final JSONObject preference = preferenceQueryService.getPreference();
if (null == preference) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
final int pageSize = preference.getInt(Preference.ARTICLE_LIST_DISPLAY_COUNT);
final int windowSize = preference.getInt(Preference.ARTICLE_LIST_PAGINATION_WINDOW_SIZE);
final JSONObject result = userQueryService.getUser(authorId);
if (null == result) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
final JSONObject author = result.getJSONObject(User.USER);
final String authorEmail = author.getString(User.USER_EMAIL);
final List<JSONObject> articles = articleQueryService.getArticlesByAuthorEmail(authorEmail, currentPageNum, pageSize);
if (articles.isEmpty()) {
try {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
return;
} catch (final IOException ex) {
LOGGER.error(ex.getMessage());
}
}
filler.setArticlesExProperties(articles, author, preference);
if (preference.optBoolean(Preference.ENABLE_ARTICLE_UPDATE_HINT)) {
Collections.sort(articles, Comparators.ARTICLE_UPDATE_DATE_COMPARATOR);
} else {
Collections.sort(articles, Comparators.ARTICLE_CREATE_DATE_COMPARATOR);
}
final int articleCount = author.getInt(UserExt.USER_PUBLISHED_ARTICLE_COUNT);
final int pageCount = (int) Math.ceil((double) articleCount / (double) pageSize);
final List<Integer> pageNums = Paginator.paginate(currentPageNum, pageSize, pageCount, windowSize);
final Map<String, Object> dataModel = renderer.getDataModel();
prepareShowAuthorArticles(pageNums, dataModel, pageCount, currentPageNum, articles, author);
filler.fillBlogHeader(request, response, dataModel, preference);
filler.fillBlogFooter(request, dataModel, preference);
filler.fillSide(request, dataModel, preference);