Search in sources :

Example 6 with Query

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

the class UpgradeService method upgradeArticles.

/**
     * Upgrades articles.
     *
     * @throws Exception exception
     */
private void upgradeArticles() throws Exception {
    LOGGER.log(Level.INFO, "Adds a property [articleEditorType] to each of articles");
    final JSONArray articles = articleRepository.get(new Query()).getJSONArray(Keys.RESULTS);
    if (articles.length() <= 0) {
        LOGGER.log(Level.TRACE, "No articles");
        return;
    }
    Transaction transaction = null;
    try {
        for (int i = 0; i < articles.length(); i++) {
            if (0 == i % STEP || !transaction.isActive()) {
                transaction = userRepository.beginTransaction();
            }
            final JSONObject article = articles.getJSONObject(i);
            final String articleId = article.optString(Keys.OBJECT_ID);
            LOGGER.log(Level.INFO, "Found an article[id={0}]", articleId);
            article.put(Article.ARTICLE_EDITOR_TYPE, "tinyMCE");
            articleRepository.update(article.getString(Keys.OBJECT_ID), article);
            if (0 == i % STEP) {
                transaction.commit();
                LOGGER.log(Level.TRACE, "Updated some articles");
            }
        }
        if (transaction.isActive()) {
            transaction.commit();
        }
        LOGGER.log(Level.TRACE, "Updated all articles");
    } catch (final Exception e) {
        if (transaction.isActive()) {
            transaction.rollback();
        }
        throw e;
    }
}
Also used : Query(org.b3log.latke.repository.Query) Transaction(org.b3log.latke.repository.Transaction) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) ServiceException(org.b3log.latke.service.ServiceException) JSONException(org.json.JSONException) IOException(java.io.IOException)

Example 7 with Query

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

the class LinkQueryService method getLinks.

/**
     * Gets links by the specified request json object.
     *
     * @param requestJSONObject the specified request json object, for example,
     * <pre>
     * {
     *     "paginationCurrentPageNum": 1,
     *     "paginationPageSize": 20,
     *     "paginationWindowSize": 10
     * }, see {@link Pagination} for more details
     * </pre>
     * @return for example,
     * <pre>
     * {
     *     "pagination": {
     *         "paginationPageCount": 100,
     *         "paginationPageNums": [1, 2, 3, 4, 5]
     *     },
     *     "links": [{
     *         "oId": "",
     *         "linkTitle": "",
     *         "linkAddress": "",
     *         ""linkDescription": ""
     *      }, ....]
     * }
     * </pre>
     * @throws ServiceException service exception
     * @see Pagination
     */
public JSONObject getLinks(final JSONObject requestJSONObject) throws ServiceException {
    final JSONObject ret = new JSONObject();
    try {
        final int currentPageNum = requestJSONObject.getInt(Pagination.PAGINATION_CURRENT_PAGE_NUM);
        final int pageSize = requestJSONObject.getInt(Pagination.PAGINATION_PAGE_SIZE);
        final int windowSize = requestJSONObject.getInt(Pagination.PAGINATION_WINDOW_SIZE);
        final Query query = new Query().setCurrentPageNum(currentPageNum).setPageSize(pageSize).addSort(Link.LINK_ORDER, SortDirection.ASCENDING);
        final JSONObject result = linkRepository.get(query);
        final int pageCount = result.getJSONObject(Pagination.PAGINATION).getInt(Pagination.PAGINATION_PAGE_COUNT);
        final JSONObject pagination = new JSONObject();
        final List<Integer> pageNums = Paginator.paginate(currentPageNum, pageSize, pageCount, windowSize);
        pagination.put(Pagination.PAGINATION_PAGE_COUNT, pageCount);
        pagination.put(Pagination.PAGINATION_PAGE_NUMS, pageNums);
        final JSONArray links = result.getJSONArray(Keys.RESULTS);
        ret.put(Pagination.PAGINATION, pagination);
        ret.put(Link.LINKS, links);
        return ret;
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, "Gets links failed", e);
        throw new ServiceException(e);
    }
}
Also used : JSONObject(org.json.JSONObject) Query(org.b3log.latke.repository.Query) ServiceException(org.b3log.latke.service.ServiceException) JSONArray(org.json.JSONArray) ServiceException(org.b3log.latke.service.ServiceException)

Example 8 with Query

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

the class ArticleProcessorTestCase method showArticle.

/**
     * showArticle.
     *
     * @throws Exception exception
     */
@Test(dependsOnMethods = "init")
public void showArticle() throws Exception {
    final JSONObject article = getArticleRepository().get(new Query()).optJSONArray(Keys.RESULTS).optJSONObject(0);
    final HttpServletRequest request = mock(HttpServletRequest.class);
    when(request.getServletContext()).thenReturn(mock(ServletContext.class));
    when(request.getRequestURI()).thenReturn("/pagepermalink");
    when(request.getMethod()).thenReturn("GET");
    when(request.getAttribute(Keys.TEMAPLTE_DIR_NAME)).thenReturn(Option.DefaultPreference.DEFAULT_SKIN_DIR_NAME);
    when(request.getAttribute(Keys.HttpRequest.START_TIME_MILLIS)).thenReturn(System.currentTimeMillis());
    when(request.getAttribute(Article.ARTICLE)).thenReturn(article);
    final StringWriter stringWriter = new StringWriter();
    final PrintWriter printWriter = new PrintWriter(stringWriter);
    final HttpServletResponse response = mock(HttpServletResponse.class);
    when(response.getWriter()).thenReturn(printWriter);
    final HTTPRequestContext httpRequestContext = new HTTPRequestContext();
    httpRequestContext.setRequest(request);
    httpRequestContext.setResponse(response);
    final ArticleProcessor articleProcessor = Lifecycle.getBeanManager().getReference(ArticleProcessor.class);
    articleProcessor.showArticle(httpRequestContext, request, response);
    final Map<String, Object> dataModel = httpRequestContext.getRenderer().getRenderDataModel();
    final JSONObject handledArticle = (JSONObject) dataModel.get(Article.ARTICLE);
    Assert.assertTrue(StringUtils.contains(handledArticle.optString(Keys.OBJECT_ID), article.optString(Keys.OBJECT_ID)));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) JSONObject(org.json.JSONObject) Query(org.b3log.latke.repository.Query) StringWriter(java.io.StringWriter) HTTPRequestContext(org.b3log.latke.servlet.HTTPRequestContext) ServletContext(javax.servlet.ServletContext) HttpServletResponse(javax.servlet.http.HttpServletResponse) JSONObject(org.json.JSONObject) PrintWriter(java.io.PrintWriter) Test(org.testng.annotations.Test)

Example 9 with Query

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

the class ArticleProcessorTestCase method showArticlePwdForm.

/**
     * showArticlePwdForm.
     *
     * @throws Exception exception
     */
@Test(dependsOnMethods = "init")
public void showArticlePwdForm() throws Exception {
    final JSONObject article = getArticleRepository().get(new Query()).optJSONArray(Keys.RESULTS).optJSONObject(0);
    final String articleId = article.optString(Keys.OBJECT_ID);
    final HttpServletRequest request = mock(HttpServletRequest.class);
    when(request.getServletContext()).thenReturn(mock(ServletContext.class));
    when(request.getRequestURI()).thenReturn("/console/article-pwd");
    when(request.getMethod()).thenReturn("GET");
    when(request.getParameter("articleId")).thenReturn(articleId);
    when(request.getAttribute(Keys.TEMAPLTE_DIR_NAME)).thenReturn(Option.DefaultPreference.DEFAULT_SKIN_DIR_NAME);
    when(request.getAttribute(Keys.HttpRequest.START_TIME_MILLIS)).thenReturn(System.currentTimeMillis());
    final MockDispatcherServlet dispatcherServlet = new MockDispatcherServlet();
    dispatcherServlet.init();
    final StringWriter stringWriter = new StringWriter();
    final PrintWriter printWriter = new PrintWriter(stringWriter);
    final HttpServletResponse response = mock(HttpServletResponse.class);
    when(response.getWriter()).thenReturn(printWriter);
    dispatcherServlet.service(request, response);
    final String content = stringWriter.toString();
    Assert.assertTrue(StringUtils.contains(content, "<title>访问密码</title>"));
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) JSONObject(org.json.JSONObject) Query(org.b3log.latke.repository.Query) StringWriter(java.io.StringWriter) ServletContext(javax.servlet.ServletContext) HttpServletResponse(javax.servlet.http.HttpServletResponse) PrintWriter(java.io.PrintWriter) Test(org.testng.annotations.Test)

Example 10 with Query

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

the class TagQueryService method getTags.

/**
     * Gets all tags.
     *
     * @return for example,      <pre>
     * [
     *     {"tagTitle": "", "tagReferenceCount": int, ....},
     *     ....
     * ]
     * </pre>, returns an empty list if not found
     *
     * @throws ServiceException service exception
     */
public List<JSONObject> getTags() throws ServiceException {
    try {
        final Query query = new Query().setPageCount(1);
        final JSONObject result = tagRepository.get(query);
        final JSONArray tagArray = result.optJSONArray(Keys.RESULTS);
        return CollectionUtils.jsonArrayToList(tagArray);
    } catch (final RepositoryException e) {
        LOGGER.log(Level.ERROR, "Gets tags failed", e);
        throw new ServiceException(e);
    }
}
Also used : Query(org.b3log.latke.repository.Query) JSONObject(org.json.JSONObject) ServiceException(org.b3log.latke.service.ServiceException) JSONArray(org.json.JSONArray) RepositoryException(org.b3log.latke.repository.RepositoryException)

Aggregations

Query (org.b3log.latke.repository.Query)54 JSONObject (org.json.JSONObject)54 JSONArray (org.json.JSONArray)47 PropertyFilter (org.b3log.latke.repository.PropertyFilter)28 RepositoryException (org.b3log.latke.repository.RepositoryException)9 ServiceException (org.b3log.latke.service.ServiceException)9 Test (org.testng.annotations.Test)8 HttpServletResponse (javax.servlet.http.HttpServletResponse)7 PrintWriter (java.io.PrintWriter)6 StringWriter (java.io.StringWriter)6 ServletContext (javax.servlet.ServletContext)6 HttpServletRequest (javax.servlet.http.HttpServletRequest)6 Date (java.util.Date)5 URL (org.b3log.solo.model.sitemap.URL)4 JSONException (org.json.JSONException)4 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 CompositeFilter (org.b3log.latke.repository.CompositeFilter)2 Filter (org.b3log.latke.repository.Filter)2 Transaction (org.b3log.latke.repository.Transaction)2