diff --git a/dmp-backend/src/main/java/dao/entities/DMPDao.java b/dmp-backend/src/main/java/dao/entities/DMPDao.java index 07b533143..1bc531e8b 100644 --- a/dmp-backend/src/main/java/dao/entities/DMPDao.java +++ b/dmp-backend/src/main/java/dao/entities/DMPDao.java @@ -15,6 +15,6 @@ public interface DMPDao extends Dao { List listAllIDsLabels(); - + List getDMPsOfUser(String userID); } \ No newline at end of file diff --git a/dmp-backend/src/main/java/dao/entities/DMPDaoImpl.java b/dmp-backend/src/main/java/dao/entities/DMPDaoImpl.java index 7553b7cd5..c3b1b83a7 100644 --- a/dmp-backend/src/main/java/dao/entities/DMPDaoImpl.java +++ b/dmp-backend/src/main/java/dao/entities/DMPDaoImpl.java @@ -41,6 +41,24 @@ public class DMPDaoImpl extends JpaDao implements DMPDao { .collect(Collectors.toList()); } + @Override + public List getDMPsOfUser(String userID) { + + String queryString = "select dmp from DMP dmp where dmp.creator.id=:userid and dmp.status >= 0"; + TypedQuery typedQuery = entityManager.createQuery(queryString, DMP.class); + typedQuery.setParameter("userid", UUID.fromString(userID)); + try { + return typedQuery.getResultList(); + } + catch(Exception ex) { //no need to distinguish between exceptions for the moment + ex.printStackTrace(); + return null; + } + + + } + + } diff --git a/dmp-backend/src/main/java/dao/entities/ProjectDao.java b/dmp-backend/src/main/java/dao/entities/ProjectDao.java index 5591c76d2..376a38c0c 100644 --- a/dmp-backend/src/main/java/dao/entities/ProjectDao.java +++ b/dmp-backend/src/main/java/dao/entities/ProjectDao.java @@ -13,4 +13,6 @@ public interface ProjectDao extends Dao { public List listAllIDsLabels(); + public List getProjectsOfUser(String userID); + } \ No newline at end of file diff --git a/dmp-backend/src/main/java/dao/entities/ProjectDaoImpl.java b/dmp-backend/src/main/java/dao/entities/ProjectDaoImpl.java index 6aa4f394d..bdd2ce37c 100644 --- a/dmp-backend/src/main/java/dao/entities/ProjectDaoImpl.java +++ b/dmp-backend/src/main/java/dao/entities/ProjectDaoImpl.java @@ -9,6 +9,7 @@ import javax.persistence.TypedQuery; import org.hibernate.query.Query; import dao.JpaDao; +import entities.DMP; import entities.Project; import entities.responses.IDLabelPair; @@ -38,5 +39,24 @@ public class ProjectDaoImpl extends JpaDao implements ProjectDao .collect(Collectors.toList()); } + + public List getProjectsOfUser(String userID){ + + String queryString = "select p from Project p where p.creationUser.id=:userid and project.status >= 0"; + TypedQuery typedQuery = entityManager.createQuery(queryString, Project.class); + typedQuery.setParameter("userid", UUID.fromString(userID)); + try { + return typedQuery.getResultList(); + } + catch(Exception ex) { //no need to distinguish between exceptions for the moment + ex.printStackTrace(); + return null; + } + + } + + + + } diff --git a/dmp-backend/src/main/java/entities/DMP.java b/dmp-backend/src/main/java/entities/DMP.java index 0932e6666..561555644 100644 --- a/dmp-backend/src/main/java/entities/DMP.java +++ b/dmp-backend/src/main/java/entities/DMP.java @@ -77,6 +77,11 @@ public class DMP implements Serializable { private DMPProfile profile; + @OneToOne(fetch = FetchType.EAGER) + @JoinColumn(name = "\"Creator\"", nullable = true) + private UserInfo creator; + + @OneToMany(fetch = FetchType.LAZY) @JoinTable(name="\"DMPOrganisation\"", joinColumns={@JoinColumn(name="\"DMP\"", referencedColumnName="\"ID\"")}, @@ -125,7 +130,16 @@ public class DMP implements Serializable { this.description = description; } - + + public UserInfo getCreator() { + return creator; + } + + + public void setCreator(UserInfo creator) { + this.creator = creator; + } + public Short getStatus() { return status; diff --git a/dmp-backend/src/main/java/entities/Dataset.java b/dmp-backend/src/main/java/entities/Dataset.java index 71b439ad8..dd55f1457 100644 --- a/dmp-backend/src/main/java/entities/Dataset.java +++ b/dmp-backend/src/main/java/entities/Dataset.java @@ -106,6 +106,10 @@ public class Dataset implements Serializable { @Column(name = "\"Modified\"") private Date modified = new Date(); + @OneToOne(fetch = FetchType.EAGER) + @JoinColumn(name = "\"Creator\"", nullable = true) + private UserInfo creator; + @Column(name = "\"Description\"") private String description; @@ -113,13 +117,20 @@ public class Dataset implements Serializable { public String getDescription() { return description; } - - + public void setDescription(String description) { this.description = description; } + public UserInfo getCreator() { + return creator; + } + + public void setCreator(UserInfo creator) { + this.creator = creator; + } + public Short getStatus() { return status; } diff --git a/dmp-backend/src/main/java/entities/Project.java b/dmp-backend/src/main/java/entities/Project.java index ff5bbebae..e79ce8792 100644 --- a/dmp-backend/src/main/java/entities/Project.java +++ b/dmp-backend/src/main/java/entities/Project.java @@ -80,9 +80,11 @@ public class Project implements Serializable { @Column(name = "\"Status\"", nullable = false) private Short status; - @Type(type="org.hibernate.type.PostgresUUIDType") - @Column(name = "\"CreationUser\"") - private UUID creationUser; + + @OneToOne(fetch = FetchType.EAGER) + @JoinColumn(name = "\"CreationUser\"", nullable = true) + private UserInfo creationUser; + @Column(name = "\"Created\"") private Date created = null; @@ -210,15 +212,14 @@ public class Project implements Serializable { public void setDmps(Set dmps) { this.dmps = dmps; } - - - - public UUID getCreationUser() { + + + public UserInfo getCreationUser() { return creationUser; } - public void setCreationUser(UUID creationUser) { + public void setCreationUser(UserInfo creationUser) { this.creationUser = creationUser; } diff --git a/dmp-backend/src/main/java/rest/entities/DMPs.java b/dmp-backend/src/main/java/rest/entities/DMPs.java index f7f4abc95..0ad83aa16 100644 --- a/dmp-backend/src/main/java/rest/entities/DMPs.java +++ b/dmp-backend/src/main/java/rest/entities/DMPs.java @@ -153,7 +153,6 @@ public class DMPs { addNullAndForeignElems(previousDmp, dmp); - try { DMP updatedDMP = dMPDao.update(dmp); return ResponseEntity.status(HttpStatus.CREATED).body(SerializerProvider.toJson(updatedDMP)); @@ -233,7 +232,8 @@ public class DMPs { } try { - List nonDeleted = userInfoDao.getDmpsOfUser(userID); + //List nonDeleted = userInfoDao.getDmpsOfUser(userID); + List nonDeleted = dMPDao.getDMPsOfUser(userID); return ResponseEntity.status(HttpStatus.OK).body(SerializerProvider.toJson(nonDeleted)); } catch(Exception ex) { @@ -262,6 +262,8 @@ public class DMPs { dmp.setId(null); + dmp.setCreator(userInfo); + Set users = new HashSet(); users.add(userInfo); dmp.setUsers(users); @@ -269,6 +271,7 @@ public class DMPs { dmp.setCreated(new Date()); dmp.setModified(new Date()); dmp.setStatus(new Short("0")); + dmp.setProfileData(null); DMP newdmp = dMPDao.create(dmp); diff --git a/dmp-backend/src/main/java/rest/entities/Datasets.java b/dmp-backend/src/main/java/rest/entities/Datasets.java index 3bc26d569..c5818b6a6 100644 --- a/dmp-backend/src/main/java/rest/entities/Datasets.java +++ b/dmp-backend/src/main/java/rest/entities/Datasets.java @@ -16,6 +16,7 @@ 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; @@ -43,6 +44,7 @@ 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; @@ -51,6 +53,7 @@ import entities.DatasetProfileRuleset; import entities.DatasetProfileViewstyle; import entities.Organisation; import entities.Project; +import entities.UserInfo; import helpers.SafeCleanAttribs; import helpers.SerializerProvider; import helpers.Transformers; @@ -73,6 +76,7 @@ public class Datasets { @Autowired private RegistryDao registryDao; @Autowired private ResearcherDao researcherDao; @Autowired private ServiceDao serviceDao; + @Autowired private UserInfoDao userInfoDao; // FETCH BY DATASET(S) @@ -124,10 +128,27 @@ public class Datasets { @RequestMapping(method = RequestMethod.POST, value = { "/dataset/create" }, consumes = "application/json", produces="application/json") public @ResponseBody ResponseEntity createDataset(@RequestBody Dataset dataset) { + + + 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"); + + + dataset.setId(null); dataset.setCreated(new Date()); dataset.setModified(new Date()); dataset.setStatus(new Short("0")); + dataset.setCreator(userInfo); try { dataset = datasetDao.create(dataset); return ResponseEntity.status(HttpStatus.CREATED).body(SerializerProvider.toJson(dataset)); diff --git a/dmp-backend/src/main/java/rest/entities/Projects.java b/dmp-backend/src/main/java/rest/entities/Projects.java index 2679270ba..80bec6518 100644 --- a/dmp-backend/src/main/java/rest/entities/Projects.java +++ b/dmp-backend/src/main/java/rest/entities/Projects.java @@ -219,6 +219,17 @@ public class Projects { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("There's no such a user on the system. You shouldn't be here"); + try { + List userProjects = projectDao.getProjectsOfUser(userID); + return ResponseEntity.status(HttpStatus.OK).body(SerializerProvider.toJson(userProjects)); + } + catch(Exception ex) { + return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Serialization issue: "+ex.getMessage()); + } + + + /* + * OLD ONE Map userProjects = new HashMap(); userInfo.getDmps().forEach( dmp -> { @@ -232,6 +243,7 @@ public class Projects { catch(Exception ex) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body("Serialization issue: "+ex.getMessage()); } + */ } @@ -257,7 +269,7 @@ public class Projects { project.setId(null); project.setStatus(new Short("0")); - project.setCreationUser(userInfo.getId()); + project.setCreationUser(userInfo); project.setCreated(new Date()); project.setModified(new Date());