1
|
/**
|
2
|
* Copyright (C) 2013 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
package eu.etaxonomy.cdm.ext.occurrence.bioCase;
|
10
|
|
11
|
import java.io.IOException;
|
12
|
import java.io.InputStream;
|
13
|
import java.util.ArrayList;
|
14
|
import java.util.HashMap;
|
15
|
import java.util.List;
|
16
|
import java.util.Map;
|
17
|
|
18
|
import javax.xml.parsers.DocumentBuilder;
|
19
|
import javax.xml.parsers.DocumentBuilderFactory;
|
20
|
import javax.xml.parsers.ParserConfigurationException;
|
21
|
|
22
|
import org.apache.http.NameValuePair;
|
23
|
import org.apache.http.client.ClientProtocolException;
|
24
|
import org.apache.http.client.entity.UrlEncodedFormEntity;
|
25
|
import org.apache.http.message.BasicNameValuePair;
|
26
|
import org.jdom.Document;
|
27
|
import org.jdom.output.Format;
|
28
|
import org.jdom.output.XMLOutputter;
|
29
|
import org.springframework.stereotype.Service;
|
30
|
import org.springframework.transaction.annotation.Transactional;
|
31
|
import org.w3c.dom.Element;
|
32
|
import org.w3c.dom.Node;
|
33
|
import org.w3c.dom.NodeList;
|
34
|
import org.xml.sax.SAXException;
|
35
|
|
36
|
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
|
37
|
import eu.etaxonomy.cdm.common.URI;
|
38
|
import eu.etaxonomy.cdm.common.UriUtils;
|
39
|
import eu.etaxonomy.cdm.ext.common.ServiceWrapperBase;
|
40
|
import eu.etaxonomy.cdm.ext.occurrence.OccurenceQuery;
|
41
|
import eu.etaxonomy.cdm.model.location.NamedArea;
|
42
|
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
|
43
|
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
|
44
|
|
45
|
/**
|
46
|
* This service provides access to BioCASe providers.<br>
|
47
|
* It sends a {@link OccurenceQuery} via HTTP POST to a given provider
|
48
|
* @author pplitzner
|
49
|
* @since 13.09.2013
|
50
|
*/
|
51
|
@Service
|
52
|
@Transactional(readOnly = true)
|
53
|
public class BioCaseQueryServiceWrapper extends ServiceWrapperBase<SpecimenOrObservationBase<?>>{
|
54
|
|
55
|
private static final BasicNameValuePair SUBMIT_PARAM = new BasicNameValuePair("Submit", "Submit");
|
56
|
private static final String QUERY_PARAM_NAME = "query";
|
57
|
private static final BasicNameValuePair CAPABILITY_TEST_PARAM = new BasicNameValuePair("capabilities", "1");
|
58
|
|
59
|
/**
|
60
|
* Queries the BioCASE provider at the given endPoint with the given {@link OccurenceQuery}.<br>
|
61
|
* @param query the query object which stores all the query parameters
|
62
|
* @param endPoint If <code>null</code> then the default endPoint is used
|
63
|
* (<i>http://ww3.bgbm.org/biocase/pywrapper.cgi?dsa=Herbar</i>)
|
64
|
* @return The response as an {@link InputStream}
|
65
|
*/
|
66
|
public InputStream query(OccurenceQuery query, URI endPoint) throws ClientProtocolException, IOException{
|
67
|
if(endPoint==null){
|
68
|
logger.warn("URI for querying was null.");
|
69
|
return null;
|
70
|
}
|
71
|
if(UriUtils.isServiceAvailable(endPoint, 10000)){
|
72
|
//check capabilities for ABCD version
|
73
|
Map<String, String> requestHeaders = new HashMap<String, String>();
|
74
|
requestHeaders.put(CAPABILITY_TEST_PARAM.getName(), CAPABILITY_TEST_PARAM.getValue());
|
75
|
InputStream response = executeHttpGet(endPoint, requestHeaders);
|
76
|
String abcdSchema = parseAbcdSchemaVersion(response);
|
77
|
|
78
|
//build XML query
|
79
|
Document doc = BioCaseQueryGenerator.generateXMLQuery(query, abcdSchema);
|
80
|
String xmlOutputString = new XMLOutputter(Format.getPrettyFormat()).outputString(doc);
|
81
|
|
82
|
//POST parameters
|
83
|
List<NameValuePair> queryParamsPOST = new ArrayList<NameValuePair>();
|
84
|
queryParamsPOST.add(SUBMIT_PARAM);
|
85
|
addNameValuePairTo(queryParamsPOST, QUERY_PARAM_NAME, xmlOutputString);
|
86
|
UrlEncodedFormEntity httpEntity = new UrlEncodedFormEntity(queryParamsPOST);
|
87
|
|
88
|
//Query provider
|
89
|
logger.debug("Querying BioCASE service with " + endPoint + ", POST: " + queryParamsPOST);
|
90
|
//FIXME do the ABCD import here
|
91
|
return executeHttpPost(endPoint, null, httpEntity);
|
92
|
}
|
93
|
else{
|
94
|
logger.error("Querying " + endPoint + " got a timeout!");
|
95
|
return null;
|
96
|
}
|
97
|
}
|
98
|
|
99
|
public String parseAbcdSchemaVersion(InputStream inputStream){
|
100
|
String errorMessage = "Unable to parse ABCD version.";
|
101
|
String schemaVersion = null;
|
102
|
|
103
|
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
|
104
|
DocumentBuilder builder;
|
105
|
try {
|
106
|
builder = factory.newDocumentBuilder();
|
107
|
org.w3c.dom.Document document = builder.parse(inputStream);
|
108
|
Element root = document.getDocumentElement();
|
109
|
NodeList supportedSchemasList = root.getElementsByTagName("biocase:SupportedSchemas");
|
110
|
if(supportedSchemasList!=null){
|
111
|
for(int i=0;i<supportedSchemasList.getLength();i++){
|
112
|
Node supportedSchema = supportedSchemasList.item(i);
|
113
|
if(supportedSchema instanceof Element){
|
114
|
String temp = ((Element) supportedSchema).getAttribute("namespace");
|
115
|
if (temp.contains("abcd")){
|
116
|
schemaVersion = temp;
|
117
|
}
|
118
|
}
|
119
|
}
|
120
|
}
|
121
|
} catch (ParserConfigurationException e) {
|
122
|
logger.error(errorMessage, e);
|
123
|
} catch (SAXException e) {
|
124
|
logger.error(errorMessage, e);
|
125
|
} catch (IOException e) {
|
126
|
logger.error(errorMessage, e);
|
127
|
}
|
128
|
|
129
|
return schemaVersion;
|
130
|
}
|
131
|
|
132
|
public List<SpecimenOrObservationBase<?>> dummyData(){
|
133
|
List<SpecimenOrObservationBase<?>> results = new ArrayList<SpecimenOrObservationBase<?>>();
|
134
|
DerivedUnitFacade unit1 = DerivedUnitFacade.NewInstance(SpecimenOrObservationType.PreservedSpecimen);
|
135
|
// newInstance.setTitleCache("Test Specimen", true);
|
136
|
unit1.setCollectingMethod("Collected from the ground");
|
137
|
unit1.setAccessionNumber("ACC-12345");
|
138
|
unit1.setLocality("locality");
|
139
|
unit1.setCountry(NamedArea.EUROPE());
|
140
|
|
141
|
DerivedUnitFacade unit2 = DerivedUnitFacade.NewInstance(SpecimenOrObservationType.PreservedSpecimen);
|
142
|
unit1.setTitleCache("Test Specimen 2", false);
|
143
|
unit2.setCollectingMethod("Collected from the ground");
|
144
|
unit2.setAccessionNumber("ACC-67890");
|
145
|
unit2.setLocality("solid ground close to car park");
|
146
|
unit2.setCountry(NamedArea.EUROPE());
|
147
|
results.add(unit1.innerDerivedUnit());
|
148
|
results.add(unit2.innerDerivedUnit());
|
149
|
return results;
|
150
|
}
|
151
|
|
152
|
/**
|
153
|
* @param query
|
154
|
* @param endPoint
|
155
|
* @return
|
156
|
*/
|
157
|
public InputStream querySiblings(OccurenceQuery query, URI endPoint) throws ClientProtocolException, IOException{
|
158
|
if(endPoint==null){
|
159
|
logger.warn("URI for querying was null.");
|
160
|
return null;
|
161
|
}
|
162
|
if(UriUtils.isServiceAvailable(endPoint, 10000)){
|
163
|
//check capabilities for ABCD version
|
164
|
Map<String, String> requestHeaders = new HashMap<String, String>();
|
165
|
requestHeaders.put(CAPABILITY_TEST_PARAM.getName(), CAPABILITY_TEST_PARAM.getValue());
|
166
|
InputStream response = executeHttpGet(endPoint, requestHeaders);
|
167
|
String abcdSchema = parseAbcdSchemaVersion(response);
|
168
|
|
169
|
//build XML query
|
170
|
Document doc = BioCaseQueryGenerator.generateXMLQueryForSiblings(query, abcdSchema);
|
171
|
String xmlOutputString = new XMLOutputter(Format.getPrettyFormat()).outputString(doc);
|
172
|
|
173
|
//POST parameters
|
174
|
List<NameValuePair> queryParamsPOST = new ArrayList<NameValuePair>();
|
175
|
queryParamsPOST.add(SUBMIT_PARAM);
|
176
|
addNameValuePairTo(queryParamsPOST, QUERY_PARAM_NAME, xmlOutputString);
|
177
|
UrlEncodedFormEntity httpEntity = new UrlEncodedFormEntity(queryParamsPOST);
|
178
|
|
179
|
//Query provider
|
180
|
logger.debug("Querying BioCASE service with " + endPoint + ", POST: " + queryParamsPOST);
|
181
|
//FIXME do the ABCD import here
|
182
|
return executeHttpPost(endPoint, null, httpEntity);
|
183
|
}
|
184
|
else{
|
185
|
logger.error("Querying " + endPoint + " got a timeout!");
|
186
|
return null;
|
187
|
}
|
188
|
}
|
189
|
}
|