Project

General

Profile

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

git_sitools_idoc / hesiod / javaExt / src / fr / ias / sitools / astro / resolverName / CorotIdResolverResource.java @ 6552a8ce

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.ias.sitools.astro.resolverName;
20

    
21
import fr.cnes.sitools.astro.representation.GeoJsonRepresentation;
22
import fr.cnes.sitools.common.resource.SitoolsParameterizedResource;
23
import fr.cnes.sitools.dataset.DataSetApplication;
24
import fr.cnes.sitools.dataset.database.DatabaseRequest;
25
import fr.cnes.sitools.dataset.database.DatabaseRequestFactory;
26
import fr.cnes.sitools.dataset.database.DatabaseRequestParameters;
27
import fr.cnes.sitools.dataset.database.common.DataSetExplorerUtil;
28
import fr.cnes.sitools.dataset.model.Column;
29
import fr.cnes.sitools.dataset.model.DataSet;
30
import fr.cnes.sitools.dataset.model.Operator;
31
import fr.cnes.sitools.dataset.model.Predicat;
32
import fr.cnes.sitools.datasource.jdbc.model.AttributeValue;
33
import fr.cnes.sitools.datasource.jdbc.model.Record;
34
import fr.cnes.sitools.extensions.astro.application.opensearch.datamodel.FeatureDataModel;
35
import fr.cnes.sitools.extensions.astro.application.opensearch.datamodel.FeaturesDataModel;
36
import fr.cnes.sitools.extensions.common.AstroCoordinate;
37
import fr.cnes.sitools.plugins.resources.model.ResourceParameter;
38
import fr.cnes.sitools.resources.order.utils.OrderResourceUtils;
39
import java.util.Map;
40
import java.util.logging.Level;
41
import org.restlet.Context;
42
import org.restlet.data.MediaType;
43
import org.restlet.data.Status;
44
import org.restlet.ext.wadl.MethodInfo;
45
import org.restlet.representation.Representation;
46
import org.restlet.representation.Variant;
47
import org.restlet.resource.Get;
48
import org.restlet.resource.ResourceException;
49

    
50

    
51

    
52
public class  CorotIdResolverResource extends SitoolsParameterizedResource{
53
    
54
    String  corotIDValueStr;
55
    Boolean exoBool;
56
    
57
    Double raValue;
58
    Double decValue;
59
    ResourceParameter corotIDParam;
60
    ResourceParameter raParam;
61
    ResourceParameter decParam;
62
    
63
    @Override
64
    public void sitoolsDescribe() {
65
        setName("CorotIdResolverResource");
66
        setDescription("Get Star coordinates from corotID");
67
    }
68

    
69
    @Override
70
    public void doInit() {
71
        super.doInit();
72
        this.corotIDValueStr = (String) this.getRequestAttributes().get("corotID");
73
        this.exoBool = Boolean.parseBoolean(getModel().getParametersMap().get("exoBool").getValue());
74
        
75
        try{
76
          int corotIdValuInt = Integer.parseInt(corotIDValueStr);
77
        }catch(NumberFormatException e){
78
            throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "CorotID must be a number.");
79
        }
80
        if(this.corotIDValueStr.length() != 9 && exoBool){
81
            throw new ResourceException(Status.CLIENT_ERROR_BAD_REQUEST, "CorotID must be composed of 9 digits.");
82
        }
83
    }
84

    
85
    /**
86
    * Get HTML
87
    * 
88
    * @return Representation the HTML result
89
    */
90
    @Get
91
    public Representation getCorotIdResolver() {
92
        return execute();
93
    }
94

    
95
  @Override
96
  protected void describeGet(MethodInfo info) {
97
    this.addInfo(info);
98
    info.setIdentifier("get ra and dec coordinates from corotID");
99
    info.setDocumentation("Method to retrieve coordinates of a star from its corotID");
100
    addStandardGetRequestInfo(info);
101
    DataSetExplorerUtil.addDatasetExplorerGetRequestInfo(info);
102
    DataSetApplication application = (DataSetApplication) getApplication();
103
    DataSetExplorerUtil.addDatasetExplorerGetFilterInfo(info, application.getFilterChained());
104
    addStandardResponseInfo(info);
105
    addStandardInternalServerErrorInfo(info);
106
  }
107

    
108
  @Override
109
  protected Representation get(Variant variant) {
110
    Representation repr = super.get();
111
    if(repr == null){
112
        throw new ResourceException(Status.CLIENT_ERROR_NOT_FOUND,"No reccord matches with your query");
113
    }
114
    repr.setMediaType(MediaType.APPLICATION_JSON);
115
    return repr;
116
  }
117

    
118
  /**
119
   * Execute the request and return a Representation
120
   * 
121
   * @return the HTML representation
122
   */
123
  private Representation execute() {
124
    
125
    
126
    Representation repr = null;
127
    
128
    // Get context
129
    Context context = getContext();
130
 
131
    // generate the DatabaseRequest
132
    DataSetApplication datasetApp = (DataSetApplication) getApplication();
133
    DataSetExplorerUtil dsExplorerUtil = new DataSetExplorerUtil(datasetApp, getRequest(), getContext());
134

    
135

    
136
    this.corotIDParam = this.getModel().getParameterByName("corotIdCol");
137
    this.raParam = this.getModel().getParameterByName("raCol");
138
    this.decParam = this.getModel().getParameterByName("decCol");
139
    
140

    
141
    // Get request parameters
142
    if (datasetApp.getConverterChained() != null) {
143
      datasetApp.getConverterChained().getContext().getAttributes().put("REQUEST", getRequest());
144
    }
145
    // Get DatabaseRequestParameters
146
    DatabaseRequestParameters params = dsExplorerUtil.getDatabaseParams();
147
    DataSet ds = datasetApp.getDataSet();
148
    createQueryCorotID(params,ds);    
149
    DatabaseRequest databaseRequest = DatabaseRequestFactory.getDatabaseRequest(params);
150

    
151
      try {
152

    
153
          if (params.getDistinct()) {
154
            databaseRequest.createDistinctRequest();
155
        }else {
156
            databaseRequest.createRequest();
157
        }
158
        // Recuperation dans la base du fichiers fits
159
        context.getLogger().log(Level.INFO, "nbr de resultat : "+databaseRequest.getCount());
160
        while (databaseRequest.nextResult()) {
161
            Record rec = databaseRequest.getRecord();
162
            this.raValue = Double.parseDouble(OrderResourceUtils.getInParam(raParam, rec).getValue().toString());
163
            this.decValue = Double.parseDouble(OrderResourceUtils.getInParam(decParam, rec).getValue().toString());
164

    
165
            Map a = getDataModel();
166
            repr = new GeoJsonRepresentation(a);
167
            
168
        }
169
      } catch (Exception e) {
170
           throw new ResourceException(Status.SERVER_ERROR_INTERNAL,e.getMessage());
171
      }
172
    return repr;
173
  }
174
  
175
  public Map getDataModel(){
176
      final FeaturesDataModel features = new FeaturesDataModel();
177
      final FeatureDataModel feature = new FeatureDataModel();
178
      AstroCoordinate a = new AstroCoordinate(this.raValue,this.decValue);
179
      
180
      feature.createGeometry(String.format("[%s,%s]", this.raValue,this.decValue),"Point");
181
      feature.createCrs(a.getCoordinateSystem().getCrs());
182
      feature.addProperty("credits", "CDS");
183
      feature.addProperty("identifier", "CDS0");
184
      features.addFeature(feature);
185
      return features.getFeatures();
186
  }
187
  
188
  private void createQueryCorotID(DatabaseRequestParameters params, DataSet ds){
189
      Column corotIDCol = ds.findByColumnAlias(this.corotIDParam.getValue());
190
      Predicat pred = new Predicat();
191
      pred.setLogicOperator("AND");
192
      pred.setLeftAttribute(corotIDCol);
193
      pred.setCompareOperator(Operator.EQ);
194
      pred.setRightValue(this.corotIDValueStr);
195
      params.getPredicats().add(pred);
196
      if(exoBool){
197
        pred = new Predicat();
198
        Column en2WinDescCol = ds.findByColumnAlias("en2_windescriptor");
199
        pred.setLogicOperator("AND");
200
        pred.setLeftAttribute(en2WinDescCol);
201
        pred.setCompareOperator(Operator.EQ);
202
        pred.setRightValue("false");
203
        params.getPredicats().add(pred);
204
      }
205
  }
206
}