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.AbstractUserType;
|
34
|
import com.ibm.wsdl.factory.WSDLFactoryImpl;
|
35
|
|
36
|
import eu.etaxonomy.cdm.model.common.LSIDWSDLLocator;
|
37
|
|
38
|
/**
|
39
|
* UserType which allows persistence of a wsdl definition - used to persist the
|
40
|
* wsdl definition of an LSIDAuthority
|
41
|
*
|
42
|
* @author ben
|
43
|
*
|
44
|
* @see org.cateproject.model.lsid.PersistableLSIDAuthority
|
45
|
* @see org.hibernate.usertype.UserType UserType
|
46
|
* @see javax.wsdl.Definition Definition
|
47
|
*/
|
48
|
public class WSDLDefinitionUserType extends AbstractUserType implements UserType {
|
49
|
private static final long serialVersionUID = 186785968465961559L;
|
50
|
private static final int[] SQL_TYPES = { Types.CLOB };
|
51
|
|
52
|
public Object deepCopy(Object o) throws HibernateException {
|
53
|
|
54
|
if (o == null) {
|
55
|
return null;
|
56
|
}
|
57
|
|
58
|
Definition d = (Definition) o;
|
59
|
|
60
|
try {
|
61
|
WSDLFactory wsdlFactory = WSDLFactoryImpl.newInstance();
|
62
|
StringWriter stringWriter = new StringWriter();
|
63
|
WSDLWriter writer = wsdlFactory.newWSDLWriter();
|
64
|
writer.writeWSDL(d, stringWriter);
|
65
|
WSDLReader wsdlReader = wsdlFactory.newWSDLReader();
|
66
|
Reader reader = new StringReader(stringWriter.getBuffer().toString());
|
67
|
WSDLLocator locator = new LSIDWSDLLocator("wsdl",reader,Thread.currentThread().getContextClassLoader());
|
68
|
Definition definition = wsdlReader.readWSDL(locator);
|
69
|
return definition;
|
70
|
} catch (Exception e) {
|
71
|
throw new HibernateException(e);
|
72
|
}
|
73
|
}
|
74
|
|
75
|
|
76
|
//not tested if this works with jadira.usertype
|
77
|
@Override
|
78
|
public Object assemble(Serializable cached, Object owner) throws HibernateException {
|
79
|
try {
|
80
|
WSDLFactory wsdlFactory = WSDLFactoryImpl.newInstance();
|
81
|
WSDLReader wsdlReader = wsdlFactory.newWSDLReader();
|
82
|
Reader reader = new StringReader(cached.toString());
|
83
|
WSDLLocator locator = new LSIDWSDLLocator("wsdl",reader,Thread.currentThread().getContextClassLoader());
|
84
|
Definition definition = wsdlReader.readWSDL(locator);
|
85
|
return definition;
|
86
|
} catch (Exception e) {
|
87
|
throw new HibernateException(e);
|
88
|
}
|
89
|
}
|
90
|
|
91
|
//not tested if this works with jadira.usertype
|
92
|
@Override
|
93
|
public Serializable disassemble(Object value) throws HibernateException {
|
94
|
try {
|
95
|
WSDLFactory wsdlFactory = WSDLFactoryImpl.newInstance();
|
96
|
Definition definition = (Definition) value;
|
97
|
StringWriter stringWriter = new StringWriter();
|
98
|
WSDLWriter writer = wsdlFactory.newWSDLWriter();
|
99
|
writer.writeWSDL(definition, stringWriter);
|
100
|
return stringWriter.getBuffer().toString();
|
101
|
} catch (WSDLException e) {
|
102
|
throw new HibernateException(e);
|
103
|
}
|
104
|
}
|
105
|
|
106
|
|
107
|
|
108
|
@Override
|
109
|
public Definition nullSafeGet(ResultSet rs, String[]names, SessionImplementor session, Object o)
|
110
|
throws HibernateException, SQLException {
|
111
|
Clob val = (Clob)StandardBasicTypes.CLOB.nullSafeGet(rs, names, session, o);
|
112
|
// Clob val = (Clob) rs.getClob(names[0]);
|
113
|
if(val == null) {
|
114
|
return null;
|
115
|
} else {
|
116
|
try {
|
117
|
WSDLFactory wsdlFactory = WSDLFactoryImpl.newInstance();
|
118
|
WSDLReader wsdlReader = wsdlFactory.newWSDLReader();
|
119
|
Reader reader = val.getCharacterStream();
|
120
|
WSDLLocator locator = new LSIDWSDLLocator("wsdl",reader,Thread.currentThread().getContextClassLoader());
|
121
|
Definition definition = wsdlReader.readWSDL(locator);
|
122
|
return definition;
|
123
|
} catch (Exception e) {
|
124
|
throw new HibernateException(e);
|
125
|
}
|
126
|
}
|
127
|
}
|
128
|
|
129
|
|
130
|
|
131
|
@Override
|
132
|
public void nullSafeSet(PreparedStatement statement, Object value, int index, SessionImplementor session)
|
133
|
throws HibernateException, SQLException {
|
134
|
if (value == null) {
|
135
|
// statement.setNull(index, Types.CLOB); //old version
|
136
|
StandardBasicTypes.CLOB.nullSafeSet(statement, value, index, session);
|
137
|
} else {
|
138
|
try {
|
139
|
Definition definition = (Definition) value;
|
140
|
WSDLFactory wsdlFactory = WSDLFactoryImpl.newInstance();
|
141
|
StringWriter stringWriter = new StringWriter();
|
142
|
WSDLWriter writer = wsdlFactory.newWSDLWriter();
|
143
|
writer.writeWSDL(definition, stringWriter);
|
144
|
// statement.setClob(index, Hibernate.createClob(stringWriter.getBuffer().toString())); //old version
|
145
|
StandardBasicTypes.CLOB.nullSafeSet(statement, stringWriter.getBuffer().toString(), index, session);
|
146
|
} catch (WSDLException e) {
|
147
|
throw new HibernateException(e);
|
148
|
}
|
149
|
|
150
|
}
|
151
|
}
|
152
|
|
153
|
|
154
|
public Class returnedClass() {
|
155
|
return Definition.class;
|
156
|
}
|
157
|
|
158
|
|
159
|
@Override
|
160
|
public int[] sqlTypes() {
|
161
|
return SQL_TYPES;
|
162
|
}
|
163
|
|
164
|
}
|