2 * Copyright (C) 2013 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
9 package eu
.etaxonomy
.cdm
.ext
.occurrence
.gbif
;
11 import java
.io
.IOException
;
12 import java
.net
.URISyntaxException
;
13 import java
.util
.Calendar
;
14 import java
.util
.Collection
;
15 import java
.util
.List
;
17 import org
.apache
.http
.NameValuePair
;
18 import org
.apache
.http
.client
.ClientProtocolException
;
19 import org
.apache
.http
.client
.utils
.URIBuilder
;
21 import eu
.etaxonomy
.cdm
.common
.URI
;
22 import eu
.etaxonomy
.cdm
.common
.UriUtils
;
23 import eu
.etaxonomy
.cdm
.ext
.common
.ServiceWrapperBase
;
24 import eu
.etaxonomy
.cdm
.ext
.occurrence
.OccurenceQuery
;
25 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
28 * This service provides access to GBIF web service API.<br>
29 * It sends a {@link GbifQuery} via HTTP GET
33 public class GbifQueryServiceWrapper
extends ServiceWrapperBase
<SpecimenOrObservationBase
<?
>>{
35 protected static final String BASE_URL
= "https://api.gbif.org";
36 private static final String SUB_PATH
= "/v1/occurrence/search";
40 * Constructs a new GbifQueryServiceWrapper
42 public GbifQueryServiceWrapper() {
47 * Queries the GBIF API with the given {@link OccurenceQuery}.
48 * @return The response as a collection of {@link GbifResponse}s or <code>null</code>
49 * if no connection could be established
51 public Collection
<GbifResponse
> query(OccurenceQuery query
) throws ClientProtocolException
, IOException
, URISyntaxException
{
52 //TODO: workaround for special case for "eventDate" which can have comma separated values
54 if(query
.dateFrom
!=null){
55 yearUri
= "&year="+query
.dateFrom
.get(Calendar
.YEAR
);
56 if(query
.dateTo
!=null){
57 yearUri
+= ","+query
.dateTo
.get(Calendar
.YEAR
);
59 //TODO we skip month range query because it only checks for the month range ignoring the year range
60 //TODO date is not supported by GBIF
62 List
<NameValuePair
> queryParamsGET
= new GbifQueryGenerator().generateQueryParams(query
);
63 if (queryParamsGET
!= null){
64 URI uri
= createUri(SUB_PATH
, queryParamsGET
);
65 URIBuilder builder
= new URIBuilder(uri
.toString()+yearUri
);
67 if(UriUtils
.isServiceAvailable(uri
, 10000)){
68 logger
.info("Querying GBIF service with " + builder
.build());
69 return GbifJsonOccurrenceParser
.parseJsonRecords(executeHttpGet(new URI(builder
.build()), null));
72 logger
.error("Querying " + uri
+ " got a timeout!");
76 logger
.info("Querying GBIF service was skipped because of missing get parameters.");
82 * Queries GBIF for the original data set<br>
83 * @param gbifResponse the GbifResponse holds the link to the dataset webservice
84 * @return a {@link DataSetResponse} holding all relevant information to query the original provider
86 * @throws ClientProtocolException
88 public DataSetResponse
queryOriginalDataSet(GbifResponse gbifResponse
) throws ClientProtocolException
, IOException
{
89 //FIXME move ABCD import here and change return type to DerivedUnitFacade/SpecimenOrObservationBase
90 GbifDataSetProtocol dataSetProtocol
= gbifResponse
.getDataSetProtocol();
91 if(dataSetProtocol
== GbifDataSetProtocol
.BIOCASE
){
92 DataSetResponse response
= GbifJsonOccurrenceParser
.parseOriginalDataSetUri(executeHttpGet(gbifResponse
.getDataSetUri(), null));
93 //the unitID is delivered in the "catalogNumber" parameter which is set as the accessionNumber of the facade
94 response
.setUnitId(gbifResponse
.getDerivedUnitFacade().getAccessionNumber());
97 DataSetResponse response
= GbifJsonOccurrenceParser
.parseOriginalDataSetUri(executeHttpGet(gbifResponse
.getDataSetUri(), null));
98 //the unitID is delivered in the "catalogNumber" parameter which is set as the accessionNumber of the facade
99 response
.setUnitId(gbifResponse
.getDerivedUnitFacade().getAccessionNumber());