added support for default values

This commit is contained in:
2018-08-23 17:00:30 +02:00
parent 4b2e4e9055
commit a0cda5f498

View File

@ -1,171 +1,193 @@
package de.muehlencord.shared.account.business.config.boundary; package de.muehlencord.shared.account.business.config.boundary;
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;
import de.muehlencord.shared.account.business.config.entity.ConfigException; import de.muehlencord.shared.account.business.config.entity.ConfigException;
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.annotation.PostConstruct; import javax.annotation.PostConstruct;
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;
/** /**
* *
* @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;
@Inject @Inject
EntityManager em; EntityManager em;
// private String storagePath = null; // private String storagePath = null;
// private int maxFailedLogins = 5; // private int maxFailedLogins = 5;
@PostConstruct @PostConstruct
public void init() { public void init() {
// ConfigEntity configEntity = em.find(ConfigEntity.class, "storage.path"); // ConfigEntity configEntity = em.find(ConfigEntity.class, "storage.path");
// if (configEntity != null) { // if (configEntity != null) {
// this.storagePath = configEntity.getConfigValue(); // this.storagePath = configEntity.getConfigValue();
// } // }
// configEntity = em.find(ConfigEntity.class, "account.maxFailedLogins"); // configEntity = em.find(ConfigEntity.class, "account.maxFailedLogins");
// if (configEntity != null) { // if (configEntity != null) {
// this.maxFailedLogins = Integer.parseInt(configEntity.getConfigValue()); // this.maxFailedLogins = Integer.parseInt(configEntity.getConfigValue());
// } // }
} }
/** /**
* 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("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, AccountEntity account) throws ConfigException { public String getConfigValue(String configKey, String defaultValue) throws ConfigException {
Query query = em.createNamedQuery("ConfigEntity.findByConfigKeyAndAccount"); return getConfigValue(configKey, defaultValue, false);
query.setParameter("configKey", configKey); }
query.setParameter("configKeyAccount", account);
List<ConfigEntity> configList = query.getResultList(); public String getConfigValue(String configKey, String defaultValue, boolean storeDefaultValue) throws ConfigException {
if ((configList == null) || (configList.isEmpty())) { // get configValue as usual
// key is not found in the database at all String configValue = getConfigValue(configKey);
return null;
} else if (configList.size() == 1) { // if config value is not found null has been returned
// exact one element found, return this one // in this case the value to return is the defaultValue
return configList.get(0).getConfigValue(); if (configValue == null) {
} else { configValue = defaultValue;
// 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());
} // check if the default value should be stored in the database
} if (storeDefaultValue) {
updateConfigValue(configKey, null, configValue);
@Transactional }
@Lock(LockType.WRITE)
public boolean updateConfigValue(String configKey, String accountName, String configValue) { return defaultValue;
if ((configKey == null) || (configKey.equals ("")) || (configValue == null) || (configValue.equals (""))) { }
// null or empty key / values are not possible
return false; public String getConfigValue(String configKey, AccountEntity account) throws ConfigException {
} Query query = em.createNamedQuery("ConfigEntity.findByConfigKeyAndAccount");
AccountEntity account; query.setParameter("configKey", configKey);
if (accountName == null) { query.setParameter("configKeyAccount", account);
account = getAccount("system"); List<ConfigEntity> configList = query.getResultList();
} else { if ((configList == null) || (configList.isEmpty())) {
account = getAccount(accountName); // key is not found in the database at all
} return null;
} else if (configList.size() == 1) {
if (account == null) { // exact one element found, return this one
// must not happen we have no account - if accountName is null, then the system account has to be used return configList.get(0).getConfigValue();
return false; } 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());
ConfigEntityPK pk = new ConfigEntityPK(configKey, account); }
ConfigEntity currentEntity = em.find(ConfigEntity.class, pk); }
if (currentEntity == null) {
currentEntity = new ConfigEntity(pk); @Transactional
currentEntity.setConfigValue(configValue); @Lock(LockType.WRITE)
em.persist(currentEntity); public boolean updateConfigValue(String configKey, String accountName, String configValue) {
return true; // config item created - udpate performed if ((configKey == null) || (configKey.equals("")) || (configValue == null) || (configValue.equals(""))) {
} else { // null or empty key / values are not possible
if ((currentEntity.getConfigValue() != null) && (currentEntity.getConfigValue().equals(configValue))) { return false;
// value is the same - no update }
return false; AccountEntity account;
} else { if (accountName == null) {
currentEntity.setConfigValue(configValue); account = getAccount("system");
em.merge(currentEntity); } else {
return true; account = getAccount(accountName);
} }
}
} if (account == null) {
// must not happen we have no account - if accountName is null, then the system account has to be used
private AccountEntity getAccount(String accountName) { return false;
Query query = em.createNamedQuery("AccountEntity.findByUsername"); }
query.setParameter("username", accountName);
List<AccountEntity> accountList = query.getResultList(); ConfigEntityPK pk = new ConfigEntityPK(configKey, account);
if ((accountList == null) || (accountList.isEmpty())) { ConfigEntity currentEntity = em.find(ConfigEntity.class, pk);
return null; if (currentEntity == null) {
} else { currentEntity = new ConfigEntity(pk);
return accountList.get(0); currentEntity.setConfigValue(configValue);
} em.persist(currentEntity);
} return true; // config item created - udpate performed
} else {
/* *** getter *** */ if ((currentEntity.getConfigValue() != null) && (currentEntity.getConfigValue().equals(configValue))) {
/** // value is the same - no update
* FIXME remove, this is application specific return false;
* } else {
* @return currentEntity.setConfigValue(configValue);
* @deprecated replace by getConfigValue ("storage.path") em.merge(currentEntity);
*/ return true;
// @Deprecated }
// public String getStoragePath() { }
// return storagePath; }
// }
/** private AccountEntity getAccount(String accountName) {
* // TODO move to accountControl Query query = em.createNamedQuery("AccountEntity.findByUsername");
* query.setParameter("username", accountName);
* @return List<AccountEntity> accountList = query.getResultList();
* @deprecated replace by getConfigValue ("account.maxFailedLogins") if ((accountList == null) || (accountList.isEmpty())) {
*/ return null;
// @Deprecated } else {
// public int getMaxFailedLogins() { return accountList.get(0);
// return maxFailedLogins; }
// } }
}
/* *** getter *** */
/**
* FIXME remove, this is application specific
*
* @return
* @deprecated replace by getConfigValue ("storage.path")
*/
// @Deprecated
// public String getStoragePath() {
// return storagePath;
// }
/**
* // TODO move to accountControl
*
* @return
* @deprecated replace by getConfigValue ("account.maxFailedLogins")
*/
// @Deprecated
// public int getMaxFailedLogins() {
// return maxFailedLogins;
// }
}