Search in sources :

Example 11 with Transaction

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

the class ArticleMgmtService method cancelPublishArticle.

/**
     * Cancels publish an article by the specified article id.
     *
     * @param articleId the specified article id
     * @throws ServiceException service exception
     */
public void cancelPublishArticle(final String articleId) throws ServiceException {
    final Transaction transaction = articleRepository.beginTransaction();
    try {
        final JSONObject article = articleRepository.get(articleId);
        article.put(ARTICLE_IS_PUBLISHED, false);
        tagMgmtService.decTagPublishedRefCount(articleId);
        decArchiveDatePublishedRefCount(articleId);
        articleRepository.update(articleId, article);
        statisticMgmtService.decPublishedBlogArticleCount();
        final int blogCmtCnt = statisticQueryService.getPublishedBlogCommentCount();
        final int articleCmtCnt = article.getInt(ARTICLE_COMMENT_COUNT);
        statisticMgmtService.setPublishedBlogCommentCount(blogCmtCnt - articleCmtCnt);
        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);
        userRepository.update(author.optString(Keys.OBJECT_ID), author);
        transaction.commit();
    } catch (final Exception e) {
        if (transaction.isActive()) {
            transaction.rollback();
        }
        LOGGER.log(Level.ERROR, "Cancels publish article failed", e);
        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 12 with Transaction

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

the class PluginMgmtService method setPluginStatus.

/**
     * Sets a plugin's status with the specified plugin id, status.
     * 
     * @param pluginId the specified plugin id
     * @param status the specified status, see {@link PluginStatus}
     * @return for example,
     * <pre>
     * {
     *     "sc": boolean,
     *     "msg": "" 
     * }
     * </pre>
     */
public JSONObject setPluginStatus(final String pluginId, final String status) {
    final Map<String, String> langs = langPropsService.getAll(Latkes.getLocale());
    final List<AbstractPlugin> plugins = pluginManager.getPlugins();
    final JSONObject ret = new JSONObject();
    for (final AbstractPlugin plugin : plugins) {
        if (plugin.getId().equals(pluginId)) {
            final Transaction transaction = pluginRepository.beginTransaction();
            try {
                plugin.setStatus(PluginStatus.valueOf(status));
                pluginRepository.update(pluginId, plugin.toJSONObject());
                transaction.commit();
                plugin.changeStatus();
                ret.put(Keys.STATUS_CODE, true);
                ret.put(Keys.MSG, langs.get("setSuccLabel"));
                return ret;
            } catch (final Exception e) {
                if (transaction.isActive()) {
                    transaction.rollback();
                }
                LOGGER.log(Level.ERROR, "Set plugin status error", e);
                ret.put(Keys.STATUS_CODE, false);
                ret.put(Keys.MSG, langs.get("setFailLabel"));
                return ret;
            }
        }
    }
    ret.put(Keys.STATUS_CODE, false);
    ret.put(Keys.MSG, langs.get("refreshAndRetryLabel"));
    return ret;
}
Also used : JSONObject(org.json.JSONObject) Transaction(org.b3log.latke.repository.Transaction) AbstractPlugin(org.b3log.latke.plugin.AbstractPlugin) JSONException(org.json.JSONException)

Example 13 with Transaction

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

the class PreferenceMgmtService method updateReplyNotificationTemplate.

/**
     * Updates the reply notification template with the specified reply notification template.
     *
     * @param replyNotificationTemplate the specified reply notification template
     * @throws ServiceException service exception
     */
public void updateReplyNotificationTemplate(final JSONObject replyNotificationTemplate) throws ServiceException {
    final Transaction transaction = optionRepository.beginTransaction();
    try {
        final JSONObject bodyOpt = optionRepository.get(Option.ID_C_REPLY_NOTI_TPL_BODY);
        bodyOpt.put(Option.OPTION_VALUE, replyNotificationTemplate.optString("body"));
        optionRepository.update(Option.ID_C_REPLY_NOTI_TPL_BODY, bodyOpt);
        final JSONObject subjectOpt = optionRepository.get(Option.ID_C_REPLY_NOTI_TPL_SUBJECT);
        subjectOpt.put(Option.OPTION_VALUE, replyNotificationTemplate.optString("subject"));
        optionRepository.update(Option.ID_C_REPLY_NOTI_TPL_SUBJECT, subjectOpt);
        transaction.commit();
    } catch (final Exception e) {
        if (transaction.isActive()) {
            transaction.rollback();
        }
        LOGGER.log(Level.ERROR, "Updates reply notification failed", e);
        throw new ServiceException(e);
    }
}
Also used : Transaction(org.b3log.latke.repository.Transaction) JSONObject(org.json.JSONObject) ServiceException(org.b3log.latke.service.ServiceException) ServiceException(org.b3log.latke.service.ServiceException)

Example 14 with Transaction

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

the class UpgradeService method perform.

/**
     * Performs upgrade.
     *
     * @throws Exception upgrade fails
     */
private void perform() throws Exception {
    LOGGER.log(Level.INFO, "Upgrading from version [{0}] to version [{1}]....", FROM_VER, TO_VER);
    Transaction transaction = optionRepository.beginTransaction();
    try {
        final JSONObject versionOpt = optionRepository.get(Option.ID_C_VERSION);
        versionOpt.put(Option.OPTION_VALUE, TO_VER);
        optionRepository.update(Option.ID_C_VERSION, versionOpt);
        transaction.commit();
        LOGGER.log(Level.INFO, "Updated preference");
    } catch (final Exception e) {
        if (null != transaction && transaction.isActive()) {
            transaction.rollback();
        }
        LOGGER.log(Level.ERROR, "Upgrade failed!", e);
        throw new Exception("Upgrade failed from version [" + FROM_VER + "] to version [" + TO_VER + ']');
    }
    LOGGER.log(Level.INFO, "Upgraded from version [{0}] to version [{1}] successfully :-)", FROM_VER, TO_VER);
}
Also used : Transaction(org.b3log.latke.repository.Transaction) JSONObject(org.json.JSONObject) ServiceException(org.b3log.latke.service.ServiceException) JSONException(org.json.JSONException) IOException(java.io.IOException)

Example 15 with Transaction

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

the class UpgradeService method upgradeArticles.

/**
     * Upgrades articles.
     *
     * @throws Exception exception
     */
private void upgradeArticles() throws Exception {
    LOGGER.log(Level.INFO, "Adds a property [articleEditorType] to each of articles");
    final JSONArray articles = articleRepository.get(new Query()).getJSONArray(Keys.RESULTS);
    if (articles.length() <= 0) {
        LOGGER.log(Level.TRACE, "No articles");
        return;
    }
    Transaction transaction = null;
    try {
        for (int i = 0; i < articles.length(); i++) {
            if (0 == i % STEP || !transaction.isActive()) {
                transaction = userRepository.beginTransaction();
            }
            final JSONObject article = articles.getJSONObject(i);
            final String articleId = article.optString(Keys.OBJECT_ID);
            LOGGER.log(Level.INFO, "Found an article[id={0}]", articleId);
            article.put(Article.ARTICLE_EDITOR_TYPE, "tinyMCE");
            articleRepository.update(article.getString(Keys.OBJECT_ID), article);
            if (0 == i % STEP) {
                transaction.commit();
                LOGGER.log(Level.TRACE, "Updated some articles");
            }
        }
        if (transaction.isActive()) {
            transaction.commit();
        }
        LOGGER.log(Level.TRACE, "Updated all articles");
    } catch (final Exception e) {
        if (transaction.isActive()) {
            transaction.rollback();
        }
        throw e;
    }
}
Also used : Query(org.b3log.latke.repository.Query) Transaction(org.b3log.latke.repository.Transaction) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) ServiceException(org.b3log.latke.service.ServiceException) JSONException(org.json.JSONException) IOException(java.io.IOException)

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