1
|
package org.bgbm.biovel.drf.checklist;
|
2
|
|
3
|
|
4
|
import java.net.URI;
|
5
|
import java.net.URISyntaxException;
|
6
|
import java.util.HashMap;
|
7
|
import java.util.Iterator;
|
8
|
import java.util.List;
|
9
|
import java.util.Map;
|
10
|
|
11
|
import org.apache.http.HttpHost;
|
12
|
import org.apache.http.client.utils.URIBuilder;
|
13
|
import org.bgbm.biovel.drf.checklist.BaseChecklistClient.ChecklistInfo;
|
14
|
import org.bgbm.biovel.drf.tnr.msg.AcceptedName;
|
15
|
import org.bgbm.biovel.drf.tnr.msg.NameType;
|
16
|
import org.bgbm.biovel.drf.tnr.msg.ScrutinyType;
|
17
|
import org.bgbm.biovel.drf.tnr.msg.SourceType;
|
18
|
import org.bgbm.biovel.drf.tnr.msg.TaxonNameType;
|
19
|
import org.bgbm.biovel.drf.tnr.msg.TnrMsg;
|
20
|
import org.bgbm.biovel.drf.tnr.msg.TnrMsg.Query;
|
21
|
import org.bgbm.biovel.drf.tnr.msg.TnrResponse;
|
22
|
import org.bgbm.biovel.drf.tnr.msg.TnrResponse.Synonym;
|
23
|
import org.bgbm.biovel.drf.utils.JSONUtils;
|
24
|
import org.bgbm.biovel.drf.utils.TnrMsgException;
|
25
|
import org.bgbm.biovel.drf.utils.TnrMsgUtils;
|
26
|
import org.json.simple.JSONArray;
|
27
|
import org.json.simple.JSONObject;
|
28
|
|
29
|
public class GBIFBackboneClient extends AggregateChecklistClient {
|
30
|
|
31
|
private static final String ID = "gbif";
|
32
|
private static final String LABEL = "GBIF Checklist Bank";
|
33
|
private static final String URL = "http://dev.gbif.org/wiki/display/POR/Webservice+API";
|
34
|
private static final String DATA_AGR_URL = "http://data.gbif.org/tutorial/datauseagreement";
|
35
|
public static final ChecklistInfo CINFO = new ChecklistInfo(ID,LABEL,URL,DATA_AGR_URL);
|
36
|
|
37
|
public GBIFBackboneClient(List<String> checklistKeys) {
|
38
|
super();
|
39
|
}
|
40
|
|
41
|
@Override
|
42
|
public HttpHost getHost() {
|
43
|
// TODO Auto-generated method stub
|
44
|
return new HttpHost("api.gbif.org",80);
|
45
|
}
|
46
|
|
47
|
|
48
|
@Override
|
49
|
protected ChecklistInfo buildChecklistInfo() {
|
50
|
ChecklistInfo checklistInfo = CINFO;
|
51
|
int offset = 0;
|
52
|
|
53
|
URIBuilder uriBuilder = new URIBuilder();
|
54
|
uriBuilder.setScheme("http");
|
55
|
uriBuilder.setHost(getHost().getHostName());
|
56
|
uriBuilder.setPath("/dataset/search");
|
57
|
uriBuilder.setParameter("type", "CHECKLIST");
|
58
|
uriBuilder.setParameter("limit", "20");
|
59
|
uriBuilder.setParameter("offset", "0");
|
60
|
URI uri;
|
61
|
boolean endOfRecords = false;
|
62
|
try {
|
63
|
do {
|
64
|
uriBuilder.setParameter("offset", Integer.toString(offset));
|
65
|
uri = uriBuilder.build();
|
66
|
System.out.println("buildChecklistMap");
|
67
|
String response = processRESTService(uri);
|
68
|
|
69
|
JSONObject jsonResponse = (JSONObject) JSONUtils.parseJsonToObject(response);
|
70
|
JSONArray results = (JSONArray) jsonResponse.get("results");
|
71
|
Iterator<JSONObject> itrResults = results.iterator();
|
72
|
while(itrResults.hasNext()) {
|
73
|
JSONObject result = itrResults.next();
|
74
|
String key = (String)result.get("key");
|
75
|
String title = (String)result.get("title");
|
76
|
String url = "http://api.gbif.org/dataset/" + key;
|
77
|
checklistInfo.addSubChecklist(new ChecklistInfo(key, title, url));
|
78
|
}
|
79
|
|
80
|
endOfRecords = (Boolean) jsonResponse.get("endOfRecords");
|
81
|
|
82
|
offset = offset + 20;
|
83
|
} while(!endOfRecords);
|
84
|
} catch (URISyntaxException e) {
|
85
|
// TODO Auto-generated catch block
|
86
|
e.printStackTrace();
|
87
|
} catch (DRFChecklistException e) {
|
88
|
// TODO Auto-generated catch block
|
89
|
e.printStackTrace();
|
90
|
}
|
91
|
|
92
|
return checklistInfo;
|
93
|
}
|
94
|
|
95
|
|
96
|
|
97
|
@Override
|
98
|
public void resolveNames(TnrMsg tnrMsg) throws DRFChecklistException {
|
99
|
List<TnrMsg.Query> queryList = tnrMsg.getQuery();
|
100
|
if(queryList.size() == 0) {
|
101
|
throw new DRFChecklistException("GBIF query list is empty");
|
102
|
}
|
103
|
|
104
|
if(queryList.size() > 1) {
|
105
|
throw new DRFChecklistException("GBIF query list has more than one query");
|
106
|
}
|
107
|
Query query = queryList.get(0);
|
108
|
Iterator<ChecklistInfo> itrKeys = getChecklistInfo().getSubChecklists().iterator();
|
109
|
//http://api.gbif.org/name_usage?q=Abies%20alba&datasetKey=fab88965-e69d-4491-a04d-e3198b626e52
|
110
|
while(itrKeys.hasNext()) {
|
111
|
ChecklistInfo checklistInfo = itrKeys.next();
|
112
|
Map<String, String> paramMap = new HashMap<String, String>();
|
113
|
paramMap.put("datasetKey", checklistInfo.getId());
|
114
|
|
115
|
URI namesUri = buildUriFromQuery(query, "/name_usage/" + QUERY_PLACEHOLDER, paramMap);
|
116
|
|
117
|
String response = processRESTService(namesUri);
|
118
|
|
119
|
updateQueryWithResponse(query,response, paramMap, checklistInfo);
|
120
|
|
121
|
try {
|
122
|
System.out.println(TnrMsgUtils.convertTnrMsgToXML(tnrMsg));
|
123
|
} catch (TnrMsgException e) {
|
124
|
// TODO Auto-generated catch block
|
125
|
e.printStackTrace();
|
126
|
}
|
127
|
}
|
128
|
}
|
129
|
|
130
|
@Override
|
131
|
public int getMaxPageSize() {
|
132
|
return 10;
|
133
|
}
|
134
|
|
135
|
private void updateQueryWithResponse(Query query ,
|
136
|
String response,
|
137
|
Map<String, String> paramMap,
|
138
|
ChecklistInfo ci) throws DRFChecklistException {
|
139
|
|
140
|
JSONArray jsonArray = (JSONArray ) JSONUtils.parseJsonToArray(response);
|
141
|
|
142
|
if(jsonArray != null) {
|
143
|
String accTaxonId = "";
|
144
|
Iterator<JSONObject> resIterator = jsonArray.iterator();
|
145
|
while (resIterator.hasNext()) {
|
146
|
JSONObject res = resIterator.next();
|
147
|
Number acceptedKey = (Number)res.get("acceptedKey");
|
148
|
boolean synonym = (Boolean)res.get("synonym");
|
149
|
// case when accepted name
|
150
|
if(!synonym && (acceptedKey == null)) {
|
151
|
Long key = (Long)res.get("key");
|
152
|
accTaxonId = key.toString();
|
153
|
|
154
|
}
|
155
|
// case when synonym
|
156
|
if(synonym && (acceptedKey != null)) {
|
157
|
Long key = (Long)res.get("acceptedKey");
|
158
|
accTaxonId = key.toString();
|
159
|
}
|
160
|
|
161
|
TnrResponse tnrResponse = new TnrResponse();
|
162
|
|
163
|
tnrResponse.setChecklist(ci.getLabel());
|
164
|
tnrResponse.setChecklistUrl(ci.getUrl());
|
165
|
|
166
|
URI taxonUri = buildUriFromQuery(query, "/name_usage/" + accTaxonId, paramMap);
|
167
|
String taxonResponse = processRESTService(taxonUri);
|
168
|
|
169
|
JSONObject taxon = (JSONObject) JSONUtils.parseJsonToObject(taxonResponse);
|
170
|
AcceptedName accName = generateAccName(taxon);
|
171
|
tnrResponse.setAcceptedName(accName);
|
172
|
if(query != null) {
|
173
|
query.getTnrResponse().add(tnrResponse);
|
174
|
}
|
175
|
int offset = 0;
|
176
|
paramMap.put("limit", "20");
|
177
|
|
178
|
boolean endOfRecords = false;
|
179
|
|
180
|
do {
|
181
|
paramMap.put("offset", Integer.toString(offset));
|
182
|
|
183
|
URI synonymsUri = buildUriFromQuery(query, "/name_usage/" + accTaxonId + "/synonyms", paramMap);
|
184
|
String synResponse = processRESTService(synonymsUri);
|
185
|
|
186
|
JSONObject pagedSynonyms = (JSONObject) JSONUtils.parseJsonToObject(synResponse);
|
187
|
generateSynonyms(pagedSynonyms, tnrResponse);
|
188
|
|
189
|
endOfRecords = (Boolean) pagedSynonyms.get("endOfRecords");
|
190
|
|
191
|
offset = offset + 20;
|
192
|
} while(!endOfRecords);
|
193
|
}
|
194
|
}
|
195
|
}
|
196
|
|
197
|
private AcceptedName generateAccName(JSONObject taxon) {
|
198
|
AcceptedName accName = new AcceptedName();
|
199
|
TaxonNameType taxonName = new TaxonNameType();
|
200
|
NameType name = new NameType();
|
201
|
|
202
|
String resName = (String) taxon.get("scientificName");
|
203
|
name.setNameComplete(resName);
|
204
|
|
205
|
name.setNameCanonical((String) taxon.get("canonicalName"));
|
206
|
name.setNameStatus((String)taxon.get("taxonomicStatus"));
|
207
|
|
208
|
taxonName.setRank((String) taxon.get("rank"));
|
209
|
taxonName.setName(name);
|
210
|
|
211
|
accName.setTaxonName(taxonName);
|
212
|
|
213
|
//FIXME : To fill in
|
214
|
String sourceUrl = "";
|
215
|
String sourceDatasetID = "";
|
216
|
String sourceDatasetName = "";
|
217
|
String sourceName = "";
|
218
|
|
219
|
SourceType source = new SourceType();
|
220
|
source.setDatasetID(sourceDatasetID);
|
221
|
source.setDatasetName(sourceDatasetName);
|
222
|
source.setName(sourceName);
|
223
|
source.setUrl(sourceUrl);
|
224
|
accName.setSource(source);
|
225
|
|
226
|
//FIXME : To fill in
|
227
|
String accordingTo = "";
|
228
|
String modified = "";
|
229
|
|
230
|
ScrutinyType scrutiny = new ScrutinyType();
|
231
|
scrutiny.setAccordingTo(accordingTo);
|
232
|
scrutiny.setModified(modified);
|
233
|
accName.setScrutiny(scrutiny);
|
234
|
|
235
|
AcceptedName.Classification c = new AcceptedName.Classification();
|
236
|
c.setKingdom((String) taxon.get("kingdom"));
|
237
|
c.setPhylum((String) taxon.get("phylum"));
|
238
|
c.setClazz((String) taxon.get("clazz"));
|
239
|
c.setOrder((String) taxon.get("order"));
|
240
|
c.setFamily((String) taxon.get("family"));
|
241
|
c.setGenus((String) taxon.get("genus"));
|
242
|
accName.setClassification(c);
|
243
|
|
244
|
return accName;
|
245
|
}
|
246
|
|
247
|
private void generateSynonyms(JSONObject pagedSynonyms, TnrResponse tnrResponse) {
|
248
|
TnrResponse.Synonym synonym = new Synonym();
|
249
|
|
250
|
JSONArray synonyms = (JSONArray)pagedSynonyms.get("results");
|
251
|
Iterator<JSONObject> itrSynonyms = synonyms.iterator();
|
252
|
while(itrSynonyms.hasNext()) {
|
253
|
JSONObject synonymjs = (JSONObject) itrSynonyms.next();
|
254
|
TaxonNameType taxonName = new TaxonNameType();
|
255
|
NameType name = new NameType();
|
256
|
|
257
|
String resName = (String) synonymjs.get("scientificName");
|
258
|
name.setNameComplete(resName);
|
259
|
|
260
|
name.setNameCanonical((String) synonymjs.get("canonicalName"));
|
261
|
name.setNameStatus((String)synonymjs.get("taxonomicStatus"));
|
262
|
|
263
|
taxonName.setRank((String) synonymjs.get("rank"));
|
264
|
taxonName.setName(name);
|
265
|
|
266
|
synonym.setTaxonName(taxonName);
|
267
|
|
268
|
//FIXME : To fill in
|
269
|
String sourceUrl = "";
|
270
|
String sourceDatasetID = "";
|
271
|
String sourceDatasetName = "";
|
272
|
String sourceName = "";
|
273
|
|
274
|
SourceType source = new SourceType();
|
275
|
source.setDatasetID(sourceDatasetID);
|
276
|
source.setDatasetName(sourceDatasetName);
|
277
|
source.setName(sourceName);
|
278
|
source.setUrl(sourceUrl);
|
279
|
synonym.setSource(source);
|
280
|
|
281
|
//FIXME : To fill in
|
282
|
String accordingTo = "";
|
283
|
String modified = "";
|
284
|
|
285
|
ScrutinyType scrutiny = new ScrutinyType();
|
286
|
scrutiny.setAccordingTo(accordingTo);
|
287
|
scrutiny.setModified(modified);
|
288
|
synonym.setScrutiny(scrutiny);
|
289
|
|
290
|
tnrResponse.getSynonym().add(synonym);
|
291
|
}
|
292
|
}
|
293
|
|
294
|
|
295
|
|
296
|
|
297
|
}
|