Altered fetching logic for users and dmps

This commit is contained in:
Nikolaos Laskaris 2017-11-16 17:43:17 +02:00
parent 48a7cb0606
commit 48ed8ac469
9 changed files with 136 additions and 14 deletions

View File

@ -1,13 +1,17 @@
package dao.entities;
import java.util.List;
import java.util.UUID;
import dao.Dao;
import entities.DMP;
import entities.UserInfo;
public interface UserInfoDao extends Dao<UserInfo, UUID> {
public UserInfo getByIdAndMail(String identification, String email);
public UserInfo getUserInfo(String userID);
public UserInfo getByIdAndMail(String id, String email);
public UserInfo getByMail(String email);
@ -15,4 +19,6 @@ public interface UserInfoDao extends Dao<UserInfo, UUID> {
public UserInfo getByUsername(String username);
public List<DMP> getDmpsOfUser(String userID);
}

View File

@ -1,12 +1,14 @@
package dao.entities;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import javax.persistence.NoResultException;
import javax.persistence.TypedQuery;
import dao.JpaDao;
import entities.DMP;
import entities.UserInfo;
import entities.security.UserAuth;
@ -21,10 +23,25 @@ public class UserInfoDaoImpl extends JpaDao<UserInfo, UUID> implements UserInfoD
@Override
public UserInfo getByIdAndMail(String identification, String email) {
String queryString = "FROM UserInfo userInfo where userInfo.identification = :userInfoID and userInfo.email = :userInfoEmail";
public UserInfo getUserInfo(String userID) {
String queryString = "FROM UserInfo userInfo where userInfo.id = :userInfoID";
TypedQuery<UserInfo> typedQuery = entityManager.createQuery(queryString, UserInfo.class);
typedQuery.setParameter("userInfoID", identification);
typedQuery.setParameter("userInfoID", UUID.fromString(userID));
try {
return typedQuery.getSingleResult();
}
catch(NoResultException ex) {
return null;
}
}
@Override
public UserInfo getByIdAndMail(String id, String email) {
String queryString = "FROM UserInfo userInfo where userInfo.id = :userInfoID and userInfo.email = :userInfoEmail";
TypedQuery<UserInfo> typedQuery = entityManager.createQuery(queryString, UserInfo.class);
typedQuery.setParameter("userInfoID", UUID.fromString(id));
typedQuery.setParameter("userInfoEmail", email);
try {
return typedQuery.getSingleResult();
@ -80,6 +97,23 @@ public class UserInfoDaoImpl extends JpaDao<UserInfo, UUID> implements UserInfoD
}
}
@Override
public List<DMP> getDmpsOfUser(String userID) {
String queryString = "select dmp from DMP dmp join dmp.users user where user.id=:userid and dmp.status >= 0";
TypedQuery<DMP> 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;
}
}

View File

@ -36,7 +36,7 @@ import com.fasterxml.jackson.databind.SerializationFeature;
@Entity
@Table(name="\"DMP\"")
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
@JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id", scope = DMP.class)
public class DMP implements Serializable {

View File

@ -80,6 +80,9 @@ 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;
@Column(name = "\"Created\"")
private Date created = null;
@ -210,6 +213,16 @@ public class Project implements Serializable {
public UUID getCreationUser() {
return creationUser;
}
public void setCreationUser(UUID creationUser) {
this.creationUser = creationUser;
}
public String toString() {
try {
return new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT).writeValueAsString(this).replace("\"", "&quot;");

View File

@ -85,10 +85,11 @@ public class UserInfo implements Serializable{
private Set<DMP> dmps;
/*
public Set<DMP> getDmpsNonDeleted(){
return getDmps().parallelStream().filter(dmp -> dmp.getStatus()>=0).collect(Collectors.toSet());
}
*/
public Set<DMP> getDmps() {
return dmps;

View File

@ -232,13 +232,8 @@ public class DMPs {
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 {
Set<DMP> nonDeleted = userInfo.getDmpsNonDeleted();
List<DMP> nonDeleted = userInfoDao.getDmpsOfUser(userID);
return ResponseEntity.status(HttpStatus.OK).body(SerializerProvider.toJson(nonDeleted));
}
catch(Exception ex) {

View File

@ -257,6 +257,7 @@ public class Projects {
project.setId(null);
project.setStatus(new Short("0"));
project.setCreationUser(userInfo.getId());
project.setCreated(new Date());
project.setModified(new Date());

View File

@ -11,6 +11,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;
@ -36,6 +37,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.DataRepository;
@ -47,6 +49,7 @@ import entities.Project;
import entities.Registry;
import entities.Researcher;
import entities.Service;
import entities.UserInfo;
import helpers.SerializerProvider;
import helpers.Transformers;
import responses.RestResponse;
@ -68,6 +71,38 @@ public class Users {
@Autowired private RegistryDao registryDao;
@Autowired private ResearcherDao researcherDao;
@Autowired private ServiceDao serviceDao;
@Autowired private UserInfoDao userInfoDao;
@RequestMapping(method = RequestMethod.GET, value = { "/user/whoami" }, produces="application/json")
public @ResponseBody ResponseEntity<Object> whoami(){
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.getUserInfo(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 {
return new ResponseEntity<Object>(SerializerProvider.toJson(userInfo), HttpStatus.OK);
}
catch(Exception ex) {
ex.printStackTrace();
return new ResponseEntity<>(null, HttpStatus.INTERNAL_SERVER_ERROR);
}
}

View File

@ -3,6 +3,7 @@ import static org.junit.Assert.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.UUID;
import org.junit.Before;
@ -17,6 +18,7 @@ import org.springframework.security.authentication.UsernamePasswordAuthenticatio
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.type.CollectionType;
import com.fasterxml.jackson.databind.type.TypeFactory;
@ -201,7 +203,7 @@ public class TestRest {
}
@Test
//@Test
public void testDmpClone() {
System.out.println(dmpsService.getDmpsOfUser().getBody());
@ -266,5 +268,40 @@ public class TestRest {
}
@Test
public void testProject2() {
// String a = null;
// try {
// //a = (String)dmpsService.getDmpsOfUser().getBody();
//
// Set<DMP> dmps = new ObjectMapper().readValue(dmpsService.getDmpsOfUser().getBody().toString(), new TypeReference<Set<DMP>>(){});
// System.out.println(dmps);
//
//// Set<DMP> dmps = SerializerProvider.fromJson((String)dmpsService.getDmpsOfUser().getBody(), new TypeReference<Set<DMP>>(){});
// } catch (Exception e) {
//
// e.printStackTrace();
// }
CollectionType typeReference = TypeFactory.defaultInstance().constructCollectionType(List.class, DMP.class);
List<DMP> dmps = null;
try {
dmps = SerializerProvider.fromJson(dmpsService.getDmpsOfUser().getBody().toString(), typeReference) ;
System.out.println(dmps);
} catch (Exception e) {
e.printStackTrace();
fail("Should not have thrown any exception");
}
assertEquals("aaa", "aaa");
}
}