Project

General

Profile

Download (7.04 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<String,Class<? extends IIdentifiableEntity>>();
82
	
83
	/**
84
	 * Hibernate requires a no-arguement constructor
85
	 */
86
	private LSIDAuthority() { }
87
	
88
	/**
89
	 * Construct an LSID authority object.
90
	 * @param String LSID Authority must be valid authority string, or an ID of the form: lsidauth:<validauthoritystring>"
91
	 */
92
	public LSIDAuthority(String authstr) throws MalformedLSIDException {
93
		try {
94
			authority = authstr.toLowerCase();
95
			if (authority.startsWith(AUTHORITY_ID_PREFIX))
96
				authority = authority.substring(AUTHORITY_ID_PREFIX.length());
97
		} catch (Exception e) {
98
			throw new MalformedLSIDException(e, "LSID Authority must be valid authority string, or of form: lsidauth:<validauthoritystring>");
99
		}								
100
	}
101
	
102
	/**
103
	 * Convenience constructor, construct an LSID authority object
104
	 * @param LSID use this LSID's authority to construct this object
105
	 */
106
	public LSIDAuthority(LSID lsid) throws MalformedLSIDException {
107
		authority = lsid.getAuthority();						
108
	}		
109
	
110
	/**
111
	 * Returns the authority String
112
	 * @return String the authority String
113
	 */
114
	public String getAuthority() {
115
		return authority;	
116
	}
117
	
118
	/**
119
	 * Returns the authority ID representation of this authority, lsidauth:authstr
120
	 * @return String the ID representation
121
	 */
122
	public String getAuthorityID() {
123
		return AUTHORITY_ID_PREFIX + authority;
124
	}
125
	
126
	/**
127
	 * Returns the authority String
128
	 * @return String the authority String
129
	 */
130
	public String toString() {
131
		return authority;	
132
	}
133
	
134
	/**
135
	 * Tests equality on the authority string
136
	 * @return
137
	 */
138
	public boolean equals(Object o) {
139
		if (o == this){
140
	    	return true;
141
	    }
142
	    if (!(o instanceof LSIDAuthority)){
143
	        return false;
144
	    }
145
	    LSIDAuthority auth = (LSIDAuthority)o;
146
	    //is toString really good for equal?
147
	    //Waht about empty strings?
148
	    //AM
149
	    return auth.toString().equals(toString());
150
	}
151
	
152
	/**
153
	 * Returns the port of the resolved Authority, invalid until resolved.
154
	 * @return int the port.
155
	 */
156
	public int getPort() {
157
		return port;
158
	}
159

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

    
184
	/**
185
	 * Sets the port.
186
	 * @param port The port to set
187
	 */
188
	public void setPort(int port) {
189
		this.port = port;
190
	}
191

    
192
	/**
193
	 * Sets the server.
194
	 * @param server The server to set
195
	 */
196
	public void setServer(String server) {
197
		this.server = server == "" ? null : server;
198
	}
199
	
200
	/**
201
	 * Sets the URL to use.  This overrides the server and port properties, and might contain a full path or even a 
202
	 * different protocol.
203
	 * @param server The server to set
204
	 */
205
	public void setUrl(String url) {
206
		this.url = url == "" ? null : url;
207
	}
208
	
209
	/**
210
	 * Set the wsdl describing the ports available
211
	 * @param LSIDWSDLWrapper the wsdl to set
212
	 */
213
	public void setWSDL(Definition wsdl) {
214
		this.authorityWSDL = wsdl;
215
	}
216
	
217
	/**
218
	 * get the wsdl describing the ports available
219
	 * @return LSIDWSDLWRapper the wsdl
220
	 */
221
	public Definition getWSDL() {
222
		return this.authorityWSDL;
223
	}
224
	
225
	/**
226
	 * @return boolean, whether or not the authority has been resolved.
227
	 */
228
	@Transient
229
	public boolean isResolved() {
230
		return (getUrl() != null);
231
	}
232
	
233
	/**
234
	 * get the url of an authority with the given server and port
235
	 */
236
	public String getAuthorityEnpointURL(String server, int port) {
237
		return AUTHORITY_PROTOCOL + "://" + server + ":" + port + AUTHORITY_PATH;	
238
	}
239

    
240
	public Map<String,Class<? extends IIdentifiableEntity>> getNamespaces() {
241
		return namespaces;
242
	}
243
	
244
	public void addNamespace(String namespace, Class<? extends IIdentifiableEntity> identifiableClass) {
245
		this.namespaces.put(namespace, identifiableClass);
246
	}
247
	
248
	public void removeNamespace(String namespace) {
249
		this.namespaces.remove(namespace);
250
	}
251
}
(41-41/72)