package at.gv.egovernment.moa.id.commons.db.dao.config; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.regex.Pattern; import javax.annotation.PostConstruct; import javax.persistence.EntityManager; import javax.persistence.PersistenceContext; import javax.persistence.TypedQuery; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.stereotype.Repository; 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 * */ @Repository @Transactional("transactionManager") public class DatabaseConfigPropertyImpl extends AbstractConfigurationImpl { private Logger log = LoggerFactory.getLogger(getClass()); @PersistenceContext(unitName = "config") private EntityManager em; @Autowired(required=true) private ApplicationContext appContext; static protected String dataBaseEntityName; @PostConstruct void postConstructor() { dataBaseEntityName = appContext.getBean(AbstractConfigProperty.class).getImplementationName(); log.info("DB-Entity for config: " + dataBaseEntityName); } /** * * @return EntityManager for database access */ protected EntityManager getPersistenceContext() { return em; } @Override protected List getAllKeys() throws ConfigurationException { if (null == em) { log.error("No EntityManager set!"); throw new ConfigurationException("No EntityManager set!"); } TypedQuery query = em.createQuery("select key from " + dataBaseEntityName, String.class); List result = query.getResultList(); return result; } /* (non-Javadoc) * @see at.gv.egiz.components.configuration.api.AbstractConfigurationImpl#getValue(java.lang.String) */ @Override protected String getValue(String key) throws ConfigurationException { AbstractConfigProperty 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 { AbstractConfigProperty 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 @Transactional("transactionManager") protected void storeKey(String key, String value) throws ConfigurationException { if (null == em) { log.error("No EntityManager set!"); throw new ConfigurationException("No EntityManager set!"); } AbstractConfigProperty property = appContext.getBean(AbstractConfigProperty.class); property.setKey(key); property.setValue(value); log.debug("Storing '{}'.", property.toString()); // em.persist(property); em.merge(property); } protected void deleteKey(String key) { log.debug("Deleting entry with key '{}'.", key); AbstractConfigProperty el = em.find(appContext.getBean(AbstractConfigProperty.class).getClass(), key); if (el != null) em.remove(el); } /* (non-Javadoc) * @see at.gv.egiz.components.configuration.api.AbstractConfigurationImpl#findConfigurationId(java.lang.String) */ @Override public String[] findConfigurationId(String searchString) throws ConfigurationException { if (null == em) { log.error("No EntityManager set!"); throw new ConfigurationException("No EntityManager set!"); } TypedQuery query = em.createQuery("select key from " + dataBaseEntityName + " dbconfig where dbconfig.key like :key", String.class); query.setParameter("key", searchString.replace("*", "%")); List result = query.getResultList(); if (result == null) return null; else return result.toArray(new String[result.size()]); } /* (non-Javadoc) * @see at.gv.egiz.components.configuration.api.AbstractConfigurationImpl#findByValue(java.lang.String) */ @Override public String[] findByValue(String searchString) throws ConfigurationException { if (null == em) { log.error("No EntityManager set!"); throw new ConfigurationException("No EntityManager set!"); } TypedQuery query = em.createQuery("select * from " + dataBaseEntityName + " dbconfig", AbstractConfigProperty.class); List all = query.getResultList(); searchString = searchString.replace(".", "\\."); String regex = searchString.replace("*", ".*"); regex = regex.replace("%", "\\w*"); log.debug("Searching with regex: {}", regex); Pattern pattern = Pattern.compile(regex); List keyList = new ArrayList(); Iterator keyIt; if (all != null) { keyIt = all.iterator(); while(keyIt.hasNext()) { AbstractConfigProperty entry = keyIt.next(); String value = entry.getValue(); String key = entry.getKey(); if(pattern.matcher(value).matches()) { keyList.add(key); } } } String[] result = new String[keyList.size()]; return keyList.toArray(result); } /* (non-Javadoc) * @see at.gv.egiz.components.configuration.api.AbstractConfigurationImpl#synchronize() */ @Override public void synchronize() throws ConfigurationException { //INFO: no implementation required } /* (non-Javadoc) * @see at.gv.egiz.components.configuration.api.AbstractConfigurationImpl#getName() */ @Override public String getName() { return "DatabaseConfiguration"; } private AbstractConfigProperty getProperty(String key) { log.trace("Looking for configuration property for key '{}'.", key); AbstractConfigProperty result = em.find(appContext.getBean(AbstractConfigProperty.class).getClass(), key); if (result != null) { log.trace("Found configuration property {}.", result); } else { log.debug("Unable to find configuration property for key '{}'.", key); } return result; } /* (non-Javadoc) * @see at.gv.egiz.components.configuration.api.AbstractConfigurationImpl#deleteIds(java.lang.String) */ @Override @Transactional("transactionManager") public void deleteIds(String idSearch) throws ConfigurationException { String[] keyList = findConfigurationId(idSearch); for (String el : keyList) { deleteKey(el); } } }