Search in sources :

Example 41 with Transaction

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

the class ArticleMgmtService method topArticle.

/**
     * Puts an article specified by the given article id to top or cancel top.
     *
     * @param articleId the given article id
     * @param top the specified flag, {@code true} to top, {@code false} to
     * cancel top
     * @throws ServiceException service exception
     */
public void topArticle(final String articleId, final boolean top) throws ServiceException {
    final Transaction transaction = articleRepository.beginTransaction();
    try {
        final JSONObject topArticle = articleRepository.get(articleId);
        topArticle.put(ARTICLE_PUT_TOP, top);
        articleRepository.update(articleId, topArticle);
        transaction.commit();
    } catch (final Exception e) {
        if (transaction.isActive()) {
            transaction.rollback();
        }
        LOGGER.log(Level.ERROR, "Can't put the article[oId{0}] to top", articleId);
        throw new ServiceException(e);
    }
}
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 42 with Transaction

use of org.b3log.latke.repository.Transaction 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 43 with Transaction

use of org.b3log.latke.repository.Transaction 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 44 with Transaction

use of org.b3log.latke.repository.Transaction 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 45 with Transaction

use of org.b3log.latke.repository.Transaction 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)

Aggregations

Transaction (org.b3log.latke.repository.Transaction)57 JSONObject (org.json.JSONObject)49 ServiceException (org.b3log.latke.service.ServiceException)33 RepositoryException (org.b3log.latke.repository.RepositoryException)23 JSONException (org.json.JSONException)21 Test (org.testng.annotations.Test)16 Date (java.util.Date)9 ParseException (java.text.ParseException)8 EventException (org.b3log.latke.event.EventException)8 IOException (java.io.IOException)6 ArticleRepository (org.b3log.solo.repository.ArticleRepository)4 JSONArray (org.json.JSONArray)4 AbstractPlugin (org.b3log.latke.plugin.AbstractPlugin)3 PageRepository (org.b3log.solo.repository.PageRepository)3 URL (java.net.URL)2 Query (org.b3log.latke.repository.Query)2 OptionRepository (org.b3log.solo.repository.OptionRepository)2 TagArticleRepository (org.b3log.solo.repository.TagArticleRepository)2 TagRepository (org.b3log.solo.repository.TagRepository)2 MalformedURLException (java.net.MalformedURLException)1