KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > type > CurrencyType


1 //$Id: CurrencyType.java,v 1.2 2004/09/25 11:22:19 oneovthafew Exp $
2
package org.hibernate.type;
3
4 import java.lang.reflect.Method JavaDoc;
5 import java.sql.PreparedStatement JavaDoc;
6 import java.sql.ResultSet JavaDoc;
7 import java.sql.SQLException JavaDoc;
8
9 import org.hibernate.AssertionFailure;
10 import org.hibernate.Hibernate;
11 import org.hibernate.HibernateException;
12
13 /**
14  * <tt>currency</tt>: A type that maps an SQL VARCHAR to a
15  * <tt>java.util.Currency</tt>
16  * @see java.util.Currency
17  * @author Gavin King
18  */

19 public class CurrencyType extends ImmutableType implements LiteralType {
20
21     public static final Class JavaDoc CURRENCY_CLASS;
22     private static final Method JavaDoc CURRENCY_GET_INSTANCE;
23     private static final Method JavaDoc CURRENCY_GET_CODE;
24
25     static {
26         Class JavaDoc clazz;
27         try {
28             clazz = Class.forName("java.util.Currency");
29         }
30         catch (ClassNotFoundException JavaDoc 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 JavaDoc[] { String JavaDoc.class } );
42                 CURRENCY_GET_CODE = clazz.getMethod("getCurrencyCode", new Class JavaDoc[0] );
43             }
44             catch (Exception JavaDoc e) {
45                 throw new AssertionFailure("Exception in static initializer of CurrencyType", e);
46             }
47         }
48     }
49
50     /**
51      * @see org.hibernate.type.NullableType#get(ResultSet, String)
52      */

53     public Object JavaDoc get(ResultSet JavaDoc rs, String JavaDoc name)
54     throws HibernateException, SQLException JavaDoc {
55         String JavaDoc code = (String JavaDoc) Hibernate.STRING.nullSafeGet(rs, name);
56         try {
57             return (code==null) ? null : CURRENCY_GET_INSTANCE.invoke(null, new Object JavaDoc[] { code } );
58         }
59         catch (Exception JavaDoc e) {
60             throw new HibernateException("Could not resolve currency code: " + code);
61         }
62     }
63
64     /**
65      * @see org.hibernate.type.NullableType#set(PreparedStatement, Object, int)
66      */

67     public void set(PreparedStatement JavaDoc st, Object JavaDoc value, int index)
68     throws HibernateException, SQLException JavaDoc {
69         Object JavaDoc code;
70         try {
71             code = CURRENCY_GET_CODE.invoke(value, null);
72         }
73         catch (Exception JavaDoc e) {
74             throw new HibernateException("Could not get Currency code", e);
75         }
76         Hibernate.STRING.set(st, code, index);
77     }
78
79     /**
80      * @see org.hibernate.type.NullableType#sqlType()
81      */

82     public int sqlType() {
83         return Hibernate.STRING.sqlType();
84     }
85
86     /**
87      */

88     public String JavaDoc toString(Object JavaDoc value) throws HibernateException {
89         try {
90             return (String JavaDoc) CURRENCY_GET_CODE.invoke(value, null);
91         }
92         catch (Exception JavaDoc e) {
93             throw new HibernateException("Could not get Currency code", e);
94         }
95     }
96
97     /**
98      * @see org.hibernate.type.Type#getReturnedClass()
99      */

100     public Class JavaDoc getReturnedClass() {
101         return CURRENCY_CLASS;
102     }
103
104     /**
105      * @see org.hibernate.type.Type#getName()
106      */

107     public String JavaDoc getName() {
108         return "currency";
109     }
110
111     /**
112      * @see org.hibernate.type.LiteralType#objectToSQLString(Object)
113      */

114     public String JavaDoc objectToSQLString(Object JavaDoc value) throws Exception JavaDoc {
115         String JavaDoc code;
116         try {
117             code = (String JavaDoc) CURRENCY_GET_CODE.invoke(value, null);
118         }
119         catch (Exception JavaDoc e) {
120             throw new HibernateException("Could not get Currency code", e);
121         }
122         return ( (LiteralType) Hibernate.STRING ).objectToSQLString(code);
123     }
124
125     public Object JavaDoc fromStringValue(String JavaDoc xml) throws HibernateException {
126         try {
127             return CURRENCY_GET_INSTANCE.invoke( null, new Object JavaDoc[] {xml} );
128         }
129         catch (Exception JavaDoc e) {
130             throw new HibernateException("Could not resolve currency code: " + xml);
131         }
132     }
133
134 }
135
136
137
138
139
140
141
Popular Tags