argos/dmp-backend/src/main/java/rest/entities/Researchers.java

177 lines
6.5 KiB
Java
Raw Normal View History

2017-10-12 14:04:38 +02:00
package rest.entities;
2017-10-06 19:20:05 +02:00
import java.util.List;
import java.util.Map;
2017-10-06 19:20:05 +02:00
import java.util.UUID;
2017-10-17 13:45:11 +02:00
import java.util.stream.Collectors;
import javax.transaction.Transactional;
2017-10-06 19:20:05 +02:00
import org.apache.commons.lang3.SerializationUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.MultiValueMap;
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 com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import dao.entities.DMPDao;
import dao.entities.DMPProfileDao;
import dao.entities.DataRepositoryDao;
import dao.entities.DatasetDao;
import dao.entities.DatasetProfileDao;
import dao.entities.DatasetProfileRulesetDao;
import dao.entities.DatasetProfileViewstyleDao;
import dao.entities.OrganisationDao;
import dao.entities.ProjectDao;
import dao.entities.RegistryDao;
import dao.entities.ResearcherDao;
import dao.entities.ServiceDao;
import entities.DMP;
import entities.DMPProfile;
import entities.DataRepository;
import entities.Dataset;
import entities.DatasetProfile;
import entities.DatasetProfileRuleset;
import entities.Organisation;
import entities.Project;
import entities.Registry;
import entities.Researcher;
import entities.Service;
import helpers.SerializerProvider;
2017-10-06 19:20:05 +02:00
import helpers.Transformers;
import proxy.config.exceptions.HugeResultSet;
import proxy.config.exceptions.NoURLFound;
import proxy.fetching.RemoteFetcher;
2017-10-06 19:20:05 +02:00
import responses.RestResponse;
@RestController
@CrossOrigin
public class Researchers {
@Autowired private DataRepositoryDao dataRepositoryDao;
@Autowired private DatasetDao datasetDao;
@Autowired private DatasetProfileDao datasetProfileDao;
@Autowired private DatasetProfileRulesetDao datasetProfileRulesetDao;
@Autowired private DatasetProfileViewstyleDao datasetProfileViewstyleDao;
@Autowired private DMPDao dMPDao;
@Autowired private DMPProfileDao dMPProfileDao;
@Autowired private OrganisationDao organisationDao;
@Autowired private ProjectDao projectDao;
@Autowired private RegistryDao registryDao;
@Autowired private ResearcherDao researcherDao;
@Autowired private ServiceDao serviceDao;
@Autowired private RemoteFetcher remoteFetcher;
2017-11-22 11:14:10 +01:00
@RequestMapping(method = RequestMethod.GET, value = { "/external/researchers" }, produces="application/json")
public @ResponseBody ResponseEntity<Object> listExternalResearchers(@RequestParam(value="query", required=false) String query ){
try {
List<Map<String,String>> remoteRepos = remoteFetcher.getResearchers(query);
return ResponseEntity.status(HttpStatus.OK).body(SerializerProvider.toJson(remoteRepos));
}
catch(NoURLFound ex) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"reason\":\""+ex.getMessage()+"\"}");
}
catch(HugeResultSet ex) {
return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body("{\"reason\":\""+ex.getMessage()+"\"}"); //the ex.getMessage has the appropriate text description
}
}
2017-10-06 19:20:05 +02:00
// MANAGE RESEARCHER(S)
@RequestMapping(method = RequestMethod.GET, value = { "/researchers" })
public @ResponseBody ResponseEntity<Object> listResearchers(){
try {
List<UUID> allIDs = researcherDao.listAllIDs();
return ResponseEntity.status(HttpStatus.OK).body(SerializerProvider.toJson(allIDs));
2017-10-06 19:20:05 +02:00
}
catch(Exception ex) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Serialization issue: "+ex.getMessage());
}
}
@RequestMapping(method = RequestMethod.GET, value = { "/researchers/{id}" })
public @ResponseBody ResponseEntity<Object> getResearchers(@PathVariable("id") String id) {
try {
Researcher researcher = researcherDao.read(UUID.fromString(id));
return ResponseEntity.status(HttpStatus.OK).body(SerializerProvider.toJson(researcher));
2017-10-06 19:20:05 +02:00
}
catch(Exception ex) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Erroneous input: "+ex.getMessage());
}
}
2017-10-18 09:58:37 +02:00
@RequestMapping(method = RequestMethod.GET, value = { "/researcher/getByEmail" }, produces="application/json")
public @ResponseBody ResponseEntity<Object> getResearcherByEmail(@RequestParam("email") String email){
try {
Researcher researcher = researcherDao.getResearcherByEmail(email);
return ResponseEntity.status(HttpStatus.OK).body(SerializerProvider.toJson(researcher));
2017-10-18 09:58:37 +02:00
}
catch(Exception ex) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Erroneous input: "+ex.getMessage());
}
}
2017-10-17 13:45:11 +02:00
@RequestMapping(method = RequestMethod.GET, value = { "/researcher/getAll" }, produces="application/json")
public @ResponseBody ResponseEntity<Object> getAllResearchers(){
try {
List<Researcher> allResearchers = researcherDao.getAll();
return new ResponseEntity<Object>(SerializerProvider.toJson(allResearchers), HttpStatus.OK);
2017-10-17 13:45:11 +02:00
}
catch(Exception ex) {
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
2017-10-06 19:20:05 +02:00
2017-10-17 13:45:11 +02:00
@Transactional
@RequestMapping(method = RequestMethod.POST, value = { "/researcher/create" }, consumes = "application/json", produces="application/json")
2017-10-06 19:20:05 +02:00
public @ResponseBody ResponseEntity<Object> setResearcher(@RequestBody Researcher researcher) {
2017-10-17 13:45:11 +02:00
Researcher createdResearcher = researcherDao.update(researcher);
2017-10-06 19:20:05 +02:00
try {
return ResponseEntity.status(HttpStatus.CREATED).body(SerializerProvider.toJson(createdResearcher));
} catch (Exception e) {
2017-10-17 13:45:11 +02:00
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"msg\":\"Could not create researcher!\"}");
}
}
@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 {
researcherDao.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!\"}");
2017-10-06 19:20:05 +02:00
}
2017-10-17 13:45:11 +02:00
2017-10-06 19:20:05 +02:00
}
}