package at.gv.egovernment.moa.id.commons.config.persistence; import java.util.Arrays; import java.util.List; import javax.persistence.EntityExistsException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Required; import org.springframework.stereotype.Component; import at.gv.egiz.components.configuration.api.Configuration; /** * 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 implements MOAIDConfiguration { private final Logger log = LoggerFactory.getLogger(getClass()); Configuration configPropertyDao; // private JsonMapper mapper = new JsonMapper(); /** * Sets the {@link ConfigPropertyDao}. * @param configPropertyDao the ConfigPropertyDao */ @Required public void setConfigPropertyDao(Configuration configPropertyDao) { this.configPropertyDao = configPropertyDao; } @Override public List 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 get(String key, Class 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 get(String key, Class clazz, Object defaultValue) { // // T value = get(key, clazz); // if (value != null) { // return value; // } else { // return clazz.cast(defaultValue); // } // } // // @SuppressWarnings("unchecked") // @Override // public List getList(String key, Class clazz) { // // CollectionType listType = TypeFactory.defaultInstance().constructCollectionType(List.class, clazz); // try { // if ((configPropertyDao.getProperty(key) == null) // || (configPropertyDao.getProperty(key).getValue() == null)) { // return new ArrayList(); // } // String json = configPropertyDao.getProperty(key).getValue(); // ObjectMapper mapper = new ObjectMapper(); // // return (List) mapper.readValue(json, listType); // } catch (JsonMappingException e) { // ArrayList tmp = new ArrayList(); // 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(); // } catch (Exception e){ // log.debug("Error while searching key '{}' in the database.", key); // return new ArrayList(); // } // } }