From c20d8f3da0b112864130031d39800e60289380d4 Mon Sep 17 00:00:00 2001 From: Christian Wagner Date: Tue, 13 Jan 2015 16:12:36 +0100 Subject: add raw configuration persistence functionality some problems: * all tables annotated via '@Table' are created in every database connected * loading of 'Configuration' via Spring isn't pretty at all --- .../moa/id/conf/persistence/ConfigurationImpl.java | 162 +++++++++++++++++++++ 1 file changed, 162 insertions(+) create mode 100644 id/server/moa-id-commons/src/main/java/com/datentechnik/moa/id/conf/persistence/ConfigurationImpl.java (limited to 'id/server/moa-id-commons/src/main/java/com') 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 get(String key, Class 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 get(String key, Class clazz, Object defaultValue) { + // TODO complete the method + T value = get(key, clazz); + if (value != null) { + return value; + } else { + return clazz.cast(defaultValue); + } + } + + @Override + public List getList(String key, Class 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 Object deserialize(String value, Class clazz) throws JsonParseException, JsonMappingException, IOException { + + ObjectMapper mapper = new ObjectMapper(); + if (clazz != null) { + return mapper.readValue(value, clazz); + } else { + return mapper.readValue(value, Object.class); + } + + } + + }; + +} -- cgit v1.2.3