1 package org.hibernate.type; 3 4 import java.lang.reflect.Method ; 5 import java.sql.PreparedStatement ; 6 import java.sql.ResultSet ; 7 import java.sql.SQLException ; 8 9 import org.hibernate.AssertionFailure; 10 import org.hibernate.Hibernate; 11 import org.hibernate.HibernateException; 12 13 19 public class CurrencyType extends ImmutableType implements LiteralType { 20 21 public static final Class CURRENCY_CLASS; 22 private static final Method CURRENCY_GET_INSTANCE; 23 private static final Method CURRENCY_GET_CODE; 24 25 static { 26 Class clazz; 27 try { 28 clazz = Class.forName("java.util.Currency"); 29 } 30 catch (ClassNotFoundException cnfe) { 31 clazz = null; 32 } 33 if (clazz==null) { 34 CURRENCY_CLASS = null; 35 CURRENCY_GET_INSTANCE = null; 36 CURRENCY_GET_CODE = null; 37 } 38 else { 39 CURRENCY_CLASS = clazz; 40 try { 41 CURRENCY_GET_INSTANCE = clazz.getMethod("getInstance", new Class [] { String .class } ); 42 CURRENCY_GET_CODE = clazz.getMethod("getCurrencyCode", new Class [0] ); 43 } 44 catch (Exception e) { 45 throw new AssertionFailure("Exception in static initializer of CurrencyType", e); 46 } 47 } 48 } 49 50 53 public Object get(ResultSet rs, String name) 54 throws HibernateException, SQLException { 55 String code = (String ) Hibernate.STRING.nullSafeGet(rs, name); 56 try { 57 return (code==null) ? null : CURRENCY_GET_INSTANCE.invoke(null, new Object [] { code } ); 58 } 59 catch (Exception e) { 60 throw new HibernateException("Could not resolve currency code: " + code); 61 } 62 } 63 64 67 public void set(PreparedStatement st, Object value, int index) 68 throws HibernateException, SQLException { 69 Object code; 70 try { 71 code = CURRENCY_GET_CODE.invoke(value, null); 72 } 73 catch (Exception e) { 74 throw new HibernateException("Could not get Currency code", e); 75 } 76 Hibernate.STRING.set(st, code, index); 77 } 78 79 82 public int sqlType() { 83 return Hibernate.STRING.sqlType(); 84 } 85 86 88 public String toString(Object value) throws HibernateException { 89 try { 90 return (String ) CURRENCY_GET_CODE.invoke(value, null); 91 } 92 catch (Exception e) { 93 throw new HibernateException("Could not get Currency code", e); 94 } 95 } 96 97 100 public Class getReturnedClass() { 101 return CURRENCY_CLASS; 102 } 103 104 107 public String getName() { 108 return "currency"; 109 } 110 111 114 public String objectToSQLString(Object value) throws Exception { 115 String code; 116 try { 117 code = (String ) CURRENCY_GET_CODE.invoke(value, null); 118 } 119 catch (Exception e) { 120 throw new HibernateException("Could not get Currency code", e); 121 } 122 return ( (LiteralType) Hibernate.STRING ).objectToSQLString(code); 123 } 124 125 public Object fromStringValue(String xml) throws HibernateException { 126 try { 127 return CURRENCY_GET_INSTANCE.invoke( null, new Object [] {xml} ); 128 } 129 catch (Exception e) { 130 throw new HibernateException("Could not resolve currency code: " + xml); 131 } 132 } 133 134 } 135 136 137 138 139 140 141 | Popular Tags |