use of org.b3log.latke.repository.Query in project solo by b3log.
the class ArticleRepositoryImpl method getRecentArticles.
@Override
public List<JSONObject> getRecentArticles(final int fetchSize) throws RepositoryException {
final Query query = new Query();
query.setFilter(new PropertyFilter(Article.ARTICLE_IS_PUBLISHED, FilterOperator.EQUAL, true));
query.addSort(Article.ARTICLE_UPDATE_DATE, SortDirection.DESCENDING);
query.setCurrentPageNum(1);
query.setPageSize(fetchSize);
query.setPageCount(1);
final JSONObject result = get(query);
final JSONArray array = result.optJSONArray(Keys.RESULTS);
return CollectionUtils.jsonArrayToList(array);
}
use of org.b3log.latke.repository.Query in project solo by b3log.
the class CommentRepositoryImpl method getRecentComments.
@Override
@SuppressWarnings("unchecked")
public List<JSONObject> getRecentComments(final int num) throws RepositoryException {
final Query query = new Query().addSort(Keys.OBJECT_ID, SortDirection.DESCENDING).setCurrentPageNum(1).setPageSize(num).setPageCount(1);
List<JSONObject> ret;
final JSONObject result = get(query);
final JSONArray array = result.optJSONArray(Keys.RESULTS);
ret = CollectionUtils.jsonArrayToList(array);
// Removes unpublished article related comments
removeForUnpublishedArticles(ret);
return ret;
}
use of org.b3log.latke.repository.Query in project solo by b3log.
the class LinkRepositoryImpl method getMaxOrder.
@Override
public int getMaxOrder() throws RepositoryException {
final Query query = new Query();
query.addSort(Link.LINK_ORDER, SortDirection.DESCENDING);
final JSONObject result = get(query);
final JSONArray array = result.optJSONArray(Keys.RESULTS);
if (0 == array.length()) {
return -1;
}
return array.optJSONObject(0).optInt(Link.LINK_ORDER);
}
use of org.b3log.latke.repository.Query in project solo by b3log.
the class LinkRepositoryImpl method getByAddress.
@Override
public JSONObject getByAddress(final String address) throws RepositoryException {
final Query query = new Query().setFilter(new PropertyFilter(Link.LINK_ADDRESS, FilterOperator.EQUAL, address)).setPageCount(1);
final JSONObject result = get(query);
final JSONArray array = result.optJSONArray(Keys.RESULTS);
if (0 == array.length()) {
return null;
}
return array.optJSONObject(0);
}
use of org.b3log.latke.repository.Query in project solo by b3log.
the class CommentQueryService method getComments.
/**
* Gets comments with the specified request json object, request and response.
*
* @param requestJSONObject the specified request json object, for example, <pre>
* {
* "paginationCurrentPageNum": 1,
* "paginationPageSize": 20,
* "paginationWindowSize": 10
* }, see {@link Pagination} for more details
* </pre>
*
* @return for example, <pre>
* {
* "comments": [{
* "oId": "",
* "commentTitle": "",
* "commentName": "",
* "commentEmail": "",
* "thumbnailUrl": "",
* "commentURL": "",
* "commentContent": "",
* "commentTime": long,
* "commentSharpURL": ""
* }, ....]
* "sc": "GET_COMMENTS_SUCC"
* }
* </pre>
*
* @throws ServiceException service exception
* @see Pagination
*/
public JSONObject getComments(final JSONObject requestJSONObject) throws ServiceException {
try {
final JSONObject ret = new JSONObject();
final int currentPageNum = requestJSONObject.getInt(Pagination.PAGINATION_CURRENT_PAGE_NUM);
final int pageSize = requestJSONObject.getInt(Pagination.PAGINATION_PAGE_SIZE);
final int windowSize = requestJSONObject.getInt(Pagination.PAGINATION_WINDOW_SIZE);
final Query query = new Query().setCurrentPageNum(currentPageNum).setPageSize(pageSize).addSort(Comment.COMMENT_DATE, SortDirection.DESCENDING);
final JSONObject result = commentRepository.get(query);
final JSONArray comments = result.getJSONArray(Keys.RESULTS);
// Sets comment title and content escaping
for (int i = 0; i < comments.length(); i++) {
final JSONObject comment = comments.getJSONObject(i);
String title;
final String onType = comment.getString(Comment.COMMENT_ON_TYPE);
final String onId = comment.getString(Comment.COMMENT_ON_ID);
if (Article.ARTICLE.equals(onType)) {
final JSONObject article = articleRepository.get(onId);
title = article.getString(Article.ARTICLE_TITLE);
comment.put(Common.TYPE, Common.ARTICLE_COMMENT_TYPE);
} else {
// It's a comment of page
final JSONObject page = pageRepository.get(onId);
title = page.getString(Page.PAGE_TITLE);
comment.put(Common.TYPE, Common.PAGE_COMMENT_TYPE);
}
comment.put(Common.COMMENT_TITLE, title);
String commentContent = comment.optString(Comment.COMMENT_CONTENT);
commentContent = Emotions.convert(commentContent);
commentContent = Markdowns.toHTML(commentContent);
comment.put(Comment.COMMENT_CONTENT, commentContent);
comment.put(Comment.COMMENT_TIME, ((Date) comment.get(Comment.COMMENT_DATE)).getTime());
comment.remove(Comment.COMMENT_DATE);
}
final int pageCount = result.getJSONObject(Pagination.PAGINATION).getInt(Pagination.PAGINATION_PAGE_COUNT);
final JSONObject pagination = new JSONObject();
final List<Integer> pageNums = Paginator.paginate(currentPageNum, pageSize, pageCount, windowSize);
pagination.put(Pagination.PAGINATION_PAGE_COUNT, pageCount);
pagination.put(Pagination.PAGINATION_PAGE_NUMS, pageNums);
ret.put(Comment.COMMENTS, comments);
ret.put(Pagination.PAGINATION, pagination);
return ret;
} catch (final Exception e) {
LOGGER.log(Level.ERROR, "Gets comments failed", e);
throw new ServiceException(e);
}
}
Aggregations