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