aboutsummaryrefslogtreecommitdiff
path: root/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/config/persistence/MOAIDConfigurationImpl.java
diff options
context:
space:
mode:
Diffstat (limited to 'id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/config/persistence/MOAIDConfigurationImpl.java')
-rw-r--r--id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/config/persistence/MOAIDConfigurationImpl.java273
1 files changed, 128 insertions, 145 deletions
diff --git a/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/config/persistence/MOAIDConfigurationImpl.java b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/config/persistence/MOAIDConfigurationImpl.java
index 2d652d748..06e174942 100644
--- a/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/config/persistence/MOAIDConfigurationImpl.java
+++ b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/config/persistence/MOAIDConfigurationImpl.java
@@ -1,51 +1,108 @@
package at.gv.egovernment.moa.id.commons.config.persistence;
+import java.util.HashMap;
+import java.util.Iterator;
import java.util.List;
-import java.util.Properties;
+import java.util.Map;
import javax.persistence.EntityManager;
import javax.persistence.TypedQuery;
-import org.springframework.beans.factory.annotation.Required;
import org.springframework.stereotype.Component;
+import org.springframework.stereotype.Repository;
+import org.springframework.transaction.annotation.EnableTransactionManagement;
+import org.springframework.transaction.annotation.Transactional;
import at.gv.egiz.components.configuration.api.Configuration;
import at.gv.egiz.components.configuration.api.ConfigurationException;
import at.gv.egovernment.moa.id.commons.config.MOAIDConfigurationConstants;
import at.gv.egovernment.moa.id.commons.db.dao.config.ConfigProperty;
-import at.gv.egovernment.moa.id.commons.db.dao.config.ConfigPropertyDaoImpl;
+import at.gv.egovernment.moa.id.commons.db.dao.config.DatabaseConfigPropertyImpl;
+import at.gv.egovernment.moa.id.commons.utils.KeyValueUtils;
import at.gv.egovernment.moa.logging.Logger;
/**
* The implementation of a key-value configuration implementing the {@link Configuration} interface.
* It employs the {@link ConfigPropertyDao} to persist configuration data.
*/
-@Component
-public class MOAIDConfigurationImpl extends ConfigPropertyDaoImpl implements MOAIDConfiguration {
-
- Configuration configPropertyDao;
-// private JsonMapper mapper = new JsonMapper();
+@Repository
+@EnableTransactionManagement
+@Transactional("transactionManager")
+public class MOAIDConfigurationImpl extends DatabaseConfigPropertyImpl implements MOAIDConfiguration {
- /**
- * Sets the {@link ConfigPropertyDao}.
- * @param configPropertyDao the ConfigPropertyDao
+// Configuration configPropertyDao;
+//
+// /**
+// * Sets the {@link ConfigPropertyDao}.
+// * @param configPropertyDao the ConfigPropertyDao
+// */
+// @Required
+// public void setConfigPropertyDao(Configuration configPropertyDao) {
+// this.configPropertyDao = configPropertyDao;
+// }
+
+ /* (non-Javadoc)
+ * @see at.gv.egovernment.moa.id.commons.config.persistence.MOAIDConfiguration#getPropertySubset(java.lang.String)
*/
- @Required
- public void setConfigPropertyDao(Configuration configPropertyDao) {
- this.configPropertyDao = configPropertyDao;
- }
+ public Map<String, String> getPropertySubset(String preFix) throws ConfigurationException{
+ EntityManager em = this.getPersistenceContext();
+ if (null == em) {
+ Logger.error("No EntityManager set!");
+ throw new ConfigurationException("No EntityManager set!");
+
+ }
+
+ TypedQuery<ConfigProperty> configQuery = em.createQuery("select * from ConfigProperty dbconfig where dbconfig.key like :key", ConfigProperty.class);
+ configQuery.setParameter("key", preFix + ".%");
+ List<ConfigProperty> configResult = configQuery.getResultList();
+ if (configResult == null || configResult.isEmpty()) {
+ Logger.warn("Found no configuration keys with prefix: " + preFix + ".%");
+ return null;
+
+ }
+ Logger.trace("Find " + configResult.size() + " key/value pairs with prefix: " + preFix + ".%");
+
+ //build key/value configuration map from database entries
+ Map<String, String> result = getKeyValueFromDatabaseDAO(
+ configResult.iterator(), preFix, true);
+ return result;
+
+ }
/* (non-Javadoc)
- * @see at.gv.egovernment.moa.id.commons.config.persistence.MOAIDConfiguration#getPropertySubset(java.lang.String)
+ * @see at.gv.egovernment.moa.id.commons.config.persistence.MOAIDConfiguration#searchPropertiesWithWildcard(java.lang.String)
*/
- public Properties getPropertySubset(String preFix) throws ConfigurationException{
- // TODO Auto-generated method stub
- return null;
+ @Override
+ public Map<String, String> searchPropertiesWithWildcard(String searchKey)
+ throws ConfigurationException {
+ EntityManager em = this.getPersistenceContext();
+ if (null == em) {
+ Logger.error("No EntityManager set!");
+ throw new ConfigurationException("No EntityManager set!");
+
+ }
+
+ TypedQuery<ConfigProperty> configQuery = em.createQuery("select * from ConfigProperty dbconfig where dbconfig.key like :key", ConfigProperty.class);
+ configQuery.setParameter("key", searchKey.replace("*", "%"));
+ List<ConfigProperty> configResult = configQuery.getResultList();
+
+ if (configResult == null || configResult.isEmpty()) {
+ Logger.warn("Found no configuration keys with searchKey: " + searchKey);
+ return null;
+
+ }
+ Logger.trace("Find " + configResult.size() + " key/value pairs with searchKey: " + searchKey);
+
+ //build key/value configuration map from database entries
+ Map<String, String> result = getKeyValueFromDatabaseDAO(
+ configResult.iterator(), null, false);
+ return result;
+
}
@Override
- public Properties getOnlineApplication(String publicURLPrefix)
+ public Map<String, String> getOnlineApplication(String publicURLPrefix)
throws ConfigurationException {
EntityManager em = this.getPersistenceContext();
if (null == em) {
@@ -54,148 +111,74 @@ public class MOAIDConfigurationImpl extends ConfigPropertyDaoImpl implements MOA
}
+ //search key prefix for online application with this publicURLPrefix
String keyId = MOAIDConfigurationConstants.PREFIX_MOAID_SERVICES
+ ".%."
+ MOAIDConfigurationConstants.SERVICE_UNIQUEIDENTIFIER;
- TypedQuery<ConfigProperty> query = em.createQuery("select * from ConfigProperty dbconfig where dbconfig.key like :key and dbconfig.value = SUBSTRING(:uniqueID, 1, LENGTH(dbconfig.value))", ConfigProperty.class);
- query.setParameter("key", keyId);
- query.setParameter("uniqueID", publicURLPrefix);
- List<ConfigProperty> result = query.getResultList();
+
+ TypedQuery<ConfigProperty> oaSearchQuery = em.createQuery("select dbconfig from ConfigProperty dbconfig where dbconfig.key like :key and dbconfig.value = SUBSTRING(:uniqueID, 1, LENGTH(dbconfig.value))", ConfigProperty.class);
+ oaSearchQuery.setParameter("key", keyId);
+ oaSearchQuery.setParameter("uniqueID", publicURLPrefix);
+ List<ConfigProperty> oaSearchResult = oaSearchQuery.getResultList();
- if (result.size() == 0) {
+ if (oaSearchResult.size() == 0) {
Logger.debug("No entries found.");
- return null;
- }
+ return null; }
- if (result.size() > 1) {
+ if (oaSearchResult.size() > 1) {
Logger.warn("OAIdentifier match to more then one DB-entry!");
return null;
}
- String oaKey = result.get(0).getKey();
- String oaValue = result.get(0).getValue();
- Logger.trace("Find online application with uniqueID: " + oaValue + " and keyID: " + oaKey);
+ String oaIdKey = oaSearchResult.get(0).getKey();
+ String oaIdValue = oaSearchResult.get(0).getValue();
+ Logger.trace("Find online application with uniqueID: " + oaIdValue + " and keyID: " + oaIdKey);
+ //load all online application key/value pairs from database
+ String oaType = KeyValueUtils.getFirstChildAfterPrefix(oaIdKey, MOAIDConfigurationConstants.PREFIX_MOAID_SERVICES);
+ String oaKey = KeyValueUtils.getPrefixFromKey(oaIdKey, MOAIDConfigurationConstants.SERVICE_UNIQUEIDENTIFIER);
-
- return null;
+ TypedQuery<ConfigProperty> oaConfigQuery = em.createQuery("select dbconfig from ConfigProperty dbconfig where dbconfig.key like :key", ConfigProperty.class);
+ oaConfigQuery.setParameter("key", oaKey + ".%");
+ List<ConfigProperty> oaConfigResult = oaConfigQuery.getResultList();
+
+ if (oaConfigResult == null) {
+ Logger.warn("Found no configuration keys with prefix: " + oaKey + ".%");
+ return null;
+ }
+ Logger.trace("Find " + oaConfigResult.size() + " key/value pairs with prefix: " + oaKey + ".%");
+
+ //build key/value configuration map from database entries
+ Map<String, String> result = getKeyValueFromDatabaseDAO(
+ oaConfigResult.iterator(), oaKey, true);
+ result.put(MOAIDConfigurationConstants.PREFIX_MOAID_SERVICES, oaType);
+
+ return result;
}
+
/**
- * Small helper method. NOTE: may return empty properties, but never {@code null}.
- * @param propPrefix the prefix of the desired property.
- * @return the {@link Properties}
+ * Small helper method. NOTE: may return empty configuration properties, but never {@code null}.
+ *
+ * @param propPrefix: the prefix of the desired property.
+ * @param input: List of database objects with key/value information.
+ * @param removePrefix: Indicates if the prefix should be removed from the result key
+ * @return the {@link Map} of configuration properties
*/
- private Properties getGeneralProperiesConfig(Properties input, final String propPrefix) {
-
- Properties configProp = new Properties();
- for (Object key : input.keySet()) {
- if (key.toString().startsWith(propPrefix)) {
- String propertyName = key.toString().substring(propPrefix.length());
- configProp.put(propertyName, input.get(key.toString()));
- }
+ private Map<String, String> getKeyValueFromDatabaseDAO(Iterator<ConfigProperty> input, final String prefix, boolean removePrefix) {
+ Map<String, String> configProp = new HashMap<String, String>();
+ while (input.hasNext()) {
+ ConfigProperty el = input.next();
+ if (removePrefix) {
+ if (el.getKey().startsWith(prefix)) {
+ String propertyName = KeyValueUtils.removePrefixFromKey(el.getKey(), prefix);
+ configProp.put(propertyName, el.getValue());
+
+ }
+ } else
+ configProp.put(el.getKey(), el.getValue());
+
}
return configProp;
}
-
-
-// @Override
-// public List<String> getAllKeys(){
-// try {
-// return Arrays.asList(this.configPropertyDao.getConfigurationIds());
-// } catch (Exception e) {
-// log.debug("Error while retrieving a list of all keys in the database.");
-// return null;
-// }
-// }
-//
-// @Override
-// public String get(String key) {
-// // return null if key does not exist
-// try {
-// return configPropertyDao.getStringValue(key);
-//
-// } catch (Exception e) {
-// log.debug("Error while searching value of key '{}' to object.", key);
-// return null;
-// }
-// }
-//
-// @Override
-// public <T> T get(String key, Class<T> clazz) {
-// // return null if key does not exist
-// try {
-// T property = configPropertyDao.getObjectValue(key, clazz);
-// return property;
-//
-// } catch (IllegalArgumentException e) {
-// log.debug("Error while searching for key '{}' in the database.", key);
-// return null;
-// } catch (Exception e) {
-// log.debug("Error while deserializing value of key '{}' to object of type {}.", key, clazz.getClass());
-// return null;
-// }
-// }
-//
-// @Override
-// public boolean set(String key, String value) {
-//
-// try {
-// //TODO: add delete
-// if (value == null) {
-// //configPropertyDao.delete(key);
-// return true;
-// } else {
-// configPropertyDao.setStringValue(key, value);
-// return true;
-// }
-// } catch (EntityExistsException e) {
-// log.debug("Property '{}' already exists!", key);
-// return false;
-// } catch (Exception e) {
-// log.debug("Error while setting value for key '{}' in the database.", key);
-// return false;
-// }
-// }
-// @Override
-// public <T> T get(String key, Class<T> clazz, Object defaultValue) {
-//
-// T value = get(key, clazz);
-// if (value != null) {
-// return value;
-// } else {
-// return clazz.cast(defaultValue);
-// }
-// }
-//
-// @SuppressWarnings("unchecked")
-// @Override
-// public <T> List<T> getList(String key, Class<T> clazz) {
-//
-// CollectionType listType = TypeFactory.defaultInstance().constructCollectionType(List.class, clazz);
-// try {
-// if ((configPropertyDao.getProperty(key) == null)
-// || (configPropertyDao.getProperty(key).getValue() == null)) {
-// return new ArrayList<T>();
-// }
-// String json = configPropertyDao.getProperty(key).getValue();
-// ObjectMapper mapper = new ObjectMapper();
-//
-// return (List<T>) mapper.readValue(json, listType);
-// } catch (JsonMappingException e) {
-// ArrayList<T> tmp = new ArrayList<T>();
-// T value = get(key, clazz);
-// if (value != null) {
-// tmp.add(value);
-// }
-// return tmp;
-// } catch (IOException e) {
-// log.debug("Error while deserializing value for key '{}' to List<{}>.", key, clazz.getClass());
-// return new ArrayList<T>();
-// } catch (Exception e){
-// log.debug("Error while searching key '{}' in the database.", key);
-// return new ArrayList<T>();
-// }
-// }
-
}