Search in sources :

Example 96 with Query

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

the class LinkQueryService method getLinks.

/**
 * Gets links by the specified request json object.
 *
 * @param requestJSONObject the specified request json object, for example,
 *                          "paginationCurrentPageNum": 1,
 *                          "paginationPageSize": 20,
 *                          "paginationWindowSize": 10
 *                          see {@link Pagination} for more details
 * @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().setPage(currentPageNum, 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 List<JSONObject> links = (List<JSONObject>) result.opt(Keys.RESULTS);
        ret.put(Pagination.PAGINATION, pagination);
        ret.put(Link.LINKS, (Object) 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) List(java.util.List) ServiceException(org.b3log.latke.service.ServiceException)

Example 97 with Query

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

the class ArticleProcessorTestCase method getRelevantArticles.

/**
 * getRelevantArticles.
 *
 * @throws Exception exception
 */
@Test
public void getRelevantArticles() throws Exception {
    final JSONObject article = getArticleRepository().getFirst(new Query());
    final String articleId = article.optString(Keys.OBJECT_ID);
    final MockRequest request = mockRequest();
    request.setRequestURI("/article/relevant/" + articleId + ".json");
    final MockResponse response = mockResponse();
    mockDispatcher(request, response);
    final String content = response.getString();
    Assert.assertTrue(StringUtils.contains(content, "{\"relevantArticles\""));
}
Also used : MockResponse(org.b3log.solo.MockResponse) JSONObject(org.json.JSONObject) Query(org.b3log.latke.repository.Query) MockRequest(org.b3log.solo.MockRequest) Test(org.testng.annotations.Test)

Example 98 with Query

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

the class ArticleConsoleTestCase method addArticle.

/**
 * addArticle.
 *
 * @throws Exception exception
 */
public void addArticle() throws Exception {
    final JSONObject article = getArticleRepository().getFirst(new Query());
    article.put(Keys.OBJECT_ID, "");
    article.put(Article.ARTICLE_PERMALINK, "");
    final MockRequest request = mockRequest();
    request.setRequestURI("/console/article/");
    request.setMethod("POST");
    final JSONObject requestJSON = new JSONObject();
    requestJSON.put(Article.ARTICLE, article);
    request.setJSON(requestJSON);
    mockAdminLogin(request);
    final MockResponse response = mockResponse();
    mockDispatcher(request, response);
    final String content = response.getString();
    Assert.assertTrue(StringUtils.contains(content, "\"code\":0"));
}
Also used : MockResponse(org.b3log.solo.MockResponse) JSONObject(org.json.JSONObject) Query(org.b3log.latke.repository.Query) MockRequest(org.b3log.solo.MockRequest)

Example 99 with Query

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

the class ArticleConsoleTestCase method putTopArticle.

/**
 * putTopArticle.
 *
 * @throws Exception exception
 */
public void putTopArticle() throws Exception {
    final JSONObject article = getArticleRepository().getFirst(new Query());
    final String articleId = article.optString(Keys.OBJECT_ID);
    final MockRequest request = mockRequest();
    request.setRequestURI("/console/article/puttop/" + articleId);
    request.setMethod("PUT");
    mockAdminLogin(request);
    final MockResponse response = mockResponse();
    mockDispatcher(request, response);
    final String content = response.getString();
    Assert.assertTrue(StringUtils.contains(content, "\"code\":0"));
}
Also used : MockResponse(org.b3log.solo.MockResponse) JSONObject(org.json.JSONObject) Query(org.b3log.latke.repository.Query) MockRequest(org.b3log.solo.MockRequest)

Example 100 with Query

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

the class ArticleConsoleTestCase method cancelPublishArticle.

/**
 * cancelPublishArticle.
 *
 * @throws Exception exception
 */
public void cancelPublishArticle() throws Exception {
    final JSONObject article = getArticleRepository().getFirst(new Query());
    final String articleId = article.optString(Keys.OBJECT_ID);
    final MockRequest request = mockRequest();
    request.setRequestURI("/console/article/unpublish/" + articleId);
    request.setMethod("PUT");
    mockAdminLogin(request);
    final MockResponse response = mockResponse();
    mockDispatcher(request, response);
    final String content = response.getString();
    Assert.assertTrue(StringUtils.contains(content, "\"code\":0"));
}
Also used : MockResponse(org.b3log.solo.MockResponse) JSONObject(org.json.JSONObject) Query(org.b3log.latke.repository.Query) MockRequest(org.b3log.solo.MockRequest)

Aggregations

Query (org.b3log.latke.repository.Query)112 JSONObject (org.json.JSONObject)105 JSONArray (org.json.JSONArray)56 PropertyFilter (org.b3log.latke.repository.PropertyFilter)47 Test (org.testng.annotations.Test)26 RepositoryException (org.b3log.latke.repository.RepositoryException)23 MockRequest (org.b3log.solo.MockRequest)23 MockResponse (org.b3log.solo.MockResponse)23 ServiceException (org.b3log.latke.service.ServiceException)14 List (java.util.List)10 Date (java.util.Date)8 URL (org.b3log.solo.model.sitemap.URL)8 HttpServletResponse (javax.servlet.http.HttpServletResponse)7 PrintWriter (java.io.PrintWriter)6 StringWriter (java.io.StringWriter)6 ArrayList (java.util.ArrayList)6 ServletContext (javax.servlet.ServletContext)6 HttpServletRequest (javax.servlet.http.HttpServletRequest)6 Transactional (org.b3log.latke.repository.annotation.Transactional)6 JSONException (org.json.JSONException)6