Project

General

Profile

Download (3.84 KB) Statistics
| Branch: | Tag: | Revision:
1
// $Id$
2
/**
3
* Copyright (C) 2013 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
package eu.etaxonomy.cdm.ext.occurrence.gbif;
11

    
12
import java.io.IOException;
13
import java.net.URI;
14
import java.net.URISyntaxException;
15
import java.util.Calendar;
16
import java.util.Collection;
17
import java.util.List;
18

    
19
import org.apache.http.NameValuePair;
20
import org.apache.http.client.ClientProtocolException;
21
import org.apache.http.client.utils.URIBuilder;
22

    
23
import eu.etaxonomy.cdm.common.UriUtils;
24
import eu.etaxonomy.cdm.ext.common.ServiceWrapperBase;
25
import eu.etaxonomy.cdm.ext.occurrence.OccurenceQuery;
26
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
27

    
28
/**
29
 * This service provides access to GBIF web service API.<br>
30
 * It sends a {@link GbifQuery} via HTTP GET
31
 * @author pplitzner
32
 * @date 13.09.2013
33
 *
34
 */
35
public class GbifQueryServiceWrapper extends ServiceWrapperBase<SpecimenOrObservationBase<?>>{
36

    
37
    protected static final String BASE_URL = "http://api.gbif.org";
38
    private static final String SUB_PATH = "/v1/occurrence/search";
39

    
40
    /**
41
     * Constructs a new GbifQueryServiceWrapper
42
     */
43
    public GbifQueryServiceWrapper() {
44
        setBaseUrl(BASE_URL);
45
    }
46

    
47
    /**
48
     * Queries the GBIF API with the given {@link OccurenceQuery}.
49
     * @return The response as a collection of {@link GbifResponse}s or <code>null</code>
50
     * if no connection could be established
51
     */
52
    public Collection<GbifResponse> query(OccurenceQuery query) throws ClientProtocolException, IOException, URISyntaxException{
53
        //TODO: workaround for special case for "eventDate" which can have comma separated values
54
        String yearUri = "";
55
        if(query.dateFrom!=null){
56
            yearUri = "&year="+query.dateFrom.get(Calendar.YEAR);
57
            if(query.dateTo!=null){
58
                yearUri += ","+query.dateTo.get(Calendar.YEAR);
59
            }
60
            //TODO we skip month range query because it only checks for the month range ignoring the year range
61
            //TODO date is not supported by GBIF
62
        }
63
        List<NameValuePair> queryParamsGET = new GbifQueryGenerator().generateQueryParams(query);
64
        URI uri = createUri(SUB_PATH, queryParamsGET);
65

    
66
        URIBuilder builder = new URIBuilder(uri.toString()+yearUri);
67

    
68
        if(UriUtils.isServiceAvailable(uri, 10000)){
69
            logger.info("Querying GBIF service with " + builder.build());
70
            return GbifJsonOccurrenceParser.parseJsonRecords(executeHttpGet(builder.build(), null));
71
        }
72
        else{
73
            logger.error("Querying " + uri + " got a timeout!");
74
            return null;
75
        }
76
    }
77

    
78
    /**
79
     * Queries GBIF for the original data set<br>
80
     * @param gbifResponse the GbifResponse holds the link to the dataset webservice
81
     * @return a {@link DataSetResponse} holding all relevant information to query the original provider
82
     * @throws IOException
83
     * @throws ClientProtocolException
84
     */
85
    public DataSetResponse queryOriginalDataSet(GbifResponse gbifResponse) throws ClientProtocolException, IOException{
86
        //FIXME move ABCD import here and change return type to DerivedUnitFacade/SpecimenOrObservationBase
87
        GbifDataSetProtocol dataSetProtocol = gbifResponse.getDataSetProtocol();
88
        if(dataSetProtocol == GbifDataSetProtocol.BIOCASE){
89
            DataSetResponse response = GbifJsonOccurrenceParser.parseOriginalDataSetUri(executeHttpGet(gbifResponse.getDataSetUri(), null));
90
            //the unitID is delivered in the "catalogNumber" parameter which is set as the accessionNumber of the facade
91
            response.setUnitId(gbifResponse.getDerivedUnitFacade().getAccessionNumber());
92
            return response;
93
        }
94
        return null;
95
    }
96

    
97
}
(5-5/6)