Search in sources :

Example 1 with FilterCriteria

use of org.openhab.core.persistence.FilterCriteria in project openhab1-addons by openhab.

the class CaldavPersistenceService method findLastOn.

private CaldavItem findLastOn(String alias, State state) {
    final FilterCriteria filter = new FilterCriteria();
    filter.setEndDate(new Date());
    filter.setItemName(alias);
    filter.setOrdering(FilterCriteria.Ordering.DESCENDING);
    filter.setPageSize(1);
    final Iterable<HistoricItem> query = this.query(filter);
    final Iterator<HistoricItem> iterator = query.iterator();
    if (iterator.hasNext()) {
        CaldavItem caldavItem = (CaldavItem) iterator.next();
        if (!isOff(caldavItem.getState())) {
            return caldavItem;
        }
    }
    return null;
}
Also used : FilterCriteria(org.openhab.core.persistence.FilterCriteria) HistoricItem(org.openhab.core.persistence.HistoricItem) Date(java.util.Date)

Example 2 with FilterCriteria

use of org.openhab.core.persistence.FilterCriteria 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)

Example 3 with FilterCriteria

use of org.openhab.core.persistence.FilterCriteria in project openhab1-addons by openhab.

the class AbstractTwoItemIntegrationTest method testQueryUsingNameAndStartAndEndWithNEQOperator.

@Test
public void testQueryUsingNameAndStartAndEndWithNEQOperator() {
    FilterCriteria criteria = new FilterCriteria();
    criteria.setOperator(Operator.NEQ);
    criteria.setState(getSecondItemState());
    criteria.setItemName(getItemName());
    criteria.setBeginDate(beforeStore);
    criteria.setEndDate(afterStore2);
    Iterable<HistoricItem> iterable = BaseIntegrationTest.service.query(criteria);
    Iterator<HistoricItem> iterator = iterable.iterator();
    HistoricItem actual1 = iterator.next();
    assertFalse(iterator.hasNext());
    assertStateEquals(getFirstItemState(), actual1.getState());
    assertTrue(actual1.getTimestamp().before(afterStore1));
    assertTrue(actual1.getTimestamp().after(beforeStore));
}
Also used : FilterCriteria(org.openhab.core.persistence.FilterCriteria) HistoricItem(org.openhab.core.persistence.HistoricItem) Test(org.junit.Test)

Example 4 with FilterCriteria

use of org.openhab.core.persistence.FilterCriteria in project openhab1-addons by openhab.

the class AbstractTwoItemIntegrationTest method testQueryUsingNameAndStartAndEndWithGTEOperator.

@Test
public void testQueryUsingNameAndStartAndEndWithGTEOperator() {
    FilterCriteria criteria = new FilterCriteria();
    criteria.setOperator(Operator.GTE);
    criteria.setState(getSecondItemState());
    criteria.setItemName(getItemName());
    criteria.setBeginDate(beforeStore);
    criteria.setEndDate(afterStore2);
    Iterable<HistoricItem> iterable = BaseIntegrationTest.service.query(criteria);
    Iterator<HistoricItem> iterator = iterable.iterator();
    HistoricItem actual1 = iterator.next();
    assertFalse(iterator.hasNext());
    assertStateEquals(getSecondItemState(), actual1.getState());
    assertTrue(actual1.getTimestamp().before(afterStore2));
    assertTrue(actual1.getTimestamp().after(afterStore1));
}
Also used : FilterCriteria(org.openhab.core.persistence.FilterCriteria) HistoricItem(org.openhab.core.persistence.HistoricItem) Test(org.junit.Test)

Example 5 with FilterCriteria

use of org.openhab.core.persistence.FilterCriteria in project openhab1-addons by openhab.

the class AbstractTwoItemIntegrationTest method testQueryUsingNameAndStart.

@Test
public void testQueryUsingNameAndStart() {
    FilterCriteria criteria = new FilterCriteria();
    criteria.setOrdering(Ordering.ASCENDING);
    criteria.setItemName(getItemName());
    criteria.setBeginDate(beforeStore);
    Iterable<HistoricItem> iterable = BaseIntegrationTest.service.query(criteria);
    assertIterableContainsItems(iterable, true);
}
Also used : FilterCriteria(org.openhab.core.persistence.FilterCriteria) HistoricItem(org.openhab.core.persistence.HistoricItem) Test(org.junit.Test)

Aggregations

FilterCriteria (org.openhab.core.persistence.FilterCriteria)19 HistoricItem (org.openhab.core.persistence.HistoricItem)19 Test (org.junit.Test)17 Query (com.db4o.query.Query)1 Date (java.util.Date)1