From 1630b3aedee276f9f873f8a193788fd68d73615e Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Sat, 8 Feb 2020 13:53:58 +0100 Subject: [PATCH] Implemented user search response --- pom.xml | 2 +- src/main/java/envoy/server/data/User.java | 4 +-- .../server/database/PersistenceManager.java | 4 +++ .../processors/ContactsRequestProcesor.java | 33 +++++++++++++++++++ .../processors/LoginCredentialProcessor.java | 3 +- 5 files changed, 41 insertions(+), 5 deletions(-) create mode 100644 src/main/java/envoy/server/processors/ContactsRequestProcesor.java diff --git a/pom.xml b/pom.xml index db5b8da..da0d59a 100644 --- a/pom.xml +++ b/pom.xml @@ -28,7 +28,7 @@ com.github.informatik-ag-ngl envoy-common - develop-SNAPSHOT + f~contacts-SNAPSHOT com.github.informatik-ag-ngl diff --git a/src/main/java/envoy/server/data/User.java b/src/main/java/envoy/server/data/User.java index 877a699..6a07bcf 100644 --- a/src/main/java/envoy/server/data/User.java +++ b/src/main/java/envoy/server/data/User.java @@ -22,8 +22,8 @@ import javax.persistence.*; @Table(name = "users") @NamedQueries( { @NamedQuery(query = "SELECT u FROM User u WHERE u.name = :name", name = "getUserByName"), - @NamedQuery(query = "SELECT u.contacts FROM User u WHERE u = :user", name = "getContactsOfUser")// not tested - } + @NamedQuery(query = "SELECT u.contacts FROM User u WHERE u = :user", name = "getContactsOfUser"), // not tested + @NamedQuery(query = "SELECT u FROM User u WHERE lower(u.name) LIKE lower(:searchPhrase)", name = "searchUsers") } ) public class User { diff --git a/src/main/java/envoy/server/database/PersistenceManager.java b/src/main/java/envoy/server/database/PersistenceManager.java index 84587e6..32a5219 100644 --- a/src/main/java/envoy/server/database/PersistenceManager.java +++ b/src/main/java/envoy/server/database/PersistenceManager.java @@ -176,6 +176,10 @@ public class PersistenceManager { return entityManager.createNamedQuery("getUnreadMessages").setParameter("recipient", user).getResultList(); } + public List searchUsers(String searchPhrase) { + return entityManager.createNamedQuery("searchUsers").setParameter("searchPhrase", searchPhrase).getResultList(); + } + /** * @param user the User whose contacts should be retrieved * @return the contacts of this User - currently everyone using Envoy diff --git a/src/main/java/envoy/server/processors/ContactsRequestProcesor.java b/src/main/java/envoy/server/processors/ContactsRequestProcesor.java new file mode 100644 index 0000000..7757345 --- /dev/null +++ b/src/main/java/envoy/server/processors/ContactsRequestProcesor.java @@ -0,0 +1,33 @@ +package envoy.server.processors; + +import java.io.IOException; +import java.util.stream.Collectors; + +import envoy.event.ContactsRequest; +import envoy.server.ObjectProcessor; +import envoy.server.database.PersistenceManager; +import envoy.server.net.ObjectWriteProxy; + +/** + * Project: envoy-server-standalone
+ * File: ContactsRequestProcesor.java
+ * Created: 08.02.2020
+ * + * @author Kai S. K. Engelbart + * @since Envoy Server Standalone v0.1-alpha + */ +public class ContactsRequestProcesor implements ObjectProcessor { + + @Override + public void process(ContactsRequest request, long socketId, ObjectWriteProxy writeProxy) throws IOException { + writeProxy.write(socketId, + PersistenceManager.getPersistenceManager() + .searchUsers(request.get()) + .stream() + .map(envoy.server.data.User::toCommonUser) + .collect(Collectors.toList())); + } + + @Override + public Class getInputClass() { return ContactsRequest.class; } +} diff --git a/src/main/java/envoy/server/processors/LoginCredentialProcessor.java b/src/main/java/envoy/server/processors/LoginCredentialProcessor.java index 4a7f2c8..2bad4e4 100644 --- a/src/main/java/envoy/server/processors/LoginCredentialProcessor.java +++ b/src/main/java/envoy/server/processors/LoginCredentialProcessor.java @@ -52,8 +52,7 @@ public class LoginCredentialProcessor implements ObjectProcessor