argos/dmp-backend/src/main/java/eu/eudat/controllers/Projects.java

377 lines
14 KiB
Java
Raw Normal View History

2017-12-15 11:05:51 +01:00
package eu.eudat.controllers;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
import javax.transaction.Transactional;
2017-12-15 17:57:41 +01:00
import eu.eudat.models.responses.ResponseItem;
2017-12-15 11:05:51 +01: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.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.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import eu.eudat.dao.entities.DMPDao;
import eu.eudat.dao.entities.DMPProfileDao;
import eu.eudat.dao.entities.DataRepositoryDao;
import eu.eudat.dao.entities.DatasetDao;
import eu.eudat.dao.entities.DatasetProfileDao;
import eu.eudat.dao.entities.DatasetProfileRulesetDao;
import eu.eudat.dao.entities.DatasetProfileViewstyleDao;
import eu.eudat.dao.entities.OrganisationDao;
import eu.eudat.dao.entities.ProjectDao;
import eu.eudat.dao.entities.RegistryDao;
import eu.eudat.dao.entities.ResearcherDao;
import eu.eudat.dao.entities.ServiceDao;
import eu.eudat.dao.entities.UserInfoDao;
import eu.eudat.entities.DMP;
import eu.eudat.entities.DMPProfile;
import eu.eudat.entities.DataRepository;
import eu.eudat.entities.Dataset;
import eu.eudat.entities.DatasetProfile;
import eu.eudat.entities.DatasetProfileRuleset;
import eu.eudat.entities.Organisation;
import eu.eudat.entities.Project;
import eu.eudat.entities.Registry;
import eu.eudat.entities.Researcher;
import eu.eudat.entities.Service;
import eu.eudat.entities.UserInfo;
import eu.eudat.entities.responses.IDLabelPair;
import eu.eudat.helpers.Transformers;
import eu.eudat.managers.ProjectManager;
import eu.eudat.models.helpers.DataTableData;
import eu.eudat.models.project.ProjectTableRequest;
import eu.eudat.proxy.config.exceptions.HugeResultSet;
import eu.eudat.proxy.config.exceptions.NoURLFound;
import eu.eudat.proxy.fetching.RemoteFetcher;
import eu.eudat.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;
@Autowired private UserInfoDao userInfoDao;
@Autowired private RemoteFetcher remoteFetcher;
@RequestMapping(method = RequestMethod.POST, value = { "/projects/getPaged" }, consumes = "application/json", produces="application/json")
2017-12-15 17:57:41 +01:00
public @ResponseBody ResponseItem<DataTableData<eu.eudat.models.project.Project>> getPaged(@RequestBody ProjectTableRequest projectTableRequest) {
2017-12-15 11:05:51 +01:00
try {
DataTableData<eu.eudat.models.project.Project> dataTable = new ProjectManager().getPaged(projectDao, projectTableRequest);
2017-12-15 17:57:41 +01:00
return new ResponseItem<DataTableData<eu.eudat.models.project.Project>>().payload(dataTable).status(HttpStatus.OK);
2017-12-15 11:05:51 +01:00
} catch (Exception ex) {
ex.printStackTrace();
2017-12-15 17:57:41 +01:00
return new ResponseItem<DataTableData<eu.eudat.models.project.Project>>().status(HttpStatus.BAD_REQUEST).message(ex.getMessage());
2017-12-15 11:05:51 +01:00
}
}
@RequestMapping(method = RequestMethod.GET, value = { "/projects/getSingle/{id}" }, produces="application/json")
2017-12-15 17:57:41 +01:00
public @ResponseBody ResponseItem<eu.eudat.models.project.Project> getPaged(@PathVariable String id) {
2017-12-15 11:05:51 +01:00
try {
eu.eudat.models.project.Project project = new ProjectManager().getSingle(projectDao, id);
2017-12-15 17:57:41 +01:00
return new ResponseItem<eu.eudat.models.project.Project>().payload(project).status(HttpStatus.OK);
2017-12-15 11:05:51 +01:00
} catch (Exception ex) {
ex.printStackTrace();
2017-12-15 17:57:41 +01:00
return new ResponseItem<eu.eudat.models.project.Project>().status(HttpStatus.BAD_REQUEST).message(ex.getMessage());
2017-12-15 11:05:51 +01:00
}
}
@Transactional
@RequestMapping(method = RequestMethod.POST, value = { "/projects/add" }, consumes = "application/json", produces="application/json")
2017-12-15 17:57:41 +01:00
public @ResponseBody ResponseItem<eu.eudat.entities.Project> addProject(@RequestBody eu.eudat.models.project.Project project) {
2017-12-15 11:05:51 +01:00
Project createdProject = projectDao.update(project.toDataModel());
2017-12-15 17:57:41 +01:00
return new ResponseItem<eu.eudat.entities.Project>().payload(createdProject).status(HttpStatus.OK);
2017-12-15 11:05:51 +01:00
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@RequestMapping(method = RequestMethod.GET, value = { "/external/projects" }, produces="application/json")
public @ResponseBody ResponseEntity<List<Map<String,String>>> listExternalProjects(@RequestParam(value="query", required=false) String query ){
try {
List<Map<String,String>> remoteRepos = remoteFetcher.getProjects(query);
return ResponseEntity.status(HttpStatus.OK).body(remoteRepos);
}
catch(NoURLFound ex) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
}
catch(HugeResultSet ex) {
return ResponseEntity.status(HttpStatus.UNPROCESSABLE_ENTITY).body(null); //the ex.getMessage has the appropriate text description
}
}
// MANAGE PROJECT(S)
@RequestMapping(method = RequestMethod.GET, value = { "/projects" }, produces="application/json")
public @ResponseBody ResponseEntity<List<UUID>> listProjects(){
try {
List<UUID> allIDs = projectDao.listAllIDs();
return ResponseEntity.status(HttpStatus.OK).body(allIDs);
}
catch(Exception ex) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
}
}
@RequestMapping(method = RequestMethod.GET, value = { "/projects/{id}" }, produces="application/json")
public @ResponseBody ResponseEntity<Project> getProject(@PathVariable("id") String id) {
try {
Project project = projectDao.read(UUID.fromString(id));
System.out.println(project.getId().toString());
return ResponseEntity.status(HttpStatus.OK).body(project);
}
catch(Exception ex) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
}
}
@RequestMapping(method = RequestMethod.GET, value = { "/project/listAllLabelIDs" }, produces="application/json")
public @ResponseBody ResponseEntity<List<IDLabelPair>> listLabelIds(){
try {
List<IDLabelPair> allIDs = projectDao.listAllIDsLabels();
return ResponseEntity.status(HttpStatus.OK).body(allIDs);
}
catch(Exception ex) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
}
}
@RequestMapping(method = RequestMethod.GET, value = { "/project/getAll" }, produces="application/json")
public @ResponseBody ResponseEntity<Object> getAllProjects(){
try {
List<Project> allProjects = projectDao.getAll();
return ResponseEntity.status(HttpStatus.OK).body(allProjects);
}
catch(Exception ex) {
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}
@Transactional
@RequestMapping(method = RequestMethod.POST, value = { "/project/create" }, consumes = "application/json", produces="application/json")
public @ResponseBody ResponseEntity<Project> createProject(@RequestBody Project project) {
Project createdProject = projectDao.update(project);
return ResponseEntity.status(HttpStatus.CREATED).body(createdProject);
}
@RequestMapping(method = RequestMethod.POST, value = { "/project/update" }, consumes = "application/json", produces="application/json")
public @ResponseBody ResponseEntity<Project> updateProject(@RequestBody Project project) {
Project updatedProject = projectDao.update(project);
return ResponseEntity.status(HttpStatus.CREATED).body(updatedProject);
}
@RequestMapping(method = RequestMethod.POST, value = { "/project/delete" }, consumes = "application/json", produces="application/json")
public @ResponseBody ResponseEntity<Project> delete(@RequestBody Project project) {
Project p = new Project();
p.setId(project.getId());
try {
projectDao.delete(p);
return ResponseEntity.status(HttpStatus.CREATED).body(null);
} catch (Exception e) {
e.printStackTrace();
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
}
}
@RequestMapping(method = RequestMethod.POST, value = { "/project/softdelete" }, consumes = "application/json", produces="application/json")
public @ResponseBody ResponseEntity<Object> softDelete(@RequestBody Project project) {
project.setStatus(new Short("-1"));
try {
projectDao.update(project);
return ResponseEntity.status(HttpStatus.CREATED).body(null);
} catch (Exception e) {
e.printStackTrace();
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
}
}
@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(dmps);
} catch (Exception e) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
}
}
////////////////////////////////
//// 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 happen
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("There's no such a user on the system. You shouldn't be here");
try {
List<Project> userProjects = projectDao.getProjectsOfUser(userID);
return ResponseEntity.status(HttpStatus.OK).body(userProjects);
}
catch(Exception ex) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
}
/*
* OLD ONE
Map<UUID, Researcher> userProjects = new HashMap<UUID, Researcher>();
userInfo.getDmps().forEach( dmp -> {
Researcher proj = dmp.getProject();
userProjects.put(proj.getId(), proj);
});
try {
return ResponseEntity.status(HttpStatus.OK).body(SerializerProvider.toJson(userProjects.values()));
}
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", consumes = "application/json")
public @ResponseBody ResponseEntity<Project> createProjectOfUser(@RequestBody Project project){
String userID = null;
try {
userID = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
} catch(NullPointerException ex) {
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
}
UserInfo userInfo = userInfoDao.read(UUID.fromString(userID));
if(userInfo==null) //this should normally never happer
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
try {
project.setId(null);
project.setStatus(new Short("0"));
project.setCreationUser(userInfo);
project.setCreated(new Date());
project.setModified(new Date());
Project newproj = projectDao.create(project);
// DMP newdmp = new DMP();
// newdmp.setId(null);
// newdmp.setLabel("Auto-Generated");
// newdmp.setCreated(new Date());
// newdmp.setVersion(1);
// newdmp.setStatus(new Short("0"));
// newdmp.setProject(newproj);
//
// Set<UserInfo> users = new HashSet<UserInfo>();
// users.add(userInfo);
// newdmp.setUsers(users);
//
// newdmp = dMPDao.create(newdmp);
return ResponseEntity.status(HttpStatus.OK).body(newproj);
}
catch(Exception ex) {
ex.printStackTrace();
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
}
}
// @Transactional
// @RequestMapping(method = RequestMethod.POST, value = { "/project/updateofuser" }, produces="text/plain")
// public @ResponseBody ResponseEntity<Object> updateProjectOfUser(@RequestBody Researcher project){
//
// if(project.getId()==null)
// return ResponseEntity.status(HttpStatus.NOT_MODIFIED).body("Cannot update, id was null");
// return setProject(project);
//
// }
}