1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2007 EDIT
|
4
|
* European Distributed Institute of Taxonomy
|
5
|
* http://www.e-taxonomy.eu
|
6
|
*
|
7
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
8
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
9
|
*/
|
10
|
|
11
|
package eu.etaxonomy.cdm.print;
|
12
|
|
13
|
import java.io.BufferedReader;
|
14
|
import java.io.IOException;
|
15
|
import java.io.InputStream;
|
16
|
import java.io.InputStreamReader;
|
17
|
import java.net.URI;
|
18
|
import java.net.URISyntaxException;
|
19
|
import java.net.URL;
|
20
|
import java.util.ArrayList;
|
21
|
import java.util.Arrays;
|
22
|
import java.util.HashMap;
|
23
|
import java.util.List;
|
24
|
import java.util.Map;
|
25
|
import java.util.UUID;
|
26
|
|
27
|
import org.apache.http.HttpResponse;
|
28
|
import org.apache.http.NameValuePair;
|
29
|
import org.apache.http.message.BasicNameValuePair;
|
30
|
import org.apache.log4j.Logger;
|
31
|
import org.jdom.Document;
|
32
|
import org.jdom.Element;
|
33
|
import org.jdom.JDOMException;
|
34
|
import org.jdom.xpath.XPath;
|
35
|
import eu.etaxonomy.cdm.common.UriUtils;
|
36
|
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor;
|
37
|
import eu.etaxonomy.cdm.print.XMLHelper.EntityType;
|
38
|
|
39
|
/**
|
40
|
* Implementation of an IXMLEntityFactory that is connected to a CDM Community
|
41
|
* Server on a remote machine. API call will be executed by accessing the
|
42
|
* servers REST API.
|
43
|
*
|
44
|
* @author n.hoffmann
|
45
|
* @author l.morris
|
46
|
* @created Apr 6, 2010
|
47
|
* @version 1.0
|
48
|
*/
|
49
|
public class RemoteXMLEntityFactory extends XmlEntityFactoryBase {
|
50
|
private static final Logger logger = Logger
|
51
|
.getLogger(RemoteXMLEntityFactory.class);
|
52
|
|
53
|
private URL serviceUrl;
|
54
|
|
55
|
private IProgressMonitor monitor;
|
56
|
|
57
|
private static final List<NameValuePair> UNLIMITED_RESULTS = Arrays
|
58
|
.asList(new NameValuePair[] { new BasicNameValuePair("start", "0"),
|
59
|
new BasicNameValuePair("limit", "-1") });
|
60
|
|
61
|
private static final String UUID = "{uuid}";
|
62
|
|
63
|
private static final String CLASSIFICATIONS = "classification";
|
64
|
private static final String CLASSIFICATION_CHILD_NODES = "classification/"
|
65
|
+ UUID + "/childNodes/";
|
66
|
private static final String TAXONNODE_CHILD_NODES = "taxonNode/" + UUID
|
67
|
+ "/childNodes/";
|
68
|
private static final String TAXONNODE = "taxonNode/" + UUID;
|
69
|
private static final String TAXONNODE_TAXON = TAXONNODE + "/taxon";
|
70
|
// private static final String TAXONNODES = TAXONNODE + "/taxon";
|
71
|
private static final String TAXA_AND_NAMES = "taxon/findTaxaAndNames";
|
72
|
private static final String TAXONNODES = "taxon/" + UUID + "/taxonNodes";
|
73
|
// http://dev.e-taxonomy.eu/cdmserver/palmae/portal/taxon/d58c0b44-29f8-4071-aa49-32baa185296f/taxonNodes
|
74
|
|
75
|
private static final String FEATURETREES = "featureTree";
|
76
|
private static final String FEATURETREE = "featureTree/" + UUID;
|
77
|
private static final String FEATURENODE = "featurenode/" + UUID;
|
78
|
// private static final String FEATURENODE = "featureNode/" + UUID +
|
79
|
// "/childNodes";
|
80
|
private static final String FEATURENODE_FEATURE = FEATURENODE + "/feature";
|
81
|
|
82
|
private static final String NAME_TYPE_DESIGNATIONS = "name/" + UUID
|
83
|
+ "/typeDesignations";
|
84
|
|
85
|
// TAXON_ACCEPTED should populate references but authorship is not always
|
86
|
// populated so call the reference controller directly
|
87
|
private static final String REFERENCES = "portal/reference/" + UUID;
|
88
|
|
89
|
private static final String TAXON_ACCEPTED = "portal/taxon/" + UUID;
|
90
|
private static final String TAXON_SYNONYMY = "portal/taxon/" + UUID
|
91
|
+ "/synonymy";
|
92
|
private static final String TAXON_DESCRIPTIONS = "portal/taxon/" + UUID
|
93
|
+ "/descriptions";
|
94
|
|
95
|
private static final String POLYTOMOUS_KEY = "dto/polytomousKey/linkedStyle?findByTaxonomicScope="
|
96
|
+ UUID;
|
97
|
// dto/polytomousKey/linkedStyle.json?findByTaxonomicScope=f820f533-06f2-4116-87e9-c9319c0c1cbf
|
98
|
|
99
|
// images /portal/taxon/{uuid}
|
100
|
private static final String TAXON_MEDIA = "portal/taxon/" + UUID + "/media";
|
101
|
|
102
|
// http://test.e-taxonomy.eu/cdmserver/palmae/portal/media/bb49e8f0-49bc-41f7-9674-1cb36eb716fb
|
103
|
|
104
|
/**
|
105
|
* Creates new instance of this factory and connects it to the given CDM
|
106
|
* Community Stores access point.
|
107
|
*
|
108
|
* Typically, there is no need to instantiate this class.
|
109
|
*
|
110
|
* @param monitor
|
111
|
*/
|
112
|
protected RemoteXMLEntityFactory(URL webserviceUrl, IProgressMonitor monitor) {
|
113
|
this.serviceUrl = webserviceUrl;
|
114
|
this.monitor = monitor;
|
115
|
}
|
116
|
|
117
|
/*
|
118
|
* (non-Javadoc)
|
119
|
*
|
120
|
* @see eu.etaxonomy.printpublisher.IXMLEntityFactory#getClassifications()
|
121
|
*/
|
122
|
public List<Element> getClassifications() {
|
123
|
Element result = queryServiceWithParameters(CLASSIFICATIONS,
|
124
|
UNLIMITED_RESULTS);
|
125
|
return processElementList(result);
|
126
|
}
|
127
|
|
128
|
/*
|
129
|
* (non-Javadoc)
|
130
|
*
|
131
|
* @see
|
132
|
* eu.etaxonomy.printpublisher.IXMLEntityFactory#getChildNodes(org.jdom.
|
133
|
* Element)
|
134
|
*/
|
135
|
public List<Element> getChildNodes(Element treeNode) {
|
136
|
EntityType entityType = XMLHelper.getEntityType(treeNode);
|
137
|
|
138
|
Element result = null;
|
139
|
|
140
|
if (EntityType.CLASSIFICATION.equals(entityType)) {
|
141
|
result = queryService(treeNode, CLASSIFICATION_CHILD_NODES);
|
142
|
} else if (EntityType.TAXON_NODE.equals(entityType)) {
|
143
|
result = queryService(treeNode, TAXONNODE_CHILD_NODES);
|
144
|
}
|
145
|
|
146
|
return processElementList(result);
|
147
|
}
|
148
|
|
149
|
/*
|
150
|
* (non-Javadoc)
|
151
|
*
|
152
|
* @see
|
153
|
* eu.etaxonomy.printpublisher.IXMLEntityFactory#getTaxonNode(java.util.
|
154
|
* UUID)
|
155
|
*/
|
156
|
public Element getTaxonNode(UUID taxonNodeUuid) {
|
157
|
return queryService(taxonNodeUuid, TAXONNODE);
|
158
|
}
|
159
|
|
160
|
/*
|
161
|
* Returns the taxonNode for a specific taxon name string.
|
162
|
*/
|
163
|
public Element getTaxonNodesByName(String taxonName, String classification) {
|
164
|
|
165
|
// 1-To find the uuid of a Taxon name:
|
166
|
// http://dev.e-taxonomy.eu/cdmserver/palmae/taxon/findTaxaAndNames.json?doTaxa=1&matchMode=EXACT&query=Acrocomia
|
167
|
// 2-Then get the taxonNodes for this name:
|
168
|
// http://dev.e-taxonomy.eu/cdmserver/palmae/portal/taxon/d58c0b44-29f8-4071-aa49-32baa185296f/taxonNodes
|
169
|
Element taxonNodes = null;
|
170
|
|
171
|
List <NameValuePair> params = new ArrayList<NameValuePair>();
|
172
|
params.add(new BasicNameValuePair("doTaxa", "1"));
|
173
|
params.add(new BasicNameValuePair("matchMode", "EXACT"));
|
174
|
params.add(new BasicNameValuePair("query", taxonName));
|
175
|
|
176
|
if (classification != null) {
|
177
|
List<Element> classifications = getClassifications();
|
178
|
String classificationUuid = "";
|
179
|
|
180
|
// String xPathString="//titleCache[.='" +
|
181
|
// classification+"']/../uuid";
|
182
|
|
183
|
for (Element element : classifications) {
|
184
|
if (element.getChild("titleCache").getValue()
|
185
|
.equals(classification)) {
|
186
|
System.out.println(element.getChild("uuid").getValue());
|
187
|
classificationUuid = element.getChild("uuid").getValue();
|
188
|
}
|
189
|
}
|
190
|
params.add(new BasicNameValuePair("tree", classificationUuid));
|
191
|
}
|
192
|
|
193
|
Element element = queryServiceWithParameters(TAXA_AND_NAMES, params);
|
194
|
|
195
|
Element taxonNodeString = null;
|
196
|
UUID taxonNodeUuid = null;
|
197
|
|
198
|
try {
|
199
|
|
200
|
// TODO: we only get the first Taxon to match this name - we should
|
201
|
// get all Taxons to match the name
|
202
|
Element taxonUuid = (Element) XPath.selectSingleNode(element,
|
203
|
"//records/e[1]");
|
204
|
|
205
|
taxonNodes = queryService(taxonUuid, TAXONNODES);
|
206
|
|
207
|
taxonNodeString = (Element) XPath.selectSingleNode(taxonNodes,
|
208
|
"//PersistentSet/e[1]/uuid");
|
209
|
String val = taxonNodeString.getValue();
|
210
|
taxonNodeUuid = java.util.UUID.fromString(taxonNodeString
|
211
|
.getValue());
|
212
|
|
213
|
} catch (JDOMException e) {
|
214
|
// TODO Auto-generated catch block
|
215
|
e.printStackTrace();
|
216
|
}
|
217
|
return getTaxonNode(taxonNodeUuid);
|
218
|
// TODO: Should be return a set of taxonNodes if there is more than one
|
219
|
// for the name
|
220
|
// public List<Element> getTaxonNodesByName(String taxonName) {
|
221
|
// return taxonNodes;
|
222
|
}
|
223
|
|
224
|
/*
|
225
|
* (non-Javadoc)
|
226
|
*
|
227
|
* @see
|
228
|
* eu.etaxonomy.printpublisher.IXMLEntityFactory#getFeatureTree(java.util
|
229
|
* .UUID)
|
230
|
*/
|
231
|
public Element getFeatureTree(UUID featureTreeUuid) {
|
232
|
return queryService(featureTreeUuid, FEATURETREE);
|
233
|
}
|
234
|
|
235
|
/*
|
236
|
* (non-Javadoc)
|
237
|
*
|
238
|
* @see eu.etaxonomy.printpublisher.IXMLEntityFactory#getFeatureTrees()
|
239
|
*/
|
240
|
public List<Element> getFeatureTrees() {
|
241
|
Element result = queryServiceWithParameters(FEATURETREES,
|
242
|
UNLIMITED_RESULTS);
|
243
|
return processElementList(result);
|
244
|
}
|
245
|
|
246
|
public Element getFeatureNode(UUID featureNodeUuid) {
|
247
|
Element result = queryService(featureNodeUuid, FEATURENODE);
|
248
|
return result;
|
249
|
}
|
250
|
|
251
|
public Element getFeatureForFeatureNode(UUID featureNodeUuid) {
|
252
|
Element result = queryService(featureNodeUuid, FEATURENODE_FEATURE);
|
253
|
return result;
|
254
|
}
|
255
|
|
256
|
/*
|
257
|
* (non-Javadoc)
|
258
|
*
|
259
|
* @see
|
260
|
* eu.etaxonomy.printpublisher.IXMLEntityFactory#getTaxonFromTaxonNode(org
|
261
|
* .jdom.Element)
|
262
|
*/
|
263
|
public Element getTaxonForTaxonNode(Element taxonNodeElement) {
|
264
|
return queryService(taxonNodeElement, TAXONNODE_TAXON);
|
265
|
}
|
266
|
|
267
|
/*
|
268
|
*
|
269
|
*/
|
270
|
/*
|
271
|
* public List<Element> getReferences(Element taxonElement) throws
|
272
|
* JDOMException {
|
273
|
*
|
274
|
* //get the references from the taxonElement String referencePattern =
|
275
|
* "//name/nomenclaturalReference";
|
276
|
*
|
277
|
* //but there could be many references Element referenceElement = (Element)
|
278
|
* XPath.selectSingleNode(taxonElement, referencePattern); //List<Element>
|
279
|
* descriptionElementElements = XPath.selectNodes(context, featurePattern +
|
280
|
* "/..");
|
281
|
*
|
282
|
* List<Element> elementList = null;
|
283
|
*
|
284
|
* if(referenceElement != null){ //the referencePattern was found in the
|
285
|
* taxonElement
|
286
|
*
|
287
|
* Element result = queryService(referenceElement, REFERENCES);
|
288
|
*
|
289
|
* elementList = new ArrayList<Element>();
|
290
|
*
|
291
|
* for(Object child : result.getChildren()){ if(child instanceof Element){
|
292
|
* Element childElement = (Element) ((Element)child).clone();
|
293
|
*
|
294
|
* childElement.detach();
|
295
|
*
|
296
|
* elementList.add(childElement); } } }
|
297
|
*
|
298
|
* return elementList; }
|
299
|
*/
|
300
|
|
301
|
public List<Element> getReferences(Element referenceElement) {
|
302
|
|
303
|
Element result = queryService(referenceElement, REFERENCES);
|
304
|
|
305
|
List<Element> elementList = new ArrayList<Element>();
|
306
|
|
307
|
for (Object child : result.getChildren()) {
|
308
|
if (child instanceof Element) {
|
309
|
Element childElement = (Element) ((Element) child).clone();
|
310
|
|
311
|
childElement.detach();
|
312
|
|
313
|
elementList.add(childElement);
|
314
|
}
|
315
|
}
|
316
|
|
317
|
return elementList;
|
318
|
}
|
319
|
|
320
|
/*
|
321
|
* (non-Javadoc)
|
322
|
*
|
323
|
* @see
|
324
|
* eu.etaxonomy.printpublisher.IXMLEntityFactory#getAcceptedTaxonElement
|
325
|
* (org.jdom.Element)
|
326
|
*/
|
327
|
public Element getAcceptedTaxonElement(Element taxonElement) {
|
328
|
Element result = queryService(taxonElement, TAXON_ACCEPTED);
|
329
|
return result;
|
330
|
}
|
331
|
|
332
|
/*
|
333
|
* (non-Javadoc)
|
334
|
*
|
335
|
* @see
|
336
|
* eu.etaxonomy.printpublisher.IXMLEntityFactory#getSynonymy(org.jdom.Element
|
337
|
* )
|
338
|
*/
|
339
|
public List<Element> getSynonymy(Element taxonElement) {
|
340
|
Element result = queryService(taxonElement, TAXON_SYNONYMY);
|
341
|
|
342
|
List<Element> elementList = new ArrayList<Element>();
|
343
|
|
344
|
for (Object child : result.getChildren()) {
|
345
|
if (child instanceof Element) {
|
346
|
Element childElement = (Element) ((Element) child).clone();
|
347
|
|
348
|
childElement.detach();
|
349
|
|
350
|
elementList.add(childElement);
|
351
|
}
|
352
|
}
|
353
|
|
354
|
return elementList;
|
355
|
}
|
356
|
|
357
|
/*
|
358
|
*
|
359
|
*/
|
360
|
public List<Element> getMedia(Element taxonElement) {
|
361
|
|
362
|
List<NameValuePair> params = Arrays
|
363
|
.asList(new NameValuePair[] {
|
364
|
new BasicNameValuePair("includeTaxonDescriptions",
|
365
|
"true"),
|
366
|
new BasicNameValuePair("includeOccurrences", "false"),
|
367
|
new BasicNameValuePair("includeTaxonNameDescriptions",
|
368
|
"false") });
|
369
|
|
370
|
UUID uuid = XMLHelper.getUuid(taxonElement);
|
371
|
|
372
|
Element result = queryServiceWithParameters(
|
373
|
TAXON_MEDIA.replace(UUID, uuid.toString()), params);
|
374
|
|
375
|
// Element result = queryService(taxonElement, TAXON_MEDIA);
|
376
|
|
377
|
List<Element> elementList = new ArrayList<Element>();
|
378
|
|
379
|
for (Object child : result.getChildren()) {
|
380
|
if (child instanceof Element) {
|
381
|
Element childElement = (Element) ((Element) child).clone();
|
382
|
|
383
|
childElement.detach();
|
384
|
|
385
|
elementList.add(childElement);
|
386
|
}
|
387
|
}
|
388
|
|
389
|
return elementList;
|
390
|
}
|
391
|
|
392
|
/*
|
393
|
* (non-Javadoc)
|
394
|
*
|
395
|
* @see
|
396
|
* eu.etaxonomy.printpublisher.IXMLEntityFactory#getTypeDesignations(org
|
397
|
* .jdom.Element)
|
398
|
*/
|
399
|
public List<Element> getTypeDesignations(Element nameElement) {
|
400
|
Element result = queryService(nameElement, NAME_TYPE_DESIGNATIONS);
|
401
|
return processElementList(result);
|
402
|
}
|
403
|
|
404
|
/*
|
405
|
* (non-Javadoc)
|
406
|
*
|
407
|
* @see
|
408
|
* eu.etaxonomy.printpublisher.IXMLEntityFactory#getDescriptions(org.jdom
|
409
|
* .Element)
|
410
|
*/
|
411
|
public Element getDescriptions(Element taxonElement) {
|
412
|
Element result = queryService(taxonElement, TAXON_DESCRIPTIONS);
|
413
|
return result;
|
414
|
}
|
415
|
|
416
|
/**
|
417
|
* Queries the service with the uuid of the given element
|
418
|
*
|
419
|
* @param element
|
420
|
* @param restRequest
|
421
|
* @return
|
422
|
*/
|
423
|
private Element queryService(Element element, String restRequest) {
|
424
|
UUID uuid = XMLHelper.getUuid(element);
|
425
|
return queryService(uuid, restRequest);
|
426
|
}
|
427
|
|
428
|
/**
|
429
|
* Queries the service with the given uuid
|
430
|
*
|
431
|
* @param uuid
|
432
|
* @param restRequest
|
433
|
* @return
|
434
|
*/
|
435
|
private Element queryService(UUID uuid, String restRequest) {
|
436
|
String request = restRequest.replace(UUID, uuid.toString());
|
437
|
return queryServiceWithParameters(request, null);
|
438
|
}
|
439
|
|
440
|
/**
|
441
|
*
|
442
|
* @param restRequest
|
443
|
* @return
|
444
|
* @throws URISyntaxException
|
445
|
*/
|
446
|
private Element queryServiceWithParameters(String restRequest,
|
447
|
List<NameValuePair> queryParameters) {
|
448
|
|
449
|
try {
|
450
|
URI newUri = UriUtils.createUri(serviceUrl, restRequest,
|
451
|
queryParameters, null);
|
452
|
|
453
|
Map<String, String> requestHeaders = new HashMap<String, String>();
|
454
|
requestHeaders.put("Accept", "application/xml");
|
455
|
// requestHeaders.put("Accept", "application/json");
|
456
|
requestHeaders.put("Accept-Charset", "UTF-8");
|
457
|
|
458
|
HttpResponse response = UriUtils
|
459
|
.getResponse(newUri, requestHeaders);
|
460
|
|
461
|
logger.info("Firing request for URI: " + newUri);
|
462
|
|
463
|
if (UriUtils.isOk(response)) {
|
464
|
|
465
|
// get the content at the resource
|
466
|
InputStream content = UriUtils.getContent(response);
|
467
|
|
468
|
// specify encoding in the reader
|
469
|
BufferedReader in = new BufferedReader(new InputStreamReader(
|
470
|
content, "UTF-8"));
|
471
|
|
472
|
// build the jdom document
|
473
|
Document document = builder.build(in);
|
474
|
// Document document = builder.build(content);
|
475
|
|
476
|
return document.getRootElement();
|
477
|
} else {
|
478
|
monitor.warning(UriUtils.getStatus(response));
|
479
|
logger.error(UriUtils.getStatus(response));
|
480
|
}
|
481
|
|
482
|
} catch (IOException e) {
|
483
|
monitor.warning("No content for request: " + restRequest, e);
|
484
|
logger.error("No content for request: " + restRequest);
|
485
|
} catch (JDOMException e) {
|
486
|
monitor.warning("Error building the document.", e);
|
487
|
logger.error("Error building the document.", e);
|
488
|
} catch (URISyntaxException e) {
|
489
|
monitor.warning("Error building URI", e);
|
490
|
logger.error("Error building URI", e);
|
491
|
}
|
492
|
|
493
|
// error
|
494
|
return null;
|
495
|
}
|
496
|
|
497
|
/**
|
498
|
* The access point of a CDM Community Server
|
499
|
*
|
500
|
* @return the serviceUrl
|
501
|
*/
|
502
|
public URL getServiceUrl() {
|
503
|
return serviceUrl;
|
504
|
}
|
505
|
|
506
|
/**
|
507
|
* The CDM Community Servers access point
|
508
|
*
|
509
|
* @param serviceUrl
|
510
|
*/
|
511
|
public void setServiceUrl(URL serviceUrl) {
|
512
|
this.serviceUrl = serviceUrl;
|
513
|
}
|
514
|
|
515
|
// dto/polytomousKey/linkedStyle.json?findByTaxonomicScope=f820f533-06f2-4116-87e9-c9319c0c1cbf
|
516
|
/*
|
517
|
* (non-Javadoc) TODO: Implement this method.
|
518
|
*
|
519
|
* @see
|
520
|
* eu.etaxonomy.cdm.print.IXMLEntityFactory#getPolytomousKey(org.jdom.Element
|
521
|
* )
|
522
|
*/
|
523
|
@Override
|
524
|
// public List<Element> getPolytomousKey(Element taxonElement) {
|
525
|
public Element getPolytomousKey(Element taxonElement) {
|
526
|
|
527
|
Element result = queryService(taxonElement, POLYTOMOUS_KEY);
|
528
|
return result;
|
529
|
// TODO Auto-generated method stub
|
530
|
// return null;
|
531
|
}
|
532
|
}
|