001: /**********************************************************************************
002: * $URL:https://source.sakaiproject.org/svn/osp/trunk/presentation/api/src/java/org/theospi/portfolio/presentation/model/impl/HibernatePresentationProperties.java $
003: * $Id:HibernatePresentationProperties.java 9134 2006-05-08 20:28:42Z chmaurer@iupui.edu $
004: ***********************************************************************************
005: *
006: * Copyright (c) 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.theospi.portfolio.presentation.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.metaobj.shared.model.ElementBean;
041: import org.sakaiproject.metaobj.shared.model.StructuredArtifact;
042:
043: public class HibernatePresentationProperties implements UserType {
044: protected final Log logger = LogFactory.getLog(getClass());
045: private final static int[] SQL_TYPES = new int[] { Types.BLOB };
046:
047: public HibernatePresentationProperties() {
048: }
049:
050: public int[] sqlTypes() {
051: return SQL_TYPES;
052: }
053:
054: /* (non-Javadoc)
055: * @see org.hibernate.UserType#deepCopy(java.lang.Object)
056: */
057: public Object deepCopy(Object arg0) throws HibernateException {
058: return arg0;
059: }
060:
061: /* (non-Javadoc)
062: * @see org.hibernate.UserType#equals(java.lang.Object, java.lang.Object)
063: */
064: public boolean equals(Object x, Object y) throws HibernateException {
065: return (x == y) || (x != null && y != null && x.equals(y));
066: }
067:
068: /* (non-Javadoc)
069: * @see org.hibernate.UserType#isMutable()
070: */
071: public boolean isMutable() {
072: return false;
073: }
074:
075: /* (non-Javadoc)
076: * @see org.hibernate.UserType#nullSafeGet(java.sql.ResultSet, java.lang.String[], java.lang.Object)
077: */
078: public Object nullSafeGet(ResultSet rs, String[] names, Object owner)
079: throws HibernateException, SQLException {
080: byte[] bytes = rs.getBytes(names[0]);
081: if (rs.wasNull())
082: return null;
083:
084: ElementBean elementBean = new ElementBean();
085: elementBean.setDeferValidation(true);
086: ByteArrayInputStream in = new ByteArrayInputStream(bytes);
087: SAXBuilder saxBuilder = new SAXBuilder();
088: try {
089: Document doc = saxBuilder.build(in);
090: elementBean.setBaseElement(doc.getRootElement());
091: } catch (JDOMException e) {
092: throw new HibernateException(e);
093: } catch (IOException e) {
094: throw new HibernateException(e);
095: }
096: return elementBean;
097: }
098:
099: /* (non-Javadoc)
100: * @see org.hibernate.UserType#nullSafeSet(java.sql.PreparedStatement, java.lang.Object, int)
101: */
102: public void nullSafeSet(PreparedStatement st, Object value,
103: int index) throws HibernateException, SQLException {
104: if (value == null) {
105: st.setNull(index, Types.VARBINARY);
106: } else {
107: ElementBean elementBean = (ElementBean) value;
108: Document doc = new Document();
109: Element rootElement = elementBean.getBaseElement();
110: rootElement.detach();
111: doc.setRootElement(rootElement);
112: ByteArrayOutputStream out = new ByteArrayOutputStream();
113: XMLOutputter xmlOutputter = new XMLOutputter();
114: try {
115: xmlOutputter.output(doc, out);
116: } catch (IOException e) {
117: throw new HibernateException(e);
118: }
119: st.setBytes(index, out.toByteArray());
120: }
121:
122: }
123:
124: /* (non-Javadoc)
125: * @see org.hibernate.UserType#returnedClass()
126: */
127: public Class returnedClass() {
128: return StructuredArtifact.class;
129: }
130:
131: public int hashCode(Object o) throws HibernateException {
132: // TODO Auto-generated method stub
133: return o.hashCode();
134: }
135:
136: public Serializable disassemble(Object arg0)
137: throws HibernateException {
138: // TODO Auto-generated method stub
139: return null;
140: }
141:
142: public Object assemble(Serializable arg0, Object arg1)
143: throws HibernateException {
144: // TODO Auto-generated method stub
145: return null;
146: }
147:
148: public Object replace(Object original, Object target, Object owner)
149: throws HibernateException {
150: // TODO Auto-generated method stub
151: return original;
152: }
153:
154: }
|