git_sitools_idoc / sitools-idoc / hesiod / javaExt / src / fr / ias / sitools / resources / geojson / DatasetToJson.java @ 779bac69
1 |
/*
|
---|---|
2 |
* To change this license header, choose License Headers in Project Properties.
|
3 |
* To change this template file, choose Tools | Templates
|
4 |
* and open the template in the editor.
|
5 |
*/
|
6 |
|
7 |
package fr.ias.sitools.resources.geojson; |
8 |
|
9 |
import fr.cnes.sitools.common.exception.SitoolsException; |
10 |
import fr.cnes.sitools.common.resource.SitoolsParameterizedResource; |
11 |
import fr.cnes.sitools.dataset.DataSetApplication; |
12 |
import fr.cnes.sitools.dataset.database.DatabaseRequest; |
13 |
import fr.cnes.sitools.dataset.database.DatabaseRequestFactory; |
14 |
import fr.cnes.sitools.dataset.database.DatabaseRequestParameters; |
15 |
import fr.cnes.sitools.dataset.database.common.DataSetExplorerUtil; |
16 |
import fr.cnes.sitools.dataset.dto.ColumnConceptMappingDTO; |
17 |
import fr.cnes.sitools.dataset.dto.DictionaryMappingDTO; |
18 |
import fr.cnes.sitools.datasource.jdbc.model.AttributeValue; |
19 |
import fr.cnes.sitools.datasource.jdbc.model.Record; |
20 |
import fr.cnes.sitools.dictionary.model.Concept; |
21 |
import java.io.IOException; |
22 |
import java.io.Writer; |
23 |
import java.util.HashMap; |
24 |
import java.util.List; |
25 |
import java.util.Map; |
26 |
import java.util.logging.Level; |
27 |
import java.util.logging.Logger; |
28 |
import org.restlet.data.MediaType; |
29 |
import org.restlet.ext.wadl.MethodInfo; |
30 |
import org.restlet.representation.Representation; |
31 |
import org.restlet.representation.Variant; |
32 |
import org.restlet.resource.Get; |
33 |
// IMPORT POUR LE JSON
|
34 |
import org.restlet.representation.WriterRepresentation; |
35 |
|
36 |
//-------------------------
|
37 |
|
38 |
/**
|
39 |
*
|
40 |
* @author marc
|
41 |
*/
|
42 |
public class DatasetToJson extends SitoolsParameterizedResource { |
43 |
|
44 |
private static final Logger LOG = Logger.getLogger(DatasetToJson.class.getName()); |
45 |
|
46 |
private String dicoNameString = new String(); |
47 |
|
48 |
final private String mimeTypeFits = "application/fits"; |
49 |
|
50 |
final private int maxResultsSend = 999999999; |
51 |
|
52 |
final private double ratioSpolyToRaDec = 57.295779513; |
53 |
|
54 |
@Override
|
55 |
public void sitoolsDescribe() { |
56 |
setName("DatasetToJson");
|
57 |
setDescription("retrieves Dataset to Json file");
|
58 |
} |
59 |
|
60 |
@Override
|
61 |
public void doInit() { |
62 |
super.doInit();
|
63 |
} |
64 |
|
65 |
/**
|
66 |
* Get HTML
|
67 |
*
|
68 |
* @return Representation the HTML result
|
69 |
*/
|
70 |
@Get
|
71 |
public Representation get() {
|
72 |
return execute();
|
73 |
} |
74 |
|
75 |
@Override
|
76 |
protected void describeGet(MethodInfo info) { |
77 |
this.addInfo(info);
|
78 |
info.setIdentifier("retrieve records and ");
|
79 |
info.setDocumentation("Method to get Json file from a dataset");
|
80 |
addStandardGetRequestInfo(info); |
81 |
DataSetExplorerUtil.addDatasetExplorerGetRequestInfo(info); |
82 |
DataSetApplication application = (DataSetApplication) getApplication(); |
83 |
DataSetExplorerUtil.addDatasetExplorerGetFilterInfo(info, application.getFilterChained()); |
84 |
addStandardResponseInfo(info); |
85 |
addStandardInternalServerErrorInfo(info); |
86 |
} |
87 |
|
88 |
@Override
|
89 |
protected Representation head(Variant variant) {
|
90 |
Representation repr = super.head();
|
91 |
//repr.setMediaType(MediaType.APPLICATION_JSON);
|
92 |
return repr;
|
93 |
} |
94 |
|
95 |
private Representation execute() {
|
96 |
|
97 |
dicoNameString = DatasetToJsonModel.DICO_PARAM_NAME; |
98 |
|
99 |
final Representation repr;
|
100 |
repr = new WriterRepresentation(MediaType.APPLICATION_JSON) { ;
|
101 |
//repr =
|
102 |
@Override
|
103 |
public void write(Writer writer) throws IOException { |
104 |
boolean jsonP = false; |
105 |
String cb = getRequest().getResourceRef().getQueryAsForm().getFirstValue("callback"); |
106 |
String limit = null; |
107 |
limit = getRequest().getResourceRef().getQueryAsForm().getFirstValue("limit");
|
108 |
|
109 |
if (cb != null) { |
110 |
jsonP = true;
|
111 |
//getResponse().set //setContentType("text/javascript");
|
112 |
this.setMediaType(MediaType.TEXT_JAVASCRIPT);
|
113 |
} else {
|
114 |
this.setMediaType(MediaType.APPLICATION_JSON);
|
115 |
} |
116 |
|
117 |
|
118 |
// generate the DatabaseRequest
|
119 |
DataSetApplication datasetApp = (DataSetApplication) getApplication(); |
120 |
DataSetExplorerUtil dsExplorerUtil = new DataSetExplorerUtil(datasetApp, getRequest(), getContext());
|
121 |
DictionaryMappingDTO dico =null;
|
122 |
|
123 |
String dicoName = getParameterValue(dicoNameString);
|
124 |
// Get the HashMap with as key the concept and value the columnAlias
|
125 |
final HashMap<Concept,String> conceptsColumns = getcolumnAliasFromDico(dicoName, dico, datasetApp); |
126 |
|
127 |
// Get request parameters
|
128 |
if (datasetApp.getConverterChained() != null) { |
129 |
datasetApp.getConverterChained().getContext().getAttributes().put("REQUEST", getRequest());
|
130 |
} |
131 |
// Get DatabaseRequestParameters
|
132 |
final DatabaseRequestParameters params = dsExplorerUtil.getDatabaseParams();
|
133 |
LOG.log(Level.INFO,"***************************************** limit : "+ limit); |
134 |
if(limit == null){ |
135 |
LOG.log(Level.INFO," ------------------- JE SUIS DANS LE IF"); |
136 |
params.setPaginationExtend(maxResultsSend); |
137 |
}else if(limit.equals("25")){ |
138 |
LOG.log(Level.INFO," ------------------- JE SUIS DANS LE ELSE IF"); |
139 |
params.setPaginationExtend(maxResultsSend); |
140 |
}else{
|
141 |
LOG.log(Level.INFO," ------------------- JE SUIS DANS LE ELSE"); |
142 |
params.setPaginationExtend(Integer.parseInt(limit));
|
143 |
} |
144 |
//params.setPaginationExtend(maxResultsSend);
|
145 |
//params.setPaginationExtend(datasetApp.getDataSet().getNbRecords());
|
146 |
DatabaseRequest databaseRequest = DatabaseRequestFactory.getDatabaseRequest(params); |
147 |
|
148 |
try {
|
149 |
databaseRequest.createRequest(); |
150 |
|
151 |
}catch (SitoolsException e) {
|
152 |
e.printStackTrace(); |
153 |
} |
154 |
if(jsonP){
|
155 |
writer.write(cb + "(");
|
156 |
} |
157 |
String response = ""; |
158 |
//response = "{";
|
159 |
writer.write("{");
|
160 |
//response += "\"type\":\"FeatureCollection\",";
|
161 |
writer.write("\"type\":\"FeatureCollection\",");
|
162 |
// start features
|
163 |
//response +="\"totalResults\":" + databaseRequest.getTotalCount() + ",";
|
164 |
writer.write("\"totalResults\":" + databaseRequest.getTotalCount() + ","); |
165 |
//response +="\"features\":[";
|
166 |
writer.write("\"features\":[");
|
167 |
// Next for reading first record
|
168 |
|
169 |
try {
|
170 |
boolean first = true; |
171 |
while (databaseRequest.nextResult()) {
|
172 |
Record rec = databaseRequest.getRecord(); |
173 |
if (!first) {
|
174 |
//response += ",";
|
175 |
writer.write(",");
|
176 |
} |
177 |
else {
|
178 |
first = false;
|
179 |
} |
180 |
// creates a geometry and a properties string
|
181 |
String geometry = ""; |
182 |
String services = ""; |
183 |
String properties = ""; |
184 |
boolean firstProp = true; |
185 |
String coords[] = new String[2]; |
186 |
String coordinateReference = ""; |
187 |
String urlDownloadFits = ""; |
188 |
String spoly = ""; |
189 |
|
190 |
for(Concept concept : conceptsColumns.keySet()){
|
191 |
/*if(cpt%100 == 0){
|
192 |
LOG.log(Level.INFO, "Je suis dans le DANS la boucle WHILE sur les records, et dans la boucle FOR des concepts et cpt = "+cpt);
|
193 |
}*/
|
194 |
concept.getPropertyFromName("category");
|
195 |
String colAlias = conceptsColumns.get(concept);
|
196 |
for(AttributeValue attr : rec.getAttributeValues()){
|
197 |
if(attr.getName().equals(colAlias) && attr.getValue() != null && !attr.getValue().equals("")){ |
198 |
if(concept.getPropertyFromName("category").getValue().contains("properties")){ |
199 |
if (!firstProp) {
|
200 |
properties += ",";
|
201 |
} |
202 |
else {
|
203 |
firstProp = false;
|
204 |
} |
205 |
properties += "\"" + concept.getName().toString() + "\":\"" + attr.getValue() + "\""; |
206 |
} |
207 |
if(concept.getPropertyFromName("category").getValue().contains("geometry")){ |
208 |
if(concept.getName().equals("ra")){ |
209 |
coords[0]= attr.getValue().toString();
|
210 |
}else if(concept.getName().equals("dec")){ |
211 |
coords[1]= attr.getValue().toString();
|
212 |
} |
213 |
if(concept.getName().equals("coordref")){ |
214 |
coordinateReference = attr.getValue().toString(); |
215 |
} |
216 |
if(concept.getName().equals("spoly")){ |
217 |
spoly = attr.getValue().toString(); |
218 |
} |
219 |
} |
220 |
if(concept.getPropertyFromName("category").getValue().contains("services")){ |
221 |
if(concept.getName().equals("download")){ |
222 |
urlDownloadFits = attr.getValue().toString(); |
223 |
} |
224 |
} |
225 |
|
226 |
} |
227 |
} |
228 |
} |
229 |
|
230 |
// Set the geometry
|
231 |
geometry = setGeometry(coords, coordinateReference, spoly); |
232 |
// Set The services
|
233 |
if(!urlDownloadFits.isEmpty()){
|
234 |
services = setServices(urlDownloadFits); |
235 |
} |
236 |
// start feature
|
237 |
//response += "{";
|
238 |
writer.write("{");
|
239 |
//response += "\"type\":\"feature\",";
|
240 |
writer.write("\"type\":\"feature\",");
|
241 |
// start geometry
|
242 |
//response += "\"geometry\":{";
|
243 |
writer.write("\"geometry\":{");
|
244 |
//response += geometry;
|
245 |
writer.write(geometry); |
246 |
//response += "}";
|
247 |
writer.write("}");
|
248 |
// end geometry
|
249 |
//response += ",";
|
250 |
writer.write(",");
|
251 |
// start properties
|
252 |
//response += "\"properties\":{";
|
253 |
writer.write("\"properties\":{");
|
254 |
//response += properties;
|
255 |
writer.write(properties); |
256 |
// end properties
|
257 |
//response += "}";
|
258 |
writer.write("}");
|
259 |
|
260 |
// start services
|
261 |
if(!services.equals("")){ |
262 |
//response += ",";
|
263 |
writer.write(",");
|
264 |
//response += "\"services\":{";
|
265 |
writer.write("\"services\":{");
|
266 |
//response += services;
|
267 |
writer.write(services); |
268 |
// end services
|
269 |
//response += "}";
|
270 |
writer.write("}");
|
271 |
} |
272 |
|
273 |
// end feature
|
274 |
//response += "}";
|
275 |
writer.write("}");
|
276 |
} |
277 |
// end features
|
278 |
//response += "]";
|
279 |
writer.write("]");
|
280 |
|
281 |
}catch (SitoolsException ex) {
|
282 |
Logger.getLogger(DatasetToJson.class.getName()).log(Level.SEVERE, null, ex); |
283 |
}finally {
|
284 |
//response += "}";
|
285 |
writer.write("}");
|
286 |
if (databaseRequest != null) { |
287 |
try {
|
288 |
databaseRequest.close(); |
289 |
}catch (SitoolsException e) {
|
290 |
e.printStackTrace(); |
291 |
} |
292 |
} |
293 |
if (writer != null) { |
294 |
//LOG.log(Level.INFO, "Je suis dans le finally avant le if jsonP");
|
295 |
if(jsonP){
|
296 |
//LOG.log(Level.INFO, "Je suis dans le finally dans le IF du if jsonP");
|
297 |
//String response2 = cb + "("+response +");";
|
298 |
writer.write(");");
|
299 |
writer.flush(); |
300 |
}else{
|
301 |
//LOG.log(Level.INFO, "Je suis dans le finally dans le ELSE du if jsonP");
|
302 |
//writer.write(response);
|
303 |
writer.flush(); |
304 |
} |
305 |
} |
306 |
} |
307 |
} |
308 |
}; |
309 |
return repr;
|
310 |
|
311 |
} |
312 |
|
313 |
private HashMap<Concept,String> getcolumnAliasFromDico(String dicoName, DictionaryMappingDTO dico, DataSetApplication datasetApp){ |
314 |
|
315 |
dico = datasetApp.getColumnConceptMappingDTO(dicoName); |
316 |
final List<ColumnConceptMappingDTO> colConceptMappingDTOList = dico.getMapping(); |
317 |
final HashMap<Concept,String> conceptColumn = new HashMap<Concept,String>(); |
318 |
|
319 |
for(ColumnConceptMappingDTO concepts : colConceptMappingDTOList){
|
320 |
conceptColumn.put(concepts.getConcept(), concepts.getColumnAlias()); |
321 |
} |
322 |
return conceptColumn;
|
323 |
} |
324 |
|
325 |
private String setGeometry(String[] coords, String coordRef, String spoly){ |
326 |
String geometry = new String(); |
327 |
String[] spolyStringTmp = new String[4]; |
328 |
String[] spolyConverted = new String[4]; |
329 |
if(spoly.equals("")){ |
330 |
geometry = "\"coordinates\": ["+coords[0]+","+coords[1]+"],"; |
331 |
geometry += "\"referencesystem\": \""+coordRef+"\",\"type\": \"Point\""; |
332 |
}else{
|
333 |
|
334 |
String[] tmpString = spoly.split("\\)"); |
335 |
//LOG.log(Level.INFO, " SPOLY : "+spoly);
|
336 |
tmpString[0] = tmpString[0].substring(2, tmpString[0].length()); |
337 |
spolyStringTmp[0] = tmpString[0]; |
338 |
//LOG.log(Level.INFO, " i : 0 = "+spolyStringTmp[0]);
|
339 |
for(int i=1;i<4;i++){ |
340 |
spolyStringTmp[i] = tmpString[i].substring(2, tmpString[i].length());
|
341 |
//LOG.log(Level.INFO, "spolyStringTmp i :"+i+ " = "+spolyStringTmp[i]);
|
342 |
} |
343 |
|
344 |
for(int k=0;k<spolyStringTmp.length;k++){ |
345 |
spolyConverted[k] = "["+String.valueOf((Double.parseDouble(spolyStringTmp[k].split(",")[0])*ratioSpolyToRaDec)) |
346 |
+" , "+String.valueOf((Double.parseDouble(spolyStringTmp[k].split(",")[1])*ratioSpolyToRaDec))+"]"; |
347 |
//LOG.log(Level.INFO, " spolyConverted k :"+k+ " = "+spolyConverted[k]);
|
348 |
} |
349 |
|
350 |
geometry = "\"coordinates\":[["+spolyConverted[3]+","+spolyConverted[2]+","+spolyConverted[1]+","+spolyConverted[0]+","+spolyConverted[3]+"]],"; |
351 |
geometry += "\"referencesystem\": \""+coordRef+"\",\"type\": \"Polygon\""; |
352 |
} |
353 |
|
354 |
return geometry;
|
355 |
} |
356 |
|
357 |
private String setServices(String urlDownFits){ |
358 |
String services = new String(); |
359 |
services += "\"download\":{ \"mimetype\":\""+mimeTypeFits+"\","; |
360 |
services += "\"url\":\""+urlDownFits+"\"}"; |
361 |
return services;
|
362 |
} |
363 |
} |