1
|
package eu.dnetlib.datasource.publisher;
|
2
|
|
3
|
import java.util.List;
|
4
|
|
5
|
import eu.dnetlib.common.rmi.DNetRestDocumentation;
|
6
|
import eu.dnetlib.datasource.publisher.clients.ClientResponse;
|
7
|
import eu.dnetlib.datasource.publisher.clients.DatasourceDao;
|
8
|
import eu.dnetlib.datasource.publisher.model.BrowseTerm;
|
9
|
import eu.dnetlib.datasource.publisher.model.DatasourceResponse;
|
10
|
import eu.dnetlib.datasource.publisher.model.db.Api;
|
11
|
import eu.dnetlib.datasource.publisher.model.db.Datasource;
|
12
|
import eu.dnetlib.datasource.publisher.model.db.SearchInterfacesEntry;
|
13
|
|
14
|
import org.apache.commons.lang.StringUtils;
|
15
|
import org.apache.commons.logging.Log;
|
16
|
import org.apache.commons.logging.LogFactory;
|
17
|
import org.apache.http.HttpStatus;
|
18
|
import org.springframework.beans.factory.annotation.Autowired;
|
19
|
import org.springframework.data.domain.PageRequest;
|
20
|
import org.springframework.web.bind.annotation.*;
|
21
|
|
22
|
@RestController
|
23
|
@DNetRestDocumentation
|
24
|
public class DatasourcesApiController implements DatasourcesApi {
|
25
|
|
26
|
private static final Log log = LogFactory.getLog(DatasourcesApiController.class);
|
27
|
|
28
|
@Autowired
|
29
|
private DatasourceDao dsDao;
|
30
|
|
31
|
@Override
|
32
|
@RequestMapping(value = "/ds/list/{page}/{size}", produces = { "application/json" }, method = RequestMethod.GET)
|
33
|
public List<String> listIds(@PathVariable int page, @PathVariable int size) throws ApiException {
|
34
|
return dsDao.listIds(new PageRequest(page, size));
|
35
|
}
|
36
|
|
37
|
@Override
|
38
|
@RequestMapping(value = "/ds/get/{id}", produces = { "application/json" }, method = RequestMethod.GET)
|
39
|
public DatasourceResponse getDs(@PathVariable final String id) {
|
40
|
|
41
|
if (log.isDebugEnabled()) {
|
42
|
log.debug(String.format("getDatasourceInfo(dsId = %s)", id));
|
43
|
}
|
44
|
|
45
|
final ClientResponse clientResponse = dsDao.getInfo(id);
|
46
|
return clientResponse.getDatasourceResponse();
|
47
|
}
|
48
|
|
49
|
@Override
|
50
|
@RequestMapping(value = "/ds/search/name/{page}/{size}", produces = { "application/json" }, method = RequestMethod.GET)
|
51
|
public List<DatasourceResponse> searchByName(
|
52
|
@RequestParam final String name, @PathVariable final int page, @PathVariable final int size) {
|
53
|
return dsDao.searchByName(name, new PageRequest(page, size));
|
54
|
}
|
55
|
|
56
|
@Override
|
57
|
@RequestMapping(value = "/ds/search/email/{page}/{size}", produces = { "application/json" }, method = RequestMethod.GET)
|
58
|
public List<DatasourceResponse> searchByContactemail(
|
59
|
@RequestParam final String contactemail, @PathVariable final int page, @PathVariable final int size) {
|
60
|
return dsDao.searchByContactemail(contactemail, new PageRequest(page, size));
|
61
|
}
|
62
|
|
63
|
@Override
|
64
|
@RequestMapping(value = "/ds/search/country/{page}/{size}", produces = { "application/json" }, method = RequestMethod.GET)
|
65
|
public List<DatasourceResponse> searchByCountry(
|
66
|
@RequestParam final String country, @PathVariable final int page, @PathVariable final int size) {
|
67
|
return dsDao.searchByCountry(country, new PageRequest(page, size));
|
68
|
}
|
69
|
|
70
|
@Override
|
71
|
@RequestMapping(value = "/api/search/{field}", produces = { "application/json" }, method = RequestMethod.GET)
|
72
|
public List<SearchInterfacesEntry> searchInterface(@PathVariable final String field, @RequestParam final String value) {
|
73
|
return dsDao.searchInterface(field, value);
|
74
|
}
|
75
|
|
76
|
@Override
|
77
|
@RequestMapping(value = "/api/browse/{field}", produces = { "application/json" }, method = RequestMethod.GET)
|
78
|
public List<? extends BrowseTerm> browseField(@PathVariable final String field) throws ApiException {
|
79
|
switch (field) {
|
80
|
case "country":
|
81
|
return dsDao.browseCountries();
|
82
|
case "typology":
|
83
|
return dsDao.browseTypologies();
|
84
|
case "protocol":
|
85
|
return dsDao.browseProtocols();
|
86
|
case "compatibility":
|
87
|
return dsDao.browseCompatibility();
|
88
|
case "activation":
|
89
|
return dsDao.browseActivation();
|
90
|
default:
|
91
|
throw new ApiException(HttpStatus.SC_BAD_REQUEST, String.format("unsupported browse field '%s'", field));
|
92
|
}
|
93
|
}
|
94
|
|
95
|
@Override
|
96
|
@RequestMapping(value = "/ds/api/{dsId}", produces = { "application/json" }, method = RequestMethod.GET)
|
97
|
public List<Api> getApi(@PathVariable final String dsId) throws ApiException {
|
98
|
return dsDao.getApi(dsId);
|
99
|
}
|
100
|
|
101
|
@Override
|
102
|
@RequestMapping(value = "/ds/api/{dsId}", method = RequestMethod.DELETE)
|
103
|
public void deleteApi(@PathVariable final String apiId) throws ApiException {
|
104
|
dsDao.deleteApi(apiId);
|
105
|
}
|
106
|
|
107
|
@Override
|
108
|
@RequestMapping(value = "/ds/api/add", method = RequestMethod.POST)
|
109
|
public void addApi(@RequestParam final Api api) throws ApiException {
|
110
|
if (StringUtils.isBlank(api.getDatasource())) {
|
111
|
throw new ApiException(HttpStatus.SC_BAD_REQUEST, "missing datasource id");
|
112
|
}
|
113
|
dsDao.addApi(api);
|
114
|
}
|
115
|
|
116
|
@Override
|
117
|
@RequestMapping(value = "/ds/manage", method = RequestMethod.POST)
|
118
|
public void setManaged(@RequestParam final String id, @RequestParam final boolean managed) {
|
119
|
dsDao.setManaged(id, managed);
|
120
|
}
|
121
|
|
122
|
@Override
|
123
|
@RequestMapping(value = "/ds/add", method = RequestMethod.POST)
|
124
|
public void saveDatasource(@RequestBody final Datasource datasource) throws ApiException {
|
125
|
if (dsDao.exist(datasource)) { //TODO further check that the DS doesn't have any API
|
126
|
throw new ApiException(HttpStatus.SC_CONFLICT, String.format("cannot already defined '%s'", datasource.getId()));
|
127
|
}
|
128
|
dsDao.save(datasource);
|
129
|
}
|
130
|
|
131
|
@Override
|
132
|
@RequestMapping(value = "/ds/update/officialname", method = RequestMethod.POST)
|
133
|
public void updateOfficialname(@RequestParam final String dsId, @RequestParam final String officialname) throws ApiException {
|
134
|
dsDao.updateOfficialName(dsId, officialname);
|
135
|
}
|
136
|
|
137
|
@Override
|
138
|
@RequestMapping(value = "/ds/update/englishname", method = RequestMethod.POST)
|
139
|
public void updateEnglishname(@RequestParam final String dsId, @RequestParam final String englishname) throws ApiException {
|
140
|
dsDao.updateEnglishName(dsId, englishname);
|
141
|
}
|
142
|
|
143
|
@Override
|
144
|
@RequestMapping(value = "/ds/update/latitude", method = RequestMethod.POST)
|
145
|
public void updateLatitude(final String dsId, final Double latitude) throws ApiException {
|
146
|
dsDao.updateLatitude(dsId, latitude);
|
147
|
}
|
148
|
|
149
|
@Override
|
150
|
@RequestMapping(value = "/ds/update/longitude", method = RequestMethod.POST)
|
151
|
public void updateLongitude(final String dsId, final Double longitude) throws ApiException {
|
152
|
dsDao.updateLongitude(dsId, longitude);
|
153
|
}
|
154
|
|
155
|
}
|