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

461 lines
17 KiB
Java

package eu.eudat.controllers;
import java.util.*;
import java.util.stream.Collectors;
import javax.transaction.Transactional;
import eu.eudat.entities.DMP;
import eu.eudat.entities.Dataset;
import eu.eudat.entities.UserInfo;
import eu.eudat.models.criteria.OrganisationCriteria;
import eu.eudat.models.criteria.ResearcherCriteria;
import eu.eudat.models.dmp.DataManagementPlan;
import eu.eudat.models.dmp.DataManagementPlanTableRequest;
import eu.eudat.models.helpers.DataTableData;
import eu.eudat.models.helpers.responses.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContextHolder;
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.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
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.responses.IDLabelPair;
import eu.eudat.managers.DataManagementPlanManager;
@RestController
@CrossOrigin
public class DMPs {
@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 DMP(S)
@RequestMapping(method = RequestMethod.POST, value = { "/dmps/getPaged" }, consumes = "application/json", produces="application/json")
public @ResponseBody ResponseItem<DataTableData<DataManagementPlan>> getPaged(@RequestBody DataManagementPlanTableRequest dataManagementPlanTableRequest) {
try {
DataTableData<eu.eudat.models.dmp.DataManagementPlan> dataTable = new DataManagementPlanManager().getPaged(dMPDao, dataManagementPlanTableRequest);
return new ResponseItem<DataTableData<DataManagementPlan>>().status(HttpStatus.OK).payload(dataTable);
} catch (Exception ex) {
ex.printStackTrace();
return new ResponseItem<DataTableData<DataManagementPlan>>().status(HttpStatus.BAD_REQUEST).message(ex.getMessage());
}
}
@RequestMapping(method = RequestMethod.GET, value = { "/dmps/getSingle/{id}" }, produces="application/json")
public @ResponseBody ResponseItem<DataManagementPlan> getPaged(@PathVariable String id) {
try {
eu.eudat.models.dmp.DataManagementPlan project = new DataManagementPlanManager().getSingle(dMPDao, id);
return new ResponseItem<DataManagementPlan>().status(HttpStatus.OK).payload(project);
} catch (Exception ex) {
ex.printStackTrace();
return new ResponseItem<DataManagementPlan>().status(HttpStatus.BAD_REQUEST).message(ex.getMessage());
}
}
@Transactional
@RequestMapping(method = RequestMethod.POST, value = { "/dmps/add" }, consumes = "application/json", produces="application/json")
public @ResponseBody ResponseEntity<eu.eudat.entities.DMP> addDmp(@RequestBody eu.eudat.models.dmp.DataManagementPlan dataManagementPlan) {
eu.eudat.entities.DMP createdProject = dMPDao.createOrUpdate(dataManagementPlan.toDataModel());
return ResponseEntity.status(HttpStatus.CREATED).body(createdProject);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// @RequestMapping(method = RequestMethod.GET, value = { "/dmps" }, produces="text/plain")
// public @ResponseBody ResponseEntity<List<UUID>> listDMPs(){
// try {
// List<UUID> allIDs = dMPDao.listAllIDs();
// return ResponseEntity.status(HttpStatus.OK).body(allIDs);
// }
// catch(Exception ex) {
// ex.printStackTrace();
// return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
// }
// }
//
// @RequestMapping(method = RequestMethod.GET, value = { "/dmps/{id}" }, produces="application/json")
// public @ResponseBody ResponseEntity<DataManagementPlan> getDMP(@PathVariable("id") String id){
// try {
// DMP dmp = dMPDao.read(UUID.fromString(id));
// DataManagementPlan dataManagementPlan = new DataManagementPlan();
// dataManagementPlan.fromDataModel(dmp);
// return ResponseEntity.status(HttpStatus.OK).body(dataManagementPlan);
// }
// catch(Exception ex) {
// ex.printStackTrace();
// return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
// }
// }
//
// @RequestMapping(method = RequestMethod.GET, value = { "/dmp/listDMPLabelID" }, produces="text/plain")
// public @ResponseBody ResponseEntity<List<IDLabelPair>> listDmpLabelID(){
// try {
// List<IDLabelPair> allIDLabels = dMPDao.listAllIDsLabels();
// return ResponseEntity.status(HttpStatus.OK).body(allIDLabels);
// }
// catch(Exception ex) {
// ex.printStackTrace();
// return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
// }
// }
//
//
// /**
// * This should be called on extreme cases. It's computationally intensive
// */
// @RequestMapping(method = RequestMethod.GET, value = { "/dmp/getAll" }, produces="application/json")
// public @ResponseBody ResponseEntity<Object> getAllDMPs(){
//
// try {
// List<DMP> allDMPs = dMPDao.getAll();
// return ResponseEntity.status(HttpStatus.OK).body(allDMPs);
// }
// catch(Exception ex) {
// ex.printStackTrace();
// return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null );
// }
// }
//
//
// @Transactional
// @RequestMapping(method = RequestMethod.POST, value = { "/dmp/create" }, consumes = "application/json", produces="application/json")
// public @ResponseBody ResponseEntity<DMP> createDMP(@RequestBody DMP dmp) {
// try {
// DMP createdDmp = dMPDao.update(dmp);
// return ResponseEntity.status(HttpStatus.CREATED).body(createdDmp);
// } catch (Exception e) {
// e.printStackTrace();
// return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
// }
//
// }
//
// @Transactional
// @RequestMapping(method = RequestMethod.POST, value = { "/dmp/update" }, consumes = "application/json", produces="application/json")
// public @ResponseBody ResponseEntity<DMP> updateDMP(@RequestBody DataManagementPlan dataManagementPlan) {
//
// DMP newDmp = dataManagementPlan.toDataModel();
// if(newDmp.getOrganisations()!=null&&!newDmp.getOrganisations().isEmpty()){
// for(eu.eudat.entities.Organisation organisation: newDmp.getOrganisations()){
// OrganisationCriteria criteria = new OrganisationCriteria();
// criteria.setLike(organisation.getReference());
// List<eu.eudat.entities.Organisation> entries = this.organisationDao.listBy(criteria);
// if(entries!=null&&!entries.isEmpty())organisation.setId(entries.get(0).getId());
// else organisation = this.organisationDao.create(organisation);
// }
// }
//
// if(newDmp.getResearchers()!=null&&!newDmp.getResearchers().isEmpty()){
// for(eu.eudat.entities.Researcher researcher : newDmp.getResearchers()){
// ResearcherCriteria criteria = new ResearcherCriteria();
// criteria.setLike(researcher.getReference());
// List<eu.eudat.entities.Researcher> entries = this.researcherDao.listBy(criteria);
// if(entries!=null&&!entries.isEmpty())researcher.setId(entries.get(0).getId());
// else researcher = this.researcherDao.create(researcher);
// }
// }
//
// DMP previousDmp = dMPDao.read(dataManagementPlan.getId());
// previousDmp.setResearchers(newDmp.getResearchers());
// previousDmp.setOrganisations(newDmp.getOrganisations());
// previousDmp.setLabel(dataManagementPlan.getLabel());
// previousDmp.setVersion(dataManagementPlan.getVersion());
// previousDmp.setStatus((short)dataManagementPlan.getStatus());
// //if(!previousDmp.getProject().getId().equals(newDmp.getProject().getId()))previousDmp.setProject(projectDao.read(newDmp.getProject().getId()));
//
// try {
// DMP updatedDMP = dMPDao.update(previousDmp);
// return ResponseEntity.status(HttpStatus.CREATED).body(updatedDMP);
// } catch (Exception e) {
// e.printStackTrace();
// return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
// }
//
// }
//
//
//
//
//
// @RequestMapping(method = RequestMethod.POST, value = { "/dmp/getdatasets" }, consumes = "application/json", produces="application/json")
// public @ResponseBody ResponseEntity<List<Dataset>> getDatasetsOfDMP(@RequestBody DMP dmp) {
// try {
// List<Dataset> datasets = datasetDao.getDatasetsOfDmp(dmp.getId());
// return ResponseEntity.status(HttpStatus.OK).body(datasets);
// } catch (Exception e) {
// return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
// }
//
// }
//
//
//
// @RequestMapping(method = RequestMethod.POST, value = { "/dmp/delete" }, consumes = "application/json", produces="text/plain")
// public @ResponseBody ResponseEntity<Object> delete(@RequestBody DMP dmp) {
//
// DMP d = new DMP();
// d.setId(dmp.getId());
// try {
// dMPDao.delete(d);
// return ResponseEntity.status(HttpStatus.CREATED).body("DELETED!");
// } catch (Exception e) {
// e.printStackTrace();
// return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"msg\":\"Could not Delete DMP!\"");
// }
//
// }
//
// @RequestMapping(method = RequestMethod.POST, value = { "/dmp/softdelete" }, consumes = "application/json", produces="text/plain")
// public @ResponseBody ResponseEntity<Object> softDelete(@RequestBody DMP dmp) {
//
// try{
// DMP d = dMPDao.read(dmp.getId());
// d.setStatus(new Short("-1"));
// dMPDao.update(d);
// return ResponseEntity.status(HttpStatus.OK).body("{\"msg\":\"deleted DMP!\"");
// } catch (Exception e) {
// e.printStackTrace();
// return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"msg\":\"Could not soft delete DMP!\"");
// }
//
// }
//
//
//
//
// ////////////////////////////////
// //// USER - RELATED ACTIONS ////
// ////////////////////////////////
//
// @RequestMapping(method = RequestMethod.GET, value = { "/dmp/getofuser" }, produces="text/plain")
// public @ResponseBody ResponseEntity<List<DMP>> getDmpsOfUser(){
//
// String userID = null;
// try {
// userID = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
// } catch(NullPointerException ex) {
// return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
// }
//
// try {
// //List<DMP> nonDeleted = userInfoDao.getDmpsOfUser(userID);
// List<DMP> nonDeleted = dMPDao.getDMPsOfUser(userID);
// return ResponseEntity.status(HttpStatus.OK).body(nonDeleted);
// }
// catch(Exception ex) {
// return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
// }
//
// }
//
// @RequestMapping(method = RequestMethod.POST, value = { "/dmp/createofuser" }, produces="text/plain", consumes = "application/json")
// public @ResponseBody ResponseEntity<DMP> createDmpOfUser(@RequestBody DataManagementPlan dataManagementPlan){
//
//
// 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 {
// DMP dmp = dataManagementPlan.toDataModel();
//
// if(dmp.getOrganisations()!=null&&!dmp.getOrganisations().isEmpty()){
// for(eu.eudat.entities.Organisation organisation: dmp.getOrganisations()){
// OrganisationCriteria criteria = new OrganisationCriteria();
// criteria.setLike(organisation.getReference());
// List<eu.eudat.entities.Organisation> entries = this.organisationDao.listBy(criteria);
// if(entries!=null&&!entries.isEmpty())organisation.setId(entries.get(0).getId());
// else organisation = this.organisationDao.create(organisation);
// }
// }
//
// if(dmp.getResearchers()!=null&&!dmp.getResearchers().isEmpty()){
// for(eu.eudat.entities.Researcher researcher : dmp.getResearchers()){
// ResearcherCriteria criteria = new ResearcherCriteria();
// criteria.setLike(researcher.getReference());
// List<eu.eudat.entities.Researcher> entries = this.researcherDao.listBy(criteria);
// if(entries!=null&&!entries.isEmpty())researcher.setId(entries.get(0).getId());
// else researcher = this.researcherDao.create(researcher);
// }
// }
// dmp.setId(null);
//
// dmp.setCreator(userInfo);
// dmp.setProject(this.projectDao.read(dataManagementPlan.getProject().getId()));
// Set<UserInfo> users = new HashSet<UserInfo>();
// users.add(userInfo);
// dmp.setUsers(users);
//
// dmp.setCreated(new Date());
// dmp.setModified(new Date());
// dmp.setStatus(new Short("0"));
//
// DMP newdmp = dMPDao.create(dmp);
//
// return ResponseEntity.status(HttpStatus.OK).body(newdmp);
// }
// catch(Exception ex) {
// ex.printStackTrace();
// return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
// }
//
// }
//
// @RequestMapping(method = RequestMethod.POST, value = { "/dmp/cloneforuser" }, produces="text/plain", consumes = "application/json")
// public @ResponseBody ResponseEntity<DMP> cloneDmpOfUser(@RequestBody DMP dmp){
//
// 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);
//
// DMP clone = dMPDao.read(dmp.getId());
//
//
// try {
//
// Set<UserInfo> users = new HashSet<UserInfo>();
// users.add(userInfo);
// clone.setUsers(users);
//
// clone.setCreated(new Date());
// clone.setModified(new Date());
// clone.setStatus(new Short("0"));
//
// String cloneLabel = dmp.getLabel();
// if(cloneLabel==null || cloneLabel.isEmpty()) //if the provided label is null or empty, use parent's label + "_clone"
// cloneLabel = clone.getLabel()+"_clone";
// clone.setVersion(clone.getVersion()+1);
// clone.setLabel(cloneLabel);
// clone.setPrevious(clone.getId());
//
//
// clone.setId(null);
//
// clone = dMPDao.create(clone);
//
// return ResponseEntity.status(HttpStatus.OK).body(clone);
// }
// catch(Exception ex) {
// ex.printStackTrace();
// return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
// }
//
// }
//
//
// @RequestMapping(method = RequestMethod.POST, value = { "/dmp/adduser" }, produces="text/plain")
// public @ResponseBody ResponseEntity<DMP> addUserToDmp(@RequestBody DMP dmp){
//
//
// String userID = null;
// try {
// userID = SecurityContextHolder.getContext().getAuthentication().getPrincipal().toString();
// } catch(NullPointerException ex) {
// return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
// }
//
// final UserInfo userInfo = userInfoDao.read(UUID.fromString(userID));
//
// if(userInfo==null) //this should normally never happer
// return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
//
// if(dmp==null || dmp.getId()==null)
// return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
//
// try {
//
// DMP existingDMP = dMPDao.read(dmp.getId());
//
// Set<UserInfo> users = existingDMP.getUsers().parallelStream().filter(user -> user.getId().toString() != userInfo.getId().toString()).collect(Collectors.toSet());
//
// users.add(userInfo);
// dmp.setUsers(users);
//
// DMP updateddmp = dMPDao.update(dmp);
//
// return ResponseEntity.status(HttpStatus.OK).body(updateddmp);
// }
// catch(Exception ex) {
// return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(null);
// }
//
// }
//
//
//
//
// private static void addNullAndForeignElems(DMP existing, DMP newone) {
//
// newone.setModified(new Date());
// if(newone.getStatus()==null)
// newone.setStatus(existing.getStatus());
// if(newone.getCreated()==null)
// newone.setCreated(existing.getCreated());
//
// newone.setDataset(existing.getDataset());
// newone.setOrganisations(existing.getOrganisations());
// newone.setResearchers(existing.getResearchers());
// newone.setUsers(existing.getUsers());
// }
}