001: /**********************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/metaobj/tags/sakai_2-4-1/metaobj-impl/api-impl/src/java/org/sakaiproject/metaobj/shared/model/impl/HibernateSchemaNode.java $
003: * $Id: HibernateSchemaNode.java 14225 2006-09-05 17:39:44Z chmaurer@iupui.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 2004, 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: **********************************************************************************/package org.sakaiproject.metaobj.shared.model.impl;
021:
022: import java.io.ByteArrayOutputStream;
023: import java.io.IOException;
024: import java.io.InputStream;
025: import java.io.Serializable;
026: import java.sql.PreparedStatement;
027: import java.sql.ResultSet;
028: import java.sql.SQLException;
029: import java.sql.Types;
030:
031: import org.apache.commons.logging.Log;
032: import org.apache.commons.logging.LogFactory;
033: import org.hibernate.HibernateException;
034: import org.hibernate.usertype.UserType;
035: import org.jdom.Document;
036: import org.jdom.output.XMLOutputter;
037: import org.sakaiproject.metaobj.utils.xml.SchemaFactory;
038: import org.sakaiproject.metaobj.utils.xml.SchemaNode;
039: import org.sakaiproject.metaobj.utils.xml.impl.SchemaNodeImpl;
040:
041: public class HibernateSchemaNode implements UserType {
042:
043: protected final Log logger = LogFactory.getLog(getClass());
044: private final static int[] SQL_TYPES = new int[] { Types.BLOB };
045:
046: public int[] sqlTypes() {
047: return SQL_TYPES;
048: }
049:
050: /* (non-Javadoc)
051: * @see org.hibernate.UserType#deepCopy(java.lang.Object)
052: */
053: public Object deepCopy(Object arg0) throws HibernateException {
054: return arg0;
055: }
056:
057: /* (non-Javadoc)
058: * @see org.hibernate.UserType#equals(java.lang.Object, java.lang.Object)
059: */
060: public boolean equals(Object x, Object y) throws HibernateException {
061: return (x == y) || (x != null && y != null && x.equals(y));
062: }
063:
064: /* (non-Javadoc)
065: * @see org.hibernate.UserType#isMutable()
066: */
067: public boolean isMutable() {
068: return false;
069: }
070:
071: /* (non-Javadoc)
072: * @see org.hibernate.UserType#nullSafeGet(java.sql.ResultSet, java.lang.String[], java.lang.Object)
073: */
074: public Object nullSafeGet(ResultSet rs, String[] names,
075: Object object) throws HibernateException, SQLException {
076: InputStream in = rs.getBinaryStream(names[0]);
077: if (rs.wasNull()) {
078: return null;
079: }
080:
081: SchemaFactory schemaFactory = SchemaFactory.getInstance();
082: return schemaFactory.getSchema(in);
083: }
084:
085: /* (non-Javadoc)
086: * @see org.hibernate.UserType#nullSafeSet(java.sql.PreparedStatement, java.lang.Object, int)
087: */
088: public void nullSafeSet(PreparedStatement st, Object value,
089: int index) throws HibernateException, SQLException {
090: if (value == null) {
091: st.setNull(index, Types.VARBINARY);
092: } else {
093: SchemaNode schemaNode = (SchemaNode) value;
094: Document doc = schemaNode.getSchemaElement().getDocument();
095:
096: ByteArrayOutputStream out = new ByteArrayOutputStream();
097: XMLOutputter xmlOutputter = new XMLOutputter();
098: try {
099: xmlOutputter.output(doc, out);
100: } catch (IOException e) {
101: throw new HibernateException(e);
102: }
103: st.setBytes(index, out.toByteArray());
104: }
105: }
106:
107: /* (non-Javadoc)
108: * @see org.hibernate.UserType#returnedClass()
109: */
110: public Class returnedClass() {
111: return SchemaNodeImpl.class;
112: }
113:
114: public Object assemble(Serializable arg0, Object arg1)
115: throws HibernateException {
116: // TODO Auto-generated method stub
117: return null;
118: }
119:
120: public Serializable disassemble(Object arg0)
121: throws HibernateException {
122: // TODO Auto-generated method stub
123: return null;
124: }
125:
126: public int hashCode(Object o) throws HibernateException {
127: return o.hashCode();
128: }
129:
130: public Object replace(Object original, Object target, Object owner)
131: throws HibernateException {
132: return original;
133: }
134:
135: }
|