EpnTapModel.java 3.89 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.epntapclient.model;

import java.net.URLEncoder;
import java.util.logging.Level;

import eu.omp.irap.vespa.epntapclient.controller.HTTPConnection;
import eu.omp.irap.vespa.epntapclient.controller.TAPServices;
import eu.omp.irap.vespa.epntapclient.utils.Const;
import eu.omp.irap.vespa.epntapclient.utils.Err;
import eu.omp.irap.vespa.epntapclient.utils.Log;

// TODO: Tests other queries to get more columns.
/**
 * The main class of the EPN-TAP model.
 * 
 * @author N. Jourdane
 */
public class EpnTapModel {

	/** The parameters sent to the HTTP request, except type and query. */
	private static final String QUERY_PARAMETERS = "REQUEST=doQuery&LANG=ADQL"; //$NON-NLS-1$

	/** The type of service to get. */
	private static final String TYPE = "tap"; //$NON-NLS-1$

	/** The name of the XML file returned by the request. */
	private static String xmlFileName = Const.TMP_DIR + "/job.xml"; //$NON-NLS-1$

	/** A list of available registry URLs. */
	@SuppressWarnings("nls")
	private static String[] AVAILABLE_REGISTRIES = { "http://dc.zah.uni-heidelberg.de",
			"http://reg.g-vo.org",
			"http://gavo.aip.de" };

	/** The query used to get the list of EPN services. */
	@SuppressWarnings("nls")
	private static String QUERY = "SELECT ivoid, short_name, res_title, reference_url, base_role, role_name, "
			+ "email, intf_index, access_url, standard_id, cap_type, cap_description, "
			+ "std_version, res_subjects "
			+ "FROM rr.resource AS res "
			+ "NATURAL JOIN rr.interface "
			+ "NATURAL JOIN rr.capability "
			+ "NATURAL LEFT OUTER JOIN rr.res_role "
			+ "NATURAL LEFT OUTER JOIN (SELECT ivoid,  ivo_string_agg(res_subject, ', ') "
			+ "AS res_subjects "
			+ "FROM rr.res_subject GROUP BY ivoid) AS sbj "
			+ "WHERE (base_role='contact' OR base_role='publisher' OR base_role IS NULL) "
			+ "AND standard_id='ivo://ivoa.net/std/tap' AND intf_type='vs:paramhttp' "
			+ "AND ((1=ivo_nocasematch(short_name, '%epn%') "
			+ "OR 1=ivo_hasword(res_title, 'epn') OR 1=ivo_hasword(res_subjects, 'epn') "
			+ "OR 1=ivo_nocasematch(ivoid, '%epn%') OR (base_role='publisher' "
			+ "AND 1=ivo_nocasematch(role_name, '%epn%'))))";

	/**
	 * Method constructor
	 */
	public EpnTapModel() {

	}

	/**
	 * @return the TAP services corresponding to the query response.
	 */
	public static TAPServices getEPNTAPServices() {
		String url = AVAILABLE_REGISTRIES[0];

		try {
			@SuppressWarnings("nls")
			int responseCode = HTTPConnection.sendGet(url + "/" + TYPE + "/sync",
					QUERY_PARAMETERS + "&QUERY="
							+ URLEncoder.encode(QUERY, Const.CHARACTER_SET).replace("+", "%20"),
					xmlFileName);
			if (responseCode != 200) {
				Log.LOGGER.log(Level.SEVERE, Err.BAD_HTTP_RESPONSE_CODE);
				throw new Exception(Err.BAD_HTTP_RESPONSE_CODE);
			}
		} catch (Exception e1) {
			Log.LOGGER.log(Level.SEVERE, Err.BAD_REQUEST, e1);
		}

		TAPServices services = new TAPServices(xmlFileName);
		services.fillServices();
		return services;
		// services.printToFile(servicesFileName);
	}
}