Search in sources :

Example 96 with ServiceException

use of org.b3log.latke.service.ServiceException in project solo by b3log.

the class ArticleMgmtService method addArticle.

/**
     * Adds an article from the specified request json object.
     *
     * @param requestJSONObject the specified request json object, for example,
     * <pre>
     * {
     *     "article": {
     *         "articleAuthorEmail": "",
     *         "articleTitle": "",
     *         "articleAbstract": "",
     *         "articleContent": "",
     *         "articleTags": "tag1,tag2,tag3",
     *         "articleIsPublished": boolean,
     *         "articlePermalink": "", // optional
     *         "postToCommunity": boolean, // optional, default is true
     *         "articleSignId": "" // optional, default is "0",
     *         "articleCommentable": boolean,
     *         "articleViewPwd": "",
     *         "articleEditorType": "", // optional, preference specified if not exists this key
     *         "oId": "" // optional, generate it if not exists this key
     *     }
     * }
     * </pre>
     * @return generated article id
     * @throws ServiceException service exception
     */
public String addArticle(final JSONObject requestJSONObject) throws ServiceException {
    // TODO: add article args check
    final Transaction transaction = articleRepository.beginTransaction();
    try {
        final JSONObject article = requestJSONObject.getJSONObject(Article.ARTICLE);
        final String ret = addArticleInternal(article);
        transaction.commit();
        return ret;
    } catch (final Exception e) {
        if (transaction.isActive()) {
            transaction.rollback();
        }
        throw new ServiceException(e.getMessage());
    }
}
Also used : Transaction(org.b3log.latke.repository.Transaction) JSONObject(org.json.JSONObject) ServiceException(org.b3log.latke.service.ServiceException) EventException(org.b3log.latke.event.EventException) ServiceException(org.b3log.latke.service.ServiceException) JSONException(org.json.JSONException) ParseException(java.text.ParseException) RepositoryException(org.b3log.latke.repository.RepositoryException)

Example 97 with ServiceException

use of org.b3log.latke.service.ServiceException in project solo by b3log.

the class ArticleMgmtService method removeArticle.

/**
     * Removes the article specified by the given id.
     *
     * @param articleId the given id
     * @throws ServiceException service exception
     */
public void removeArticle(final String articleId) throws ServiceException {
    LOGGER.log(Level.DEBUG, "Removing an article[id={0}]", articleId);
    final Transaction transaction = articleRepository.beginTransaction();
    try {
        decTagRefCount(articleId);
        unArchiveDate(articleId);
        removeTagArticleRelations(articleId);
        removeArticleComments(articleId);
        final JSONObject article = articleRepository.get(articleId);
        articleRepository.remove(articleId);
        statisticMgmtService.decBlogArticleCount();
        if (article.getBoolean(Article.ARTICLE_IS_PUBLISHED)) {
            statisticMgmtService.decPublishedBlogArticleCount();
        }
        final JSONObject author = userRepository.getByEmail(article.optString(Article.ARTICLE_AUTHOR_EMAIL));
        author.put(UserExt.USER_PUBLISHED_ARTICLE_COUNT, author.optInt(UserExt.USER_PUBLISHED_ARTICLE_COUNT) - 1);
        author.put(UserExt.USER_ARTICLE_COUNT, author.optInt(UserExt.USER_ARTICLE_COUNT) - 1);
        userRepository.update(author.optString(Keys.OBJECT_ID), author);
        transaction.commit();
    } catch (final Exception e) {
        if (transaction.isActive()) {
            transaction.rollback();
        }
        LOGGER.log(Level.ERROR, "Removes an article[id=" + articleId + "] failed", e);
        throw new ServiceException(e);
    }
    LOGGER.log(Level.DEBUG, "Removed an article[id={0}]", articleId);
}
Also used : Transaction(org.b3log.latke.repository.Transaction) JSONObject(org.json.JSONObject) ServiceException(org.b3log.latke.service.ServiceException) EventException(org.b3log.latke.event.EventException) ServiceException(org.b3log.latke.service.ServiceException) JSONException(org.json.JSONException) ParseException(java.text.ParseException) RepositoryException(org.b3log.latke.repository.RepositoryException)

Example 98 with ServiceException

use of org.b3log.latke.service.ServiceException in project solo by b3log.

the class ArticleMgmtService method incViewCount.

/**
     * Increments the view count of the article specified by the given article id.
     * 
     * @param articleId the given article id
     * @throws ServiceException service exception
     */
public void incViewCount(final String articleId) throws ServiceException {
    JSONObject article;
    try {
        article = articleRepository.get(articleId);
        if (null == article) {
            return;
        }
    } catch (final RepositoryException e) {
        LOGGER.log(Level.ERROR, "Gets article [id=" + articleId + "] failed", e);
        return;
    }
    final Transaction transaction = articleRepository.beginTransaction();
    try {
        article.put(Article.ARTICLE_VIEW_COUNT, article.getInt(Article.ARTICLE_VIEW_COUNT) + 1);
        articleRepository.update(articleId, article);
        transaction.commit();
    } catch (final Exception e) {
        if (transaction.isActive()) {
            transaction.rollback();
        }
        LOGGER.log(Level.WARN, "Updates article view count failed");
        throw new ServiceException(e);
    }
}
Also used : JSONObject(org.json.JSONObject) Transaction(org.b3log.latke.repository.Transaction) ServiceException(org.b3log.latke.service.ServiceException) RepositoryException(org.b3log.latke.repository.RepositoryException) EventException(org.b3log.latke.event.EventException) ServiceException(org.b3log.latke.service.ServiceException) JSONException(org.json.JSONException) ParseException(java.text.ParseException) RepositoryException(org.b3log.latke.repository.RepositoryException)

Example 99 with ServiceException

use of org.b3log.latke.service.ServiceException in project solo by b3log.

the class MetaWeblogAPI method addArticle.

/**
     * Adds the specified article.
     *
     * @param article the specified article
     * @throws Exception exception
     */
private void addArticle(final JSONObject article) throws Exception {
    final Transaction transaction = articleRepository.beginTransaction();
    try {
        articleMgmtService.addArticleInternal(article);
        transaction.commit();
    } catch (final ServiceException e) {
        if (transaction.isActive()) {
            transaction.rollback();
        }
        throw e;
    }
}
Also used : Transaction(org.b3log.latke.repository.Transaction) ServiceException(org.b3log.latke.service.ServiceException)

Example 100 with ServiceException

use of org.b3log.latke.service.ServiceException in project solo by b3log.

the class ArticleReceiver method updateArticle.

/**
     * Updates an article with the specified request.
     * <p>
     * Renders the response with a json object, for example,
     * <pre>
     * {
     *     "sc": boolean,
     *     "msg": ""
     * }
     * </pre>
     * </p>
     *
     * @param request  the specified http servlet request, for example,
     *                 "article": {
     *                 "oId": "", // Symphony Article#clientArticleId
     *                 "articleTitle": "",
     *                 "articleContent": "",
     *                 "articleTags": "tag1,tag2,tag3",
     *                 "userB3Key": "",
     *                 "articleEditorType": ""
     *                 }
     * @param response the specified http servlet response
     * @param context  the specified http request context
     * @throws Exception exception
     */
@RequestProcessing(value = "/apis/symphony/article", method = HTTPRequestMethod.PUT)
public void updateArticle(final HttpServletRequest request, final HttpServletResponse response, final HTTPRequestContext context) throws Exception {
    final JSONRenderer renderer = new JSONRenderer();
    context.setRenderer(renderer);
    final JSONObject ret = new JSONObject();
    renderer.setJSONObject(ret);
    try {
        final JSONObject requestJSONObject = Requests.parseRequestJSONObject(request, response);
        final JSONObject article = requestJSONObject.optJSONObject(Article.ARTICLE);
        final String userB3Key = article.optString("userB3Key");
        final JSONObject preference = preferenceQueryService.getPreference();
        if (!userB3Key.equals(preference.optString(Option.ID_C_KEY_OF_SOLO))) {
            LOGGER.log(Level.WARN, "B3 key not match, ignored update article");
            return;
        }
        article.remove("userB3Key");
        final String articleId = article.getString(Keys.OBJECT_ID);
        if (null == articleQueryService.getArticleById(articleId)) {
            ret.put(Keys.MSG, "No found article[oId=" + articleId + "] to update");
            ret.put(Keys.STATUS_CODE, false);
            return;
        }
        final String articleContent = article.optString(Article.ARTICLE_CONTENT);
        final String plainTextContent = Jsoup.clean(Markdowns.toHTML(articleContent), Whitelist.none());
        if (plainTextContent.length() > ARTICLE_ABSTRACT_LENGTH) {
            article.put(Article.ARTICLE_ABSTRACT, plainTextContent.substring(0, ARTICLE_ABSTRACT_LENGTH) + "....");
        } else {
            article.put(Article.ARTICLE_ABSTRACT, plainTextContent);
        }
        article.put(Article.ARTICLE_IS_PUBLISHED, true);
        // Do not send to rhythm
        article.put(Common.POST_TO_COMMUNITY, false);
        article.put(Article.ARTICLE_COMMENTABLE, true);
        article.put(Article.ARTICLE_VIEW_PWD, "");
        String content = article.getString(Article.ARTICLE_CONTENT);
        //            content += "\n\n<p style='font-size: 12px;'><i>该文章同步自 <a href='https://hacpai.com/article/" + articleId
        //                + "' target='_blank'>黑客派</a></i></p>";
        article.put(Article.ARTICLE_CONTENT, content);
        articleMgmtService.updateArticle(requestJSONObject);
        ret.put(Keys.MSG, "update article succ");
        ret.put(Keys.STATUS_CODE, true);
    } catch (final ServiceException e) {
        LOGGER.log(Level.ERROR, e.getMessage(), e);
        final JSONObject jsonObject = QueryResults.defaultResult();
        renderer.setJSONObject(jsonObject);
        jsonObject.put(Keys.MSG, e.getMessage());
    }
}
Also used : JSONRenderer(org.b3log.latke.servlet.renderer.JSONRenderer) JSONObject(org.json.JSONObject) ServiceException(org.b3log.latke.service.ServiceException) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Aggregations

ServiceException (org.b3log.latke.service.ServiceException)102 JSONObject (org.json.JSONObject)91 JSONException (org.json.JSONException)45 RepositoryException (org.b3log.latke.repository.RepositoryException)34 Transaction (org.b3log.latke.repository.Transaction)31 RequestProcessing (org.b3log.latke.servlet.annotation.RequestProcessing)23 JSONArray (org.json.JSONArray)17 JSONRenderer (org.b3log.latke.servlet.renderer.JSONRenderer)16 EventException (org.b3log.latke.event.EventException)14 IOException (java.io.IOException)11 ParseException (java.text.ParseException)10 ArrayList (java.util.ArrayList)8 Query (org.b3log.latke.repository.Query)8 Date (java.util.Date)7 AbstractFreeMarkerRenderer (org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer)6 Template (freemarker.template.Template)3 FreeMarkerRenderer (org.b3log.latke.servlet.renderer.freemarker.FreeMarkerRenderer)3 ArchiveDate (org.b3log.solo.model.ArchiveDate)3 URL (java.net.URL)2 HashMap (java.util.HashMap)2