package eu.dnetlib.repo.manager.controllers; import eu.dnetlib.domain.data.Repository; import eu.dnetlib.domain.data.RepositoryInterface; import eu.dnetlib.repo.manager.domain.RepositorySnippet; import eu.dnetlib.repo.manager.exception.ResourceNotFoundException; import eu.dnetlib.repo.manager.service.RepositoryServiceImpl; import eu.dnetlib.repo.manager.shared.*; 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.PreAuthorize; import org.springframework.security.core.Authentication; import org.springframework.web.bind.annotation.*; import javax.ws.rs.Path; 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 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 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 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 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 public Repository getRepositoryById(@PathVariable("id") String id) throws JSONException,ResourceNotFoundException { return repositoryService.getRepositoryById(id); } @RequestMapping(value = "/getRepositoryAggregations/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody public List getRepositoryAggregations(@PathVariable("id") String id) throws JSONException { return repositoryService.getRepositoryAggregations(id); } @RequestMapping(value = "/getRepositoryAggregationsByYear/{id}", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody public Map> 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 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 public List 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_USER') and #repository.registeredBy == authentication.userInfo.email") 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 List getDnetCountries(){ return repositoryService.getDnetCountries(); } @RequestMapping(value = "/getTypologies", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody List getTypologies(){ return repositoryService.getTypologies(); } @RequestMapping(value = "/getTimezones", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody List getTimezones(){ return repositoryService.getTimezones(); } @RequestMapping(value = "/updateRepository", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE) @ResponseBody //@PreAuthorize("hasRole('ROLE_USER') and #repository.registeredBy == authentication.userInfo.email") 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_USER') and #registeredBy == authentication.userInfo.email") public RepositoryInterface addRepositoryInterface(@RequestParam("datatype") String datatype, @RequestParam("repoId") String repoId, @RequestParam("registeredBy") String registeredBy, @RequestBody RepositoryInterface repositoryInterface) throws JSONException,ResourceNotFoundException { 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 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 getDatasourceVocabularies(@PathVariable("mode") String mode) { return repositoryService.getDatasourceVocabularies(mode); } @RequestMapping(value = "/getCompatibilityClasses/{mode}",method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody public Map getCompatibilityClasses(@PathVariable("mode") String mode) { return repositoryService.getCompatibilityClasses(mode); } @RequestMapping(value = "/getDatasourceClasses/{mode}",method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) @ResponseBody public Map 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 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_USER') and #registeredBy == authentication.userInfo.email") public RepositoryInterface updateRepositoryInterface(@RequestParam("repoId") String repoId, @RequestParam("registeredBy") String registeredBy, @RequestBody RepositoryInterface repositoryInterface) throws Exception { return repositoryService.updateRepositoryInterface(repoId, registeredBy, repositoryInterface); } }