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/HibernateStructuredArtifact.java $
003: * $Id: HibernateStructuredArtifact.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.ByteArrayInputStream;
023: import java.io.ByteArrayOutputStream;
024: import java.io.IOException;
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.Element;
037: import org.jdom.JDOMException;
038: import org.jdom.input.SAXBuilder;
039: import org.jdom.output.XMLOutputter;
040: import org.sakaiproject.component.cover.ComponentManager;
041: import org.sakaiproject.metaobj.shared.mgt.HomeFactory;
042: import org.sakaiproject.metaobj.shared.mgt.WritableObjectHome;
043: import org.sakaiproject.metaobj.shared.model.StructuredArtifact;
044:
045: public class HibernateStructuredArtifact extends StructuredArtifact
046: implements UserType {
047: protected final Log logger = LogFactory.getLog(getClass());
048: private final static int[] SQL_TYPES = new int[] { Types.BLOB };
049:
050: public HibernateStructuredArtifact() {
051: }
052:
053: public int[] sqlTypes() {
054: return SQL_TYPES;
055: }
056:
057: /* (non-Javadoc)
058: * @see org.hibernate.UserType#deepCopy(java.lang.Object)
059: */
060: public Object deepCopy(Object arg0) throws HibernateException {
061: return arg0;
062: }
063:
064: /* (non-Javadoc)
065: * @see org.hibernate.UserType#equals(java.lang.Object, java.lang.Object)
066: */
067: public boolean equals(Object x, Object y) throws HibernateException {
068: return (x == y) || (x != null && y != null && x.equals(y));
069: }
070:
071: /* (non-Javadoc)
072: * @see org.hibernate.UserType#isMutable()
073: */
074: public boolean isMutable() {
075: return false;
076: }
077:
078: /* (non-Javadoc)
079: * @see org.hibernate.UserType#nullSafeGet(java.sql.ResultSet, java.lang.String[], java.lang.Object)
080: */
081: public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
082: throws HibernateException, SQLException {
083: byte[] bytes = rs.getBytes(names[0]);
084: if (rs.wasNull()) {
085: return null;
086: }
087:
088: //TODO figure out how to inject this
089: HomeFactory homeFactory = (HomeFactory) ComponentManager
090: .getInstance().get("xmlHomeFactory");
091: WritableObjectHome home = (WritableObjectHome) homeFactory
092: .getHome("agent");
093:
094: StructuredArtifact artifact = (StructuredArtifact) home
095: .createInstance();
096: ByteArrayInputStream in = new ByteArrayInputStream(bytes);
097: SAXBuilder saxBuilder = new SAXBuilder();
098: try {
099: Document doc = saxBuilder.build(in);
100: artifact.setBaseElement(doc.getRootElement());
101: } catch (JDOMException e) {
102: throw new HibernateException(e);
103: } catch (IOException e) {
104: throw new HibernateException(e);
105: }
106: return artifact;
107: }
108:
109: /* (non-Javadoc)
110: * @see org.hibernate.UserType#nullSafeSet(java.sql.PreparedStatement, java.lang.Object, int)
111: */
112: public void nullSafeSet(PreparedStatement st, Object value,
113: int index) throws HibernateException, SQLException {
114: if (value == null) {
115: st.setNull(index, Types.VARBINARY);
116: } else {
117: StructuredArtifact artifact = (StructuredArtifact) value;
118: Document doc = new Document();
119: Element rootElement = artifact.getBaseElement();
120: rootElement.detach();
121: doc.setRootElement(rootElement);
122: ByteArrayOutputStream out = new ByteArrayOutputStream();
123: XMLOutputter xmlOutputter = new XMLOutputter();
124: try {
125: xmlOutputter.output(doc, out);
126: } catch (IOException e) {
127: throw new HibernateException(e);
128: }
129: st.setBytes(index, out.toByteArray());
130: }
131:
132: }
133:
134: /* (non-Javadoc)
135: * @see org.hibernate.UserType#returnedClass()
136: */
137: public Class returnedClass() {
138: return StructuredArtifact.class;
139: }
140:
141: public Object assemble(Serializable arg0, Object arg1)
142: throws HibernateException {
143: return null;
144: }
145:
146: public Serializable disassemble(Object arg0)
147: throws HibernateException {
148: return null;
149: }
150:
151: public int hashCode(Object arg0) throws HibernateException {
152: return arg0.hashCode();
153: }
154:
155: public Object replace(Object arg0, Object arg1, Object arg2)
156: throws HibernateException {
157: return arg0;
158: }
159:
160: }
|