diff --git a/bundles/org.openhab.core.persistence/src/main/java/org/openhab/core/persistence/FilterCriteria.java b/bundles/org.openhab.core.persistence/src/main/java/org/openhab/core/persistence/FilterCriteria.java index 1f296308b3d..4ff41fb3776 100644 --- a/bundles/org.openhab.core.persistence/src/main/java/org/openhab/core/persistence/FilterCriteria.java +++ b/bundles/org.openhab.core.persistence/src/main/java/org/openhab/core/persistence/FilterCriteria.java @@ -91,6 +91,20 @@ public enum Ordering { /** Filter result to only contain entries that evaluate to true with the given operator and state */ private @Nullable State state; + public FilterCriteria() { + } + + public FilterCriteria(FilterCriteria filter) { + this.itemName = filter.itemName; + this.beginDate = filter.beginDate; + this.endDate = filter.endDate; + this.pageNumber = filter.pageNumber; + this.pageSize = filter.pageSize; + this.operator = filter.operator; + this.ordering = filter.ordering; + this.state = filter.state; + } + public @Nullable String getItemName() { return itemName; } diff --git a/bundles/org.openhab.core.persistence/src/main/java/org/openhab/core/persistence/ModifiablePersistenceService.java b/bundles/org.openhab.core.persistence/src/main/java/org/openhab/core/persistence/ModifiablePersistenceService.java index 481bb550e1f..ba46ff972d1 100644 --- a/bundles/org.openhab.core.persistence/src/main/java/org/openhab/core/persistence/ModifiablePersistenceService.java +++ b/bundles/org.openhab.core.persistence/src/main/java/org/openhab/core/persistence/ModifiablePersistenceService.java @@ -83,8 +83,8 @@ public interface ModifiablePersistenceService extends QueryablePersistenceServic * Removes data associated with an item from a persistence service. * If all data is removed for the specified item, the persistence service should free any resources associated with * the item (e.g. remove any tables or delete files from the storage). - * Persistence services supporting aliases should override the default implementation from this interface that - * ignores aliases when querying. + * Persistence services supporting aliases, and relying on lookups in the item registry, should override the default + * implementation from this interface. * * @param filter the filter to apply to the data removal. ItemName can not be null. * @param alias for item name in database @@ -92,7 +92,13 @@ public interface ModifiablePersistenceService extends QueryablePersistenceServic * @throws IllegalArgumentException if item name is null. */ default boolean remove(FilterCriteria filter, @Nullable String alias) throws IllegalArgumentException { - // Default implementation ignores alias + // Default implementation changes the filter to have the alias as itemName. + // This gives correct results as long as the persistence service does not rely on a lookup in the item registry + // (in which case the item will not be found). + if (alias != null) { + FilterCriteria aliasFilter = new FilterCriteria(filter).setItemName(alias); + return remove(aliasFilter); + } return remove(filter); } } diff --git a/bundles/org.openhab.core.persistence/src/main/java/org/openhab/core/persistence/QueryablePersistenceService.java b/bundles/org.openhab.core.persistence/src/main/java/org/openhab/core/persistence/QueryablePersistenceService.java index 7b948c4b264..bec80cd1fe0 100644 --- a/bundles/org.openhab.core.persistence/src/main/java/org/openhab/core/persistence/QueryablePersistenceService.java +++ b/bundles/org.openhab.core.persistence/src/main/java/org/openhab/core/persistence/QueryablePersistenceService.java @@ -12,10 +12,14 @@ */ package org.openhab.core.persistence; +import java.time.ZonedDateTime; import java.util.Set; +import java.util.stream.Collectors; +import java.util.stream.StreamSupport; import org.eclipse.jdt.annotation.NonNullByDefault; import org.eclipse.jdt.annotation.Nullable; +import org.openhab.core.types.State; /** * A queryable persistence service which can be used to store and retrieve @@ -39,15 +43,39 @@ public interface QueryablePersistenceService extends PersistenceService { /** * Queries the {@link PersistenceService} for historic data with a given {@link FilterCriteria}. - * If the persistence service implementing this interface supports aliases, the default implementation should be - * overriden to query the database with the aliased name. + * If the persistence service implementing this interface supports aliases and relies on item registry lookups, the + * default implementation should be overriden to query the database with the aliased name. * * @param filter the filter to apply to the query * @param alias for item name in database * @return a time series of items */ default Iterable<HistoricItem> query(FilterCriteria filter, @Nullable String alias) { - // Default implementation ignores alias + // Default implementation changes the filter to have the alias as itemName and sets it back in the returned + // result. + // This gives correct results as long as the persistence service does not rely on a lookup in the item registry + // (in which case the item will not be found). + String itemName = filter.getItemName(); + if (itemName != null && alias != null) { + FilterCriteria aliasFilter = new FilterCriteria(filter).setItemName(alias); + return StreamSupport.stream(query(aliasFilter).spliterator(), false).map(hi -> new HistoricItem() { + + @Override + public ZonedDateTime getTimestamp() { + return hi.getTimestamp(); + } + + @Override + public State getState() { + return hi.getState(); + } + + @Override + public String getName() { + return itemName; + } + }).collect(Collectors.toList()); + } return query(filter); }