package com.bideafactory.wom.usageconsuption.converter; import org.bson.Document; import org.springframework.core.convert.converter.Converter; import org.springframework.data.convert.ReadingConverter; import org.springframework.data.mongodb.core.convert.MongoConverter; import javax.xml.bind.JAXBElement; import javax.xml.namespace.QName; import java.util.LinkedHashMap; import java.util.List; @ReadingConverter public class JAXBElementReaderConverter implements Converter { private final MongoConverter converter; public JAXBElementReaderConverter(MongoConverter converter) { this.converter = converter; } @Override @SuppressWarnings("unchecked") public JAXBElement convert(Document dbObject) { Class declaredType, scope; Document qName = (Document) dbObject.get("name"); QName name = new QName(qName.getString("namespaceURI"), qName.getString("localPart"), qName.getString("prefix")); Object rawValue = dbObject.get("value"); try { declaredType = Class.forName((String) dbObject.get("declaredType")); } catch (ClassNotFoundException e) { if (rawValue.getClass().isArray()) declaredType = List.class; else declaredType = LinkedHashMap.class; } try { scope = Class.forName((String) dbObject.get("scope")); } catch (ClassNotFoundException e) { scope = JAXBElement.GlobalScope.class; } Object value = rawValue instanceof Document ? converter.read(declaredType, (Document) rawValue) : rawValue; return new JAXBElement(name, declaredType, scope, value); } }