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