Commit 0d0b44fe833462fbfe20efbcfaeaaacf5a259c4a
1 parent
6415d908
Exists in
master
All parameters fields are now parsed to generate the query.
Showing
2 changed files
with
94 additions
and
87 deletions
Show diff stats
src/main/java/eu/omp/irap/vespa/epntapclient/utils/Queries.java
... | ... | @@ -16,27 +16,48 @@ |
16 | 16 | |
17 | 17 | package eu.omp.irap.vespa.epntapclient.utils; |
18 | 18 | |
19 | +import java.util.Map; | |
20 | +import java.util.StringJoiner; | |
21 | + | |
19 | 22 | /** |
20 | 23 | * @author N. Jourdane |
21 | 24 | */ |
22 | 25 | public final class Queries { |
23 | 26 | |
24 | 27 | /** Query to get all EPN-TAP services. */ |
25 | - public static final String GET_EPN_TAP_SERVICES = "SELECT short_name, res_title AS schema_title, table_name, schema_name, ivoid, access_url " | |
26 | - + "FROM rr.resource NATURAL JOIN rr.res_schema NATURAL JOIN rr.res_table NATURAL JOIN rr.interface NATURAL JOIN rr.res_detail NATURAL JOIN rr.capability " | |
28 | + public static final String GET_EPN_TAP_SERVICES = "SELECT short_name, " | |
29 | + + "res_title AS schema_title, table_name, schema_name, ivoid, access_url " | |
30 | + + "FROM rr.resource NATURAL JOIN rr.res_schema NATURAL JOIN rr.res_table " | |
31 | + + "NATURAL JOIN rr.interface NATURAL JOIN rr.res_detail NATURAL JOIN rr.capability " | |
27 | 32 | + "WHERE standard_id='ivo://ivoa.net/std/tap' AND " |
28 | 33 | + "intf_type='vs:paramhttp' AND " |
29 | 34 | + "detail_xpath='/capability/dataModel/@ivo-id' AND " |
30 | 35 | + "1=ivo_nocasematch(detail_value, 'ivo://vopdc.obspm/std/EpnCore%') " |
31 | 36 | + "ORDER BY short_name, table_name"; |
37 | + | |
32 | 38 | /** |
33 | 39 | * The default query, with these parameters, respectively: max_rows, target_name, time_min, |
34 | 40 | * time_max, dataproduct_type, spectral_range_min, spectral_range_max. |
41 | + * | |
42 | + * @param nbRow The maximum number of rows returned. | |
43 | + * @param tableName The name of the target table for the query. | |
44 | + * @param params A map of parameters, for the `WHERE` keywords. | |
45 | + * @return The literal string of the query. | |
35 | 46 | */ |
36 | - public static final String SAMPLE_AMDA_QUERY = "SELECT TOP %d index, target_name, target_class " | |
37 | - + "FROM %s WHERE target_name LIKE '%%%s%%'"; | |
38 | - // + "WHERE target_name = '%s' AND time_min=%f AND time_max=%f AND dataproduct_type='%s' AND " | |
39 | - // + "spectral_range_min=%f AND spectral_range_max=%f"; | |
47 | + public static String getQuery(String tableName, Map<String, Object> params, int nbRow) { | |
48 | + StringJoiner join = new StringJoiner(" AND "); | |
49 | + for (Map.Entry<String, Object> e : params.entrySet()) { | |
50 | + Class paramClass = e.getValue().getClass(); | |
51 | + if (paramClass == String.class && !"".equals(e.getValue())) { | |
52 | + join.add(e.getKey() + " LIKE '%" + e.getValue() + "%'"); | |
53 | + } else if ((paramClass == Float.class && ((Float) e.getValue()) == new Float(0)) || | |
54 | + (paramClass == Integer.class && ((Integer) e.getValue()) == new Integer(0))) { | |
55 | + join.add(e.getKey() + " = " + e.getValue().toString()); | |
56 | + } | |
57 | + } | |
58 | + String where = "".equals(join.toString()) ? "" : " WHERE " + join.toString(); | |
59 | + return "SELECT TOP " + nbRow + " target_name, target_class FROM " + tableName + where; | |
60 | + } | |
40 | 61 | |
41 | 62 | /** Constructor to hide the implicit public one. */ |
42 | 63 | private Queries() { | ... | ... |
src/main/java/eu/omp/irap/vespa/epntapclient/view/RequestView.java
... | ... | @@ -21,6 +21,8 @@ import java.awt.Dimension; |
21 | 21 | import java.awt.GridLayout; |
22 | 22 | import java.awt.event.ActionEvent; |
23 | 23 | import java.awt.event.ActionListener; |
24 | +import java.util.HashMap; | |
25 | +import java.util.Map; | |
24 | 26 | |
25 | 27 | import javax.swing.BorderFactory; |
26 | 28 | import javax.swing.BoxLayout; |
... | ... | @@ -56,6 +58,16 @@ public class RequestView extends JPanel implements ActionListener { |
56 | 58 | /** The text area where the user put the query. */ |
57 | 59 | private JTextArea queryArea; |
58 | 60 | |
61 | + /** | |
62 | + * The parameters fields for the request. | |
63 | + */ | |
64 | + private Map<String, JTextField> paramsFields; | |
65 | + | |
66 | + /** | |
67 | + * The parameters fields for the request. | |
68 | + */ | |
69 | + private Map<String, Object> params; | |
70 | + | |
59 | 71 | /** The height of the buttons panel. */ |
60 | 72 | private static final int BUTTON_PANEL_HEIGHT = 15; |
61 | 73 | |
... | ... | @@ -68,37 +80,65 @@ public class RequestView extends JPanel implements ActionListener { |
68 | 80 | this.mainView = mainView; |
69 | 81 | setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); |
70 | 82 | |
83 | + // TODO: Get max row numberfrom the GUI | |
84 | + | |
85 | + params = new HashMap<>(); | |
86 | + //@noformat | |
87 | + params.put("target_name", "Galileo"); | |
88 | + params.put("dataproduct_type", "sp"); | |
89 | + params.put("minTime", new Float(0.0)); | |
90 | + params.put("maxTime", new Float(0.0)); | |
91 | + params.put("spectral_range_min", new Float(0.0)); | |
92 | + params.put("spectral_range_max", new Float(0.0)); | |
93 | + //@format | |
94 | + | |
71 | 95 | this.add(buildParamPanel(), this); |
72 | 96 | this.add(buildQueryPanel(), this); |
73 | 97 | this.add(buildButtonPanel(), this); |
98 | + | |
74 | 99 | } |
75 | 100 | |
76 | 101 | /** |
77 | 102 | * @return A JPanel containing graphical elements for the service parameters. |
78 | 103 | */ |
79 | 104 | private JPanel buildParamPanel() { |
80 | - JTextField jtfTargetName = new JTextField("Galileo"); | |
81 | - JFormattedTextField jtfMinTime = new JFormattedTextField(new Float(0.0)); | |
82 | - JFormattedTextField jtfMaxTime = new JFormattedTextField(new Float(1.0)); | |
83 | - JTextField jtfProductType = new JTextField("SpectralRange"); | |
84 | - JFormattedTextField jtfMinSpectralRange = new JFormattedTextField(new Float(0.0)); | |
85 | - JFormattedTextField jtfMaxSpectralRange = new JFormattedTextField(new Float(1.0)); | |
86 | - JFormattedTextField jtfMaxRows = new JFormattedTextField(new Integer(10)); | |
105 | + paramsFields = new HashMap(); | |
106 | + | |
107 | + // TODO: Find a way to set numerical value to "" by default. | |
108 | + // TODO: new GUI field column to allow the user to select the comparison operator: | |
109 | + // - if the field is a String: listbox with 'xx', '%xx', 'xx%', and '%xx%'. | |
110 | + // - if the field is a numeric value: listbox with <, <=, =, =>, >. | |
111 | + for (Map.Entry<String, Object> param : params.entrySet()) { | |
112 | + Class paramClass = param.getValue().getClass(); | |
113 | + | |
114 | + if (paramClass == String.class) { | |
115 | + paramsFields.put(param.getKey(), new JTextField()); | |
116 | + } else if (paramClass == Float.class) { | |
117 | + paramsFields.put(param.getKey(), new JFormattedTextField(new Float(0))); | |
118 | + } else if (paramClass == Integer.class) { | |
119 | + paramsFields.put(param.getKey(), new JFormattedTextField(new Integer(0))); | |
120 | + } else { | |
121 | + logger.error("Unreconized parameter type " + param.getValue() + " for " | |
122 | + + param.getKey() + "."); | |
123 | + } | |
124 | + } | |
87 | 125 | |
88 | 126 | DocumentListener paramListener = new DocumentListener() { |
89 | 127 | @Override |
90 | 128 | public void insertUpdate(DocumentEvent de) { |
91 | 129 | try { |
92 | - int maxRows = Integer.parseInt(jtfMaxRows.getText()); | |
93 | - String targetName = jtfTargetName.getText(); | |
94 | - float minTime = (float) jtfMinTime.getValue(); | |
95 | - float maxTime = (float) jtfMaxTime.getValue(); | |
96 | - String productType = jtfProductType.getText(); | |
97 | - float minSpectralRange = (float) jtfMinSpectralRange.getValue(); | |
98 | - float maxSpectralRange = (float) jtfMaxSpectralRange.getValue(); | |
99 | - | |
100 | - updateQueryArea(maxRows, targetName, minTime, maxTime, productType, | |
101 | - minSpectralRange, maxSpectralRange); | |
130 | + for (Map.Entry<String, JTextField> e : paramsFields.entrySet()) { | |
131 | + Class paramClass = params.get(e.getKey()).getClass(); | |
132 | + if (paramClass == String.class) { | |
133 | + params.put(e.getKey(), e.getValue().getText()); | |
134 | + } else if (paramClass == Float.class || paramClass == Integer.class) { | |
135 | + params.put(e.getKey(), ((JFormattedTextField) e.getValue()).getValue()); | |
136 | + } else { | |
137 | + logger.error("Unreconized parameter type " + e.getValue() + " for " | |
138 | + + paramClass + "."); | |
139 | + } | |
140 | + } | |
141 | + updateQueryArea(); | |
102 | 142 | } catch (Exception e) { |
103 | 143 | logger.error("Can not parse parameters.", e); |
104 | 144 | } |
... | ... | @@ -119,58 +159,14 @@ public class RequestView extends JPanel implements ActionListener { |
119 | 159 | JPanel paramPanel = new JPanel(new GridLayout(0, 2)); |
120 | 160 | paramPanel.setBorder(BorderFactory.createTitledBorder("Query parameters")); |
121 | 161 | |
122 | - // Target name | |
123 | - JLabel targetNameLabel = new JLabel("Target name"); | |
124 | - paramPanel.add(targetNameLabel); | |
125 | - | |
126 | - jtfTargetName.getDocument().addDocumentListener(paramListener); | |
127 | - jtfTargetName.setToolTipText("The target name, for example 'Jupiter'."); | |
128 | - paramPanel.add(jtfTargetName); | |
162 | + for (Map.Entry<String, JTextField> e : paramsFields.entrySet()) { | |
163 | + JLabel label = new JLabel(e.getKey()); | |
164 | + paramPanel.add(label); | |
129 | 165 | |
130 | - // Time | |
131 | - JLabel timeLabel = new JLabel("Time (min, max)"); | |
132 | - paramPanel.add(timeLabel); | |
133 | - JPanel timePanel = new JPanel(new GridLayout(0, 2)); | |
134 | - | |
135 | - jtfMinTime.getDocument().addDocumentListener(paramListener); | |
136 | - jtfMinTime.setToolTipText("The minimum time, for example '0.01'."); | |
137 | - timePanel.add(jtfMinTime); | |
138 | - | |
139 | - jtfMaxTime.getDocument().addDocumentListener(paramListener); | |
140 | - jtfMaxTime.setToolTipText("The maximum time, for example '1.50'."); | |
141 | - timePanel.add(jtfMaxTime); | |
142 | - paramPanel.add(timePanel); | |
143 | - | |
144 | - // Data product type | |
145 | - JLabel productTypeLabel = new JLabel("Product type"); | |
146 | - paramPanel.add(productTypeLabel); | |
147 | - | |
148 | - jtfProductType.getDocument().addDocumentListener(paramListener); | |
149 | - jtfProductType.setToolTipText("The product type, for example '...'."); | |
150 | - paramPanel.add(jtfProductType); | |
151 | - | |
152 | - JLabel spectralRangeLabel = new JLabel("Spectral range"); | |
153 | - paramPanel.add(spectralRangeLabel); | |
154 | - JPanel spectralRangePanel = new JPanel(new GridLayout(0, 2)); | |
155 | - | |
156 | - jtfMinSpectralRange.getDocument().addDocumentListener(paramListener); | |
157 | - jtfMinSpectralRange.setToolTipText("The minimum spectral range, for example '0.01'."); | |
158 | - spectralRangePanel.add(jtfMinSpectralRange); | |
159 | - | |
160 | - jtfMaxSpectralRange.getDocument().addDocumentListener(paramListener); | |
161 | - jtfMaxSpectralRange.setToolTipText("The maximum spectral range, for example '1.50'."); | |
162 | - spectralRangePanel.add(jtfMaxSpectralRange); | |
163 | - | |
164 | - paramPanel.add(spectralRangePanel); | |
165 | - | |
166 | - // Number rows limit | |
167 | - JLabel maxRowsLabel = new JLabel("Rows limit"); | |
168 | - paramPanel.add(maxRowsLabel); | |
169 | - | |
170 | - jtfMaxRows.setName("targetName"); | |
171 | - jtfMaxRows.addActionListener(this); | |
172 | - jtfMaxRows.setToolTipText("The maximum number of rows to display."); | |
173 | - paramPanel.add(jtfMaxRows); | |
166 | + e.getValue().getDocument().addDocumentListener(paramListener); | |
167 | + // TODO: Add tooltip text based on rr.table_column.column_description | |
168 | + paramPanel.add(e.getValue()); | |
169 | + } | |
174 | 170 | |
175 | 171 | return paramPanel; |
176 | 172 | } |
... | ... | @@ -191,21 +187,11 @@ public class RequestView extends JPanel implements ActionListener { |
191 | 187 | } |
192 | 188 | |
193 | 189 | /** |
194 | - * @param maxRows | |
195 | - * @param targetName | |
196 | - * @param timeMin | |
197 | - * @param timeMax | |
198 | - * @param dataproductType | |
199 | - * @param spectralRangeMin | |
200 | - * @param spectralRangeMax | |
190 | + * Update the query JTextArea according to the parameters values. | |
201 | 191 | */ |
202 | - private void updateQueryArea(int maxRows, String targetName, float timeMin, float timeMax, | |
203 | - String dataproductType, float spectralRangeMin, float spectralRangeMax) { | |
192 | + private void updateQueryArea() { | |
204 | 193 | String tableName = mainView.getController().getSelectedTable(); |
205 | - queryArea.setText(String.format(Queries.SAMPLE_AMDA_QUERY, maxRows, tableName, targetName)); | |
206 | - // queryArea.setText(String.format(Queries.SAMPLE_AMDA_QUERY, max_rows, target_name, | |
207 | - // time_min, | |
208 | - // time_max, dataproduct_type, spectral_range_min, spectral_range_max)); | |
194 | + queryArea.setText(Queries.getQuery(tableName, params, 10)); | |
209 | 195 | } |
210 | 196 | |
211 | 197 | /** | ... | ... |