1
|
|
2
|
|
3
|
|
4
|
|
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.logging.Level;
|
26
|
import java.util.logging.Logger;
|
27
|
import org.restlet.data.MediaType;
|
28
|
import org.restlet.ext.wadl.MethodInfo;
|
29
|
import org.restlet.representation.Representation;
|
30
|
import org.restlet.representation.Variant;
|
31
|
import org.restlet.resource.Get;
|
32
|
|
33
|
import org.restlet.representation.WriterRepresentation;
|
34
|
|
35
|
|
36
|
|
37
|
|
38
|
|
39
|
|
40
|
|
41
|
public class DatasetToJsonORIG extends SitoolsParameterizedResource {
|
42
|
|
43
|
private static final Logger LOG = Logger.getLogger(DatasetToJsonORIG.class.getName());
|
44
|
|
45
|
private String dicoNameString = new String();
|
46
|
|
47
|
final private String mimeTypeFits = "application/fits";
|
48
|
|
49
|
final private int maxResultsSend = 999999999;
|
50
|
|
51
|
final private double ratioSpolyToRaDec = 57.295779513;
|
52
|
|
53
|
@Override
|
54
|
public void sitoolsDescribe() {
|
55
|
setName("DatasetToJson");
|
56
|
setDescription("retrieves Dataset to Json file");
|
57
|
}
|
58
|
|
59
|
@Override
|
60
|
public void doInit() {
|
61
|
super.doInit();
|
62
|
}
|
63
|
|
64
|
|
65
|
|
66
|
|
67
|
|
68
|
|
69
|
@Get
|
70
|
public Representation get() {
|
71
|
return execute();
|
72
|
}
|
73
|
|
74
|
@Override
|
75
|
protected void describeGet(MethodInfo info) {
|
76
|
this.addInfo(info);
|
77
|
info.setIdentifier("retrieve records and ");
|
78
|
info.setDocumentation("Method to get Json file from a dataset");
|
79
|
addStandardGetRequestInfo(info);
|
80
|
DataSetExplorerUtil.addDatasetExplorerGetRequestInfo(info);
|
81
|
DataSetApplication application = (DataSetApplication) getApplication();
|
82
|
DataSetExplorerUtil.addDatasetExplorerGetFilterInfo(info, application.getFilterChained());
|
83
|
addStandardResponseInfo(info);
|
84
|
addStandardInternalServerErrorInfo(info);
|
85
|
}
|
86
|
|
87
|
@Override
|
88
|
protected Representation head(Variant variant) {
|
89
|
Representation repr = super.head();
|
90
|
repr.setMediaType(MediaType.APPLICATION_JSON);
|
91
|
return repr;
|
92
|
}
|
93
|
|
94
|
private Representation execute() {
|
95
|
|
96
|
dicoNameString = DatasetToJsonModel.DICO_PARAM_NAME;
|
97
|
|
98
|
Representation repr = new WriterRepresentation(MediaType.APPLICATION_JSON) {
|
99
|
|
100
|
@Override
|
101
|
public void write(Writer writer) throws IOException {
|
102
|
|
103
|
|
104
|
DataSetApplication datasetApp = (DataSetApplication) getApplication();
|
105
|
DataSetExplorerUtil dsExplorerUtil = new DataSetExplorerUtil(datasetApp, getRequest(), getContext());
|
106
|
DictionaryMappingDTO dico =null;
|
107
|
|
108
|
String dicoName = getParameterValue(dicoNameString);
|
109
|
|
110
|
final HashMap<Concept,String> conceptsColumns = getcolumnAliasFromDico(dicoName, dico, datasetApp);
|
111
|
|
112
|
|
113
|
if (datasetApp.getConverterChained() != null) {
|
114
|
datasetApp.getConverterChained().getContext().getAttributes().put("REQUEST", getRequest());
|
115
|
}
|
116
|
|
117
|
final DatabaseRequestParameters params = dsExplorerUtil.getDatabaseParams();
|
118
|
params.setPaginationExtend(maxResultsSend);
|
119
|
|
120
|
DatabaseRequest databaseRequest = DatabaseRequestFactory.getDatabaseRequest(params);
|
121
|
|
122
|
try {
|
123
|
databaseRequest.createRequest();
|
124
|
|
125
|
}catch (SitoolsException e) {
|
126
|
e.printStackTrace();
|
127
|
}
|
128
|
writer.write("{");
|
129
|
writer.write("\"type\":\"FeatureCollection\",");
|
130
|
|
131
|
writer.write("\"totalResults\":" + databaseRequest.getTotalCount() + ",");
|
132
|
writer.write("\"features\":[");
|
133
|
|
134
|
|
135
|
try {
|
136
|
boolean first = true;
|
137
|
|
138
|
while (databaseRequest.nextResult()) {
|
139
|
Record rec = databaseRequest.getRecord();
|
140
|
if (!first) {
|
141
|
writer.write(",");
|
142
|
}
|
143
|
else {
|
144
|
first = false;
|
145
|
}
|
146
|
|
147
|
String geometry = "";
|
148
|
String services = "";
|
149
|
String properties = "";
|
150
|
boolean firstProp = true;
|
151
|
String coords[] = new String[2];
|
152
|
String coordinateReference = "";
|
153
|
String urlDownloadFits = "";
|
154
|
String spoly = "";
|
155
|
|
156
|
for(Concept concept : conceptsColumns.keySet()){
|
157
|
concept.getPropertyFromName("category");
|
158
|
String colAlias = conceptsColumns.get(concept);
|
159
|
for(AttributeValue attr : rec.getAttributeValues()){
|
160
|
if(attr.getName().equals(colAlias) && attr.getValue() != null && !attr.getValue().equals("")){
|
161
|
if(concept.getPropertyFromName("category").getValue().contains("properties")){
|
162
|
if (!firstProp) {
|
163
|
properties += ",";
|
164
|
}
|
165
|
else {
|
166
|
firstProp = false;
|
167
|
}
|
168
|
properties += "\"" + concept.getName().toString() + "\":\"" + attr.getValue() + "\"";
|
169
|
}
|
170
|
if(concept.getPropertyFromName("category").getValue().contains("geometry")){
|
171
|
if(concept.getName().equals("ra")){
|
172
|
coords[0]= attr.getValue().toString();
|
173
|
}else if(concept.getName().equals("dec")){
|
174
|
coords[1]= attr.getValue().toString();
|
175
|
}
|
176
|
if(concept.getName().equals("coordref")){
|
177
|
coordinateReference = attr.getValue().toString();
|
178
|
}
|
179
|
if(concept.getName().equals("spoly")){
|
180
|
spoly = attr.getValue().toString();
|
181
|
}
|
182
|
}
|
183
|
if(concept.getPropertyFromName("category").getValue().contains("services")){
|
184
|
if(concept.getName().equals("download")){
|
185
|
urlDownloadFits = attr.getValue().toString();
|
186
|
}
|
187
|
}
|
188
|
|
189
|
}
|
190
|
}
|
191
|
}
|
192
|
|
193
|
|
194
|
geometry = setGeometry(coords, coordinateReference, spoly);
|
195
|
|
196
|
if(!urlDownloadFits.isEmpty()){
|
197
|
services = setServices(urlDownloadFits);
|
198
|
}
|
199
|
|
200
|
writer.write("{");
|
201
|
writer.write("\"type\":\"feature\",");
|
202
|
|
203
|
writer.write("\"geometry\":{");
|
204
|
writer.write(geometry);
|
205
|
writer.write("}");
|
206
|
|
207
|
writer.write(",");
|
208
|
|
209
|
writer.write("\"properties\":{");
|
210
|
writer.write(properties);
|
211
|
|
212
|
writer.write("}");
|
213
|
|
214
|
|
215
|
if(!services.equals("")){
|
216
|
writer.write(",");
|
217
|
writer.write("\"services\":{");
|
218
|
|
219
|
writer.write(services);
|
220
|
|
221
|
writer.write("}");
|
222
|
}
|
223
|
|
224
|
|
225
|
writer.write("}");
|
226
|
|
227
|
}
|
228
|
|
229
|
|
230
|
writer.write("]");
|
231
|
|
232
|
|
233
|
|
234
|
}catch (SitoolsException ex) {
|
235
|
Logger.getLogger(DatasetToJsonORIG.class.getName()).log(Level.SEVERE, null, ex);
|
236
|
}finally {
|
237
|
writer.write("}");
|
238
|
if (databaseRequest != null) {
|
239
|
try {
|
240
|
databaseRequest.close();
|
241
|
}catch (SitoolsException e) {
|
242
|
e.printStackTrace();
|
243
|
}
|
244
|
}
|
245
|
if (writer != null) {
|
246
|
writer.flush();
|
247
|
}
|
248
|
}
|
249
|
}
|
250
|
};
|
251
|
return repr;
|
252
|
|
253
|
}
|
254
|
|
255
|
private HashMap<Concept,String> getcolumnAliasFromDico(String dicoName, DictionaryMappingDTO dico, DataSetApplication datasetApp){
|
256
|
|
257
|
dico = datasetApp.getColumnConceptMappingDTO(dicoName);
|
258
|
final List<ColumnConceptMappingDTO> colConceptMappingDTOList = dico.getMapping();
|
259
|
final HashMap<Concept,String> conceptColumn = new HashMap<Concept,String>();
|
260
|
|
261
|
for(ColumnConceptMappingDTO concepts : colConceptMappingDTOList){
|
262
|
conceptColumn.put(concepts.getConcept(), concepts.getColumnAlias());
|
263
|
}
|
264
|
return conceptColumn;
|
265
|
}
|
266
|
|
267
|
private String setGeometry(String[] coords, String coordRef, String spoly){
|
268
|
String geometry = new String();
|
269
|
String[] spolyStringTmp = new String[4];
|
270
|
String[] spolyConverted = new String[4];
|
271
|
if(spoly.equals("")){
|
272
|
geometry = "\"coordinates\": ["+coords[0]+","+coords[1]+"],";
|
273
|
geometry += "\"referencesystem\": \""+coordRef+"\",\"type\": \"Point\"";
|
274
|
}else{
|
275
|
|
276
|
String[] tmpString = spoly.split("\\)");
|
277
|
|
278
|
tmpString[0] = tmpString[0].substring(2, tmpString[0].length());
|
279
|
spolyStringTmp[0] = tmpString[0];
|
280
|
|
281
|
for(int i=1;i<4;i++){
|
282
|
spolyStringTmp[i] = tmpString[i].substring(2, tmpString[i].length());
|
283
|
|
284
|
}
|
285
|
|
286
|
for(int k=0;k<spolyStringTmp.length;k++){
|
287
|
spolyConverted[k] = "["+String.valueOf((Double.parseDouble(spolyStringTmp[k].split(",")[0])*ratioSpolyToRaDec))
|
288
|
+" , "+String.valueOf((Double.parseDouble(spolyStringTmp[k].split(",")[1])*ratioSpolyToRaDec))+"]";
|
289
|
|
290
|
}
|
291
|
|
292
|
geometry = "\"coordinates\":[["+spolyConverted[3]+","+spolyConverted[2]+","+spolyConverted[1]+","+spolyConverted[0]+","+spolyConverted[3]+"]],";
|
293
|
geometry += "\"referencesystem\": \""+coordRef+"\",\"type\": \"Polygon\"";
|
294
|
}
|
295
|
|
296
|
return geometry;
|
297
|
}
|
298
|
|
299
|
private String setServices(String urlDownFits){
|
300
|
String services = new String();
|
301
|
services += "\"download\":{ \"mimetype\":\""+mimeTypeFits+"\",";
|
302
|
services += "\"url\":\""+urlDownFits+"\"}";
|
303
|
return services;
|
304
|
}
|
305
|
}
|