1 package org.hibernate.type; 3 4 import java.sql.PreparedStatement ; 5 import java.sql.ResultSet ; 6 import java.sql.SQLException ; 7 import java.util.HashMap ; 8 import java.util.Iterator ; 9 import java.util.Map ; 10 11 import org.dom4j.Node; 12 import org.hibernate.EntityMode; 13 import org.hibernate.HibernateException; 14 import org.hibernate.MappingException; 15 import org.hibernate.engine.Mapping; 16 import org.hibernate.engine.SessionFactoryImplementor; 17 import org.hibernate.engine.SessionImplementor; 18 19 22 public class MetaType extends AbstractType { 23 24 private final Map values; 25 private final Map keys; 26 private final Type baseType; 27 28 public MetaType(Map values, Type baseType) { 29 this.baseType = baseType; 30 this.values = values; 31 keys = new HashMap (); 32 Iterator iter = values.entrySet().iterator(); 33 while ( iter.hasNext() ) { 34 Map.Entry me = (Map.Entry ) iter.next(); 35 keys.put( me.getValue(), me.getKey() ); 36 } 37 } 38 39 public int[] sqlTypes(Mapping mapping) throws MappingException { 40 return baseType.sqlTypes(mapping); 41 } 42 43 public int getColumnSpan(Mapping mapping) throws MappingException { 44 return baseType.getColumnSpan(mapping); 45 } 46 47 public Class getReturnedClass() { 48 return String .class; 49 } 50 51 public Object nullSafeGet( 52 ResultSet rs, 53 String [] names, 54 SessionImplementor session, 55 Object owner) 56 throws HibernateException, SQLException { 57 Object key = baseType.nullSafeGet(rs, names, session, owner); 58 return key==null ? null : values.get(key); 59 } 60 61 public Object nullSafeGet( 62 ResultSet rs, 63 String name, 64 SessionImplementor session, 65 Object owner) 66 throws HibernateException, SQLException { 67 Object key = baseType.nullSafeGet(rs, name, session, owner); 68 return key==null ? null : values.get(key); 69 } 70 71 public void nullSafeSet(PreparedStatement st, Object value, int index, SessionImplementor session) 72 throws HibernateException, SQLException { 73 baseType.nullSafeSet(st, value==null ? null : keys.get(value), index, session); 74 } 75 76 public void nullSafeSet( 77 PreparedStatement st, 78 Object value, 79 int index, 80 boolean[] settable, 81 SessionImplementor session) 82 throws HibernateException, SQLException { 83 if ( settable[0] ) nullSafeSet(st, value, index, session); 84 } 85 86 public String toLoggableString(Object value, SessionFactoryImplementor factory) throws HibernateException { 87 return toXMLString(value, factory); 88 } 89 90 public String toXMLString(Object value, SessionFactoryImplementor factory) 91 throws HibernateException { 92 return (String ) value; } 94 95 public Object fromXMLString(String xml, Mapping factory) 96 throws HibernateException { 97 return xml; } 99 100 public String getName() { 101 return baseType.getName(); } 103 104 public Object deepCopy(Object value, EntityMode entityMode, SessionFactoryImplementor factory) 105 throws HibernateException { 106 return value; 107 } 108 109 public Object replace( 110 Object original, 111 Object target, 112 SessionImplementor session, 113 Object owner, 114 Map copyCache 115 ) { 116 return original; 117 } 118 119 public boolean isMutable() { 120 return false; 121 } 122 123 public Object fromXMLNode(Node xml, Mapping factory) throws HibernateException { 124 return fromXMLString( xml.getText(), factory ); 125 } 126 127 public void setToXMLNode(Node node, Object value, SessionFactoryImplementor factory) throws HibernateException { 128 node.setText( toXMLString(value, factory) ); 129 } 130 131 public boolean[] toColumnNullness(Object value, Mapping mapping) { 132 throw new UnsupportedOperationException (); 133 } 134 135 } 136 | Popular Tags |