package rest.entities; import java.util.ArrayList; 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.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.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 com.mchange.v2.sql.filter.SynchronizedFilterDataSource; 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.Organisation; import entities.Project; import entities.Researcher; import entities.UserInfo; import entities.responses.IDLabelPair; import helpers.SerializerProvider; import helpers.Transformers; import responses.RestResponse; @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.GET, value = { "/dmps" }, produces="text/plain") public @ResponseBody ResponseEntity listDMPs(){ try { List allIDs = dMPDao.listAllIDs(); return ResponseEntity.status(HttpStatus.OK).body(SerializerProvider.toJson(allIDs)); } catch(Exception ex) { ex.printStackTrace(); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Serialization issue: "+ex.getMessage()); } } @RequestMapping(method = RequestMethod.GET, value = { "/dmps/{id}" }, produces="application/json") public @ResponseBody ResponseEntity getDMP(@PathVariable("id") String id, @RequestParam(value="eager", defaultValue="false") boolean eager){ try { DMP dmp = dMPDao.read(UUID.fromString(id)); if(eager) { dmp.getOrganisations().size(); //used only for lazy load trigger... dmp.getResearchers().size(); //used only for lazy load trigger... dmp.getUsers().size(); //used only for lazy load trigger... } return ResponseEntity.status(HttpStatus.OK).body(SerializerProvider.toJson(dmp)); } catch(Exception ex) { ex.printStackTrace(); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Erroneous input: "+ex.getMessage()); } } @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(SerializerProvider.toJson(allIDLabels)); } catch(Exception ex) { ex.printStackTrace(); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Serialization issue: "+ex.getMessage()); } } /** * 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 new ResponseEntity(SerializerProvider.toJson(allDMPs), HttpStatus.OK); } catch(Exception ex) { ex.printStackTrace(); return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR); } } @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(SerializerProvider.toJson(createdDmp)); } catch (Exception e) { e.printStackTrace(); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"msg\":\"Could not create DMP!\""); } } @Transactional @RequestMapping(method = RequestMethod.POST, value = { "/dmp/update" }, consumes = "application/json", produces="application/json") public @ResponseBody ResponseEntity updateDMP(@RequestBody DMP dmp) { DMP previousDmp = dMPDao.read(dmp.getId()); addNullAndForeignElems(previousDmp, dmp); try { DMP updatedDMP = dMPDao.update(dmp); return ResponseEntity.status(HttpStatus.CREATED).body(SerializerProvider.toJson(updatedDMP)); } catch (Exception e) { e.printStackTrace(); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"msg\":\"Could not update DMP!\""); } } @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(SerializerProvider.toJson(datasets)); } catch (Exception e) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("{\"msg\":\"Could not get datasets of DMP!\""); } } @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) { DMP d = dMPDao.read(dmp.getId()); d.setStatus(new Short("-1")); try { int code = updateDMP(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 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("You have not logged in. You shouldn't be here"); } try { //List nonDeleted = userInfoDao.getDmpsOfUser(userID); List nonDeleted = dMPDao.getDMPsOfUser(userID); return ResponseEntity.status(HttpStatus.OK).body(SerializerProvider.toJson(nonDeleted)); } catch(Exception ex) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Serialization issue: "+ex.getMessage()); } } @RequestMapping(method = RequestMethod.POST, value = { "/dmp/createofuser" }, produces="text/plain", consumes = "application/json") public @ResponseBody ResponseEntity createDmpOfUser(@RequestBody DMP dmp){ 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 { dmp.setId(null); dmp.setCreator(userInfo); 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(SerializerProvider.toJson(newdmp)); } catch(Exception ex) { ex.printStackTrace(); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Serialization issue: "+ex.getMessage()); } } @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("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"); 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(SerializerProvider.toJson(clone)); } catch(Exception ex) { ex.printStackTrace(); return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Serialization issue: "+ex.getMessage()); } } @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("You have not logged in. You shouldn't be here"); } final 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"); if(dmp==null || dmp.getId()==null) return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("DMP is null or dmp has null id"); 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(SerializerProvider.toJson(updateddmp)); } catch(Exception ex) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Serialization issue: "+ex.getMessage()); } } 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()); } }