Skip to content

Commit

Permalink
Add new persistence method (openhab#55)
Browse files Browse the repository at this point in the history
* Updated to include new persistence method getItems

Signed-off-by: Chris Jackson <chris@cd-jackson.com>
  • Loading branch information
cdjackson authored and kaikreuzer committed Aug 2, 2016
1 parent 0d64091 commit 795aab9
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

public PersistenceServiceDelegate(org.openhab.core.persistence.PersistenceService service) {
this.service = service;
}

@Override
public String getName() {
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 void store(Item item, String alias) {
org.openhab.core.items.Item ohItem = ItemMapper.mapToOpenHABItem(item);
if(ohItem!=null) {
service.store(ohItem, alias);
}
}
protected org.openhab.core.persistence.PersistenceService service;

public PersistenceServiceDelegate(org.openhab.core.persistence.PersistenceService service) {
this.service = service;
}

@Override
public String getId() {
return service.getName();
}

@Override
public String getLabel(Locale locale) {
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 void store(Item item, String alias) {
org.openhab.core.items.Item ohItem = ItemMapper.mapToOpenHABItem(item);
if (ohItem != null) {
service.store(ohItem, alias);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

@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;
}

public QueryablePersistenceServiceDelegate(
org.openhab.core.persistence.PersistenceService persistenceService) {
super(persistenceService);
}
private org.openhab.core.types.State mapState(State state) {
return (org.openhab.core.types.State) TypeMapper.mapToOpenHABType(state);
}

@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;
}
private Ordering mapOrdering(FilterCriteria.Ordering ordering) {
if (ordering == null) {
return null;
}

private org.openhab.core.types.State mapState(State state) {
return (org.openhab.core.types.State) TypeMapper.mapToOpenHABType(state);
}
return org.openhab.core.persistence.FilterCriteria.Ordering.valueOf(ordering.toString());
}

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());
}

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();
}

}

0 comments on commit 795aab9

Please sign in to comment.