VOTableController.java 5.4 KB
/*
 * 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;

import java.util.SortedMap;
import java.util.TreeMap;
import java.util.logging.Logger;

import eu.omp.irap.vespa.epntapclient.votable.model.Info;
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;
import eu.omp.irap.vespa.votable.utils.CantSendQueryException;
import eu.omp.irap.vespa.votable.utils.Network;
import eu.omp.irap.vespa.votable.view.VOTableViewListener;
import eu.omp.irap.vespa.votable.votabledata.VOTableData;
import eu.omp.irap.vespa.votable.votabledata.VOTableDataParser;

/**
 * @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;

	protected String voTablePath;

	protected String targetURL;

	protected String query;

	private VOTableData voTableData;


	/**
	 * Constructor of VOTableController
	 */
	public VOTableController() {
		/* Subclasses initializes their own attributes, we don't want to initialize them twice. */
	}

	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.
	 */
	public VOTableController(String targetURL, String query) {
		voTablePath = null;
		this.targetURL = targetURL;
		this.query = query;
	}

	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();
	}

	/**
	 * @param voTablePath The path of the VOTable file.
	 * @throws CantDisplayVOTableException If the VOTable can not be filled.
	 * @throws CantSendQueryException
	 */
	public void readTable() throws CantGetVOTableException {
		if (voTablePath == null) {
			voTablePath = VOTableController.downloadVOTable(targetURL, query);
		}
		voTable = VOTableParser.parseVOTable(voTablePath);
		checkVOTable(voTable);

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

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

	public static void checkVOTable(VOTABLE voTable) throws CantGetVOTableException {
		if (voTable.getRESOURCE().isEmpty()) {
			throw new CantGetVOTableException("The VOTable do not have any resource. "
					+ "See the local file for more informations.");
		}

		if (voTable.getRESOURCE().size() > 1) {
			throw new CantGetVOTableException(
					"VOTable with more than one resource are not yet supported.");
		}

		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. ");
			}
		}

		if (voTable.getRESOURCE().get(0).getLINKAndTABLEOrRESOURCE().size() > 1) {
			throw new CantGetVOTableException(
					"VOTable with more than one table are not yet supported. "
							+ "See the local file for more informations.");
		}

	}

	public static String downloadVOTable(String targetURL, String query)
			throws CantGetVOTableException {
		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);

		String fullURL = Network.buildQuery(url, parameters);
		VOTableController.logger.info("Sending query '" + fullURL + "'");

		String voTablePath;
		try {
			voTablePath = Network.saveQuery(fullURL);
		} catch (CantSendQueryException e) {
			throw new CantGetVOTableException("Can not get the VOTable on " + url, e);
		}
		VOTableController.logger.info("VOTable downloaded in " + voTablePath);

		return voTablePath;
	}

	public VOTableData getVOTableData() {
		return voTableData;
	}

	public VOTABLE getVOTable() {
		return voTable;
	}

	public String getVOTablePath() {
		return voTablePath;
	}
}