📄 enumtype.java
字号:
/*
* JBoss, Home of Professional Open Source
* Copyright 2005, JBoss Inc., and individual contributors as indicated
* by the @authors tag.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the JBPM BPEL PUBLIC LICENSE AGREEMENT as
* published by JBoss Inc.; either version 1.0 of the License, or
* (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*/
package org.jbpm.bpel.db.type;
import java.io.Serializable;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.Properties;
import org.apache.commons.lang.enums.Enum;
import org.apache.commons.lang.enums.EnumUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.usertype.ParameterizedType;
import org.hibernate.usertype.UserType;
/**
* @author Juan Cantu
* @version $Revision: 1.3 $ $Date: 2006/09/27 03:53:01 $
*/
public class EnumType implements UserType, ParameterizedType {
private Class enumClass;
/**
* Enumeration class parameter name.
*/
public static final String ENUM_CLASS_PARAM = "class";
private static final int[] SQL_TYPES = { Types.VARCHAR };
private static final Log log = LogFactory.getLog(EnumType.class);
public boolean equals(Object o1, Object o2) {
return o1 == null ? o2 == null : o1.equals(o2);
}
public int hashCode(Object o) throws HibernateException {
return o.hashCode();
}
public Object deepCopy(Object o) throws HibernateException {
return o;
}
public boolean isMutable() {
return false;
}
public Serializable disassemble(Object o) throws HibernateException {
return (Serializable) o;
}
public Object assemble(Serializable s, Object o) throws HibernateException {
return s;
}
public Object replace(Object original, Object target, Object owner) {
return target;
}
public int[] sqlTypes() {
return SQL_TYPES;
}
public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
throws HibernateException, SQLException {
// get enum constant name
final String columnName = names[0];
String enumName = rs.getString(columnName);
// resolve enum constant
Enum enumConstant = enumName != null ? EnumUtils.getEnum(enumClass,
enumName) : null;
if (log.isTraceEnabled())
log.trace("returning '" + enumConstant + "' as column: " + columnName);
return enumConstant;
}
public void nullSafeSet(PreparedStatement st, Object value, int index)
throws HibernateException, SQLException {
if (value == null) {
st.setNull(index, SQL_TYPES[0]);
if (log.isTraceEnabled())
log.trace("binding null to parameter: " + index);
}
else {
final String enumName = ((Enum) value).getName();
st.setString(index, enumName);
if (log.isTraceEnabled())
log.trace("binding '" + enumName + "' to parameter: " + index);
}
}
public Class returnedClass() {
return enumClass;
}
public void setParameterValues(Properties parameters)
throws HibernateException {
try {
enumClass = Class.forName((String) parameters.get(ENUM_CLASS_PARAM));
}
catch (ClassNotFoundException e) {
throw new HibernateException(e);
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -