Search in sources :

Example 1 with Transaction

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

the class ArticleMgmtService method updateArticlesRandomValue.

/**
     * Updates the random values of articles fetched with the specified update
     * count.
     *
     * @param updateCnt the specified update count
     * @throws ServiceException service exception
     */
public void updateArticlesRandomValue(final int updateCnt) throws ServiceException {
    final Transaction transaction = articleRepository.beginTransaction();
    try {
        final List<JSONObject> randomArticles = articleRepository.getRandomly(updateCnt);
        for (final JSONObject article : randomArticles) {
            article.put(Article.ARTICLE_RANDOM_DOUBLE, Math.random());
            articleRepository.update(article.getString(Keys.OBJECT_ID), article);
        }
        transaction.commit();
    } catch (final Exception e) {
        if (transaction.isActive()) {
            transaction.rollback();
        }
        LOGGER.log(Level.WARN, "Updates article random value failed");
        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 2 with Transaction

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

the class ArticleMgmtService method updateArticle.

/**
     * Updates an article by the specified request json object.
     *
     * @param requestJSONObject the specified request json object, for example,
     * <pre>
     * {
     *     "article": {
     *         "oId": "",
     *         "articleTitle": "",
     *         "articleAbstract": "",
     *         "articleContent": "",
     *         "articleTags": "tag1,tag2,tag3",
     *         "articlePermalink": "", // optional
     *         "articleIsPublished": boolean,
     *         "articleSignId": "", // optional
     *         "articleCommentable": boolean,
     *         "articleViewPwd": "",
     *         "articleEditorType": "" // optional, preference specified if not exists this key
     *     }
     * }
     * </pre>
     * @throws ServiceException service exception
     */
public void updateArticle(final JSONObject requestJSONObject) throws ServiceException {
    final JSONObject ret = new JSONObject();
    final Transaction transaction = articleRepository.beginTransaction();
    try {
        final JSONObject article = requestJSONObject.getJSONObject(ARTICLE);
        final String tagsString = article.optString(Article.ARTICLE_TAGS_REF);
        article.put(Article.ARTICLE_TAGS_REF, tagsString.replaceAll(",", ",").replaceAll("、", ","));
        final String articleId = article.getString(Keys.OBJECT_ID);
        // Set permalink
        final JSONObject oldArticle = articleRepository.get(articleId);
        final String permalink = getPermalinkForUpdateArticle(oldArticle, article, (Date) oldArticle.get(ARTICLE_CREATE_DATE));
        article.put(ARTICLE_PERMALINK, permalink);
        processTagsForArticleUpdate(oldArticle, article);
        if (!oldArticle.getString(Article.ARTICLE_PERMALINK).equals(permalink)) {
            // The permalink has been updated
            // Updates related comments' links
            processCommentsForArticleUpdate(article);
        }
        // Fill auto properties
        fillAutoProperties(oldArticle, article);
        // Set date
        article.put(ARTICLE_UPDATE_DATE, oldArticle.get(ARTICLE_UPDATE_DATE));
        final JSONObject preference = preferenceQueryService.getPreference();
        final Date date = new Date();
        // The article to update has no sign
        if (!article.has(Article.ARTICLE_SIGN_ID)) {
            article.put(Article.ARTICLE_SIGN_ID, "0");
        }
        if (article.getBoolean(ARTICLE_IS_PUBLISHED)) {
            // Publish it
            if (articleQueryService.hadBeenPublished(oldArticle)) {
                // Edit update date only for published article
                article.put(ARTICLE_UPDATE_DATE, date);
            } else {
                // This article is a draft and this is the first time to publish it
                article.put(ARTICLE_CREATE_DATE, date);
                article.put(ARTICLE_UPDATE_DATE, date);
                article.put(ARTICLE_HAD_BEEN_PUBLISHED, true);
            }
        } else {
            // Save as draft
            if (articleQueryService.hadBeenPublished(oldArticle)) {
                // Save update date only for published article
                article.put(ARTICLE_UPDATE_DATE, date);
            } else {
                // Reset create/update date to indicate this is an new draft
                article.put(ARTICLE_CREATE_DATE, date);
                article.put(ARTICLE_UPDATE_DATE, date);
            }
        }
        // Set editor type
        if (!article.has(Article.ARTICLE_EDITOR_TYPE)) {
            article.put(Article.ARTICLE_EDITOR_TYPE, preference.optString(Option.ID_C_EDITOR_TYPE));
        }
        final boolean publishNewArticle = !oldArticle.getBoolean(ARTICLE_IS_PUBLISHED) && article.getBoolean(ARTICLE_IS_PUBLISHED);
        // Set statistic
        if (publishNewArticle) {
            // This article is updated from unpublished to published
            statisticMgmtService.incPublishedBlogArticleCount();
            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);
        }
        if (publishNewArticle) {
            incArchiveDatePublishedRefCount(articleId);
        }
        // Update
        final boolean postToCommunity = article.optBoolean(Common.POST_TO_COMMUNITY, true);
        // Do not persist this property
        article.remove(Common.POST_TO_COMMUNITY);
        articleRepository.update(articleId, article);
        // Restores the property
        article.put(Common.POST_TO_COMMUNITY, postToCommunity);
        if (publishNewArticle) {
            // Fire add article event
            final JSONObject eventData = new JSONObject();
            eventData.put(ARTICLE, article);
            eventData.put(Keys.RESULTS, ret);
            try {
                eventManager.fireEventSynchronously(new Event<JSONObject>(EventTypes.ADD_ARTICLE, eventData));
            } catch (final EventException e) {
                LOGGER.log(Level.ERROR, e.getMessage(), e);
            }
        } else {
            // Fire update article event
            final JSONObject eventData = new JSONObject();
            eventData.put(ARTICLE, article);
            eventData.put(Keys.RESULTS, ret);
            try {
                eventManager.fireEventSynchronously(new Event<JSONObject>(EventTypes.UPDATE_ARTICLE, eventData));
            } catch (final EventException e) {
                LOGGER.log(Level.ERROR, e.getMessage(), e);
            }
        }
        transaction.commit();
    } catch (final ServiceException e) {
        if (transaction.isActive()) {
            transaction.rollback();
        }
        LOGGER.log(Level.ERROR, "Updates an article failed", e);
        throw e;
    } catch (final Exception e) {
        if (transaction.isActive()) {
            transaction.rollback();
        }
        LOGGER.log(Level.ERROR, "Updates an article failed", e);
        throw new ServiceException(e.getMessage());
    }
}
Also used : JSONObject(org.json.JSONObject) Transaction(org.b3log.latke.repository.Transaction) ServiceException(org.b3log.latke.service.ServiceException) EventException(org.b3log.latke.event.EventException) Date(java.util.Date) ArchiveDate(org.b3log.solo.model.ArchiveDate) 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 3 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 4 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 5 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)

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