Commit 5b999f8fe759252318a697ac4a4d700cb51b175e

Authored by Nathanael Jourdane
1 parent 5e6b2228
Exists in master

Fix #12

src/main/java/eu/omp/irap/vespa/epntapclient/EpnTapGet.java 0 → 100644
... ... @@ -0,0 +1,64 @@
  1 +/*
  2 + * This file is a part of EpnTAPClient.
  3 + * This program aims to provide EPN-TAP support for software clients, like CASSIS spectrum analyzer.
  4 + * See draft specifications: https://voparis-confluence.obspm.fr/pages/viewpage.action?pageId=559861
  5 + * Copyright (C) 2016 Institut de Recherche en Astrophysique et Planétologie.
  6 + *
  7 + * This program is free software: you can
  8 + * redistribute it and/or modify it under the terms of the GNU General Public License as published
  9 + * by the Free Software Foundation, either version 3 of the License, or (at your option) any later
  10 + * version. This program is distributed in the hope that it will be useful, but WITHOUT ANY
  11 + * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  12 + * PURPOSE. See the GNU General Public License for more details. You should have received a copy of
  13 + * the GNU General Public License along with this program. If not, see
  14 + * <http://www.gnu.org/licenses/>.
  15 + */
  16 +
  17 +package eu.omp.irap.vespa.epntapclient;
  18 +
  19 +import java.util.HashMap;
  20 +import java.util.Map;
  21 +import java.util.logging.Logger;
  22 +
  23 +import com.google.gson.JsonArray;
  24 +import com.google.gson.JsonObject;
  25 +
  26 +import eu.omp.irap.vespa.votable.utils.CantSendQueryException;
  27 +import eu.omp.irap.vespa.votable.utils.Network;
  28 +
  29 +/**
  30 + * @author N. Jourdane
  31 + */
  32 +public class EpnTapGet {
  33 +
  34 + /** The logger for the class EpnTapGetQueries. */
  35 + private static final Logger logger = Logger.getLogger(EpnTapGet.class.getName());
  36 +
  37 + /** The URL of the resolver used for the `target name` field. */
  38 + private static final String RESOLVER_URL = "http://voparis-registry.obspm.fr/ssodnet/1/autocomplete";
  39 +
  40 +
  41 + /**
  42 + * The method used to get target names propositions by asking to the resolver.
  43 + *
  44 + * @param begining The beginning of the target_name.
  45 + * @return An array of Strings corresponding to the target names got.
  46 + * @throws CantSendQueryException
  47 + * @throws CantGetXMLException If the resolver do not work.
  48 + */
  49 + public static String[] getTargetNames(String begining) throws CantSendQueryException {
  50 + Map<String, String> params = new HashMap<>();
  51 + params.put("q", "\"" + begining + "\"");
  52 +
  53 + String query = Network.buildQuery(RESOLVER_URL, params);
  54 + JsonObject root = Network.readJson(query);
  55 + int count = Integer.parseInt(root.get("count").toString());
  56 + String[] targetNames = new String[count];
  57 + JsonArray hits = root.getAsJsonArray("hits");
  58 + for (int i = 0; i < count; i++) {
  59 + JsonObject elmt = hits.get(i).getAsJsonObject();
  60 + targetNames[i] = elmt.get("name").toString().replace("\"", "");
  61 + }
  62 + return targetNames;
  63 + }
  64 +}
... ...
src/main/java/eu/omp/irap/vespa/epntapclient/gui/ParamField.java
... ... @@ -24,10 +24,8 @@ import java.text.DateFormat;
24 24 import java.text.ParseException;
25 25 import java.text.SimpleDateFormat;
26 26 import java.util.ArrayList;
27   -import java.util.HashMap;
28 27 import java.util.List;
29 28 import java.util.Locale;
30   -import java.util.Map;
31 29 import java.util.logging.Level;
32 30 import java.util.logging.Logger;
33 31  
... ... @@ -40,11 +38,8 @@ import javax.swing.SwingUtilities;
40 38 import javax.swing.event.DocumentEvent;
41 39 import javax.swing.event.DocumentListener;
42 40  
43   -import com.google.gson.JsonArray;
44   -import com.google.gson.JsonObject;
45   -
  41 +import eu.omp.irap.vespa.epntapclient.EpnTapGet;
46 42 import eu.omp.irap.vespa.votable.utils.CantSendQueryException;
47   -import eu.omp.irap.vespa.votable.utils.Network;
48 43  
49 44 /**
50 45 * A field used to set a service parameter to build the query (in the parameter panel). ParamField
... ... @@ -74,9 +69,6 @@ public abstract class ParamField extends JPanel {
74 69 /** The preferred label width. */
75 70 private static final int LABEL_WIDTH = 140;
76 71  
77   - /** The URL of the resolver used for the `target name` field. */
78   - private static final String RESOLVER_URL = "http://voparis-registry.obspm.fr/ssodnet/1/autocomplete";
79   -
80 72 /** The date format used in the DateRange field */
81 73 private static final String DATE_FORMAT = "dd/MM/yyyy";
82 74  
... ... @@ -384,7 +376,7 @@ public abstract class ParamField extends JPanel {
384 376 lastContent = content;
385 377 comboBox.removeAllItems();
386 378 try {
387   - for (String s : TargetNameField.getItems(content)) {
  379 + for (String s : EpnTapGet.getTargetNames(content)) {
388 380 comboBox.addItem(s);
389 381 }
390 382 } catch (CantSendQueryException e) {
... ... @@ -423,30 +415,6 @@ public abstract class ParamField extends JPanel {
423 415 }
424 416  
425 417 /**
426   - * The method used to get target names propositions by asking to the resolver.
427   - *
428   - * @param begining The beginning of the target_name.
429   - * @return An array of Strings corresponding to the target names got.
430   - * @throws CantSendQueryException
431   - * @throws CantGetXMLException If the resolver do not work.
432   - */
433   - static String[] getItems(String begining) throws CantSendQueryException {
434   - Map<String, String> params = new HashMap<>();
435   - params.put("q", "\"" + begining + "\"");
436   -
437   - String query = Network.buildQuery(ParamField.RESOLVER_URL, params);
438   - JsonObject root = Network.readJson(query);
439   - int count = Integer.parseInt(root.get("count").toString());
440   - String[] targetNames = new String[count];
441   - JsonArray hits = root.getAsJsonArray("hits");
442   - for (int i = 0; i < count; i++) {
443   - JsonObject elmt = hits.get(i).getAsJsonObject();
444   - targetNames[i] = elmt.get("name").toString().replace("\"", "");
445   - }
446   - return targetNames;
447   - }
448   -
449   - /**
450 418 * This method is called each time the field is modified.
451 419 */
452 420 @Override
... ...