Add new persistence method (#55)

* Updated to include new persistence method getItems

Signed-off-by: Chris Jackson <chris@cd-jackson.com>
pull/57/head
Chris Jackson 2016-08-02 21:45:23 +01:00 committed by Kai Kreuzer
parent 0d640910de
commit 795aab9b19
2 changed files with 93 additions and 79 deletions

View File

@ -8,45 +8,52 @@
*/
package org.openhab.core.persistence.internal;
import java.util.Locale;
import org.eclipse.smarthome.core.items.Item;
import org.eclipse.smarthome.core.persistence.PersistenceService;
import org.openhab.core.compat1x.internal.ItemMapper;
/**
* This class serves as a mapping from the "old" org.openhab namespace to the new org.eclipse.smarthome
* namespace for the persistence service. It wraps an instance with the old interface
* into a class with the new interface.
*
* into a class with the new interface.
*
* @author Kai Kreuzer - Initial contribution and API
* @author Chris Jackson - updated API to support getId/getLabel
*/
public class PersistenceServiceDelegate implements PersistenceService {
protected org.openhab.core.persistence.PersistenceService service;
protected org.openhab.core.persistence.PersistenceService service;
public PersistenceServiceDelegate(org.openhab.core.persistence.PersistenceService service) {
this.service = service;
}
public PersistenceServiceDelegate(org.openhab.core.persistence.PersistenceService service) {
this.service = service;
}
@Override
public String getName() {
return service.getName();
}
@Override
public String getId() {
return service.getName();
}
@Override
public void store(Item item) {
org.openhab.core.items.Item ohItem = ItemMapper.mapToOpenHABItem(item);
if(ohItem!=null) {
service.store(ohItem);
}
}
@Override
public String getLabel(Locale locale) {
return service.getName();
}
@Override
public void store(Item item, String alias) {
org.openhab.core.items.Item ohItem = ItemMapper.mapToOpenHABItem(item);
if(ohItem!=null) {
service.store(ohItem, alias);
}
}
@Override
public void store(Item item) {
org.openhab.core.items.Item ohItem = ItemMapper.mapToOpenHABItem(item);
if (ohItem != null) {
service.store(ohItem);
}
}
@Override
public void store(Item item, String alias) {
org.openhab.core.items.Item ohItem = ItemMapper.mapToOpenHABItem(item);
if (ohItem != null) {
service.store(ohItem, alias);
}
}
}

View File

@ -9,79 +9,86 @@
package org.openhab.core.persistence.internal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.Set;
import org.eclipse.smarthome.core.persistence.FilterCriteria;
import org.eclipse.smarthome.core.persistence.HistoricItem;
import org.eclipse.smarthome.core.persistence.PersistenceItemInfo;
import org.eclipse.smarthome.core.persistence.QueryablePersistenceService;
import org.eclipse.smarthome.core.types.State;
import org.openhab.core.compat1x.internal.TypeMapper;
import org.openhab.core.persistence.FilterCriteria.Operator;
import org.openhab.core.persistence.FilterCriteria.Ordering;
/**
* This class serves as a mapping from the "old" org.openhab namespace to the new org.eclipse.smarthome
* namespace for the queryable persistence service. It wraps an instance with the old interface
* into a class with the new interface.
*
* into a class with the new interface.
*
* @author Kai Kreuzer - Initial contribution and API
*/
public class QueryablePersistenceServiceDelegate extends PersistenceServiceDelegate implements QueryablePersistenceService {
public class QueryablePersistenceServiceDelegate extends PersistenceServiceDelegate
implements QueryablePersistenceService {
public QueryablePersistenceServiceDelegate(
org.openhab.core.persistence.PersistenceService persistenceService) {
super(persistenceService);
}
public QueryablePersistenceServiceDelegate(org.openhab.core.persistence.PersistenceService persistenceService) {
super(persistenceService);
}
@Override
public Iterable<HistoricItem> query(FilterCriteria filter) {
org.openhab.core.persistence.FilterCriteria mappedFilter = new org.openhab.core.persistence.FilterCriteria()
.setBeginDate(filter.getBeginDate())
.setEndDate(filter.getEndDate())
.setItemName(filter.getItemName())
.setOperator(mapOperator(filter.getOperator()))
.setOrdering(mapOrdering(filter.getOrdering()))
.setPageNumber(filter.getPageNumber())
.setPageSize(filter.getPageSize())
.setState(mapState(filter.getState()));
org.openhab.core.persistence.QueryablePersistenceService pService = (org.openhab.core.persistence.QueryablePersistenceService) service;
Iterable<org.openhab.core.persistence.HistoricItem> historicItems = pService.query(mappedFilter);
ArrayList<HistoricItem> result = new ArrayList<>();
for(final org.openhab.core.persistence.HistoricItem item : historicItems) {
result.add(new HistoricItem() {
@Override
public Date getTimestamp() {
return item.getTimestamp();
}
@Override
public State getState() {
return (State) TypeMapper.mapToESHType(item.getState());
}
@Override
public String getName() {
return item.getName();
}
});
}
return result;
}
@Override
public Iterable<HistoricItem> query(FilterCriteria filter) {
org.openhab.core.persistence.FilterCriteria mappedFilter = new org.openhab.core.persistence.FilterCriteria()
.setBeginDate(filter.getBeginDate()).setEndDate(filter.getEndDate()).setItemName(filter.getItemName())
.setOperator(mapOperator(filter.getOperator())).setOrdering(mapOrdering(filter.getOrdering()))
.setPageNumber(filter.getPageNumber()).setPageSize(filter.getPageSize())
.setState(mapState(filter.getState()));
org.openhab.core.persistence.QueryablePersistenceService pService = (org.openhab.core.persistence.QueryablePersistenceService) service;
Iterable<org.openhab.core.persistence.HistoricItem> historicItems = pService.query(mappedFilter);
ArrayList<HistoricItem> result = new ArrayList<>();
for (final org.openhab.core.persistence.HistoricItem item : historicItems) {
result.add(new HistoricItem() {
@Override
public Date getTimestamp() {
return item.getTimestamp();
}
private org.openhab.core.types.State mapState(State state) {
return (org.openhab.core.types.State) TypeMapper.mapToOpenHABType(state);
}
@Override
public State getState() {
return (State) TypeMapper.mapToESHType(item.getState());
}
private Ordering mapOrdering(FilterCriteria.Ordering ordering) {
if(ordering==null) return null;
return org.openhab.core.persistence.FilterCriteria.Ordering.valueOf(ordering.toString());
}
@Override
public String getName() {
return item.getName();
}
});
}
return result;
}
private Operator mapOperator(FilterCriteria.Operator operator) {
if(operator==null) return null;
return org.openhab.core.persistence.FilterCriteria.Operator.valueOf(operator.toString());
}
private org.openhab.core.types.State mapState(State state) {
return (org.openhab.core.types.State) TypeMapper.mapToOpenHABType(state);
}
private Ordering mapOrdering(FilterCriteria.Ordering ordering) {
if (ordering == null) {
return null;
}
return org.openhab.core.persistence.FilterCriteria.Ordering.valueOf(ordering.toString());
}
private Operator mapOperator(FilterCriteria.Operator operator) {
if (operator == null) {
return null;
}
return org.openhab.core.persistence.FilterCriteria.Operator.valueOf(operator.toString());
}
@Override
public Set<PersistenceItemInfo> getItemInfo() {
return Collections.emptySet();
}
}