Search in sources :

Example 26 with PropertyFilter

use of org.b3log.latke.repository.PropertyFilter 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 27 with PropertyFilter

use of org.b3log.latke.repository.PropertyFilter 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)

Example 28 with PropertyFilter

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

the class FeedProcessor method blogArticlesAtom.

/**
     * Blog articles Atom output.
     *
     * @param context the specified context
     */
@RequestProcessing(value = { "/blog-articles-feed.do" }, method = { HTTPRequestMethod.GET, HTTPRequestMethod.HEAD })
public void blogArticlesAtom(final HTTPRequestContext context) {
    final AtomRenderer renderer = new AtomRenderer();
    context.setRenderer(renderer);
    final Feed feed = new Feed();
    try {
        final JSONObject preference = preferenceQueryService.getPreference();
        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);
        feed.setTitle(StringEscapeUtils.escapeXml(blogTitle));
        feed.setSubtitle(StringEscapeUtils.escapeXml(blogSubtitle));
        feed.setUpdated(new Date());
        feed.setAuthor(StringEscapeUtils.escapeXml(blogTitle));
        feed.setLink(Latkes.getServePath() + "/blog-articles-feed.do");
        feed.setId(Latkes.getServePath() + "/");
        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 boolean hasMultipleUsers = userQueryService.hasMultipleUsers();
        String authorName = "";
        final JSONObject articleResult = articleRepository.get(query);
        final JSONArray articles = articleResult.getJSONArray(Keys.RESULTS);
        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++) {
            Entry entry = getEntry(hasMultipleUsers, authorName, articles, isFullContent, i);
            feed.addEntry(entry);
        }
        renderer.setContent(feed.toString());
    } catch (final Exception e) {
        LOGGER.log(Level.ERROR, "Get blog article feed 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) AtomRenderer(org.b3log.latke.servlet.renderer.AtomRenderer) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) IOException(java.io.IOException) Date(java.util.Date) IOException(java.io.IOException) Entry(org.b3log.solo.model.feed.atom.Entry) 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) Feed(org.b3log.solo.model.feed.atom.Feed) RequestProcessing(org.b3log.latke.servlet.annotation.RequestProcessing)

Aggregations

PropertyFilter (org.b3log.latke.repository.PropertyFilter)28 Query (org.b3log.latke.repository.Query)28 JSONArray (org.json.JSONArray)28 JSONObject (org.json.JSONObject)28 Date (java.util.Date)5 RepositoryException (org.b3log.latke.repository.RepositoryException)5 ArrayList (java.util.ArrayList)3 IOException (java.io.IOException)2 CompositeFilter (org.b3log.latke.repository.CompositeFilter)2 Filter (org.b3log.latke.repository.Filter)2 RequestProcessing (org.b3log.latke.servlet.annotation.RequestProcessing)2 JSONException (org.json.JSONException)2 ParseException (java.text.ParseException)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 Transaction (org.b3log.latke.repository.Transaction)1 ServiceException (org.b3log.latke.service.ServiceException)1 AtomRenderer (org.b3log.latke.servlet.renderer.AtomRenderer)1 RssRenderer (org.b3log.latke.servlet.renderer.RssRenderer)1 ArchiveDate (org.b3log.solo.model.ArchiveDate)1 Entry (org.b3log.solo.model.feed.atom.Entry)1