1
|
package org.cybertaxonomy.utis.checklist;
|
2
|
|
3
|
|
4
|
import java.rmi.RemoteException;
|
5
|
import java.util.EnumSet;
|
6
|
|
7
|
import javax.xml.rpc.ServiceException;
|
8
|
|
9
|
import org.apache.http.HttpHost;
|
10
|
import org.cybertaxonomy.utis.checklist.worms.AphiaNameServiceLocator;
|
11
|
import org.cybertaxonomy.utis.checklist.worms.AphiaNameServicePortType;
|
12
|
import org.cybertaxonomy.utis.checklist.worms.AphiaRecord;
|
13
|
import org.cybertaxonomy.utis.client.ServiceProviderInfo;
|
14
|
import org.cybertaxonomy.utis.query.SoapClient;
|
15
|
import org.cybertaxonomy.utis.tnr.msg.Classification;
|
16
|
import org.cybertaxonomy.utis.tnr.msg.NameType;
|
17
|
import org.cybertaxonomy.utis.tnr.msg.Query;
|
18
|
import org.cybertaxonomy.utis.tnr.msg.Response;
|
19
|
import org.cybertaxonomy.utis.tnr.msg.Source;
|
20
|
import org.cybertaxonomy.utis.tnr.msg.Synonym;
|
21
|
import org.cybertaxonomy.utis.tnr.msg.Taxon;
|
22
|
import org.cybertaxonomy.utis.tnr.msg.TaxonName;
|
23
|
import org.cybertaxonomy.utis.tnr.msg.TnrMsg;
|
24
|
import org.cybertaxonomy.utis.utils.IdentifierUtils;
|
25
|
import org.cybertaxonomy.utis.utils.TnrMsgUtils;
|
26
|
|
27
|
|
28
|
public class WoRMSClient extends BaseChecklistClient<SoapClient> {
|
29
|
|
30
|
/**
|
31
|
*
|
32
|
*/
|
33
|
private static final HttpHost HTTP_HOST = new HttpHost("http://www.marinespecies.org",80);
|
34
|
public static final String ID = "worms";
|
35
|
public static final String LABEL = "WoRMS";
|
36
|
public static final String URL = "http://www.marinespecies.org/index.php";
|
37
|
public static final String DATA_AGR_URL = "";
|
38
|
|
39
|
public static final EnumSet<SearchMode> SEARCH_MODES = EnumSet.of(
|
40
|
SearchMode.scientificNameExact,
|
41
|
SearchMode.scientificNameLike,
|
42
|
SearchMode.vernacularNameExact,
|
43
|
SearchMode.vernacularNameLike,
|
44
|
SearchMode.findByIdentifier
|
45
|
);
|
46
|
|
47
|
public static final EnumSet<SearchMode> SCIENTIFICNAME_SEARCH_MODES = EnumSet.of(
|
48
|
SearchMode.scientificNameExact,
|
49
|
SearchMode.scientificNameLike
|
50
|
);
|
51
|
|
52
|
|
53
|
|
54
|
public WoRMSClient() {
|
55
|
super();
|
56
|
|
57
|
}
|
58
|
|
59
|
/**
|
60
|
* {@inheritDoc}
|
61
|
*/
|
62
|
@Override
|
63
|
public void initQueryClient() {
|
64
|
queryClient = new SoapClient();
|
65
|
}
|
66
|
|
67
|
@Override
|
68
|
public ServiceProviderInfo buildServiceProviderInfo() {
|
69
|
ServiceProviderInfo checklistInfo = new ServiceProviderInfo(ID,LABEL,URL,DATA_AGR_URL, getSearchModes());
|
70
|
return checklistInfo;
|
71
|
}
|
72
|
|
73
|
@Override
|
74
|
public EnumSet<SearchMode> getSearchModes() {
|
75
|
return SEARCH_MODES;
|
76
|
}
|
77
|
|
78
|
/**
|
79
|
* @return
|
80
|
* @throws DRFChecklistException
|
81
|
*/
|
82
|
private AphiaNameServicePortType getAphiaNameService() throws DRFChecklistException {
|
83
|
AphiaNameServicePortType aphianspt;
|
84
|
|
85
|
AphiaNameServiceLocator aphiansl = new AphiaNameServiceLocator();
|
86
|
|
87
|
try {
|
88
|
aphianspt = aphiansl.getAphiaNameServicePort();
|
89
|
} catch (ServiceException e) {
|
90
|
logger.error("Error in accessing AphiaNameSerice", e);
|
91
|
throw new DRFChecklistException("Error in accessing AphiaNameService");
|
92
|
}
|
93
|
return aphianspt;
|
94
|
}
|
95
|
|
96
|
/**
|
97
|
* @param aphianspt
|
98
|
* @param record
|
99
|
* @return
|
100
|
* @throws RemoteException
|
101
|
*/
|
102
|
private Response tnrResponseFromRecord(AphiaNameServicePortType aphianspt, AphiaRecord record, Query.Request request)
|
103
|
throws RemoteException {
|
104
|
|
105
|
Response tnrResponse = TnrMsgUtils.tnrResponseFor(getServiceProviderInfo());
|
106
|
|
107
|
SearchMode searchMode = SearchMode.valueOf(request.getSearchMode());
|
108
|
|
109
|
int accNameGUID = record.getValid_AphiaID();
|
110
|
String matchingName = record.getScientificname();
|
111
|
if(SCIENTIFICNAME_SEARCH_MODES.contains(searchMode)){
|
112
|
tnrResponse.setMatchingNameString(matchingName);
|
113
|
}
|
114
|
|
115
|
// case when accepted name
|
116
|
if(record.getAphiaID() == accNameGUID) {
|
117
|
Taxon accName = generateAccName(record);
|
118
|
tnrResponse.setTaxon(accName);
|
119
|
if(SCIENTIFICNAME_SEARCH_MODES.contains(searchMode)){
|
120
|
tnrResponse.setMatchingNameType(NameType.TAXON);
|
121
|
}
|
122
|
} else {
|
123
|
// case when synonym
|
124
|
AphiaRecord accNameRecord = aphianspt.getAphiaRecordByID(accNameGUID);
|
125
|
Taxon accName = generateAccName(accNameRecord);
|
126
|
tnrResponse.setTaxon(accName);
|
127
|
if(SCIENTIFICNAME_SEARCH_MODES.contains(searchMode)){
|
128
|
tnrResponse.setMatchingNameType(NameType.SYNONYM);
|
129
|
}
|
130
|
}
|
131
|
|
132
|
AphiaRecord[] synonyms = aphianspt.getAphiaSynonymsByID(accNameGUID);
|
133
|
|
134
|
if(request.isAddSynonymy() && synonyms != null && synonyms.length > 0) {
|
135
|
generateSynonyms(synonyms, tnrResponse);
|
136
|
}
|
137
|
return tnrResponse;
|
138
|
}
|
139
|
|
140
|
private Taxon generateAccName(AphiaRecord taxonRecord) {
|
141
|
Taxon accName = new Taxon();
|
142
|
TaxonName taxonName = new TaxonName();
|
143
|
|
144
|
String resName = taxonRecord.getScientificname();
|
145
|
taxonName.setFullName(resName + " " + taxonRecord.getAuthority());
|
146
|
|
147
|
taxonName.setCanonicalName(resName);
|
148
|
|
149
|
taxonName.setRank(taxonRecord.getRank());
|
150
|
taxonName.setAuthorship(taxonRecord.getAuthority());
|
151
|
|
152
|
accName.setTaxonName(taxonName);
|
153
|
accName.setTaxonomicStatus(taxonRecord.getStatus());
|
154
|
|
155
|
accName.setUrl(taxonRecord.getUrl());
|
156
|
accName.setIdentifier(taxonRecord.getLsid());
|
157
|
|
158
|
|
159
|
//FIXME : To fill in
|
160
|
String sourceUrl = taxonRecord.getUrl();
|
161
|
String sourceDatasetID = "";
|
162
|
String sourceDatasetName = "";
|
163
|
String sourceName = "";
|
164
|
|
165
|
Source source = new Source();
|
166
|
source.setIdentifier(sourceDatasetID);
|
167
|
source.setDatasetName(sourceDatasetName);
|
168
|
source.setName(sourceName);
|
169
|
source.setUrl(sourceUrl);
|
170
|
accName.getSources().add(source);
|
171
|
|
172
|
Classification c = new Classification();
|
173
|
c.setKingdom(taxonRecord.getKingdom());
|
174
|
c.setPhylum(taxonRecord.getPhylum());
|
175
|
c.setClazz("");
|
176
|
c.setOrder(taxonRecord.getOrder());
|
177
|
c.setFamily(taxonRecord.getFamily());
|
178
|
c.setGenus(taxonRecord.getGenus());
|
179
|
accName.setClassification(c);
|
180
|
|
181
|
return accName;
|
182
|
}
|
183
|
|
184
|
|
185
|
private void generateSynonyms(AphiaRecord[] synonyms, Response tnrResponse) {
|
186
|
|
187
|
for(AphiaRecord synRecord : synonyms) {
|
188
|
Synonym synonym = new Synonym();
|
189
|
|
190
|
TaxonName taxonName = new TaxonName();
|
191
|
|
192
|
String resName = synRecord.getScientificname();
|
193
|
taxonName.setFullName(resName + " " + synRecord.getAuthority());
|
194
|
|
195
|
taxonName.setCanonicalName(resName);
|
196
|
|
197
|
taxonName.setRank(synRecord.getRank());
|
198
|
taxonName.setAuthorship(synRecord.getAuthority());
|
199
|
|
200
|
synonym.setTaxonName(taxonName);
|
201
|
synonym.setTaxonomicStatus(synRecord.getStatus());
|
202
|
|
203
|
synonym.setUrl(synRecord.getUrl());
|
204
|
|
205
|
//FIXME : To fill in
|
206
|
String sourceUrl = synRecord.getUrl();
|
207
|
String sourceDatasetID = "";
|
208
|
String sourceDatasetName = "";
|
209
|
String sourceName = "";
|
210
|
|
211
|
Source source = new Source();
|
212
|
source.setIdentifier(sourceDatasetID);
|
213
|
source.setDatasetName(sourceDatasetName);
|
214
|
source.setName(sourceName);
|
215
|
source.setUrl(sourceUrl);
|
216
|
synonym.getSources().add(source);
|
217
|
|
218
|
tnrResponse.getSynonym().add(synonym);
|
219
|
}
|
220
|
}
|
221
|
|
222
|
|
223
|
|
224
|
@Override
|
225
|
public void resolveScientificNamesExact(TnrMsg tnrMsg) throws DRFChecklistException {
|
226
|
|
227
|
Query query = singleQueryFrom(tnrMsg);
|
228
|
String name = query.getRequest().getQueryString();
|
229
|
AphiaNameServicePortType aphianspt = getAphiaNameService();
|
230
|
|
231
|
try {
|
232
|
AphiaRecord record = null;
|
233
|
try {
|
234
|
Integer nameAphiaID = aphianspt.getAphiaID(name, false);
|
235
|
logger.debug("nameAphiaID : " + nameAphiaID);
|
236
|
record = aphianspt.getAphiaRecordByID(nameAphiaID);
|
237
|
Response tnrResponse = tnrResponseFromRecord(aphianspt, record, query.getRequest());
|
238
|
query.getResponse().add(tnrResponse);
|
239
|
} catch(NullPointerException npe) {
|
240
|
//FIXME : Workaround for NPE thrown by the aphia stub due to a,
|
241
|
// null aphia id (Integer), when the name is not present
|
242
|
// in the db
|
243
|
}
|
244
|
} catch (RemoteException e) {
|
245
|
logger.error("Error in autogenerated getGUID method", e);
|
246
|
throw new DRFChecklistException("Error in getGUID method", e);
|
247
|
}
|
248
|
|
249
|
}
|
250
|
|
251
|
@Override
|
252
|
public void resolveScientificNamesLike(TnrMsg tnrMsg) throws DRFChecklistException {
|
253
|
|
254
|
Query query = singleQueryFrom(tnrMsg);
|
255
|
String name = query.getRequest().getQueryString();
|
256
|
AphiaNameServicePortType aphianspt = getAphiaNameService();
|
257
|
boolean fuzzy = false;
|
258
|
|
259
|
try {
|
260
|
AphiaRecord[] records = aphianspt.getAphiaRecords(name + "%", true, fuzzy, false, 1);
|
261
|
if(records != null){
|
262
|
for (AphiaRecord record : records) {
|
263
|
Response tnrResponse = tnrResponseFromRecord(aphianspt, record, query.getRequest());
|
264
|
query.getResponse().add(tnrResponse);
|
265
|
}
|
266
|
}
|
267
|
|
268
|
} catch (RemoteException e) {
|
269
|
logger.error("Error in getGUID method in AphiaNameSerice", e);
|
270
|
throw new DRFChecklistException("Error in getGUID method in AphiaNameSerice", e);
|
271
|
}
|
272
|
|
273
|
}
|
274
|
|
275
|
@Override
|
276
|
public void resolveVernacularNamesExact(TnrMsg tnrMsg) throws DRFChecklistException {
|
277
|
|
278
|
Query query = singleQueryFrom(tnrMsg);
|
279
|
String name = query.getRequest().getQueryString();
|
280
|
AphiaNameServicePortType aphianspt = getAphiaNameService();
|
281
|
|
282
|
try {
|
283
|
AphiaRecord[] records = aphianspt.getAphiaRecordsByVernacular(name, false, 1);
|
284
|
if(records != null){
|
285
|
for (AphiaRecord record : records) {
|
286
|
Response tnrResponse = tnrResponseFromRecord(aphianspt, record, query.getRequest());
|
287
|
query.getResponse().add(tnrResponse);
|
288
|
}
|
289
|
}
|
290
|
|
291
|
} catch (RemoteException e) {
|
292
|
logger.error("Error in getAphiaRecordsByVernacular() in AphiaNameSerice", e);
|
293
|
throw new DRFChecklistException("Error in getGUID method in AphiaNameSerice", e);
|
294
|
}
|
295
|
|
296
|
}
|
297
|
|
298
|
@Override
|
299
|
public void resolveVernacularNamesLike(TnrMsg tnrMsg) throws DRFChecklistException {
|
300
|
|
301
|
Query query = singleQueryFrom(tnrMsg);
|
302
|
String name = query.getRequest().getQueryString();
|
303
|
AphiaNameServicePortType aphianspt = getAphiaNameService();
|
304
|
|
305
|
try {
|
306
|
AphiaRecord[] records = aphianspt.getAphiaRecordsByVernacular(name, true, 1);
|
307
|
if(records != null){
|
308
|
for (AphiaRecord record : records) {
|
309
|
Response tnrResponse = tnrResponseFromRecord(aphianspt, record, query.getRequest());
|
310
|
query.getResponse().add(tnrResponse);
|
311
|
}
|
312
|
}
|
313
|
|
314
|
} catch (RemoteException e) {
|
315
|
logger.error("Error in getAphiaRecordsByVernacular() AphiaNameSerice", e);
|
316
|
throw new DRFChecklistException("Error in getGUID method in AphiaNameSerice", e);
|
317
|
}
|
318
|
|
319
|
}
|
320
|
|
321
|
@Override
|
322
|
public void findByIdentifier(TnrMsg tnrMsg) throws DRFChecklistException {
|
323
|
Query query = singleQueryFrom(tnrMsg);
|
324
|
String name = query.getRequest().getQueryString();
|
325
|
AphiaNameServicePortType aphianspt = getAphiaNameService();
|
326
|
|
327
|
try {
|
328
|
AphiaRecord record = null;
|
329
|
try {
|
330
|
record = aphianspt.getAphiaRecordByExtID(name, "lsid");
|
331
|
Response tnrResponse = tnrResponseFromRecord(aphianspt, record, query.getRequest());
|
332
|
query.getResponse().add(tnrResponse);
|
333
|
} catch(NullPointerException npe) {
|
334
|
//FIXME : Workaround for NPE thrown by the aphia stub due to a,
|
335
|
// null aphia id (Integer), when the name is not present
|
336
|
// in the db
|
337
|
}
|
338
|
} catch (RemoteException e) {
|
339
|
logger.error("Error in autogenerated getGUID method", e);
|
340
|
throw new DRFChecklistException("Error in getGUID method", e);
|
341
|
}
|
342
|
|
343
|
}
|
344
|
|
345
|
@Override
|
346
|
public boolean isSupportedIdentifier(String value) {
|
347
|
return IdentifierUtils.checkLSID(value);
|
348
|
}
|
349
|
}
|
350
|
|
351
|
|