use of org.b3log.latke.servlet.renderer.JSONRenderer in project solo by b3log.
the class ArticleProcessor method getAuthorsArticlesByPage.
/**
* Gets author articles paged with the specified context.
*
* @param context the specified context
* @param request the specified request
*/
@RequestProcessing(value = "/articles/authors/\\d+/\\d+", uriPatternsMode = URIPatternMode.REGEX, method = HTTPRequestMethod.GET)
public void getAuthorsArticlesByPage(final HTTPRequestContext context, final HttpServletRequest request) {
final JSONObject jsonObject = new JSONObject();
final String authorId = getAuthorsArticlesPagedAuthorId(request.getRequestURI());
final int currentPageNum = getAuthorsArticlesPagedCurrentPageNum(request.getRequestURI());
Stopwatchs.start("Get Author-Articles Paged[authorId=" + authorId + ", pageNum=" + currentPageNum + ']');
try {
jsonObject.put(Keys.STATUS_CODE, true);
final JSONObject preference = preferenceQueryService.getPreference();
final int pageSize = preference.getInt(Option.ID_C_ARTICLE_LIST_DISPLAY_COUNT);
final JSONObject authorRet = userQueryService.getUser(authorId);
if (null == authorRet) {
context.getResponse().sendError(HttpServletResponse.SC_NOT_FOUND);
return;
}
final JSONObject author = authorRet.getJSONObject(User.USER);
final String authorEmail = author.optString(User.USER_EMAIL);
final List<JSONObject> articles = articleQueryService.getArticlesByAuthorEmail(authorEmail, currentPageNum, pageSize);
if (!articles.isEmpty()) {
filler.setArticlesExProperties(request, articles, author, preference);
}
final int articleCount = author.getInt(UserExt.USER_PUBLISHED_ARTICLE_COUNT);
final int pageCount = (int) Math.ceil((double) articleCount / (double) pageSize);
final JSONObject result = new JSONObject();
final JSONObject pagination = new JSONObject();
pagination.put(Pagination.PAGINATION_PAGE_COUNT, pageCount);
result.put(Pagination.PAGINATION, pagination);
result.put(Article.ARTICLES, articles);
jsonObject.put(Keys.RESULTS, result);
} catch (final Exception e) {
jsonObject.put(Keys.STATUS_CODE, false);
LOGGER.log(Level.ERROR, "Gets article paged failed", e);
} finally {
Stopwatchs.end();
}
final JSONRenderer renderer = new JSONRenderer();
context.setRenderer(renderer);
renderer.setJSONObject(jsonObject);
}
use of org.b3log.latke.servlet.renderer.JSONRenderer in project solo by b3log.
the class ArticleProcessor method getTagArticlesByPage.
/**
* Gets tag articles paged with the specified context.
*
* @param context the specified context
* @param request the specified request
*/
@RequestProcessing(value = "/articles/tags/.+/\\d+", uriPatternsMode = URIPatternMode.REGEX, method = HTTPRequestMethod.GET)
public void getTagArticlesByPage(final HTTPRequestContext context, final HttpServletRequest request) {
final JSONObject jsonObject = new JSONObject();
String tagTitle = getTagArticlesPagedTag(request.getRequestURI());
try {
tagTitle = URLDecoder.decode(tagTitle, "UTF-8");
} catch (final UnsupportedEncodingException e) {
LOGGER.log(Level.ERROR, "Gets tag title failed[requestURI=" + request.getRequestURI() + ']', e);
tagTitle = "";
}
final int currentPageNum = getTagArticlesPagedCurrentPageNum(request.getRequestURI());
Stopwatchs.start("Get Tag-Articles Paged[tagTitle=" + tagTitle + ", pageNum=" + currentPageNum + ']');
try {
jsonObject.put(Keys.STATUS_CODE, true);
final JSONObject preference = preferenceQueryService.getPreference();
final int pageSize = preference.getInt(Option.ID_C_ARTICLE_LIST_DISPLAY_COUNT);
final JSONObject tagQueryResult = tagQueryService.getTagByTitle(tagTitle);
if (null == tagQueryResult) {
throw new Exception("Can not foud tag[title=" + tagTitle + "]");
}
final JSONObject tag = tagQueryResult.getJSONObject(Tag.TAG);
final String tagId = tag.getString(Keys.OBJECT_ID);
final List<JSONObject> articles = articleQueryService.getArticlesByTag(tagId, currentPageNum, pageSize);
final int tagArticleCount = tag.getInt(Tag.TAG_PUBLISHED_REFERENCE_COUNT);
final int pageCount = (int) Math.ceil((double) tagArticleCount / (double) pageSize);
final boolean hasMultipleUsers = userQueryService.hasMultipleUsers();
if (hasMultipleUsers) {
filler.setArticlesExProperties(request, articles, preference);
} else if (!articles.isEmpty()) {
final JSONObject author = articleQueryService.getAuthor(articles.get(0));
filler.setArticlesExProperties(request, articles, author, preference);
}
Collections.sort(articles, Comparators.ARTICLE_CREATE_DATE_COMPARATOR);
final JSONObject result = new JSONObject();
final JSONObject pagination = new JSONObject();
pagination.put(Pagination.PAGINATION_PAGE_COUNT, pageCount);
result.put(Pagination.PAGINATION, pagination);
result.put(Article.ARTICLES, articles);
jsonObject.put(Keys.RESULTS, result);
} catch (final Exception e) {
jsonObject.put(Keys.STATUS_CODE, false);
LOGGER.log(Level.ERROR, "Gets article paged failed", e);
} finally {
Stopwatchs.end();
}
final JSONRenderer renderer = new JSONRenderer();
context.setRenderer(renderer);
renderer.setJSONObject(jsonObject);
}
Aggregations