Project

General

Profile

Download (17.5 KB) Statistics
| Branch: | Revision:

git_sitools_idoc / sitools-idoc / hesiod / astronmy-extension / src / fr / cnes / sitools / astro / vo / sia / SimpleImageAccessInputParameters.java @ 779bac69

1
 /*******************************************************************************
2
 * Copyright 2010-2013 CNES - CENTRE NATIONAL d'ETUDES SPATIALES
3
 *
4
 * This file is part of SITools2.
5
 *
6
 * SITools2 is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * SITools2 is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with SITools2.  If not, see <http://www.gnu.org/licenses/>.
18
 ******************************************************************************/
19
package fr.cnes.sitools.astro.vo.sia;
20

    
21
import fr.cnes.sitools.dataset.DataSetApplication;
22
import fr.cnes.sitools.dataset.dto.ColumnConceptMappingDTO;
23
import fr.cnes.sitools.dictionary.model.Concept;
24
import fr.cnes.sitools.extensions.common.InputsValidation;
25
import fr.cnes.sitools.extensions.common.NotNullAndNotEmptyValidation;
26
import fr.cnes.sitools.extensions.common.NumberArrayValidation;
27
import fr.cnes.sitools.extensions.common.SpatialGeoValidation; 
28
import fr.cnes.sitools.extensions.common.StatusValidation;
29
import fr.cnes.sitools.extensions.common.Validation;
30
import fr.cnes.sitools.plugins.resources.model.ResourceModel;
31
import fr.cnes.sitools.util.Util;
32
import java.math.BigInteger;
33
import java.util.ArrayList;
34
import java.util.Collections;
35
import java.util.HashMap;
36
import java.util.List;
37
import java.util.Map;
38
import java.util.Set;
39
import java.util.logging.Level;
40
import java.util.logging.Logger;
41
import net.ivoa.xml.votable.v1.AnyTEXT;
42
import net.ivoa.xml.votable.v1.DataType;
43
import net.ivoa.xml.votable.v1.Field;
44
import net.ivoa.xml.votable.v1.Info;
45
import net.ivoa.xml.votable.v1.Option;
46
import net.ivoa.xml.votable.v1.Param;
47
import net.ivoa.xml.votable.v1.Values;
48
import org.restlet.Context;
49
import org.restlet.Request;
50

    
51

    
52
/**
53
 * Input parameters for SIA.
54
 *
55
 * @author Jean-Crhistophe Malapert <jean-christophe.malapert@cnes.fr>
56
 */
57
import fr.cnes.sitools.common.exception.SitoolsException;
58
import fr.cnes.sitools.dataset.dto.DictionaryMappingDTO;
59
public class SimpleImageAccessInputParameters implements DataModelInterface {
60
  /**
61
   * Logger.
62
   */
63
  private static final Logger LOG = Logger.getLogger(SimpleImageAccessInputParameters.class.getName());
64
  /**
65
   * Init value for right ascension parameter of the user input.
66
   */
67
  private transient double ra = 0.0;
68
  /**
69
   * Init value for declination parameter of the user input.
70
   */
71
  private transient double dec = 0.0;
72
  /**
73
   * Array that stores the size parameter of the user input.
74
   */
75
  private transient double[] size;
76
  /**
77
   * Default value for the verb parameter of the user input.
78
   */
79
  private transient int verb = 0;
80
  /**
81
   * Data model that stores the metadata response of the service.
82
   */
83
  private final transient Map dataModel = new HashMap();
84
  /**
85
   * Request.
86
   */
87
  private final transient Request request;
88
  /**
89
   * Context.
90
   */
91
  private final transient Context context;
92
  /**
93
   * Application where this resources is linked.
94
   */
95
  private final transient DataSetApplication datasetApp;
96
  /**
97
   * Configuration parameters of this resource.
98
   */
99
  private final transient ResourceModel resourceModel;
100

    
101
  /**
102
   * Constructs the objet that returns the metadata of the service.
103
   * @param datasetAppVal application
104
   * @param requestVal request
105
   * @param contextVal context
106
   * @param resourceModelVal configuration parameters
107
   */
108
  public SimpleImageAccessInputParameters(final DataSetApplication datasetAppVal, final Request requestVal, final Context contextVal, final ResourceModel resourceModelVal) {
109
    this.datasetApp = datasetAppVal;
110
    this.context = contextVal;
111
    this.request = requestVal;
112
    this.resourceModel = resourceModelVal;
113
    final String posInput = this.request.getResourceRef().getQueryAsForm().getFirstValue(SimpleImageAccessProtocolLibrary.POS);
114
    final String sizeInput = this.request.getResourceRef().getQueryAsForm().getFirstValue(SimpleImageAccessProtocolLibrary.SIZE);
115
    final String format = this.request.getResourceRef().getQueryAsForm().getFirstValue(SimpleImageAccessProtocolLibrary.FORMAT);
116
    final String intersect = this.request.getResourceRef().getQueryAsForm().getFirstValue(SimpleImageAccessProtocolLibrary.INTERSECT);
117
    final String verbosity = this.request.getResourceRef().getQueryAsForm().getFirstValue(SimpleImageAccessProtocolLibrary.VERB);
118
    //TODO check the differentParameters
119
    if(posInput == null && sizeInput == null){
120
        Info info = new Info();
121
        info.setName("QUERY_STATUS");
122
        info.setValueAttribute("ERROR");
123
        final List<Info> listInfos = new ArrayList<Info>();
124
        listInfos.add(info);
125
        this.dataModel.put("infos", listInfos);
126
        if(format.equalsIgnoreCase("METADATA")){
127
            fillMetadataFormat();
128
        }
129
    }else{
130
        if (Util.isSet(format) && format.equals(SimpleImageAccessProtocolLibrary.ParamStandardFormat.METADATA.name())) {
131
            fillMetadataFormat();
132
        } else {
133
            checkInputParameters(posInput, sizeInput);
134
        }
135
    }
136
  }
137

    
138
  /**
139
   * Fills metadata response.
140
   */
141
  private void fillMetadataFormat() {
142
      
143
    this.dataModel.put("description", this.resourceModel.getParameterByName(SimpleImageAccessProtocolLibrary.DESCRIPTION).getValue());
144

    
145
    final Info info = new Info();
146
    info.setName("QUERY_STATUS");
147
    info.setValueAttribute("OK");
148
    final List<Info> listInfos = new ArrayList<Info>();
149
    listInfos.add(info);
150
    this.dataModel.put("infos", listInfos);
151
   
152
    final List<Param> listParam = new ArrayList<Param>();
153
    Param param = new Param();
154
    param.setName("INPUT:POS");
155
    param.setValue("0,0");
156
    param.setDatatype(DataType.DOUBLE);
157
    AnyTEXT anyText = new AnyTEXT();
158
    anyText.getContent().add("Search Position in the form ra,dec where ra and dec are given in decimal degrees in the ICRS coordinate system.");
159
    param.setDESCRIPTION(anyText);
160
    listParam.add(param);
161

    
162
    param = new Param();
163
    param.setName("INPUT:SIZE");
164
    param.setValue("0.05");
165
    param.setDatatype(DataType.DOUBLE);
166
    anyText = new AnyTEXT();
167
    anyText.getContent().add("Size of search region in the RA and Dec directions.");
168
    param.setDESCRIPTION(anyText);
169
    listParam.add(param);
170

    
171
    param = new Param();
172
    param.setName("INPUT:FORMAT");
173
    param.setValue(SimpleImageAccessProtocolLibrary.ParamStandardFormat.ALL.name());
174
    param.setDatatype(DataType.CHAR);
175
    param.setArraysize("*");
176
    anyText = new AnyTEXT();
177
    anyText.getContent().add("Requested format of images.");
178
    param.setDESCRIPTION(anyText);
179
    final List<String> formatList = SimpleImageAccessProtocolLibrary.ParamStandardFormat.getCtes();
180
    final Values values = new Values();
181
    for (String formatIter : formatList) {
182
      final Option option = new Option();
183
      option.setValue(formatIter);
184
      values.getOPTION().add(option);
185
    }
186
    param.setVALUES(values);
187
    //TODO : le faire pour chaque format
188
    listParam.add(param);
189
    
190
    param = new Param();
191
    param.setName("INPUT:INTERSECT");
192
    param.setValue(this.resourceModel.getParameterByName(SimpleImageAccessProtocolLibrary.INTERSECT).getValue());
193
    param.setDatatype(DataType.CHAR);
194
    anyText = new AnyTEXT();
195
    anyText.getContent().add("Choice of overlap with requested region.");
196
    param.setDESCRIPTION(anyText);
197
    listParam.add(param);
198

    
199
    param = new Param();
200
    param.setName("INPUT:VERB");
201
    param.setValue(this.resourceModel.getParameterByName(SimpleImageAccessProtocolLibrary.VERB).getValue());
202
    param.setDatatype(DataType.INT);
203
    anyText = new AnyTEXT();
204
    anyText.getContent().add("Verbosity level, controlling the number of columns returned.");
205
    param.setDESCRIPTION(anyText);
206
    listParam.add(param);
207

    
208
    dataModel.put("params", listParam);
209
    //******************************************************************************************************************
210
    //******************************************************************************************************************
211
    //******************************************************************************************************************
212
    String dictionaryName = resourceModel.getParameterByName(SimpleImageAccessProtocolLibrary.DICTIONARY).getValue();
213
    final List<String> columnList = new ArrayList<String>();
214
    List<Field> fieldList = new ArrayList<Field>();
215
    try {
216
        List<ColumnConceptMappingDTO> mappingList = getDicoFromConfiguration(datasetApp, dictionaryName);
217
        setFields(fieldList, columnList, mappingList);
218
    }catch (SitoolsException ex) {
219
             
220
    }
221
    
222
  }
223

    
224
  /**
225
   * Checks input parameters.
226
   * @param posInput input parameter for POS
227
   * @param sizeInput input parameter for SIZE
228
   */
229
  private void checkInputParameters(final String posInput, final String sizeInput) {
230
    final List<Info> infos = new ArrayList<Info>();
231
    final Map<String, String> validationMap = new HashMap<String, String>();
232
    validationMap.put(SimpleImageAccessProtocolLibrary.POS, posInput);
233
    validationMap.put(SimpleImageAccessProtocolLibrary.SIZE, sizeInput);    
234
    Validation validation = new InputsValidation(validationMap);
235
    validation = new NotNullAndNotEmptyValidation(validation, SimpleImageAccessProtocolLibrary.POS);
236
    validation = new NotNullAndNotEmptyValidation(validation, SimpleImageAccessProtocolLibrary.SIZE);
237
    if(validation.validate().isValid()){
238
        validation = new SpatialGeoValidation(validation, SimpleImageAccessProtocolLibrary.POS, 0, 1, new double[]{0.0, 360.0}, new double[]{-90.0, 90.0});
239
        // LA LIGNE SUIVANTE A ETE AJOUTEE POUR VERIFIER QUE LA SIZE EST BIEN UN ARRAY DE NOMBRE
240
        validation = new NumberArrayValidation(validation, SimpleImageAccessProtocolLibrary.SIZE, ",", 1, 2);
241
        //-----------------------------------------------------------------------------------------------
242
    }  
243
    StatusValidation status = validation.validate();
244
    if (status.isValid()) {
245
        final String pos = validation.getMap().get(SimpleImageAccessProtocolLibrary.POS);
246
        final String[] arrayPos = pos.split(",");
247
        this.ra = Double.valueOf(arrayPos[0]);
248
        this.dec = Double.valueOf(arrayPos[1]);
249
        final String size = validation.getMap().get(SimpleImageAccessProtocolLibrary.SIZE);
250
        final String[] arraySize = size.split(",");
251
        if(arraySize.length == 1) {
252
            this.size = new double[1];
253
            this.size[0] = Double.valueOf(arraySize[0]);
254
        } else {
255
            this.size = new double[2];
256
            this.size[0] = Double.valueOf(arraySize[0]);
257
            this.size[1] = Double.valueOf(arraySize[1]);
258
        }
259
        
260
    } else {
261
        Info info = new Info();
262
        info.setName("QUERY_STATUS");
263
        info.setValueAttribute("ERROR");
264
        infos.add(info);
265
        final Map<String, String> errors = status.getMessages();
266
        final Set<Map.Entry<String, String>> entries = errors.entrySet();        
267
        for (Map.Entry<String, String> entry : entries) {
268
            info = new Info();
269
            info.setID(entry.getKey());
270
            info.setName("Error in " + entry.getKey());
271
            info.setValueAttribute("Error in input " + entry.getKey() + ": " + entry.getValue());
272
            infos.add(info);
273
            LOG.log(Level.FINEST, "{0}: {1}", new Object[]{entry.getKey(), entry.getValue()});            
274
        }
275
        
276
    }
277
    
278
    if (!infos.isEmpty()) {
279
      this.dataModel.put("infos", infos);
280
    }
281
  }
282

    
283
  @Override
284
  public final Map getDataModel() {
285
    return Collections.unmodifiableMap(this.dataModel);
286
  }
287

    
288
  /**
289
   * Get Ra.
290
   *
291
   * @return Ra
292
   */
293
  public final double getRa() {
294
    return this.ra;
295
  }
296

    
297
  /**
298
   * Get Dec.
299
   *
300
   * @return Dec
301
   */
302
  public final double getDec() {
303
    return this.dec;
304
  }
305

    
306
  /**
307
   * Get Sr.
308
   *
309
   * @return Sr
310
   */
311
  public final double[] getSize() {
312
      final double[] copySize = new double[this.size.length];
313
      System.arraycopy(this.size, 0, copySize, 0, this.size.length);
314
      return copySize;
315
  }
316

    
317
  /**
318
   * Get the request.
319
   *
320
   * @return Request
321
   */
322
  public final Request getRequest() {
323
    return this.request;
324
  }
325

    
326
  /**
327
   * Get the context.
328
   *
329
   * @return Context
330
   */
331
  public final Context getContext() {
332
    return this.context;
333
  }
334

    
335
  /**
336
   * Get DatasetApplication.
337
   *
338
   * @return Dataset application
339
   */
340
  public final DataSetApplication getDatasetApplication() {
341
    return this.datasetApp;
342
  }
343

    
344
  /**
345
   * Get verb.
346
   * @return verb
347
   */
348
  public final int getVerb() {
349
    return this.verb;
350
  }
351
  
352
  //----------------------------------------------------------------------------
353
  //----------------------------------------------------------------------------
354
  //----------------------------------------------------------------------------
355
  /**
356
   * Set Fields and columnSqlAliasList.
357
   *
358
   * @param fieldList List of fields to display on the VOTable
359
   * @param columnList List of SQL column
360
   * @param mappingList List of SQL column/concept
361
   */
362
  private void setFields(final List<Field> fieldList, final List<String> columnList, final List<ColumnConceptMappingDTO> mappingList) {
363

    
364
    for (ColumnConceptMappingDTO mappingIter : mappingList) {
365

    
366
      String id = null;
367
      String name = null;
368
      String ucd = null;
369
      String utype = null;
370
      String ref = null;
371
      String datatype = null;
372
      String width = null;
373
      String precision = null;
374
      String unit = null;
375
      String type = null;
376
      String xtype = null;
377
      String arraysize = null;
378
      String descriptionValue = null;
379
      columnList.add(mappingIter.getColumnAlias());
380
      final Concept concept = mappingIter.getConcept();
381
      if (concept.getName() != null) {
382
        name = concept.getName();
383
      }
384
      if (concept.getPropertyFromName("ID").getValue() != null) {
385
        id = concept.getPropertyFromName("ID").getValue();
386
      }
387
      if (concept.getPropertyFromName("ucd").getValue() != null) {
388
        ucd = concept.getPropertyFromName("ucd").getValue();
389
      }
390
      if (concept.getPropertyFromName("utype").getValue() != null) {
391
        utype = concept.getPropertyFromName("utype").getValue();
392
      }
393
      if (concept.getPropertyFromName("ref").getValue() != null) {
394
        ref = concept.getPropertyFromName("ref").getValue();
395
      }
396
      if (concept.getPropertyFromName("datatype").getValue() != null) {
397
        datatype = concept.getPropertyFromName("datatype").getValue();
398
      }
399
      if (concept.getPropertyFromName("width").getValue() != null) {
400
        width = concept.getPropertyFromName("width").getValue();
401
      }
402
      if (concept.getPropertyFromName("precision").getValue() != null) {
403
        precision = concept.getPropertyFromName("precision").getValue();
404
      }
405
      if (concept.getPropertyFromName("unit").getValue() != null) {
406
        unit = concept.getPropertyFromName("unit").getValue();
407
      }
408
      if (concept.getPropertyFromName("type").getValue() != null) {
409
        type = concept.getPropertyFromName("type").getValue();
410
      }
411
      if (concept.getPropertyFromName("xtype").getValue() != null) {
412
        xtype = concept.getPropertyFromName("xtype").getValue();
413
      }
414
      if (concept.getPropertyFromName("arraysize").getValue() != null) {
415
        arraysize = concept.getPropertyFromName("arraysize").getValue();
416
      }
417
      if (concept.getDescription() != null) {
418
        descriptionValue = concept.getDescription();
419
      }
420
      final Field field = new Field();
421
      field.setID(id);
422
      field.setName(name);
423
      field.setUcd(ucd);
424
      field.setUtype(utype);
425
      field.setRef(ref);
426
      field.setDatatype(DataType.fromValue(datatype));
427
      if (width != null) {
428
        field.setWidth(BigInteger.valueOf(Long.valueOf(width)));
429
      }
430
      field.setPrecision(precision);
431
      field.setUnit(unit);
432
      field.setType(type);
433
      field.setXtype(xtype);
434
      field.setArraysize(arraysize);
435
      final AnyTEXT anyText = new AnyTEXT();
436
      anyText.getContent().add(descriptionValue);
437
      field.setDESCRIPTION(anyText);
438
      fieldList.add(field);
439
    }
440
    dataModel.put("fields", fieldList);
441
  }
442
  
443
  /**
444
   * Provide the mapping between SQL column/concept for a given dictionary.
445
   *
446
   * @param datasetApp Application where this service is attached
447
   * @param dicoToFind Dictionary name to find
448
   * @return Returns a mapping SQL column/Concept
449
   * @throws SitoolsException No mapping has been done or cannot find the dico
450
   */
451
  protected List<ColumnConceptMappingDTO> getDicoFromConfiguration(final DataSetApplication datasetApp,
452
          final String dicoToFind) throws SitoolsException {
453
    List<ColumnConceptMappingDTO> colConceptMappingDTOList = null;
454

    
455
    // Get the list of dictionnaries related to the datasetApplication
456
    final List<DictionaryMappingDTO> dicoMappingList = datasetApp.getDictionaryMappings();
457
    if (!Util.isSet(dicoMappingList) || dicoMappingList.isEmpty()) {
458
      throw new SitoolsException("No mapping with VO concepts has been done. please contact the administrator");
459
    }
460

    
461
    // For each dictionary, find the interesting one and return the mapping SQLcolumn/concept
462
    for (DictionaryMappingDTO dicoMappingIter : dicoMappingList) {
463
      final String dicoName = dicoMappingIter.getDictionaryName();
464
      if (dicoToFind.equals(dicoName)) {
465
        colConceptMappingDTOList = dicoMappingIter.getMapping();
466
        break;
467
      }
468
    }
469
    return colConceptMappingDTOList;
470
  }
471
  
472
  
473
  
474
}