Blame view

src/main/java/eu/omp/irap/vespa/epntapclient/service/ServiceCtrl.java 7.33 KB
d11a0a1d   Nathanael Jourdane   Create package er...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*
 * 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.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
35daa117   Nathanael Jourdane   Improve JUnits te...
22
import java.util.Map;
d11a0a1d   Nathanael Jourdane   Create package er...
23
24
25
26
27
28
29

import javax.xml.datatype.XMLGregorianCalendar;

import eu.omp.irap.vespa.epntapclient.voresource.model.ContentLevel;
import eu.omp.irap.vespa.epntapclient.voresource.model.Creator;
import eu.omp.irap.vespa.epntapclient.voresource.model.Resource;
import eu.omp.irap.vespa.epntapclient.voresource.model.Type;
35daa117   Nathanael Jourdane   Improve JUnits te...
30
31
import eu.omp.irap.vespa.epntapclient.votable.model.Table;
import eu.omp.irap.vespa.epntapclient.votable.model.VOTABLE;
cfbb6d07   Nathanael Jourdane   Implement most of...
32
import eu.omp.irap.vespa.votable.Consts;
e669c5d3   Nathanael Jourdane   Improve Javadoc.
33
import eu.omp.irap.vespa.votable.controller.VOTableException;
cfbb6d07   Nathanael Jourdane   Implement most of...
34
import eu.omp.irap.vespa.votable.controller.VOTableController;
e2264856   Nathanael Jourdane   Delete votable pa...
35
import eu.omp.irap.vespa.votable.utils.StringJoiner;
cfbb6d07   Nathanael Jourdane   Implement most of...
36
import eu.omp.irap.vespa.votable.votabledata.VOTableData;
35daa117   Nathanael Jourdane   Improve JUnits te...
37
import eu.omp.irap.vespa.votable.votabledata.VOTableDataParser;
d11a0a1d   Nathanael Jourdane   Create package er...
38
39
40
41
42
43

/**
 * @author N. Jourdane
 */
public class ServiceCtrl {

76a922bc   Nathanael Jourdane   Improve Javadoc.
44
	/** The date format used to serialize date parameters. */
d11a0a1d   Nathanael Jourdane   Create package er...
45
46
47
	public static final String DATE_FORMAT = "yyyy/MM/dd HH:mm:ss";


76a922bc   Nathanael Jourdane   Improve Javadoc.
48
	/** Private constructor to hide the implicit public one. */
5672c106   Nathanael Jourdane   Fix Sonar issues.
49
50
51
	private ServiceCtrl() {
	}

76a922bc   Nathanael Jourdane   Improve Javadoc.
52
53
54
55
56
57
	/**
	 * Parse a Resource and returns the corresponding Service.
	 *
	 * @param resource the resource to parse.
	 * @return The corresponding service.
	 */
5672c106   Nathanael Jourdane   Fix Sonar issues.
58
	public static final Service getService(Resource resource) {
4bcbd19f   Nathanael Jourdane   Fix imports
59
		Service service = new Service(resource.getIdentifier());
d11a0a1d   Nathanael Jourdane   Create package er...
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
		service.setTitle(resource.getTitle());
		service.setShortName(resource.getShortName());
		StringJoiner types = new StringJoiner(", ");
		for (Type type : resource.getContent().getType()) {
			types.add(type.toString());
		}
		service.setType(types.toString());
		service.setDescription(resource.getContent().getDescription());
		StringJoiner creators = new StringJoiner(", ");
		for (Creator creator : resource.getCuration().getCreator()) {
			creators.add(creator.getName().getValue());
		}
		service.setCreator(creators.toString());
		StringJoiner contentLevels = new StringJoiner(", ");
		for (ContentLevel contentLevel : resource.getContent().getContentLevel()) {
			contentLevels.add(contentLevel.value());
		}

		service.setContentLevel(contentLevels.toString());
		service.setReferenceURL(resource.getContent().getReferenceURL());
5672c106   Nathanael Jourdane   Fix Sonar issues.
80
81
		service.setCreated(ServiceCtrl.xmlDateToString(resource.getCreated()));
		service.setUpdated(ServiceCtrl.xmlDateToString(resource.getUpdated()));
d11a0a1d   Nathanael Jourdane   Create package er...
82
83
84
		return service;
	}

76a922bc   Nathanael Jourdane   Improve Javadoc.
85
86
87
88
89
90
91
	/**
	 * Parse a VOTableData and returns the corresponding list of services.
	 *
	 * @param data The VOTableData of the service, created by the VOTable parser. Each row of the
	 *            data set is a service.
	 * @return The corresponding list of service.
	 */
35daa117   Nathanael Jourdane   Improve JUnits te...
92
	public static final List<Service> getServices(VOTableData data) {
4bcbd19f   Nathanael Jourdane   Fix imports
93
		List<Service> services = new ArrayList<>();
d11a0a1d   Nathanael Jourdane   Create package er...
94
		for (int i = 0; i < data.getNbRows(); i++) {
4bcbd19f   Nathanael Jourdane   Fix imports
95
			Service service = new Service((String) data.getCell(i, "ivoid"));
d11a0a1d   Nathanael Jourdane   Create package er...
96
97
98
99
100
101
102
103
104
105
106
107
			service.setTitle((String) data.getCell(i, "res_title"));
			service.setShortName((String) data.getCell(i, "short_name"));
			service.setType((String) data.getCell(i, "content_type"));
			service.setDescription((String) data.getCell(i, "res_description"));
			service.setCreator((String) data.getCell(i, "creator_seq"));
			service.setContentLevel((String) data.getCell(i, "content_level"));
			service.setReferenceURL((String) data.getCell(i, "reference_url"));
			service.setCreated((String) data.getCell(i, "created"));
			service.setUpdated((String) data.getCell(i, "updated"));
			// TODO: Convert date format
			services.add(service);
		}
d11a0a1d   Nathanael Jourdane   Create package er...
108
109
		return services;
	}
cfbb6d07   Nathanael Jourdane   Implement most of...
110

76a922bc   Nathanael Jourdane   Improve Javadoc.
111
112
113
	/**
	 * @param query The query to get the service.
	 * @return The VOTableData
e669c5d3   Nathanael Jourdane   Improve Javadoc.
114
	 * @throws VOTableException Can not get the VOTable.
76a922bc   Nathanael Jourdane   Improve Javadoc.
115
	 */
e669c5d3   Nathanael Jourdane   Improve Javadoc.
116
	public static VOTableData getVoTableData(String query) throws VOTableException {
35daa117   Nathanael Jourdane   Improve JUnits te...
117
118
119
		return getVoTableData(getVoTable(query));
	}

76a922bc   Nathanael Jourdane   Improve Javadoc.
120
121
122
123
124
	/**
	 * Parse a VOTable and returns the corresponding VOTableData.
	 *
	 * @param voTable The VOTable parsed.
	 * @return The corresponding VOTableData.
e669c5d3   Nathanael Jourdane   Improve Javadoc.
125
	 * @throws VOTableException Can not get the VOTable.
76a922bc   Nathanael Jourdane   Improve Javadoc.
126
	 */
e669c5d3   Nathanael Jourdane   Improve Javadoc.
127
	public static VOTableData getVoTableData(VOTABLE voTable) throws VOTableException {
35daa117   Nathanael Jourdane   Improve JUnits te...
128
129
130
131
132
133
134
135
		VOTableController.checkVOTable(voTable);

		Table table = (Table) voTable.getRESOURCE().get(0).getLINKAndTABLEOrRESOURCE().get(0);
		VOTableDataParser dataParser = new VOTableDataParser(table);
		dataParser.parseData();
		return dataParser.getData();
	}

e669c5d3   Nathanael Jourdane   Improve Javadoc.
136
137
138
139
140
141
	/**
	 * @param query The query.
	 * @return The VOTable resulting the query.
	 * @throws VOTableException Can not get the VOTable.
	 */
	public static VOTABLE getVoTable(String query) throws VOTableException {
35daa117   Nathanael Jourdane   Improve JUnits te...
142
143
144
145
146
		VOTableController voTableCtrl = new VOTableController(Consts.DEFAULT_REGISTRY_URL, query);
		voTableCtrl.readTable();
		return voTableCtrl.getVOTable();
	}

e669c5d3   Nathanael Jourdane   Improve Javadoc.
147
148
149
150
151
152
	/**
	 * @param ivoid The ivoid of the service.
	 * @param parameters The parameters to get, separated by commas.
	 * @return A map of <parameter name, value> resulting the request.
	 * @throws VOTableException Can not get the VOTable.
	 */
35daa117   Nathanael Jourdane   Improve JUnits te...
153
	public static Map<String, Object> getParameters(String ivoid, String parameters)
e669c5d3   Nathanael Jourdane   Improve Javadoc.
154
			throws VOTableException {
35daa117   Nathanael Jourdane   Improve JUnits te...
155
156
157
158
		String query = String.format(Queries.SELECT_TAP_SERVICES_WHERE_IVOID, parameters, ivoid);
		return getVoTableData(query).getRowMap(0);
	}

e669c5d3   Nathanael Jourdane   Improve Javadoc.
159
160
161
162
163
164
	/**
	 * @param ivoid The ivoid of the service.
	 * @param parameters A list of parameters to get.
	 * @return A map of <parameter name, value> resulting the request.
	 * @throws VOTableException Can not get the VOTable.
	 */
35daa117   Nathanael Jourdane   Improve JUnits te...
165
	public static Map<String, Object> getParameters(String ivoid, List<String> parameters)
e669c5d3   Nathanael Jourdane   Improve Javadoc.
166
			throws VOTableException {
35daa117   Nathanael Jourdane   Improve JUnits te...
167
168
169
		return getParameters(ivoid, StringJoiner.join(parameters));
	}

e669c5d3   Nathanael Jourdane   Improve Javadoc.
170
171
172
173
174
175
176
177
178
	/**
	 * @param ivoid The ivoid of the service.
	 * @param parameter The unique parameter to get.
	 * @return The value of the parameter.
	 * @throws VOTableException Can not get the VOTable.
	 */
	public static Object getParameter(String ivoid, String parameter)
			throws VOTableException {
		String query = String.format(Queries.SELECT_TAP_SERVICES_WHERE_IVOID, parameter, ivoid);
35daa117   Nathanael Jourdane   Improve JUnits te...
179
		return getVoTableData(query).getCell(0, 0);
cfbb6d07   Nathanael Jourdane   Implement most of...
180
181
	}

e669c5d3   Nathanael Jourdane   Improve Javadoc.
182
183
184
185
186
187
	/**
	 * Convert a date from XMLGregorianCalendar to a string literal.
	 * 
	 * @param date The date to convert
	 * @return The serialized date.
	 */
5672c106   Nathanael Jourdane   Fix Sonar issues.
188
	private static String xmlDateToString(XMLGregorianCalendar date) {
cfbb6d07   Nathanael Jourdane   Implement most of...
189
190
191
		SimpleDateFormat sdf = new SimpleDateFormat(ServiceCtrl.DATE_FORMAT);
		return sdf.format(date.toGregorianCalendar().getTime());
	}
d11a0a1d   Nathanael Jourdane   Create package er...
192
}