Changed return type of SystemManagement.getConfigurationValue()

* changed Return type to wrapper object, adding additional meta data stored in the database
* updated all calls of this method
* updated function calls in tests
* verified correct execution of correspending tests

Signed-off-by: Nonnenmacher Fabian <fabian.nonnenmacher@bosch-si.com>
This commit is contained in:
Fabian Nonnenmacher
2016-01-26 14:31:56 +01:00
committed by Nonnenmacher Fabian
parent f2e7cdb92a
commit 07fce42469
14 changed files with 277 additions and 268 deletions

View File

@@ -10,7 +10,6 @@ package org.eclipse.hawkbit.repository;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.time.format.DateTimeParseException;
import java.util.List;
import java.util.stream.Collectors;
@@ -21,15 +20,11 @@ import org.eclipse.hawkbit.cache.TenancyCacheManager;
import org.eclipse.hawkbit.im.authentication.SpPermission.SpringEvalExpressions;
import org.eclipse.hawkbit.report.model.SystemUsageReport;
import org.eclipse.hawkbit.repository.exception.EntityNotFoundException;
import org.eclipse.hawkbit.repository.exception.InvalidDistributionSetTypeException;
import org.eclipse.hawkbit.repository.exception.InvalidPollingTimeException;
import org.eclipse.hawkbit.repository.model.DistributionSetType;
import org.eclipse.hawkbit.repository.model.SoftwareModuleType;
import org.eclipse.hawkbit.repository.model.TenantConfiguration;
import org.eclipse.hawkbit.repository.model.TenantConfigurationValue;
import org.eclipse.hawkbit.repository.model.TenantMetaData;
import org.eclipse.hawkbit.repository.model.helper.DurationHelper;
import org.eclipse.hawkbit.repository.specifications.DistributionSetTypeSpecification;
import org.eclipse.hawkbit.rest.resource.model.system.SystemConfigurationRequestBodyPut;
import org.eclipse.hawkbit.tenancy.TenantAware;
import org.eclipse.hawkbit.tenancy.configuration.TenantConfigurationKey;
import org.eclipse.persistence.config.PersistenceUnitProperties;
@@ -330,6 +325,8 @@ public class SystemManagement implements EnvironmentAware {
* Retrieves a configuration value from the e.g. tenant overwritten
* configuration values or in case the tenant does not a have a specific
* configuration the global default value hold in the {@link Environment}.
*
* @param <T>
*
* @param configurationKey
* the key of the configuration
@@ -345,15 +342,26 @@ public class SystemManagement implements EnvironmentAware {
* {@code propertyType}
*/
@Cacheable(value = "tenantConfiguration", key = "#configurationKey.getKeyName()")
public <T> T getConfigurationValue(final TenantConfigurationKey configurationKey, final Class<T> propertyType) {
public <T> TenantConfigurationValue<T> getConfigurationValue(final TenantConfigurationKey configurationKey,
final Class<T> propertyType) {
final TenantConfiguration tenantConfiguration = tenantConfigurationRepository
.findByKey(configurationKey.getKeyName());
if (tenantConfiguration != null) {
return conversionService.convert(tenantConfiguration.getValue(), propertyType);
return TenantConfigurationValue.<T> builder().isGlobal(false).createdBy(tenantConfiguration.getCreatedBy())
.createdAt(tenantConfiguration.getCreatedAt())
.lastModifiedAt(tenantConfiguration.getLastModifiedAt())
.lastModifiedBy(tenantConfiguration.getLastModifiedBy())
.value(conversionService.convert(tenantConfiguration.getValue(), propertyType)).build();
} else if (configurationKey.getDefaultKeyName() != null) {
final T defaultKeyNameValue = environment.getProperty(configurationKey.getDefaultKeyName(), propertyType);
return defaultKeyNameValue != null ? defaultKeyNameValue
: conversionService.convert(configurationKey.getDefaultValue(), propertyType);
final T valueInProperties = environment.getProperty(configurationKey.getDefaultKeyName(), propertyType);
return TenantConfigurationValue.<T> builder().isGlobal(true).createdBy(null).createdAt(null)
.lastModifiedAt(null).lastModifiedBy(null).value(valueInProperties != null ? valueInProperties
: conversionService.convert(configurationKey.getDefaultValue(), propertyType))
.build();
}
return null;
}
@@ -467,29 +475,33 @@ public class SystemManagement implements EnvironmentAware {
}
@Transactional
@Modifying
public void updateTenantConfiguration(SystemConfigurationRequestBodyPut systemConReq) {
DurationHelper dh = new DurationHelper();
TenantMetaData tenantMetaData = getTenantMetadata();
String ddstypeKey = systemConReq.getDefaultDistributionSetType();
if (distributionSetTypeRepository.findAll(DistributionSetTypeSpecification.byKey(ddstypeKey)).isEmpty()) {
throw new InvalidDistributionSetTypeException(
String.format("The specified default distribution set type %s doe not exist.", ddstypeKey));
}
try {
tenantMetaData.setPollingOverdueTime(dh.formattedStringToDuration(systemConReq.getPollingOverdueTime()));
tenantMetaData.setPollingTime(dh.formattedStringToDuration(systemConReq.getPollingTime()));
} catch (DateTimeParseException ex) {
throw new InvalidPollingTimeException(ex);
}
updateTenantMetadata(tenantMetaData);
}
// @Transactional
// @Modifying
// public void updateTenantConfiguration(SystemConfigurationRequestBodyPut
// systemConReq) {
//
// DurationHelper dh = new DurationHelper();
//
// TenantMetaData tenantMetaData = getTenantMetadata();
//
// String ddstypeKey = systemConReq.getDefaultDistributionSetType();
//
// if
// (distributionSetTypeRepository.findAll(DistributionSetTypeSpecification.byKey(ddstypeKey)).isEmpty())
// {
// throw new InvalidDistributionSetTypeException(
// String.format("The specified default distribution set type %s doe not
// exist.", ddstypeKey));
// }
//
// try {
// tenantMetaData.setPollingOverdueTime(dh.formattedStringToDuration(systemConReq.getPollingOverdueTime()));
// tenantMetaData.setPollingTime(dh.formattedStringToDuration(systemConReq.getPollingTime()));
// } catch (DateTimeParseException ex) {
// throw new InvalidPollingTimeException(ex);
// }
//
// updateTenantMetadata(tenantMetaData);
// }
}

View File

@@ -0,0 +1,179 @@
package org.eclipse.hawkbit.repository.model;
/**
* represents a tenant configuration value including some meta data
*
* @param <T>
* type of the configuration value
*/
public class TenantConfigurationValue<T> {
private TenantConfigurationValue() {
}
private T value = null;
private boolean isGlobal = true;
private Long lastModifiedAt = null;
private String lastModifiedBy = null;
private Long createdAt = null;
private String createdBy = null;
/**
* Gets the value.
*
* @return the value
*/
public T getValue() {
return value;
}
/**
* Checks if is global.
*
* @return true, if is global
*/
public boolean isGlobal() {
return isGlobal;
}
/**
* Gets the last modified at.
*
* @return the last modified at
*/
public long getLastModifiedAt() {
return lastModifiedAt;
}
/**
* Gets the last modified by.
*
* @return the last modified by
*/
public String getLastModifiedBy() {
return lastModifiedBy;
}
/**
* Gets the created at.
*
* @return the created at
*/
public long getCreatedAt() {
return createdAt;
}
/**
* Gets the created by.
*
* @return the created by
*/
public String getCreatedBy() {
return createdBy;
}
/**
* Builder.
*
* @param <K>
* the key type
* @return the tenant configuration value builder
*/
public static <K> TenantConfigurationValueBuilder<K> builder() {
return new TenantConfigurationValueBuilder<K>();
}
/**
* builds the tenant configuration value including some meta data
*
* @param <T>
* type of the configuration value
*/
public static class TenantConfigurationValueBuilder<T> {
private final TenantConfigurationValue<T> configuration = new TenantConfigurationValue<>();
/**
* Builds the.
*
* @return the tenant configuration value
*/
public TenantConfigurationValue<T> build() {
return configuration;
}
/**
* sets the configuration value itself
*
* @param value
* the value
* @return the tenant configuration value builder
*/
public TenantConfigurationValueBuilder<T> value(final T value) {
this.configuration.value = value;
return this;
}
/**
* set the is global attribute.
*
* @param isGlobal
* true when there is no tenant specific value, false
* otherwise
* @return the tenant configuration value builder
*/
public TenantConfigurationValueBuilder<T> isGlobal(final boolean isGlobal) {
this.configuration.isGlobal = isGlobal;
return this;
}
/**
* Sets the last modified at attribute
*
* @param lastModifiedAt
* timestamp of last modification
* @return the tenant configuration value builder
*/
public TenantConfigurationValueBuilder<T> lastModifiedAt(final Long lastModifiedAt) {
this.configuration.lastModifiedAt = lastModifiedAt;
return this;
}
/**
* sets the last modified by attribute
*
* @param lastModifiedBy
* the last modified by
* @return the tenant configuration value builder
*/
public TenantConfigurationValueBuilder<T> lastModifiedBy(final String lastModifiedBy) {
this.configuration.lastModifiedBy = lastModifiedBy;
return this;
}
/**
* sets the created at attribute
*
* @param createdAt
* defined when the configuration has been created
* @return the tenant configuration value builder
*/
public TenantConfigurationValueBuilder<T> createdAt(final Long createdAt) {
this.configuration.createdAt = createdAt;
return this;
}
/**
* sets the created by attribute
*
* @param createdBy
* defines by whom the configuration has been created
* @return the tenant configuration value builder
*/
public TenantConfigurationValueBuilder<T> createdBy(final String createdBy) {
this.configuration.createdBy = createdBy;
return this;
}
}
}