Search in sources :

Example 1 with Query

use of com.db4o.query.Query in project openhab1-addons by openhab.

the class Db4oPersistenceService method query.

@Override
public Iterable<HistoricItem> query(FilterCriteria filter) {
    Query query = queryWithReconnect();
    if (query != null) {
        query.constrain(Db4oItem.class);
        if (filter == null) {
            filter = new FilterCriteria();
        }
        if (filter.getBeginDate() != null) {
            query.descend("timestamp").constrain(filter.getBeginDate()).greater().equal();
        }
        if (filter.getEndDate() != null) {
            query.descend("timestamp").constrain(filter.getEndDate()).smaller().equal();
        }
        if (filter.getItemName() != null) {
            query.descend("name").constrain(filter.getItemName()).equal();
        }
        if (filter.getState() != null && filter.getOperator() != null) {
            switch(filter.getOperator()) {
                case EQ:
                    query.descend("state").constrain(filter.getState()).equal();
                    break;
                case GT:
                    query.descend("state").constrain(filter.getState()).greater();
                    break;
                case LT:
                    query.descend("state").constrain(filter.getState()).smaller();
                    break;
                case NEQ:
                    query.descend("state").constrain(filter.getState()).equal().not();
                    break;
                case GTE:
                    query.descend("state").constrain(filter.getState()).greater().equal();
                    break;
                case LTE:
                    query.descend("state").constrain(filter.getState()).smaller().equal();
                    break;
            }
        }
        if (filter.getOrdering() == Ordering.ASCENDING) {
            query.descend("timestamp").orderAscending();
        } else {
            query.descend("timestamp").orderDescending();
        }
        ObjectSet<HistoricItem> results = query.execute();
        int startIndex = filter.getPageNumber() * filter.getPageSize();
        if (startIndex < results.size()) {
            int endIndex = startIndex + filter.getPageSize();
            if (endIndex > results.size()) {
                endIndex = results.size();
            }
            return results.subList(startIndex, endIndex);
        }
    }
    return Collections.emptyList();
}
Also used : Query(com.db4o.query.Query) FilterCriteria(org.openhab.core.persistence.FilterCriteria) HistoricItem(org.openhab.core.persistence.HistoricItem)

Aggregations

Query (com.db4o.query.Query)1 FilterCriteria (org.openhab.core.persistence.FilterCriteria)1 HistoricItem (org.openhab.core.persistence.HistoricItem)1