argos/dmp-backend/src/main/java/rest/BackendInterface.java

328 lines
11 KiB
Java

package rest;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.UUID;
import org.hibernate.Hibernate;
import org.hibernate.Session;
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.transaction.annotation.Transactional;
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.DMPResearcherDao;
import dao.entities.DataRepositoryDao;
import dao.entities.DatasetDao;
import dao.entities.DatasetProfileDao;
import dao.entities.DatasetProfileRulesetDao;
import dao.entities.DatasetProfileViewstyleDao;
import dao.entities.DatasetRegistryDao;
import dao.entities.DatasetServiceDao;
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.DatasetProfileViewstyle;
import entities.DatasetRegistry;
import entities.DatasetService;
import entities.Organisation;
import entities.Project;
import entities.Registry;
import entities.Service;
import helpers.Transformers;
@RestController
@CrossOrigin
public class BackendInterface {
@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;
// FETCH BY DMP(S)
@RequestMapping(method = RequestMethod.GET, value = { "/DMP" }, produces="text/plain")
public @ResponseBody ResponseEntity<Object> listDMPs(){
try {
List<UUID> allIDs = dMPDao.listAllIDs();
return ResponseEntity.status(HttpStatus.OK).body(new ObjectMapper().writeValueAsString(allIDs));
}
catch(Exception ex) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Serialization issue: "+ex.getMessage());
}
}
@RequestMapping(method = RequestMethod.GET, value = { "/DMP/{id}" }, produces="application/json")
public @ResponseBody ResponseEntity<Object> getDMP(@PathVariable("id") String id){
try {
DMP dmp = dMPDao.read(UUID.fromString(id));
return ResponseEntity.status(HttpStatus.OK).body(new ObjectMapper().writeValueAsString(dmp));
}
catch(Exception ex) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Erroneous input: "+ex.getMessage());
}
}
/**
* This should be called on extreme cases. It's computationally intensive
*/
@RequestMapping(method = RequestMethod.GET, value = { "/getAllDMPs" }, produces="application/json")
public @ResponseBody ResponseEntity<Object> getAllDMPs(){
try {
List<DMP> allDMPs = dMPDao.getAll();
return ResponseEntity.status(HttpStatus.OK).body(new ObjectMapper().writeValueAsString(allDMPs));
}
catch(Exception ex) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Serialization issue: "+ex.getMessage());
}
}
@RequestMapping(method = RequestMethod.POST, value = { "/setDMP" }, consumes = "application/json", produces="text/plain")
public @ResponseBody ResponseEntity<Object> setDMP(@RequestBody DMP dmp) {
//This function is a little bit tricky to implement (due to the irregular ORM mappings of the hibernate).
// Please make changes only if you are sure about what you're altering.
Dataset dataset = SerializationUtils.clone(dmp.getDataset());
dmp.setDataset(null);
int failsDMP = 0;
String reasonDmp = "";
DMP storedDMP = null;
//try first to create DMP
try {
storedDMP = dMPDao.create(dmp);
}
catch(Exception e) {
e.printStackTrace();
failsDMP++;
reasonDmp += e.getMessage();
//try updating DMP
try {
storedDMP = dMPDao.update(dmp);
}
catch(Exception ex) {
reasonDmp += (System.lineSeparator()+e.getMessage());
failsDMP++;
ex.printStackTrace();
}
}
if(failsDMP==2)
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Could not create or update DMP! Reason: " + reasonDmp);
dataset.setDmp(storedDMP); //very important!
int failsDataset = 0;
String reasonDataset = "";
Dataset storedDataset = null;
if(dataset != null) {
//try first to create DMP
try {
storedDataset = datasetDao.create(dataset);
}
catch(Exception e) {
e.printStackTrace();
failsDataset++;
reasonDataset += e.getMessage();
//try updating DMP
try {
storedDataset = datasetDao.update(dataset);
}
catch(Exception ex) {
reasonDataset += (System.lineSeparator()+e.getMessage());
failsDataset++;
ex.printStackTrace();
}
}
}
if(failsDataset != 2) {
if(failsDMP==0)
return ResponseEntity.status(HttpStatus.CREATED).body("Created DMP with id: " + storedDMP.getId());
else if(failsDMP==1)
return ResponseEntity.status(HttpStatus.CREATED).body("Updated DMP with id: " + storedDMP.getId());
else
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Could not create DMP! Reason:"+ reasonDmp);
}
else {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Could not create DMP! Failed to create or update its Dataset. Reason: "+reasonDataset);
}
}
@RequestMapping(method = RequestMethod.POST, value = { "/setDMPByForm" }, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE, produces="text/plain")
public @ResponseBody ResponseEntity<Object> setDMPByForm(@RequestBody MultiValueMap<String,String> formData) {
DMP dmp = Transformers.createDMPfromMap(formData);
return setDMP(dmp);
}
// FETCH BY DATASET(S)
@RequestMapping(method = RequestMethod.GET, value = { "/dataset" })
public @ResponseBody ResponseEntity<Object> listDatasets(){
try {
List<UUID> allIDs = datasetDao.listAllIDs();
return ResponseEntity.status(HttpStatus.OK).body(new ObjectMapper().writeValueAsString(allIDs));
}
catch(Exception ex) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Serialization issue: "+ex.getMessage());
}
}
@RequestMapping(method = RequestMethod.GET, value = { "/dataset/{id}" })
public @ResponseBody ResponseEntity<Object> getDataset(@PathVariable("id") String id) {
try {
Dataset ds = datasetDao.read(UUID.fromString(id));
return ResponseEntity.status(HttpStatus.OK).body(new ObjectMapper().writeValueAsString(ds));
}
catch(Exception ex) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Erroneous input: "+ex.getMessage());
}
}
/**
* This should be called on extreme cases. It's computationally intensive
*/
@RequestMapping(method = RequestMethod.GET, value = { "/getAllDatasets" })
public @ResponseBody ResponseEntity<Object> getAllDatasets(){
try {
List<Dataset> allDatasets = datasetDao.getAll();
return ResponseEntity.status(HttpStatus.OK).body(new ObjectMapper().writeValueAsString(allDatasets));
}
catch(Exception ex) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Serialization issue: "+ex.getMessage());
}
}
@RequestMapping(method = RequestMethod.POST, value = { "/setDataset" }, consumes = "application/json")
public @ResponseBody ResponseEntity<Object> setDataset(@RequestBody Dataset dataset) {
String reason = "";
Dataset storedDataset = null;
//try first to create
try {
storedDataset = datasetDao.create(dataset);
return ResponseEntity.status(HttpStatus.CREATED).body("Created Dataset with id: " + storedDataset.getId());
}
catch(Exception e) {
reason += e.getMessage();
//try updating
try {
storedDataset = datasetDao.update(dataset);
return ResponseEntity.status(HttpStatus.CREATED).body("Updated Dataset with id: " + storedDataset.getId());
}
catch(Exception ex) {
reason += (System.lineSeparator()+e.getMessage());
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Could not create or update Dataset! Reason: " + reason);
}
}
}
// FETCH BY PROJECT(S)
@RequestMapping(method = RequestMethod.GET, value = { "/project" })
public @ResponseBody ResponseEntity<Object> listProjects(){
try {
List<UUID> allIDs = projectDao.listAllIDs();
return ResponseEntity.status(HttpStatus.OK).body(new ObjectMapper().writeValueAsString(allIDs));
}
catch(Exception ex) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Serialization issue: "+ex.getMessage());
}
}
@RequestMapping(method = RequestMethod.GET, value = { "/project/{id}" })
public @ResponseBody ResponseEntity<Object> getProject(@PathVariable("id") String id) {
try {
Project project = projectDao.read(UUID.fromString(id));
return ResponseEntity.status(HttpStatus.OK).body(new ObjectMapper().writeValueAsString(project));
}
catch(Exception ex) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Erroneous input: "+ex.getMessage());
}
}
@RequestMapping(method = RequestMethod.POST, value = { "/setProject" }, consumes = "application/json")
public @ResponseBody ResponseEntity<Object> setProject(@RequestBody Project project) {
String reason = "";
Project storedProject = null;
//try first to create
try {
storedProject = projectDao.create(project);
return ResponseEntity.status(HttpStatus.CREATED).body("Created project with id: " + storedProject.getId());
}
catch(Exception e) {
reason += e.getMessage();
//try updating
try {
storedProject = projectDao.update(project);
return ResponseEntity.status(HttpStatus.CREATED).body("Updated project with id: " + storedProject.getId());
}
catch(Exception ex) {
reason += (System.lineSeparator()+e.getMessage());
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Could not create or update project! Reason: " + reason);
}
}
}
}