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