Project

General

Profile

Download (5.28 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.util.ArrayList;
13
import java.util.List;
14

    
15
import org.apache.http.NameValuePair;
16

    
17
import eu.etaxonomy.cdm.ext.common.ServiceWrapperBase;
18
import eu.etaxonomy.cdm.ext.occurrence.OccurenceQuery;
19

    
20
/**
21
 * Generates the query URL string for GBIF queries.
22
 * @author pplitzner
23
 * @date 13.09.2013
24
 *
25
 */
26
public class GbifQueryGenerator {
27

    
28
    /**
29
     * Generates the query URL string for GBIF queries.
30
     * @param query the {@link OccurenceQuery}
31
     * @param queryParamsGET
32
     * @return the query URL string
33
     */
34
    public List<NameValuePair> generateQueryParams(OccurenceQuery query){
35
        List<NameValuePair> queryParamsGET = new ArrayList<NameValuePair>();
36
        // only look for preserved specimens
37

    
38
        if (checkForValidQuery(query)) {
39
            ServiceWrapperBase.addNameValuePairTo(queryParamsGET, "basisOfRecord", "PRESERVED_SPECIMEN");
40

    
41
            ServiceWrapperBase.addNameValuePairTo(queryParamsGET, "limit", "100");
42
            if (query.tripleIds != null){
43
                for (String[] tripleId:query.tripleIds){
44
                    ServiceWrapperBase.addNameValuePairTo(queryParamsGET, "CatalogNumber",tripleId[0]);
45
                    ServiceWrapperBase.addNameValuePairTo(queryParamsGET, "InstitutionCode",tripleId[1]);
46
                    ServiceWrapperBase.addNameValuePairTo(queryParamsGET, "CollectionCode", tripleId[2]);
47
                }
48
            }else{
49
                if((query.accessionNumber!=null && !query.accessionNumber.isEmpty()) ){
50
                    ServiceWrapperBase.addNameValuePairTo(queryParamsGET, "CatalogNumber", query.accessionNumber);
51

    
52

    
53
                }
54
                if(query.collector!=null && !query.collector.isEmpty()){
55
                    ServiceWrapperBase.addNameValuePairTo(queryParamsGET, "recordedBy", query.collector);
56
                }
57
                if(query.collectorsNumber!=null && !query.collectorsNumber.isEmpty()){
58
                    // TODO refine parameter
59
                    ServiceWrapperBase.addNameValuePairTo(queryParamsGET, "fieldNumber", query.collectorsNumber);
60
                    ServiceWrapperBase.addNameValuePairTo(queryParamsGET, "recordNumber", query.collectorsNumber);
61
                }
62
                if(query.country!=null && !query.country.isEmpty()){
63
                    ServiceWrapperBase.addNameValuePairTo(queryParamsGET, "country", query.country);
64
                }
65
                /**
66
                       Date Range: January – June 1899
67
                       http://api.gbif.org/v0.9/occurrence/search?basisOfRecord=preserved_specimen&scientificName=Campanula%20persicifolia&year=1899&month=1,6
68
                       Exact Date: june 20th 1901
69
                       http://api.gbif.org/v0.9/occurrence/search?basisOfRecord=preserved_specimen&scientificName=Campanula%20persicifolia&eventDate=1901-6-20
70
                       Date range exact: Jan. 02.1899 to june 03. 1902
71
                       http://api.gbif.org/v0.9/occurrence/search?basisOfRecord=preserved_specimen&scientificName=Campanula%20persicifolia&eventDate=1899-1-2,1902-6-3
72
                 */
73
                // FIXME: currently ONLY year is handled by the query (see GbifServiceWrapper)
74
                if(query.herbarium!=null && !query.herbarium.isEmpty()){
75
                    // TODO refine parameter
76
                    ServiceWrapperBase.addNameValuePairTo(queryParamsGET, "institutionCode", query.herbarium);
77
                }
78
                if(query.locality!=null && !query.locality.isEmpty()){
79
                    //TODO not yet available at GBIF
80
        //            ServiceWrapperBase.addNameValuePairTo(queryParamsGET, param, query.locality);
81
                }
82
                if(query.taxonName!=null && !query.taxonName.isEmpty()){
83
                    ServiceWrapperBase.addNameValuePairTo(queryParamsGET, "scientificName", query.taxonName);
84
                }
85
            }
86
            return queryParamsGET;
87
        }
88
        return null;
89
    }
90

    
91
    /**
92
     * @param query
93
     */
94
    private boolean checkForValidQuery(OccurenceQuery query) {
95
       boolean valid = false;
96
       if (query.tripleIds != null) {
97
           for (String[] tripleId:query.tripleIds){
98
               if (tripleId[0] != null || tripleId[1] != null || tripleId[2] != null){
99
                   valid = true;
100
               }
101
           }
102
       } else if((query.accessionNumber!=null && !query.accessionNumber.isEmpty()) ){
103
           valid = true;
104
       } else if (query.collector!=null && !query.collector.isEmpty()){
105
           valid = true;
106
       }else if(query.collectorsNumber!=null && !query.collectorsNumber.isEmpty()){
107
           valid = true;
108
       } else if (query.country!=null && !query.country.isEmpty()){
109
           valid = true;
110
       } else if(query.herbarium!=null && !query.herbarium.isEmpty()){
111
           valid = true;
112
       } else if (query.taxonName!=null && !query.taxonName.isEmpty()){
113
          valid = true;
114
       } else if (query.locality!=null && !query.locality.isEmpty()){
115
           //TODO not yet available at GBIF
116
       }
117

    
118
       return valid;
119
    }
120

    
121

    
122
}
(4-4/6)