Project

General

Profile

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

git_sitools_idoc / sitools-idoc / hesiod / astronmy-extension / src / fr / cnes / sitools / astro / vo / conesearch / ConeSearchInputParameters.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.conesearch;
20

    
21
import fr.cnes.sitools.dataset.DataSetApplication;
22
import fr.cnes.sitools.extensions.common.InputsValidation;
23
import fr.cnes.sitools.extensions.common.NotNullAndNotEmptyValidation;
24
import fr.cnes.sitools.extensions.common.RangeValidation;
25
import fr.cnes.sitools.extensions.common.StatusValidation;
26
import fr.cnes.sitools.extensions.common.Validation;
27
import java.util.ArrayList;
28
import java.util.Collections;
29
import java.util.HashMap;
30
import java.util.List;
31
import java.util.Map;
32
import java.util.Map.Entry;
33
import java.util.Set;
34
import java.util.logging.Level;
35
import java.util.logging.Logger;
36
import net.ivoa.xml.votable.v1.Info;
37
import org.restlet.Context;
38
import org.restlet.Request;
39

    
40
/**
41
 * This object provides methods to handle input parameters for the cone search protocol.
42
 *
43
 * @author Jean-Christophe Malapert
44
 */
45
public class ConeSearchInputParameters implements ConeSearchDataModelInterface {
46

    
47
  /**
48
   * Logger.
49
   */
50
  private static final Logger LOG = Logger.getLogger(ConeSearchInputParameters.class.getName());
51
  /**
52
   * Default verbosity mode.
53
   */
54
  private static final int DEFAULT_VERBOSITY_MODE = 0;
55
  /**
56
   * Verbosity mode.
57
   */
58
  private transient int verb = DEFAULT_VERBOSITY_MODE;
59
  /**
60
   * Data model.
61
   */
62
  private final transient Map dataModel = new HashMap();
63
  /**
64
   * User request.
65
   */
66
  private final transient Request request;
67
  /**
68
   * Application context.
69
   */
70
  private final transient Context context;
71
  /**
72
   * Application.
73
   */
74
  private final transient DataSetApplication datasetApp;
75
  /**
76
   * Right ascension of the cone center.
77
   */
78
  private transient double rightAscension;
79
  /**
80
   * Declination of the cone center.
81
   */
82
  private transient double declination;
83
  /**
84
   * Radius of the cone.
85
   */
86
  private transient double radius;
87

    
88
  /**
89
   * Constructor.
90
   *
91
   * @param datasetAppVal dataset application
92
   * @param requestVal Request
93
   * @param contextVal Context
94
   * @param maxSr maxSr for the cone search
95
   * @param verbosity verbosity
96
   * @param verbVal verbosity level
97
   */
98
  public ConeSearchInputParameters(final DataSetApplication datasetAppVal, final Request requestVal, final Context contextVal,
99
          final double maxSr, final boolean verbosity, final int verbVal) {
100
    this.datasetApp = datasetAppVal;
101
    this.context = contextVal;
102
    this.request = requestVal;
103
    final String raInput = this.request.getResourceRef().getQueryAsForm().getFirstValue(ConeSearchProtocolLibrary.RA);
104
    final String decInput = this.request.getResourceRef().getQueryAsForm().getFirstValue(ConeSearchProtocolLibrary.DEC);
105
    final String srInput = this.request.getResourceRef().getQueryAsForm().getFirstValue(ConeSearchProtocolLibrary.SR);
106
    final String verbInput = this.request.getResourceRef().getQueryAsForm().getFirstValue(ConeSearchProtocolLibrary.VERB);
107
    if(raInput == null && decInput == null && srInput == null){
108
        Info info = new Info();
109
        info.setName("QUERY_STATUS");
110
        info.setValueAttribute("ERROR");
111
        info.setValue("RA, DEC and SR must be not null");
112
        final List<Info> listInfos = new ArrayList<Info>();
113
        listInfos.add(info);
114
        this.dataModel.put("infos", listInfos);
115
    }else{
116
        checkInputParameters(raInput, decInput, srInput, maxSr, verbInput, verbosity, verbVal);
117
    }
118
  }
119

    
120
  /**
121
   * Check input parameters.
122
   *
123
   * @param raInput Ra
124
   * @param decInput Dec
125
   * @param srInput Sr
126
   * @param maxSr maxSr
127
   * @param verbosity verbosity
128
   * @param verbInput user inpur verb
129
   * @param verbVal verbosity from admin
130
   */
131
  private void checkInputParameters(final String raInput, final String decInput, final String srInput, final double maxSr,
132
          final String verbInput, final boolean verbosity, final int verbVal) {
133
    final List<Info> infos = new ArrayList<Info>();
134
    final Map<String, String> validationMap = new HashMap<String, String>();
135
    validationMap.put(ConeSearchProtocolLibrary.RA, raInput);
136
    validationMap.put(ConeSearchProtocolLibrary.DEC, decInput);
137
    validationMap.put(ConeSearchProtocolLibrary.SR, srInput);
138
    validationMap.put(ConeSearchProtocolLibrary.VERB, verbInput);
139
    validationMap.put(ConeSearchProtocolLibrary.VERBOSITY, String.valueOf(verbosity));
140
    Validation validation = new InputsValidation(validationMap);
141
    validation = new RangeValidation(validation, ConeSearchProtocolLibrary.RA, ConeSearchProtocolLibrary.RA_MIN, ConeSearchProtocolLibrary.RA_MAX);
142
    validation = new RangeValidation(validation, ConeSearchProtocolLibrary.DEC, ConeSearchProtocolLibrary.DEC_MIN, ConeSearchProtocolLibrary.DEC_MAX);
143
    validation = new RangeValidation(validation, ConeSearchProtocolLibrary.SR, 0, maxSr);
144
    validation = new NotNullAndNotEmptyValidation(validation, ConeSearchProtocolLibrary.VERBOSITY);
145
    validation = new NotNullAndNotEmptyValidation(validation, ConeSearchProtocolLibrary.VERB, String.valueOf(verbVal));
146
    final StatusValidation status = validation.validate();
147
    if (status.isValid()) {
148
        final Map<String, String> input = validation.getMap();
149
        this.rightAscension = Double.valueOf(input.get(ConeSearchProtocolLibrary.RA));
150
        this.declination = Double.valueOf(input.get(ConeSearchProtocolLibrary.DEC));
151
        this.radius = Double.valueOf(input.get(ConeSearchProtocolLibrary.SR));
152
        this.verb = Integer.valueOf(input.get(ConeSearchProtocolLibrary.VERB));
153
    } else {
154
        Info infoError = new Info();
155
        infoError.setName("QUERY_STATUS");
156
        infoError.setValueAttribute("ERROR");
157
        infos.add(infoError);
158
        final Map<String, String> errors = status.getMessages();
159
        final Set<Entry<String, String>> entries = errors.entrySet();
160
        for (Entry<String, String> entry : entries) {
161
            final Info info = new Info();
162
            info.setID(entry.getKey());
163
            info.setName("Error in " + entry.getKey());
164
            info.setValueAttribute("Error in input " + entry.getKey() + ": " + entry.getValue());
165
            infos.add(info);
166
            LOG.log(Level.FINEST, "{0}: {1}", new Object[]{entry.getKey(), entry.getValue()});
167
        }
168
    }
169
    if (!infos.isEmpty()) {
170
      this.dataModel.put("infos", infos);
171
    }
172
  }
173

    
174
  /**
175
   * Get Ra.
176
   *
177
   * @return Ra
178
   */
179
  public final double getRa() {
180
    return this.rightAscension;
181
  }
182

    
183
  /**
184
   * Get Dec.
185
   *
186
   * @return Dec
187
   */
188
  public final double getDec() {
189
    return this.declination;
190
  }
191

    
192
  /**
193
   * Get Sr.
194
   *
195
   * @return Sr
196
   */
197
  public final double getSr() {
198
    return this.radius;
199
  }
200

    
201
  /**
202
   * Get verbosity level.
203
   *
204
   * @return verbosity level
205
   */
206
  public final int getVerb() {
207
    return this.verb;
208
  }
209

    
210
  @Override
211
  public final Map getDataModel() {
212
    return Collections.unmodifiableMap(this.dataModel);
213
  }
214

    
215
  /**
216
   * Returns the request.
217
   *
218
   * @return Request
219
   */
220
  public final Request getRequest() {
221
    return this.request;
222
  }
223

    
224
  /**
225
   * Returns the context.
226
   *
227
   * @return Context
228
   */
229
  public final Context getContext() {
230
    return this.context;
231
  }
232

    
233
  /**
234
   * Get DatasetApplication.
235
   *
236
   * @return Dataset application
237
   */
238
  public final DataSetApplication getDatasetApplication() {
239
    return this.datasetApp;
240
  }
241
}