82 lines
4.6 KiB
Java
82 lines
4.6 KiB
Java
package eu.eudat.controllers;
|
|
|
|
import eu.eudat.exceptions.security.UnauthorisedException;
|
|
import eu.eudat.logic.managers.ResearcherManager;
|
|
import eu.eudat.models.data.dmp.Researcher;
|
|
import eu.eudat.models.data.external.ResearchersExternalSourcesModel;
|
|
import eu.eudat.models.data.helpers.responses.ResponseItem;
|
|
import eu.eudat.data.query.items.item.researcher.ResearcherCriteriaRequest;
|
|
import eu.eudat.models.data.security.Principal;
|
|
import eu.eudat.logic.proxy.config.exceptions.HugeResultSet;
|
|
import eu.eudat.logic.proxy.config.exceptions.NoURLFound;
|
|
import eu.eudat.logic.services.ApiContext;
|
|
import eu.eudat.types.ApiMessageCode;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.http.HttpStatus;
|
|
import org.springframework.http.ResponseEntity;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
|
|
|
|
@RestController
|
|
@CrossOrigin
|
|
@RequestMapping(value = {"/api"})
|
|
|
|
public class Researchers extends BaseController {
|
|
|
|
@Autowired
|
|
public Researchers(ApiContext apiContext) {
|
|
super(apiContext);
|
|
}
|
|
|
|
@RequestMapping(method = RequestMethod.GET, value = {"/external/researchers"}, produces = "application/json")
|
|
public @ResponseBody
|
|
ResponseEntity<ResponseItem<ResearchersExternalSourcesModel>> listExternalResearchers(
|
|
@RequestParam(value = "query", required = false) String query,@RequestParam(value = "type", required = false) String type) {
|
|
try {
|
|
List<Map<String, String>> remoteRepos = this.getApiContext().getOperationsContext().getRemoteFetcher().getResearchers(query,type);
|
|
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()));
|
|
}
|
|
}
|
|
|
|
@RequestMapping(method = RequestMethod.POST, value = {"/researchers/getWithExternal"}, consumes = "application/json", produces = "application/json")
|
|
public @ResponseBody
|
|
ResponseEntity<ResponseItem<List<eu.eudat.models.data.dmp.Researcher>>> getWithExternal(@RequestBody ResearcherCriteriaRequest researcherCriteriaRequest, Principal principal) {
|
|
try {
|
|
List<eu.eudat.models.data.dmp.Researcher> dataTable = new ResearcherManager().getCriteriaWithExternal(this.getApiContext(),this.getApiContext().getOperationsContext().getRemoteFetcher(), researcherCriteriaRequest);
|
|
return ResponseEntity.status(HttpStatus.OK).body(new ResponseItem<List<eu.eudat.models.data.dmp.Researcher>>().payload(dataTable).status(ApiMessageCode.NO_MESSAGE));
|
|
} catch (Exception ex) {
|
|
ex.printStackTrace();
|
|
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(new ResponseItem<List<eu.eudat.models.data.dmp.Researcher>>().status(ApiMessageCode.DEFAULT_ERROR_MESSAGE).message(ex.getMessage()));
|
|
}
|
|
}
|
|
|
|
@Transactional
|
|
@RequestMapping(method = RequestMethod.POST, value = {"/researchers/create"}, consumes = "application/json", produces = "application/json")
|
|
public @ResponseBody
|
|
ResponseEntity<ResponseItem<Researcher>> create(@RequestBody eu.eudat.models.data.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()));
|
|
}
|
|
}
|
|
|
|
}
|
|
|