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 {
aa2a59ba   Nathanael Jourdane   [coding style] 2 ...
38

5d3c344e   Nathanael Jourdane   Use Java logging ...
39
	/** The logger for the class EpnTapController. */
53d49e83   Nathanael Jourdane   Fix all Eclipse w...
40
	private Logger logger = Logger.getLogger(EpnTapController.class.getName());
7706bfa4   Nathanael Jourdane   Use log4j as logg...
41

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

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

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

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

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

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

aa2a59ba   Nathanael Jourdane   [coding style] 2 ...
62

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

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

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

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

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

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

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

a227a22d   Nathanael Jourdane   Add comments.
125
126
	/**
	 * Update a query parameter in the parameter list.
1e543ea0   Nathanael Jourdane   Code clean-up
127
	 *
a227a22d   Nathanael Jourdane   Add comments.
128
129
130
	 * @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...
131
132
133
134
135
136
	public void updateParameter(String paramName, Object value) {
		paramValues.put(paramName, value);
		updateQueryArea();
		logger.info("uploaded " + paramName + ": " + value);
	}

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

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

	/**
a227a22d   Nathanael Jourdane   Add comments.
157
	 * This methods is called each time a graphical event is detected from a view.
1e543ea0   Nathanael Jourdane   Code clean-up
158
	 *
a227a22d   Nathanael Jourdane   Add comments.
159
160
	 * @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...
161
	 */
53d49e83   Nathanael Jourdane   Fix all Eclipse w...
162
163
	@Override
	public void event(Event event, Object... args) {
5af53be7   Nathanael Jourdane   Make the applicat...
164
165
166
		logger.info("new event: " + event.toString());

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