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