Blame view

src/main/java/eu/omp/irap/vespa/votable/controller/VOTableController.java 5.4 KB
ff1c1b4c   Nathanael Jourdane   Initial commit on...
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.votable.controller;

ff1c1b4c   Nathanael Jourdane   Initial commit on...
19
20
21
22
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.logging.Logger;

3ba8e2a7   Nathanael Jourdane   Improve VOTable v...
23
import eu.omp.irap.vespa.epntapclient.votable.model.Info;
ff1c1b4c   Nathanael Jourdane   Initial commit on...
24
25
26
import eu.omp.irap.vespa.epntapclient.votable.model.Table;
import eu.omp.irap.vespa.epntapclient.votable.model.VOTABLE;
import eu.omp.irap.vespa.votable.Consts;
275fa148   Nathanael Jourdane   Use VOTableData i...
27
import eu.omp.irap.vespa.votable.utils.CantSendQueryException;
ff1c1b4c   Nathanael Jourdane   Initial commit on...
28
29
import eu.omp.irap.vespa.votable.utils.Network;
import eu.omp.irap.vespa.votable.view.VOTableViewListener;
275fa148   Nathanael Jourdane   Use VOTableData i...
30
31
import eu.omp.irap.vespa.votable.votabledata.VOTableData;
import eu.omp.irap.vespa.votable.votabledata.VOTableDataParser;
ff1c1b4c   Nathanael Jourdane   Initial commit on...
32
33
34
35
36
37
38
39
40
41
42
43

/**
 * @author N. Jourdane
 */
public class VOTableController implements VOTableViewListener {

	/** The logger for the class VOTableController. */
	private static final Logger logger = Logger.getLogger(VOTableController.class.getName());

	/** The VOTable model */
	private VOTABLE voTable;

e6fb239b   Nathanael Jourdane   Add Javadoc
44
	protected String voTablePath;
ff1c1b4c   Nathanael Jourdane   Initial commit on...
45

e6fb239b   Nathanael Jourdane   Add Javadoc
46
	protected String targetURL;
ff1c1b4c   Nathanael Jourdane   Initial commit on...
47

e6fb239b   Nathanael Jourdane   Add Javadoc
48
	protected String query;
ff1c1b4c   Nathanael Jourdane   Initial commit on...
49

275fa148   Nathanael Jourdane   Use VOTableData i...
50
	private VOTableData voTableData;
ff1c1b4c   Nathanael Jourdane   Initial commit on...
51
52


e6fb239b   Nathanael Jourdane   Add Javadoc
53
54
55
56
	/**
	 * Constructor of VOTableController
	 */
	public VOTableController() {
a36c05d6   Nathanael Jourdane   Fix Sonar issues.
57
		/* Subclasses initializes their own attributes, we don't want to initialize them twice. */
ff1c1b4c   Nathanael Jourdane   Initial commit on...
58
59
	}

ff1c1b4c   Nathanael Jourdane   Initial commit on...
60
61
62
63
64
65
66
67
68
69
70
	public VOTableController(String voTablePath) {
		this.voTablePath = voTablePath;
	}

	/**
	 * Method constructor
	 *
	 * @param targetURL The URL of the registry to communicate (ie. "http://reg.g-vo.org").
	 * @param queryLanguage The language used for the queries (ie. "ADQL").
	 * @param query The query to ask to the registry.
	 */
275fa148   Nathanael Jourdane   Use VOTableData i...
71
	public VOTableController(String targetURL, String query) {
ff1c1b4c   Nathanael Jourdane   Initial commit on...
72
73
		voTablePath = null;
		this.targetURL = targetURL;
ff1c1b4c   Nathanael Jourdane   Initial commit on...
74
75
76
		this.query = query;
	}

e6fb239b   Nathanael Jourdane   Add Javadoc
77
78
79
80
81
82
83
84
85
86
87
88
	public void updateVOTable(String voTablePath) throws CantGetVOTableException {
		this.voTablePath = voTablePath;
		readTable();
	}

	public void updateVOTable(String targetURL, String query) throws CantGetVOTableException {
		voTablePath = null;
		this.targetURL = targetURL;
		this.query = query;
		readTable();
	}

ff1c1b4c   Nathanael Jourdane   Initial commit on...
89
90
91
92
93
	/**
	 * @param voTablePath The path of the VOTable file.
	 * @throws CantDisplayVOTableException If the VOTable can not be filled.
	 * @throws CantSendQueryException
	 */
275fa148   Nathanael Jourdane   Use VOTableData i...
94
	public void readTable() throws CantGetVOTableException {
ff1c1b4c   Nathanael Jourdane   Initial commit on...
95
		if (voTablePath == null) {
d457bfe7   Nathanael Jourdane   Add VOTable getter.
96
			voTablePath = VOTableController.downloadVOTable(targetURL, query);
ff1c1b4c   Nathanael Jourdane   Initial commit on...
97
98
		}
		voTable = VOTableParser.parseVOTable(voTablePath);
3ba8e2a7   Nathanael Jourdane   Improve VOTable v...
99
		checkVOTable(voTable);
275fa148   Nathanael Jourdane   Use VOTableData i...
100
101
102
103
104
105
106
107
108

		Table table = (Table) voTable.getRESOURCE().get(0).getLINKAndTABLEOrRESOURCE().get(0);

		VOTableDataParser dataCtrl;
		dataCtrl = new VOTableDataParser(table);
		dataCtrl.parseData();
		voTableData = dataCtrl.getData();
	}

3ba8e2a7   Nathanael Jourdane   Improve VOTable v...
109
	public static void checkVOTable(VOTABLE voTable) throws CantGetVOTableException {
275fa148   Nathanael Jourdane   Use VOTableData i...
110
111
		if (voTable.getRESOURCE().isEmpty()) {
			throw new CantGetVOTableException("The VOTable do not have any resource. "
3ba8e2a7   Nathanael Jourdane   Improve VOTable v...
112
					+ "See the local file for more informations.");
275fa148   Nathanael Jourdane   Use VOTableData i...
113
		}
ff1c1b4c   Nathanael Jourdane   Initial commit on...
114
115

		if (voTable.getRESOURCE().size() > 1) {
275fa148   Nathanael Jourdane   Use VOTableData i...
116
117
			throw new CantGetVOTableException(
					"VOTable with more than one resource are not yet supported.");
ff1c1b4c   Nathanael Jourdane   Initial commit on...
118
119
		}

3ba8e2a7   Nathanael Jourdane   Improve VOTable v...
120
121
122
123
124
		for (Info info : voTable.getRESOURCE().get(0).getINFO()) {
			if ("ERROR".equals(info.getValueAttribute())) {
				throw new CantGetVOTableException("There is an error in the VOTable:\n"
						+ info.getValue() + "\nPlease check your ADQL query. ");
			}
ff1c1b4c   Nathanael Jourdane   Initial commit on...
125
126
127
		}

		if (voTable.getRESOURCE().get(0).getLINKAndTABLEOrRESOURCE().size() > 1) {
275fa148   Nathanael Jourdane   Use VOTableData i...
128
129
			throw new CantGetVOTableException(
					"VOTable with more than one table are not yet supported. "
3ba8e2a7   Nathanael Jourdane   Improve VOTable v...
130
							+ "See the local file for more informations.");
ff1c1b4c   Nathanael Jourdane   Initial commit on...
131
132
		}

ff1c1b4c   Nathanael Jourdane   Initial commit on...
133
134
	}

9ecbf109   Nathanael Jourdane   BugFix: Look for ...
135
136
	public static String downloadVOTable(String targetURL, String query)
			throws CantGetVOTableException {
ff1c1b4c   Nathanael Jourdane   Initial commit on...
137
138
139
140
141
142
143
144
		String url = targetURL + "/sync";

		SortedMap<String, String> parameters = new TreeMap<>();
		parameters.put("REQUEST", Consts.QUERY_REQUEST);
		parameters.put("LANG", Consts.QUERY_LANG);
		parameters.put("FORMAT", Consts.QUERY_FORMAT);
		parameters.put("QUERY", query);

275fa148   Nathanael Jourdane   Use VOTableData i...
145
		String fullURL = Network.buildQuery(url, parameters);
9ecbf109   Nathanael Jourdane   BugFix: Look for ...
146
		VOTableController.logger.info("Sending query '" + fullURL + "'");
275fa148   Nathanael Jourdane   Use VOTableData i...
147
148
149
150
151
152
153

		String voTablePath;
		try {
			voTablePath = Network.saveQuery(fullURL);
		} catch (CantSendQueryException e) {
			throw new CantGetVOTableException("Can not get the VOTable on " + url, e);
		}
008b447e   Nathanael Jourdane   Make the function...
154
155
156
		VOTableController.logger.info("VOTable downloaded in " + voTablePath);

		return voTablePath;
ff1c1b4c   Nathanael Jourdane   Initial commit on...
157
158
	}

275fa148   Nathanael Jourdane   Use VOTableData i...
159
	public VOTableData getVOTableData() {
ff1c1b4c   Nathanael Jourdane   Initial commit on...
160
161
162
		return voTableData;
	}

d457bfe7   Nathanael Jourdane   Add VOTable getter.
163
164
165
166
	public VOTABLE getVOTable() {
		return voTable;
	}

ff1c1b4c   Nathanael Jourdane   Initial commit on...
167
168
169
170
	public String getVOTablePath() {
		return voTablePath;
	}
}