From 6ee56bd635b551212c9ef3f08c72c40115444e30 Mon Sep 17 00:00:00 2001 From: delvh Date: Sat, 18 Jan 2020 13:28:54 +0100 Subject: [PATCH] Added method to get contacts, although as of now all Users are returned --- src/main/java/envoy/server/data/User.java | 7 ++++++- .../envoy/server/database/PersistenceManager.java | 12 +++++++++++- .../server/processors/LoginCredentialProcessor.java | 9 +++++++-- 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/src/main/java/envoy/server/data/User.java b/src/main/java/envoy/server/data/User.java index 5f4e28e..e596cca 100644 --- a/src/main/java/envoy/server/data/User.java +++ b/src/main/java/envoy/server/data/User.java @@ -8,6 +8,7 @@ import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; +import javax.persistence.NamedQueries; import javax.persistence.NamedQuery; import javax.persistence.Table; import javax.persistence.Temporal; @@ -28,7 +29,11 @@ import javax.persistence.TemporalType; */ @Entity @Table(name = "users") -@NamedQuery(query = "SELECT u FROM User u WHERE u.id = :id", name = "getUserById") +@NamedQueries( + { @NamedQuery(query = "SELECT u FROM User u WHERE u.id = :id", name = "getUserById"), + @NamedQuery(query = "SELECT u.contacts FROM User u WHERE u = :user", name = "getContactsOfUser")// not tested + } +) public class User { @Id diff --git a/src/main/java/envoy/server/database/PersistenceManager.java b/src/main/java/envoy/server/database/PersistenceManager.java index fb38a9e..a61275f 100644 --- a/src/main/java/envoy/server/database/PersistenceManager.java +++ b/src/main/java/envoy/server/database/PersistenceManager.java @@ -98,7 +98,17 @@ public class PersistenceManager { */ @SuppressWarnings("unchecked") public List getUnreadMessages(User user) { - // TODO may need to be changed to clientId return entityManager.createNamedQuery("getUnreadMessages").setParameter("recipient", user).getResultList(); } + + /** + * @param user the User whose contacts should be retrieved + * @return the contacts of this User - currently everyone using Envoy + * @since Envoy Server Standalone v0.1-alpha + */ + @SuppressWarnings("unchecked") + public List getContacts(User user) { return entityManager.createQuery("FROM User").getResultList(); } + // TODO current solution gets all users, not just contacts. Should be changed to + // entityManager.createNamedQuery("getContactsOfUser").setParameter("user", + // user).getResultList(); } \ No newline at end of file diff --git a/src/main/java/envoy/server/processors/LoginCredentialProcessor.java b/src/main/java/envoy/server/processors/LoginCredentialProcessor.java index 3196382..3574f0e 100644 --- a/src/main/java/envoy/server/processors/LoginCredentialProcessor.java +++ b/src/main/java/envoy/server/processors/LoginCredentialProcessor.java @@ -1,9 +1,9 @@ package envoy.server.processors; import java.io.IOException; -import java.util.ArrayList; import java.util.Date; import java.util.List; +import java.util.stream.Collectors; import envoy.data.Contacts; import envoy.data.LoginCredentials; @@ -42,7 +42,12 @@ public class LoginCredentialProcessor implements ObjectProcessor()); + List users = PersistenceManager.getPersistenceManager() + .getContacts(new envoy.server.data.User(user)) + .stream() + .map(envoy.server.data.User::toCommonUser) + .collect(Collectors.toList()); + Contacts contacts = new Contacts(user.getId(), users); // Complete handshake System.out.println("Sending user...");