Factory method for molecular.Sequence
[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.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.hibernate.Hibernate;
32 import org.hibernate.HibernateException;
33 import org.hibernate.usertype.UserType;
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 implements UserType {
50 private static Log log = LogFactory.getLog(WSDLDefinitionUserType.class);
51 private static final int[] TYPES = { Types.CLOB };
52
53 public Object deepCopy(Object o) throws HibernateException {
54
55 if (o == null) {
56 return null;
57 }
58
59 Definition d = (Definition) o;
60
61 try {
62 WSDLFactory wsdlFactory = WSDLFactoryImpl.newInstance();
63 StringWriter stringWriter = new StringWriter();
64 WSDLWriter writer = wsdlFactory.newWSDLWriter();
65 writer.writeWSDL(d, stringWriter);
66 WSDLReader wsdlReader = wsdlFactory.newWSDLReader();
67 Reader reader = new StringReader(stringWriter.getBuffer().toString());
68 WSDLLocator locator = new LSIDWSDLLocator("wsdl",reader,Thread.currentThread().getContextClassLoader());
69 Definition definition = wsdlReader.readWSDL(locator);
70 return definition;
71 } catch (Exception e) {
72 throw new HibernateException(e);
73 }
74 }
75
76 public Object assemble(Serializable cached, Object owner) throws HibernateException {
77 try {
78 WSDLFactory wsdlFactory = WSDLFactoryImpl.newInstance();
79 WSDLReader wsdlReader = wsdlFactory.newWSDLReader();
80 Reader reader = new StringReader(cached.toString());
81 WSDLLocator locator = new LSIDWSDLLocator("wsdl",reader,Thread.currentThread().getContextClassLoader());
82 Definition definition = wsdlReader.readWSDL(locator);
83 return definition;
84 } catch (Exception e) {
85 throw new HibernateException(e);
86 }
87 }
88
89 public Serializable disassemble(Object value) throws HibernateException {
90 try {
91 WSDLFactory wsdlFactory = WSDLFactoryImpl.newInstance();
92 Definition definition = (Definition) value;
93 StringWriter stringWriter = new StringWriter();
94 WSDLWriter writer = wsdlFactory.newWSDLWriter();
95 writer.writeWSDL(definition, stringWriter);
96 return stringWriter.getBuffer().toString();
97 } catch (WSDLException e) {
98 throw new HibernateException(e);
99 }
100 }
101
102 public boolean equals(Object x, Object y) throws HibernateException {
103 return (x == y) || (x != null && y != null && (x.equals(y)));
104 }
105
106 public int hashCode(Object x) throws HibernateException {
107 return x.hashCode();
108 }
109
110 public boolean isMutable() {
111 return true;
112 }
113
114 public Object nullSafeGet(ResultSet resultSet, String[] names, Object o)
115 throws HibernateException, SQLException {
116 Clob val = (Clob) resultSet.getClob(names[0]);
117 if(val == null) {
118 return null;
119 } else {
120 try {
121 WSDLFactory wsdlFactory = WSDLFactoryImpl.newInstance();
122 WSDLReader wsdlReader = wsdlFactory.newWSDLReader();
123 Reader reader = val.getCharacterStream();
124 WSDLLocator locator = new LSIDWSDLLocator("wsdl",reader,Thread.currentThread().getContextClassLoader());
125 Definition definition = wsdlReader.readWSDL(locator);
126 return definition;
127 } catch (Exception e) {
128 throw new HibernateException(e);
129 }
130 }
131 }
132
133 public void nullSafeSet(PreparedStatement preparedStatement, Object o, int index)
134 throws HibernateException, SQLException {
135 if (null == o) {
136 preparedStatement.setNull(index, Types.CLOB);
137 } else {
138 try {
139 Definition definition = (Definition) o;
140 WSDLFactory wsdlFactory = WSDLFactoryImpl.newInstance();
141 StringWriter stringWriter = new StringWriter();
142 WSDLWriter writer = wsdlFactory.newWSDLWriter();
143 writer.writeWSDL(definition, stringWriter);
144 preparedStatement.setClob(index, Hibernate.createClob(stringWriter.getBuffer().toString()));
145 } catch (WSDLException e) {
146 throw new HibernateException(e);
147 }
148
149 }
150 }
151
152 public Object replace(Object original, Object target, Object owner)
153 throws HibernateException {
154 return original;
155 }
156
157 public Class returnedClass() {
158 return Definition.class;
159 }
160
161 public int[] sqlTypes() {
162 return TYPES;
163 }
164
165 }