Search in sources :

Example 26 with Transaction

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

the class CommentMgmtService method addArticleComment.

/**
     * Adds an article comment with the specified request json object.
     *
     * @param requestJSONObject the specified request json object, for example,      <pre>
     * {
     *     "oId": "", // article id
     *     "commentName": "",
     *     "commentEmail": "",
     *     "commentURL": "", // optional
     *     "commentContent": "",
     *     "commentOriginalCommentId": "" // optional
     * }
     * </pre>
     *
     * @return add result, for example,      <pre>
     * {
     *     "oId": "", // generated comment id
     *     "commentDate": "", // format: yyyy-MM-dd HH:mm:ss
     *     "commentOriginalCommentName": "" // optional, corresponding to argument "commentOriginalCommentId"
     *     "commentThumbnailURL": "",
     *     "commentSharpURL": "",
     *     "commentContent": "", // processed XSS HTML
     *     "commentName": "", // processed XSS
     *     "commentURL": "", // optional
     *     "isReply": boolean,
     *     "article": {},
     *     "commentOriginalCommentId": "", // optional
     *     "commentable": boolean,
     *     "permalink": "" // article.articlePermalink
     * }
     * </pre>
     *
     * @throws ServiceException service exception
     */
public JSONObject addArticleComment(final JSONObject requestJSONObject) throws ServiceException {
    final JSONObject ret = new JSONObject();
    ret.put(Common.IS_REPLY, false);
    final Transaction transaction = commentRepository.beginTransaction();
    try {
        final String articleId = requestJSONObject.getString(Keys.OBJECT_ID);
        final JSONObject article = articleRepository.get(articleId);
        ret.put(Article.ARTICLE, article);
        final String commentName = requestJSONObject.getString(Comment.COMMENT_NAME);
        final String commentEmail = requestJSONObject.getString(Comment.COMMENT_EMAIL).trim().toLowerCase();
        final String commentURL = requestJSONObject.optString(Comment.COMMENT_URL);
        final String commentContent = requestJSONObject.getString(Comment.COMMENT_CONTENT);
        final String originalCommentId = requestJSONObject.optString(Comment.COMMENT_ORIGINAL_COMMENT_ID);
        ret.put(Comment.COMMENT_ORIGINAL_COMMENT_ID, originalCommentId);
        // Step 1: Add comment
        final JSONObject comment = new JSONObject();
        comment.put(Comment.COMMENT_ORIGINAL_COMMENT_ID, "");
        comment.put(Comment.COMMENT_ORIGINAL_COMMENT_NAME, "");
        JSONObject originalComment = null;
        comment.put(Comment.COMMENT_NAME, commentName);
        comment.put(Comment.COMMENT_EMAIL, commentEmail);
        comment.put(Comment.COMMENT_URL, commentURL);
        comment.put(Comment.COMMENT_CONTENT, commentContent);
        comment.put(Comment.COMMENT_ORIGINAL_COMMENT_ID, requestJSONObject.optString(Comment.COMMENT_ORIGINAL_COMMENT_ID));
        comment.put(Comment.COMMENT_ORIGINAL_COMMENT_NAME, requestJSONObject.optString(Comment.COMMENT_ORIGINAL_COMMENT_NAME));
        final JSONObject preference = preferenceQueryService.getPreference();
        final Date date = new Date();
        comment.put(Comment.COMMENT_DATE, date);
        ret.put(Comment.COMMENT_DATE, DateFormatUtils.format(date, "yyyy-MM-dd HH:mm:ss"));
        ret.put("commentDate2", date);
        ret.put(Common.COMMENTABLE, preference.getBoolean(Option.ID_C_COMMENTABLE) && article.getBoolean(Article.ARTICLE_COMMENTABLE));
        ret.put(Common.PERMALINK, article.getString(Article.ARTICLE_PERMALINK));
        ret.put(Comment.COMMENT_NAME, commentName);
        ret.put(Comment.COMMENT_CONTENT, commentContent);
        ret.put(Comment.COMMENT_URL, commentURL);
        if (!Strings.isEmptyOrNull(originalCommentId)) {
            originalComment = commentRepository.get(originalCommentId);
            if (null != originalComment) {
                comment.put(Comment.COMMENT_ORIGINAL_COMMENT_ID, originalCommentId);
                final String originalCommentName = originalComment.getString(Comment.COMMENT_NAME);
                comment.put(Comment.COMMENT_ORIGINAL_COMMENT_NAME, originalCommentName);
                ret.put(Comment.COMMENT_ORIGINAL_COMMENT_NAME, originalCommentName);
                ret.put(Common.IS_REPLY, true);
            } else {
                LOGGER.log(Level.WARN, "Not found orginal comment[id={0}] of reply[name={1}, content={2}]", new String[] { originalCommentId, commentName, commentContent });
            }
        }
        setCommentThumbnailURL(comment);
        ret.put(Comment.COMMENT_THUMBNAIL_URL, comment.getString(Comment.COMMENT_THUMBNAIL_URL));
        // Sets comment on article....
        comment.put(Comment.COMMENT_ON_ID, articleId);
        comment.put(Comment.COMMENT_ON_TYPE, Article.ARTICLE);
        final String commentId = Ids.genTimeMillisId();
        comment.put(Keys.OBJECT_ID, commentId);
        ret.put(Keys.OBJECT_ID, commentId);
        final String commentSharpURL = Comments.getCommentSharpURLForArticle(article, commentId);
        comment.put(Comment.COMMENT_SHARP_URL, commentSharpURL);
        ret.put(Comment.COMMENT_SHARP_URL, commentSharpURL);
        commentRepository.add(comment);
        // Step 2: Update article comment count
        articleMgmtService.incArticleCommentCount(articleId);
        // Step 3: Update blog statistic comment count
        statisticMgmtService.incBlogCommentCount();
        statisticMgmtService.incPublishedBlogCommentCount();
        // Step 4: Send an email to admin
        try {
            sendNotificationMail(article, comment, originalComment, preference);
        } catch (final Exception e) {
            LOGGER.log(Level.WARN, "Send mail failed", e);
        }
        // Step 5: Fire add comment event
        final JSONObject eventData = new JSONObject();
        eventData.put(Comment.COMMENT, comment);
        eventData.put(Article.ARTICLE, article);
        eventManager.fireEventSynchronously(new Event<JSONObject>(EventTypes.ADD_COMMENT_TO_ARTICLE, eventData));
        transaction.commit();
    } catch (final Exception e) {
        if (transaction.isActive()) {
            transaction.rollback();
        }
        throw new ServiceException(e);
    }
    return ret;
}
Also used : JSONObject(org.json.JSONObject) Transaction(org.b3log.latke.repository.Transaction) ServiceException(org.b3log.latke.service.ServiceException) Date(java.util.Date) ServiceException(org.b3log.latke.service.ServiceException) JSONException(org.json.JSONException) RepositoryException(org.b3log.latke.repository.RepositoryException) IOException(java.io.IOException)

Example 27 with Transaction

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

the class PluginMgmtService method updatePluginSetting.

/**
     * updatePluginSetting.
     * 
     * @param pluginId the specified pluginoId
     * @param setting the specified setting
     * @return the ret json
     */
public JSONObject updatePluginSetting(final String pluginId, final String setting) {
    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 {
                final JSONObject pluginJson = plugin.toJSONObject();
                pluginJson.put(Plugin.PLUGIN_SETTING, setting);
                pluginRepository.update(pluginId, pluginJson);
                transaction.commit();
                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 28 with Transaction

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

the class PreferenceMgmtService method updatePreference.

/**
     * Updates the preference with the specified preference.
     *
     * @param preference the specified preference
     * @throws ServiceException service exception
     */
public void updatePreference(final JSONObject preference) throws ServiceException {
    @SuppressWarnings("unchecked") final Iterator<String> keys = preference.keys();
    while (keys.hasNext()) {
        final String key = keys.next();
        if (preference.isNull(key)) {
            throw new ServiceException("A value is null of preference[key=" + key + "]");
        }
    }
    final Transaction transaction = optionRepository.beginTransaction();
    try {
        final String skinDirName = preference.getString(Skin.SKIN_DIR_NAME);
        final String skinName = Latkes.getSkinName(skinDirName);
        preference.put(Skin.SKIN_NAME, skinName);
        final Set<String> skinDirNames = Skins.getSkinDirNames();
        final JSONArray skinArray = new JSONArray();
        for (final String dirName : skinDirNames) {
            final JSONObject skin = new JSONObject();
            skinArray.put(skin);
            final String name = Latkes.getSkinName(dirName);
            skin.put(Skin.SKIN_NAME, name);
            skin.put(Skin.SKIN_DIR_NAME, dirName);
        }
        preference.put(Skin.SKINS, skinArray.toString());
        final String timeZoneId = preference.getString(Option.ID_C_TIME_ZONE_ID);
        TimeZones.setTimeZone(timeZoneId);
        preference.put(Option.ID_C_SIGNS, preference.get(Option.ID_C_SIGNS).toString());
        final JSONObject oldPreference = preferenceQueryService.getPreference();
        final String adminEmail = oldPreference.getString(Option.ID_C_ADMIN_EMAIL);
        preference.put(Option.ID_C_ADMIN_EMAIL, adminEmail);
        final String version = oldPreference.optString(Option.ID_C_VERSION);
        preference.put(Option.ID_C_VERSION, version);
        final String localeString = preference.getString(Option.ID_C_LOCALE_STRING);
        LOGGER.log(Level.DEBUG, "Current locale[string={0}]", localeString);
        Latkes.setLocale(new Locale(Locales.getLanguage(localeString), Locales.getCountry(localeString)));
        final JSONObject adminEmailOpt = optionRepository.get(Option.ID_C_ADMIN_EMAIL);
        adminEmailOpt.put(Option.OPTION_VALUE, adminEmail);
        optionRepository.update(Option.ID_C_ADMIN_EMAIL, adminEmailOpt);
        final JSONObject allowVisitDraftViaPermalinkOpt = optionRepository.get(Option.ID_C_ALLOW_VISIT_DRAFT_VIA_PERMALINK);
        allowVisitDraftViaPermalinkOpt.put(Option.OPTION_VALUE, preference.optString(Option.ID_C_ALLOW_VISIT_DRAFT_VIA_PERMALINK));
        optionRepository.update(Option.ID_C_ALLOW_VISIT_DRAFT_VIA_PERMALINK, allowVisitDraftViaPermalinkOpt);
        final JSONObject allowRegisterOpt = optionRepository.get(Option.ID_C_ALLOW_REGISTER);
        allowRegisterOpt.put(Option.OPTION_VALUE, preference.optString(Option.ID_C_ALLOW_REGISTER));
        optionRepository.update(Option.ID_C_ALLOW_REGISTER, allowRegisterOpt);
        final JSONObject articleListDisplayCountOpt = optionRepository.get(Option.ID_C_ARTICLE_LIST_DISPLAY_COUNT);
        articleListDisplayCountOpt.put(Option.OPTION_VALUE, preference.optString(Option.ID_C_ARTICLE_LIST_DISPLAY_COUNT));
        optionRepository.update(Option.ID_C_ARTICLE_LIST_DISPLAY_COUNT, articleListDisplayCountOpt);
        final JSONObject articleListPaginationWindowSizeOpt = optionRepository.get(Option.ID_C_ARTICLE_LIST_PAGINATION_WINDOW_SIZE);
        articleListPaginationWindowSizeOpt.put(Option.OPTION_VALUE, preference.optString(Option.ID_C_ARTICLE_LIST_PAGINATION_WINDOW_SIZE));
        optionRepository.update(Option.ID_C_ARTICLE_LIST_PAGINATION_WINDOW_SIZE, articleListPaginationWindowSizeOpt);
        final JSONObject articleListStyleOpt = optionRepository.get(Option.ID_C_ARTICLE_LIST_STYLE);
        articleListStyleOpt.put(Option.OPTION_VALUE, preference.optString(Option.ID_C_ARTICLE_LIST_STYLE));
        optionRepository.update(Option.ID_C_ARTICLE_LIST_STYLE, articleListStyleOpt);
        final JSONObject blogSubtitleOpt = optionRepository.get(Option.ID_C_BLOG_SUBTITLE);
        blogSubtitleOpt.put(Option.OPTION_VALUE, preference.optString(Option.ID_C_BLOG_SUBTITLE));
        optionRepository.update(Option.ID_C_BLOG_SUBTITLE, blogSubtitleOpt);
        final JSONObject blogTitleOpt = optionRepository.get(Option.ID_C_BLOG_TITLE);
        blogTitleOpt.put(Option.OPTION_VALUE, preference.optString(Option.ID_C_BLOG_TITLE));
        optionRepository.update(Option.ID_C_BLOG_TITLE, blogTitleOpt);
        final JSONObject commentableOpt = optionRepository.get(Option.ID_C_COMMENTABLE);
        commentableOpt.put(Option.OPTION_VALUE, preference.optString(Option.ID_C_COMMENTABLE));
        optionRepository.update(Option.ID_C_COMMENTABLE, commentableOpt);
        final JSONObject editorTypeOpt = optionRepository.get(Option.ID_C_EDITOR_TYPE);
        editorTypeOpt.put(Option.OPTION_VALUE, preference.optString(Option.ID_C_EDITOR_TYPE));
        optionRepository.update(Option.ID_C_EDITOR_TYPE, editorTypeOpt);
        final JSONObject enableArticleUpdateHintOpt = optionRepository.get(Option.ID_C_ENABLE_ARTICLE_UPDATE_HINT);
        enableArticleUpdateHintOpt.put(Option.OPTION_VALUE, preference.optString(Option.ID_C_ENABLE_ARTICLE_UPDATE_HINT));
        optionRepository.update(Option.ID_C_ENABLE_ARTICLE_UPDATE_HINT, enableArticleUpdateHintOpt);
        final JSONObject externalRelevantArticlesDisplayCountOpt = optionRepository.get(Option.ID_C_EXTERNAL_RELEVANT_ARTICLES_DISPLAY_CNT);
        externalRelevantArticlesDisplayCountOpt.put(Option.OPTION_VALUE, preference.optString(Option.ID_C_EXTERNAL_RELEVANT_ARTICLES_DISPLAY_CNT));
        optionRepository.update(Option.ID_C_EXTERNAL_RELEVANT_ARTICLES_DISPLAY_CNT, externalRelevantArticlesDisplayCountOpt);
        final JSONObject feedOutputCntOpt = optionRepository.get(Option.ID_C_FEED_OUTPUT_CNT);
        feedOutputCntOpt.put(Option.OPTION_VALUE, preference.optString(Option.ID_C_FEED_OUTPUT_CNT));
        optionRepository.update(Option.ID_C_FEED_OUTPUT_CNT, feedOutputCntOpt);
        final JSONObject feedOutputModeOpt = optionRepository.get(Option.ID_C_FEED_OUTPUT_MODE);
        feedOutputModeOpt.put(Option.OPTION_VALUE, preference.optString(Option.ID_C_FEED_OUTPUT_MODE));
        optionRepository.update(Option.ID_C_FEED_OUTPUT_MODE, feedOutputModeOpt);
        final JSONObject footerContentOpt = optionRepository.get(Option.ID_C_FOOTER_CONTENT);
        footerContentOpt.put(Option.OPTION_VALUE, preference.optString(Option.ID_C_FOOTER_CONTENT));
        optionRepository.update(Option.ID_C_FOOTER_CONTENT, footerContentOpt);
        final JSONObject htmlHeadOpt = optionRepository.get(Option.ID_C_HTML_HEAD);
        htmlHeadOpt.put(Option.OPTION_VALUE, preference.optString(Option.ID_C_HTML_HEAD));
        optionRepository.update(Option.ID_C_HTML_HEAD, htmlHeadOpt);
        final JSONObject keyOfSoloOpt = optionRepository.get(Option.ID_C_KEY_OF_SOLO);
        keyOfSoloOpt.put(Option.OPTION_VALUE, preference.optString(Option.ID_C_KEY_OF_SOLO));
        optionRepository.update(Option.ID_C_KEY_OF_SOLO, keyOfSoloOpt);
        final JSONObject localeStringOpt = optionRepository.get(Option.ID_C_LOCALE_STRING);
        localeStringOpt.put(Option.OPTION_VALUE, preference.optString(Option.ID_C_LOCALE_STRING));
        optionRepository.update(Option.ID_C_LOCALE_STRING, localeStringOpt);
        final JSONObject metaDescriptionOpt = optionRepository.get(Option.ID_C_META_DESCRIPTION);
        metaDescriptionOpt.put(Option.OPTION_VALUE, preference.optString(Option.ID_C_META_DESCRIPTION));
        optionRepository.update(Option.ID_C_META_DESCRIPTION, metaDescriptionOpt);
        final JSONObject metaKeywordsOpt = optionRepository.get(Option.ID_C_META_KEYWORDS);
        metaKeywordsOpt.put(Option.OPTION_VALUE, preference.optString(Option.ID_C_META_KEYWORDS));
        optionRepository.update(Option.ID_C_META_KEYWORDS, metaKeywordsOpt);
        final JSONObject mostCommentArticleDisplayCountOpt = optionRepository.get(Option.ID_C_MOST_COMMENT_ARTICLE_DISPLAY_CNT);
        mostCommentArticleDisplayCountOpt.put(Option.OPTION_VALUE, preference.optString(Option.ID_C_MOST_COMMENT_ARTICLE_DISPLAY_CNT));
        optionRepository.update(Option.ID_C_MOST_COMMENT_ARTICLE_DISPLAY_CNT, mostCommentArticleDisplayCountOpt);
        final JSONObject mostUsedTagDisplayCountOpt = optionRepository.get(Option.ID_C_MOST_USED_TAG_DISPLAY_CNT);
        mostUsedTagDisplayCountOpt.put(Option.OPTION_VALUE, preference.optString(Option.ID_C_MOST_USED_TAG_DISPLAY_CNT));
        optionRepository.update(Option.ID_C_MOST_USED_TAG_DISPLAY_CNT, mostUsedTagDisplayCountOpt);
        final JSONObject mostViewArticleDisplayCountOpt = optionRepository.get(Option.ID_C_MOST_VIEW_ARTICLE_DISPLAY_CNT);
        mostViewArticleDisplayCountOpt.put(Option.OPTION_VALUE, preference.optString(Option.ID_C_MOST_VIEW_ARTICLE_DISPLAY_CNT));
        optionRepository.update(Option.ID_C_MOST_VIEW_ARTICLE_DISPLAY_CNT, mostViewArticleDisplayCountOpt);
        final JSONObject noticeBoardOpt = optionRepository.get(Option.ID_C_NOTICE_BOARD);
        noticeBoardOpt.put(Option.OPTION_VALUE, preference.optString(Option.ID_C_NOTICE_BOARD));
        optionRepository.update(Option.ID_C_NOTICE_BOARD, noticeBoardOpt);
        final JSONObject randomArticlesDisplayCountOpt = optionRepository.get(Option.ID_C_RANDOM_ARTICLES_DISPLAY_CNT);
        randomArticlesDisplayCountOpt.put(Option.OPTION_VALUE, preference.optString(Option.ID_C_RANDOM_ARTICLES_DISPLAY_CNT));
        optionRepository.update(Option.ID_C_RANDOM_ARTICLES_DISPLAY_CNT, randomArticlesDisplayCountOpt);
        final JSONObject recentArticleDisplayCountOpt = optionRepository.get(Option.ID_C_RECENT_ARTICLE_DISPLAY_CNT);
        recentArticleDisplayCountOpt.put(Option.OPTION_VALUE, preference.optString(Option.ID_C_RECENT_ARTICLE_DISPLAY_CNT));
        optionRepository.update(Option.ID_C_RECENT_ARTICLE_DISPLAY_CNT, recentArticleDisplayCountOpt);
        final JSONObject recentCommentDisplayCountOpt = optionRepository.get(Option.ID_C_RECENT_COMMENT_DISPLAY_CNT);
        recentCommentDisplayCountOpt.put(Option.OPTION_VALUE, preference.optString(Option.ID_C_RECENT_COMMENT_DISPLAY_CNT));
        optionRepository.update(Option.ID_C_RECENT_COMMENT_DISPLAY_CNT, recentCommentDisplayCountOpt);
        final JSONObject relevantArticlesDisplayCountOpt = optionRepository.get(Option.ID_C_RELEVANT_ARTICLES_DISPLAY_CNT);
        relevantArticlesDisplayCountOpt.put(Option.OPTION_VALUE, preference.optString(Option.ID_C_RELEVANT_ARTICLES_DISPLAY_CNT));
        optionRepository.update(Option.ID_C_RELEVANT_ARTICLES_DISPLAY_CNT, relevantArticlesDisplayCountOpt);
        final JSONObject signsOpt = optionRepository.get(Option.ID_C_SIGNS);
        signsOpt.put(Option.OPTION_VALUE, preference.optString(Option.ID_C_SIGNS));
        optionRepository.update(Option.ID_C_SIGNS, signsOpt);
        final JSONObject skinDirNameOpt = optionRepository.get(Option.ID_C_SKIN_DIR_NAME);
        skinDirNameOpt.put(Option.OPTION_VALUE, preference.optString(Option.ID_C_SKIN_DIR_NAME));
        optionRepository.update(Option.ID_C_SKIN_DIR_NAME, skinDirNameOpt);
        final JSONObject skinNameOpt = optionRepository.get(Option.ID_C_SKIN_NAME);
        skinNameOpt.put(Option.OPTION_VALUE, preference.optString(Option.ID_C_SKIN_NAME));
        optionRepository.update(Option.ID_C_SKIN_NAME, skinNameOpt);
        final JSONObject skinsOpt = optionRepository.get(Option.ID_C_SKINS);
        skinsOpt.put(Option.OPTION_VALUE, preference.optString(Option.ID_C_SKINS));
        optionRepository.update(Option.ID_C_SKINS, skinsOpt);
        final JSONObject timeZoneIdOpt = optionRepository.get(Option.ID_C_TIME_ZONE_ID);
        timeZoneIdOpt.put(Option.OPTION_VALUE, preference.optString(Option.ID_C_TIME_ZONE_ID));
        optionRepository.update(Option.ID_C_TIME_ZONE_ID, timeZoneIdOpt);
        final JSONObject versionOpt = optionRepository.get(Option.ID_C_VERSION);
        versionOpt.put(Option.OPTION_VALUE, preference.optString(Option.ID_C_VERSION));
        optionRepository.update(Option.ID_C_VERSION, versionOpt);
        transaction.commit();
        final ServletContext servletContext = SoloServletListener.getServletContext();
        Templates.MAIN_CFG.setServletContextForTemplateLoading(servletContext, "/skins/" + skinDirName);
    } catch (final Exception e) {
        if (transaction.isActive()) {
            transaction.rollback();
        }
        LOGGER.log(Level.ERROR, "Updates preference failed", e);
        throw new ServiceException(langPropsService.get("updateFailLabel"));
    }
    LOGGER.log(Level.DEBUG, "Updates preference successfully");
}
Also used : Locale(java.util.Locale) ServiceException(org.b3log.latke.service.ServiceException) Transaction(org.b3log.latke.repository.Transaction) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) ServletContext(javax.servlet.ServletContext) ServiceException(org.b3log.latke.service.ServiceException)

Example 29 with Transaction

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

the class LinkMgmtService method updateLink.

/**
     * Updates a link by the specified request json object.
     *
     * @param requestJSONObject the specified request json object, for example,
     * <pre>
     * {
     *     "link": {
     *         "oId": "",
     *         "linkTitle": "",
     *         "linkAddress": ""
     *     }
     * }, see {@link Link} for more details
     * </pre>
     * @throws ServiceException service exception
     */
public void updateLink(final JSONObject requestJSONObject) throws ServiceException {
    final Transaction transaction = linkRepository.beginTransaction();
    try {
        final JSONObject link = requestJSONObject.getJSONObject(Link.LINK);
        final String linkId = link.getString(Keys.OBJECT_ID);
        final JSONObject oldLink = linkRepository.get(linkId);
        link.put(Link.LINK_ORDER, oldLink.getInt(Link.LINK_ORDER));
        linkRepository.update(linkId, link);
        transaction.commit();
    } catch (final Exception e) {
        if (transaction.isActive()) {
            transaction.rollback();
        }
        LOGGER.log(Level.ERROR, e.getMessage(), 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 30 with Transaction

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

the class LinkMgmtService method addLink.

/**
     * Adds a link with the specified request json object.
     * 
     * @param requestJSONObject the specified request json object, for example,
     * <pre>
     * {
     *     "link": {
     *         "linkTitle": "",
     *         "linkAddress": "",
     *         "linkDescription": "" // optional
     *     }
     * }, see {@link Link} for more details
     * </pre>
     * @return generated link id
     * @throws ServiceException service exception
     */
public String addLink(final JSONObject requestJSONObject) throws ServiceException {
    final Transaction transaction = linkRepository.beginTransaction();
    try {
        final JSONObject link = requestJSONObject.getJSONObject(Link.LINK);
        final int maxOrder = linkRepository.getMaxOrder();
        link.put(Link.LINK_ORDER, maxOrder + 1);
        final String ret = linkRepository.add(link);
        transaction.commit();
        return ret;
    } catch (final Exception e) {
        if (transaction.isActive()) {
            transaction.rollback();
        }
        LOGGER.log(Level.ERROR, "Adds a link 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