package at.gv.egovernment.moa.id.commons.config.persistence; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import javax.persistence.EntityManager; import javax.persistence.TypedQuery; import org.springframework.stereotype.Component; 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.egiz.eaaf.core.impl.utils.KeyValueUtils; import at.gv.egovernment.moa.id.commons.config.MOAIDConfigurationConstants; import at.gv.egovernment.moa.id.commons.db.dao.config.AbstractConfigProperty; //import at.gv.egovernment.moa.id.commons.db.dao.config.ConfigProperty; import at.gv.egovernment.moa.id.commons.db.dao.config.DatabaseConfigPropertyImpl; 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 @Transactional("transactionManager") public class MOAIDConfigurationImpl extends DatabaseConfigPropertyImpl implements MOAIDConfiguration { // Configuration configPropertyDao; // public void setStringValue(String id, String value) throws ConfigurationException { super.setStringValue(id, value); } public void deleteIds(String idSearch) throws ConfigurationException { super.deleteIds(idSearch); } /* (non-Javadoc) * @see at.gv.egovernment.moa.id.commons.config.persistence.MOAIDConfiguration#getPropertySubset(java.lang.String, boolean) */ @Override public Map getPropertySubset(String preFix, boolean removePrefix) throws ConfigurationException { EntityManager em = this.getPersistenceContext(); if (null == em) { Logger.error("No EntityManager set!"); throw new ConfigurationException("No EntityManager set!"); } TypedQuery configQuery = em.createQuery("select dbconfig from " + dataBaseEntityName + " dbconfig where dbconfig.key like :key", AbstractConfigProperty.class); configQuery.setParameter("key", preFix + "%"); List 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 result = getKeyValueFromDatabaseDAO( configResult.iterator(), preFix, removePrefix); return result; } /* (non-Javadoc) * @see at.gv.egovernment.moa.id.commons.config.persistence.MOAIDConfiguration#getPropertySubset(java.lang.String) */ public Map getPropertySubset(String preFix) throws ConfigurationException{ return getPropertySubset(preFix, true); } /* (non-Javadoc) * @see at.gv.egovernment.moa.id.commons.config.persistence.MOAIDConfiguration#searchPropertiesWithWildcard(java.lang.String) */ @Override public Map searchPropertiesWithWildcard(String searchKey) throws ConfigurationException { EntityManager em = this.getPersistenceContext(); if (null == em) { Logger.error("No EntityManager set!"); throw new ConfigurationException("No EntityManager set!"); } TypedQuery configQuery = em.createQuery("select dbconfig from " + dataBaseEntityName + " dbconfig where dbconfig.key like :key", AbstractConfigProperty.class); configQuery.setParameter("key", searchKey.replace("*", "%")); List 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 result = getKeyValueFromDatabaseDAO( configResult.iterator(), null, false); return result; } @Override public Map getOnlineApplication(String publicURLPrefix) throws ConfigurationException { EntityManager em = this.getPersistenceContext(); if (null == em) { Logger.error("No EntityManager set!"); throw new ConfigurationException("No EntityManager set!"); } //search key prefix for online application with this publicURLPrefix String keyId = MOAIDConfigurationConstants.PREFIX_MOAID_SERVICES + ".%." + MOAIDConfigurationConstants.SERVICE_UNIQUEIDENTIFIER; List oaSearchResult = null; TypedQuery oaSearchQuery = em.createQuery("select dbconfig from " + dataBaseEntityName + " dbconfig where dbconfig.key like :key and dbconfig.value = SUBSTRING(:uniqueID, 1, LENGTH(dbconfig.value))", AbstractConfigProperty.class); oaSearchQuery.setParameter("key", keyId); oaSearchQuery.setParameter("uniqueID", publicURLPrefix); oaSearchResult = oaSearchQuery.getResultList(); return postProcessLoadOnlineApplication(em, oaSearchResult); } /* (non-Javadoc) * @see at.gv.egovernment.moa.id.commons.config.persistence.MOAIDConfiguration#getOnlineApplicationBackupVersion(java.lang.String) */ @Override public Map getOnlineApplicationBackupVersion( String publicURLPrefix) throws ConfigurationException { Logger.debug("Use backup implementation to query configuration database"); EntityManager em = this.getPersistenceContext(); if (null == em) { Logger.error("No EntityManager set!"); throw new ConfigurationException("No EntityManager set!"); } //search key prefix for online application with this publicURLPrefix String keyId = MOAIDConfigurationConstants.PREFIX_MOAID_SERVICES + ".%." + MOAIDConfigurationConstants.SERVICE_UNIQUEIDENTIFIER; List oaSearchResult = new ArrayList(); TypedQuery oaSearchQuery = em.createQuery("select dbconfig from " + dataBaseEntityName + " dbconfig where dbconfig.key like :key", AbstractConfigProperty.class); oaSearchQuery.setParameter("key", keyId); List intermResult = oaSearchQuery.getResultList(); if (intermResult != null) { for (AbstractConfigProperty el : intermResult) { if (publicURLPrefix.startsWith(el.getValue())) oaSearchResult.add(el); } } return postProcessLoadOnlineApplication(em, oaSearchResult); } /** * 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 Map getKeyValueFromDatabaseDAO(Iterator input, final String prefix, boolean removePrefix) { Map configProp = new HashMap(); while (input.hasNext()) { AbstractConfigProperty 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; } /** * Online-Application load operation post-processing * * @param em EntityManager for Database access * @param oaSearchResult Search result of first OA selection operation * @return Map of post-processed OA configuration key/value pairs */ private Map postProcessLoadOnlineApplication(EntityManager em, List oaSearchResult) { if (oaSearchResult == null || oaSearchResult.size() == 0) { Logger.debug("No entries found."); return null; } if (oaSearchResult.size() > 1) { Logger.warn("OAIdentifier match to more then one DB-entry!"); return null; } 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); TypedQuery oaConfigQuery = em.createQuery("select dbconfig from " + dataBaseEntityName + " dbconfig where dbconfig.key like :key", AbstractConfigProperty.class); oaConfigQuery.setParameter("key", oaKey + ".%"); List 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 result = getKeyValueFromDatabaseDAO( oaConfigResult.iterator(), oaKey, true); result.put(MOAIDConfigurationConstants.PREFIX_MOAID_SERVICES, oaType); return result; } }