Search in sources :

Example 1 with ServiceException

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

the class Filler method setArticleExProperties.

/**
     * Sets some extra properties into the specified article with the specified author and preference, performs content
     * and abstract editor processing.
     *
     * <p>
     * Article ext properties:
     * <pre>
     * {
     *     ....,
     *     "authorName": "",
     *     "authorId": "",
     *     "authorThumbnailURL": "",
     *     "hasUpdated": boolean
     * }
     * </pre> </p>
     *
     * @param request the specified HTTP servlet request
     * @param article the specified article
     * @param author the specified author
     * @param preference the specified preference
     * @throws ServiceException service exception
     * @see #setArticlesExProperties(HttpServletRequest, List, JSONObject, JSONObject)
     */
private void setArticleExProperties(final HttpServletRequest request, final JSONObject article, final JSONObject author, final JSONObject preference) throws ServiceException {
    try {
        final String authorName = author.getString(User.USER_NAME);
        article.put(Common.AUTHOR_NAME, authorName);
        final String authorId = author.getString(Keys.OBJECT_ID);
        article.put(Common.AUTHOR_ID, authorId);
        final String userAvatar = author.optString(UserExt.USER_AVATAR);
        if (!Strings.isEmptyOrNull(userAvatar)) {
            article.put(Common.AUTHOR_THUMBNAIL_URL, userAvatar);
        } else {
            final String thumbnailURL = Thumbnails.getGravatarURL(author.optString(User.USER_EMAIL), "128");
            article.put(Common.AUTHOR_THUMBNAIL_URL, thumbnailURL);
        }
        if (preference.getBoolean(Option.ID_C_ENABLE_ARTICLE_UPDATE_HINT)) {
            article.put(Common.HAS_UPDATED, articleQueryService.hasUpdated(article));
        } else {
            article.put(Common.HAS_UPDATED, false);
        }
        if (articleQueryService.needViewPwd(request, article)) {
            final String content = langPropsService.get("articleContentPwd");
            article.put(ARTICLE_CONTENT, content);
        }
        processArticleAbstract(preference, article);
        articleQueryService.markdown(article);
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, "Sets article extra properties failed", e);
        throw new ServiceException(e);
    }
}
Also used : ServiceException(org.b3log.latke.service.ServiceException) JSONException(org.json.JSONException) EventException(org.b3log.latke.event.EventException) ServiceException(org.b3log.latke.service.ServiceException)

Example 2 with ServiceException

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

the class ArticleProcessor method getArticleContent.

/**
     * Gets article content with the specified context.
     *
     * @param context the specified context
     * @param request the specified request
     */
@RequestProcessing(value = "/get-article-content", method = HTTPRequestMethod.GET)
public void getArticleContent(final HTTPRequestContext context, final HttpServletRequest request) {
    final String articleId = request.getParameter("id");
    if (Strings.isEmptyOrNull(articleId)) {
        return;
    }
    final TextHTMLRenderer renderer = new TextHTMLRenderer();
    context.setRenderer(renderer);
    String content;
    try {
        content = articleQueryService.getArticleContent(request, articleId);
    } catch (final ServiceException e) {
        LOGGER.log(Level.ERROR, "Can not get article content", e);
        return;
    }
    if (null == content) {
        return;
    }
    renderer.setContent(content);
}
Also used : ServiceException(org.b3log.latke.service.ServiceException) TextHTMLRenderer(org.b3log.latke.servlet.renderer.TextHTMLRenderer) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Example 3 with ServiceException

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

the class ArticleMgmtService method decTagRefCount.

/**
     * Decrements reference count of every tag of an article specified by the
     * given article id.
     *
     * @param articleId the given article id
     * @throws ServiceException service exception
     */
private void decTagRefCount(final String articleId) throws ServiceException {
    try {
        final List<JSONObject> tags = tagRepository.getByArticleId(articleId);
        final JSONObject article = articleRepository.get(articleId);
        for (final JSONObject tag : tags) {
            final String tagId = tag.getString(Keys.OBJECT_ID);
            final int refCnt = tag.getInt(Tag.TAG_REFERENCE_COUNT);
            tag.put(Tag.TAG_REFERENCE_COUNT, refCnt - 1);
            final int publishedRefCnt = tag.getInt(Tag.TAG_PUBLISHED_REFERENCE_COUNT);
            if (article.getBoolean(Article.ARTICLE_IS_PUBLISHED)) {
                tag.put(Tag.TAG_PUBLISHED_REFERENCE_COUNT, publishedRefCnt - 1);
            } else {
                tag.put(Tag.TAG_PUBLISHED_REFERENCE_COUNT, publishedRefCnt);
            }
            tagRepository.update(tagId, tag);
            LOGGER.log(Level.TRACE, "Deced tag[title={0}, refCnt={1}, publishedRefCnt={2}] of article[id={3}]", new Object[] { tag.getString(Tag.TAG_TITLE), tag.getInt(Tag.TAG_REFERENCE_COUNT), tag.getInt(Tag.TAG_PUBLISHED_REFERENCE_COUNT), articleId });
        }
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, "Decs tag references count of article[id" + articleId + "] failed", e);
        throw new ServiceException(e);
    }
    LOGGER.log(Level.DEBUG, "Deced all tag reference count of article[id={0}]", articleId);
}
Also used : 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 ServiceException

use of org.b3log.latke.service.ServiceException 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 5 with ServiceException

use of org.b3log.latke.service.ServiceException 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)

Aggregations

ServiceException (org.b3log.latke.service.ServiceException)268 JSONObject (org.json.JSONObject)219 RequestProcessing (org.b3log.latke.servlet.annotation.RequestProcessing)57 JSONArray (org.json.JSONArray)57 JSONException (org.json.JSONException)52 RepositoryException (org.b3log.latke.repository.RepositoryException)46 Transaction (org.b3log.latke.repository.Transaction)40 Transactional (org.b3log.latke.repository.annotation.Transactional)38 Before (org.b3log.latke.servlet.annotation.Before)32 EventException (org.b3log.latke.event.EventException)22 ArrayList (java.util.ArrayList)18 After (org.b3log.latke.servlet.annotation.After)16 JSONRenderer (org.b3log.latke.servlet.renderer.JSONRenderer)16 IOException (java.io.IOException)15 AbstractFreeMarkerRenderer (org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer)15 ParseException (java.text.ParseException)13 Date (java.util.Date)13 Query (org.b3log.latke.repository.Query)8 UnsupportedEncodingException (java.io.UnsupportedEncodingException)5 Template (freemarker.template.Template)3