uoa-repository-manager-service/src/main/java/eu/dnetlib/repo/manager/controllers/RepositoryController.java

224 lines
12 KiB
Java

package eu.dnetlib.repo.manager.controllers;
import eu.dnetlib.api.functionality.ValidatorServiceException;
import eu.dnetlib.domain.data.Repository;
import eu.dnetlib.domain.data.RepositoryInterface;
import eu.dnetlib.repo.manager.domain.*;
import eu.dnetlib.repo.manager.exception.ResourceNotFoundException;
import eu.dnetlib.repo.manager.service.RepositoryServiceImpl;
import io.swagger.annotations.Api;
import org.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.security.access.prepost.PostAuthorize;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;
import java.io.IOException;
import java.util.List;
import java.util.Map;
@RestController
@RequestMapping(value = "/repository")
@Api(description = "Repository API", tags = {"repository"})
public class RepositoryController {
@Autowired
private RepositoryServiceImpl repositoryService;
@RequestMapping(value = "/getCountries", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public Country[] getCountries() {
return repositoryService.getCountries();
}
@RequestMapping(value = "/getRepositoriesByCountry/{country}/{mode}", method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public List<RepositorySnippet> getRepositoriesByCountry(@PathVariable("country") String country,
@PathVariable("mode") String mode,
@RequestParam(value = "managed",required=false) Boolean managed) throws JSONException, IOException {
return repositoryService.getRepositoriesByCountry(country, mode, managed);
}
@RequestMapping(value = "/getRepositoriesOfUser/{userEmail}/{page}/{size}",method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
@PreAuthorize("hasRole('ROLE_USER')")
public List<Repository> getRepositoriesOfUser(@PathVariable("userEmail") String userEmail,
@PathVariable("page") String page,
@PathVariable("size") String size) throws JSONException {
return repositoryService.getRepositoriesOfUser(userEmail, page, size);
}
@RequestMapping(value = "/searchRegisteredRepositories/{page}/{size}",method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
@PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN')")
public List<RepositorySnippet> searchRegisteredRepositories(@RequestParam(name="country", required=false) String country,
@RequestParam(name="typology", required=false) String typology,
@RequestParam(name="englishName", required=false) String englishName,
@RequestParam(name="officialName", required=false) String officialName,
@RequestParam("requestSortBy") String requestSortBy,
@RequestParam("order") String order,
@PathVariable("page") int page,
@PathVariable("size") int pageSize) throws Exception {
return repositoryService.searchRegisteredRepositories(country, typology, englishName, officialName, requestSortBy, order, page, pageSize);
}
@RequestMapping(value = "/getRepositoryById/{id}", method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
@PostAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN') or ((returnObject.registeredBy=='null' or returnObject.registeredBy==authentication.userInfo.email) and hasRole('ROLE_USER'))")
public Repository getRepositoryById(@PathVariable("id") String id) throws JSONException,ResourceNotFoundException {
Repository repo = repositoryService.getRepositoryById(id);
return repo;
}
@RequestMapping(value = "/getRepositoryAggregations/{id}", method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public List<AggregationDetails> getRepositoryAggregations(@PathVariable("id") String id) throws JSONException {
return repositoryService.getRepositoryAggregations(id,0,20);
}
@RequestMapping(value = "/getRepositoryAggregationsByYear/{id}", method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public Map<String, List<AggregationDetails>> getRepositoryAggregationsByYear(@PathVariable("id") String id) throws JSONException {
return repositoryService.getRepositoryAggregationsByYear(id);
}
@RequestMapping(value = "/getRepositoriesByName/{name:.+}/{page}/{size}/", method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public List<Repository> getRepositoriesByName(@PathVariable("name") String name,
@PathVariable("page") String page,
@PathVariable("size") String size) throws JSONException {
return repositoryService.getRepositoriesByName(name, page, size);
}
@RequestMapping(value = "/getRepositoryInterface/{id}", method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
@PostAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN') or ((@repositoryService.getRepositoryById(#id).registeredBy==authentication.userInfo.email or @repositoryService.getRepositoryById(#id).registeredBy=='null' ) and hasRole('ROLE_USER'))")
public List<RepositoryInterface> getRepositoryInterface(@PathVariable("id") String id) throws JSONException {
return repositoryService.getRepositoryInterface(id);
}
@RequestMapping(value = "/addRepository", method = RequestMethod.POST,
consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
@PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN') or ((#repository.registeredBy==authentication.userInfo.email or returnObject.registeredBy=='null') and hasRole('ROLE_USER'))")
public Repository addRepository(@RequestParam("datatype") String datatype,
@RequestBody Repository repository) throws Exception {
return repositoryService.addRepository(datatype, repository);
}
@RequestMapping(value = "/getDnetCountries", method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public List<String> getDnetCountries(){
return repositoryService.getDnetCountries();
}
@RequestMapping(value = "/getTypologies", method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public List<String> getTypologies(){
return repositoryService.getTypologies();
}
@RequestMapping(value = "/getTimezones", method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public List<Timezone> getTimezones(){
return repositoryService.getTimezones();
}
@RequestMapping(value = "/updateRepository", method = RequestMethod.POST,
consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
@PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN') or ((#repository.registeredBy==authentication.userInfo.email or #repository.registeredBy=='null') and hasRole('ROLE_USER'))")
public Repository updateRepository(@RequestBody Repository repository,Authentication authentication) throws Exception {
return repositoryService.updateRepository(repository, authentication);
}
@RequestMapping(value = "/deleteInterface/", method = RequestMethod.DELETE)
@PreAuthorize("hasRole('ROLE_USER') and #registeredBy == authentication.userInfo.email")
public void deleteRepositoryInterface(@RequestParam("id") String id ,
@RequestParam("registeredBy") String registeredBy){
repositoryService.deleteRepositoryInterface(id, registeredBy);
}
@RequestMapping(value = "/addInterface", method = RequestMethod.POST,
consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
@PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN') or ((@repositoryService.getRepositoryById(#repoId).registeredBy==authentication.userInfo.email or @repositoryService.getRepositoryById(#repoId).registeredBy=='null') and hasRole('ROLE_USER'))")
public RepositoryInterface addRepositoryInterface(@RequestParam("datatype") String datatype,
@RequestParam("repoId") String repoId,
@RequestParam("registeredBy") String registeredBy,
@RequestBody RepositoryInterface repositoryInterface) throws JSONException, ResourceNotFoundException, ValidatorServiceException {
return repositoryService.addRepositoryInterface(datatype, repoId, registeredBy, repositoryInterface);
}
@RequestMapping(value = "/getUrlsOfUserRepos/{user_email}/{page}/{size}/",method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
@PreAuthorize("hasRole('ROLE_USER')")
public List<String> getUrlsOfUserRepos(@PathVariable("user_email") String userEmail,
@PathVariable("page") String page,
@PathVariable("size") String size) throws JSONException {
return repositoryService.getUrlsOfUserRepos(userEmail, page, size);
}
@RequestMapping(value = "/getDatasourceVocabularies/{mode}",method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public List<String> getDatasourceVocabularies(@PathVariable("mode") String mode) {
return repositoryService.getDatasourceVocabularies(mode);
}
@RequestMapping(value = "/getCompatibilityClasses/{mode}",method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public Map<String, String> getCompatibilityClasses(@PathVariable("mode") String mode) {
return repositoryService.getCompatibilityClasses(mode);
}
@RequestMapping(value = "/getDatasourceClasses/{mode}",method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public Map<String, String> getDatasourceClasses(@PathVariable("mode") String mode) {
return repositoryService.getDatasourceClasses(mode);
}
@RequestMapping(value = "/getMetricsInfoForRepository/{repoId}",method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public MetricsInfo getMetricsInfoForRepository(@PathVariable("repoId") String repoId) throws RepositoryServiceException {
return repositoryService.getMetricsInfoForRepository(repoId);
}
@RequestMapping(value = "/getListLatestUpdate/{mode}",method = RequestMethod.GET,
produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public Map<String, String> getListLatestUpdate(@PathVariable("mode") String mode) throws JSONException {
return repositoryService.getListLatestUpdate(mode);
}
@RequestMapping(value = "/updateRepositoryInterface", method = RequestMethod.POST,
consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
@PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN') or ((@repositoryService.getRepositoryById(#repoId).registeredBy==authentication.userInfo.email or @repositoryService.getRepositoryById(#repoId).registeredBy=='null') and hasRole('ROLE_USER'))")
public RepositoryInterface updateRepositoryInterface(@RequestParam("repoId") String repoId,
@RequestParam("registeredBy") String registeredBy,
@RequestBody RepositoryInterface repositoryInterface) throws Exception {
return repositoryService.updateRepositoryInterface(repoId, registeredBy, repositoryInterface);
}
}