add serId
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / hibernate / WSDLDefinitionUserType.java
1 /**
2 * Copyright (C) 2009 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9
10 package eu.etaxonomy.cdm.hibernate;
11
12 import java.io.Reader;
13 import java.io.Serializable;
14 import java.io.StringReader;
15 import java.io.StringWriter;
16 import java.sql.Clob;
17 import java.sql.PreparedStatement;
18 import java.sql.ResultSet;
19 import java.sql.SQLException;
20 import java.sql.Types;
21
22 import javax.wsdl.Definition;
23 import javax.wsdl.WSDLException;
24 import javax.wsdl.factory.WSDLFactory;
25 import javax.wsdl.xml.WSDLLocator;
26 import javax.wsdl.xml.WSDLReader;
27 import javax.wsdl.xml.WSDLWriter;
28
29 import org.hibernate.HibernateException;
30 import org.hibernate.engine.spi.SessionImplementor;
31 import org.hibernate.type.StandardBasicTypes;
32 import org.hibernate.usertype.UserType;
33 import org.jadira.usertype.dateandtime.shared.spi.AbstractSingleColumnUserType;
34 import org.jadira.usertype.dateandtime.shared.spi.AbstractUserType;
35 import org.jadira.usertype.dateandtime.shared.spi.ColumnMapper;
36
37 import com.ibm.wsdl.factory.WSDLFactoryImpl;
38
39 import eu.etaxonomy.cdm.model.common.LSIDWSDLLocator;
40
41 /**
42 * UserType which allows persistence of a wsdl definition - used to persist the
43 * wsdl definition of an LSIDAuthority
44 *
45 * @author ben
46 *
47 * @see org.cateproject.model.lsid.PersistableLSIDAuthority
48 * @see org.hibernate.usertype.UserType UserType
49 * @see javax.wsdl.Definition Definition
50 */
51 public class WSDLDefinitionUserType extends AbstractUserType implements UserType {
52 private static final long serialVersionUID = 186785968465961559L;
53 private static final int[] SQL_TYPES = { Types.CLOB };
54
55 public Object deepCopy(Object o) throws HibernateException {
56
57 if (o == null) {
58 return null;
59 }
60
61 Definition d = (Definition) o;
62
63 try {
64 WSDLFactory wsdlFactory = WSDLFactoryImpl.newInstance();
65 StringWriter stringWriter = new StringWriter();
66 WSDLWriter writer = wsdlFactory.newWSDLWriter();
67 writer.writeWSDL(d, stringWriter);
68 WSDLReader wsdlReader = wsdlFactory.newWSDLReader();
69 Reader reader = new StringReader(stringWriter.getBuffer().toString());
70 WSDLLocator locator = new LSIDWSDLLocator("wsdl",reader,Thread.currentThread().getContextClassLoader());
71 Definition definition = wsdlReader.readWSDL(locator);
72 return definition;
73 } catch (Exception e) {
74 throw new HibernateException(e);
75 }
76 }
77
78
79 //not tested if this works with jadira.usertype
80 @Override
81 public Object assemble(Serializable cached, Object owner) throws HibernateException {
82 try {
83 WSDLFactory wsdlFactory = WSDLFactoryImpl.newInstance();
84 WSDLReader wsdlReader = wsdlFactory.newWSDLReader();
85 Reader reader = new StringReader(cached.toString());
86 WSDLLocator locator = new LSIDWSDLLocator("wsdl",reader,Thread.currentThread().getContextClassLoader());
87 Definition definition = wsdlReader.readWSDL(locator);
88 return definition;
89 } catch (Exception e) {
90 throw new HibernateException(e);
91 }
92 }
93
94 //not tested if this works with jadira.usertype
95 @Override
96 public Serializable disassemble(Object value) throws HibernateException {
97 try {
98 WSDLFactory wsdlFactory = WSDLFactoryImpl.newInstance();
99 Definition definition = (Definition) value;
100 StringWriter stringWriter = new StringWriter();
101 WSDLWriter writer = wsdlFactory.newWSDLWriter();
102 writer.writeWSDL(definition, stringWriter);
103 return stringWriter.getBuffer().toString();
104 } catch (WSDLException e) {
105 throw new HibernateException(e);
106 }
107 }
108
109
110
111 @Override
112 public Definition nullSafeGet(ResultSet rs, String[]names, SessionImplementor session, Object o)
113 throws HibernateException, SQLException {
114 Clob val = (Clob)StandardBasicTypes.CLOB.nullSafeGet(rs, names, session, o);
115 // Clob val = (Clob) rs.getClob(names[0]);
116 if(val == null) {
117 return null;
118 } else {
119 try {
120 WSDLFactory wsdlFactory = WSDLFactoryImpl.newInstance();
121 WSDLReader wsdlReader = wsdlFactory.newWSDLReader();
122 Reader reader = val.getCharacterStream();
123 WSDLLocator locator = new LSIDWSDLLocator("wsdl",reader,Thread.currentThread().getContextClassLoader());
124 Definition definition = wsdlReader.readWSDL(locator);
125 return definition;
126 } catch (Exception e) {
127 throw new HibernateException(e);
128 }
129 }
130 }
131
132
133
134 @Override
135 public void nullSafeSet(PreparedStatement statement, Object value, int index, SessionImplementor session)
136 throws HibernateException, SQLException {
137 if (value == null) {
138 // statement.setNull(index, Types.CLOB); //old version
139 StandardBasicTypes.CLOB.nullSafeSet(statement, value, index, session);
140 } else {
141 try {
142 Definition definition = (Definition) value;
143 WSDLFactory wsdlFactory = WSDLFactoryImpl.newInstance();
144 StringWriter stringWriter = new StringWriter();
145 WSDLWriter writer = wsdlFactory.newWSDLWriter();
146 writer.writeWSDL(definition, stringWriter);
147 // statement.setClob(index, Hibernate.createClob(stringWriter.getBuffer().toString())); //old version
148 StandardBasicTypes.CLOB.nullSafeSet(statement, stringWriter.getBuffer().toString(), index, session);
149 } catch (WSDLException e) {
150 throw new HibernateException(e);
151 }
152
153 }
154 }
155
156
157 public Class returnedClass() {
158 return Definition.class;
159 }
160
161
162 @Override
163 public int[] sqlTypes() {
164 return SQL_TYPES;
165 }
166
167 }