Search in sources :

Example 11 with RepositoryException

use of org.b3log.latke.repository.RepositoryException in project solo by b3log.

the class TagQueryService method getTagByTitle.

/**
     * Gets a tag by the specified tag title.
     *
     * @param tagTitle the specified tag title
     * @return for example,      <pre>
     * {
     *     "tag": {
     *         "oId": "",
     *         "tagTitle": "",
     *         "tagReferenceCount": int,
     *         "tagPublishedRefCount": int
     *     }
     * }
     * </pre>, returns {@code null} if not found
     *
     * @throws ServiceException service exception
     */
public JSONObject getTagByTitle(final String tagTitle) throws ServiceException {
    try {
        final JSONObject ret = new JSONObject();
        final JSONObject tag = tagRepository.getByTitle(tagTitle);
        if (null == tag) {
            return null;
        }
        ret.put(Tag.TAG, tag);
        LOGGER.log(Level.DEBUG, "Got an tag[title={0}]", tagTitle);
        return ret;
    } catch (final RepositoryException e) {
        LOGGER.log(Level.ERROR, "Gets an article failed", e);
        throw new ServiceException(e);
    }
}
Also used : JSONObject(org.json.JSONObject) ServiceException(org.b3log.latke.service.ServiceException) RepositoryException(org.b3log.latke.repository.RepositoryException)

Example 12 with RepositoryException

use of org.b3log.latke.repository.RepositoryException in project solo by b3log.

the class TagQueryService method getTags.

/**
     * Gets all tags.
     *
     * @return for example,      <pre>
     * [
     *     {"tagTitle": "", "tagReferenceCount": int, ....},
     *     ....
     * ]
     * </pre>, returns an empty list if not found
     *
     * @throws ServiceException service exception
     */
public List<JSONObject> getTags() throws ServiceException {
    try {
        final Query query = new Query().setPageCount(1);
        final JSONObject result = tagRepository.get(query);
        final JSONArray tagArray = result.optJSONArray(Keys.RESULTS);
        return CollectionUtils.jsonArrayToList(tagArray);
    } catch (final RepositoryException e) {
        LOGGER.log(Level.ERROR, "Gets tags failed", e);
        throw new ServiceException(e);
    }
}
Also used : Query(org.b3log.latke.repository.Query) JSONObject(org.json.JSONObject) ServiceException(org.b3log.latke.service.ServiceException) JSONArray(org.json.JSONArray) RepositoryException(org.b3log.latke.repository.RepositoryException)

Example 13 with RepositoryException

use of org.b3log.latke.repository.RepositoryException in project solo by b3log.

the class TagQueryService method getTopTags.

/**
     * Gets top (reference count descending) tags.
     *
     * @param fetchSize the specified fetch size
     * @return for example,      <pre>
     * [
     *     {"tagTitle": "", "tagReferenceCount": int, ....},
     *     ....
     * ]
     * </pre>, returns an empty list if not found
     *
     * @throws ServiceException service exception
     */
public List<JSONObject> getTopTags(final int fetchSize) throws ServiceException {
    try {
        final Query query = new Query().setPageCount(1).setPageSize(fetchSize).addSort(Tag.TAG_PUBLISHED_REFERENCE_COUNT, SortDirection.DESCENDING);
        final JSONObject result = tagRepository.get(query);
        final JSONArray tagArray = result.optJSONArray(Keys.RESULTS);
        return CollectionUtils.jsonArrayToList(tagArray);
    } catch (final RepositoryException e) {
        LOGGER.log(Level.ERROR, "Gets top tags failed", e);
        throw new ServiceException(e);
    }
}
Also used : Query(org.b3log.latke.repository.Query) JSONObject(org.json.JSONObject) ServiceException(org.b3log.latke.service.ServiceException) JSONArray(org.json.JSONArray) RepositoryException(org.b3log.latke.repository.RepositoryException)

Example 14 with RepositoryException

use of org.b3log.latke.repository.RepositoryException in project solo by b3log.

the class ArchiveDateRepositoryImpl method getByArchiveDate.

@Override
public JSONObject getByArchiveDate(final String archiveDate) throws RepositoryException {
    long time = 0L;
    try {
        time = DateUtils.parseDate(archiveDate, new String[] { "yyyy/MM" }).getTime();
    } catch (final ParseException e) {
        LOGGER.log(Level.ERROR, "Can not parse archive date [" + archiveDate + "]", e);
        throw new RepositoryException("Can not parse archive date [" + archiveDate + "]");
    }
    LOGGER.log(Level.TRACE, "Archive date [{0}] parsed to time [{1}]", new Object[] { archiveDate, time });
    final Query query = new Query();
    query.setFilter(new PropertyFilter(ArchiveDate.ARCHIVE_TIME, FilterOperator.EQUAL, time)).setPageCount(1);
    final JSONObject result = get(query);
    final JSONArray array = result.optJSONArray(Keys.RESULTS);
    if (0 == array.length()) {
        return null;
    }
    return array.optJSONObject(0);
}
Also used : Query(org.b3log.latke.repository.Query) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) RepositoryException(org.b3log.latke.repository.RepositoryException) PropertyFilter(org.b3log.latke.repository.PropertyFilter) ParseException(java.text.ParseException)

Example 15 with RepositoryException

use of org.b3log.latke.repository.RepositoryException in project solo by b3log.

the class ArticleRepositoryImpl method getNextArticle.

@Override
public JSONObject getNextArticle(final String articleId) throws RepositoryException {
    final JSONObject currentArticle = get(articleId);
    final Date currentArticleCreateDate = (Date) currentArticle.opt(Article.ARTICLE_CREATE_DATE);
    final Query query = new Query().setFilter(CompositeFilterOperator.and(new PropertyFilter(Article.ARTICLE_CREATE_DATE, FilterOperator.GREATER_THAN, currentArticleCreateDate), new PropertyFilter(Article.ARTICLE_IS_PUBLISHED, FilterOperator.EQUAL, true))).addSort(Article.ARTICLE_CREATE_DATE, SortDirection.ASCENDING).setCurrentPageNum(1).setPageSize(1).setPageCount(1).addProjection(Article.ARTICLE_TITLE, String.class).addProjection(Article.ARTICLE_PERMALINK, String.class).addProjection(Article.ARTICLE_ABSTRACT, String.class);
    final JSONObject result = get(query);
    final JSONArray array = result.optJSONArray(Keys.RESULTS);
    if (1 != array.length()) {
        return null;
    }
    final JSONObject ret = new JSONObject();
    final JSONObject article = array.optJSONObject(0);
    try {
        ret.put(Article.ARTICLE_TITLE, article.getString(Article.ARTICLE_TITLE));
        ret.put(Article.ARTICLE_PERMALINK, article.getString(Article.ARTICLE_PERMALINK));
        ret.put(Article.ARTICLE_ABSTRACT, article.getString((Article.ARTICLE_ABSTRACT)));
    } catch (final JSONException e) {
        throw new RepositoryException(e);
    }
    return ret;
}
Also used : JSONObject(org.json.JSONObject) Query(org.b3log.latke.repository.Query) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) PropertyFilter(org.b3log.latke.repository.PropertyFilter) RepositoryException(org.b3log.latke.repository.RepositoryException) Date(java.util.Date)

Aggregations

RepositoryException (org.b3log.latke.repository.RepositoryException)35 JSONObject (org.json.JSONObject)32 ServiceException (org.b3log.latke.service.ServiceException)14 JSONArray (org.json.JSONArray)10 Query (org.b3log.latke.repository.Query)8 Transaction (org.b3log.latke.repository.Transaction)8 Date (java.util.Date)5 ParseException (java.text.ParseException)4 PropertyFilter (org.b3log.latke.repository.PropertyFilter)4 JSONException (org.json.JSONException)4 EventException (org.b3log.latke.event.EventException)2 ArchiveDate (org.b3log.solo.model.ArchiveDate)2 IOException (java.io.IOException)1 ServletException (javax.servlet.ServletException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 LatkeBeanManager (org.b3log.latke.ioc.LatkeBeanManager)1 GeneralUser (org.b3log.latke.user.GeneralUser)1 ArticleRepository (org.b3log.solo.repository.ArticleRepository)1 PageRepository (org.b3log.solo.repository.PageRepository)1