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.ClientResponse;
8
import eu.dnetlib.openaire.exporter.datasource.clients.DatasourceDao;
9
import eu.dnetlib.openaire.exporter.model.datasource.BrowseTerm;
10
import eu.dnetlib.openaire.exporter.model.datasource.DatasourceResponse;
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.logging.Log;
20
import org.apache.commons.logging.LogFactory;
21
import org.apache.http.HttpStatus;
22
import org.springframework.beans.factory.annotation.Autowired;
23
import org.springframework.data.domain.PageRequest;
24
import org.springframework.web.bind.annotation.*;
25

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

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

    
32
	@Autowired
33
	private DatasourceDao dsDao;
34

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

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

    
45
	    if (log.isDebugEnabled()) {
46
		    log.debug(String.format("getDatasourceInfo(dsId = %s)", id));
47
	    }
48

    
49
	    final ClientResponse clientResponse = dsDao.getDsById(id);
50
    	return clientResponse.getDatasourceResponse();
51
    }
52

    
53
	@Override
54
	@RequestMapping(value = "/ds/search/name/{page}/{size}", produces = { "application/json" }, method = RequestMethod.GET)
55
	public List<DatasourceResponse> searchByName(
56
			@RequestParam final String name, @PathVariable final int page, @PathVariable final int size) {
57
		return dsDao.searchByName(name, new PageRequest(page, size));
58
	}
59

    
60
	@Override
61
	@RequestMapping(value = "/ds/search/email/{page}/{size}", produces = { "application/json" }, method = RequestMethod.GET)
62
	public List<DatasourceResponse> searchByContactemail(
63
			@RequestParam final String contactemail, @PathVariable final int page, @PathVariable final int size) {
64
		return dsDao.searchByContactemail(contactemail, new PageRequest(page, size));
65
	}
66

    
67
	@Override
68
	@RequestMapping(value = "/ds/search/country/{page}/{size}", produces = { "application/json" }, method = RequestMethod.GET)
69
	public List<DatasourceResponse> searchByCountry(
70
			@RequestParam final String country, @PathVariable final int page, @PathVariable final int size) {
71
		return dsDao.searchByCountry(country, new PageRequest(page, size));
72
	}
73

    
74
	@Override
75
	@RequestMapping(value = "/api/search/{field}", produces = { "application/json" }, method = RequestMethod.GET)
76
	public List<SearchInterfacesEntry> searchInterface(@PathVariable final String field, @RequestParam final String value) {
77
		return dsDao.searchInterface(field, value);
78
	}
79

    
80
	@Override
81
	@RequestMapping(value = "/api/browse/{field}", produces = { "application/json" }, method = RequestMethod.GET)
82
	public List<? extends BrowseTerm> browseField(@PathVariable final String field) throws ApiException {
83
		switch (field) {
84
		case "country":
85
			return dsDao.browseCountries();
86
		case "typology":
87
			return dsDao.browseTypologies();
88
		case "protocol":
89
			return dsDao.browseProtocols();
90
		case "compatibility":
91
			return dsDao.browseCompatibility();
92
		case "activation":
93
			return dsDao.browseActivation();
94
		default:
95
			throw new ApiException(HttpStatus.SC_BAD_REQUEST, String.format("unsupported browse field '%s'", field));
96
		}
97
	}
98

    
99
	@Override
100
	@RequestMapping(value = "/ds/countries", produces = { "application/json" }, method = RequestMethod.GET)
101
	public List<Country> listCountries() throws ApiException {
102
		return dsDao.listCountries();
103
	}
104

    
105
	@Override
106
	@RequestMapping(value = "/ds/api/{dsId}", produces = { "application/json" }, method = RequestMethod.GET)
107
	public List<Api> getApi(@PathVariable final String dsId) throws ApiException {
108
		return dsDao.getApi(dsId);
109
	}
110

    
111
	@Override
112
	@RequestMapping(value = "/ds/api/{dsId}", method = RequestMethod.DELETE)
113
	public void deleteApi(@PathVariable final String apiId) throws ApiException {
114
		dsDao.deleteApi(apiId);
115
	}
116

    
117
	@Override
118
	@RequestMapping(value = "/ds/api/add", method = RequestMethod.POST)
119
	public void addApi(@RequestParam final Api api) throws ApiException {
120
		if (StringUtils.isBlank(api.getDatasource())) {
121
			throw new ApiException(HttpStatus.SC_BAD_REQUEST, "missing datasource id");
122
		}
123
		dsDao.addApi(api);
124
	}
125

    
126
	@Override
127
	@RequestMapping(value = "/ds/manage", method = RequestMethod.POST)
128
	public void setManaged(@RequestParam final String id, @RequestParam final boolean managed) {
129
		dsDao.setManaged(id, managed);
130
	}
131

    
132
	@Override
133
	@RequestMapping(value = "/ds/add", method = RequestMethod.POST)
134
	public void saveDatasource(@RequestBody final Datasource datasource) throws ApiException {
135
		if (dsDao.exist(datasource)) { //TODO further check that the DS doesn't have any API
136
			throw new ApiException(HttpStatus.SC_CONFLICT, String.format("cannot already defined '%s'", datasource.getId()));
137
		}
138
		dsDao.save(datasource);
139
	}
140

    
141
	@Override
142
	@RequestMapping(value = "/ds/update/officialname", method = RequestMethod.POST)
143
	public void updateOfficialname(@RequestParam final String dsId, @RequestParam final String officialname) throws ApiException {
144
		dsDao.updateOfficialName(dsId, officialname);
145
	}
146

    
147
	@Override
148
	@RequestMapping(value = "/ds/update/englishname", method = RequestMethod.POST)
149
	public void updateEnglishname(@RequestParam final String dsId, @RequestParam final String englishname) throws ApiException {
150
		dsDao.updateEnglishName(dsId, englishname);
151
	}
152

    
153
	@Override
154
	@RequestMapping(value = "/ds/update/latitude", method = RequestMethod.POST)
155
	public void updateLatitude(final String dsId, final Double latitude) throws ApiException {
156
		dsDao.updateLatitude(dsId, latitude);
157
	}
158

    
159
	@Override
160
	@RequestMapping(value = "/ds/update/longitude", method = RequestMethod.POST)
161
	public void updateLongitude(final String dsId, final Double longitude) throws ApiException {
162
		dsDao.updateLongitude(dsId, longitude);
163
	}
164

    
165
	@ApiOperation(value = "drop the datasource manager caches", notes = "drop the datasource manager caches", httpMethod = "GET")
166
	@ApiResponses(value = {
167
			@ApiResponse(code = 200, message = "OK"),
168
			@ApiResponse(code = 500, message = "unexpected error") })
169
	@RequestMapping(value = "/ds/dropcache.do", method = RequestMethod.GET)
170
	public void dropDsCaches() {
171
		dsDao.dropCaches();
172
	}
173

    
174

    
175
}
(3-3/4)