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> getPaged(@RequestBody DataManagementPlanTableRequest dataManagementPlanTableRequest) { try { DataTableData dataTable = new DataManagementPlanManager().getPaged(dMPDao, dataManagementPlanTableRequest); return new ResponseItem>().status(HttpStatus.OK).payload(dataTable); } catch (Exception ex) { ex.printStackTrace(); return new ResponseItem>().status(HttpStatus.BAD_REQUEST).message(ex.getMessage()); } } @RequestMapping(method = RequestMethod.GET, value = { "/dmps/getSingle/{id}" }, produces="application/json") public @ResponseBody ResponseItem getPaged(@PathVariable String id) { try { eu.eudat.models.dmp.DataManagementPlan project = new DataManagementPlanManager().getSingle(dMPDao, id); return new ResponseItem().status(HttpStatus.OK).payload(project); } catch (Exception ex) { ex.printStackTrace(); return new ResponseItem().status(HttpStatus.BAD_REQUEST).message(ex.getMessage()); } } @Transactional @RequestMapping(method = RequestMethod.POST, value = { "/dmps/add" }, consumes = "application/json", produces="application/json") public @ResponseBody ResponseEntity 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> listDMPs(){ // try { // List 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 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> listDmpLabelID(){ // try { // List 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 getAllDMPs(){ // // try { // List 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 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 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 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 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> getDatasetsOfDMP(@RequestBody DMP dmp) { // try { // List 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 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 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> 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 nonDeleted = userInfoDao.getDmpsOfUser(userID); // List 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 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 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 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 users = new HashSet(); // 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 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 users = new HashSet(); // 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 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 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()); // } }