Project

General

Profile

Download (7.07 KB) Statistics
| Branch: | Tag: | Revision:
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.model.common;
11

    
12
import java.util.HashMap;
13
import java.util.Map;
14

    
15
import javax.persistence.Column;
16
import javax.persistence.ElementCollection;
17
import javax.persistence.Entity;
18
import javax.persistence.FetchType;
19
import javax.persistence.MapKeyColumn;
20
import javax.persistence.Transient;
21
import javax.wsdl.Definition;
22
import javax.xml.bind.annotation.XmlAccessType;
23
import javax.xml.bind.annotation.XmlAccessorType;
24
import javax.xml.bind.annotation.XmlElement;
25
import javax.xml.bind.annotation.XmlRootElement;
26
import javax.xml.bind.annotation.XmlTransient;
27
import javax.xml.bind.annotation.XmlType;
28
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
29

    
30
import org.hibernate.annotations.NaturalId;
31
import org.hibernate.annotations.Type;
32
import org.hibernate.annotations.TypeDef;
33
import org.hibernate.annotations.TypeDefs;
34

    
35
import com.ibm.lsid.MalformedLSIDException;
36

    
37
import eu.etaxonomy.cdm.hibernate.WSDLDefinitionUserType;
38
import eu.etaxonomy.cdm.jaxb.NamespacesAdapter;
39

    
40
@XmlAccessorType(XmlAccessType.FIELD)
41
@XmlType(name = "LSIDAuthority", propOrder = {
42
		"authority",
43
		"server",
44
		"port",
45
		"url",
46
		"namespaces"
47
})
48
@XmlRootElement(name = "LSIDAuthority")
49
@Entity
50
@TypeDefs(@TypeDef(name="wsdlDefinitionUserType", typeClass=WSDLDefinitionUserType.class))
51
public class LSIDAuthority extends CdmBase {
52
	private static final long serialVersionUID = 9168994979216936689L;
53

    
54
	public static final String AUTHORITY_ID_PREFIX = "lsidauth:";
55

    
56
	private static final String AUTHORITY_PROTOCOL="http";
57
    private static final String AUTHORITY_PATH="/authority/";
58

    
59
    @XmlElement(name = "Authority")
60
    @NaturalId
61
	private String authority;
62

    
63
	// the resolved components of the lsid
64
    @XmlElement(name = "Server")
65
	private String server;
66
    @XmlElement(name = "Port")
67
	private int port = -1;
68
    @XmlElement(name = "Url")
69
	private String url;
70

    
71
	// the wsdl describing how to invoke operations on the authority
72
    @XmlTransient
73
	@Type(type = "wsdlDefinitionUserType")
74
	private Definition authorityWSDL;
75

    
76
    @XmlElement(name = "Namespaces")
77
    @XmlJavaTypeAdapter(NamespacesAdapter.class)
78
    @ElementCollection(fetch = FetchType.LAZY)
79
	@MapKeyColumn(name="namespaces_mapkey")  //from hibernate 3, maybe changed in future to default "namespaces_key"
80
    @Column(name="namespaces_element")   //from hibernate 3, maybe changed in future to default "namespaces"
81
    private Map<String,Class<? extends IIdentifiableEntity>> namespaces = new HashMap<>();
82

    
83
	/**
84
	 * Hibernate requires a no-arguement constructor
85
	 */
86
	@SuppressWarnings("unused")
87
    private LSIDAuthority() { }
88

    
89
	/**
90
	 * Construct an LSID authority object.
91
	 * @param String LSID Authority must be valid authority string, or an ID of the form: lsidauth:<validauthoritystring>"
92
	 */
93
	public LSIDAuthority(String authstr) throws MalformedLSIDException {
94
		try {
95
			authority = authstr.toLowerCase();
96
			if (authority.startsWith(AUTHORITY_ID_PREFIX)) {
97
                authority = authority.substring(AUTHORITY_ID_PREFIX.length());
98
            }
99
		} catch (Exception e) {
100
			throw new MalformedLSIDException(e, "LSID Authority must be valid authority string, or of form: lsidauth:<validauthoritystring>");
101
		}
102
	}
103

    
104
	/**
105
	 * Convenience constructor, construct an LSID authority object
106
	 * @param LSID use this LSID's authority to construct this object
107
	 */
108
	public LSIDAuthority(LSID lsid){
109
		authority = lsid.getAuthority();
110
	}
111

    
112
	/**
113
	 * Returns the authority String
114
	 * @return String the authority String
115
	 */
116
	public String getAuthority() {
117
		return authority;
118
	}
119

    
120
	/**
121
	 * Returns the authority ID representation of this authority, lsidauth:authstr
122
	 * @return String the ID representation
123
	 */
124
	public String getAuthorityID() {
125
		return AUTHORITY_ID_PREFIX + authority;
126
	}
127

    
128
	/**
129
	 * Returns the authority String
130
	 * @return String the authority String
131
	 */
132
	@Override
133
    public String toString() {
134
		return authority;
135
	}
136

    
137
	/**
138
	 * Tests equality on the authority string
139
	 * @return
140
	 */
141
	@Override
142
    public boolean equals(Object o) {
143
		if (o == this){
144
	    	return true;
145
	    }
146
	    if (!(o instanceof LSIDAuthority)){
147
	        return false;
148
	    }
149
	    LSIDAuthority auth = (LSIDAuthority)o;
150
	    //is toString really good for equal?
151
	    //Waht about empty strings?
152
	    //AM
153
	    return auth.toString().equals(toString());
154
	}
155

    
156
	/**
157
	 * Returns the port of the resolved Authority, invalid until resolved.
158
	 * @return int the port.
159
	 */
160
	public int getPort() {
161
		return port;
162
	}
163

    
164
	/**
165
	 * Returns the server of the resolved Authority, invalid until resolved.
166
	 * @return String the hostname of the server
167
	 */
168
	public String getServer() {
169
		return server;
170
	}
171

    
172
	/**
173
	 * Returns the url of the resolved Authority, invalid until resolved.  This overrides the
174
	 * server and port properties, and might contain a full path or even a different protocol.
175
	 * @return String
176
	 */
177
	@Transient
178
	public String getUrl() {
179
		if (url == null) {
180
			if (server != null && port != -1) {
181
                url = "http://" + getServer() + ":" + getPort() + AUTHORITY_PATH;
182
            } else {
183
                return null;
184
            }
185
		}
186
		return url;
187
	}
188

    
189
	/**
190
	 * Sets the port.
191
	 * @param port The port to set
192
	 */
193
	public void setPort(int port) {
194
		this.port = port;
195
	}
196

    
197
	/**
198
	 * Sets the server.
199
	 * @param server The server to set
200
	 */
201
	public void setServer(String server) {
202
		this.server = server == "" ? null : server;
203
	}
204

    
205
	/**
206
	 * Sets the URL to use.  This overrides the server and port properties, and might contain a full path or even a
207
	 * different protocol.
208
	 * @param server The server to set
209
	 */
210
	public void setUrl(String url) {
211
		this.url = url == "" ? null : url;
212
	}
213

    
214
	/**
215
	 * Set the wsdl describing the ports available
216
	 * @param LSIDWSDLWrapper the wsdl to set
217
	 */
218
	public void setWSDL(Definition wsdl) {
219
		this.authorityWSDL = wsdl;
220
	}
221

    
222
	/**
223
	 * get the wsdl describing the ports available
224
	 * @return LSIDWSDLWRapper the wsdl
225
	 */
226
	public Definition getWSDL() {
227
		return this.authorityWSDL;
228
	}
229

    
230
	/**
231
	 * @return boolean, whether or not the authority has been resolved.
232
	 */
233
	@Transient
234
	public boolean isResolved() {
235
		return (getUrl() != null);
236
	}
237

    
238
	/**
239
	 * get the url of an authority with the given server and port
240
	 */
241
	public String getAuthorityEnpointURL(String server, int port) {
242
		return AUTHORITY_PROTOCOL + "://" + server + ":" + port + AUTHORITY_PATH;
243
	}
244

    
245
	public Map<String,Class<? extends IIdentifiableEntity>> getNamespaces() {
246
		return namespaces;
247
	}
248

    
249
	public void addNamespace(String namespace, Class<? extends IIdentifiableEntity> identifiableClass) {
250
		this.namespaces.put(namespace, identifiableClass);
251
	}
252

    
253
	public void removeNamespace(String namespace) {
254
		this.namespaces.remove(namespace);
255
	}
256
}
(36-36/56)