270 lines
9.5 KiB
Java
270 lines
9.5 KiB
Java
package rest.entities;
|
|
|
|
import java.io.IOException;
|
|
import java.io.PrintStream;
|
|
import java.util.Date;
|
|
import java.util.HashSet;
|
|
import java.util.List;
|
|
import java.util.Set;
|
|
import java.util.UUID;
|
|
import java.util.stream.Collectors;
|
|
|
|
import javax.transaction.Transactional;
|
|
|
|
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.security.core.context.SecurityContextHolder;
|
|
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.JsonParseException;
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
|
import com.fasterxml.jackson.databind.JsonMappingException;
|
|
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 dao.entities.UserInfoDao;
|
|
import entities.DMP;
|
|
import entities.DMPProfile;
|
|
import entities.Dataset;
|
|
import entities.DatasetProfile;
|
|
import entities.DatasetProfileRuleset;
|
|
import entities.DatasetProfileViewstyle;
|
|
import entities.Organisation;
|
|
import entities.Project;
|
|
import entities.UserInfo;
|
|
import helpers.SafeCleanAttribs;
|
|
import helpers.SerializerProvider;
|
|
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;
|
|
@Autowired private UserInfoDao userInfoDao;
|
|
|
|
|
|
// FETCH BY DATASET(S)
|
|
|
|
|
|
@RequestMapping(method = RequestMethod.GET, value = { "/datasets" })
|
|
public @ResponseBody ResponseEntity<Object> listDatasets(){
|
|
try {
|
|
List<UUID> allIDs = datasetDao.listAllIDs();
|
|
return ResponseEntity.status(HttpStatus.OK).body(SerializerProvider.toJson(allIDs));
|
|
}
|
|
catch(Exception ex) {
|
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Serialization issue: "+ex.getMessage());
|
|
}
|
|
}
|
|
|
|
|
|
@RequestMapping(method = RequestMethod.GET, value = { "/datasets/{id}" })
|
|
public @ResponseBody ResponseEntity<Object> getDataset(@PathVariable("id") String id) {
|
|
try {
|
|
Dataset ds = datasetDao.read(UUID.fromString(id));
|
|
ds.setDmp(ds.getDmp());
|
|
return ResponseEntity.status(HttpStatus.OK).body(SerializerProvider.toJson(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 = { "/dataset/getAll" })
|
|
public @ResponseBody ResponseEntity<Object> getAllDatasets(){
|
|
|
|
try {
|
|
List<Dataset> allDatasets = datasetDao.getAll();
|
|
return new ResponseEntity<Object>(SerializerProvider.toJson(allDatasets), HttpStatus.OK);
|
|
}
|
|
catch(Exception ex) {
|
|
ex.printStackTrace();
|
|
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
|
|
}
|
|
|
|
}
|
|
|
|
|
|
@RequestMapping(method = RequestMethod.POST, value = { "/dataset/create" }, consumes = "application/json", produces="application/json")
|
|
public @ResponseBody ResponseEntity<Object> createDataset(@RequestBody Dataset dataset) {
|
|
|
|
|
|
|
|
String userID = null;
|
|
try {
|
|
userID = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
|
|
} catch(NullPointerException ex) {
|
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("You have not logged in. You shouldn't be here");
|
|
}
|
|
|
|
UserInfo userInfo = userInfoDao.read(UUID.fromString(userID));
|
|
|
|
if(userInfo==null) //this should normally never happer
|
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("There's no such a user on the system. You shouldn't be here");
|
|
|
|
|
|
|
|
dataset.setId(null);
|
|
dataset.setCreated(new Date());
|
|
dataset.setModified(new Date());
|
|
dataset.setStatus(new Short("0"));
|
|
dataset.setCreator(userInfo);
|
|
try {
|
|
dataset = datasetDao.create(dataset);
|
|
return ResponseEntity.status(HttpStatus.CREATED).body(SerializerProvider.toJson(dataset));
|
|
}
|
|
catch(Exception e) {
|
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Could not create or update Dataset! Reason: " + e.getMessage());
|
|
}
|
|
|
|
}
|
|
|
|
|
|
@RequestMapping(method = RequestMethod.POST, value = { "/dataset/update" }, consumes = "application/json", produces="application/json")
|
|
public @ResponseBody ResponseEntity<Object> updateDataset(@RequestBody String datasetJson) {
|
|
|
|
Dataset dataset;
|
|
try {
|
|
dataset = SerializerProvider.fromJson(datasetJson, Dataset.class);
|
|
} catch (Exception e) {
|
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Could not create or update Dataset! Reason: " + e.getMessage());
|
|
}
|
|
|
|
SafeCleanAttribs.clean(dataset);
|
|
|
|
try {
|
|
dataset = datasetDao.update(dataset);
|
|
return ResponseEntity.status(HttpStatus.CREATED).body(SerializerProvider.toJson(dataset));
|
|
}
|
|
catch(Exception ex) {
|
|
ex.printStackTrace();
|
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Could not create or update Dataset! Reason: " + ex.getMessage());
|
|
}
|
|
|
|
}
|
|
|
|
|
|
@RequestMapping(method = RequestMethod.POST, value = { "/dataset/delete" }, 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.POST, value = { "/dataset/softdelete" }, consumes = "application/json", produces="text/plain")
|
|
public @ResponseBody ResponseEntity<Object> softDelete(@RequestBody Dataset dataset) {
|
|
|
|
Dataset d = datasetDao.read(dataset.getId());
|
|
d.setStatus(new Short("-1"));
|
|
try {
|
|
int code = updateDataset(SerializerProvider.toJson(d)).getStatusCodeValue();
|
|
if(code>199 && code<300)
|
|
return ResponseEntity.status(HttpStatus.CREATED).body("DELETED!");
|
|
else
|
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"msg\":\"Could not soft delete dataset!\"");
|
|
} catch (Exception e) {
|
|
e.printStackTrace();
|
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"msg\":\"Could not soft delete dataset!\"");
|
|
}
|
|
|
|
}
|
|
|
|
|
|
@RequestMapping(method = RequestMethod.GET, value = { "/dataset/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 = { "/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();
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|