Search in sources :

Example 6 with ArticleRepository

use of org.b3log.solo.repository.ArticleRepository in project solo by b3log.

the class ArticleRepositoryImplTestCase method getRandomly.

/**
     * Get Randomly.
     * 
     * @throws Exception exception
     */
@Test(dependsOnMethods = { "add", "previousAndNext", "getMostCommentArticles", "getMostViewCountArticles" })
public void getRandomly() throws Exception {
    final ArticleRepository articleRepository = getArticleRepository();
    List<JSONObject> articles = articleRepository.getRandomly(3);
    Assert.assertNotNull(articles);
}
Also used : JSONObject(org.json.JSONObject) ArticleRepository(org.b3log.solo.repository.ArticleRepository) Test(org.testng.annotations.Test)

Example 7 with ArticleRepository

use of org.b3log.solo.repository.ArticleRepository in project solo by b3log.

the class ArticleRepositoryImplTestCase method add.

/**
     * Adds successfully.
     * 
     * @throws Exception exception
     */
@Test
public void add() throws Exception {
    final ArticleRepository articleRepository = getArticleRepository();
    final JSONObject article = new JSONObject();
    article.put(Article.ARTICLE_TITLE, "article title1");
    article.put(Article.ARTICLE_ABSTRACT, "article abstract");
    article.put(Article.ARTICLE_TAGS_REF, "tag1, tag2");
    article.put(Article.ARTICLE_AUTHOR_EMAIL, "test@gmail.com");
    article.put(Article.ARTICLE_COMMENT_COUNT, 0);
    article.put(Article.ARTICLE_VIEW_COUNT, 0);
    article.put(Article.ARTICLE_CONTENT, "article content");
    article.put(Article.ARTICLE_PERMALINK, "article permalink1");
    article.put(Article.ARTICLE_HAD_BEEN_PUBLISHED, true);
    article.put(Article.ARTICLE_IS_PUBLISHED, true);
    article.put(Article.ARTICLE_PUT_TOP, false);
    article.put(Article.ARTICLE_CREATE_DATE, new Date());
    article.put(Article.ARTICLE_UPDATE_DATE, new Date());
    article.put(Article.ARTICLE_RANDOM_DOUBLE, Math.random());
    article.put(Article.ARTICLE_SIGN_ID, "1");
    article.put(Article.ARTICLE_COMMENTABLE, true);
    article.put(Article.ARTICLE_VIEW_PWD, "");
    article.put(Article.ARTICLE_EDITOR_TYPE, "");
    final Transaction transaction = articleRepository.beginTransaction();
    articleRepository.add(article);
    transaction.commit();
    final JSONArray results = articleRepository.getByAuthorEmail("test@gmail.com", 1, Integer.MAX_VALUE).getJSONArray(Keys.RESULTS);
    Assert.assertEquals(results.length(), 1);
}
Also used : JSONObject(org.json.JSONObject) Transaction(org.b3log.latke.repository.Transaction) JSONArray(org.json.JSONArray) ArticleRepository(org.b3log.solo.repository.ArticleRepository) Date(java.util.Date) Test(org.testng.annotations.Test)

Example 8 with ArticleRepository

use of org.b3log.solo.repository.ArticleRepository in project solo by b3log.

the class ArticleRepositoryImplTestCase method getMostViewCountArticles.

/**
     * Get Most View Count Articles 
     * 
     * @throws Exception exception
     */
@Test(dependsOnMethods = { "add", "previousAndNext", "getMostCommentArticles" })
public void getMostViewCountArticles() throws Exception {
    final ArticleRepository articleRepository = getArticleRepository();
    final JSONObject article = new JSONObject();
    article.put(Article.ARTICLE_TITLE, "article title4");
    article.put(Article.ARTICLE_ABSTRACT, "article abstract");
    article.put(Article.ARTICLE_TAGS_REF, "tag1, tag2");
    article.put(Article.ARTICLE_AUTHOR_EMAIL, "test@gmail.com");
    article.put(Article.ARTICLE_COMMENT_COUNT, 3);
    article.put(Article.ARTICLE_VIEW_COUNT, 3);
    article.put(Article.ARTICLE_CONTENT, "article content");
    article.put(Article.ARTICLE_PERMALINK, "article permalink4");
    article.put(Article.ARTICLE_HAD_BEEN_PUBLISHED, false);
    // Unpublished
    article.put(Article.ARTICLE_IS_PUBLISHED, false);
    article.put(Article.ARTICLE_PUT_TOP, false);
    article.put(Article.ARTICLE_CREATE_DATE, new Date());
    article.put(Article.ARTICLE_UPDATE_DATE, new Date());
    article.put(Article.ARTICLE_RANDOM_DOUBLE, Math.random());
    article.put(Article.ARTICLE_SIGN_ID, "1");
    article.put(Article.ARTICLE_COMMENTABLE, true);
    article.put(Article.ARTICLE_VIEW_PWD, "");
    article.put(Article.ARTICLE_EDITOR_TYPE, "");
    final Transaction transaction = articleRepository.beginTransaction();
    articleRepository.add(article);
    transaction.commit();
    List<JSONObject> mostViewCountArticles = articleRepository.getMostViewCountArticles(2);
    Assert.assertNotNull(mostViewCountArticles);
    Assert.assertEquals(mostViewCountArticles.size(), 2);
    Assert.assertEquals(mostViewCountArticles.get(0).getInt(Article.ARTICLE_VIEW_COUNT), 2);
    Assert.assertEquals(mostViewCountArticles.get(1).getInt(Article.ARTICLE_VIEW_COUNT), 1);
    mostViewCountArticles = articleRepository.getMostViewCountArticles(1);
    Assert.assertNotNull(mostViewCountArticles);
    Assert.assertEquals(mostViewCountArticles.size(), 1);
    Assert.assertEquals(mostViewCountArticles.get(0).getInt(Article.ARTICLE_VIEW_COUNT), 2);
}
Also used : JSONObject(org.json.JSONObject) Transaction(org.b3log.latke.repository.Transaction) ArticleRepository(org.b3log.solo.repository.ArticleRepository) Date(java.util.Date) Test(org.testng.annotations.Test)

Example 9 with ArticleRepository

use of org.b3log.solo.repository.ArticleRepository 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

ArticleRepository (org.b3log.solo.repository.ArticleRepository)9 JSONObject (org.json.JSONObject)9 Test (org.testng.annotations.Test)8 Date (java.util.Date)4 Transaction (org.b3log.latke.repository.Transaction)4 JSONArray (org.json.JSONArray)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 Query (org.b3log.latke.repository.Query)1 RepositoryException (org.b3log.latke.repository.RepositoryException)1 PageRepository (org.b3log.solo.repository.PageRepository)1 ArticleQueryService (org.b3log.solo.service.ArticleQueryService)1