Blame view

src/main/java/eu/omp/irap/vespa/epntapclient/controller/EpnTapController.java 5.34 KB
1353e182   Nathanael Jourdane   Add minimal eleme...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/*
 * 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.
 * 
 * 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.Const;
6b021d84   Nathanael Jourdane   VOTable view can ...
25
import eu.omp.irap.vespa.epntapclient.utils.Queries;
1353e182   Nathanael Jourdane   Add minimal eleme...
26
import eu.omp.irap.vespa.epntapclient.view.EpnTapMainView;
5af53be7   Nathanael Jourdane   Make the applicat...
27
28
import eu.omp.irap.vespa.epntapclient.view.EpnTapMainView.MainViewListener;
import eu.omp.irap.vespa.epntapclient.view.Event;
1353e182   Nathanael Jourdane   Add minimal eleme...
29
import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableController;
4dd68979   Nathanael Jourdane   Improve Exception...
30
import eu.omp.irap.vespa.epntapclient.votable.controller.VOTableException;
1353e182   Nathanael Jourdane   Add minimal eleme...
31
32
33
34

/**
 * @author N. Jourdane
 */
5af53be7   Nathanael Jourdane   Make the applicat...
35
public class EpnTapController implements MainViewListener {
5d3c344e   Nathanael Jourdane   Use Java logging ...
36
	/** The logger for the class EpnTapController. */
53d49e83   Nathanael Jourdane   Fix all Eclipse w...
37
	private Logger logger = Logger.getLogger(EpnTapController.class.getName());
7706bfa4   Nathanael Jourdane   Use log4j as logg...
38

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

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

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

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

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

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

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

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

	/**
	 * @return the EPN-TAP view.
	 */
	public EpnTapMainView getView() {
5af53be7   Nathanael Jourdane   Make the applicat...
76
		return mainView;
1353e182   Nathanael Jourdane   Add minimal eleme...
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
	}

	/**
	 * @return The controller of the VOTable displaying the query results.
	 */
	public VOTableController getResultsController() {
		return resultsController;
	}

	/**
	 * @return The controller of the VOTable displaying the list of services.
	 */
	public VOTableController getServicesController() {
		return servicesController;
	}
4cc84b63   Nathanael Jourdane   Add the possibili...
92
93
94
95

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

5af53be7   Nathanael Jourdane   Make the applicat...
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
	public void removeParameter(String paramName) {
		paramValues.remove(paramName);
		updateQueryArea();
		logger.info("removed " + paramName);
	}

	public void updateParameter(String paramName, Object value) {
		paramValues.put(paramName, value);
		updateQueryArea();
		logger.info("uploaded " + paramName + ": " + value);
	}

	private void updateQueryArea() {
		String query = Queries.getQuery(selectedTableName, paramValues, 10);
		mainView.getRequestPanel().updateQueryArea(query);
6415d908   Nathanael Jourdane   Get all tap servi...
123
124
	}

4cc84b63   Nathanael Jourdane   Add the possibili...
125
126
	/**
	 * @param query The query to send to the selected service.
4dd68979   Nathanael Jourdane   Improve Exception...
127
	 * @throws VOTableException Can not fill the Table
4cc84b63   Nathanael Jourdane   Add the possibili...
128
	 */
4dd68979   Nathanael Jourdane   Improve Exception...
129
	public void sendQuery(String query) throws VOTableException {
6415d908   Nathanael Jourdane   Get all tap servi...
130
131
		logger.info("Sending query: " + query + " on " + selectedTableServiceURL);
		resultsController.fillTable(selectedTableServiceURL, "ADQL", query);
4cc84b63   Nathanael Jourdane   Add the possibili...
132
	}
5af53be7   Nathanael Jourdane   Make the applicat...
133
134
135
136
137

	/**
	 * @param event
	 * @param args
	 */
53d49e83   Nathanael Jourdane   Fix all Eclipse w...
138
139
	@Override
	public void event(Event event, Object... args) {
5af53be7   Nathanael Jourdane   Make the applicat...
140
141
142
		logger.info("new event: " + event.toString());

		try {
53d49e83   Nathanael Jourdane   Fix all Eclipse w...
143
			Object object = args[0];
5af53be7   Nathanael Jourdane   Make the applicat...
144
145
			switch (event) {
			case serviceSelected:
53d49e83   Nathanael Jourdane   Fix all Eclipse w...
146
				updateSelected(((Integer) args[0]).intValue());
5af53be7   Nathanael Jourdane   Make the applicat...
147
148
				break;
			case btnSearchClicked:
53d49e83   Nathanael Jourdane   Fix all Eclipse w...
149
				sendQuery((String) object);
5af53be7   Nathanael Jourdane   Make the applicat...
150
151
				break;
			case paramChanged:
53d49e83   Nathanael Jourdane   Fix all Eclipse w...
152
				updateParameter((String) object, args[1]);
5af53be7   Nathanael Jourdane   Make the applicat...
153
154
				break;
			case paramRemoved:
53d49e83   Nathanael Jourdane   Fix all Eclipse w...
155
				removeParameter((String) object);
5af53be7   Nathanael Jourdane   Make the applicat...
156
157
158
159
160
161
				break;
			default:
				logger.warning("Event " + event.toString() + " detected but is not implemented.");
			}
		} catch (Exception e) {
			mainView.displayError("Error", "An unexpected error occured: " + e.getMessage()
a8594a14   Nathanael Jourdane   [VOTable] Throw e...
162
					+ ".\nPlease report it to the developper team along with the stacktrace.");
5af53be7   Nathanael Jourdane   Make the applicat...
163
164
165
			logger.log(Level.SEVERE, "Error occured when " + event.toString(), e);
		}
	}
1353e182   Nathanael Jourdane   Add minimal eleme...
166
}