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

259 lines
9.0 KiB
Java

package rest;
import java.util.List;
import java.util.UUID;
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.Dataset;
import entities.DatasetProfile;
import entities.DatasetProfileRuleset;
import entities.Project;
import helpers.Transformers;
import responses.RestResponse;
@RestController
@CrossOrigin
public class Datasets {
@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 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);
RestResponse rr = new RestResponse("Created dataset with id: "+storedDataset.toString(), storedDataset.getId().toString());
return ResponseEntity.status(HttpStatus.CREATED).body(rr.toString());
}
catch(Exception e) {
e.printStackTrace();
reason += e.getMessage();
//try updating
try {
storedDataset = datasetDao.update(dataset);
RestResponse rr = new RestResponse("Updated dataset with id: "+storedDataset.toString(), storedDataset.getId().toString());
return ResponseEntity.status(HttpStatus.CREATED).body(rr.toString());
}
catch(Exception ex) {
ex.printStackTrace();
reason += (System.lineSeparator()+e.getMessage());
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Could not create or update Dataset! Reason: " + reason);
}
}
}
@RequestMapping(method = RequestMethod.POST, value = { "/deleteDataset" }, consumes = "application/json")
public @ResponseBody ResponseEntity<Object> deleteDataset(@RequestBody Dataset dataset) {
//if we want to make sure it won't cascade up to other (child) components, we can just unhook them by setting them = new ones
// e.g: DMP dmp = new DMP() and then dataset.setDMP(dmp)
try {
datasetDao.delete(dataset);
RestResponse rr = new RestResponse("Deleted dataset with id: "+dataset.getId().toString(), dataset.getId().toString());
return ResponseEntity.status(HttpStatus.OK).body(rr.toString());
}
catch(Exception e) {
e.printStackTrace();
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Could not delete Dataset! Reason: " + e.getMessage());
}
}
@RequestMapping(method = RequestMethod.GET, value = { "/assignDMPToDataset" })
public @ResponseBody ResponseEntity<Object> assignDMPToDataset(@RequestParam("datasetID") String datasetID, @RequestParam("dmpID") String dmpID) {
Dataset dataset = null;
try {
dataset = datasetDao.read(UUID.fromString(datasetID));
if(dataset==null || dataset.getId()==null) throw new Exception("Could not find a Dataset by this id");
DMP dmp = new DMP();
dmp.setId(UUID.fromString(dmpID));
dataset.setDmp(dmp);
datasetDao.update(dataset);
return ResponseEntity.status(HttpStatus.OK).build();
}
catch(Exception ex) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
}
}
/*
@RequestMapping(method = RequestMethod.GET, value = { "/createEmptyDataset" })
public @ResponseBody ResponseEntity<Object> createEmptyDataset() {
Dataset dataset = new Dataset();
dataset.setLabel("");
try {
dataset = datasetDao.create(dataset);
}
catch(Exception e) {
e.printStackTrace();
try {
dataset = datasetDao.update(dataset);
}
catch(Exception ex) {
ex.printStackTrace();
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("FAILED: reason: "+e.getMessage());
}
}
return ResponseEntity.status(HttpStatus.CREATED).body("Created Dataset with id: " + dataset.getId());
}
*/
//FETCH BY DATASET PROFILE
@RequestMapping(method = RequestMethod.GET, value = { "/datasetprofile/{id}" })
public @ResponseBody ResponseEntity<Object> getDatasetProfile(@PathVariable("id") String id) {
try {
DatasetProfile profile = datasetProfileDao.read(UUID.fromString(id));
return ResponseEntity.status(HttpStatus.OK).body(new ObjectMapper().writeValueAsString(profile));
}
catch(Exception ex) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Erroneous input: "+ex.getMessage());
}
}
@RequestMapping(method = RequestMethod.POST, value = { "/setDatasetProfile" }, consumes = "application/json")
public @ResponseBody ResponseEntity<Object> setDatasetProfile(@RequestBody DatasetProfile datasetProfile) {
String reason = "";
DatasetProfile storedDatasetProfile = null;
//try first to create
try {
storedDatasetProfile = datasetProfileDao.create(datasetProfile);
return ResponseEntity.status(HttpStatus.CREATED).body(new ObjectMapper().writeValueAsString(storedDatasetProfile));
}
catch(Exception e) {
reason += e.getMessage();
//try updating
try {
storedDatasetProfile = datasetProfileDao.update(datasetProfile);
return ResponseEntity.status(HttpStatus.CREATED).body(new ObjectMapper().writeValueAsString(storedDatasetProfile));
}
catch(Exception ex) {
reason += (System.lineSeparator()+e.getMessage());
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("An error occurred! Reason: " + reason);
}
}
}
/*
@RequestMapping(method = RequestMethod.GET, value = { "/createEmptyDatasetProfile" })
public @ResponseBody ResponseEntity<Object> createEmptyDatasetProfile() {
DatasetProfile datasetProfile = new DatasetProfile();
datasetProfile.setLabel("");
datasetProfile.setDefinition("");
try {
datasetProfile = datasetProfileDao.create(datasetProfile);
return ResponseEntity.status(HttpStatus.CREATED).body(new ObjectMapper().writeValueAsString(datasetProfile));
}
catch(Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("FAILED: reason: "+e.getMessage());
}
}
*/
}