introduced option to load user preferences and fallback to system preference
This commit is contained in:
@ -1,5 +1,6 @@
|
|||||||
package de.muehlencord.shared.account.business.config.boundary;
|
package de.muehlencord.shared.account.business.config.boundary;
|
||||||
|
|
||||||
|
import de.muehlencord.shared.account.business.account.entity.Account;
|
||||||
import de.muehlencord.shared.account.business.account.entity.AccountEntity;
|
import de.muehlencord.shared.account.business.account.entity.AccountEntity;
|
||||||
import de.muehlencord.shared.account.business.config.entity.ConfigEntity;
|
import de.muehlencord.shared.account.business.config.entity.ConfigEntity;
|
||||||
import de.muehlencord.shared.account.business.config.entity.ConfigEntityPK;
|
import de.muehlencord.shared.account.business.config.entity.ConfigEntityPK;
|
||||||
@ -16,6 +17,8 @@ import javax.inject.Inject;
|
|||||||
import javax.persistence.EntityManager;
|
import javax.persistence.EntityManager;
|
||||||
import javax.persistence.Query;
|
import javax.persistence.Query;
|
||||||
import javax.transaction.Transactional;
|
import javax.transaction.Transactional;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
@ -27,6 +30,8 @@ public class ConfigService implements Serializable {
|
|||||||
|
|
||||||
private static final long serialVersionUID = -3195224653632853003L;
|
private static final long serialVersionUID = -3195224653632853003L;
|
||||||
|
|
||||||
|
private static final Logger LOGGER = LoggerFactory.getLogger(ConfigService.class);
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
EntityManager em;
|
EntityManager em;
|
||||||
|
|
||||||
@ -97,20 +102,25 @@ public class ConfigService implements Serializable {
|
|||||||
|
|
||||||
// check if the default value should be stored in the database
|
// check if the default value should be stored in the database
|
||||||
if (storeDefaultValue) {
|
if (storeDefaultValue) {
|
||||||
updateConfigValue(configKey, null, configValue);
|
AccountEntity account = getAccount("system");
|
||||||
|
updateConfigValue(configKey, account, configValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
return defaultValue;
|
return defaultValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getConfigValue(String configKey, AccountEntity account) throws ConfigException {
|
public String getConfigValue(String configKey, Account account, boolean fallbackToSystem) throws ConfigException {
|
||||||
Query query = em.createNamedQuery("ConfigEntity.findByConfigKeyAndAccount");
|
Query query = em.createNamedQuery("ConfigEntity.findByConfigKeyAndAccount");
|
||||||
query.setParameter("configKey", configKey);
|
query.setParameter("configKey", configKey);
|
||||||
query.setParameter("configKeyAccount", account);
|
query.setParameter("account", account);
|
||||||
List<ConfigEntity> configList = query.getResultList();
|
List<ConfigEntity> configList = query.getResultList();
|
||||||
if ((configList == null) || (configList.isEmpty())) {
|
if ((configList == null) || (configList.isEmpty())) {
|
||||||
// key is not found in the database at all
|
// fallback to default / system value
|
||||||
|
if (fallbackToSystem) {
|
||||||
|
return getConfigValue(configKey);
|
||||||
|
} else {
|
||||||
return null;
|
return null;
|
||||||
|
}
|
||||||
} else if (configList.size() == 1) {
|
} else if (configList.size() == 1) {
|
||||||
// exact one element found, return this one
|
// exact one element found, return this one
|
||||||
return configList.get(0).getConfigValue();
|
return configList.get(0).getConfigValue();
|
||||||
@ -120,26 +130,58 @@ public class ConfigService implements Serializable {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public String getConfigValue(String configKey, String defaultValue, boolean storeDefaultValue, Account account, boolean fallbackToSystem) throws ConfigException {
|
||||||
|
String configValue = getConfigValue(configKey, account, fallbackToSystem);
|
||||||
|
|
||||||
|
if (configValue == null) {
|
||||||
|
// value not found for given account and if allowed also not found for system user
|
||||||
|
configValue = defaultValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// check if the default value should be stored in the database
|
||||||
|
if (storeDefaultValue) {
|
||||||
|
updateConfigValue(configKey, account, configValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
return configValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
@Lock(LockType.WRITE)
|
||||||
|
public boolean updateConfigValue(String configKey, String configValue) throws ConfigException {
|
||||||
|
Account account = getAccount("system");
|
||||||
|
return updateConfigValue(configKey, account, configValue);
|
||||||
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
@Lock(LockType.WRITE)
|
@Lock(LockType.WRITE)
|
||||||
public boolean updateConfigValue(String configKey, String accountName, String configValue) {
|
public boolean updateConfigValue(String configKey, String accountName, String configValue) {
|
||||||
|
Account account = getAccount(accountName);
|
||||||
|
if (accountName == null) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if (account == null) {
|
||||||
|
LOGGER.error("Account for usreName {} not found", accountName);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return updateConfigValue(configKey, account, configValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Transactional
|
||||||
|
@Lock(LockType.WRITE)
|
||||||
|
public boolean updateConfigValue(String configKey, Account account, String configValue) {
|
||||||
if ((configKey == null) || (configKey.equals("")) || (configValue == null) || (configValue.equals(""))) {
|
if ((configKey == null) || (configKey.equals("")) || (configValue == null) || (configValue.equals(""))) {
|
||||||
// null or empty key / values are not possible
|
// null or empty key / values are not possible
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
AccountEntity account;
|
|
||||||
if (accountName == null) {
|
|
||||||
account = getAccount("system");
|
|
||||||
} else {
|
|
||||||
account = getAccount(accountName);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (account == null) {
|
if (account == null) {
|
||||||
// must not happen we have no account - if accountName is null, then the system account has to be used
|
LOGGER.error("Account must not be null, not updating");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
ConfigEntityPK pk = new ConfigEntityPK(configKey, account);
|
AccountEntity accountEntity = getAccount (account.getUsername());
|
||||||
|
ConfigEntityPK pk = new ConfigEntityPK(configKey, accountEntity);
|
||||||
ConfigEntity currentEntity = em.find(ConfigEntity.class, pk);
|
ConfigEntity currentEntity = em.find(ConfigEntity.class, pk);
|
||||||
if (currentEntity == null) {
|
if (currentEntity == null) {
|
||||||
currentEntity = new ConfigEntity(pk);
|
currentEntity = new ConfigEntity(pk);
|
||||||
|
|||||||
Reference in New Issue
Block a user