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

153 lines
6.0 KiB
Java

package eu.eudat.controllers;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import javax.transaction.Transactional;
import eu.eudat.entities.Researcher;
import eu.eudat.models.external.ResearchersExternalSourcesModel;
import eu.eudat.models.external.ServiceExternalSourcesModel;
import eu.eudat.models.helpers.responses.ResponseItem;
import eu.eudat.services.ApiContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import eu.eudat.dao.entities.DMPDao;
import eu.eudat.dao.entities.DMPProfileDao;
import eu.eudat.dao.entities.DataRepositoryDao;
import eu.eudat.dao.entities.DatasetDao;
import eu.eudat.dao.entities.DatasetProfileDao;
import eu.eudat.dao.entities.DatasetProfileRulesetDao;
import eu.eudat.dao.entities.DatasetProfileViewstyleDao;
import eu.eudat.dao.entities.OrganisationDao;
import eu.eudat.dao.entities.ProjectDao;
import eu.eudat.dao.entities.RegistryDao;
import eu.eudat.dao.entities.ResearcherDao;
import eu.eudat.dao.entities.ServiceDao;
import eu.eudat.proxy.config.exceptions.HugeResultSet;
import eu.eudat.proxy.config.exceptions.NoURLFound;
import eu.eudat.proxy.fetching.RemoteFetcher;
@RestController
@CrossOrigin
public class Researchers extends BaseController{
@Autowired
public Researchers(ApiContext apiContext) {
super(apiContext);
}
@RequestMapping(method = RequestMethod.GET, value = { "/external/researchers" }, produces="application/json")
public @ResponseBody ResponseItem<ResearchersExternalSourcesModel> listExternalResearchers(@RequestParam(value="query", required=false) String query ){
try {
List<Map<String,String>> remoteRepos = this.getApiContext().getRemoteFetcher().getResearchers(query);
ResearchersExternalSourcesModel researchersExternalSourcesModel = new ResearchersExternalSourcesModel().fromExternalItem(remoteRepos);
return new ResponseItem<ResearchersExternalSourcesModel>().payload(researchersExternalSourcesModel).status(HttpStatus.OK);
}
catch(NoURLFound ex) {
return new ResponseItem<ResearchersExternalSourcesModel>().status(HttpStatus.BAD_REQUEST).message("External Url Not Found");
}
catch(HugeResultSet ex) {
return new ResponseItem<ResearchersExternalSourcesModel>().status(HttpStatus.BAD_REQUEST).message("Huge Result Set");
}catch (Exception ex){
return new ResponseItem<ResearchersExternalSourcesModel>().status(HttpStatus.BAD_REQUEST).message(ex.getMessage());
}
}
// MANAGE RESEARCHER(S)
@RequestMapping(method = RequestMethod.GET, value = { "/researchers" })
public @ResponseBody ResponseEntity<List<UUID>> listResearchers(){
try {
List<UUID> allIDs = this.getApiContext().getDatabaseRepository().getResearcherDao().listAllIDs();
return ResponseEntity.status(HttpStatus.OK).body(allIDs);
}
catch(Exception ex) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
}
}
@RequestMapping(method = RequestMethod.GET, value = { "/researchers/{id}" })
public @ResponseBody ResponseEntity<Researcher> getResearchers(@PathVariable("id") String id) {
try {
Researcher researcher = this.getApiContext().getDatabaseRepository().getResearcherDao().read(UUID.fromString(id));
return ResponseEntity.status(HttpStatus.OK).body(researcher);
}
catch(Exception ex) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
}
}
@RequestMapping(method = RequestMethod.GET, value = { "/researcher/getByEmail" }, produces="application/json")
public @ResponseBody ResponseEntity<Researcher> getResearcherByEmail(@RequestParam("email") String email){
try {
Researcher researcher = this.getApiContext().getDatabaseRepository().getResearcherDao().getResearcherByEmail(email);
return ResponseEntity.status(HttpStatus.OK).body(researcher);
}
catch(Exception ex) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
}
}
@RequestMapping(method = RequestMethod.GET, value = { "/researcher/getAll" }, produces="application/json")
public @ResponseBody ResponseEntity<List<Researcher>> getAllResearchers(){
try {
List<Researcher> allResearchers = this.getApiContext().getDatabaseRepository().getResearcherDao().getAll();
return ResponseEntity.status(HttpStatus.OK).body(allResearchers);
}
catch(Exception ex) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
}
}
@Transactional
@RequestMapping(method = RequestMethod.POST, value = { "/researcher/create" }, consumes = "application/json", produces="application/json")
public @ResponseBody ResponseEntity<Researcher> setResearcher(@RequestBody Researcher researcher) {
Researcher createdResearcher = this.getApiContext().getDatabaseRepository().getResearcherDao().update(researcher);
try {
return ResponseEntity.status(HttpStatus.CREATED).body(createdResearcher);
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
}
}
@RequestMapping(method = RequestMethod.POST, value = { "/researcher/delete" }, consumes = "application/json", produces="text/plain")
public @ResponseBody ResponseEntity<Object> delete(@RequestBody Researcher researcher) {
Researcher res = new Researcher();
res.setId(researcher.getId());
try {
this.getApiContext().getDatabaseRepository().getResearcherDao().delete(res);
return ResponseEntity.status(HttpStatus.CREATED).body("{\"msg\":\"Deleted researcher!\"}");
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"msg\":\"Could not delete researcher!\"}");
}
}
}