package fr.ina.research.rex.model.sql;
import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.commons.lang.ObjectUtils;
import org.hibernate.HibernateException;
import org.hibernate.engine.spi.SessionImplementor;
import org.hibernate.type.StandardBasicTypes;
import org.hibernate.type.Type;
import org.hibernate.usertype.CompositeUserType;
import fr.ina.research.rex.model.core.FCPid;
public class FCPidType implements CompositeUserType {
@Override
public Object assemble(Serializable cached, SessionImplementor session, Object owner) throws HibernateException {
return deepCopy(cached);
}
@Override
public Object deepCopy(Object value) throws HibernateException {
FCPid returnVal = new FCPid();
FCPid currVal = (FCPid) value;
returnVal.setNamespace(currVal.getNamespace());
returnVal.setId(currVal.getId());
return returnVal;
}
@Override
public Serializable disassemble(Object value, SessionImplementor session) throws HibernateException {
Object deepCopy = deepCopy(value);
if (!(deepCopy instanceof Serializable)) {
return (Serializable) deepCopy;
}
return null;
}
@Override
public boolean equals(Object x, Object y) throws HibernateException {
return ObjectUtils.equals(x, y);
}
@Override
public String[] getPropertyNames() {
return new String[] { "namespace", "id" };
}
@Override
public Type[] getPropertyTypes() {
return new Type[] { StandardBasicTypes.STRING, StandardBasicTypes.STRING };
}
@Override
public Object getPropertyValue(Object component, int property) throws HibernateException {
if (component == null) {
return null;
} else {
if (property == 0) {
return ((FCPid) component).getNamespace();
} else if (property == 1) {
return ((FCPid) component).getId();
}
}
return null;
}
@Override
public int hashCode(Object x) throws HibernateException {
assert (x != null);
return x.hashCode();
}
@Override
public boolean isMutable() {
return false;
}
@Override
public Object nullSafeGet(ResultSet rs, String[] names, SessionImplementor session, Object owner) throws HibernateException, SQLException {
String namespace = rs.getString(names[0]);
String id = rs.getString(names[1]);
if (namespace != null && id != null) {
return new FCPid(namespace, id);
} else {
return null;
}
}
@Override
public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session) throws HibernateException, SQLException {
if (value != null) {
st.setString(index, ((FCPid) value).getNamespace());
st.setString(index + 1, ((FCPid) value).getId());
} else {
st.setObject(index, null);
st.setObject(index + 1, null);
}
}
@Override
public Object replace(Object original, Object target, SessionImplementor session, Object owner) throws HibernateException {
return deepCopy(original);
}
@Override
public Class<FCPid> returnedClass() {
return FCPid.class;
}
@Override
public void setPropertyValue(Object component, int property, Object value) throws HibernateException {
if (value != null) {
if (property == 0) {
((FCPid) component).setNamespace((String) value);
} else if (property == 1) {
((FCPid) component).setId((String) value);
}
}
}
}