Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Untitled

By: a guest on Nov 2nd, 2012  |  syntax: Java  |  size: 2.82 KB  |  views: 91  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.  
  2.  
  3. import xxx.EnumAlternativeName;
  4. import com.google.common.base.Preconditions;
  5. import com.google.common.collect.Lists;
  6. import com.google.common.collect.Maps;
  7. import org.apache.ibatis.type.BaseTypeHandler;
  8. import org.apache.ibatis.type.JdbcType;
  9.  
  10. import java.sql.CallableStatement;
  11. import java.sql.PreparedStatement;
  12. import java.sql.ResultSet;
  13. import java.sql.SQLException;
  14. import java.util.List;
  15. import java.util.Map;
  16.  
  17. /**
  18.  * <result column="location" property="location" jdbcType="VARCHAR" typeHandler="xxx.noscan.EnumAlternativeNameTypeHandler"/>
  19.  *
  20.  * @author Sebastien Lorber <i>(lorber.sebastien@gmail.com)</i>
  21.  * @param <E>
  22.  */
  23. public class EnumAlternativeNameTypeHandler<E extends Enum<E> & EnumAlternativeName> extends BaseTypeHandler<E> {
  24.  
  25.   private Class<E> type;
  26.   private Map<String,E> mappedByAlternativeName;
  27.  
  28.  
  29.   public EnumAlternativeNameTypeHandler() {
  30.     throw new IllegalStateException("" +
  31.             "You are trying to use a EnumAlternativeNameTypeHandler without declaring the javaType." +
  32.             "Read the EnumAlternativeNameTypeHandler javadoc for details! " +
  33.             "If this happens, it may be because  ");
  34.   }
  35.  
  36.   public EnumAlternativeNameTypeHandler(Class<E> type) {
  37.     Preconditions.checkNotNull(type);
  38.     Preconditions.checkNotNull(type.getEnumConstants(),type.getSimpleName() + " does not represent an enum type.");
  39.     List<E> enums = Lists.newArrayList(type.getEnumConstants());
  40.     this.type = type;
  41.     this.mappedByAlternativeName = Maps.uniqueIndex(enums,EnumAlternativeName.GET_ALTERNATIVE_NAME);
  42.   }
  43.  
  44.   private E getEnumConstFromAlternativeName(String alternativeName) {
  45.     if ( alternativeName == null ) {
  46.       return null;
  47.     } else {
  48.       E enumConst = mappedByAlternativeName.get(alternativeName);
  49.       Preconditions.checkNotNull(enumConst,"No enum could be found for "+type+" with alternative name " + alternativeName);
  50.       return enumConst;
  51.     }
  52.   }
  53.  
  54.   @Override
  55.   public void setNonNullParameter(PreparedStatement ps, int i, E parameter, JdbcType jdbcType) throws SQLException {
  56.     if (jdbcType == null) {
  57.       ps.setString(i, parameter.getAlternativeName() );
  58.     } else {
  59.       ps.setObject(i, parameter.getAlternativeName() , jdbcType.TYPE_CODE);
  60.     }
  61.   }
  62.  
  63.   @Override
  64.   public E getNullableResult(ResultSet rs, String columnName) throws SQLException {
  65.     String s = rs.getString(columnName);
  66.     return getEnumConstFromAlternativeName(s);
  67.   }
  68.  
  69.   @Override
  70.   public E getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
  71.     String s = rs.getString(columnIndex);
  72.     return getEnumConstFromAlternativeName(s);
  73.   }
  74.  
  75.   @Override
  76.   public E getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
  77.     String s = cs.getString(columnIndex);
  78.     return getEnumConstFromAlternativeName(s);
  79.   }
  80.  
  81. }
clone this paste RAW Paste Data