001: //$Id: CompositeCustomType.java 7670 2005-07-29 05:36:14Z oneovthafew $
002: package org.hibernate.type;
003:
004: import java.io.Serializable;
005: import java.lang.reflect.Method;
006: import java.sql.PreparedStatement;
007: import java.sql.ResultSet;
008: import java.sql.SQLException;
009: import java.util.Map;
010: import java.util.Properties;
011:
012: import org.dom4j.Element;
013: import org.dom4j.Node;
014: import org.hibernate.EntityMode;
015: import org.hibernate.FetchMode;
016: import org.hibernate.HibernateException;
017: import org.hibernate.MappingException;
018: import org.hibernate.engine.CascadeStyle;
019: import org.hibernate.engine.Mapping;
020: import org.hibernate.engine.SessionFactoryImplementor;
021: import org.hibernate.engine.SessionImplementor;
022: import org.hibernate.usertype.CompositeUserType;
023:
024: /**
025: * Adapts <tt>CompositeUserType</tt> to <tt>Type</tt> interface
026: * @author Gavin King
027: */
028: public class CompositeCustomType extends AbstractType implements
029: AbstractComponentType {
030:
031: private final CompositeUserType userType;
032: private final String name;
033:
034: public CompositeCustomType(Class userTypeClass,
035: Properties parameters) throws MappingException {
036: name = userTypeClass.getName();
037:
038: if (!CompositeUserType.class.isAssignableFrom(userTypeClass)) {
039: throw new MappingException(
040: "Custom type does not implement CompositeUserType: "
041: + userTypeClass.getName());
042: }
043:
044: try {
045: userType = (CompositeUserType) userTypeClass.newInstance();
046: } catch (InstantiationException ie) {
047: throw new MappingException(
048: "Cannot instantiate custom type: "
049: + userTypeClass.getName());
050: } catch (IllegalAccessException iae) {
051: throw new MappingException(
052: "IllegalAccessException trying to instantiate custom type: "
053: + userTypeClass.getName());
054: }
055: TypeFactory.injectParameters(userType, parameters);
056: }
057:
058: public boolean isMethodOf(Method method) {
059: return false;
060: }
061:
062: public Type[] getSubtypes() {
063: return userType.getPropertyTypes();
064: }
065:
066: public String[] getPropertyNames() {
067: return userType.getPropertyNames();
068: }
069:
070: public Object[] getPropertyValues(Object component,
071: SessionImplementor session) throws HibernateException {
072: return getPropertyValues(component, session.getEntityMode());
073: }
074:
075: public Object[] getPropertyValues(Object component,
076: EntityMode entityMode) throws HibernateException {
077:
078: int len = getSubtypes().length;
079: Object[] result = new Object[len];
080: for (int i = 0; i < len; i++) {
081: result[i] = getPropertyValue(component, i);
082: }
083: return result;
084: }
085:
086: public void setPropertyValues(Object component, Object[] values,
087: EntityMode entityMode) throws HibernateException {
088:
089: for (int i = 0; i < values.length; i++) {
090: userType.setPropertyValue(component, i, values[i]);
091: }
092: }
093:
094: public Object getPropertyValue(Object component, int i,
095: SessionImplementor session) throws HibernateException {
096: return getPropertyValue(component, i);
097: }
098:
099: public Object getPropertyValue(Object component, int i)
100: throws HibernateException {
101: return userType.getPropertyValue(component, i);
102: }
103:
104: public CascadeStyle getCascadeStyle(int i) {
105: return CascadeStyle.NONE;
106: }
107:
108: public FetchMode getFetchMode(int i) {
109: return FetchMode.DEFAULT;
110: }
111:
112: public boolean isComponentType() {
113: return true;
114: }
115:
116: public Object deepCopy(Object value, EntityMode entityMode,
117: SessionFactoryImplementor factory)
118: throws HibernateException {
119: return userType.deepCopy(value);
120: }
121:
122: public Object assemble(Serializable cached,
123: SessionImplementor session, Object owner)
124: throws HibernateException {
125:
126: return userType.assemble(cached, session, owner);
127: }
128:
129: public Serializable disassemble(Object value,
130: SessionImplementor session, Object owner)
131: throws HibernateException {
132: return userType.disassemble(value, session);
133: }
134:
135: public Object replace(Object original, Object target,
136: SessionImplementor session, Object owner, Map copyCache)
137: throws HibernateException {
138: return userType.replace(original, target, session, owner);
139: }
140:
141: public boolean isEqual(Object x, Object y, EntityMode entityMode)
142: throws HibernateException {
143: return userType.equals(x, y);
144: }
145:
146: public int getHashCode(Object x, EntityMode entityMode) {
147: return userType.hashCode(x);
148: }
149:
150: public int getColumnSpan(Mapping mapping) throws MappingException {
151: Type[] types = userType.getPropertyTypes();
152: int n = 0;
153: for (int i = 0; i < types.length; i++) {
154: n += types[i].getColumnSpan(mapping);
155: }
156: return n;
157: }
158:
159: public String getName() {
160: return name;
161: }
162:
163: public Class getReturnedClass() {
164: return userType.returnedClass();
165: }
166:
167: public boolean isMutable() {
168: return userType.isMutable();
169: }
170:
171: public Object nullSafeGet(ResultSet rs, String columnName,
172: SessionImplementor session, Object owner)
173: throws HibernateException, SQLException {
174:
175: return userType.nullSafeGet(rs, new String[] { columnName },
176: session, owner);
177: }
178:
179: public Object nullSafeGet(ResultSet rs, String[] names,
180: SessionImplementor session, Object owner)
181: throws HibernateException, SQLException {
182:
183: return userType.nullSafeGet(rs, names, session, owner);
184: }
185:
186: public void nullSafeSet(PreparedStatement st, Object value,
187: int index, SessionImplementor session)
188: throws HibernateException, SQLException {
189:
190: userType.nullSafeSet(st, value, index, session);
191:
192: }
193:
194: public void nullSafeSet(PreparedStatement st, Object value,
195: int index, boolean[] settable, SessionImplementor session)
196: throws HibernateException, SQLException {
197:
198: userType.nullSafeSet(st, value, index, session);
199:
200: }
201:
202: public int[] sqlTypes(Mapping mapping) throws MappingException {
203: Type[] types = userType.getPropertyTypes();
204: int[] result = new int[getColumnSpan(mapping)];
205: int n = 0;
206: for (int i = 0; i < types.length; i++) {
207: int[] sqlTypes = types[i].sqlTypes(mapping);
208: for (int k = 0; k < sqlTypes.length; k++)
209: result[n++] = sqlTypes[k];
210: }
211: return result;
212: }
213:
214: public String toLoggableString(Object value,
215: SessionFactoryImplementor factory)
216: throws HibernateException {
217:
218: return value == null ? "null" : value.toString();
219: }
220:
221: public boolean[] getPropertyNullability() {
222: return null;
223: }
224:
225: public Object fromXMLNode(Node xml, Mapping factory)
226: throws HibernateException {
227: return xml;
228: }
229:
230: public void setToXMLNode(Node node, Object value,
231: SessionFactoryImplementor factory)
232: throws HibernateException {
233: replaceNode(node, (Element) value);
234: }
235:
236: public boolean[] toColumnNullness(Object value, Mapping mapping) {
237: boolean[] result = new boolean[getColumnSpan(mapping)];
238: if (value == null)
239: return result;
240: Object[] values = getPropertyValues(value, EntityMode.POJO); //TODO!!!!!!!
241: int loc = 0;
242: Type[] propertyTypes = getSubtypes();
243: for (int i = 0; i < propertyTypes.length; i++) {
244: boolean[] propertyNullness = propertyTypes[i]
245: .toColumnNullness(values[i], mapping);
246: System.arraycopy(propertyNullness, 0, result, loc,
247: propertyNullness.length);
248: loc += propertyNullness.length;
249: }
250: return result;
251: }
252:
253: public boolean isDirty(Object old, Object current,
254: boolean[] checkable, SessionImplementor session)
255: throws HibernateException {
256: return isDirty(old, current, session);
257: }
258:
259: public boolean isEmbedded() {
260: return false;
261: }
262: }
|