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);
}
}
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;
}
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\""));
}
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);
}
}
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);
}
}
Aggregations