diff --git a/src/main/java/eu/omp/irap/vespa/epntapclient/utils/Queries.java b/src/main/java/eu/omp/irap/vespa/epntapclient/utils/Queries.java index 7be305b..098ea83 100644 --- a/src/main/java/eu/omp/irap/vespa/epntapclient/utils/Queries.java +++ b/src/main/java/eu/omp/irap/vespa/epntapclient/utils/Queries.java @@ -16,27 +16,48 @@ package eu.omp.irap.vespa.epntapclient.utils; +import java.util.Map; +import java.util.StringJoiner; + /** * @author N. Jourdane */ public final class Queries { /** Query to get all EPN-TAP services. */ - public static final String GET_EPN_TAP_SERVICES = "SELECT short_name, res_title AS schema_title, table_name, schema_name, ivoid, access_url " - + "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 " + public static final String GET_EPN_TAP_SERVICES = "SELECT short_name, " + + "res_title AS schema_title, table_name, schema_name, ivoid, access_url " + + "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 " + "WHERE standard_id='ivo://ivoa.net/std/tap' AND " + "intf_type='vs:paramhttp' AND " + "detail_xpath='/capability/dataModel/@ivo-id' AND " + "1=ivo_nocasematch(detail_value, 'ivo://vopdc.obspm/std/EpnCore%') " + "ORDER BY short_name, table_name"; + /** * The default query, with these parameters, respectively: max_rows, target_name, time_min, * time_max, dataproduct_type, spectral_range_min, spectral_range_max. + * + * @param nbRow The maximum number of rows returned. + * @param tableName The name of the target table for the query. + * @param params A map of parameters, for the `WHERE` keywords. + * @return The literal string of the query. */ - public static final String SAMPLE_AMDA_QUERY = "SELECT TOP %d index, target_name, target_class " - + "FROM %s WHERE target_name LIKE '%%%s%%'"; - // + "WHERE target_name = '%s' AND time_min=%f AND time_max=%f AND dataproduct_type='%s' AND " - // + "spectral_range_min=%f AND spectral_range_max=%f"; + public static String getQuery(String tableName, Map params, int nbRow) { + StringJoiner join = new StringJoiner(" AND "); + for (Map.Entry e : params.entrySet()) { + Class paramClass = e.getValue().getClass(); + if (paramClass == String.class && !"".equals(e.getValue())) { + join.add(e.getKey() + " LIKE '%" + e.getValue() + "%'"); + } else if ((paramClass == Float.class && ((Float) e.getValue()) == new Float(0)) || + (paramClass == Integer.class && ((Integer) e.getValue()) == new Integer(0))) { + join.add(e.getKey() + " = " + e.getValue().toString()); + } + } + String where = "".equals(join.toString()) ? "" : " WHERE " + join.toString(); + return "SELECT TOP " + nbRow + " target_name, target_class FROM " + tableName + where; + } /** Constructor to hide the implicit public one. */ private Queries() { diff --git a/src/main/java/eu/omp/irap/vespa/epntapclient/view/RequestView.java b/src/main/java/eu/omp/irap/vespa/epntapclient/view/RequestView.java index 13d2ca7..97c8cd8 100644 --- a/src/main/java/eu/omp/irap/vespa/epntapclient/view/RequestView.java +++ b/src/main/java/eu/omp/irap/vespa/epntapclient/view/RequestView.java @@ -21,6 +21,8 @@ import java.awt.Dimension; import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; +import java.util.HashMap; +import java.util.Map; import javax.swing.BorderFactory; import javax.swing.BoxLayout; @@ -56,6 +58,16 @@ public class RequestView extends JPanel implements ActionListener { /** The text area where the user put the query. */ private JTextArea queryArea; + /** + * The parameters fields for the request. + */ + private Map paramsFields; + + /** + * The parameters fields for the request. + */ + private Map params; + /** The height of the buttons panel. */ private static final int BUTTON_PANEL_HEIGHT = 15; @@ -68,37 +80,65 @@ public class RequestView extends JPanel implements ActionListener { this.mainView = mainView; setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); + // TODO: Get max row numberfrom the GUI + + params = new HashMap<>(); + //@noformat + params.put("target_name", "Galileo"); + params.put("dataproduct_type", "sp"); + params.put("minTime", new Float(0.0)); + params.put("maxTime", new Float(0.0)); + params.put("spectral_range_min", new Float(0.0)); + params.put("spectral_range_max", new Float(0.0)); + //@format + this.add(buildParamPanel(), this); this.add(buildQueryPanel(), this); this.add(buildButtonPanel(), this); + } /** * @return A JPanel containing graphical elements for the service parameters. */ private JPanel buildParamPanel() { - JTextField jtfTargetName = new JTextField("Galileo"); - JFormattedTextField jtfMinTime = new JFormattedTextField(new Float(0.0)); - JFormattedTextField jtfMaxTime = new JFormattedTextField(new Float(1.0)); - JTextField jtfProductType = new JTextField("SpectralRange"); - JFormattedTextField jtfMinSpectralRange = new JFormattedTextField(new Float(0.0)); - JFormattedTextField jtfMaxSpectralRange = new JFormattedTextField(new Float(1.0)); - JFormattedTextField jtfMaxRows = new JFormattedTextField(new Integer(10)); + paramsFields = new HashMap(); + + // TODO: Find a way to set numerical value to "" by default. + // TODO: new GUI field column to allow the user to select the comparison operator: + // - if the field is a String: listbox with 'xx', '%xx', 'xx%', and '%xx%'. + // - if the field is a numeric value: listbox with <, <=, =, =>, >. + for (Map.Entry param : params.entrySet()) { + Class paramClass = param.getValue().getClass(); + + if (paramClass == String.class) { + paramsFields.put(param.getKey(), new JTextField()); + } else if (paramClass == Float.class) { + paramsFields.put(param.getKey(), new JFormattedTextField(new Float(0))); + } else if (paramClass == Integer.class) { + paramsFields.put(param.getKey(), new JFormattedTextField(new Integer(0))); + } else { + logger.error("Unreconized parameter type " + param.getValue() + " for " + + param.getKey() + "."); + } + } DocumentListener paramListener = new DocumentListener() { @Override public void insertUpdate(DocumentEvent de) { try { - int maxRows = Integer.parseInt(jtfMaxRows.getText()); - String targetName = jtfTargetName.getText(); - float minTime = (float) jtfMinTime.getValue(); - float maxTime = (float) jtfMaxTime.getValue(); - String productType = jtfProductType.getText(); - float minSpectralRange = (float) jtfMinSpectralRange.getValue(); - float maxSpectralRange = (float) jtfMaxSpectralRange.getValue(); - - updateQueryArea(maxRows, targetName, minTime, maxTime, productType, - minSpectralRange, maxSpectralRange); + for (Map.Entry e : paramsFields.entrySet()) { + Class paramClass = params.get(e.getKey()).getClass(); + if (paramClass == String.class) { + params.put(e.getKey(), e.getValue().getText()); + } else if (paramClass == Float.class || paramClass == Integer.class) { + params.put(e.getKey(), ((JFormattedTextField) e.getValue()).getValue()); + } else { + logger.error("Unreconized parameter type " + e.getValue() + " for " + + paramClass + "."); + } + } + updateQueryArea(); } catch (Exception e) { logger.error("Can not parse parameters.", e); } @@ -119,58 +159,14 @@ public class RequestView extends JPanel implements ActionListener { JPanel paramPanel = new JPanel(new GridLayout(0, 2)); paramPanel.setBorder(BorderFactory.createTitledBorder("Query parameters")); - // Target name - JLabel targetNameLabel = new JLabel("Target name"); - paramPanel.add(targetNameLabel); - - jtfTargetName.getDocument().addDocumentListener(paramListener); - jtfTargetName.setToolTipText("The target name, for example 'Jupiter'."); - paramPanel.add(jtfTargetName); + for (Map.Entry e : paramsFields.entrySet()) { + JLabel label = new JLabel(e.getKey()); + paramPanel.add(label); - // Time - JLabel timeLabel = new JLabel("Time (min, max)"); - paramPanel.add(timeLabel); - JPanel timePanel = new JPanel(new GridLayout(0, 2)); - - jtfMinTime.getDocument().addDocumentListener(paramListener); - jtfMinTime.setToolTipText("The minimum time, for example '0.01'."); - timePanel.add(jtfMinTime); - - jtfMaxTime.getDocument().addDocumentListener(paramListener); - jtfMaxTime.setToolTipText("The maximum time, for example '1.50'."); - timePanel.add(jtfMaxTime); - paramPanel.add(timePanel); - - // Data product type - JLabel productTypeLabel = new JLabel("Product type"); - paramPanel.add(productTypeLabel); - - jtfProductType.getDocument().addDocumentListener(paramListener); - jtfProductType.setToolTipText("The product type, for example '...'."); - paramPanel.add(jtfProductType); - - JLabel spectralRangeLabel = new JLabel("Spectral range"); - paramPanel.add(spectralRangeLabel); - JPanel spectralRangePanel = new JPanel(new GridLayout(0, 2)); - - jtfMinSpectralRange.getDocument().addDocumentListener(paramListener); - jtfMinSpectralRange.setToolTipText("The minimum spectral range, for example '0.01'."); - spectralRangePanel.add(jtfMinSpectralRange); - - jtfMaxSpectralRange.getDocument().addDocumentListener(paramListener); - jtfMaxSpectralRange.setToolTipText("The maximum spectral range, for example '1.50'."); - spectralRangePanel.add(jtfMaxSpectralRange); - - paramPanel.add(spectralRangePanel); - - // Number rows limit - JLabel maxRowsLabel = new JLabel("Rows limit"); - paramPanel.add(maxRowsLabel); - - jtfMaxRows.setName("targetName"); - jtfMaxRows.addActionListener(this); - jtfMaxRows.setToolTipText("The maximum number of rows to display."); - paramPanel.add(jtfMaxRows); + e.getValue().getDocument().addDocumentListener(paramListener); + // TODO: Add tooltip text based on rr.table_column.column_description + paramPanel.add(e.getValue()); + } return paramPanel; } @@ -191,21 +187,11 @@ public class RequestView extends JPanel implements ActionListener { } /** - * @param maxRows - * @param targetName - * @param timeMin - * @param timeMax - * @param dataproductType - * @param spectralRangeMin - * @param spectralRangeMax + * Update the query JTextArea according to the parameters values. */ - private void updateQueryArea(int maxRows, String targetName, float timeMin, float timeMax, - String dataproductType, float spectralRangeMin, float spectralRangeMax) { + private void updateQueryArea() { String tableName = mainView.getController().getSelectedTable(); - queryArea.setText(String.format(Queries.SAMPLE_AMDA_QUERY, maxRows, tableName, targetName)); - // queryArea.setText(String.format(Queries.SAMPLE_AMDA_QUERY, max_rows, target_name, - // time_min, - // time_max, dataproduct_type, spectral_range_min, spectral_range_max)); + queryArea.setText(Queries.getQuery(tableName, params, 10)); } /** -- libgit2 0.21.2