2017-10-12 14:04:38 +02:00
|
|
|
package rest.entities;
|
2017-10-04 11:48:21 +02:00
|
|
|
|
2017-10-18 01:26:27 +02:00
|
|
|
import java.io.IOException;
|
|
|
|
import java.io.PrintStream;
|
2017-10-20 17:11:40 +02:00
|
|
|
import java.util.HashSet;
|
2017-10-04 11:48:21 +02:00
|
|
|
import java.util.List;
|
2017-10-20 17:11:40 +02:00
|
|
|
import java.util.Set;
|
2017-10-04 11:48:21 +02:00
|
|
|
import java.util.UUID;
|
2017-10-20 12:03:55 +02:00
|
|
|
import java.util.stream.Collectors;
|
2017-10-04 11:48:21 +02:00
|
|
|
|
2017-10-18 01:26:27 +02:00
|
|
|
import javax.transaction.Transactional;
|
|
|
|
|
2017-10-04 11:48:21 +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;
|
|
|
|
|
2017-10-18 01:26:27 +02:00
|
|
|
import com.fasterxml.jackson.core.JsonParseException;
|
2017-10-04 11:48:21 +02:00
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
2017-10-18 01:26:27 +02:00
|
|
|
import com.fasterxml.jackson.databind.JsonMappingException;
|
2017-10-04 11:48:21 +02:00
|
|
|
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;
|
2017-10-18 01:26:27 +02:00
|
|
|
import entities.DMPProfile;
|
2017-10-04 11:48:21 +02:00
|
|
|
import entities.Dataset;
|
|
|
|
import entities.DatasetProfile;
|
|
|
|
import entities.DatasetProfileRuleset;
|
2017-10-18 01:26:27 +02:00
|
|
|
import entities.DatasetProfileViewstyle;
|
2017-10-20 12:03:55 +02:00
|
|
|
import entities.Organisation;
|
2017-10-04 11:48:21 +02:00
|
|
|
import entities.Project;
|
2017-10-20 17:11:40 +02:00
|
|
|
import helpers.SerializerProvider;
|
2017-10-04 11:48:21 +02:00
|
|
|
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;
|
|
|
|
|
2017-10-20 17:11:40 +02:00
|
|
|
private ObjectMapper objectMapper = SerializerProvider.getJsonSerializer();
|
2017-10-04 11:48:21 +02:00
|
|
|
|
|
|
|
// FETCH BY DATASET(S)
|
|
|
|
|
2017-10-23 18:06:24 +02:00
|
|
|
|
|
|
|
@RequestMapping(method = RequestMethod.GET, value = { "/datasets" })
|
2017-10-04 11:48:21 +02:00
|
|
|
public @ResponseBody ResponseEntity<Object> listDatasets(){
|
|
|
|
try {
|
|
|
|
List<UUID> allIDs = datasetDao.listAllIDs();
|
2017-10-20 17:11:40 +02:00
|
|
|
return ResponseEntity.status(HttpStatus.OK).body(objectMapper.writeValueAsString(allIDs));
|
2017-10-04 11:48:21 +02:00
|
|
|
}
|
|
|
|
catch(Exception ex) {
|
|
|
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Serialization issue: "+ex.getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-23 18:06:24 +02:00
|
|
|
@RequestMapping(method = RequestMethod.GET, value = { "/datasets/{id}" })
|
2017-10-04 11:48:21 +02:00
|
|
|
public @ResponseBody ResponseEntity<Object> getDataset(@PathVariable("id") String id) {
|
|
|
|
try {
|
|
|
|
Dataset ds = datasetDao.read(UUID.fromString(id));
|
2017-10-20 17:11:40 +02:00
|
|
|
return ResponseEntity.status(HttpStatus.OK).body(objectMapper.writeValueAsString(ds));
|
2017-10-04 11:48:21 +02:00
|
|
|
}
|
|
|
|
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
|
|
|
|
*/
|
2017-10-23 18:06:24 +02:00
|
|
|
@RequestMapping(method = RequestMethod.GET, value = { "/dataset/getAll" })
|
2017-10-04 11:48:21 +02:00
|
|
|
public @ResponseBody ResponseEntity<Object> getAllDatasets(){
|
2017-10-20 12:03:55 +02:00
|
|
|
|
2017-10-04 11:48:21 +02:00
|
|
|
try {
|
|
|
|
List<Dataset> allDatasets = datasetDao.getAll();
|
2017-10-20 12:03:55 +02:00
|
|
|
|
2017-10-24 18:33:51 +02:00
|
|
|
|
2017-10-20 12:03:55 +02:00
|
|
|
//sorry for that, spring-jersey serialisation has issues when performed on tables, so -> custom
|
2017-10-24 18:33:51 +02:00
|
|
|
List<String> datasetsStrL = allDatasets.stream().map((datasetObj) -> {
|
2017-10-20 12:03:55 +02:00
|
|
|
try {
|
|
|
|
return objectMapper.writeValueAsString(datasetObj);
|
|
|
|
} catch (JsonProcessingException e) {
|
2017-10-24 18:33:51 +02:00
|
|
|
e.printStackTrace();
|
2017-10-20 12:03:55 +02:00
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}).collect(Collectors.toList());
|
|
|
|
|
|
|
|
return new ResponseEntity<Object>("["+String.join(",", datasetsStrL)+"]", HttpStatus.OK);
|
2017-10-04 11:48:21 +02:00
|
|
|
}
|
|
|
|
catch(Exception ex) {
|
2017-10-24 18:33:51 +02:00
|
|
|
ex.printStackTrace();
|
2017-10-20 12:03:55 +02:00
|
|
|
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
|
2017-10-04 11:48:21 +02:00
|
|
|
}
|
2017-10-20 12:03:55 +02:00
|
|
|
|
2017-10-04 11:48:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-23 18:06:24 +02:00
|
|
|
@RequestMapping(method = RequestMethod.POST, value = { "/dataset/create" }, consumes = "application/json")
|
|
|
|
public @ResponseBody ResponseEntity<Object> createDataset(@RequestBody Dataset dataset) {
|
2017-10-04 11:48:21 +02:00
|
|
|
|
2017-10-23 18:06:24 +02:00
|
|
|
dataset.setId(null);
|
2017-10-04 11:48:21 +02:00
|
|
|
try {
|
2017-10-23 18:06:24 +02:00
|
|
|
dataset = datasetDao.create(dataset);
|
|
|
|
return ResponseEntity.status(HttpStatus.CREATED).body(objectMapper.writeValueAsString(dataset));
|
2017-10-04 11:48:21 +02:00
|
|
|
}
|
|
|
|
catch(Exception e) {
|
2017-10-23 18:06:24 +02:00
|
|
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Could not create or update Dataset! Reason: " + e.getMessage());
|
2017-10-04 11:48:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-23 18:06:24 +02:00
|
|
|
@RequestMapping(method = RequestMethod.POST, value = { "/dataset/update" }, consumes = "application/json")
|
|
|
|
public @ResponseBody ResponseEntity<Object> updateDataset(@RequestBody Dataset dataset) {
|
|
|
|
|
|
|
|
try {
|
|
|
|
dataset = datasetDao.update(dataset);
|
|
|
|
RestResponse rr = new RestResponse("Updated dataset with id: "+dataset.toString(), dataset.getId().toString());
|
|
|
|
return ResponseEntity.status(HttpStatus.CREATED).body(rr.toString());
|
|
|
|
}
|
|
|
|
catch(Exception ex) {
|
|
|
|
ex.printStackTrace();
|
|
|
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Could not create or update Dataset! Reason: " + ex.getMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2017-10-06 19:20:05 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-10-23 18:06:24 +02:00
|
|
|
@RequestMapping(method = RequestMethod.POST, value = { "/dataset/delete" }, consumes = "application/json")
|
2017-10-04 11:48:21 +02:00
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-23 18:06:24 +02:00
|
|
|
@RequestMapping(method = RequestMethod.GET, value = { "/dataset/assignDMPToDataset" })
|
2017-10-04 19:44:41 +02:00
|
|
|
public @ResponseBody ResponseEntity<Object> assignDMPToDataset(@RequestParam("datasetID") String datasetID, @RequestParam("dmpID") String dmpID) {
|
2017-10-06 19:20:05 +02:00
|
|
|
|
2017-10-04 19:44:41 +02:00
|
|
|
Dataset dataset = null;
|
|
|
|
try {
|
|
|
|
dataset = datasetDao.read(UUID.fromString(datasetID));
|
2017-10-06 19:20:05 +02:00
|
|
|
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));
|
2017-10-04 19:44:41 +02:00
|
|
|
dataset.setDmp(dmp);
|
|
|
|
datasetDao.update(dataset);
|
|
|
|
return ResponseEntity.status(HttpStatus.OK).build();
|
|
|
|
}
|
|
|
|
catch(Exception ex) {
|
|
|
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-23 18:06:24 +02:00
|
|
|
@RequestMapping(method = RequestMethod.GET, value = { "/dataset/assignProfileToDataset" })
|
|
|
|
public @ResponseBody ResponseEntity<Object> assignProfileToDataset(@RequestParam("datasetID") String datasetID, @RequestParam("profileID") String profileID) {
|
|
|
|
|
|
|
|
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");
|
|
|
|
DatasetProfile profile = new DatasetProfile();
|
|
|
|
profile.setId(UUID.fromString(profileID));
|
|
|
|
dataset.setProfile(profile);
|
|
|
|
datasetDao.update(dataset);
|
|
|
|
return ResponseEntity.status(HttpStatus.OK).build();
|
|
|
|
}
|
|
|
|
catch(Exception ex) {
|
|
|
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-10-04 11:48:21 +02:00
|
|
|
}
|
|
|
|
|