KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: CustomType.java,v 1.20 2005/07/19 18:17:14 oneovthafew Exp $
2
package org.hibernate.type;
3
4 import java.io.Serializable JavaDoc;
5 import java.sql.PreparedStatement JavaDoc;
6 import java.sql.ResultSet JavaDoc;
7 import java.sql.SQLException JavaDoc;
8 import java.util.Arrays JavaDoc;
9 import java.util.Comparator JavaDoc;
10 import java.util.Map JavaDoc;
11 import java.util.Properties JavaDoc;
12
13 import org.dom4j.Node;
14 import org.hibernate.EntityMode;
15 import org.hibernate.HibernateException;
16 import org.hibernate.MappingException;
17 import org.hibernate.engine.Mapping;
18 import org.hibernate.engine.SessionFactoryImplementor;
19 import org.hibernate.engine.SessionImplementor;
20 import org.hibernate.usertype.EnhancedUserType;
21 import org.hibernate.usertype.UserType;
22 import org.hibernate.usertype.UserVersionType;
23
24 /**
25  * Adapts <tt>UserType</tt> to the generic <tt>Type</tt> interface.
26  *
27  * @see org.hibernate.usertype.UserType
28  * @author Gavin King
29  */

30 public class CustomType extends AbstractType implements IdentifierType, DiscriminatorType, VersionType {
31
32     private final UserType userType;
33     private final String JavaDoc name;
34     private final int[] types;
35
36     public CustomType(Class JavaDoc userTypeClass, Properties JavaDoc parameters) throws MappingException {
37
38         name = userTypeClass.getName();
39
40         if ( !UserType.class.isAssignableFrom(userTypeClass) ) {
41             throw new MappingException(
42                     "Custom type does not implement UserType: " +
43                     userTypeClass.getName()
44                 );
45         }
46         
47         try {
48             userType = (UserType) userTypeClass.newInstance();
49         }
50         catch (InstantiationException JavaDoc ie) {
51             throw new MappingException(
52                     "Cannot instantiate custom type: " +
53                     userTypeClass.getName()
54                 );
55         }
56         catch (IllegalAccessException JavaDoc iae) {
57             throw new MappingException(
58                     "IllegalAccessException trying to instantiate custom type: " +
59                     userTypeClass.getName()
60                 );
61         }
62
63         /*if ( !Serializable.class.isAssignableFrom( userType.returnedClass() ) ) {
64             LogFactory.getLog(CustomType.class).warn("custom type does not implement Serializable: " + userTypeClass);
65         }*/

66         TypeFactory.injectParameters(userType, parameters);
67         types = userType.sqlTypes();
68
69     }
70
71     public int[] sqlTypes(Mapping pi) {
72         return types;
73     }
74
75     public int getColumnSpan(Mapping session) {
76         return types.length;
77     }
78
79     public Class JavaDoc getReturnedClass() {
80         return userType.returnedClass();
81     }
82
83     public boolean isEqual(Object JavaDoc x, Object JavaDoc y) throws HibernateException {
84         return userType.equals(x, y);
85     }
86
87     public boolean isEqual(Object JavaDoc x, Object JavaDoc y, EntityMode entityMode)
88     throws HibernateException {
89         return isEqual(x, y);
90     }
91
92     public int getHashCode(Object JavaDoc x, EntityMode entityMode) {
93         return userType.hashCode(x);
94     }
95     
96     public Object JavaDoc nullSafeGet(
97         ResultSet JavaDoc rs,
98         String JavaDoc[] names,
99         SessionImplementor session,
100         Object JavaDoc owner
101     ) throws HibernateException, SQLException JavaDoc {
102
103         return userType.nullSafeGet(rs, names, owner);
104     }
105
106     public Object JavaDoc nullSafeGet(
107         ResultSet JavaDoc rs,
108         String JavaDoc columnName,
109         SessionImplementor session,
110         Object JavaDoc owner
111     ) throws HibernateException, SQLException JavaDoc {
112         return nullSafeGet(rs, new String JavaDoc[] { columnName }, session, owner);
113     }
114
115
116     public Object JavaDoc assemble(Serializable JavaDoc cached, SessionImplementor session, Object JavaDoc owner)
117     throws HibernateException {
118         return userType.assemble(cached, owner);
119     }
120
121     public Serializable JavaDoc disassemble(Object JavaDoc value, SessionImplementor session, Object JavaDoc owner)
122     throws HibernateException {
123         return userType.disassemble(value);
124     }
125
126     public Object JavaDoc replace(
127             Object JavaDoc original,
128             Object JavaDoc target,
129             SessionImplementor session,
130             Object JavaDoc owner,
131             Map JavaDoc copyCache)
132     throws HibernateException {
133         return userType.replace(original, target, owner);
134     }
135     
136     public void nullSafeSet(
137         PreparedStatement JavaDoc st,
138         Object JavaDoc value,
139         int index,
140         boolean[] settable,
141         SessionImplementor session
142     ) throws HibernateException, SQLException JavaDoc {
143
144         if ( settable[0] ) userType.nullSafeSet(st, value, index);
145     }
146
147     public void nullSafeSet(
148         PreparedStatement JavaDoc st,
149         Object JavaDoc value,
150         int index,
151         SessionImplementor session
152     ) throws HibernateException, SQLException JavaDoc {
153
154         userType.nullSafeSet(st, value, index);
155     }
156
157     public String JavaDoc toXMLString(Object JavaDoc value, SessionFactoryImplementor factory) {
158         if (value==null) return null;
159         if (userType instanceof EnhancedUserType) {
160             return ( (EnhancedUserType) userType ).toXMLString(value);
161         }
162         else {
163             return value.toString();
164         }
165     }
166
167     public Object JavaDoc fromXMLString(String JavaDoc xml, Mapping factory) {
168         return ( (EnhancedUserType) userType ).fromXMLString(xml);
169     }
170
171     public String JavaDoc getName() {
172         return name;
173     }
174
175     public Object JavaDoc deepCopy(Object JavaDoc value, EntityMode entityMode, SessionFactoryImplementor factory)
176     throws HibernateException {
177         return userType.deepCopy(value);
178     }
179
180     public boolean isMutable() {
181         return userType.isMutable();
182     }
183
184     public Object JavaDoc stringToObject(String JavaDoc xml) {
185         return ( (EnhancedUserType) userType ).fromXMLString(xml);
186     }
187
188     public String JavaDoc objectToSQLString(Object JavaDoc value) throws Exception JavaDoc {
189         return ( (EnhancedUserType) userType ).objectToSQLString(value);
190     }
191
192     public Comparator JavaDoc getComparator() {
193         return (Comparator JavaDoc) userType;
194     }
195
196     public Object JavaDoc next(Object JavaDoc current) {
197         return ( (UserVersionType) userType ).next(current);
198     }
199
200     public Object JavaDoc seed() {
201         return ( (UserVersionType) userType ).seed();
202     }
203
204     public Object JavaDoc fromXMLNode(Node xml, Mapping factory) throws HibernateException {
205         return fromXMLString( xml.getText(), factory );
206     }
207
208     public void setToXMLNode(Node node, Object JavaDoc value, SessionFactoryImplementor factory)
209     throws HibernateException {
210         node.setText( toXMLString(value, factory) );
211     }
212
213     public String JavaDoc toLoggableString(Object JavaDoc value, SessionFactoryImplementor factory)
214     throws HibernateException {
215         return value==null ? "null" : toXMLString(value, factory);
216     }
217
218     public boolean[] toColumnNullness(Object JavaDoc value, Mapping mapping) {
219         boolean[] result = new boolean[ getColumnSpan(mapping) ];
220         if (value!=null) Arrays.fill(result, true);
221         return result;
222     }
223     
224 }
225
Popular Tags