KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > hibernate > ce > auction > persistence > MonetaryAmountSimpleUserType


1 package org.hibernate.ce.auction.persistence;
2
3 import org.hibernate.*;
4 import org.hibernate.usertype.UserType;
5 import org.hibernate.ce.auction.model.*;
6
7 import java.math.BigDecimal JavaDoc;
8 import java.sql.*;
9 import java.util.Currency JavaDoc;
10 import java.io.Serializable JavaDoc;
11
12 /**
13  * This is a simple Hibernate custom mapping type for MonetaryAmount value types.
14  * <p>
15  * Note that this mapping type is for legacy databases that only have a
16  * single numeric column to hold monetary amounts. Every <tt>MonetaryAmount</tt>
17  * will be converted to USD (using some conversion magic of the class itself)
18  * and saved to the database.
19  *
20  * @author Christian Bauer <christian@hibernate.org>
21  */

22 public class MonetaryAmountSimpleUserType
23         implements UserType {
24
25     private static final int[] SQL_TYPES = {Types.NUMERIC};
26
27     public int[] sqlTypes() { return SQL_TYPES; }
28
29     public Class JavaDoc returnedClass() { return MonetaryAmount.class; }
30
31     public boolean isMutable() { return false; }
32
33     public Object JavaDoc deepCopy(Object JavaDoc value) {
34         return value; // MonetaryAmount is immutable
35
}
36
37     public boolean equals(Object JavaDoc x, Object JavaDoc y) {
38         if (x == y) return true;
39         if (x == null || y == null) return false;
40         return x.equals(y);
41     }
42
43     public int hashCode(Object JavaDoc o) throws HibernateException { return o.hashCode(); }
44     public Serializable JavaDoc disassemble(Object JavaDoc o) throws HibernateException { return (Serializable JavaDoc)o; }
45     public Object JavaDoc assemble(Serializable JavaDoc cached, Object JavaDoc owner) throws HibernateException { return cached; }
46     // TODO: Whats this?
47
public Object JavaDoc replace(Object JavaDoc o, Object JavaDoc o1, Object JavaDoc o2) throws HibernateException { return null; }
48
49     public Object JavaDoc nullSafeGet(ResultSet resultSet,
50                               String JavaDoc[] names,
51                               Object JavaDoc owner)
52             throws HibernateException, SQLException {
53
54         if (resultSet.wasNull()) return null;
55         BigDecimal JavaDoc valueInUSD = resultSet.getBigDecimal(names[0]);
56         Currency JavaDoc userCurrency = Currency.getInstance("USD");
57         return new MonetaryAmount(valueInUSD, userCurrency);
58     }
59
60     public void nullSafeSet(PreparedStatement statement,
61                             Object JavaDoc value,
62                             int index)
63             throws HibernateException, SQLException {
64
65         if (value == null) {
66             statement.setNull(index, Types.NUMERIC);
67         } else {
68             MonetaryAmount anyCurrency = (MonetaryAmount)value;
69             MonetaryAmount amountInUSD =
70               MonetaryAmount.convert( anyCurrency,
71                                       Currency.getInstance("USD") );
72             statement.setBigDecimal(index, amountInUSD.getValue());
73         }
74     }
75 }
76
Popular Tags