package org.jbpm.env.session.hibernate;
import java.io.Serializable;
import java.lang.reflect.Method;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Properties;
import java.util.logging.Logger;
import org.hibernate.HibernateException;
import org.hibernate.usertype.ParameterizedType;
import org.hibernate.usertype.UserType;
import org.jbpm.pvm.Execution;
import org.jbpm.pvm.impl.ExecutionImpl;
import org.jbpm.pvm.impl.NodeImpl;
import org.jbpm.pvm.impl.ProcessDefinitionImpl;
public class ExecutionType implements UserType, ParameterizedType {
private static final long serialVersionUID = 1L;
private static final Logger log = Logger.getLogger(ExecutionType.class.getName());
int[] sqlTypes = new int[]{Types.VARCHAR};
protected String processResource;
protected ProcessDefinitionImpl processDefinition;
public void setParameterValues(Properties properties) {
}
public int[] sqlTypes() {
return sqlTypes;
}
// JDBC - object translation
public Object nullSafeGet(ResultSet rs, String[] names, Object owner) throws HibernateException, SQLException {
ExecutionImpl execution = createExecution(owner);
String nodeName = rs.getString(names[0]);
NodeImpl node = (NodeImpl) execution.getProcessDefinition().getNode(nodeName);
execution.setNode(node);
return execution;
}
private ExecutionImpl createExecution(Object owner) {
ExecutionImpl execution = new ExecutionImpl();
execution.setProcess(getProcessDefinition(owner));
return execution;
}
private synchronized ProcessDefinitionImpl getProcessDefinition(Object owner) {
if (processDefinition!=null) {
return processDefinition;
}
Class<?> ownerClass = owner.getClass();
try {
Method method = ownerClass.getMethod("getProcessDefinition");
processDefinition = (ProcessDefinitionImpl) method.invoke(null, (Object[]) null);
} catch (Exception e) {
throw new RuntimeException("couldn't get process definition for "+owner);
}
return processDefinition;
}
public void nullSafeSet(PreparedStatement st, Object owner, int index) throws HibernateException, SQLException {
if (owner!=null) {
ExecutionImpl execution = (ExecutionImpl) owner;
String nodeName = execution.getNode().getName();
log.finest("binding 'execution-state{"+nodeName+"}' to parameter: "+index);
st.setString(index, nodeName);
}
}
// for dirty checking ?
public Object deepCopy(Object object) throws HibernateException {
if (object==null) {
return null;
}
ExecutionImpl original = (ExecutionImpl) object;
NodeImpl node = new NodeImpl();
node.setName(original.getNode().getName());
ExecutionImpl copy = new ExecutionImpl();
copy.setNode(node);
return copy;
}
public boolean equals(Object arg0, Object arg1) throws HibernateException {
if ( (arg0==null) || (arg1==null)) return false;
ExecutionImpl execution0 = (ExecutionImpl) arg0;
ExecutionImpl execution1 = (ExecutionImpl) arg1;
String nodeName0 = execution0.getNode().getName();
String nodeName1 = execution1.getNode().getName();
return nodeName0.equals(nodeName1);
}
public int hashCode(Object object) throws HibernateException {
return object.hashCode();
}
public boolean isMutable() {
return true;
}
public Class<?> returnedClass() {
return Execution.class;
}
// merge functionality //////////////////////////////////////////////////////
public Object replace(Object arg0, Object arg1, Object arg2) throws HibernateException {
return null;
}
// serialization for cache //////////////////////////////////////////////////
public Object assemble(Serializable arg0, Object arg1) throws HibernateException {
return null;
}
public Serializable disassemble(Object arg0) throws HibernateException {
return null;
}
}