diff options
Diffstat (limited to 'id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/dao/config/ConfigPropertyDaoImpl.java')
-rw-r--r-- | id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/dao/config/ConfigPropertyDaoImpl.java | 169 |
1 files changed, 126 insertions, 43 deletions
diff --git a/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/dao/config/ConfigPropertyDaoImpl.java b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/dao/config/ConfigPropertyDaoImpl.java index 6a76c1d17..24195b0cf 100644 --- a/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/dao/config/ConfigPropertyDaoImpl.java +++ b/id/server/moa-id-commons/src/main/java/at/gv/egovernment/moa/id/commons/db/dao/config/ConfigPropertyDaoImpl.java @@ -1,10 +1,8 @@ package at.gv.egovernment.moa.id.commons.db.dao.config; import java.util.List; -import java.util.Set; import javax.persistence.EntityManager; -import javax.persistence.NoResultException; import javax.persistence.PersistenceContext; import javax.persistence.TypedQuery; @@ -12,20 +10,25 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.transaction.annotation.Transactional; +import at.gv.egiz.components.configuration.api.AbstractConfigurationImpl; +import at.gv.egiz.components.configuration.api.ConfigurationException; +import at.gv.egovernment.moa.util.MiscUtil; + /** * Database backed implementation of the DAO interface * */ @Transactional("transactionManager") -public class ConfigPropertyDaoImpl implements ConfigPropertyDao { +public class ConfigPropertyDaoImpl extends AbstractConfigurationImpl { private Logger log = LoggerFactory.getLogger(getClass()); @PersistenceContext(unitName = "moaidconf") private EntityManager em; + @Override - public List<String> getAllKeys() { + protected List<String> getAllKeys() { if (null == em) { log.error("No EntityManager set!"); return null; @@ -35,70 +38,150 @@ public class ConfigPropertyDaoImpl implements ConfigPropertyDao { return result; } + /* (non-Javadoc) + * @see at.gv.egiz.components.configuration.api.AbstractConfigurationImpl#getValue(java.lang.String) + */ + @Override + protected String getValue(String key) throws ConfigurationException { + ConfigProperty property = getProperty(key); + if (property == null) + return null; + + else { + if (MiscUtil.isEmpty(property.getValue())) + return new String(); + else + return property.getValue(); + + } + } + + /* (non-Javadoc) + * @see at.gv.egiz.components.configuration.api.AbstractConfigurationImpl#containsKey(java.lang.String) + */ + @Override + protected boolean containsKey(String key) throws ConfigurationException { + ConfigProperty property = getProperty(key); + if (property == null) + return false; + else + return true; + + } + + /* (non-Javadoc) + * @see at.gv.egiz.components.configuration.api.AbstractConfigurationImpl#storeKey(java.lang.String, java.lang.String) + */ @Override - public void saveProperty(ConfigProperty property) { + protected void storeKey(String key, String value) throws ConfigurationException { if (null == em) { log.error("No EntityManager set!"); return; + } - + ConfigProperty property = new ConfigProperty(); + property.setKey(key); + property.setValue(value); log.debug("Storing '{}'.", property.toString()); em.persist(property); + } - @Override - public ConfigProperty getProperty(String key) { - log.debug("Looking for configuration property for key '{}'.", key); - ConfigProperty result = em.find(ConfigProperty.class, key); - if (result != null) { - log.debug("Found configuration property {}.", result); - } else { - log.debug("Unable to find configuration property for key '{}'.", key); - } - return result; + protected void deleteKey(String key) { + log.debug("Deleting entry with key '{}'.", key); + em.remove(em.find(ConfigProperty.class, key)); } - + + /* (non-Javadoc) + * @see at.gv.egiz.components.configuration.api.AbstractConfigurationImpl#findConfigurationId(java.lang.String) + */ @Override - public String getPropertyValue(String key) { - ConfigProperty property = getProperty(key); - if (property == null) { + public String[] findConfigurationId(String searchString) + throws ConfigurationException { + if (null == em) { + log.error("No EntityManager set!"); return null; } - return property.getValue(); + + TypedQuery<String> query = em.createQuery("select * from ConfigProperty dbconfig where dbconfig.key like :key", String.class); + query.setParameter("key", searchString.replace("*", "%")); + List<String> result = query.getResultList(); + return result.toArray(new String[result.size()]); } + /* (non-Javadoc) + * @see at.gv.egiz.components.configuration.api.AbstractConfigurationImpl#findByValue(java.lang.String) + */ @Override - public List<ConfigProperty> getProperties() { - + public String[] findByValue(String searchString) + throws ConfigurationException { if (null == em) { log.error("No EntityManager set!"); return null; } - - log.debug("Retrieving all properties from database."); - TypedQuery<ConfigProperty> query = em.createQuery("select mc from ConfigProperty mc", ConfigProperty.class); - try { - List<ConfigProperty> propertiesList = query.getResultList(); - return propertiesList; - } catch (NoResultException e) { - log.debug("No property found in database."); - return null; - } + + TypedQuery<String> query = em.createQuery("select * from ConfigProperty dbconfig where dbconfig.value like :value", String.class); + query.setParameter("value", searchString.replace("*", "%")); + List<String> result = query.getResultList(); + return result.toArray(new String[result.size()]); } + /* (non-Javadoc) + * @see at.gv.egiz.components.configuration.api.AbstractConfigurationImpl#synchronize() + */ @Override - public void saveProperties(Set<ConfigProperty> properties) { - log.debug("Storing {} properties to database.", properties.size()); - for (ConfigProperty cp : properties) { - saveProperty(cp); - } - em.flush(); + public void synchronize() throws ConfigurationException { + //INFO: no implementation required + } + /* (non-Javadoc) + * @see at.gv.egiz.components.configuration.api.AbstractConfigurationImpl#getName() + */ @Override - public void delete(String key) { - log.debug("Deleting entry with key '{}'.", key); - em.remove(em.find(ConfigProperty.class, key)); + public String getName() { + return "DatabaseConfiguration"; + } + + + private ConfigProperty getProperty(String key) { + log.debug("Looking for configuration property for key '{}'.", key); + ConfigProperty result = em.find(ConfigProperty.class, key); + if (result != null) { + log.debug("Found configuration property {}.", result); + } else { + log.debug("Unable to find configuration property for key '{}'.", key); + } + return result; } -} + +// @Override +// public String getPropertyValue(String key) { +// ConfigProperty property = getProperty(key); +// if (property == null) { +// return null; +// } +// return property.getValue(); +// } +// +// @Override +// public List<ConfigProperty> getProperties() { +// +// if (null == em) { +// log.error("No EntityManager set!"); +// return null; +// } +// +// log.debug("Retrieving all properties from database."); +// TypedQuery<ConfigProperty> query = em.createQuery("select mc from ConfigProperty mc", ConfigProperty.class); +// try { +// List<ConfigProperty> propertiesList = query.getResultList(); +// return propertiesList; +// } catch (NoResultException e) { +// log.debug("No property found in database."); +// return null; +// } +// } + +}
\ No newline at end of file |