package com.datentechnik.moa.id.conf.persistence; import java.io.IOException; import java.util.ArrayList; 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.JsonAutoDetect.Visibility; import com.fasterxml.jackson.annotation.PropertyAccessor; import com.fasterxml.jackson.core.JsonParseException; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JavaType; import com.fasterxml.jackson.databind.JsonMappingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.SerializationFeature; import com.fasterxml.jackson.databind.type.CollectionType; import com.fasterxml.jackson.databind.type.TypeFactory; /** * * */ @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); } } @SuppressWarnings("unchecked") @Override public List getList(String key, Class clazz) { CollectionType listType = TypeFactory.defaultInstance().constructCollectionType(List.class, clazz); String json = configPropertyDao.getProperty(key).getValue(); ObjectMapper mapper = new ObjectMapper(); try { 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) { return new ArrayList(); } } 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) { JavaType javaType = TypeFactory.defaultInstance().constructType(clazz); return mapper.readValue(value, javaType); } else { return mapper.readValue(value, Object.class); } } }; }