1
|
|
2
|
|
3
|
|
4
|
|
5
|
|
6
|
|
7
|
|
8
|
|
9
|
|
10
|
|
11
|
|
12
|
|
13
|
|
14
|
|
15
|
|
16
|
|
17
|
|
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
|
|
87
|
|
88
|
|
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
|
|
120
|
|
121
|
|
122
|
|
123
|
private Representation execute() {
|
124
|
|
125
|
|
126
|
Representation repr = null;
|
127
|
|
128
|
|
129
|
Context context = getContext();
|
130
|
|
131
|
|
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
|
|
142
|
if (datasetApp.getConverterChained() != null) {
|
143
|
datasetApp.getConverterChained().getContext().getAttributes().put("REQUEST", getRequest());
|
144
|
}
|
145
|
|
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
|
|
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
|
}
|