Project

General

Profile

Download (4.42 KB) Statistics
| Branch: | Tag: | Revision:
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.gbif;
10

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

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

    
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;
26

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

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

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

    
46
    /**
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
50
     */
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
53
        String yearUri = "";
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);
58
            }
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
61
        }
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);
66

    
67
            if(UriUtils.isServiceAvailable(uri, 10000)){
68
                logger.info("Querying GBIF service with " + builder.build());
69
                return GbifJsonOccurrenceParser.parseJsonRecords(executeHttpGet(builder.build(), null));
70
            }
71
            else{
72
                logger.error("Querying " + uri + " got a timeout!");
73
                return null;
74
            }
75
        } else{
76
            logger.info("Querying GBIF service was skipped because of missing get parameters.");
77
            return null;
78
        }
79
    }
80

    
81
    /**
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
85
     * @throws IOException
86
     * @throws ClientProtocolException
87
     */
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());
95
            return response;
96
        }else{
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());
100
            return response;
101
        }
102

    
103
    }
104

    
105
}
(5-5/6)