diff --git a/libraries.gradle b/libraries.gradle index 1d603aaafa..993d947c89 100644 --- a/libraries.gradle +++ b/libraries.gradle @@ -9,6 +9,7 @@ ext { jgroupsVersion = '3.6.3.Final' jaxrsVersion = '2.0.1' jsonSchemaVersion = '2.2.6' + jacksonVersion = '2.4.5' libraries = [ //jUnit (Tests) diff --git a/modules/dbsupport/build.gradle b/modules/dbsupport/build.gradle index 8d5558bc5c..bed9b8146f 100644 --- a/modules/dbsupport/build.gradle +++ b/modules/dbsupport/build.gradle @@ -6,4 +6,5 @@ dependencies { compile libraries.hibernate_envers compile libraries.hibernate_c3p0 compile libraries.hibernate_ehcache + compile "com.fasterxml.jackson.core:jackson-databind:${jacksonVersion}" } diff --git a/modules/dbsupport/src/main/java/org/jpos/ee/converter/ObjectNodeConverter.java b/modules/dbsupport/src/main/java/org/jpos/ee/converter/ObjectNodeConverter.java new file mode 100644 index 0000000000..880a03dcde --- /dev/null +++ b/modules/dbsupport/src/main/java/org/jpos/ee/converter/ObjectNodeConverter.java @@ -0,0 +1,26 @@ +package org.jpos.ee.converter; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import javax.persistence.AttributeConverter; +import javax.persistence.Converter; +import java.io.IOException; + +@Converter(autoApply = true) +public class ObjectNodeConverter implements AttributeConverter { + + @Override + public String convertToDatabaseColumn(ObjectNode attribute) { + return attribute.toString(); + } + + @Override + public ObjectNode convertToEntityAttribute(String dbData) { + try { + return (ObjectNode) new ObjectMapper().readTree(dbData); + } catch (IOException e) { + return null; + } + } +} + diff --git a/modules/dbsupport/src/main/java/org/jpos/ee/usertype/JsonType.java b/modules/dbsupport/src/main/java/org/jpos/ee/usertype/JsonType.java new file mode 100644 index 0000000000..d19e9cd28f --- /dev/null +++ b/modules/dbsupport/src/main/java/org/jpos/ee/usertype/JsonType.java @@ -0,0 +1,89 @@ +package org.jpos.ee.usertype; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.ObjectNode; +import org.hibernate.HibernateException; +import org.hibernate.engine.spi.SessionImplementor; +import org.hibernate.usertype.UserType; + +import java.io.IOException; +import java.io.Serializable; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.sql.Types; + +public class JsonType implements UserType { + + private static int[] TYPES = { Types.VARCHAR }; + private static ObjectMapper mapper = new ObjectMapper(); + + @Override + public int[] sqlTypes() { + return TYPES; + } + + @Override + public Class returnedClass() { + return ObjectNode.class; + } + + @Override + public boolean equals(Object x, Object y) throws HibernateException { + return (x == y) || ((x != null) && (y != null) && (x.equals(y))); + } + + @Override + public int hashCode(Object x) throws HibernateException { + return x.hashCode(); + } + + @Override + public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner) throws HibernateException, SQLException { + String json = rs.getString(names[0]); + if (rs.wasNull()) + return null; + try { + return mapper.readTree(json); + } catch (IOException e) { + return new HibernateException("unable to read object",e); + } + } + + @Override + public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session) throws HibernateException, SQLException { + if (value==null) { + st.setNull(index, Types.VARCHAR); + } else { + st.setString (index, value.toString()); + } + } + + @Override + public Object deepCopy(Object value) throws HibernateException { + if (value == null) + return null; + return mapper.createObjectNode().putAll((ObjectNode) value); + + } + + @Override + public boolean isMutable() { + return true; + } + + @Override + public Serializable disassemble(Object value) throws HibernateException { + return (Serializable) value; + } + + @Override + public Object assemble(Serializable cached, Object owner) throws HibernateException { + return cached; + } + + @Override + public Object replace(Object original, Object target, Object owner) throws HibernateException { + return this.deepCopy(original); + } +}