[AB-112] adding star levels to setup feature wizard

moving star level configuration to only be instantiated when needed
This commit is contained in:
Sheldan
2021-02-14 16:35:51 +01:00
parent 23bdf4e906
commit 94532be54d
22 changed files with 153 additions and 142 deletions

View File

@@ -26,7 +26,7 @@ public class SetConfig extends AbstractConditionableCommand {
public CommandResult execute(CommandContext commandContext) {
String key = (String) commandContext.getParameters().getParameters().get(0);
String value = (String) commandContext.getParameters().getParameters().get(1);
configService.setConfigValue(key, commandContext.getGuild().getIdLong(), value);
configService.setOrCreateConfigValue(key, commandContext.getGuild().getIdLong(), value);
return CommandResult.fromSuccess();
}

View File

@@ -17,7 +17,7 @@ public class SystemConfigDelayedAction implements DelayedAction {
public void execute(DelayedActionConfig delayedActionConfig) {
SystemConfigDelayedActionConfig concrete = (SystemConfigDelayedActionConfig) delayedActionConfig;
log.trace("Executing delayed system config action for key {} in server {}.", concrete.getConfigKey(), concrete.getServerId());
configService.setConfigValue(concrete.getConfigKey(), concrete.getServerId(), concrete.getValue());
configService.setOrCreateConfigValue(concrete.getServerId(), concrete.getConfigKey(), concrete.getValue());
}
@Override

View File

@@ -78,7 +78,7 @@ public class SystemConfigSetupStep extends AbstractConfigSetupStep {
config = self.loadDefaultConfig(systemConfigStepParameter);
log.info("It was decided to keep the original value for key {} in server {}.", systemConfigStepParameter.getConfigKey(), user.getGuildId());
} else {
config = self.checkValidity(user, systemConfigStepParameter, event);
config = self.checkValidity(systemConfigStepParameter, event);
log.trace("The given value for key {} in server {} was valid.", systemConfigStepParameter.getConfigKey(), user.getGuildId());
}
SystemConfigDelayedActionConfig build = SystemConfigDelayedActionConfig
@@ -120,8 +120,8 @@ public class SystemConfigSetupStep extends AbstractConfigSetupStep {
}
@Transactional
public AConfig checkValidity(AServerChannelUserId user, SystemConfigStepParameter systemConfigStepParameter, MessageReceivedEvent event) {
return configService.getFakeConfigForValue(systemConfigStepParameter.getConfigKey(), user.getGuildId(), event.getMessage().getContentRaw());
public AConfig checkValidity(SystemConfigStepParameter systemConfigStepParameter, MessageReceivedEvent event) {
return configService.getFakeConfigForValue(systemConfigStepParameter.getConfigKey(), event.getMessage().getContentRaw());
}

View File

@@ -22,6 +22,6 @@ public class CoreServiceConfigListener implements ServerConfigListener {
public void updateServerConfig(AServer server) {
log.info("Creating prefix config for server {}.", server.getId());
String defaultPrefix = defaultConfigManagementService.getDefaultConfig(CommandManager.PREFIX).getStringValue();
configManagementService.createIfNotExists(server.getId(), CommandManager.PREFIX, defaultPrefix);
configManagementService.loadOrCreateIfNotExists(server.getId(), CommandManager.PREFIX, defaultPrefix);
}
}

View File

@@ -2,16 +2,21 @@ package dev.sheldan.abstracto.core.service;
import dev.sheldan.abstracto.core.exception.ConfigurationKeyNotFoundException;
import dev.sheldan.abstracto.core.models.database.AConfig;
import dev.sheldan.abstracto.core.models.property.SystemConfigProperty;
import dev.sheldan.abstracto.core.service.management.ConfigManagementService;
import dev.sheldan.abstracto.core.service.management.DefaultConfigManagementService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@Component
public class ConfigServiceBean implements ConfigService{
public class ConfigServiceBean implements ConfigService {
@Autowired
private ConfigManagementService configManagementService;
@Autowired
private DefaultConfigManagementService defaultConfigManagementService;
@Override
public Double getDoubleValue(String name, Long serverId) {
return getDoubleValue(name, serverId, 0D);
@@ -84,6 +89,27 @@ public class ConfigServiceBean implements ConfigService{
}
@Override
public AConfig setOrCreateConfigValue(String name, Long serverId, String value) {
if(defaultConfigManagementService.configKeyExists(name)) {
AConfig fakeConfigValue = getFakeConfigForValue(name, value);
return setOrCreateConfigValue(serverId, name, fakeConfigValue);
} else {
throw new ConfigurationKeyNotFoundException(name);
}
}
@Override
public AConfig setOrCreateConfigValue(Long serverId, String name, AConfig value) {
if(value.getDoubleValue() != null) {
return configManagementService.setOrCreateDoubleValue(serverId, name, value.getDoubleValue());
} else if(value.getLongValue() != null) {
return configManagementService.setOrCreateLongValue(serverId, name, value.getLongValue());
} else {
return configManagementService.setOrCreateStringValue(serverId, name, value.getStringValue());
}
}
@Override
public void setConfigValue(String name, Long serverId, AConfig value) {
if(value.getDoubleValue() != null) {
@@ -105,9 +131,9 @@ public class ConfigServiceBean implements ConfigService{
}
@Override
public boolean configIsFitting(String name, Long serverId, String value) {
public boolean configurationIsValid(String name, String value) {
try {
getFakeConfigForValue(name, serverId, value);
getFakeConfigForValue(name, value);
return true;
} catch (Exception e) {
return false;
@@ -115,13 +141,13 @@ public class ConfigServiceBean implements ConfigService{
}
@Override
public AConfig getFakeConfigForValue(String name, Long serverId, String value) {
if(configManagementService.configExists(serverId, name)) {
public AConfig getFakeConfigForValue(String name, String value) {
if(defaultConfigManagementService.configKeyExists(name)) {
AConfig newConfig = AConfig.builder().name(value).build();
AConfig existing = configManagementService.loadConfig(serverId, name);
if(existing.getDoubleValue() != null) {
SystemConfigProperty defaultConfig = defaultConfigManagementService.getDefaultConfig(name);
if(defaultConfig.getDoubleValue() != null) {
newConfig.setDoubleValue(Double.parseDouble(value));
} else if(existing.getLongValue() != null) {
} else if(defaultConfig.getLongValue() != null) {
newConfig.setLongValue(Long.parseLong(value));
} else {
newConfig.setStringValue(value);

View File

@@ -39,6 +39,17 @@ public class ConfigManagementServiceBean implements ConfigManagementService {
return config;
}
@Override
public AConfig setOrCreateLongValue(Long serverId, String name, Long value) {
AConfig config = loadConfig(serverId, name);
if(config == null) {
config = createConfig(serverId, name, value);
} else {
config.setLongValue(value);
}
return config;
}
@Override
public AConfig createConfig(Long serverId, String name, String value) {
AServer server = serverManagementService.loadOrCreate(serverId);
@@ -82,7 +93,7 @@ public class ConfigManagementServiceBean implements ConfigManagementService {
}
@Override
public AConfig createIfNotExists(Long serverId, String name, String value) {
public AConfig loadOrCreateIfNotExists(Long serverId, String name, String value) {
AConfig config = loadConfig(serverId, name);
if(config == null) {
return this.createConfig(serverId, name, value);
@@ -91,7 +102,7 @@ public class ConfigManagementServiceBean implements ConfigManagementService {
}
@Override
public AConfig createIfNotExists(Long serverId, String name, Long value) {
public AConfig loadOrCreateIfNotExists(Long serverId, String name, Long value) {
AConfig config = loadConfig(serverId, name);
if(config == null) {
return this.createConfig(serverId, name, value);
@@ -100,7 +111,7 @@ public class ConfigManagementServiceBean implements ConfigManagementService {
}
@Override
public AConfig createIfNotExists(Long serverId, String name, Double value) {
public AConfig loadOrCreateIfNotExists(Long serverId, String name, Double value) {
AConfig config = loadConfig(serverId, name);
if(config == null) {
return this.createConfig(serverId, name, value);

View File

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