2019-02-16 11:04:41 +01:00
package eu.dnetlib.repo.manager.controllers ;
2019-07-25 13:54:21 +02:00
import eu.dnetlib.api.functionality.ValidatorServiceException ;
2019-02-16 11:04:41 +01:00
import eu.dnetlib.domain.data.Repository ;
import eu.dnetlib.domain.data.RepositoryInterface ;
2019-11-28 12:25:56 +01:00
import eu.dnetlib.repo.manager.domain.* ;
2019-02-16 11:04:41 +01:00
import eu.dnetlib.repo.manager.exception.ResourceNotFoundException ;
import eu.dnetlib.repo.manager.service.RepositoryServiceImpl ;
import io.swagger.annotations.Api ;
import org.json.JSONException ;
2019-12-18 11:04:22 +01:00
import org.mitre.openid.connect.model.OIDCAuthenticationToken ;
2019-02-16 11:04:41 +01:00
import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.http.MediaType ;
2019-10-11 13:53:48 +02:00
import org.springframework.security.access.prepost.PostAuthorize ;
2019-02-16 11:04:41 +01:00
import org.springframework.security.access.prepost.PreAuthorize ;
import org.springframework.security.core.Authentication ;
2019-12-18 11:04:22 +01:00
import org.springframework.security.core.context.SecurityContextHolder ;
2019-02-16 11:04:41 +01:00
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
2019-07-22 15:00:51 +02:00
private RepositoryServiceImpl repositoryService ;
2019-02-16 11:04:41 +01:00
@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') " )
2019-12-13 14:40:55 +01:00
public List < RepositorySnippet > getRepositoriesOfUser ( @PathVariable ( " userEmail " ) String userEmail ,
2019-02-16 11:04:41 +01:00
@PathVariable ( " page " ) String page ,
2019-12-13 14:40:55 +01:00
@PathVariable ( " size " ) String size ) throws JSONException , IOException {
2019-12-18 11:04:22 +01:00
userEmail = ( ( OIDCAuthenticationToken ) SecurityContextHolder . getContext ( ) . getAuthentication ( ) ) . getUserInfo ( ) . getEmail ( ) ;
2019-12-13 14:40:55 +01:00
return repositoryService . getRepositoriesSnippetOfUser ( userEmail , page , size ) ;
2019-02-16 11:04:41 +01:00
}
2019-07-19 11:31:23 +02:00
@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 ) ;
}
2019-02-16 11:04:41 +01:00
@RequestMapping ( value = " /getRepositoryById/{id} " , method = RequestMethod . GET ,
produces = MediaType . APPLICATION_JSON_VALUE )
@ResponseBody
2019-11-08 13:56:47 +01:00
@PostAuthorize ( " hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN') or ((returnObject.registeredBy=='null' or returnObject.registeredBy==authentication.userInfo.email) and hasRole('ROLE_USER')) " )
2019-02-16 11:04:41 +01:00
public Repository getRepositoryById ( @PathVariable ( " id " ) String id ) throws JSONException , ResourceNotFoundException {
2019-11-08 13:56:47 +01:00
Repository repo = repositoryService . getRepositoryById ( id ) ;
return repo ;
2019-02-16 11:04:41 +01:00
}
@RequestMapping ( value = " /getRepositoryAggregations/{id} " , method = RequestMethod . GET ,
produces = MediaType . APPLICATION_JSON_VALUE )
@ResponseBody
public List < AggregationDetails > getRepositoryAggregations ( @PathVariable ( " id " ) String id ) throws JSONException {
2019-09-27 13:29:40 +02:00
return repositoryService . getRepositoryAggregations ( id , 0 , 20 ) ;
2019-02-16 11:04:41 +01:00
}
@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
2019-11-08 13:56:47 +01:00
@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')) " )
2019-02-16 11:04:41 +01:00
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
2019-11-08 13:56:47 +01:00
@PreAuthorize ( " hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN') or ((#repository.registeredBy==authentication.userInfo.email or returnObject.registeredBy=='null') and hasRole('ROLE_USER')) " )
2019-02-16 11:04:41 +01:00
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
2019-07-22 15:00:51 +02:00
public List < String > getDnetCountries ( ) {
2019-02-16 11:04:41 +01:00
return repositoryService . getDnetCountries ( ) ;
}
@RequestMapping ( value = " /getTypologies " , method = RequestMethod . GET ,
produces = MediaType . APPLICATION_JSON_VALUE )
@ResponseBody
2019-07-22 15:00:51 +02:00
public List < String > getTypologies ( ) {
2019-02-16 11:04:41 +01:00
return repositoryService . getTypologies ( ) ;
}
@RequestMapping ( value = " /getTimezones " , method = RequestMethod . GET ,
produces = MediaType . APPLICATION_JSON_VALUE )
@ResponseBody
2019-07-22 15:00:51 +02:00
public List < Timezone > getTimezones ( ) {
2019-02-16 11:04:41 +01:00
return repositoryService . getTimezones ( ) ;
}
@RequestMapping ( value = " /updateRepository " , method = RequestMethod . POST ,
consumes = MediaType . APPLICATION_JSON_VALUE )
@ResponseBody
2019-11-08 13:56:47 +01:00
@PreAuthorize ( " hasRole('ROLE_ADMIN') or hasRole('ROLE_PROVIDE_ADMIN') or ((#repository.registeredBy==authentication.userInfo.email or #repository.registeredBy=='null') and hasRole('ROLE_USER')) " )
2019-02-16 11:04:41 +01:00
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
2019-11-08 13:56:47 +01:00
@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')) " )
2019-02-16 11:04:41 +01:00
public RepositoryInterface addRepositoryInterface ( @RequestParam ( " datatype " ) String datatype ,
@RequestParam ( " repoId " ) String repoId ,
@RequestParam ( " registeredBy " ) String registeredBy ,
2019-07-25 13:54:21 +02:00
@RequestBody RepositoryInterface repositoryInterface ) throws JSONException , ResourceNotFoundException , ValidatorServiceException {
2019-02-16 11:04:41 +01:00
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 {
2019-12-18 11:04:22 +01:00
userEmail = ( ( OIDCAuthenticationToken ) SecurityContextHolder . getContext ( ) . getAuthentication ( ) ) . getUserInfo ( ) . getEmail ( ) ;
2019-02-16 11:04:41 +01:00
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
2019-11-08 13:56:47 +01:00
@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')) " )
2019-02-16 11:04:41 +01:00
public RepositoryInterface updateRepositoryInterface ( @RequestParam ( " repoId " ) String repoId ,
@RequestParam ( " registeredBy " ) String registeredBy ,
@RequestBody RepositoryInterface repositoryInterface ) throws Exception {
return repositoryService . updateRepositoryInterface ( repoId , registeredBy , repositoryInterface ) ;
}
}