Blame view

src/main/java/eu/omp/irap/vespa/epntapclient/controller/EpnTapController.java 6.21 KB
1353e182   Nathanael Jourdane   Add minimal eleme...
1
2
3
4
5
/*
 * This file is a part of EpnTAPClient.
 * This program aims to provide EPN-TAP support for software clients, like CASSIS spectrum analyzer.
 * See draft specifications: https://voparis-confluence.obspm.fr/pages/viewpage.action?pageId=559861
 * Copyright (C) 2016 Institut de Recherche en Astrophysique et Planétologie.
1e543ea0   Nathanael Jourdane   Code clean-up
6
 *
1353e182   Nathanael Jourdane   Add minimal eleme...
7
8
9
10
11
12
13
14
15
16
17
18
 * This program is free software: you can
 * redistribute it and/or modify it under the terms of the GNU General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or (at your option) any later
 * version. This program is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
 * PURPOSE. See the GNU General Public License for more details. You should have received a copy of
 * the GNU General Public License along with this program. If not, see
 * <http://www.gnu.org/licenses/>.
 */

package eu.omp.irap.vespa.epntapclient.controller;

5af53be7   Nathanael Jourdane   Make the applicat...
19
20
21
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
5d3c344e   Nathanael Jourdane   Use Java logging ...
22
import java.util.logging.Logger;
7706bfa4   Nathanael Jourdane   Use log4j as logg...
23

6b021d84   Nathanael Jourdane   VOTable view can ...
24
import eu.omp.irap.vespa.epntapclient.utils.Queries;
1353e182   Nathanael Jourdane   Add minimal eleme...
25
import eu.omp.irap.vespa.epntapclient.view.EpnTapMainView;
5af53be7   Nathanael Jourdane   Make the applicat...
26
27
import eu.omp.irap.vespa.epntapclient.view.EpnTapMainView.MainViewListener;
import eu.omp.irap.vespa.epntapclient.view.Event;
1353e182   Nathanael Jourdane   Add minimal eleme...
28
import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableController;
4dd68979   Nathanael Jourdane   Improve Exception...
29
import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException;
ffe64e74   Nathanael Jourdane   move Const.java a...
30
import eu.omp.irap.vespa.epntapclient.votable.utils.Const;
1353e182   Nathanael Jourdane   Add minimal eleme...
31
32

/**
a227a22d   Nathanael Jourdane   Add comments.
33
 * The main controller which manage views and controllers.
1e543ea0   Nathanael Jourdane   Code clean-up
34
 *
1353e182   Nathanael Jourdane   Add minimal eleme...
35
36
 * @author N. Jourdane
 */
5af53be7   Nathanael Jourdane   Make the applicat...
37
public class EpnTapController implements MainViewListener {
5d3c344e   Nathanael Jourdane   Use Java logging ...
38
	/** The logger for the class EpnTapController. */
53d49e83   Nathanael Jourdane   Fix all Eclipse w...
39
	private Logger logger = Logger.getLogger(EpnTapController.class.getName());
7706bfa4   Nathanael Jourdane   Use log4j as logg...
40

1353e182   Nathanael Jourdane   Add minimal eleme...
41
	/** The view of EPN-TAP application. */
53d49e83   Nathanael Jourdane   Fix all Eclipse w...
42
	private EpnTapMainView mainView;
1353e182   Nathanael Jourdane   Add minimal eleme...
43

4cc84b63   Nathanael Jourdane   Add the possibili...
44
	/** The controller of the VOTable displaying the list of services. */
53d49e83   Nathanael Jourdane   Fix all Eclipse w...
45
	private VOTableController servicesController;
4cc84b63   Nathanael Jourdane   Add the possibili...
46

1353e182   Nathanael Jourdane   Add minimal eleme...
47
	/** The controller of the VOTable displaying the query results. */
53d49e83   Nathanael Jourdane   Fix all Eclipse w...
48
	private VOTableController resultsController;
1353e182   Nathanael Jourdane   Add minimal eleme...
49

6415d908   Nathanael Jourdane   Get all tap servi...
50
	/** The name of the table selected by the user on the table list panel. */
53d49e83   Nathanael Jourdane   Fix all Eclipse w...
51
	private String selectedTableName;
6415d908   Nathanael Jourdane   Get all tap servi...
52
53

	/** The URL of the service corresponding to the selected table. */
53d49e83   Nathanael Jourdane   Fix all Eclipse w...
54
	private String selectedTableServiceURL;
1353e182   Nathanael Jourdane   Add minimal eleme...
55
56

	/**
5af53be7   Nathanael Jourdane   Make the applicat...
57
58
59
60
61
	 * The parameters fields for the request.
	 */
	private Map<String, Object> paramValues = new HashMap<>();

	/**
a227a22d   Nathanael Jourdane   Add comments.
62
	 * Method constructor, which initialize servicesController, resultsController and mainView.
1353e182   Nathanael Jourdane   Add minimal eleme...
63
64
	 */
	public EpnTapController() {
6b021d84   Nathanael Jourdane   VOTable view can ...
65
		servicesController = new VOTableController(Const.DEFAULT_REGISTRY_URL, "ADQL",
8a39b260   Nathanael Jourdane   Get only EPN-core...
66
				Queries.GET_EPN_TAP_SERVICES);
4cc84b63   Nathanael Jourdane   Add the possibili...
67
		resultsController = new VOTableController();
43c14591   Nathanael Jourdane   View: Add methods...
68

a8594a14   Nathanael Jourdane   [VOTable] Throw e...
69
		mainView = new EpnTapMainView(servicesController.getView(), resultsController.getView());
5af53be7   Nathanael Jourdane   Make the applicat...
70
		mainView.addMainViewListener(this);
6415d908   Nathanael Jourdane   Get all tap servi...
71
		updateSelected(0);
1353e182   Nathanael Jourdane   Add minimal eleme...
72
73
74
	}

	/**
a227a22d   Nathanael Jourdane   Add comments.
75
	 * @return the EPN-TAP main view.
1353e182   Nathanael Jourdane   Add minimal eleme...
76
77
	 */
	public EpnTapMainView getView() {
5af53be7   Nathanael Jourdane   Make the applicat...
78
		return mainView;
1353e182   Nathanael Jourdane   Add minimal eleme...
79
80
81
	}

	/**
a227a22d   Nathanael Jourdane   Add comments.
82
	 * @return The controller of the VOTable which displays the result of the query.
1353e182   Nathanael Jourdane   Add minimal eleme...
83
84
85
86
87
88
	 */
	public VOTableController getResultsController() {
		return resultsController;
	}

	/**
a227a22d   Nathanael Jourdane   Add comments.
89
	 * @return The controller of the VOTable which displays the list of services.
1353e182   Nathanael Jourdane   Add minimal eleme...
90
91
92
93
	 */
	public VOTableController getServicesController() {
		return servicesController;
	}
4cc84b63   Nathanael Jourdane   Add the possibili...
94
95

	/**
a227a22d   Nathanael Jourdane   Add comments.
96
	 * Update the row selected by the user on the Services Panel.
1e543ea0   Nathanael Jourdane   Code clean-up
97
	 *
4cc84b63   Nathanael Jourdane   Add the possibili...
98
99
	 * @param row The row selected by the user on the Jtable.
	 */
6415d908   Nathanael Jourdane   Get all tap servi...
100
	public void updateSelected(int row) {
5af53be7   Nathanael Jourdane   Make the applicat...
101
102
		String serviceURL = mainView.getServicesPanel().getServiceURL(row);
		String tableName = mainView.getServicesPanel().getTableName(row);
6415d908   Nathanael Jourdane   Get all tap servi...
103
104
105
		if (!tableName.equals(selectedTableName)) {
			selectedTableServiceURL = serviceURL;
			selectedTableName = tableName;
5af53be7   Nathanael Jourdane   Make the applicat...
106
			updateQueryArea();
6415d908   Nathanael Jourdane   Get all tap servi...
107
108
			logger.info("Selected table: " + selectedTableName + " - service: "
					+ selectedTableServiceURL);
4cc84b63   Nathanael Jourdane   Add the possibili...
109
110
111
		}
	}

a227a22d   Nathanael Jourdane   Add comments.
112
113
	/**
	 * Remove a query parameter from the parameters list.
1e543ea0   Nathanael Jourdane   Code clean-up
114
	 *
a227a22d   Nathanael Jourdane   Add comments.
115
116
	 * @param paramName The name of the parameter as described in REG-TAP specifications.
	 */
5af53be7   Nathanael Jourdane   Make the applicat...
117
118
119
120
121
122
	public void removeParameter(String paramName) {
		paramValues.remove(paramName);
		updateQueryArea();
		logger.info("removed " + paramName);
	}

a227a22d   Nathanael Jourdane   Add comments.
123
124
	/**
	 * Update a query parameter in the parameter list.
1e543ea0   Nathanael Jourdane   Code clean-up
125
	 *
a227a22d   Nathanael Jourdane   Add comments.
126
127
128
	 * @param paramName The name of the parameter as described in REG-TAP specifications.
	 * @param value The value of the parameter to update.
	 */
5af53be7   Nathanael Jourdane   Make the applicat...
129
130
131
132
133
134
	public void updateParameter(String paramName, Object value) {
		paramValues.put(paramName, value);
		updateQueryArea();
		logger.info("uploaded " + paramName + ": " + value);
	}

a227a22d   Nathanael Jourdane   Add comments.
135
136
137
	/**
	 * Update the query area with a working ADQL query, based on the parameters list.
	 */
5af53be7   Nathanael Jourdane   Make the applicat...
138
139
140
	private void updateQueryArea() {
		String query = Queries.getQuery(selectedTableName, paramValues, 10);
		mainView.getRequestPanel().updateQueryArea(query);
6415d908   Nathanael Jourdane   Get all tap servi...
141
142
	}

4cc84b63   Nathanael Jourdane   Add the possibili...
143
	/**
a227a22d   Nathanael Jourdane   Add comments.
144
	 * Send a query to the selected service on the services panel.
1e543ea0   Nathanael Jourdane   Code clean-up
145
	 *
4cc84b63   Nathanael Jourdane   Add the possibili...
146
	 * @param query The query to send to the selected service.
4dd68979   Nathanael Jourdane   Improve Exception...
147
	 * @throws VOTableException Can not fill the Table
4cc84b63   Nathanael Jourdane   Add the possibili...
148
	 */
4dd68979   Nathanael Jourdane   Improve Exception...
149
	public void sendQuery(String query) throws VOTableException {
6415d908   Nathanael Jourdane   Get all tap servi...
150
151
		logger.info("Sending query: " + query + " on " + selectedTableServiceURL);
		resultsController.fillTable(selectedTableServiceURL, "ADQL", query);
4cc84b63   Nathanael Jourdane   Add the possibili...
152
	}
5af53be7   Nathanael Jourdane   Make the applicat...
153
154

	/**
a227a22d   Nathanael Jourdane   Add comments.
155
	 * This methods is called each time a graphical event is detected from a view.
1e543ea0   Nathanael Jourdane   Code clean-up
156
	 *
a227a22d   Nathanael Jourdane   Add comments.
157
158
	 * @param event The type of the detected event. @see Event
	 * @param args The possible arguments which comes with the event (ie. a row column).
5af53be7   Nathanael Jourdane   Make the applicat...
159
	 */
53d49e83   Nathanael Jourdane   Fix all Eclipse w...
160
161
	@Override
	public void event(Event event, Object... args) {
5af53be7   Nathanael Jourdane   Make the applicat...
162
163
164
		logger.info("new event: " + event.toString());

		try {
53d49e83   Nathanael Jourdane   Fix all Eclipse w...
165
			Object object = args[0];
5af53be7   Nathanael Jourdane   Make the applicat...
166
			switch (event) {
4268557f   Nathanael Jourdane   Fix some Sonar is...
167
			case SERVICE_SELECTED:
53d49e83   Nathanael Jourdane   Fix all Eclipse w...
168
				updateSelected(((Integer) args[0]).intValue());
5af53be7   Nathanael Jourdane   Make the applicat...
169
				break;
4268557f   Nathanael Jourdane   Fix some Sonar is...
170
			case SEND_BUTTON_CLICKED:
53d49e83   Nathanael Jourdane   Fix all Eclipse w...
171
				sendQuery((String) object);
5af53be7   Nathanael Jourdane   Make the applicat...
172
				break;
4268557f   Nathanael Jourdane   Fix some Sonar is...
173
			case PARAMETER_CHANGED:
53d49e83   Nathanael Jourdane   Fix all Eclipse w...
174
				updateParameter((String) object, args[1]);
5af53be7   Nathanael Jourdane   Make the applicat...
175
				break;
4268557f   Nathanael Jourdane   Fix some Sonar is...
176
			case PARAMETER_REMOVED:
53d49e83   Nathanael Jourdane   Fix all Eclipse w...
177
				removeParameter((String) object);
5af53be7   Nathanael Jourdane   Make the applicat...
178
179
180
181
182
				break;
			default:
				logger.warning("Event " + event.toString() + " detected but is not implemented.");
			}
		} catch (Exception e) {
4164c074   Nathanael Jourdane   Use dedicated exc...
183
184
			mainView.displayError("Error", e.getMessage());
			logger.log(Level.WARNING,
4268557f   Nathanael Jourdane   Fix some Sonar is...
185
					"Exception thrown when " + event.toString() + ": " + e.getMessage(), e);
5af53be7   Nathanael Jourdane   Make the applicat...
186
187
		}
	}
1353e182   Nathanael Jourdane   Add minimal eleme...
188
}