cn.featherfly.rc.repository.ConfigurationSqlDBRepository Maven / Gradle / Ivy
The newest version!
package cn.featherfly.rc.repository;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.transaction.annotation.Transactional;
import cn.featherfly.common.lang.Lang;
import cn.featherfly.conversion.string.ToStringConversionPolicys;
import cn.featherfly.conversion.string.ToStringTypeConversion;
import cn.featherfly.hammer.Hammer;
import cn.featherfly.hammer.sqldb.SqldbHammer;
import cn.featherfly.hammer.sqldb.jdbc.Jdbc;
import cn.featherfly.rc.Configuration;
import cn.featherfly.rc.ConfigurationException;
import cn.featherfly.rc.ConfigurationRepository;
import cn.featherfly.rc.ConfigurationUtils;
import cn.featherfly.rc.ConfigurationValue;
import cn.featherfly.rc.SimpleConfiguration;
import cn.featherfly.rc.SimpleConfigurationValue;
/**
* ConfigurationPersistenceService.
*
* @author 钟冀
*/
public class ConfigurationSqlDBRepository implements ConfigurationRepository {
/** The logger. */
protected final Logger logger = LoggerFactory.getLogger(this.getClass());
/**
* Instantiates a new configuration sql DB repository.
*/
public ConfigurationSqlDBRepository() {
super();
}
private Hammer hammer;
private Jdbc jdbc;
private ToStringTypeConversion conversion = new ToStringTypeConversion(
ToStringConversionPolicys.getFormatConversionPolicy());
private static final String CONFIGURATION_DIFINITION_TABLE_NAME = "RC_CONFIGURATION_DIFINITION";
private static final String CONFIGURATION_VALUE_TABLE_NAME = "RC_CONFIGURATION_VALUE";
private static final String CONFIG_NAME = "config_name";
private static final String NAME = "name";
private static final String VALUE = "value";
private static final String DESCP = "descp";
/**
* {@inheritDoc}
*/
@Override
@CachePut(value = { "configurationCache" }, key = "'config:'+ #configName + ':' + #name")
@Transactional
public V set(String configName, String name, V value) {
if (hammer.update(CONFIGURATION_VALUE_TABLE_NAME).set(valueColumn(), value).where()
.eq(configNameColumn(), configName) //
.and().eq(nameColumn(), name) //
.execute() < 1) {
ConfigurationException.throwConfigNotInit(configName, name);
}
return value;
}
/**
* {@inheritDoc}
*/
@Override
@Transactional
public ConfigurationRepository set(String configName, Map configNameValueMap) {
configNameValueMap.entrySet().forEach(e -> this.set(configName, e.getKey(), e.getValue()));
return this;
}
/**
* {@inheritDoc}
*/
@Override
@Cacheable(value = { "configurationCache" }, key = "'config:'+ #configName + ':' + #name")
public V get(String configName, String name, Class type) {
String valueStr = hammer.query(CONFIGURATION_VALUE_TABLE_NAME).field(valueColumn()) //
.where() //
.eq(configNameColumn(), configName) //
.and().eq(nameColumn(), name) //
.string();
return conversion.targetToSource(valueStr, type);
}
/**
* {@inheritDoc}
*/
@Override
public Collection getConfigurations() {
List