Search in sources :

Example 31 with RepositoryException

use of org.b3log.latke.repository.RepositoryException 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 32 with RepositoryException

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

the class ArticleMgmtService method archiveDate.

/**
     * Archive the create date with the specified article.
     *
     * @param article the specified article, for example,
     * <pre>
     * {
     *     ....,
     *     "oId": "",
     *     "articleCreateDate": java.util.Date,
     *     ....
     * }
     * </pre>
     * @throws RepositoryException repository exception
     */
private void archiveDate(final JSONObject article) throws RepositoryException {
    final Date createDate = (Date) article.opt(Article.ARTICLE_CREATE_DATE);
    final String createDateString = DateFormatUtils.format(createDate, "yyyy/MM");
    JSONObject archiveDate = archiveDateRepository.getByArchiveDate(createDateString);
    if (null == archiveDate) {
        archiveDate = new JSONObject();
        try {
            archiveDate.put(ArchiveDate.ARCHIVE_TIME, DateUtils.parseDate(createDateString, new String[] { "yyyy/MM" }).getTime());
            archiveDate.put(ArchiveDate.ARCHIVE_DATE_ARTICLE_COUNT, 0);
            archiveDate.put(ArchiveDate.ARCHIVE_DATE_PUBLISHED_ARTICLE_COUNT, 0);
            archiveDateRepository.add(archiveDate);
        } catch (final ParseException e) {
            LOGGER.log(Level.ERROR, e.getMessage(), e);
            throw new RepositoryException(e);
        }
    }
    final JSONObject newArchiveDate = new JSONObject(archiveDate, CollectionUtils.jsonArrayToArray(archiveDate.names(), String[].class));
    newArchiveDate.put(ArchiveDate.ARCHIVE_DATE_ARTICLE_COUNT, archiveDate.optInt(ArchiveDate.ARCHIVE_DATE_ARTICLE_COUNT) + 1);
    if (article.optBoolean(Article.ARTICLE_IS_PUBLISHED)) {
        newArchiveDate.put(ArchiveDate.ARCHIVE_DATE_PUBLISHED_ARTICLE_COUNT, archiveDate.optInt(ArchiveDate.ARCHIVE_DATE_PUBLISHED_ARTICLE_COUNT) + 1);
    }
    archiveDateRepository.update(archiveDate.optString(Keys.OBJECT_ID), newArchiveDate);
    final JSONObject archiveDateArticleRelation = new JSONObject();
    archiveDateArticleRelation.put(ArchiveDate.ARCHIVE_DATE + "_" + Keys.OBJECT_ID, archiveDate.optString(Keys.OBJECT_ID));
    archiveDateArticleRelation.put(Article.ARTICLE + "_" + Keys.OBJECT_ID, article.optString(Keys.OBJECT_ID));
    archiveDateArticleRepository.add(archiveDateArticleRelation);
}
Also used : JSONObject(org.json.JSONObject) RepositoryException(org.b3log.latke.repository.RepositoryException) ParseException(java.text.ParseException) Date(java.util.Date) ArchiveDate(org.b3log.solo.model.ArchiveDate)

Example 33 with RepositoryException

use of org.b3log.latke.repository.RepositoryException 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)

Example 34 with RepositoryException

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

the class ArticleRepositoryImpl method getPreviousArticle.

@Override
public JSONObject getPreviousArticle(final String articleId) throws RepositoryException {
    final JSONObject currentArticle = get(articleId);
    final Date currentArticleCreateDate = (Date) currentArticle.opt(Article.ARTICLE_CREATE_DATE);
    final Query query = new Query().setFilter(CompositeFilterOperator.and(new PropertyFilter(Article.ARTICLE_CREATE_DATE, FilterOperator.LESS_THAN, currentArticleCreateDate), new PropertyFilter(Article.ARTICLE_IS_PUBLISHED, FilterOperator.EQUAL, true))).addSort(Article.ARTICLE_CREATE_DATE, SortDirection.DESCENDING).setCurrentPageNum(1).setPageSize(1).setPageCount(1).addProjection(Article.ARTICLE_TITLE, String.class).addProjection(Article.ARTICLE_PERMALINK, String.class).addProjection(Article.ARTICLE_ABSTRACT, String.class);
    final JSONObject result = get(query);
    final JSONArray array = result.optJSONArray(Keys.RESULTS);
    if (1 != array.length()) {
        return null;
    }
    final JSONObject ret = new JSONObject();
    final JSONObject article = array.optJSONObject(0);
    try {
        ret.put(Article.ARTICLE_TITLE, article.getString(Article.ARTICLE_TITLE));
        ret.put(Article.ARTICLE_PERMALINK, article.getString(Article.ARTICLE_PERMALINK));
        ret.put(Article.ARTICLE_ABSTRACT, article.getString((Article.ARTICLE_ABSTRACT)));
    } catch (final JSONException e) {
        throw new RepositoryException(e);
    }
    return ret;
}
Also used : JSONObject(org.json.JSONObject) Query(org.b3log.latke.repository.Query) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) PropertyFilter(org.b3log.latke.repository.PropertyFilter) RepositoryException(org.b3log.latke.repository.RepositoryException) Date(java.util.Date)

Example 35 with RepositoryException

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

the class PermalinkFilter method doFilter.

/**
     * Tries to dispatch request to article processor.
     *
     * @param request the specified request
     * @param response the specified response
     * @param chain filter chain
     * @throws IOException io exception
     * @throws ServletException servlet exception
     */
@Override
public void doFilter(final ServletRequest request, final ServletResponse response, final FilterChain chain) throws IOException, ServletException {
    final HttpServletRequest httpServletRequest = (HttpServletRequest) request;
    final HttpServletResponse httpServletResponse = (HttpServletResponse) response;
    final String requestURI = httpServletRequest.getRequestURI();
    LOGGER.log(Level.DEBUG, "Request URI[{0}]", requestURI);
    final String contextPath = Latkes.getContextPath();
    final String permalink = StringUtils.substringAfter(requestURI, contextPath);
    if (PermalinkQueryService.invalidPermalinkFormat(permalink)) {
        LOGGER.log(Level.DEBUG, "Skip filter request[URI={0}]", permalink);
        chain.doFilter(request, response);
        return;
    }
    JSONObject article;
    JSONObject page = null;
    final LatkeBeanManager beanManager = Lifecycle.getBeanManager();
    try {
        final ArticleRepository articleRepository = beanManager.getReference(ArticleRepositoryImpl.class);
        article = articleRepository.getByPermalink(permalink);
        if (null == article) {
            final PageRepository pageRepository = beanManager.getReference(PageRepositoryImpl.class);
            page = pageRepository.getByPermalink(permalink);
        }
        if (null == page && null == article) {
            LOGGER.log(Level.DEBUG, "Not found article/page with permalink[{0}]", permalink);
            chain.doFilter(request, response);
            return;
        }
    } catch (final RepositoryException e) {
        LOGGER.log(Level.ERROR, "Processes article permalink filter failed", e);
        httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND);
        return;
    }
    // If requests an article and the article need view passowrd, sends redirect to the password form
    final ArticleQueryService articleQueryService = beanManager.getReference(ArticleQueryService.class);
    if (null != article && articleQueryService.needViewPwd(httpServletRequest, article)) {
        try {
            httpServletResponse.sendRedirect(Latkes.getServePath() + "/console/article-pwd?articleId=" + article.optString(Keys.OBJECT_ID));
            return;
        } catch (final Exception e) {
            httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
    }
    dispatchToArticleOrPageProcessor(request, response, article, page);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) JSONObject(org.json.JSONObject) PageRepository(org.b3log.solo.repository.PageRepository) ArticleQueryService(org.b3log.solo.service.ArticleQueryService) HttpServletResponse(javax.servlet.http.HttpServletResponse) ArticleRepository(org.b3log.solo.repository.ArticleRepository) RepositoryException(org.b3log.latke.repository.RepositoryException) ServletException(javax.servlet.ServletException) RepositoryException(org.b3log.latke.repository.RepositoryException) IOException(java.io.IOException) LatkeBeanManager(org.b3log.latke.ioc.LatkeBeanManager)

Aggregations

RepositoryException (org.b3log.latke.repository.RepositoryException)35 JSONObject (org.json.JSONObject)32 ServiceException (org.b3log.latke.service.ServiceException)14 JSONArray (org.json.JSONArray)10 Query (org.b3log.latke.repository.Query)8 Transaction (org.b3log.latke.repository.Transaction)8 Date (java.util.Date)5 ParseException (java.text.ParseException)4 PropertyFilter (org.b3log.latke.repository.PropertyFilter)4 JSONException (org.json.JSONException)4 EventException (org.b3log.latke.event.EventException)2 ArchiveDate (org.b3log.solo.model.ArchiveDate)2 IOException (java.io.IOException)1 ServletException (javax.servlet.ServletException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 LatkeBeanManager (org.b3log.latke.ioc.LatkeBeanManager)1 GeneralUser (org.b3log.latke.user.GeneralUser)1 ArticleRepository (org.b3log.solo.repository.ArticleRepository)1 PageRepository (org.b3log.solo.repository.PageRepository)1