use of org.b3log.latke.service.ServiceException in project solo by b3log.
the class ArticleQueryService method getArticlesRandomly.
/**
* Gets a list of articles randomly with the specified fetch size.
*
* <p>
* <b>Note</b>: The article content and abstract is raw (no editor type processing).
* </p>
*
* @param fetchSize the specified fetch size
* @return a list of json objects, its size less or equal to the specified fetch size
* @throws ServiceException service exception
*/
public List<JSONObject> getArticlesRandomly(final int fetchSize) throws ServiceException {
try {
final List<JSONObject> ret = articleRepository.getRandomly(fetchSize);
removeUnusedProperties(ret);
return ret;
} catch (final RepositoryException e) {
LOGGER.log(Level.ERROR, "Gets articles randomly failed[fetchSize=" + fetchSize + "]", e);
throw new ServiceException(e);
}
}
use of org.b3log.latke.service.ServiceException in project solo by b3log.
the class ArticleQueryService method getArticle.
/**
* Gets an article by the specified article id.
*
* <p>
* <b>Note</b>: The article content and abstract is raw (no editor type processing).
* </p>
*
* @param articleId the specified article id
* @return for example, <pre>
* {
* "article": {
* "oId": "",
* "articleTitle": "",
* "articleAbstract": "",
* "articleContent": "",
* "articlePermalink": "",
* "articleHadBeenPublished": boolean,
* "articleCreateDate": java.util.Date,
* "articleTags": [{
* "oId": "",
* "tagTitle": ""
* }, ....],
* "articleSignId": "",
* "articleViewPwd": "",
* "articleEditorType": "",
* "signs": [{
* "oId": "",
* "signHTML": ""
* }, ....]
* }
* }
* </pre>, returns {@code null} if not found
*
* @throws ServiceException service exception
*/
public JSONObject getArticle(final String articleId) throws ServiceException {
try {
final JSONObject ret = new JSONObject();
final JSONObject article = articleRepository.get(articleId);
if (null == article) {
return null;
}
ret.put(ARTICLE, article);
// Tags
final JSONArray tags = new JSONArray();
final List<JSONObject> tagArticleRelations = tagArticleRepository.getByArticleId(articleId);
for (int i = 0; i < tagArticleRelations.size(); i++) {
final JSONObject tagArticleRelation = tagArticleRelations.get(i);
final String tagId = tagArticleRelation.getString(Tag.TAG + "_" + Keys.OBJECT_ID);
final JSONObject tag = tagRepository.get(tagId);
tags.put(tag);
}
article.put(ARTICLE_TAGS_REF, tags);
// Signs
final JSONObject preference = preferenceQueryService.getPreference();
article.put(Sign.SIGNS, new JSONArray(preference.getString(Option.ID_C_SIGNS)));
// Remove unused properties
article.remove(ARTICLE_AUTHOR_EMAIL);
article.remove(ARTICLE_COMMENT_COUNT);
article.remove(ARTICLE_IS_PUBLISHED);
article.remove(ARTICLE_PUT_TOP);
article.remove(ARTICLE_UPDATE_DATE);
article.remove(ARTICLE_VIEW_COUNT);
article.remove(ARTICLE_RANDOM_DOUBLE);
LOGGER.log(Level.DEBUG, "Got an article[id={0}]", articleId);
return ret;
} catch (final Exception e) {
LOGGER.log(Level.ERROR, "Gets an article failed", e);
throw new ServiceException(e);
}
}
use of org.b3log.latke.service.ServiceException in project solo by b3log.
the class ArticleQueryService method getRecentArticleTime.
/**
* Gets time of the recent updated article.
*
* @return time of the recent updated article, returns {@code 0} if not found
* @throws ServiceException service exception
*/
public long getRecentArticleTime() throws ServiceException {
try {
final List<JSONObject> recentArticles = articleRepository.getRecentArticles(1);
if (recentArticles.isEmpty()) {
return 0;
}
final JSONObject recentArticle = recentArticles.get(0);
return ((Date) recentArticle.get(Article.ARTICLE_UPDATE_DATE)).getTime();
} catch (final Exception e) {
LOGGER.log(Level.ERROR, e.getMessage(), e);
throw new ServiceException("Gets recent article time failed");
}
}
use of org.b3log.latke.service.ServiceException in project solo by b3log.
the class ArticleQueryService method getRelevantArticles.
/**
* Gets the relevant published articles of the specified article.
*
* <p>
* <b>Note</b>: The article content and abstract is raw (no editor type processing).
* </p>
*
* @param article the specified article
* @param preference the specified preference
* @return a list of articles, returns an empty list if not found
* @throws ServiceException service exception
*/
public List<JSONObject> getRelevantArticles(final JSONObject article, final JSONObject preference) throws ServiceException {
try {
final int displayCnt = preference.getInt(Option.ID_C_RELEVANT_ARTICLES_DISPLAY_CNT);
final String[] tagTitles = article.getString(Article.ARTICLE_TAGS_REF).split(",");
final int maxTagCnt = displayCnt > tagTitles.length ? tagTitles.length : displayCnt;
final String articleId = article.getString(Keys.OBJECT_ID);
final List<JSONObject> articles = new ArrayList<JSONObject>();
for (int i = 0; i < maxTagCnt; i++) {
// XXX: should average by tag?
final String tagTitle = tagTitles[i];
final JSONObject tag = tagRepository.getByTitle(tagTitle);
final String tagId = tag.getString(Keys.OBJECT_ID);
final JSONObject result = tagArticleRepository.getByTagId(tagId, 1, displayCnt);
final JSONArray tagArticleRelations = result.getJSONArray(Keys.RESULTS);
final int relationSize = displayCnt < tagArticleRelations.length() ? displayCnt : tagArticleRelations.length();
for (int j = 0; j < relationSize; j++) {
final JSONObject tagArticleRelation = tagArticleRelations.getJSONObject(j);
final String relatedArticleId = tagArticleRelation.getString(Article.ARTICLE + "_" + Keys.OBJECT_ID);
if (articleId.equals(relatedArticleId)) {
continue;
}
final JSONObject relevant = articleRepository.get(relatedArticleId);
if (!relevant.getBoolean(Article.ARTICLE_IS_PUBLISHED)) {
continue;
}
boolean existed = false;
for (final JSONObject relevantArticle : articles) {
if (relevantArticle.getString(Keys.OBJECT_ID).equals(relevant.getString(Keys.OBJECT_ID))) {
existed = true;
}
}
if (!existed) {
articles.add(relevant);
}
}
}
Collections.sort(articles, Comparators.ARTICLE_UPDATE_DATE_COMPARATOR);
removeUnusedProperties(articles);
if (displayCnt > articles.size()) {
return articles;
}
final List<Integer> randomIntegers = CollectionUtils.getRandomIntegers(0, articles.size() - 1, displayCnt);
final List<JSONObject> ret = new ArrayList<JSONObject>();
for (final int index : randomIntegers) {
ret.add(articles.get(index));
}
return ret;
} catch (final Exception e) {
LOGGER.log(Level.ERROR, "Gets relevant articles failed", e);
throw new ServiceException(e);
}
}
use of org.b3log.latke.service.ServiceException in project solo by b3log.
the class ArticleQueryService method getArticles.
/**
* Gets articles(by crate date descending) by the specified request json object.
*
* <p>
* If the property "articleIsPublished" of the specified request json object is {@code true}, the returned articles
* all are published, {@code false} otherwise.
* </p>
*
* <p>
* Specified the "excludes" for results properties exclusion.
* </p>
*
* @param requestJSONObject the specified request json object, for example, <pre>
* {
* "paginationCurrentPageNum": 1,
* "paginationPageSize": 20,
* "paginationWindowSize": 10,
* "articleIsPublished": boolean,
* "excludes": ["", ....] // Optional
* }, see {@link Pagination} for more details
* </pre>
*
* @return for example, <pre>
* {
* "pagination": {
* "paginationPageCount": 100,
* "paginationPageNums": [1, 2, 3, 4, 5]
* },
* "articles": [{
* "oId": "",
* "articleTitle": "",
* "articleCommentCount": int,
* "articleCreateTime"; long,
* "articleViewCount": int,
* "articleTags": "tag1, tag2, ....",
* "articlePutTop": boolean,
* "articleSignId": "",
* "articleViewPwd": "",
* "articleEditorType": "",
* .... // Specified by the "excludes"
* }, ....]
* }
* </pre>, order by article update date and sticky(put top).
*
* @throws ServiceException service exception
* @see Pagination
*/
public JSONObject getArticles(final JSONObject requestJSONObject) throws ServiceException {
final JSONObject ret = new JSONObject();
try {
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 boolean articleIsPublished = requestJSONObject.optBoolean(ARTICLE_IS_PUBLISHED, true);
final Query query = new Query().setCurrentPageNum(currentPageNum).setPageSize(pageSize).addSort(ARTICLE_PUT_TOP, SortDirection.DESCENDING).addSort(ARTICLE_CREATE_DATE, SortDirection.DESCENDING).setFilter(new PropertyFilter(ARTICLE_IS_PUBLISHED, FilterOperator.EQUAL, articleIsPublished));
int articleCount = statisticQueryService.getBlogArticleCount();
if (!articleIsPublished) {
articleCount -= statisticQueryService.getPublishedBlogArticleCount();
} else {
articleCount = statisticQueryService.getPublishedBlogArticleCount();
}
final int pageCount = (int) Math.ceil((double) articleCount / (double) pageSize);
query.setPageCount(pageCount);
final JSONObject result = articleRepository.get(query);
final JSONObject pagination = new JSONObject();
ret.put(Pagination.PAGINATION, pagination);
final List<Integer> pageNums = Paginator.paginate(currentPageNum, pageSize, pageCount, windowSize);
pagination.put(Pagination.PAGINATION_PAGE_COUNT, pageCount);
pagination.put(Pagination.PAGINATION_PAGE_NUMS, pageNums);
final JSONArray articles = result.getJSONArray(Keys.RESULTS);
JSONArray excludes = requestJSONObject.optJSONArray(Keys.EXCLUDES);
excludes = null == excludes ? new JSONArray() : excludes;
for (int i = 0; i < articles.length(); i++) {
final JSONObject article = articles.getJSONObject(i);
final JSONObject author = getAuthor(article);
final String authorName = author.getString(User.USER_NAME);
article.put(Common.AUTHOR_NAME, authorName);
article.put(ARTICLE_CREATE_TIME, ((Date) article.get(ARTICLE_CREATE_DATE)).getTime());
article.put(ARTICLE_UPDATE_TIME, ((Date) article.get(ARTICLE_UPDATE_DATE)).getTime());
// Remove unused properties
for (int j = 0; j < excludes.length(); j++) {
article.remove(excludes.optString(j));
}
}
ret.put(ARTICLES, articles);
return ret;
} catch (final Exception e) {
LOGGER.log(Level.ERROR, "Gets articles failed", e);
throw new ServiceException(e);
}
}
Aggregations