Search in sources :

Example 81 with Query

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

the class LinkRepositoryImpl method getByOrder.

@Override
public JSONObject getByOrder(final int order) throws RepositoryException {
    final Query query = new Query();
    query.setFilter(new PropertyFilter(Link.LINK_ORDER, FilterOperator.EQUAL, order));
    final JSONObject result = get(query);
    final JSONArray array = result.optJSONArray(Keys.RESULTS);
    if (0 == array.length()) {
        return null;
    }
    return array.optJSONObject(0);
}
Also used : Query(org.b3log.latke.repository.Query) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) PropertyFilter(org.b3log.latke.repository.PropertyFilter)

Example 82 with Query

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

the class LinkRepositoryImpl method getUpper.

@Override
public JSONObject getUpper(final String id) throws RepositoryException {
    final JSONObject link = get(id);
    if (null == link) {
        return null;
    }
    final Query query = new Query();
    query.setFilter(new PropertyFilter(Link.LINK_ORDER, FilterOperator.LESS_THAN, link.optInt(Link.LINK_ORDER))).addSort(Link.LINK_ORDER, SortDirection.DESCENDING);
    query.setCurrentPageNum(1);
    query.setPageSize(1);
    final JSONObject result = get(query);
    final JSONArray array = result.optJSONArray(Keys.RESULTS);
    if (1 != array.length()) {
        return null;
    }
    return array.optJSONObject(0);
}
Also used : JSONObject(org.json.JSONObject) Query(org.b3log.latke.repository.Query) JSONArray(org.json.JSONArray) PropertyFilter(org.b3log.latke.repository.PropertyFilter)

Example 83 with Query

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

the class PageRepositoryImpl method getUpper.

@Override
public JSONObject getUpper(final String id) throws RepositoryException {
    final JSONObject page = get(id);
    if (null == page) {
        return null;
    }
    final Query query = new Query().setFilter(new PropertyFilter(Page.PAGE_ORDER, FilterOperator.LESS_THAN, page.optInt(Page.PAGE_ORDER))).addSort(Page.PAGE_ORDER, SortDirection.DESCENDING).setCurrentPageNum(1).setPageSize(1).setPageCount(1);
    final JSONObject result = get(query);
    final JSONArray array = result.optJSONArray(Keys.RESULTS);
    if (1 != array.length()) {
        return null;
    }
    return array.optJSONObject(0);
}
Also used : JSONObject(org.json.JSONObject) Query(org.b3log.latke.repository.Query) JSONArray(org.json.JSONArray) PropertyFilter(org.b3log.latke.repository.PropertyFilter)

Example 84 with Query

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

the class CommentQueryService method getComments.

/**
     * Gets comments with the specified request json object, request and response.
     *
     * @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>
     * {
     *     "comments": [{
     *         "oId": "",
     *         "commentTitle": "",
     *         "commentName": "",
     *         "commentEmail": "",
     *         "thumbnailUrl": "",
     *         "commentURL": "",
     *         "commentContent": "",
     *         "commentTime": long,
     *         "commentSharpURL": ""
     *      }, ....]
     *     "sc": "GET_COMMENTS_SUCC"
     * }
     * </pre>
     *
     * @throws ServiceException service exception
     * @see Pagination
     */
public JSONObject getComments(final JSONObject requestJSONObject) throws ServiceException {
    try {
        final JSONObject ret = new JSONObject();
        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(Comment.COMMENT_DATE, SortDirection.DESCENDING);
        final JSONObject result = commentRepository.get(query);
        final JSONArray comments = result.getJSONArray(Keys.RESULTS);
        // Sets comment title and content escaping
        for (int i = 0; i < comments.length(); i++) {
            final JSONObject comment = comments.getJSONObject(i);
            String title;
            final String onType = comment.getString(Comment.COMMENT_ON_TYPE);
            final String onId = comment.getString(Comment.COMMENT_ON_ID);
            if (Article.ARTICLE.equals(onType)) {
                final JSONObject article = articleRepository.get(onId);
                title = article.getString(Article.ARTICLE_TITLE);
                comment.put(Common.TYPE, Common.ARTICLE_COMMENT_TYPE);
            } else {
                // It's a comment of page
                final JSONObject page = pageRepository.get(onId);
                title = page.getString(Page.PAGE_TITLE);
                comment.put(Common.TYPE, Common.PAGE_COMMENT_TYPE);
            }
            comment.put(Common.COMMENT_TITLE, title);
            String commentContent = comment.optString(Comment.COMMENT_CONTENT);
            commentContent = Emotions.convert(commentContent);
            commentContent = Markdowns.toHTML(commentContent);
            comment.put(Comment.COMMENT_CONTENT, commentContent);
            comment.put(Comment.COMMENT_TIME, ((Date) comment.get(Comment.COMMENT_DATE)).getTime());
            comment.remove(Comment.COMMENT_DATE);
        }
        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);
        ret.put(Comment.COMMENTS, comments);
        ret.put(Pagination.PAGINATION, pagination);
        return ret;
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, "Gets comments 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 85 with Query

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

the class FeedProcessor method blogArticlesRSS.

/**
     * Blog articles RSS output.
     *
     * @param context the specified context
     */
@RequestProcessing(value = { "/blog-articles-rss.do" }, method = { HTTPRequestMethod.GET, HTTPRequestMethod.HEAD })
public void blogArticlesRSS(final HTTPRequestContext context) {
    final HttpServletResponse response = context.getResponse();
    final RssRenderer renderer = new RssRenderer();
    context.setRenderer(renderer);
    final Channel channel = new Channel();
    try {
        final JSONObject preference = preferenceQueryService.getPreference();
        if (null == preference) {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
        final String blogTitle = preference.getString(Option.ID_C_BLOG_TITLE);
        final String blogSubtitle = preference.getString(Option.ID_C_BLOG_SUBTITLE);
        final int outputCnt = preference.getInt(Option.ID_C_FEED_OUTPUT_CNT);
        channel.setTitle(StringEscapeUtils.escapeXml(blogTitle));
        channel.setLastBuildDate(new Date());
        channel.setLink(Latkes.getServePath());
        channel.setAtomLink(Latkes.getServePath() + "/blog-articles-rss.do");
        channel.setGenerator("Solo, ver " + SoloServletListener.VERSION);
        final String localeString = preference.getString(Option.ID_C_LOCALE_STRING);
        final String country = Locales.getCountry(localeString).toLowerCase();
        final String language = Locales.getLanguage(localeString).toLowerCase();
        channel.setLanguage(language + '-' + country);
        channel.setDescription(blogSubtitle);
        final List<Filter> filters = new ArrayList<Filter>();
        filters.add(new PropertyFilter(Article.ARTICLE_IS_PUBLISHED, FilterOperator.EQUAL, true));
        filters.add(new PropertyFilter(Article.ARTICLE_VIEW_PWD, FilterOperator.EQUAL, ""));
        final Query query = new Query().setCurrentPageNum(1).setPageSize(outputCnt).setFilter(new CompositeFilter(CompositeFilterOperator.AND, filters)).addSort(Article.ARTICLE_UPDATE_DATE, SortDirection.DESCENDING).setPageCount(1);
        final JSONObject articleResult = articleRepository.get(query);
        final JSONArray articles = articleResult.getJSONArray(Keys.RESULTS);
        final boolean hasMultipleUsers = userQueryService.hasMultipleUsers();
        String authorName = "";
        if (!hasMultipleUsers && 0 != articles.length()) {
            authorName = articleQueryService.getAuthor(articles.getJSONObject(0)).getString(User.USER_NAME);
        }
        final boolean isFullContent = "fullContent".equals(preference.getString(Option.ID_C_FEED_OUTPUT_MODE));
        for (int i = 0; i < articles.length(); i++) {
            Item item = getItem(articles, hasMultipleUsers, authorName, isFullContent, i);
            channel.addItem(item);
        }
        renderer.setContent(channel.toString());
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, "Get blog article rss error", e);
        try {
            context.getResponse().sendError(HttpServletResponse.SC_SERVICE_UNAVAILABLE);
        } catch (final IOException ex) {
            throw new RuntimeException(ex);
        }
    }
}
Also used : CompositeFilter(org.b3log.latke.repository.CompositeFilter) Query(org.b3log.latke.repository.Query) Channel(org.b3log.solo.model.feed.rss.Channel) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) RssRenderer(org.b3log.latke.servlet.renderer.RssRenderer) Date(java.util.Date) IOException(java.io.IOException) Item(org.b3log.solo.model.feed.rss.Item) JSONObject(org.json.JSONObject) PropertyFilter(org.b3log.latke.repository.PropertyFilter) CompositeFilter(org.b3log.latke.repository.CompositeFilter) Filter(org.b3log.latke.repository.Filter) PropertyFilter(org.b3log.latke.repository.PropertyFilter) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

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