Project

General

Profile

Download (14 KB) Statistics
| Branch: | Tag: | Revision:
1
package org.bgbm.biovel.drf.occurrences;
2

    
3
import java.net.URI;
4
import java.text.ParseException;
5
import java.text.SimpleDateFormat;
6
import java.util.ArrayList;
7
import java.util.Date;
8
import java.util.HashMap;
9
import java.util.Iterator;
10
import java.util.List;
11
import java.util.Map;
12

    
13
import org.apache.http.HttpHost;
14
import org.bgbm.biovel.drf.checklist.DRFChecklistException;
15
import org.bgbm.biovel.drf.client.ServiceProviderInfo;
16
import org.bgbm.biovel.drf.query.RestClient;
17
import org.bgbm.biovel.drf.utils.CSVUtils;
18
import org.bgbm.biovel.drf.utils.JSONUtils;
19
import org.json.simple.JSONArray;
20
import org.json.simple.JSONObject;
21

    
22
public class GBIFOccurrencesClient extends BaseOccurrencesClient<RestClient> {
23

    
24

    
25
    /**
26
     *
27
     */
28
    private static final HttpHost HTTP_HOST = new HttpHost("api.gbif.org",80);
29
    public static final String ID = "gbif";
30
    public static final String LABEL = "GBIF Occurrence Bank";
31
    public static final String URL = "http://uat.gbif.org/developer/species";
32
    public static final String DATA_AGR_URL = "http://data.gbif.org/tutorial/datauseagreement";
33
    // in v0.9 the max limit is 300
34
    private static final String MAX_PAGING_LIMIT = "300";
35
    private static final String VERSION = "v0.9";
36
    private static final ServiceProviderInfo CINFO = new ServiceProviderInfo(ID,LABEL,ServiceProviderInfo.DEFAULT_SEARCH_MODE,URL,DATA_AGR_URL, VERSION);
37

    
38
    private final Map<String, JSONObject> datasetCacheMap = new HashMap<String, JSONObject>();
39
    private final Map<String, JSONObject> orgCacheMap = new HashMap<String, JSONObject>();
40
    public final static List<String> nameidList = new ArrayList<String>();
41

    
42

    
43
    /**
44
     * {@inheritDoc}
45
     */
46
    @Override
47
    public void initQueryClient() {
48
        queryClient = new RestClient(HTTP_HOST);
49

    
50
    }
51

    
52
    @Override
53
    public int getMaxPageSize() {
54
        // TODO Auto-generated method stub
55
        return 0;
56
    }
57

    
58
    @Override
59
    public ServiceProviderInfo buildServiceProviderInfo() {
60
        ServiceProviderInfo ocbankInfo = CINFO;
61
        return ocbankInfo;
62
    }
63

    
64
    @Override
65
    public String getOccurrences(String nameid) throws DRFChecklistException {
66

    
67
        URI namesUri = queryClient.buildUriFromQueryString(nameid, "/" + CINFO.getVersion() + "/species/match", "name", null);
68
        String nameResponse = queryClient.processRESTService(namesUri);
69
        JSONObject nameJsonResponse = JSONUtils.parseJsonToObject(nameResponse);
70
        StringBuilder occurrences = new StringBuilder();
71
        if(nameJsonResponse.get("usageKey") != null) {
72
            String usageKey = String.valueOf(nameJsonResponse.get("usageKey"));
73

    
74
            if(!nameidList.contains(usageKey)) {
75
                nameidList.add(usageKey);
76
                //http://api.gbif.org/v0.9/occurrence/search?offset=100&limit=100&taxonKey=2818622
77
                Map<String, String> paramMap = new HashMap<String, String>();
78
                paramMap.put("limit", MAX_PAGING_LIMIT);
79
                boolean endOfRecords = false;
80
                int offset = 0;
81

    
82

    
83
                int count = 0;
84
                do {
85
                    paramMap.put("offset", Integer.toString(offset));
86
                    URI occUri = queryClient.buildUriFromQueryString(usageKey, "/" + CINFO.getVersion() + "/occurrence/search", "taxonKey", paramMap);
87

    
88
                    String occResponse = queryClient.processRESTService(occUri);
89

    
90
                    JSONObject jsonOccResponse = JSONUtils.parseJsonToObject(occResponse);
91
                    JSONArray results = (JSONArray) jsonOccResponse.get("results");
92
                    System.out.println("actual results size : " + results.size());
93
                    if(results != null) {
94
                        Iterator<JSONObject> resIterator = results.iterator();
95

    
96
                        while (resIterator.hasNext()) {
97
                            JSONObject jsonOccurence = resIterator.next();
98
                            occurrences.append(",");
99

    
100
                            if(jsonOccurence.get("genus") != null) {
101
                                occurrences.append(CSVUtils.wrapWhenComma((String) jsonOccurence.get("genus")));
102
                            }
103
                            occurrences.append(",");
104

    
105

    
106
                            occurrences.append(",");
107

    
108

    
109
                            occurrences.append(",");
110

    
111

    
112
                            occurrences.append(",");
113

    
114
                            if(jsonOccurence.get("scientificName") != null) {
115
                                occurrences.append(CSVUtils.wrapWhenComma((String) jsonOccurence.get("scientificName")));
116
                            }
117
                            occurrences.append(",");
118

    
119
                            occurrences.append(",");
120

    
121
                            if(jsonOccurence.get("scientificName") != null) {
122
                                occurrences.append(CSVUtils.wrapWhenComma((String) jsonOccurence.get("scientificName")));
123
                            }
124
                            occurrences.append(",");
125

    
126
                            if(jsonOccurence.get("key") != null) {
127
                                occurrences.append(CSVUtils.wrapWhenComma(String.valueOf(jsonOccurence.get("key"))));
128
                            }
129
                            occurrences.append(",");
130

    
131
                            if(jsonOccurence.get("decimalLatitude") != null) {
132
                                occurrences.append(CSVUtils.wrapWhenComma(String.valueOf(jsonOccurence.get("decimalLatitude"))));
133
                            }
134
                            occurrences.append(",");
135

    
136
                            if(jsonOccurence.get("decimalLongitude") != null) {
137
                                occurrences.append(CSVUtils.wrapWhenComma(String.valueOf(jsonOccurence.get("decimalLongitude"))));
138
                            }
139
                            occurrences.append(",");
140

    
141
                            String formattedDate = "";
142
                            SimpleDateFormat gbifFormatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
143
                            SimpleDateFormat biovelFormatter = new SimpleDateFormat("yyyy-MM-dd");
144

    
145
                            if(jsonOccurence.get("occurrenceDate") != null) {
146
                                String strDate = (String) jsonOccurence.get("occurrenceDate");
147
                                try {
148
                                    Date date = gbifFormatter.parse(strDate);
149
                                    formattedDate = biovelFormatter.format(date);
150
                                } catch (ParseException e) {
151
                                    formattedDate = "";
152
                                }
153
                            } else if((jsonOccurence.get("year") != null) && (jsonOccurence.get("month") != null) && (jsonOccurence.get("day") != null)) {
154
                                String year = String.valueOf(jsonOccurence.get("year"));
155
                                String month = String.valueOf(jsonOccurence.get("month"));
156
                                String day = String.valueOf(jsonOccurence.get("day"));
157
                                formattedDate = year + "-" + month + "-" + day;
158
                                System.out.println("date : " + formattedDate);
159
                            }
160
                            occurrences.append(CSVUtils.wrapWhenComma(formattedDate));
161
                            occurrences.append(",");
162

    
163

    
164
                            //if(jsonOccurence.get("occurrenceDate") != null) {
165
                                occurrences.append(CSVUtils.wrapWhenComma(formattedDate));
166
                            //}
167
                            occurrences.append(",");
168

    
169

    
170
                            if(jsonOccurence.get("coordinateAccurracyInMeters") != null) {
171
                                occurrences.append(CSVUtils.wrapWhenComma(Integer.toString((Integer) jsonOccurence.get("coordinateAccurracyInMeters"))));
172
                            }
173
                            occurrences.append(",");
174

    
175
                            if(jsonOccurence.get("country") != null) {
176
                                occurrences.append(CSVUtils.wrapWhenComma((String) jsonOccurence.get("country")));
177
                            }
178
                            occurrences.append(",");
179

    
180
                            if(jsonOccurence.get("collectorName") != null) {
181
                                occurrences.append(CSVUtils.wrapWhenComma((String) jsonOccurence.get("collectorName")));
182
                            }
183
                            occurrences.append(",");
184

    
185
                            occurrences.append(",");
186

    
187
                            if(jsonOccurence.get("locality") != null) {
188
                                occurrences.append(CSVUtils.wrapWhenComma((String) jsonOccurence.get("locality")));
189
                            }
190
                            occurrences.append(",");
191

    
192
                            if(jsonOccurence.get("depth") != null) {
193
                                occurrences.append(CSVUtils.wrapWhenComma(String.valueOf(jsonOccurence.get("depth"))));
194
                            }
195
                            occurrences.append(",");
196

    
197
                            if(jsonOccurence.get("altitude") != null) {
198
                                occurrences.append(CSVUtils.wrapWhenComma(String.valueOf(jsonOccurence.get("altitude"))));
199
                            }
200
                            occurrences.append(",");
201

    
202
                            if(jsonOccurence.get("depth") != null) {
203
                                String depth = CSVUtils.wrapWhenComma(String.valueOf(jsonOccurence.get("depth")));
204
                                occurrences.append(depth);
205
                                System.out.println("depth : " + depth);
206
                            }
207
                            occurrences.append(",");
208

    
209
                            if(jsonOccurence.get("altitude") != null) {
210
                                occurrences.append(CSVUtils.wrapWhenComma(String.valueOf(jsonOccurence.get("altitude"))));
211
                            }
212
                            occurrences.append(",");
213

    
214
                            occurrences.append(",");
215

    
216
                            JSONObject datasetJsonResponse = null;
217
                            if(jsonOccurence.get("datasetKey") != null) {
218
                                String datasetKey = (String) jsonOccurence.get("datasetKey");
219
                                datasetJsonResponse = datasetCacheMap.get(datasetKey);
220
                                if(datasetJsonResponse == null) {
221
                                    URI datasetUri = queryClient.buildUriFromQueryString("/" + CINFO.getVersion() + "/dataset/" + datasetKey, null);
222
                                    String datasetResponse = queryClient.processRESTService(datasetUri);
223
                                    datasetJsonResponse = JSONUtils.parseJsonToObject(datasetResponse);
224
                                    datasetCacheMap.put(datasetKey, datasetJsonResponse);
225
                                }
226
                            }
227

    
228
                            JSONObject orgJsonResponse = null;
229
                            if(datasetJsonResponse != null && datasetJsonResponse.get("owningOrganizationKey") != null) {
230
                                String owningOrganizationKey = (String) datasetJsonResponse.get("owningOrganizationKey");
231
                                orgJsonResponse = orgCacheMap.get(owningOrganizationKey);
232
                                if(orgJsonResponse == null) {
233
                                    URI orgUri = queryClient.buildUriFromQueryString("/" + CINFO.getVersion() + "/organization/" + owningOrganizationKey, null);
234
                                    String orgResponse = queryClient.processRESTService(orgUri);
235
                                    orgJsonResponse = JSONUtils.parseJsonToObject(orgResponse);
236
                                    orgCacheMap.put(owningOrganizationKey, orgJsonResponse);
237
                                }
238
                                if(orgJsonResponse != null && orgJsonResponse.get("title") != null) {
239
                                    occurrences.append(CSVUtils.wrapWhenComma((String) orgJsonResponse.get("title")));
240
                                }
241

    
242
                            }
243
                            occurrences.append(",");
244

    
245
                            if(datasetJsonResponse != null && datasetJsonResponse.get("title") != null) {
246
                                occurrences.append(CSVUtils.wrapWhenComma((String) datasetJsonResponse.get("title")));
247
                            }
248
                            occurrences.append(",");
249

    
250
                            if(datasetJsonResponse != null && datasetJsonResponse.get("rights") != null) {
251
                                occurrences.append(CSVUtils.wrapWhenComma(((String) datasetJsonResponse.get("rights")).replaceAll("\r\n|\r|\n", " ")));
252
                                //System.out.println("rights : " + CSVUtils.wrapWhenComma(((String) datasetJsonResponse.get("rights")).replaceAll("\r\n|\r|\n", " ")));
253
                            }
254
                            occurrences.append(",");
255

    
256
                            if(datasetJsonResponse != null && datasetJsonResponse.get("citation") != null) {
257
                                JSONObject citationJson = (JSONObject) datasetJsonResponse.get("citation");
258
                                if(citationJson.get("text") != null) {
259
                                    occurrences.append(CSVUtils.wrapWhenComma(((String) citationJson.get("text")).replaceAll("\r\n|\r|\n", " ")));
260

    
261
                                }
262
                            }
263
                            occurrences.append(System.getProperty("line.separator"));
264
                            count++;
265

    
266
                        }
267
                    }
268
                    endOfRecords = (Boolean) jsonOccResponse.get("endOfRecords");
269
                    System.out.println("usageKey : " + usageKey + ", count : " + String.valueOf(jsonOccResponse.get("count")) + ", offset : " + offset + ",  + occ count : " + count);
270
                    offset = offset + Integer.parseInt(MAX_PAGING_LIMIT);
271
                } while(!endOfRecords);
272
                System.out.println("occ count : " + count);
273
            }
274
        }
275
        return occurrences.toString();
276
    }
277

    
278
}
(2-2/2)