Merge branch 'master' of gitlab.eudat.eu:dmp/OpenAIRE-EUDAT-DMP-service-pilot

This commit is contained in:
Nikolaos Laskaris 2017-11-16 19:32:55 +02:00
commit 360cdcb80d
10 changed files with 176 additions and 36 deletions

View File

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

View File

@ -1,12 +1,14 @@
package dao.entities; package dao.entities;
import java.util.List; import java.util.List;
import java.util.Set;
import java.util.UUID; import java.util.UUID;
import javax.persistence.NoResultException; import javax.persistence.NoResultException;
import javax.persistence.TypedQuery; import javax.persistence.TypedQuery;
import dao.JpaDao; import dao.JpaDao;
import entities.DMP;
import entities.UserInfo; import entities.UserInfo;
import entities.security.UserAuth; import entities.security.UserAuth;
@ -21,10 +23,25 @@ public class UserInfoDaoImpl extends JpaDao<UserInfo, UUID> implements UserInfoD
@Override @Override
public UserInfo getByIdAndMail(String identification, String email) { public UserInfo getUserInfo(String userID) {
String queryString = "FROM UserInfo userInfo where userInfo.identification = :userInfoID and userInfo.email = :userInfoEmail"; String queryString = "FROM UserInfo userInfo where userInfo.id = :userInfoID";
TypedQuery<UserInfo> typedQuery = entityManager.createQuery(queryString, UserInfo.class); 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); typedQuery.setParameter("userInfoEmail", email);
try { try {
return typedQuery.getSingleResult(); 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 @Entity
@Table(name="\"DMP\"") @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 { public class DMP implements Serializable {

View File

@ -80,6 +80,9 @@ public class Project implements Serializable {
@Column(name = "\"Status\"", nullable = false) @Column(name = "\"Status\"", nullable = false)
private Short status; private Short status;
@Type(type="org.hibernate.type.PostgresUUIDType")
@Column(name = "\"CreationUser\"")
private UUID creationUser;
@Column(name = "\"Created\"") @Column(name = "\"Created\"")
private Date created = null; 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() { public String toString() {
try { try {
return new ObjectMapper().enable(SerializationFeature.INDENT_OUTPUT).writeValueAsString(this).replace("\"", "&quot;"); 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; private Set<DMP> dmps;
/*
public Set<DMP> getDmpsNonDeleted(){ public Set<DMP> getDmpsNonDeleted(){
return getDmps().parallelStream().filter(dmp -> dmp.getStatus()>=0).collect(Collectors.toSet()); return getDmps().parallelStream().filter(dmp -> dmp.getStatus()>=0).collect(Collectors.toSet());
} }
*/
public Set<DMP> getDmps() { public Set<DMP> getDmps() {
return dmps; 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"); 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 { try {
Set<DMP> nonDeleted = userInfo.getDmpsNonDeleted(); List<DMP> nonDeleted = userInfoDao.getDmpsOfUser(userID);
return ResponseEntity.status(HttpStatus.OK).body(SerializerProvider.toJson(nonDeleted)); return ResponseEntity.status(HttpStatus.OK).body(SerializerProvider.toJson(nonDeleted));
} }
catch(Exception ex) { catch(Exception ex) {

View File

@ -257,24 +257,25 @@ public class Projects {
project.setId(null); project.setId(null);
project.setStatus(new Short("0")); project.setStatus(new Short("0"));
project.setCreationUser(userInfo.getId());
project.setCreated(new Date()); project.setCreated(new Date());
project.setModified(new Date()); project.setModified(new Date());
Project newproj = projectDao.create(project); Project newproj = projectDao.create(project);
DMP newdmp = new DMP(); // DMP newdmp = new DMP();
newdmp.setId(null); // newdmp.setId(null);
newdmp.setLabel("Auto-Generated"); // newdmp.setLabel("Auto-Generated");
newdmp.setCreated(new Date()); // newdmp.setCreated(new Date());
newdmp.setVersion(1); // newdmp.setVersion(1);
newdmp.setStatus(new Short("0")); // newdmp.setStatus(new Short("0"));
newdmp.setProject(newproj); // newdmp.setProject(newproj);
//
Set<UserInfo> users = new HashSet<UserInfo>(); // Set<UserInfo> users = new HashSet<UserInfo>();
users.add(userInfo); // users.add(userInfo);
newdmp.setUsers(users); // newdmp.setUsers(users);
//
newdmp = dMPDao.create(newdmp); // newdmp = dMPDao.create(newdmp);
return ResponseEntity.status(HttpStatus.OK).body(SerializerProvider.toJson(newproj)); return ResponseEntity.status(HttpStatus.OK).body(SerializerProvider.toJson(newproj));
} }

View File

@ -11,6 +11,7 @@ import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType; import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.util.MultiValueMap; import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
@ -36,6 +37,7 @@ import dao.entities.ProjectDao;
import dao.entities.RegistryDao; import dao.entities.RegistryDao;
import dao.entities.ResearcherDao; import dao.entities.ResearcherDao;
import dao.entities.ServiceDao; import dao.entities.ServiceDao;
import dao.entities.UserInfoDao;
import entities.DMP; import entities.DMP;
import entities.DMPProfile; import entities.DMPProfile;
import entities.DataRepository; import entities.DataRepository;
@ -47,6 +49,7 @@ import entities.Project;
import entities.Registry; import entities.Registry;
import entities.Researcher; import entities.Researcher;
import entities.Service; import entities.Service;
import entities.UserInfo;
import helpers.SerializerProvider; import helpers.SerializerProvider;
import helpers.Transformers; import helpers.Transformers;
import responses.RestResponse; import responses.RestResponse;
@ -68,6 +71,39 @@ public class Users {
@Autowired private RegistryDao registryDao; @Autowired private RegistryDao registryDao;
@Autowired private ResearcherDao researcherDao; @Autowired private ResearcherDao researcherDao;
@Autowired private ServiceDao serviceDao; @Autowired private ServiceDao serviceDao;
@Autowired private UserInfoDao userInfoDao;
@RequestMapping(method = RequestMethod.GET, value = { "/user/whoami" }, produces="application/json;charset=UTF-8")
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);
System.out.println(userInfo.getName());
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

@ -65,16 +65,16 @@
</listener> </listener>
<!-- THIS FILTER IS FOR SPRING SECURITY -->
<context-param> <context-param>
<param-name>contextConfigLocation</param-name> <param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml,/WEB-INF/spring-security.xml</param-value> <param-value>/WEB-INF/applicationContext.xml,/WEB-INF/spring-security.xml</param-value>
<!-- <param-value>/WEB-INF/applicationContext.xml</param-value> --> <!-- <param-value>/WEB-INF/applicationContext.xml</param-value> -->
</context-param> </context-param>
<session-config> <session-config>
<session-timeout>30</session-timeout> <session-timeout>30</session-timeout>
</session-config> </session-config>
<!-- THIS FILTER IS FOR SPRING SECURITY -->
<filter> <filter>
<filter-name>springSecurityFilterChain</filter-name> <filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
@ -85,6 +85,23 @@
</filter-mapping> </filter-mapping>
<!-- THIS FILTER IS FOR SUPPORTING UNICODE CHARACTERS -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app> </web-app>

View File

@ -3,6 +3,7 @@ import static org.junit.Assert.*;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Set;
import java.util.UUID; import java.util.UUID;
import org.junit.Before; 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.Authentication;
import org.springframework.security.core.context.SecurityContextHolder; 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.ObjectMapper;
import com.fasterxml.jackson.databind.type.CollectionType; import com.fasterxml.jackson.databind.type.CollectionType;
import com.fasterxml.jackson.databind.type.TypeFactory; import com.fasterxml.jackson.databind.type.TypeFactory;
@ -201,7 +203,7 @@ public class TestRest {
} }
@Test //@Test
public void testDmpClone() { public void testDmpClone() {
System.out.println(dmpsService.getDmpsOfUser().getBody()); 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");
}
} }