KickJava   Java API By Example, From Geeks To Geeks.

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


1 //$Id: MetaType.java,v 1.11 2005/07/19 18:17:14 oneovthafew Exp $
2
package org.hibernate.type;
3
4 import java.sql.PreparedStatement JavaDoc;
5 import java.sql.ResultSet JavaDoc;
6 import java.sql.SQLException JavaDoc;
7 import java.util.HashMap JavaDoc;
8 import java.util.Iterator JavaDoc;
9 import java.util.Map JavaDoc;
10
11 import org.dom4j.Node;
12 import org.hibernate.EntityMode;
13 import org.hibernate.HibernateException;
14 import org.hibernate.MappingException;
15 import org.hibernate.engine.Mapping;
16 import org.hibernate.engine.SessionFactoryImplementor;
17 import org.hibernate.engine.SessionImplementor;
18
19 /**
20  * @author Gavin King
21  */

22 public class MetaType extends AbstractType {
23
24     private final Map JavaDoc values;
25     private final Map JavaDoc keys;
26     private final Type baseType;
27
28     public MetaType(Map JavaDoc values, Type baseType) {
29         this.baseType = baseType;
30         this.values = values;
31         keys = new HashMap JavaDoc();
32         Iterator JavaDoc iter = values.entrySet().iterator();
33         while ( iter.hasNext() ) {
34             Map.Entry JavaDoc me = (Map.Entry JavaDoc) iter.next();
35             keys.put( me.getValue(), me.getKey() );
36         }
37     }
38
39     public int[] sqlTypes(Mapping mapping) throws MappingException {
40         return baseType.sqlTypes(mapping);
41     }
42
43     public int getColumnSpan(Mapping mapping) throws MappingException {
44         return baseType.getColumnSpan(mapping);
45     }
46
47     public Class JavaDoc getReturnedClass() {
48         return String JavaDoc.class;
49     }
50
51     public Object JavaDoc nullSafeGet(
52         ResultSet JavaDoc rs,
53         String JavaDoc[] names,
54         SessionImplementor session,
55         Object JavaDoc owner)
56     throws HibernateException, SQLException JavaDoc {
57         Object JavaDoc key = baseType.nullSafeGet(rs, names, session, owner);
58         return key==null ? null : values.get(key);
59     }
60
61     public Object JavaDoc nullSafeGet(
62         ResultSet JavaDoc rs,
63         String JavaDoc name,
64         SessionImplementor session,
65         Object JavaDoc owner)
66     throws HibernateException, SQLException JavaDoc {
67         Object JavaDoc key = baseType.nullSafeGet(rs, name, session, owner);
68         return key==null ? null : values.get(key);
69     }
70
71     public void nullSafeSet(PreparedStatement JavaDoc st, Object JavaDoc value, int index, SessionImplementor session)
72     throws HibernateException, SQLException JavaDoc {
73         baseType.nullSafeSet(st, value==null ? null : keys.get(value), index, session);
74     }
75     
76     public void nullSafeSet(
77             PreparedStatement JavaDoc st,
78             Object JavaDoc value,
79             int index,
80             boolean[] settable,
81             SessionImplementor session)
82     throws HibernateException, SQLException JavaDoc {
83         if ( settable[0] ) nullSafeSet(st, value, index, session);
84     }
85
86     public String JavaDoc toLoggableString(Object JavaDoc value, SessionFactoryImplementor factory) throws HibernateException {
87         return toXMLString(value, factory);
88     }
89     
90     public String JavaDoc toXMLString(Object JavaDoc value, SessionFactoryImplementor factory)
91         throws HibernateException {
92         return (String JavaDoc) value; //value is the entity name
93
}
94
95     public Object JavaDoc fromXMLString(String JavaDoc xml, Mapping factory)
96         throws HibernateException {
97         return xml; //xml is the entity name
98
}
99
100     public String JavaDoc getName() {
101         return baseType.getName(); //TODO!
102
}
103
104     public Object JavaDoc deepCopy(Object JavaDoc value, EntityMode entityMode, SessionFactoryImplementor factory)
105     throws HibernateException {
106         return value;
107     }
108
109     public Object JavaDoc replace(
110             Object JavaDoc original,
111             Object JavaDoc target,
112             SessionImplementor session,
113             Object JavaDoc owner,
114             Map JavaDoc copyCache
115     ) {
116         return original;
117     }
118     
119     public boolean isMutable() {
120         return false;
121     }
122
123     public Object JavaDoc fromXMLNode(Node xml, Mapping factory) throws HibernateException {
124         return fromXMLString( xml.getText(), factory );
125     }
126
127     public void setToXMLNode(Node node, Object JavaDoc value, SessionFactoryImplementor factory) throws HibernateException {
128         node.setText( toXMLString(value, factory) );
129     }
130
131     public boolean[] toColumnNullness(Object JavaDoc value, Mapping mapping) {
132         throw new UnsupportedOperationException JavaDoc();
133     }
134
135 }
136
Popular Tags