argos/dmp-backend/src/main/java/eu/eudat/controllers/Researchers.java

68 lines
3.3 KiB
Java
Raw Normal View History

2017-12-15 00:01:26 +01:00
package eu.eudat.controllers;
2017-10-06 19:20:05 +02:00
2018-02-07 10:56:30 +01:00
import eu.eudat.exceptions.security.UnauthorisedException;
import eu.eudat.managers.ResearcherManager;
import eu.eudat.models.dmp.Researcher;
2017-12-19 15:09:49 +01:00
import eu.eudat.models.external.ResearchersExternalSourcesModel;
import eu.eudat.models.helpers.responses.ResponseItem;
2018-02-07 10:56:30 +01:00
import eu.eudat.models.security.Principal;
2018-02-16 11:34:02 +01:00
import eu.eudat.proxy.config.exceptions.HugeResultSet;
import eu.eudat.proxy.config.exceptions.NoURLFound;
2018-01-04 10:32:39 +01:00
import eu.eudat.services.ApiContext;
2018-01-23 16:21:38 +01:00
import eu.eudat.types.ApiMessageCode;
2017-10-06 19:20:05 +02:00
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
2018-02-07 10:56:30 +01:00
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
2017-10-06 19:20:05 +02:00
2018-02-16 11:34:02 +01:00
import java.util.List;
import java.util.Map;
2017-10-06 19:20:05 +02:00
@RestController
@CrossOrigin
2018-02-09 16:54:41 +01:00
@RequestMapping(value = {"/api"})
2018-02-16 11:34:02 +01:00
public class Researchers extends BaseController {
2018-01-04 10:32:39 +01:00
2018-02-16 11:34:02 +01:00
@Autowired
public Researchers(ApiContext apiContext) {
super(apiContext);
}
2018-01-04 10:32:39 +01:00
2018-02-16 11:34:02 +01:00
@RequestMapping(method = RequestMethod.GET, value = {"/external/researchers"}, produces = "application/json")
public @ResponseBody
ResponseEntity<ResponseItem<ResearchersExternalSourcesModel>> listExternalResearchers(@RequestParam(value = "query", required = false) String query) {
try {
2018-03-05 17:18:45 +01:00
List<Map<String, String>> remoteRepos = this.getApiContext().getOperationsContext().getRemoteFetcher().getResearchers(query);
2018-02-16 11:34:02 +01:00
ResearchersExternalSourcesModel researchersExternalSourcesModel = new ResearchersExternalSourcesModel().fromExternalItem(remoteRepos);
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<ResearchersExternalSourcesModel>().payload(researchersExternalSourcesModel).status(ApiMessageCode.NO_MESSAGE));
} catch (NoURLFound ex) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ResponseItem<ResearchersExternalSourcesModel>().status(ApiMessageCode.ERROR_MESSAGE).message("External Url Not Found"));
} catch (HugeResultSet ex) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ResponseItem<ResearchersExternalSourcesModel>().status(ApiMessageCode.ERROR_MESSAGE).message("Huge Result Set"));
} catch (Exception ex) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ResponseItem<ResearchersExternalSourcesModel>().status(ApiMessageCode.ERROR_MESSAGE).message(ex.getMessage()));
}
}
2017-10-06 19:20:05 +02:00
2018-02-16 11:34:02 +01:00
@Transactional
@RequestMapping(method = RequestMethod.POST, value = {"/researchers/create"}, consumes = "application/json", produces = "application/json")
public @ResponseBody
ResponseEntity<ResponseItem<Researcher>> create(@RequestBody eu.eudat.models.researcher.Researcher researcher, Principal principal) {
try {
ResearcherManager.create(this.getApiContext(), researcher);
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<Researcher>().status(ApiMessageCode.SUCCESS_MESSAGE));
} catch (UnauthorisedException e) {
e.printStackTrace();
throw e;
} catch (Exception e) {
e.printStackTrace();
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ResponseItem<Researcher>().status(ApiMessageCode.SUCCESS_MESSAGE).message(e.getMessage()));
}
}
2018-02-07 10:56:30 +01:00
2017-10-06 19:20:05 +02:00
}