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.MapKeyJoinColumn;
21
import javax.persistence.Transient;
22
import javax.wsdl.Definition;
23
import javax.xml.bind.annotation.XmlAccessType;
24
import javax.xml.bind.annotation.XmlAccessorType;
25
import javax.xml.bind.annotation.XmlElement;
26
import javax.xml.bind.annotation.XmlRootElement;
27
import javax.xml.bind.annotation.XmlTransient;
28
import javax.xml.bind.annotation.XmlType;
29
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
30

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

    
36
import com.ibm.lsid.MalformedLSIDException;
37

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

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

    
54
	/**
55
	 * 
56
	 */
57
	private static final long serialVersionUID = 9168994979216936689L;
58

    
59
	public static final String AUTHORITY_ID_PREFIX = "lsidauth:";
60
	
61
	private static final String AUTHORITY_PROTOCOL="http";
62
    private static final String AUTHORITY_PATH="/authority/";
63

    
64
    @XmlElement(name = "Authority")
65
    @NaturalId
66
	private String authority;
67
	
68
	// the resolved components of the lsid
69
    @XmlElement(name = "Server")
70
	private String server;
71
    @XmlElement(name = "Port")
72
	private int port = -1;
73
    @XmlElement(name = "Url")
74
	private String url;
75
	
76
	// the wsdl describing how to invoke operations on the authority
77
    @XmlTransient
78
	@Type(type = "wsdlDefinitionUserType")
79
	private Definition authorityWSDL;
80
	
81
    @XmlElement(name = "Namespaces")
82
    @XmlJavaTypeAdapter(NamespacesAdapter.class)
83
    @ElementCollection(fetch = FetchType.LAZY)
84
	@MapKeyColumn(name="namespaces_mapkey")  //from hibernate 3, maybe changed in future to default "namespaces_key"
85
    @Column(name="namespaces_element")   //from hibernate 3, maybe changed in future to default "namespaces"
86
    private Map<String,Class<? extends IIdentifiableEntity>> namespaces = new HashMap<String,Class<? extends IIdentifiableEntity>>();
87
	
88
	/**
89
	 * Hibernate requires a no-arguement constructor
90
	 */
91
	private LSIDAuthority() { }
92
	
93
	/**
94
	 * Construct an LSID authority object.
95
	 * @param String LSID Authority must be valid authority string, or an ID of the form: lsidauth:<validauthoritystring>"
96
	 */
97
	public LSIDAuthority(String authstr) throws MalformedLSIDException {
98
		try {
99
			authority = authstr.toLowerCase();
100
			if (authority.startsWith(AUTHORITY_ID_PREFIX))
101
				authority = authority.substring(AUTHORITY_ID_PREFIX.length());
102
		} catch (Exception e) {
103
			throw new MalformedLSIDException(e, "LSID Authority must be valid authority string, or of form: lsidauth:<validauthoritystring>");
104
		}								
105
	}
106
	
107
	/**
108
	 * Convenience constructor, construct an LSID authority object
109
	 * @param LSID use this LSID's authority to construct this object
110
	 */
111
	public LSIDAuthority(LSID lsid) throws MalformedLSIDException {
112
		authority = lsid.getAuthority();						
113
	}		
114
	
115
	/**
116
	 * Returns the authority String
117
	 * @return String the authority String
118
	 */
119
	public String getAuthority() {
120
		return authority;	
121
	}
122
	
123
	/**
124
	 * Returns the authority ID representation of this authority, lsidauth:authstr
125
	 * @return String the ID representation
126
	 */
127
	public String getAuthorityID() {
128
		return AUTHORITY_ID_PREFIX + authority;
129
	}
130
	
131
	/**
132
	 * Returns the authority String
133
	 * @return String the authority String
134
	 */
135
	public String toString() {
136
		return authority;	
137
	}
138
	
139
	/**
140
	 * Tests equality on the authority string
141
	 * @return
142
	 */
143
	public boolean equals(Object o) {
144
		if (o == this){
145
	    	return true;
146
	    }
147
	    if (!(o instanceof LSIDAuthority)){
148
	        return false;
149
	    }
150
	    LSIDAuthority auth = (LSIDAuthority)o;
151
	    //is toString really good for equal?
152
	    //Waht about empty strings?
153
	    //AM
154
	    return auth.toString().equals(toString());
155
	}
156
	
157
	/**
158
	 * Returns the port of the resolved Authority, invalid until resolved.
159
	 * @return int the port.
160
	 */
161
	public int getPort() {
162
		return port;
163
	}
164

    
165
	/**
166
	 * Returns the server of the resolved Authority, invalid until resolved.
167
	 * @return String the hostname of the server
168
	 */
169
	public String getServer() {
170
		return server;
171
	}
172
	
173
	/**
174
	 * Returns the url of the resolved Authority, invalid until resolved.  This overrides the 
175
	 * server and port properties, and might contain a full path or even a different protocol.
176
	 * @return String
177
	 */
178
	@Transient
179
	public String getUrl() {
180
		if (url == null) {
181
			if (server != null && port != -1)
182
				url = "http://" + getServer() + ":" + getPort() + AUTHORITY_PATH;
183
			else
184
				return null;
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;
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;
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
}
(38-38/70)