2017-10-12 14:04:38 +02:00
|
|
|
package rest.entities;
|
2017-10-06 19:20:05 +02:00
|
|
|
|
2017-10-23 18:06:24 +02:00
|
|
|
import java.util.HashSet;
|
2017-10-06 19:20:05 +02:00
|
|
|
import java.util.List;
|
2017-10-23 18:06:24 +02:00
|
|
|
import java.util.Set;
|
2017-10-06 19:20:05 +02:00
|
|
|
import java.util.UUID;
|
2017-10-12 14:02:41 +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;
|
2017-10-23 18:06:24 +02:00
|
|
|
import org.springframework.security.core.context.SecurityContextHolder;
|
2017-10-06 19:20:05 +02:00
|
|
|
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-20 17:11:40 +02:00
|
|
|
import com.fasterxml.jackson.annotation.JsonInclude.Include;
|
2017-10-06 19:20:05 +02:00
|
|
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
2017-10-20 17:11:40 +02:00
|
|
|
import com.fasterxml.jackson.databind.DeserializationFeature;
|
2017-10-06 19:20:05 +02:00
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
2017-10-20 17:11:40 +02:00
|
|
|
import com.fasterxml.jackson.datatype.hibernate5.Hibernate5Module;
|
2017-10-06 19:20:05 +02:00
|
|
|
|
|
|
|
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;
|
2017-10-23 18:06:24 +02:00
|
|
|
import dao.entities.UserInfoDao;
|
2017-10-06 19:20:05 +02:00
|
|
|
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;
|
2017-10-23 18:06:24 +02:00
|
|
|
import entities.UserInfo;
|
2017-10-06 19:20:05 +02:00
|
|
|
import entities.responses.IDLabelPair;
|
2017-10-20 17:11:40 +02:00
|
|
|
import helpers.SerializerProvider;
|
2017-10-06 19:20:05 +02:00
|
|
|
import helpers.Transformers;
|
|
|
|
import responses.RestResponse;
|
|
|
|
|
|
|
|
|
|
|
|
@RestController
|
|
|
|
@CrossOrigin
|
|
|
|
public class Projects {
|
|
|
|
|
|
|
|
@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-23 18:06:24 +02:00
|
|
|
@Autowired private UserInfoDao userInfoDao;
|
2017-10-06 19:20:05 +02:00
|
|
|
|
|
|
|
|
2017-10-20 17:11:40 +02:00
|
|
|
private ObjectMapper objectMapper = SerializerProvider.getJsonSerializer();
|
2017-10-06 19:20:05 +02:00
|
|
|
|
|
|
|
// MANAGE PROJECT(S)
|
|
|
|
|
2017-10-24 18:33:51 +02:00
|
|
|
@RequestMapping(method = RequestMethod.GET, value = { "/projects" }, produces="application/json")
|
2017-10-06 19:20:05 +02:00
|
|
|
public @ResponseBody ResponseEntity<Object> listProjects(){
|
|
|
|
try {
|
|
|
|
List<UUID> allIDs = projectDao.listAllIDs();
|
2017-10-24 18:33:51 +02:00
|
|
|
return ResponseEntity.status(HttpStatus.OK).body(allIDs);
|
2017-10-06 19:20:05 +02:00
|
|
|
}
|
|
|
|
catch(Exception ex) {
|
|
|
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Serialization issue: "+ex.getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-24 18:33:51 +02:00
|
|
|
@RequestMapping(method = RequestMethod.GET, value = { "/projects/{id}" }, produces="application/json")
|
2017-10-06 19:20:05 +02:00
|
|
|
public @ResponseBody ResponseEntity<Object> getProject(@PathVariable("id") String id) {
|
|
|
|
try {
|
|
|
|
Project project = projectDao.read(UUID.fromString(id));
|
2017-10-20 17:11:40 +02:00
|
|
|
|
|
|
|
System.out.println(project.getId().toString());
|
|
|
|
|
2017-10-24 18:33:51 +02:00
|
|
|
return ResponseEntity.status(HttpStatus.OK).body(project);
|
2017-10-06 19:20:05 +02:00
|
|
|
}
|
|
|
|
catch(Exception ex) {
|
|
|
|
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Erroneous input: "+ex.getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-24 18:33:51 +02:00
|
|
|
@RequestMapping(method = RequestMethod.GET, value = { "/project/listAllLabelIDs" }, produces="application/json")
|
2017-10-06 19:20:05 +02:00
|
|
|
public @ResponseBody ResponseEntity<Object> listLabelIds(){
|
|
|
|
try {
|
|
|
|
List<IDLabelPair> allIDs = projectDao.listAllIDsLabels();
|
2017-10-24 18:33:51 +02:00
|
|
|
return ResponseEntity.status(HttpStatus.OK).body(allIDs);
|
2017-10-06 19:20:05 +02:00
|
|
|
}
|
|
|
|
catch(Exception ex) {
|
|
|
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Serialization issue: "+ex.getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-12 14:02:41 +02:00
|
|
|
@RequestMapping(method = RequestMethod.GET, value = { "/project/getAll" }, produces="application/json")
|
|
|
|
public @ResponseBody ResponseEntity<Object> getAllProjects(){
|
2017-10-06 19:20:05 +02:00
|
|
|
try {
|
2017-10-12 14:02:41 +02:00
|
|
|
List<Project> allProjects = projectDao.getAll();
|
|
|
|
|
|
|
|
//sorry for that, spring-jersey serialisation has issues when performed on tables, so -> custom
|
|
|
|
List<String> projectStrL = allProjects.parallelStream().map((projectObj) -> {
|
|
|
|
try {
|
|
|
|
return objectMapper.writeValueAsString(projectObj);
|
|
|
|
} catch (JsonProcessingException e) {
|
|
|
|
return "";
|
|
|
|
}
|
|
|
|
}).collect(Collectors.toList());
|
|
|
|
|
|
|
|
return new ResponseEntity<Object>("["+String.join(",", projectStrL)+"]", HttpStatus.OK);
|
|
|
|
|
|
|
|
}
|
|
|
|
catch(Exception ex) {
|
|
|
|
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
|
2017-10-06 19:20:05 +02:00
|
|
|
}
|
2017-10-12 14:02:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-18 15:12:25 +02:00
|
|
|
|
2017-10-12 14:02:41 +02:00
|
|
|
@Transactional
|
|
|
|
@RequestMapping(method = RequestMethod.POST, value = { "/project/create" }, consumes = "application/json", produces="application/json")
|
|
|
|
public @ResponseBody ResponseEntity<Object> setProject(@RequestBody Project project) {
|
|
|
|
Project createdProject = projectDao.update(project);
|
2017-10-24 18:33:51 +02:00
|
|
|
return ResponseEntity.status(HttpStatus.CREATED).body(createdProject);
|
2017-10-12 14:02:41 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-10-24 18:33:51 +02:00
|
|
|
@RequestMapping(method = RequestMethod.POST, value = { "/project/delete" }, consumes = "application/json", produces="application/json")
|
2017-10-12 14:02:41 +02:00
|
|
|
public @ResponseBody ResponseEntity<Object> delete(@RequestBody Project project) {
|
|
|
|
|
|
|
|
Project p = new Project();
|
|
|
|
p.setId(project.getId());
|
|
|
|
try {
|
|
|
|
projectDao.delete(p);
|
2017-10-17 13:45:11 +02:00
|
|
|
return ResponseEntity.status(HttpStatus.CREATED).body("{\"msg\":\"Deleted Project entity!\"}");
|
2017-10-12 14:02:41 +02:00
|
|
|
} catch (Exception e) {
|
2017-10-20 12:03:55 +02:00
|
|
|
e.printStackTrace();
|
2017-10-17 13:45:11 +02:00
|
|
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"msg\":\"Could not Delete Project!\"}");
|
2017-10-06 19:20:05 +02:00
|
|
|
}
|
2017-10-12 14:02:41 +02:00
|
|
|
|
2017-10-06 19:20:05 +02:00
|
|
|
}
|
|
|
|
|
2017-10-23 18:06:24 +02:00
|
|
|
|
|
|
|
@Transactional
|
|
|
|
@RequestMapping(method = RequestMethod.POST, value = { "/project/getdmps" }, consumes = "application/json", produces="application/json")
|
|
|
|
public @ResponseBody ResponseEntity<Object> getProjectDmps(@RequestBody Project project) {
|
|
|
|
try {
|
|
|
|
Set<DMP> dmps = projectDao.read(project.getId()).getDmps();
|
|
|
|
return ResponseEntity.status(HttpStatus.CREATED).body(objectMapper.writeValueAsString(dmps));
|
|
|
|
} catch (JsonProcessingException e) {
|
|
|
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"msg\":\"Could not create Project!\"}");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
////////////////////////////////
|
|
|
|
//// USER - RELATED ACTIONS ////
|
|
|
|
////////////////////////////////
|
|
|
|
|
|
|
|
|
|
|
|
@RequestMapping(method = RequestMethod.GET, value = { "/project/getofuser" }, produces="text/plain")
|
|
|
|
public @ResponseBody ResponseEntity<Object> getProjectsOfUser(){
|
|
|
|
|
|
|
|
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");
|
|
|
|
|
|
|
|
try {
|
|
|
|
return ResponseEntity.status(HttpStatus.OK).body(objectMapper.writeValueAsString(userInfo.getProjects()));
|
|
|
|
}
|
|
|
|
catch(Exception ex) {
|
|
|
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Serialization issue: "+ex.getMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Transactional
|
|
|
|
@RequestMapping(method = RequestMethod.POST, value = { "/project/createofuser" }, produces="text/plain")
|
|
|
|
public @ResponseBody ResponseEntity<Object> createProjectOfUser(@RequestBody Project project){
|
|
|
|
|
2017-10-06 19:20:05 +02:00
|
|
|
|
2017-10-23 18:06:24 +02:00
|
|
|
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");
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
project.setId(null);
|
|
|
|
|
|
|
|
Project newproj = projectDao.create(project);
|
|
|
|
|
|
|
|
Set<Project> userProjects = userInfo.getProjects();
|
|
|
|
if(userProjects==null)
|
|
|
|
userProjects = new HashSet<Project>();
|
|
|
|
userProjects.add(newproj);
|
|
|
|
|
|
|
|
userInfo.setProjects(userProjects);
|
|
|
|
|
|
|
|
userInfo = userInfoDao.update(userInfo);
|
|
|
|
|
|
|
|
return ResponseEntity.status(HttpStatus.OK).body(objectMapper.writeValueAsString(userInfo));
|
|
|
|
}
|
|
|
|
catch(Exception ex) {
|
|
|
|
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Serialization issue: "+ex.getMessage());
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Transactional
|
|
|
|
@RequestMapping(method = RequestMethod.POST, value = { "/project/updateofuser" }, produces="text/plain")
|
|
|
|
public @ResponseBody ResponseEntity<Object> updateProjectOfUser(@RequestBody Project project){
|
|
|
|
|
|
|
|
if(project.getId()==null)
|
|
|
|
return ResponseEntity.status(HttpStatus.NOT_MODIFIED).body("Cannot update, id was null");
|
|
|
|
return setProject(project);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-10-06 19:20:05 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|