Search in sources :

Example 91 with ServiceException

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

the class ArticleMgmtService method addArticleInternal.

/**
     * Adds the specified article for internal invocation purposes.
     *
     * @param article the specified article
     * @return generated article id
     * @throws ServiceException service exception
     */
public String addArticleInternal(final JSONObject article) throws ServiceException {
    String ret = article.optString(Keys.OBJECT_ID);
    if (Strings.isEmptyOrNull(ret)) {
        ret = Ids.genTimeMillisId();
        article.put(Keys.OBJECT_ID, ret);
    }
    try {
        // Step 1: Add tags
        String tagsString = article.optString(Article.ARTICLE_TAGS_REF);
        tagsString = tagsString.replaceAll(",", ",").replaceAll("、", ",");
        article.put(Article.ARTICLE_TAGS_REF, tagsString);
        final String[] tagTitles = tagsString.split(",");
        final JSONArray tags = tag(tagTitles, article);
        // Step 2; Set comment/view count to 0
        article.put(Article.ARTICLE_COMMENT_COUNT, 0);
        article.put(Article.ARTICLE_VIEW_COUNT, 0);
        // Step 3: Set create/updat date
        final JSONObject preference = preferenceQueryService.getPreference();
        final Date date = new Date();
        if (!article.has(Article.ARTICLE_CREATE_DATE)) {
            article.put(Article.ARTICLE_CREATE_DATE, date);
        }
        article.put(Article.ARTICLE_UPDATE_DATE, article.opt(Article.ARTICLE_CREATE_DATE));
        // Step 4: Set put top to false
        article.put(Article.ARTICLE_PUT_TOP, false);
        // Step 5: Add tag-article relations
        addTagArticleRelation(tags, article);
        // Step 6: Inc blog article count statictis
        statisticMgmtService.incBlogArticleCount();
        if (article.optBoolean(Article.ARTICLE_IS_PUBLISHED)) {
            statisticMgmtService.incPublishedBlogArticleCount();
        }
        // Step 7: Add archive date-article relations
        archiveDate(article);
        // Step 8: Set permalink
        final String permalink = getPermalinkForAddArticle(article);
        article.put(Article.ARTICLE_PERMALINK, permalink);
        // Step 9: Add article sign id
        final String signId = article.optString(Article.ARTICLE_SIGN_ID, "1");
        article.put(Article.ARTICLE_SIGN_ID, signId);
        // Step 10: Set had been published status
        article.put(Article.ARTICLE_HAD_BEEN_PUBLISHED, false);
        if (article.optBoolean(Article.ARTICLE_IS_PUBLISHED)) {
            // Publish it directly
            article.put(Article.ARTICLE_HAD_BEEN_PUBLISHED, true);
        }
        // Step 11: Set random double
        article.put(Article.ARTICLE_RANDOM_DOUBLE, Math.random());
        // Step 12: Set post to community
        final boolean postToCommunity = article.optBoolean(Common.POST_TO_COMMUNITY, true);
        // Do not persist this property
        article.remove(Common.POST_TO_COMMUNITY);
        // Setp 13: Update user article statistic
        final JSONObject author = userRepository.getByEmail(article.optString(Article.ARTICLE_AUTHOR_EMAIL));
        final int userArticleCnt = author.optInt(UserExt.USER_ARTICLE_COUNT);
        author.put(UserExt.USER_ARTICLE_COUNT, userArticleCnt + 1);
        if (article.optBoolean(Article.ARTICLE_IS_PUBLISHED)) {
            author.put(UserExt.USER_PUBLISHED_ARTICLE_COUNT, author.optInt(UserExt.USER_PUBLISHED_ARTICLE_COUNT) + 1);
        }
        userRepository.update(author.optString(Keys.OBJECT_ID), author);
        // Step 14: Set editor type
        if (!article.has(Article.ARTICLE_EDITOR_TYPE)) {
            article.put(Article.ARTICLE_EDITOR_TYPE, preference.optString(Option.ID_C_EDITOR_TYPE));
        }
        // Step 15: Add article
        articleRepository.add(article);
        // Restores the property
        article.put(Common.POST_TO_COMMUNITY, postToCommunity);
        if (article.optBoolean(Article.ARTICLE_IS_PUBLISHED)) {
            // Fire add article event
            final JSONObject eventData = new JSONObject();
            eventData.put(Article.ARTICLE, article);
            eventManager.fireEventSynchronously(new Event<JSONObject>(EventTypes.ADD_ARTICLE, eventData));
        }
        article.remove(Common.POST_TO_COMMUNITY);
    } catch (final RepositoryException e) {
        LOGGER.log(Level.ERROR, "Adds an article failed", e);
        throw new ServiceException(e);
    } catch (final EventException e) {
        LOGGER.log(Level.WARN, "Adds an article event process failed", e);
    }
    return ret;
}
Also used : JSONObject(org.json.JSONObject) ServiceException(org.b3log.latke.service.ServiceException) EventException(org.b3log.latke.event.EventException) JSONArray(org.json.JSONArray) RepositoryException(org.b3log.latke.repository.RepositoryException) Date(java.util.Date) ArchiveDate(org.b3log.solo.model.ArchiveDate)

Example 92 with ServiceException

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

the class ArticleMgmtService method unArchiveDate.

/**
     * Un-archive an article specified by the given specified article id.
     *
     * @param articleId the given article id
     * @throws ServiceException service exception
     */
private void unArchiveDate(final String articleId) throws ServiceException {
    try {
        final JSONObject archiveDateArticleRelation = archiveDateArticleRepository.getByArticleId(articleId);
        final String archiveDateId = archiveDateArticleRelation.getString(ArchiveDate.ARCHIVE_DATE + "_" + Keys.OBJECT_ID);
        final JSONObject archiveDate = archiveDateRepository.get(archiveDateId);
        int archiveDateArticleCnt = archiveDate.getInt(ArchiveDate.ARCHIVE_DATE_ARTICLE_COUNT);
        --archiveDateArticleCnt;
        int archiveDatePublishedArticleCnt = archiveDate.getInt(ArchiveDate.ARCHIVE_DATE_PUBLISHED_ARTICLE_COUNT);
        final JSONObject article = articleRepository.get(articleId);
        if (article.getBoolean(Article.ARTICLE_IS_PUBLISHED)) {
            --archiveDatePublishedArticleCnt;
        }
        if (0 == archiveDateArticleCnt) {
            archiveDateRepository.remove(archiveDateId);
        } else {
            final JSONObject newArchiveDate = new JSONObject(archiveDate, CollectionUtils.jsonArrayToArray(archiveDate.names(), String[].class));
            newArchiveDate.put(ArchiveDate.ARCHIVE_DATE_ARTICLE_COUNT, archiveDateArticleCnt);
            newArchiveDate.put(ArchiveDate.ARCHIVE_DATE_PUBLISHED_ARTICLE_COUNT, archiveDatePublishedArticleCnt);
            archiveDateRepository.update(archiveDateId, newArchiveDate);
        }
        archiveDateArticleRepository.remove(archiveDateArticleRelation.getString(Keys.OBJECT_ID));
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, "Unarchive date for article[id=" + articleId + "] failed", e);
        throw new ServiceException(e);
    }
}
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 93 with ServiceException

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

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

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

Aggregations

ServiceException (org.b3log.latke.service.ServiceException)102 JSONObject (org.json.JSONObject)91 JSONException (org.json.JSONException)45 RepositoryException (org.b3log.latke.repository.RepositoryException)34 Transaction (org.b3log.latke.repository.Transaction)31 RequestProcessing (org.b3log.latke.servlet.annotation.RequestProcessing)23 JSONArray (org.json.JSONArray)17 JSONRenderer (org.b3log.latke.servlet.renderer.JSONRenderer)16 EventException (org.b3log.latke.event.EventException)14 IOException (java.io.IOException)11 ParseException (java.text.ParseException)10 ArrayList (java.util.ArrayList)8 Query (org.b3log.latke.repository.Query)8 Date (java.util.Date)7 AbstractFreeMarkerRenderer (org.b3log.latke.servlet.renderer.freemarker.AbstractFreeMarkerRenderer)6 Template (freemarker.template.Template)3 FreeMarkerRenderer (org.b3log.latke.servlet.renderer.freemarker.FreeMarkerRenderer)3 ArchiveDate (org.b3log.solo.model.ArchiveDate)3 URL (java.net.URL)2 HashMap (java.util.HashMap)2