Search in sources :

Example 21 with PropertyFilter

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

the class ArticleRepositoryImpl method getRandomly.

@Override
public List<JSONObject> getRandomly(final int fetchSize) throws RepositoryException {
    final List<JSONObject> ret = new ArrayList<JSONObject>();
    if (0 == count()) {
        return ret;
    }
    final double mid = Math.random() + RANDOM_RANGE;
    LOGGER.log(Level.TRACE, "Random mid[{0}]", mid);
    Query query = new Query();
    query.setFilter(CompositeFilterOperator.and(new PropertyFilter(Article.ARTICLE_RANDOM_DOUBLE, FilterOperator.GREATER_THAN_OR_EQUAL, mid), new PropertyFilter(Article.ARTICLE_RANDOM_DOUBLE, FilterOperator.LESS_THAN_OR_EQUAL, mid), new PropertyFilter(Article.ARTICLE_IS_PUBLISHED, FilterOperator.EQUAL, true)));
    query.setCurrentPageNum(1);
    query.setPageSize(fetchSize);
    query.setPageCount(1);
    final JSONObject result1 = get(query);
    final JSONArray array1 = result1.optJSONArray(Keys.RESULTS);
    final List<JSONObject> list1 = CollectionUtils.<JSONObject>jsonArrayToList(array1);
    ret.addAll(list1);
    final int reminingSize = fetchSize - array1.length();
    if (0 != reminingSize) {
        // Query for remains
        query = new Query();
        query.setFilter(CompositeFilterOperator.and(new PropertyFilter(Article.ARTICLE_RANDOM_DOUBLE, FilterOperator.GREATER_THAN_OR_EQUAL, 0D), new PropertyFilter(Article.ARTICLE_RANDOM_DOUBLE, FilterOperator.LESS_THAN_OR_EQUAL, mid), new PropertyFilter(Article.ARTICLE_IS_PUBLISHED, FilterOperator.EQUAL, true)));
        query.setCurrentPageNum(1);
        query.setPageSize(reminingSize);
        query.setPageCount(1);
        final JSONObject result2 = get(query);
        final JSONArray array2 = result2.optJSONArray(Keys.RESULTS);
        final List<JSONObject> list2 = CollectionUtils.<JSONObject>jsonArrayToList(array2);
        ret.addAll(list2);
    }
    return ret;
}
Also used : JSONObject(org.json.JSONObject) Query(org.b3log.latke.repository.Query) ArrayList(java.util.ArrayList) JSONArray(org.json.JSONArray) PropertyFilter(org.b3log.latke.repository.PropertyFilter)

Example 22 with PropertyFilter

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

the class CommentRepositoryImpl method getComments.

@Override
public List<JSONObject> getComments(final String onId, final int currentPageNum, final int pageSize) throws RepositoryException {
    final Query query = new Query().addSort(Keys.OBJECT_ID, SortDirection.DESCENDING).setFilter(new PropertyFilter(Comment.COMMENT_ON_ID, FilterOperator.EQUAL, onId)).setCurrentPageNum(currentPageNum).setPageSize(pageSize).setPageCount(1);
    final JSONObject result = get(query);
    final JSONArray array = result.optJSONArray(Keys.RESULTS);
    return CollectionUtils.jsonArrayToList(array);
}
Also used : Query(org.b3log.latke.repository.Query) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) PropertyFilter(org.b3log.latke.repository.PropertyFilter)

Example 23 with PropertyFilter

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

the class LinkRepositoryImpl method getUnder.

@Override
public JSONObject getUnder(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.GREATER_THAN, link.optInt(Link.LINK_ORDER))).addSort(Link.LINK_ORDER, SortDirection.ASCENDING);
    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 24 with PropertyFilter

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

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

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