argos/dmp-backend/web/src/main/java/eu/eudat/configurations/typedefinition/XMLType.java

86 lines
2.5 KiB
Java
Raw Normal View History

2018-06-27 12:29:21 +02:00
package eu.eudat.configurations.typedefinition;
2017-12-15 00:01:26 +01:00
2018-02-16 11:34:02 +01:00
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SharedSessionContractImplementor;
import org.hibernate.usertype.UserType;
2017-12-15 00:01:26 +01:00
import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
/**
* Store and retrieve a PostgreSQL "xml" column as a Java string.
*/
public class XMLType implements UserType {
2018-02-16 11:34:02 +01:00
private final int[] sqlTypesSupported = new int[]{Types.VARCHAR};
2017-12-15 00:01:26 +01:00
public int[] sqlTypes() {
return sqlTypesSupported;
}
2018-02-16 11:34:02 +01:00
2017-12-15 00:01:26 +01:00
public Class returnedClass() {
return String.class;
}
2018-02-16 11:34:02 +01:00
2017-12-15 00:01:26 +01:00
public boolean equals(Object x, Object y) throws HibernateException {
if (x == null) {
return y == null;
} else {
return x.equals(y);
}
}
2018-02-16 11:34:02 +01:00
2017-12-15 00:01:26 +01:00
public int hashCode(Object x) throws HibernateException {
return x == null ? null : x.hashCode();
}
2018-02-16 11:34:02 +01:00
2017-12-15 00:01:26 +01:00
public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException {
2018-02-16 11:34:02 +01:00
assert (names.length == 1);
String xmldoc = rs.getString(names[0]);
2017-12-15 00:01:26 +01:00
return rs.wasNull() ? null : xmldoc;
}
2018-02-16 11:34:02 +01:00
2017-12-15 00:01:26 +01:00
public void nullSafeSet(PreparedStatement st, Object value, int index) throws HibernateException, SQLException {
if (value == null) {
st.setNull(index, Types.OTHER);
} else {
st.setObject(index, value, Types.OTHER);
}
}
2018-02-16 11:34:02 +01:00
2017-12-15 00:01:26 +01:00
public Object deepCopy(Object value) throws HibernateException {
if (value == null)
return null;
2018-02-16 11:34:02 +01:00
return new String((String) value);
2017-12-15 00:01:26 +01:00
}
2018-02-16 11:34:02 +01:00
2017-12-15 00:01:26 +01:00
public boolean isMutable() {
return false;
}
2018-02-16 11:34:02 +01:00
2017-12-15 00:01:26 +01:00
public Serializable disassemble(Object value) throws HibernateException {
return (String) value;
}
2018-02-16 11:34:02 +01:00
2017-12-15 00:01:26 +01:00
public Object assemble(Serializable cached, Object owner) throws HibernateException {
return (String) cached;
}
2018-02-16 11:34:02 +01:00
2017-12-15 00:01:26 +01:00
public Object replace(Object original, Object target, Object owner) throws HibernateException {
return original;
}
2018-02-16 11:34:02 +01:00
public Object nullSafeGet(ResultSet rs, String[] names, SharedSessionContractImplementor session, Object owner)
throws HibernateException, SQLException {
return nullSafeGet(rs, names, owner);
}
public void nullSafeSet(PreparedStatement st, Object value, int index, SharedSessionContractImplementor session) throws HibernateException, SQLException {
nullSafeSet(st, value, index);
}
2017-12-15 00:01:26 +01:00
}