fixed broken query in ConfigService
This commit is contained in:
@ -1,209 +1,210 @@
|
|||||||
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.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.application.entity.ApplicationEntity;
|
import de.muehlencord.shared.account.business.application.entity.ApplicationEntity;
|
||||||
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;
|
||||||
import de.muehlencord.shared.account.business.config.entity.ConfigException;
|
import de.muehlencord.shared.account.business.config.entity.ConfigException;
|
||||||
import de.muehlencord.shared.account.util.AccountPU;
|
import de.muehlencord.shared.account.util.AccountPU;
|
||||||
import java.io.Serializable;
|
import java.io.Serializable;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import javax.ejb.Lock;
|
import javax.ejb.Lock;
|
||||||
import javax.ejb.LockType;
|
import javax.ejb.LockType;
|
||||||
import javax.ejb.Singleton;
|
import javax.ejb.Singleton;
|
||||||
import javax.ejb.Startup;
|
import javax.ejb.Startup;
|
||||||
import javax.inject.Inject;
|
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.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @author joern.muehlencord
|
* @author joern.muehlencord
|
||||||
*/
|
*/
|
||||||
@Singleton
|
@Singleton
|
||||||
@Startup
|
@Startup
|
||||||
public class ConfigService implements Serializable {
|
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);
|
private static final Logger LOGGER = LoggerFactory.getLogger(ConfigService.class);
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
@AccountPU
|
@AccountPU
|
||||||
EntityManager em;
|
EntityManager em;
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
ApplicationEntity application;
|
ApplicationEntity application;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* returns global config key which is not assigned to any. If more than one
|
* returns global config key which is not assigned to any. If more than one
|
||||||
* value is defined for the given key, the key assigned to system is
|
* value is defined for the given key, the key assigned to system is
|
||||||
* returned. If more than one key is defined but system key is not defined,
|
* returned. If more than one key is defined but system key is not defined,
|
||||||
* an exception is thrown.
|
* an exception is thrown.
|
||||||
*
|
*
|
||||||
* @param configKey the key to return
|
* @param configKey the key to return
|
||||||
* @return the configValue belonging to the given configKey
|
* @return the configValue belonging to the given configKey
|
||||||
* @throws
|
* @throws
|
||||||
* de.muehlencord.shared.account.business.config.entity.ConfigException if
|
* de.muehlencord.shared.account.business.config.entity.ConfigException if
|
||||||
* more than one value is defined for the given key but none of the values
|
* more than one value is defined for the given key but none of the values
|
||||||
* is defined for the system user
|
* is defined for the system user
|
||||||
*/
|
*/
|
||||||
public String getConfigValue(String configKey) throws ConfigException {
|
public String getConfigValue(String configKey) throws ConfigException {
|
||||||
Query query = em.createNamedQuery("ConfigEntity.findByConfigKey");
|
Query query = em.createNamedQuery("ConfigEntity.findByConfigKey");
|
||||||
query.setParameter("application", application);
|
query.setParameter("application", application);
|
||||||
query.setParameter("configKey", configKey);
|
query.setParameter("configKey", configKey);
|
||||||
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
|
// key is not found in the database at all
|
||||||
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();
|
||||||
} else {
|
} else {
|
||||||
// if more than one result found, return the one which is assigned to system if present
|
// if more than one result found, return the one which is assigned to system if present
|
||||||
// if not present, throw exception
|
// if not present, throw exception
|
||||||
Optional<ConfigEntity> firstItem = configList.stream()
|
Optional<ConfigEntity> firstItem = configList.stream()
|
||||||
.filter(config -> config.getConfigPK().getConfigKeyAccount().getUsername().equals("system"))
|
.filter(config -> config.getConfigPK().getConfigKeyAccount().getUsername().equals("system"))
|
||||||
.findFirst();
|
.findFirst();
|
||||||
if (firstItem.isPresent()) {
|
if (firstItem.isPresent()) {
|
||||||
return firstItem.get().getConfigValue();
|
return firstItem.get().getConfigValue();
|
||||||
} else {
|
} else {
|
||||||
throw new ConfigException("ConfigKey " + configKey + " not unique and system value not defined");
|
throw new ConfigException("ConfigKey " + configKey + " not unique and system value not defined");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getConfigValue(String configKey, String defaultValue) throws ConfigException {
|
public String getConfigValue(String configKey, String defaultValue) throws ConfigException {
|
||||||
return getConfigValue(configKey, defaultValue, false);
|
return getConfigValue(configKey, defaultValue, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Transactional
|
@Transactional
|
||||||
@Lock(LockType.WRITE)
|
@Lock(LockType.WRITE)
|
||||||
public String getConfigValue(String configKey, String defaultValue, boolean storeDefaultValue) throws ConfigException {
|
public String getConfigValue(String configKey, String defaultValue, boolean storeDefaultValue) throws ConfigException {
|
||||||
// get configValue as usual
|
// get configValue as usual
|
||||||
String configValue = getConfigValue(configKey);
|
String configValue = getConfigValue(configKey);
|
||||||
|
|
||||||
// if config value is not found null has been returned
|
// if config value is not found null has been returned
|
||||||
// in this case the value to return is the defaultValue
|
// in this case the value to return is the defaultValue
|
||||||
if (configValue == null) {
|
if (configValue == null) {
|
||||||
configValue = defaultValue;
|
configValue = defaultValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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) {
|
||||||
AccountEntity account = getAccount("system");
|
AccountEntity account = getAccount("system");
|
||||||
updateConfigValue(configKey, account, configValue);
|
updateConfigValue(configKey, account, configValue);
|
||||||
}
|
}
|
||||||
|
|
||||||
return defaultValue;
|
return defaultValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getConfigValue(String configKey, Account account, boolean fallbackToSystem) 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("application", application);
|
||||||
query.setParameter("account", account);
|
query.setParameter("configKey", configKey);
|
||||||
List<ConfigEntity> configList = query.getResultList();
|
query.setParameter("account", account);
|
||||||
if ((configList == null) || (configList.isEmpty())) {
|
List<ConfigEntity> configList = query.getResultList();
|
||||||
// fallback to default / system value
|
if ((configList == null) || (configList.isEmpty())) {
|
||||||
if (fallbackToSystem) {
|
// fallback to default / system value
|
||||||
return getConfigValue(configKey);
|
if (fallbackToSystem) {
|
||||||
} else {
|
return getConfigValue(configKey);
|
||||||
return null;
|
} else {
|
||||||
}
|
return null;
|
||||||
} else if (configList.size() == 1) {
|
}
|
||||||
// exact one element found, return this one
|
} else if (configList.size() == 1) {
|
||||||
return configList.get(0).getConfigValue();
|
// exact one element found, return this one
|
||||||
} else {
|
return configList.get(0).getConfigValue();
|
||||||
// more than one value must not happen - this is not possible per the defintion of the datamodel
|
} else {
|
||||||
throw new ConfigException("Cannot have more than one value for the the given key " + configKey + " and the given account " + account.getUsername());
|
// more than one value must not happen - this is not possible per the defintion of the datamodel
|
||||||
}
|
throw new ConfigException("Cannot have more than one value for the the given key " + configKey + " and the given account " + account.getUsername());
|
||||||
}
|
}
|
||||||
|
}
|
||||||
@Transactional
|
|
||||||
@Lock(LockType.WRITE)
|
@Transactional
|
||||||
public String getConfigValue(String configKey, String defaultValue, boolean storeDefaultValue, Account account, boolean fallbackToSystem) throws ConfigException {
|
@Lock(LockType.WRITE)
|
||||||
String configValue = getConfigValue(configKey, account, fallbackToSystem);
|
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
|
if (configValue == null) {
|
||||||
configValue = defaultValue;
|
// 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) {
|
// check if the default value should be stored in the database
|
||||||
updateConfigValue(configKey, account, configValue);
|
if (storeDefaultValue) {
|
||||||
}
|
updateConfigValue(configKey, account, configValue);
|
||||||
|
}
|
||||||
return configValue;
|
|
||||||
}
|
return configValue;
|
||||||
|
}
|
||||||
@Transactional
|
|
||||||
@Lock(LockType.WRITE)
|
@Transactional
|
||||||
public boolean updateConfigValue(String configKey, String configValue) throws ConfigException {
|
@Lock(LockType.WRITE)
|
||||||
Account account = getAccount("system");
|
public boolean updateConfigValue(String configKey, String configValue) throws ConfigException {
|
||||||
return updateConfigValue(configKey, account, configValue);
|
Account account = getAccount("system");
|
||||||
}
|
return updateConfigValue(configKey, account, configValue);
|
||||||
|
}
|
||||||
@Transactional
|
|
||||||
@Lock(LockType.WRITE)
|
@Transactional
|
||||||
public boolean updateConfigValue(String configKey, String accountName, String configValue) {
|
@Lock(LockType.WRITE)
|
||||||
Account account = getAccount(accountName);
|
public boolean updateConfigValue(String configKey, String accountName, String configValue) {
|
||||||
if (accountName == null) {
|
Account account = getAccount(accountName);
|
||||||
return false;
|
if (accountName == null) {
|
||||||
}
|
return false;
|
||||||
if (account == null) {
|
}
|
||||||
LOGGER.error("Account for usreName {} not found", accountName);
|
if (account == null) {
|
||||||
return false;
|
LOGGER.error("Account for usreName {} not found", accountName);
|
||||||
}
|
return false;
|
||||||
return updateConfigValue(configKey, account, configValue);
|
}
|
||||||
}
|
return updateConfigValue(configKey, account, configValue);
|
||||||
|
}
|
||||||
@Transactional
|
|
||||||
@Lock(LockType.WRITE)
|
@Transactional
|
||||||
public boolean updateConfigValue(String configKey, Account account, String configValue) {
|
@Lock(LockType.WRITE)
|
||||||
if ((configKey == null) || (configKey.equals("")) || (configValue == null) || (configValue.equals(""))) {
|
public boolean updateConfigValue(String configKey, Account account, String configValue) {
|
||||||
// null or empty key / values are not possible
|
if ((configKey == null) || (configKey.equals("")) || (configValue == null) || (configValue.equals(""))) {
|
||||||
return false;
|
// null or empty key / values are not possible
|
||||||
}
|
return false;
|
||||||
|
}
|
||||||
if (account == null) {
|
|
||||||
LOGGER.error("Account must not be null, not updating");
|
if (account == null) {
|
||||||
return false;
|
LOGGER.error("Account must not be null, not updating");
|
||||||
}
|
return false;
|
||||||
|
}
|
||||||
AccountEntity accountEntity = getAccount(account.getUsername());
|
|
||||||
ConfigEntityPK pk = new ConfigEntityPK(application, configKey, accountEntity);
|
AccountEntity accountEntity = getAccount(account.getUsername());
|
||||||
ConfigEntity currentEntity = em.find(ConfigEntity.class, pk);
|
ConfigEntityPK pk = new ConfigEntityPK(application, configKey, accountEntity);
|
||||||
if (currentEntity == null) {
|
ConfigEntity currentEntity = em.find(ConfigEntity.class, pk);
|
||||||
currentEntity = new ConfigEntity(pk);
|
if (currentEntity == null) {
|
||||||
currentEntity.setConfigValue(configValue);
|
currentEntity = new ConfigEntity(pk);
|
||||||
em.persist(currentEntity);
|
currentEntity.setConfigValue(configValue);
|
||||||
return true; // config item created - udpate performed
|
em.persist(currentEntity);
|
||||||
} else {
|
return true; // config item created - udpate performed
|
||||||
if ((currentEntity.getConfigValue() != null) && (currentEntity.getConfigValue().equals(configValue))) {
|
} else {
|
||||||
// value is the same - no update
|
if ((currentEntity.getConfigValue() != null) && (currentEntity.getConfigValue().equals(configValue))) {
|
||||||
return false;
|
// value is the same - no update
|
||||||
} else {
|
return false;
|
||||||
currentEntity.setConfigValue(configValue);
|
} else {
|
||||||
em.merge(currentEntity);
|
currentEntity.setConfigValue(configValue);
|
||||||
return true;
|
em.merge(currentEntity);
|
||||||
}
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
private AccountEntity getAccount(String accountName) {
|
|
||||||
Query query = em.createNamedQuery("AccountEntity.findByUsername");
|
private AccountEntity getAccount(String accountName) {
|
||||||
query.setParameter("username", accountName);
|
Query query = em.createNamedQuery("AccountEntity.findByUsername");
|
||||||
List<AccountEntity> accountList = query.getResultList();
|
query.setParameter("username", accountName);
|
||||||
if ((accountList == null) || (accountList.isEmpty())) {
|
List<AccountEntity> accountList = query.getResultList();
|
||||||
return null;
|
if ((accountList == null) || (accountList.isEmpty())) {
|
||||||
} else {
|
return null;
|
||||||
return accountList.get(0);
|
} else {
|
||||||
}
|
return accountList.get(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user