Search in sources :

Example 31 with Query

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

the class UpgradeService method upgradeUsers.

/**
     * Upgrades users.
     * <p>
     * Password hashing.
     * </p>
     *
     * @throws Exception exception
     */
private void upgradeUsers() throws Exception {
    final JSONArray users = userRepository.get(new Query()).getJSONArray(Keys.RESULTS);
    for (int i = 0; i < users.length(); i++) {
        final JSONObject user = users.getJSONObject(i);
        final String email = user.optString(User.USER_EMAIL);
        user.put(UserExt.USER_AVATAR, Thumbnails.getGravatarURL(email, "128"));
        userRepository.update(user.optString(Keys.OBJECT_ID), user);
        LOGGER.log(Level.INFO, "Updated user[email={0}]", email);
    }
}
Also used : Query(org.b3log.latke.repository.Query) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray)

Example 32 with Query

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

the class UserQueryService method getUsers.

/**
     * Gets users 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]
     *     },
     *     "users": [{
     *         "oId": "",
     *         "userName": "",
     *         "userEmail": "",
     *         "userPassword": "",
     *         "roleName": ""
     *      }, ....]
     * }
     * </pre>
     * @throws ServiceException service exception
     * @see Pagination
     */
public JSONObject getUsers(final JSONObject requestJSONObject) throws ServiceException {
    final JSONObject ret = new JSONObject();
    final int currentPageNum = requestJSONObject.optInt(Pagination.PAGINATION_CURRENT_PAGE_NUM);
    final int pageSize = requestJSONObject.optInt(Pagination.PAGINATION_PAGE_SIZE);
    final int windowSize = requestJSONObject.optInt(Pagination.PAGINATION_WINDOW_SIZE);
    final Query query = new Query().setCurrentPageNum(currentPageNum).setPageSize(pageSize);
    JSONObject result = null;
    try {
        result = userRepository.get(query);
    } catch (final RepositoryException e) {
        LOGGER.log(Level.ERROR, "Gets users failed", e);
        throw new ServiceException(e);
    }
    final int pageCount = result.optJSONObject(Pagination.PAGINATION).optInt(Pagination.PAGINATION_PAGE_COUNT);
    final JSONObject pagination = new JSONObject();
    ret.put(Pagination.PAGINATION, pagination);
    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 users = result.optJSONArray(Keys.RESULTS);
    ret.put(User.USERS, users);
    return ret;
}
Also used : JSONObject(org.json.JSONObject) Query(org.b3log.latke.repository.Query) ServiceException(org.b3log.latke.service.ServiceException) JSONArray(org.json.JSONArray) RepositoryException(org.b3log.latke.repository.RepositoryException)

Example 33 with Query

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

the class FeedProcessorTestCase method tagArticlesRSS.

/**
     * tagArticlesRSS.
     *
     * @throws Exception exception
     */
@Test(dependsOnMethods = "init")
public void tagArticlesRSS() throws Exception {
    final HttpServletRequest request = mock(HttpServletRequest.class);
    when(request.getServletContext()).thenReturn(mock(ServletContext.class));
    when(request.getRequestURI()).thenReturn("/tag-articles-rss.do");
    when(request.getMethod()).thenReturn("GET");
    final JSONObject tag = getTagRepository().get(new Query()).optJSONArray(Keys.RESULTS).optJSONObject(0);
    when(request.getQueryString()).thenReturn("tag=" + tag.optString(Keys.OBJECT_ID));
    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.startsWith(content, "<?xml version=\"1.0\""));
}
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 34 with Query

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

the class SitemapProcessor method addArticles.

/**
     * Adds articles into the specified sitemap.
     * 
     * @param sitemap the specified sitemap
     * @throws Exception exception
     */
private void addArticles(final Sitemap sitemap) throws Exception {
    // XXX: query all articles?
    final Query query = new Query().setCurrentPageNum(1).setFilter(new PropertyFilter(Article.ARTICLE_IS_PUBLISHED, FilterOperator.EQUAL, true)).addSort(Article.ARTICLE_CREATE_DATE, SortDirection.DESCENDING);
    // XXX: maybe out of memory 
    final JSONObject articleResult = articleRepository.get(query);
    final JSONArray articles = articleResult.getJSONArray(Keys.RESULTS);
    for (int i = 0; i < articles.length(); i++) {
        final JSONObject article = articles.getJSONObject(i);
        final String permalink = article.getString(Article.ARTICLE_PERMALINK);
        final URL url = new URL();
        url.setLoc(Latkes.getServePath() + permalink);
        final Date updateDate = (Date) article.get(Article.ARTICLE_UPDATE_DATE);
        final String lastMod = DateFormatUtils.ISO_DATETIME_TIME_ZONE_FORMAT.format(updateDate);
        url.setLastMod(lastMod);
        sitemap.addURL(url);
    }
}
Also used : Query(org.b3log.latke.repository.Query) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) PropertyFilter(org.b3log.latke.repository.PropertyFilter) URL(org.b3log.solo.model.sitemap.URL) Date(java.util.Date) ArchiveDate(org.b3log.solo.model.ArchiveDate)

Example 35 with Query

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

the class SitemapProcessor method addArchives.

/**
     * Adds archives (archive-articles) into the specified sitemap.
     * 
     * @param sitemap the specified sitemap
     * @throws Exception exception
     */
private void addArchives(final Sitemap sitemap) throws Exception {
    final JSONObject result = archiveDateRepository.get(new Query());
    final JSONArray archiveDates = result.getJSONArray(Keys.RESULTS);
    for (int i = 0; i < archiveDates.length(); i++) {
        final JSONObject archiveDate = archiveDates.getJSONObject(i);
        final long time = archiveDate.getLong(ArchiveDate.ARCHIVE_TIME);
        final String dateString = DateFormatUtils.format(time, "yyyy/MM");
        final URL url = new URL();
        url.setLoc(Latkes.getServePath() + "/archives/" + dateString);
        sitemap.addURL(url);
    }
}
Also used : JSONObject(org.json.JSONObject) Query(org.b3log.latke.repository.Query) JSONArray(org.json.JSONArray) URL(org.b3log.solo.model.sitemap.URL)

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