KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: CompositeCustomType.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.lang.reflect.Method JavaDoc;
6 import java.sql.PreparedStatement JavaDoc;
7 import java.sql.ResultSet JavaDoc;
8 import java.sql.SQLException JavaDoc;
9 import java.util.Map JavaDoc;
10 import java.util.Properties JavaDoc;
11
12 import org.dom4j.Element;
13 import org.dom4j.Node;
14 import org.hibernate.EntityMode;
15 import org.hibernate.FetchMode;
16 import org.hibernate.HibernateException;
17 import org.hibernate.MappingException;
18 import org.hibernate.engine.CascadeStyle;
19 import org.hibernate.engine.Mapping;
20 import org.hibernate.engine.SessionFactoryImplementor;
21 import org.hibernate.engine.SessionImplementor;
22 import org.hibernate.usertype.CompositeUserType;
23
24 /**
25  * Adapts <tt>CompositeUserType</tt> to <tt>Type</tt> interface
26  * @author Gavin King
27  */

28 public class CompositeCustomType extends AbstractType
29     implements AbstractComponentType {
30
31     private final CompositeUserType userType;
32     private final String JavaDoc name;
33
34     public CompositeCustomType(Class JavaDoc userTypeClass, Properties JavaDoc parameters)
35     throws MappingException {
36         name = userTypeClass.getName();
37
38         if ( !CompositeUserType.class.isAssignableFrom(userTypeClass) ) {
39             throw new MappingException(
40                     "Custom type does not implement CompositeUserType: " +
41                     userTypeClass.getName()
42                 );
43         }
44         
45         try {
46             userType = (CompositeUserType) userTypeClass.newInstance();
47         }
48         catch (InstantiationException JavaDoc ie) {
49             throw new MappingException(
50                     "Cannot instantiate custom type: " +
51                     userTypeClass.getName()
52                 );
53         }
54         catch (IllegalAccessException JavaDoc iae) {
55             throw new MappingException(
56                     "IllegalAccessException trying to instantiate custom type: " +
57                     userTypeClass.getName()
58                 );
59         }
60         TypeFactory.injectParameters(userType, parameters);
61     }
62     
63     public boolean isMethodOf(Method JavaDoc method) {
64         return false;
65     }
66
67     public Type[] getSubtypes() {
68         return userType.getPropertyTypes();
69     }
70
71     public String JavaDoc[] getPropertyNames() {
72         return userType.getPropertyNames();
73     }
74
75     public Object JavaDoc[] getPropertyValues(Object JavaDoc component, SessionImplementor session)
76         throws HibernateException {
77         return getPropertyValues( component, session.getEntityMode() );
78     }
79
80     public Object JavaDoc[] getPropertyValues(Object JavaDoc component, EntityMode entityMode)
81         throws HibernateException {
82
83         int len = getSubtypes().length;
84         Object JavaDoc[] result = new Object JavaDoc[len];
85         for ( int i=0; i<len; i++ ) {
86             result[i] = getPropertyValue(component, i);
87         }
88         return result;
89     }
90
91     public void setPropertyValues(Object JavaDoc component, Object JavaDoc[] values, EntityMode entityMode)
92         throws HibernateException {
93
94         for (int i=0; i<values.length; i++) {
95             userType.setPropertyValue( component, i, values[i] );
96         }
97     }
98
99     public Object JavaDoc getPropertyValue(Object JavaDoc component, int i, SessionImplementor session)
100         throws HibernateException {
101         return getPropertyValue(component, i);
102     }
103
104     public Object JavaDoc getPropertyValue(Object JavaDoc component, int i)
105         throws HibernateException {
106         return userType.getPropertyValue(component, i);
107     }
108
109     public CascadeStyle getCascadeStyle(int i) {
110         return CascadeStyle.NONE;
111     }
112
113     public FetchMode getFetchMode(int i) {
114         return FetchMode.DEFAULT;
115     }
116
117     public boolean isComponentType() {
118         return true;
119     }
120
121     public Object JavaDoc deepCopy(Object JavaDoc value, EntityMode entityMode, SessionFactoryImplementor factory)
122     throws HibernateException {
123         return userType.deepCopy(value);
124     }
125
126     public Object JavaDoc assemble(
127         Serializable JavaDoc cached,
128         SessionImplementor session,
129         Object JavaDoc owner)
130         throws HibernateException {
131
132         return userType.assemble(cached, session, owner);
133     }
134
135     public Serializable JavaDoc disassemble(Object JavaDoc value, SessionImplementor session, Object JavaDoc owner)
136     throws HibernateException {
137         return userType.disassemble(value, session);
138     }
139     
140     public Object JavaDoc replace(
141             Object JavaDoc original,
142             Object JavaDoc target,
143             SessionImplementor session,
144             Object JavaDoc owner,
145             Map JavaDoc copyCache)
146     throws HibernateException {
147         return userType.replace(original, target, session, owner);
148     }
149     
150     public boolean isEqual(Object JavaDoc x, Object JavaDoc y, EntityMode entityMode)
151     throws HibernateException {
152         return userType.equals(x, y);
153     }
154
155     public int getHashCode(Object JavaDoc x, EntityMode entityMode) {
156         return userType.hashCode(x);
157     }
158     
159     public int getColumnSpan(Mapping mapping) throws MappingException {
160         Type[] types = userType.getPropertyTypes();
161         int n=0;
162         for (int i=0; i<types.length; i++) {
163             n+=types[i].getColumnSpan(mapping);
164         }
165         return n;
166     }
167
168     public String JavaDoc getName() {
169         return name;
170     }
171
172     public Class JavaDoc getReturnedClass() {
173         return userType.returnedClass();
174     }
175
176     public boolean isMutable() {
177         return userType.isMutable();
178     }
179
180     public Object JavaDoc nullSafeGet(
181         ResultSet JavaDoc rs,
182         String JavaDoc columnName,
183         SessionImplementor session,
184         Object JavaDoc owner)
185         throws HibernateException, SQLException JavaDoc {
186
187         return userType.nullSafeGet(rs, new String JavaDoc[] {columnName}, session, owner);
188     }
189
190     public Object JavaDoc nullSafeGet(
191         ResultSet JavaDoc rs,
192         String JavaDoc[] names,
193         SessionImplementor session,
194         Object JavaDoc owner)
195         throws HibernateException, SQLException JavaDoc {
196
197         return userType.nullSafeGet(rs, names, session, owner);
198     }
199
200     public void nullSafeSet(
201         PreparedStatement JavaDoc st,
202         Object JavaDoc value,
203         int index,
204         SessionImplementor session)
205         throws HibernateException, SQLException JavaDoc {
206
207         userType.nullSafeSet(st, value, index, session);
208
209     }
210
211     public void nullSafeSet(
212         PreparedStatement JavaDoc st,
213         Object JavaDoc value,
214         int index,
215         boolean[] settable,
216         SessionImplementor session)
217         throws HibernateException, SQLException JavaDoc {
218
219         userType.nullSafeSet(st, value, index, session);
220
221     }
222
223     public int[] sqlTypes(Mapping mapping) throws MappingException {
224         Type[] types = userType.getPropertyTypes();
225         int[] result = new int[ getColumnSpan(mapping) ];
226         int n=0;
227         for (int i=0; i<types.length; i++) {
228             int[] sqlTypes = types[i].sqlTypes(mapping);
229             for ( int k=0; k<sqlTypes.length; k++ ) result[n++] = sqlTypes[k];
230         }
231         return result;
232     }
233     
234     public String JavaDoc toLoggableString(Object JavaDoc value, SessionFactoryImplementor factory)
235         throws HibernateException {
236
237         return value==null ? "null" : value.toString();
238     }
239
240     public boolean[] getPropertyNullability() {
241         return null;
242     }
243
244     public Object JavaDoc fromXMLNode(Node xml, Mapping factory) throws HibernateException {
245         return xml;
246     }
247
248     public void setToXMLNode(Node node, Object JavaDoc value, SessionFactoryImplementor factory)
249     throws HibernateException {
250         replaceNode( node, (Element) value );
251     }
252
253     public boolean[] toColumnNullness(Object JavaDoc value, Mapping mapping) {
254         boolean[] result = new boolean[ getColumnSpan(mapping) ];
255         if (value==null) return result;
256         Object JavaDoc[] values = getPropertyValues(value, EntityMode.POJO); //TODO!!!!!!!
257
int loc = 0;
258         Type[] propertyTypes = getSubtypes();
259         for ( int i=0; i<propertyTypes.length; i++ ) {
260             boolean[] propertyNullness = propertyTypes[i].toColumnNullness( values[i], mapping );
261             System.arraycopy(propertyNullness, 0, result, loc, propertyNullness.length);
262             loc += propertyNullness.length;
263         }
264         return result;
265     }
266 }
267
Popular Tags