diff options
Diffstat (limited to 'id/server/moa-id-commons/src/main/java')
-rw-r--r-- | id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/persistence/ConfigurationImpl.java | 162 |
1 files changed, 162 insertions, 0 deletions
diff --git a/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/persistence/ConfigurationImpl.java b/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/persistence/ConfigurationImpl.java new file mode 100644 index 000000000..da2c4b7e6 --- /dev/null +++ b/id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/persistence/ConfigurationImpl.java @@ -0,0 +1,162 @@ +package com.datentechnik.moa.id.conf.persistence; + +import java.io.IOException; +import java.util.List; + +import org.springframework.beans.factory.annotation.Required; +import org.springframework.stereotype.Component; + +import com.datentechnik.moa.id.conf.persistence.dal.ConfigProperty; +import com.datentechnik.moa.id.conf.persistence.dal.ConfigPropertyDao; +import com.fasterxml.jackson.annotation.PropertyAccessor; +import com.fasterxml.jackson.annotation.JsonAutoDetect.Visibility; +import com.fasterxml.jackson.core.JsonParseException; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.SerializationFeature; + +/** + * + * + */ +@Component +public class ConfigurationImpl implements Configuration { + + ConfigPropertyDao configPropertyDao; + private JsonMapper mapper = new JsonMapper(); + + /** + * Sets the {@link ConfigPropertyDao}. + * @param configPropertyDao the ConfigPropertyDao + */ + @Required + public void setConfigPropertyDao(ConfigPropertyDao configPropertyDao) { + this.configPropertyDao = configPropertyDao; + } + + @Override + public Object get(String key) { + // return null if key does not exist + + try { + return mapper.deserialize(getPropertyValue(key), null); + } catch (Exception e) { + return null; + } + } + + @Override + public <T> T get(String key, Class<T> clazz) { + // return null if key does not exist + ConfigProperty property = configPropertyDao.getProperty(key); + if (property != null && property.getValue() != null) { + try { + return clazz.cast(mapper.deserialize(property.getValue(), clazz)); + } catch (IOException e) { + return null; + } + } else { + return null; + } + } + + @Override + public void set(String key, Object value) { + + ConfigProperty keyValue = new ConfigProperty(); + keyValue.setKey(key); + try { + keyValue.setValue(mapper.serialize(value)); + // System.out.println(">> key - value: " + keyValue.getKey() + " - " + keyValue.getValue() + "\n"); + configPropertyDao.saveProperty(keyValue); + + } catch (JsonProcessingException e) { + // TODO do proper error handling + e.printStackTrace(); + } + + } + + @Override + public <T> T get(String key, Class<T> clazz, Object defaultValue) { + // TODO complete the method + T value = get(key, clazz); + if (value != null) { + return value; + } else { + return clazz.cast(defaultValue); + } + } + + @Override + public <T> List<T> getList(String key, Class<T> clazz) { + // return empty list if key does not exist + // TODO Auto-generated method stub + + return null; + } + + private String getPropertyValue(String key) { + ConfigProperty property = configPropertyDao.getProperty(key); + return property.getValue(); + } + + /** + * + * + */ + private class JsonMapper { + + private ObjectMapper mapper = new ObjectMapper(); + + public JsonMapper() { + this(false); + } + + /** + * + * @param prettyPrint + */ + public JsonMapper(boolean prettyPrint) { + mapper.setVisibility(PropertyAccessor.ALL, Visibility.NONE); + mapper.setVisibility(PropertyAccessor.GETTER, Visibility.PUBLIC_ONLY); + mapper.setVisibility(PropertyAccessor.IS_GETTER, Visibility.PUBLIC_ONLY); + if (prettyPrint) { + mapper.enable(SerializationFeature.INDENT_OUTPUT); + } + } + + /** + * + * @param value + * @return + * @throws JsonProcessingException + */ + public String serialize(Object value) throws JsonProcessingException { + return mapper.writeValueAsString(value); + } + + /** + * + * @param value + * @param clazz + * @return + * @throws JsonParseException + * @throws JsonMappingException + * @throws IOException + */ + public <T> Object deserialize(String value, Class<T> clazz) throws JsonParseException, JsonMappingException, IOException { + + ObjectMapper mapper = new ObjectMapper(); + if (clazz != null) { + return mapper.readValue(value, clazz); + } else { + return mapper.readValue(value, Object.class); + } + + } + + }; + +} |