4b89f1ef8b100d17b992a6ebc8ca6906cdb0fa23
[cdmlib.git] / cdmlib-model / src / main / java / eu / etaxonomy / cdm / model / common / LSIDAuthority.java
1 package eu.etaxonomy.cdm.model.common;
2
3 import java.util.HashMap;
4 import java.util.Map;
5
6 import javax.persistence.Entity;
7 import javax.persistence.FetchType;
8 import javax.persistence.Transient;
9 import javax.wsdl.Definition;
10 import javax.xml.bind.annotation.XmlAccessType;
11 import javax.xml.bind.annotation.XmlAccessorType;
12 import javax.xml.bind.annotation.XmlElement;
13 import javax.xml.bind.annotation.XmlRootElement;
14 import javax.xml.bind.annotation.XmlTransient;
15 import javax.xml.bind.annotation.XmlType;
16 import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
17
18 import org.hibernate.annotations.CollectionOfElements;
19 import org.hibernate.annotations.NaturalId;
20 import org.hibernate.annotations.Type;
21 import org.hibernate.annotations.TypeDef;
22 import org.hibernate.annotations.TypeDefs;
23 import org.hibernate.envers.Audited;
24
25 import com.ibm.lsid.MalformedLSIDException;
26
27 import eu.etaxonomy.cdm.jaxb.NamespacesAdapter;
28
29 @XmlAccessorType(XmlAccessType.FIELD)
30 @XmlType(name = "LSIDAuthority", propOrder = {
31 "authority",
32 "server",
33 "port",
34 "url",
35 "namespaces"
36 })
37 @XmlRootElement(name = "LSIDAuthority")
38 @Entity
39 @TypeDefs(@TypeDef(name="wsdlDefinitionUserType", typeClass=WSDLDefinitionUserType.class))
40 public class LSIDAuthority extends CdmBase {
41
42 /**
43 *
44 */
45 private static final long serialVersionUID = 9168994979216936689L;
46
47 public static final String AUTHORITY_ID_PREFIX = "lsidauth:";
48
49 private static final String AUTHORITY_PROTOCOL="http";
50 private static final String AUTHORITY_PATH="/authority/";
51
52 @XmlElement(name = "Authority")
53 @NaturalId
54 private String authority;
55
56 // the resolved components of the lsid
57 @XmlElement(name = "Server")
58 private String server;
59 @XmlElement(name = "Port")
60 private int port = -1;
61 @XmlElement(name = "Url")
62 private String url;
63
64 // the wsdl describing how to invoke operations on the authority
65 @XmlTransient
66 @Type(type = "wsdlDefinitionUserType")
67 private Definition authorityWSDL;
68
69 @XmlElement(name = "Namespaces")
70 @XmlJavaTypeAdapter(NamespacesAdapter.class)
71 @CollectionOfElements(fetch = FetchType.LAZY)
72 private Map<String,Class<? extends IIdentifiableEntity>> namespaces = new HashMap<String,Class<? extends IIdentifiableEntity>>();
73
74 /**
75 * Hibernate requires a no-arguement constructor (this could be private, I suppose)
76 */
77 private LSIDAuthority() { }
78
79 /**
80 * Construct an LSID authority object.
81 * @param String LSID Authority must be valid authority string, or an ID of the form: lsidauth:<validauthoritystring>"
82 */
83 public LSIDAuthority(String authstr) throws MalformedLSIDException {
84 try {
85 authority = authstr.toLowerCase();
86 if (authority.startsWith(AUTHORITY_ID_PREFIX))
87 authority = authority.substring(AUTHORITY_ID_PREFIX.length());
88 } catch (Exception e) {
89 throw new MalformedLSIDException(e, "LSID Authority must be valid authority string, or of form: lsidauth:<validauthoritystring>");
90 }
91 }
92
93 /**
94 * Convenience constructor, construct an LSID authority object
95 * @param LSID use this LSID's authority to construct this object
96 */
97 public LSIDAuthority(LSID lsid) throws MalformedLSIDException {
98 authority = lsid.getAuthority();
99 }
100
101 /**
102 * Returns the authority String
103 * @return String the authority String
104 */
105 public String getAuthority() {
106 return authority;
107 }
108
109 /**
110 * Returns the authority ID representation of this authority, lsidauth:authstr
111 * @return String the ID representation
112 */
113 public String getAuthorityID() {
114 return AUTHORITY_ID_PREFIX + authority;
115 }
116
117 /**
118 * Returns the authority String
119 * @return String the authority String
120 */
121 public String toString() {
122 return authority;
123 }
124
125 /**
126 * Tests equality on the authority string
127 * @return
128 */
129 public boolean equals(Object o) {
130 if (!(o instanceof LSIDAuthority))
131 return false;
132 LSIDAuthority auth = (LSIDAuthority)o;
133 return o.toString().equals(toString());
134 }
135
136 /**
137 * Returns the port of the resolved Authority, invalid until resolved.
138 * @return int the port.
139 */
140 public int getPort() {
141 return port;
142 }
143
144 /**
145 * Returns the server of the resolved Authority, invalid until resolved.
146 * @return String the hostname of the server
147 */
148 public String getServer() {
149 return server;
150 }
151
152 /**
153 * Returns the url of the resolved Authority, invalid until resolved. This overrides the
154 * server and port properties, and might contain a full path or even a different protocol.
155 * @return String
156 */
157 @Transient
158 public String getUrl() {
159 if (url == null) {
160 if (server != null && port != -1)
161 url = "http://" + getServer() + ":" + getPort() + AUTHORITY_PATH;
162 else
163 return null;
164 }
165 return url;
166 }
167
168 /**
169 * Sets the port.
170 * @param port The port to set
171 */
172 public void setPort(int port) {
173 this.port = port;
174 }
175
176 /**
177 * Sets the server.
178 * @param server The server to set
179 */
180 public void setServer(String server) {
181 this.server = server;
182 }
183
184 /**
185 * Sets the URL to use. This overrides the server and port properties, and might contain a full path or even a
186 * different protocol.
187 * @param server The server to set
188 */
189 public void setUrl(String url) {
190 this.url = url;
191 }
192
193 /**
194 * Set the wsdl describing the ports available
195 * @param LSIDWSDLWrapper the wsdl to set
196 */
197 public void setWSDL(Definition wsdl) {
198 this.authorityWSDL = wsdl;
199 }
200
201 /**
202 * get the wsdl describing the ports available
203 * @return LSIDWSDLWRapper the wsdl
204 */
205 public Definition getWSDL() {
206 return this.authorityWSDL;
207 }
208
209 /**
210 * @return boolean, whether or not the authority has been resolved.
211 */
212 @Transient
213 public boolean isResolved() {
214 return (getUrl() != null);
215 }
216
217 /**
218 * get the url of an authority with the given server and port
219 */
220 public String getAuthorityEnpointURL(String server, int port) {
221 return AUTHORITY_PROTOCOL + "://" + server + ":" + port + AUTHORITY_PATH;
222 }
223
224 public Map<String,Class<? extends IIdentifiableEntity>> getNamespaces() {
225 return namespaces;
226 }
227
228 public void addNamespace(String namespace, Class<? extends IIdentifiableEntity> identifiableClass) {
229 this.namespaces.put(namespace, identifiableClass);
230 }
231
232 public void removeNamespace(String namespace) {
233 this.namespaces.remove(namespace);
234 }
235 }