Project

General

Profile

1
package eu.dnetlib.openaire.exporter.datasource;
2

    
3
import java.util.List;
4

    
5
import eu.dnetlib.common.rmi.DNetRestDocumentation;
6
import eu.dnetlib.openaire.exporter.AbstractExporterController;
7
import eu.dnetlib.openaire.exporter.datasource.clients.DatasourceDao;
8
import eu.dnetlib.openaire.exporter.model.datasource.BrowseTerm;
9
import eu.dnetlib.openaire.exporter.model.datasource.DatasourceResponse;
10
import eu.dnetlib.openaire.exporter.model.datasource.Response;
11
import eu.dnetlib.openaire.exporter.model.datasource.db.Api;
12
import eu.dnetlib.openaire.exporter.model.datasource.db.Datasource;
13
import eu.dnetlib.openaire.exporter.model.datasource.db.SearchInterfacesEntry;
14
import eu.dnetlib.openaire.exporter.vocabularies.Country;
15
import io.swagger.annotations.ApiOperation;
16
import io.swagger.annotations.ApiResponse;
17
import io.swagger.annotations.ApiResponses;
18
import org.apache.commons.lang.StringUtils;
19
import org.apache.commons.lang3.time.StopWatch;
20
import org.apache.commons.logging.Log;
21
import org.apache.commons.logging.LogFactory;
22
import org.apache.http.HttpStatus;
23
import org.springframework.beans.factory.annotation.Autowired;
24
import org.springframework.data.domain.PageRequest;
25
import org.springframework.web.bind.annotation.*;
26

    
27
@RestController
28
@DNetRestDocumentation
29
public class DatasourcesApiController extends AbstractExporterController implements DatasourcesApi {
30

    
31
	private static final Log log = LogFactory.getLog(DatasourcesApiController.class);
32

    
33
	@Autowired
34
	private DatasourceDao dsDao;
35

    
36
	@Override
37
	@RequestMapping(value = "/ds/list/{page}/{size}", produces = { "application/json" }, method = RequestMethod.GET)
38
    public List<String> listIds(@PathVariable int page, @PathVariable int size) throws ApiException {
39
	    return dsDao.listIds(new PageRequest(page, size));
40
    }
41

    
42
    @Override
43
    @RequestMapping(value = "/ds/get/{id}", produces = { "application/json" }, method = RequestMethod.GET)
44
    public DatasourceResponse getDs(@PathVariable final String id) {
45

    
46
	    if (log.isDebugEnabled()) {
47
		    log.debug(String.format("getDatasourceInfo(dsId = %s)", id));
48
	    }
49
	    final StopWatch stop = StopWatch.createStarted();
50
	    final Response rsp = dsDao.getDsById(id);
51
	    rsp.getHeader().setTime(stop.getTime());
52
	    if (log.isDebugEnabled()) {
53
	    	log.debug("getDs by id: " + rsp.getHeader().toJson());
54
	    }
55
    	return rsp.getDatasourceResponse().get(0);
56
    }
57

    
58
	@Override
59
	@RequestMapping(value = "/ds/search/name/{page}/{size}", produces = { "application/json" }, method = RequestMethod.GET)
60
	public List<DatasourceResponse> searchByName(
61
			@RequestParam final String name, @PathVariable final int page, @PathVariable final int size) {
62
		final StopWatch stop = StopWatch.createStarted();
63
		final Response rsp = dsDao.searchByName(name, new PageRequest(page, size));
64
		rsp.getHeader().setTime(stop.getTime());
65
		if (log.isDebugEnabled()) {
66
			log.debug("searchByName: " + rsp.getHeader().toJson());
67
		}
68
		return rsp.getDatasourceResponse();
69
	}
70

    
71
	@Override
72
	@RequestMapping(value = "/ds/search/email/{page}/{size}", produces = { "application/json" }, method = RequestMethod.GET)
73
	public List<DatasourceResponse> searchByContactemail(
74
			@RequestParam final String contactemail, @PathVariable final int page, @PathVariable final int size) {
75
		final StopWatch stop = StopWatch.createStarted();
76
		final Response rsp = dsDao.searchByContactemail(contactemail, new PageRequest(page, size));
77
		rsp.getHeader().setTime(stop.getTime());
78
		if (log.isDebugEnabled()) {
79
			log.debug("searchByContactemail: " + rsp.getHeader().toJson());
80
		}
81
		return rsp.getDatasourceResponse();
82
	}
83

    
84
	@Override
85
	@RequestMapping(value = "/ds/search/country/{page}/{size}", produces = { "application/json" }, method = RequestMethod.GET)
86
	public List<DatasourceResponse> searchByCountry(
87
			@RequestParam final String country, @PathVariable final int page, @PathVariable final int size) {
88
		final StopWatch stop = StopWatch.createStarted();
89
		final Response rsp = dsDao.searchByCountry(country, new PageRequest(page, size));
90
		rsp.getHeader().setTime(stop.getTime());
91
		if (log.isDebugEnabled()) {
92
			log.debug("searchByCountry: " + rsp.getHeader().toJson());
93
		}
94
		return rsp.getDatasourceResponse();
95
	}
96

    
97
	@Override
98
	@RequestMapping(value = "/ds/search/registeredby/{page}/{size}", produces = { "application/json" }, method = RequestMethod.GET)
99
	public List<DatasourceResponse> searchByRegisteringUser(final String registeredBy, final int page, final int size) {
100
		final StopWatch stop = StopWatch.createStarted();
101
		final Response rsp = dsDao.searchByRegisteringUser(registeredBy, new PageRequest(page, size));
102
		rsp.getHeader().setTime(stop.getTime());
103
		if (log.isDebugEnabled()) {
104
			log.debug("searchByRegisteringUser: " + rsp.getHeader().toJson());
105
		}
106
		return rsp.getDatasourceResponse();
107
	}
108

    
109
	@Override
110
	@RequestMapping(value = "/api/search/{field}", produces = { "application/json" }, method = RequestMethod.GET)
111
	public List<SearchInterfacesEntry> searchInterface(@PathVariable final String field, @RequestParam final String value) {
112
		return dsDao.searchInterface(field, value);
113
	}
114

    
115
	@Override
116
	@RequestMapping(value = "/api/baseurl/{page}/{size}", produces = { "application/json" }, method = RequestMethod.GET)
117
	public List<String> searchBaseUrls(@RequestParam final String userEmail, @RequestParam(required = false) final Boolean managed, @PathVariable final int page, @PathVariable final int size) throws ApiException {
118
		return dsDao.findBaseURLs(userEmail, managed, new PageRequest(page, size));
119
	}
120

    
121
	@Override
122
	@RequestMapping(value = "/api/browse/{field}", produces = { "application/json" }, method = RequestMethod.GET)
123
	public List<? extends BrowseTerm> browseField(@PathVariable final String field) throws ApiException {
124
		switch (field) {
125
		case "country":
126
			return dsDao.browseCountries();
127
		case "typology":
128
			return dsDao.browseTypologies();
129
		case "protocol":
130
			return dsDao.browseProtocols();
131
		case "compatibility":
132
			return dsDao.browseCompatibility();
133
		case "activation":
134
			return dsDao.browseActivation();
135
		default:
136
			throw new ApiException(HttpStatus.SC_BAD_REQUEST, String.format("unsupported browse field '%s'", field));
137
		}
138
	}
139

    
140
	@Override
141
	@RequestMapping(value = "/ds/countries", produces = { "application/json" }, method = RequestMethod.GET)
142
	public List<Country> listCountries() throws ApiException {
143
		return dsDao.listCountries();
144
	}
145

    
146
	@Override
147
	@RequestMapping(value = "/ds/api/{dsId}", produces = { "application/json" }, method = RequestMethod.GET)
148
	public List<Api> getApi(@PathVariable final String dsId) throws ApiException {
149
		return dsDao.getApi(dsId);
150
	}
151

    
152
	@Override
153
	@RequestMapping(value = "/ds/api/{dsId}", method = RequestMethod.DELETE)
154
	public void deleteApi(@PathVariable final String apiId) throws ApiException {
155
		dsDao.deleteApi(apiId);
156
	}
157

    
158
	@Override
159
	@RequestMapping(value = "/ds/api/add", method = RequestMethod.POST)
160
	public void addApi(@RequestParam final Api api) throws ApiException {
161
		if (StringUtils.isBlank(api.getDatasource())) {
162
			throw new ApiException(HttpStatus.SC_BAD_REQUEST, "missing datasource id");
163
		}
164
		dsDao.addApi(api);
165
	}
166

    
167
	@Override
168
	@RequestMapping(value = "/ds/manage", method = RequestMethod.POST)
169
	public void setManaged(@RequestParam final String id, @RequestParam final boolean managed) {
170
		dsDao.setManaged(id, managed);
171
	}
172

    
173
	@Override
174
	@RequestMapping(value = "/ds/add", method = RequestMethod.POST)
175
	public void saveDatasource(@RequestBody final Datasource datasource) throws ApiException {
176
		if (dsDao.exist(datasource)) { //TODO further check that the DS doesn't have any API
177
			throw new ApiException(HttpStatus.SC_CONFLICT, String.format("cannot already defined '%s'", datasource.getId()));
178
		}
179
		dsDao.save(datasource);
180
	}
181

    
182
	@Override
183
	@RequestMapping(value = "/ds/officialname", method = RequestMethod.POST)
184
	public void updateOfficialname(@RequestParam final String dsId, @RequestParam final String officialname) throws ApiException {
185
		dsDao.updateOfficialName(dsId, officialname);
186
	}
187

    
188
	@Override
189
	@RequestMapping(value = "/ds/englishname", method = RequestMethod.POST)
190
	public void updateEnglishname(@RequestParam final String dsId, @RequestParam final String englishname) throws ApiException {
191
		dsDao.updateEnglishName(dsId, englishname);
192
	}
193

    
194
	@Override
195
	@RequestMapping(value = "/ds/latitude", method = RequestMethod.POST)
196
	public void updateLatitude(@RequestParam final String dsId, @RequestParam final Double latitude) throws ApiException {
197
		dsDao.updateLatitude(dsId, latitude);
198
	}
199

    
200
	@Override
201
	@RequestMapping(value = "/ds/longitude", method = RequestMethod.POST)
202
	public void updateLongitude(@RequestParam final String dsId, @RequestParam final Double longitude) throws ApiException {
203
		dsDao.updateLongitude(dsId, longitude);
204
	}
205

    
206
	@Override
207
	@RequestMapping(value = "/ds/logourl", method = RequestMethod.POST)
208
	public void updateLogourl(@RequestParam final String dsId, @RequestParam final String logourl) throws ApiException {
209
		dsDao.updateLogourl(dsId, logourl);
210
	}
211

    
212
	@Override
213
	@RequestMapping(value = "/ds/timezone", method = RequestMethod.POST)
214
	public void updateTimezone(@RequestParam final String dsId, @RequestParam final String timezone) throws ApiException {
215
		dsDao.updateTimezone(dsId, timezone);
216
	}
217

    
218
	@Override
219
	@RequestMapping(value = "/ds/typology", method = RequestMethod.POST)
220
	public void updateTypology(@RequestParam final String dsId, @RequestParam final String typology) throws ApiException {
221
		dsDao.updateDatasourceTypology(dsId, typology);
222
	}
223

    
224
	@Override
225
	@RequestMapping(value = "/ds/registeredby", method = RequestMethod.POST)
226
	public void updateRegisteringUser(@RequestParam final String dsId, @RequestParam final String registeredBy) throws ApiException {
227
		dsDao.updateDatasourceRegisteringUser(dsId, registeredBy);
228
	}
229

    
230
	@ApiOperation(value = "drop the datasource manager caches", notes = "drop the datasource manager caches", httpMethod = "GET")
231
	@ApiResponses(value = {
232
			@ApiResponse(code = 200, message = "OK"),
233
			@ApiResponse(code = 500, message = "unexpected error") })
234
	@RequestMapping(value = "/ds/dropcache.do", method = RequestMethod.GET)
235
	public void dropDsCaches() {
236
		dsDao.dropCaches();
237
	}
238

    
239

    
240
}
(3-3/4)