001: //$Id: CustomType.java 10084 2006-07-05 15:03:52Z steve.ebersole@jboss.com $
002: package org.hibernate.type;
003:
004: import java.io.Serializable;
005: import java.sql.PreparedStatement;
006: import java.sql.ResultSet;
007: import java.sql.SQLException;
008: import java.util.Arrays;
009: import java.util.Comparator;
010: import java.util.Map;
011: import java.util.Properties;
012:
013: import org.dom4j.Node;
014: import org.hibernate.EntityMode;
015: import org.hibernate.HibernateException;
016: import org.hibernate.MappingException;
017: import org.hibernate.dialect.Dialect;
018: import org.hibernate.engine.Mapping;
019: import org.hibernate.engine.SessionFactoryImplementor;
020: import org.hibernate.engine.SessionImplementor;
021: import org.hibernate.usertype.EnhancedUserType;
022: import org.hibernate.usertype.UserType;
023: import org.hibernate.usertype.UserVersionType;
024: import org.hibernate.usertype.LoggableUserType;
025:
026: /**
027: * Adapts {@link UserType} to the generic {@link Type} interface, in order
028: * to isolate user code from changes in the internal Type contracts.
029: *
030: * @see org.hibernate.usertype.UserType
031: * @author Gavin King
032: */
033: public class CustomType extends AbstractType implements IdentifierType,
034: DiscriminatorType, VersionType {
035:
036: private final UserType userType;
037: private final String name;
038: private final int[] types;
039: private final boolean customLogging;
040:
041: public CustomType(Class userTypeClass, Properties parameters)
042: throws MappingException {
043:
044: if (!UserType.class.isAssignableFrom(userTypeClass)) {
045: throw new MappingException(
046: "Custom type does not implement UserType: "
047: + userTypeClass.getName());
048: }
049:
050: name = userTypeClass.getName();
051:
052: try {
053: userType = (UserType) userTypeClass.newInstance();
054: } catch (InstantiationException ie) {
055: throw new MappingException(
056: "Cannot instantiate custom type: "
057: + userTypeClass.getName());
058: } catch (IllegalAccessException iae) {
059: throw new MappingException(
060: "IllegalAccessException trying to instantiate custom type: "
061: + userTypeClass.getName());
062: }
063:
064: TypeFactory.injectParameters(userType, parameters);
065: types = userType.sqlTypes();
066:
067: customLogging = LoggableUserType.class
068: .isAssignableFrom(userTypeClass);
069: }
070:
071: public int[] sqlTypes(Mapping pi) {
072: return types;
073: }
074:
075: public int getColumnSpan(Mapping session) {
076: return types.length;
077: }
078:
079: public Class getReturnedClass() {
080: return userType.returnedClass();
081: }
082:
083: public boolean isEqual(Object x, Object y)
084: throws HibernateException {
085: return userType.equals(x, y);
086: }
087:
088: public boolean isEqual(Object x, Object y, EntityMode entityMode)
089: throws HibernateException {
090: return isEqual(x, y);
091: }
092:
093: public int getHashCode(Object x, EntityMode entityMode) {
094: return userType.hashCode(x);
095: }
096:
097: public Object nullSafeGet(ResultSet rs, String[] names,
098: SessionImplementor session, Object owner)
099: throws HibernateException, SQLException {
100:
101: return userType.nullSafeGet(rs, names, owner);
102: }
103:
104: public Object nullSafeGet(ResultSet rs, String columnName,
105: SessionImplementor session, Object owner)
106: throws HibernateException, SQLException {
107: return nullSafeGet(rs, new String[] { columnName }, session,
108: owner);
109: }
110:
111: public Object assemble(Serializable cached,
112: SessionImplementor session, Object owner)
113: throws HibernateException {
114: return userType.assemble(cached, owner);
115: }
116:
117: public Serializable disassemble(Object value,
118: SessionImplementor session, Object owner)
119: throws HibernateException {
120: return userType.disassemble(value);
121: }
122:
123: public Object replace(Object original, Object target,
124: SessionImplementor session, Object owner, Map copyCache)
125: throws HibernateException {
126: return userType.replace(original, target, owner);
127: }
128:
129: public void nullSafeSet(PreparedStatement st, Object value,
130: int index, boolean[] settable, SessionImplementor session)
131: throws HibernateException, SQLException {
132:
133: if (settable[0])
134: userType.nullSafeSet(st, value, index);
135: }
136:
137: public void nullSafeSet(PreparedStatement st, Object value,
138: int index, SessionImplementor session)
139: throws HibernateException, SQLException {
140:
141: userType.nullSafeSet(st, value, index);
142: }
143:
144: public String toXMLString(Object value,
145: SessionFactoryImplementor factory) {
146: if (value == null)
147: return null;
148: if (userType instanceof EnhancedUserType) {
149: return ((EnhancedUserType) userType).toXMLString(value);
150: } else {
151: return value.toString();
152: }
153: }
154:
155: public Object fromXMLString(String xml, Mapping factory) {
156: return ((EnhancedUserType) userType).fromXMLString(xml);
157: }
158:
159: public String getName() {
160: return name;
161: }
162:
163: public Object deepCopy(Object value, EntityMode entityMode,
164: SessionFactoryImplementor factory)
165: throws HibernateException {
166: return userType.deepCopy(value);
167: }
168:
169: public boolean isMutable() {
170: return userType.isMutable();
171: }
172:
173: public Object stringToObject(String xml) {
174: return ((EnhancedUserType) userType).fromXMLString(xml);
175: }
176:
177: public String objectToSQLString(Object value, Dialect dialect)
178: throws Exception {
179: return ((EnhancedUserType) userType).objectToSQLString(value);
180: }
181:
182: public Comparator getComparator() {
183: return (Comparator) userType;
184: }
185:
186: public Object next(Object current, SessionImplementor session) {
187: return ((UserVersionType) userType).next(current, session);
188: }
189:
190: public Object seed(SessionImplementor session) {
191: return ((UserVersionType) userType).seed(session);
192: }
193:
194: public Object fromXMLNode(Node xml, Mapping factory)
195: throws HibernateException {
196: return fromXMLString(xml.getText(), factory);
197: }
198:
199: public void setToXMLNode(Node node, Object value,
200: SessionFactoryImplementor factory)
201: throws HibernateException {
202: node.setText(toXMLString(value, factory));
203: }
204:
205: public String toLoggableString(Object value,
206: SessionFactoryImplementor factory)
207: throws HibernateException {
208: if (value == null) {
209: return "null";
210: } else if (customLogging) {
211: return ((LoggableUserType) userType).toLoggableString(
212: value, factory);
213: } else {
214: return toXMLString(value, factory);
215: }
216: }
217:
218: public boolean[] toColumnNullness(Object value, Mapping mapping) {
219: boolean[] result = new boolean[getColumnSpan(mapping)];
220: if (value != null)
221: Arrays.fill(result, true);
222: return result;
223: }
224:
225: public boolean isDirty(Object old, Object current,
226: boolean[] checkable, SessionImplementor session)
227: throws HibernateException {
228: return checkable[0] && isDirty(old, current, session);
229: }
230:
231: }
|