Skip to content

Commit

Permalink
aliases for getItemInfo, cleanup
Browse files Browse the repository at this point in the history
Signed-off-by: Mark Herwege <mark.herwege@telenet.be>
  • Loading branch information
mherwege committed Jan 6, 2025
1 parent b7d0862 commit 811e8aa
Show file tree
Hide file tree
Showing 11 changed files with 61 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,13 @@
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;

import javax.annotation.security.RolesAllowed;
import javax.ws.rs.Consumes;
Expand Down Expand Up @@ -105,6 +108,7 @@
* @author Lyubomir Papazov - Change java.util.Date references to be of type java.time.ZonedDateTime
* @author Markus Rathgeb - Migrated to JAX-RS Whiteboard Specification
* @author Wouter Born - Migrated to OpenAPI annotations
* @author Mark Herwege - Implement aliases
*/
@Component
@JaxrsResource
Expand Down Expand Up @@ -551,26 +555,60 @@ private List<PersistenceServiceDTO> getPersistenceServiceList(Locale locale) {
private Response getServiceItemList(@Nullable String serviceId) {
// If serviceId is null, then use the default service
PersistenceService service;
if (serviceId == null) {
service = persistenceServiceRegistry.getDefault();
} else {
service = persistenceServiceRegistry.get(serviceId);
String effectiveServiceId = serviceId != null ? serviceId : persistenceServiceRegistry.getDefaultId();
if (effectiveServiceId == null) {
logger.debug("Persistence service not found '{}'.", effectiveServiceId);
return JSONResponse.createErrorResponse(Status.BAD_REQUEST,
"Persistence service not found: " + effectiveServiceId);
}
service = persistenceServiceRegistry.get(effectiveServiceId);

if (service == null) {
logger.debug("Persistence service not found '{}'.", serviceId);
return JSONResponse.createErrorResponse(Status.BAD_REQUEST, "Persistence service not found: " + serviceId);
logger.debug("Persistence service not found '{}'.", effectiveServiceId);
return JSONResponse.createErrorResponse(Status.BAD_REQUEST,
"Persistence service not found: " + effectiveServiceId);
}

if (!(service instanceof QueryablePersistenceService)) {
logger.debug("Persistence service not queryable '{}'.", serviceId);
logger.debug("Persistence service not queryable '{}'.", effectiveServiceId);
return JSONResponse.createErrorResponse(Status.BAD_REQUEST,
"Persistence service not queryable: " + serviceId);
"Persistence service not queryable: " + effectiveServiceId);
}

QueryablePersistenceService qService = (QueryablePersistenceService) service;

return JSONResponse.createResponse(Status.OK, qService.getItemInfo(), "");
PersistenceServiceConfiguration config = persistenceServiceConfigurationRegistry.get(effectiveServiceId);
Map<String, String> aliases = config != null ? config.getAliases() : Map.of();
Set<PersistenceItemInfo> itemInfo = qService.getItemInfo().stream().map(info -> {
String alias = aliases.get(info.getName());
if (alias != null) {
return new PersistenceItemInfo() {

@Override
public String getName() {
return alias;
}

@Override
public @Nullable Integer getCount() {
return info.getCount();
}

@Override
public @Nullable Date getEarliest() {
return info.getEarliest();
}

@Override
public @Nullable Date getLatest() {
return info.getLatest();
}
};
} else {
return info;
}
}).collect(Collectors.toSet());
return JSONResponse.createResponse(Status.OK, itemInfo, "");
}

private Response deletePersistenceItemData(@Nullable String serviceId, String itemName, @Nullable String timeBegin,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
* Tests for PersistenceItem Restresource
*
* @author Stefan Triller - Initial contribution
* @author Mark Herwege - Implement aliases
*/
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
* a filter.
*
* @author Kai Kreuzer - Initial contribution
* @author Lyubomir Papazov - Deprecate methods using java.util and add methods
* that use Java8's ZonedDateTime
* @author Lyubomir Papazov - Deprecate methods using java.util and add methods that use Java8's ZonedDateTime
* @author Mark Herwege - Copy constructor
*/
@NonNullByDefault
public class FilterCriteria {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
* and then periodically provide it to the server to be accommodated.
*
* @author Chris Jackson - Initial contribution
* @author Mark Herwege - Implement aliases
*/
@NonNullByDefault
public interface ModifiablePersistenceService extends QueryablePersistenceService {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* This class holds the configuration of a persistence strategy for specific items.
*
* @author Markus Rathgeb - Initial contribution
* @author Mark Herwege - extract alias configuration
* @author Mark Herwege - Extract alias configuration
*/
@NonNullByDefault
public record PersistenceItemConfiguration(List<PersistenceConfig> items, List<PersistenceStrategy> strategies,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
*
* @author Kai Kreuzer - Initial contribution
* @author Chris Jackson - Added getItems method
* @author Mark Herwege - Implement aliases
*/
@NonNullByDefault
public interface QueryablePersistenceService extends PersistenceService {
Expand Down Expand Up @@ -85,8 +86,9 @@ public String getName() {
* {@link org.openhab.core.items.Item} in openHAB. If it is not possible to retrieve the information an empty set
* should be returned.
*
* Note that implementations of this method may return an alias for an existing item if the database does not store
* the mapping between item name and alias.
* Note that implementations for method callers that this method may return an alias for an existing item if the
* database does not store the mapping between item name and alias or the reverse mapping is not implemented in the
* persistence service.
*
* @return a set of information about the persisted items
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
* The {@link PersistenceServiceConfiguration} represents the configuration for a persistence service.
*
* @author Jan N. Klug - Initial contribution
* @author Mark Herwege - Implement aliases
*/
@NonNullByDefault
public class PersistenceServiceConfiguration implements Identifiable<String> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
* The {@link PersistenceServiceConfigurationDTOMapper} is a utility class to map persistence configurations for storage
*
* @author Jan N. Klug - Initial contribution
* @author Mark Herwege - Implement aliases
*/
@NonNullByDefault
public class PersistenceServiceConfigurationDTOMapper {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
* @author Mark Herwege - lastChange and nextChange methods
* @author Mark Herwege - handle persisted GroupItem with QuantityType
* @author Mark Herwege - add median methods
* @author Mark Herwege - Implement aliases
*/
@ExtendWith(MockitoExtension.class)
@MockitoSettings(strictness = Strictness.LENIENT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
* The {@link PersistenceManagerTest} contains tests for the {@link PersistenceManagerImpl}
*
* @author Jan N. Klug - Initial contribution
* @author Mark Herwege - Implement aliases
*/
@NonNullByDefault
@ExtendWith(MockitoExtension.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
* @author Holger Reichert - Support for themes, DPI, legend hiding
* @author Christoph Weitkamp - Consider default persistence service
* @author Jan N. Klug - Add y-axis label formatter
* @author Mark Herwege - Implement aliases
*/
@NonNullByDefault
@Component(immediate = true)
Expand Down

0 comments on commit 811e8aa

Please sign in to comment.