Search in sources :

Example 16 with Query

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

the class SitemapProcessor method addNavigations.

/**
     * Adds navigations into the specified sitemap.
     * 
     * @param sitemap the specified sitemap
     * @throws Exception exception 
     */
private void addNavigations(final Sitemap sitemap) throws Exception {
    final JSONObject result = pageRepository.get(new Query());
    final JSONArray pages = result.getJSONArray(Keys.RESULTS);
    for (int i = 0; i < pages.length(); i++) {
        final JSONObject page = pages.getJSONObject(i);
        final String permalink = page.getString(Page.PAGE_PERMALINK);
        final URL url = new URL();
        // Just filters for user mistakes tolerance
        if (!permalink.contains("://")) {
            url.setLoc(Latkes.getServePath() + permalink);
        } else {
            url.setLoc(permalink);
        }
        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)

Example 17 with Query

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

the class ArchiveDateArticleRepositoryImpl method getByArticleId.

@Override
public JSONObject getByArticleId(final String articleId) throws RepositoryException {
    final Query query = new Query();
    query.setFilter(new PropertyFilter(Article.ARTICLE + "_" + Keys.OBJECT_ID, FilterOperator.EQUAL, articleId));
    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 18 with Query

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

the class ArchiveDateRepositoryImpl method getByArchiveDate.

@Override
public JSONObject getByArchiveDate(final String archiveDate) throws RepositoryException {
    long time = 0L;
    try {
        time = DateUtils.parseDate(archiveDate, new String[] { "yyyy/MM" }).getTime();
    } catch (final ParseException e) {
        LOGGER.log(Level.ERROR, "Can not parse archive date [" + archiveDate + "]", e);
        throw new RepositoryException("Can not parse archive date [" + archiveDate + "]");
    }
    LOGGER.log(Level.TRACE, "Archive date [{0}] parsed to time [{1}]", new Object[] { archiveDate, time });
    final Query query = new Query();
    query.setFilter(new PropertyFilter(ArchiveDate.ARCHIVE_TIME, FilterOperator.EQUAL, time)).setPageCount(1);
    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) RepositoryException(org.b3log.latke.repository.RepositoryException) PropertyFilter(org.b3log.latke.repository.PropertyFilter) ParseException(java.text.ParseException)

Example 19 with Query

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

the class ArticleRepositoryImpl method getNextArticle.

@Override
public JSONObject getNextArticle(final String articleId) throws RepositoryException {
    final JSONObject currentArticle = get(articleId);
    final Date currentArticleCreateDate = (Date) currentArticle.opt(Article.ARTICLE_CREATE_DATE);
    final Query query = new Query().setFilter(CompositeFilterOperator.and(new PropertyFilter(Article.ARTICLE_CREATE_DATE, FilterOperator.GREATER_THAN, currentArticleCreateDate), new PropertyFilter(Article.ARTICLE_IS_PUBLISHED, FilterOperator.EQUAL, true))).addSort(Article.ARTICLE_CREATE_DATE, SortDirection.ASCENDING).setCurrentPageNum(1).setPageSize(1).setPageCount(1).addProjection(Article.ARTICLE_TITLE, String.class).addProjection(Article.ARTICLE_PERMALINK, String.class).addProjection(Article.ARTICLE_ABSTRACT, String.class);
    final JSONObject result = get(query);
    final JSONArray array = result.optJSONArray(Keys.RESULTS);
    if (1 != array.length()) {
        return null;
    }
    final JSONObject ret = new JSONObject();
    final JSONObject article = array.optJSONObject(0);
    try {
        ret.put(Article.ARTICLE_TITLE, article.getString(Article.ARTICLE_TITLE));
        ret.put(Article.ARTICLE_PERMALINK, article.getString(Article.ARTICLE_PERMALINK));
        ret.put(Article.ARTICLE_ABSTRACT, article.getString((Article.ARTICLE_ABSTRACT)));
    } catch (final JSONException e) {
        throw new RepositoryException(e);
    }
    return ret;
}
Also used : JSONObject(org.json.JSONObject) Query(org.b3log.latke.repository.Query) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) PropertyFilter(org.b3log.latke.repository.PropertyFilter) RepositoryException(org.b3log.latke.repository.RepositoryException) Date(java.util.Date)

Example 20 with Query

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

the class ArticleRepositoryImpl method getByPermalink.

@Override
public JSONObject getByPermalink(final String permalink) throws RepositoryException {
    final Query query = new Query().setFilter(new PropertyFilter(Article.ARTICLE_PERMALINK, FilterOperator.EQUAL, permalink)).setPageCount(1);
    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)

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