[AB-149] adding command to reset configuration to default values

removing listener to create system config instances
adding default value retrieval to appropriate places
making config key handling case insensitive
changing exp multiplier to double value
refactoring experience service bean test
This commit is contained in:
Sheldan
2021-02-24 21:11:01 +01:00
parent 20b6b37151
commit 5f6746d742
35 changed files with 613 additions and 560 deletions

View File

@@ -0,0 +1,74 @@
package dev.sheldan.abstracto.core.commands.config;
import dev.sheldan.abstracto.core.command.condition.AbstractConditionableCommand;
import dev.sheldan.abstracto.core.command.config.CommandConfiguration;
import dev.sheldan.abstracto.core.command.config.HelpInfo;
import dev.sheldan.abstracto.core.command.config.Parameter;
import dev.sheldan.abstracto.core.command.config.features.CoreFeatures;
import dev.sheldan.abstracto.core.command.execution.CommandContext;
import dev.sheldan.abstracto.core.command.execution.CommandResult;
import dev.sheldan.abstracto.core.command.service.management.FeatureManagementService;
import dev.sheldan.abstracto.core.config.FeatureEnum;
import dev.sheldan.abstracto.core.exception.ConfigurationKeyNotFoundException;
import dev.sheldan.abstracto.core.service.ConfigService;
import dev.sheldan.abstracto.core.service.management.DefaultConfigManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
@Component
public class ResetConfig extends AbstractConditionableCommand {
@Autowired
private ConfigService configService;
@Autowired
private DefaultConfigManagementService defaultConfigManagementService;
@Autowired
private FeatureManagementService featureManagementService;
@Override
public CompletableFuture<CommandResult> executeAsync(CommandContext commandContext) {
Long serverId = commandContext.getGuild().getIdLong();
if(!commandContext.getParameters().getParameters().isEmpty()) {
String name = (String) commandContext.getParameters().getParameters().get(0);
if(featureManagementService.featureExists(name)) {
configService.resetConfigForFeature(name, serverId);
} else if(defaultConfigManagementService.configKeyExists(name)) {
configService.resetConfigForKey(name, serverId);
} else {
throw new ConfigurationKeyNotFoundException(name);
}
} else {
configService.resetConfigForServer(serverId);
}
return CompletableFuture.completedFuture(CommandResult.fromSuccess());
}
@Override
public CommandConfiguration getConfiguration() {
Parameter keyToChange = Parameter.builder().name("key").type(String.class).optional(true).templated(true).build();
List<Parameter> parameters = Arrays.asList(keyToChange);
HelpInfo helpInfo = HelpInfo.builder().templated(true).hasExample(true).build();
return CommandConfiguration.builder()
.name("resetConfig")
.module(ConfigModuleInterface.CONFIG)
.parameters(parameters)
.templated(true)
.async(true)
.supportsEmbedException(true)
.help(helpInfo)
.causesReaction(true)
.build();
}
@Override
public FeatureEnum getFeature() {
return CoreFeatures.CORE_FEATURE;
}
}

View File

@@ -21,7 +21,6 @@ public class SetConfig extends AbstractConditionableCommand {
@Autowired
private ConfigService configService;
@Override
public CommandResult execute(CommandContext commandContext) {
String key = (String) commandContext.getParameters().getParameters().get(0);

View File

@@ -6,10 +6,12 @@ import dev.sheldan.abstracto.core.models.property.PostTargetProperty;
import dev.sheldan.abstracto.core.models.property.SystemConfigProperty;
import lombok.Getter;
import lombok.Setter;
import org.apache.commons.lang3.tuple.Pair;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;
import java.util.Map;
import javax.annotation.PostConstruct;
import java.util.*;
@Getter
@Setter
@@ -20,4 +22,26 @@ public class DefaultConfigProperties {
private Map<String, FeatureFlagProperty> featureFlags;
private Map<String, PostTargetProperty> postTargets;
private Map<String, FeatureModeProperty> featureModes;
/**
* This is required to make the keys all lower case, so we can search for them faster, and also make it possible
* for users to not require exact names
*/
@PostConstruct
public void postConstruct() {
makeKeysLowerCase(systemConfigs);
makeKeysLowerCase(featureFlags);
makeKeysLowerCase(postTargets);
makeKeysLowerCase(featureModes);
}
private <T> void makeKeysLowerCase(Map<String, T> map) {
Set<String> keys = new HashSet<>(map.keySet());
List<Pair<String, T>> pairs = new ArrayList<>();
keys.forEach(s ->
pairs.add(Pair.of(s.toLowerCase(), map.get(s)))
);
keys.forEach(map::remove);
pairs.forEach(stringTPair -> map.put(stringTPair.getKey(), stringTPair.getValue()));
}
}

View File

@@ -1,27 +0,0 @@
package dev.sheldan.abstracto.core.listener.sync.entity;
import dev.sheldan.abstracto.core.command.service.CommandManager;
import dev.sheldan.abstracto.core.models.database.AServer;
import dev.sheldan.abstracto.core.service.management.ConfigManagementService;
import dev.sheldan.abstracto.core.service.management.DefaultConfigManagementService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
@Slf4j
public class CoreServiceConfigListener implements ServerConfigListener {
@Autowired
private ConfigManagementService configManagementService;
@Autowired
private DefaultConfigManagementService defaultConfigManagementService;
@Override
public void updateServerConfig(AServer server) {
log.info("Creating prefix config for server {}.", server.getId());
String defaultPrefix = defaultConfigManagementService.getDefaultConfig(CommandManager.PREFIX).getStringValue();
configManagementService.loadOrCreateIfNotExists(server.getId(), CommandManager.PREFIX, defaultPrefix);
}
}

View File

@@ -9,9 +9,10 @@ import org.springframework.stereotype.Repository;
@Repository
public interface ConfigRepository extends JpaRepository<AConfig, Long> {
AConfig findAConfigByServerIdAndName(Long serverId, String name);
AConfig findAConfigByServerIdAndNameIgnoreCase(Long serverId, String name);
void deleteAConfigByServerId(Long serverId);
boolean existsAConfigByServerIdAndName(Long serverId, String name);
boolean existsAConfigByServerIdAndNameIgnoreCase(Long serverId, String name);
boolean existsAConfigByServerAndName(AServer server, String name);
boolean existsAConfigByServerAndNameIgnoreCase(AServer server, String name);
}

View File

@@ -8,5 +8,5 @@ import org.springframework.stereotype.Repository;
@Repository
public interface FeatureRepository extends JpaRepository<AFeature, Long> {
AFeature findByKey(String key);
AFeature findByKeyIgnoreCase(String key);
}

View File

@@ -1,5 +1,6 @@
package dev.sheldan.abstracto.core.service;
import dev.sheldan.abstracto.core.config.FeatureConfig;
import dev.sheldan.abstracto.core.exception.ConfigurationKeyNotFoundException;
import dev.sheldan.abstracto.core.models.database.AConfig;
import dev.sheldan.abstracto.core.models.property.SystemConfigProperty;
@@ -17,6 +18,9 @@ public class ConfigServiceBean implements ConfigService {
@Autowired
private DefaultConfigManagementService defaultConfigManagementService;
@Autowired
private FeatureConfigService featureConfigService;
@Override
public Double getDoubleValue(String name, Long serverId) {
return getDoubleValue(name, serverId, 0D);
@@ -93,7 +97,7 @@ public class ConfigServiceBean implements ConfigService {
public AConfig setOrCreateConfigValue(String name, Long serverId, String value) {
if(defaultConfigManagementService.configKeyExists(name)) {
AConfig fakeConfigValue = getFakeConfigForValue(name, value);
return setOrCreateConfigValue(serverId, name, fakeConfigValue);
return setOrCreateConfigValue(serverId, fakeConfigValue.getName(), fakeConfigValue);
} else {
throw new ConfigurationKeyNotFoundException(name);
}
@@ -143,8 +147,8 @@ public class ConfigServiceBean implements ConfigService {
@Override
public AConfig getFakeConfigForValue(String name, String value) {
if(defaultConfigManagementService.configKeyExists(name)) {
AConfig newConfig = AConfig.builder().name(value).build();
SystemConfigProperty defaultConfig = defaultConfigManagementService.getDefaultConfig(name);
AConfig newConfig = AConfig.builder().name(defaultConfig.getName()).build();
if(defaultConfig.getDoubleValue() != null) {
newConfig.setDoubleValue(Double.parseDouble(value));
} else if(defaultConfig.getLongValue() != null) {
@@ -157,4 +161,24 @@ public class ConfigServiceBean implements ConfigService {
throw new ConfigurationKeyNotFoundException(name);
}
}
@Override
public void resetConfigForKey(String configKey, Long serverId) {
configManagementService.deleteConfig(serverId, configKey);
}
@Override
public void resetConfigForFeature(String featureKey, Long serverId) {
FeatureConfig featureConfig = featureConfigService.getFeatureDisplayForFeature(featureKey);
featureConfig.getRequiredSystemConfigKeys().forEach(s -> {
if(configManagementService.configExists(serverId, s)) {
resetConfigForKey(s, serverId);
}
});
}
@Override
public void resetConfigForServer(Long serverId) {
configManagementService.deleteConfigForServer(serverId);
}
}

View File

@@ -121,17 +121,17 @@ public class ConfigManagementServiceBean implements ConfigManagementService {
@Override
public AConfig loadConfig(Long serverId, String name) {
return configRepository.findAConfigByServerIdAndName(serverId, name);
return configRepository.findAConfigByServerIdAndNameIgnoreCase(serverId, name);
}
@Override
public boolean configExists(Long serverId, String name) {
return configRepository.existsAConfigByServerIdAndName(serverId, name);
return configRepository.existsAConfigByServerIdAndNameIgnoreCase(serverId, name);
}
@Override
public boolean configExists(AServer server, String name) {
return configRepository.existsAConfigByServerAndName(server, name);
return configRepository.existsAConfigByServerAndNameIgnoreCase(server, name);
}
@Override
@@ -158,4 +158,15 @@ public class ConfigManagementServiceBean implements ConfigManagementService {
return config;
}
@Override
public void deleteConfig(Long serverId, String name) {
AConfig config = loadConfig(serverId, name);
configRepository.delete(config);
}
@Override
public void deleteConfigForServer(Long serverId) {
configRepository.deleteAConfigByServerId(serverId);
}
}

View File

@@ -15,11 +15,11 @@ public class DefaultConfigManagementServiceBean implements DefaultConfigManageme
@Override
public SystemConfigProperty getDefaultConfig(String key) {
return defaultConfigProperties.getSystemConfigs().get(key);
return defaultConfigProperties.getSystemConfigs().get(key.toLowerCase());
}
@Override
public boolean configKeyExists(String key) {
return defaultConfigProperties.getSystemConfigs().containsKey(key);
return defaultConfigProperties.getSystemConfigs().containsKey(key.toLowerCase());
}
}

View File

@@ -32,6 +32,6 @@ public class FeatureManagementServiceBean implements FeatureManagementService {
@Override
public AFeature getFeature(String key) {
return featureRepository.findByKey(key);
return featureRepository.findByKeyIgnoreCase(key);
}
}

View File

@@ -97,6 +97,12 @@
<column name="feature_id" valueComputed="${coreFeature}"/>
<column name="created" valueComputed="${today}"/>
</insert>
<insert tableName="command">
<column name="name" value="resetConfig"/>
<column name="module_id" valueComputed="${configModule}"/>
<column name="feature_id" valueComputed="${coreFeature}"/>
<column name="created" valueComputed="${today}"/>
</insert>
<insert tableName="command">
<column name="name" value="setPrefix"/>
<column name="module_id" valueComputed="${configModule}"/>