From a96199ccd7c7734a545b525e1386843a170be453 Mon Sep 17 00:00:00 2001
From: DieGurke <55625494+DieGurke@users.noreply.github.com>
Date: Mon, 10 Feb 2020 20:02:05 +0100
Subject: [PATCH] Implemented contact adding mechanism + database update on
both clients.
* Added filtering on searchRequest response (temporary: has to be done
in query not manually in ContactsRequestProcessor.)
---
src/main/java/envoy/server/data/User.java | 7 ++--
.../server/database/PersistenceManager.java | 8 ++---
.../processors/ContactsRequestProcesor.java | 33 +++++++++++++++----
.../processors/LoginCredentialProcessor.java | 2 +-
4 files changed, 36 insertions(+), 14 deletions(-)
diff --git a/src/main/java/envoy/server/data/User.java b/src/main/java/envoy/server/data/User.java
index 9865978..a7e9bb5 100644
--- a/src/main/java/envoy/server/data/User.java
+++ b/src/main/java/envoy/server/data/User.java
@@ -8,9 +8,9 @@ import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
+import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
-import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@@ -26,13 +26,14 @@ import javax.persistence.TemporalType;
* Created: 02.01.2020
*
* @author Kai S. K. Engelbart
+ * @author Maximilian Käfer
* @since Envoy Server Standalone v0.1-alpha
*/
@Entity
@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"),
@NamedQuery(query = "SELECT u FROM User u WHERE lower(u.name) LIKE lower(:searchPhrase)", name = "searchUsers") }
)
public class User {
@@ -47,7 +48,7 @@ public class User {
private Date lastSeen;
private envoy.data.User.UserStatus status;
- @OneToMany(targetEntity = User.class, cascade = CascadeType.ALL, orphanRemoval = true)
+ @ManyToMany(targetEntity = User.class, cascade = CascadeType.ALL) // , orphanRemoval = true
private List contacts;
/**
diff --git a/src/main/java/envoy/server/database/PersistenceManager.java b/src/main/java/envoy/server/database/PersistenceManager.java
index 0ec57d6..a5cb160 100644
--- a/src/main/java/envoy/server/database/PersistenceManager.java
+++ b/src/main/java/envoy/server/database/PersistenceManager.java
@@ -172,10 +172,12 @@ public class PersistenceManager {
* @return all messages that the client does not yet have (unread messages)
* @since Envoy Server Standalone v0.1-alpha
*/
+ @SuppressWarnings("unchecked")
public List getUnreadMessages(User user) {
return entityManager.createNamedQuery("getUnreadMessages").setParameter("recipient", user).getResultList();
}
+ @SuppressWarnings("unchecked")
public List searchUsers(String searchPhrase) {
return entityManager.createNamedQuery("searchUsers").setParameter("searchPhrase", searchPhrase + "%").getResultList();
}
@@ -191,13 +193,11 @@ public class PersistenceManager {
/**
* @param user the User whose contacts should be retrieved
- * @return the contacts of this User - currently everyone using Envoy
+ * @return the contacts of this User
* @since Envoy Server Standalone v0.1-alpha
*/
+ @SuppressWarnings("unchecked")
public List getContacts(User user) {
return entityManager.createNamedQuery("getContactsOfUser").setParameter("user", 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/ContactsRequestProcesor.java b/src/main/java/envoy/server/processors/ContactsRequestProcesor.java
index 13bda86..32e9714 100644
--- a/src/main/java/envoy/server/processors/ContactsRequestProcesor.java
+++ b/src/main/java/envoy/server/processors/ContactsRequestProcesor.java
@@ -1,11 +1,14 @@
package envoy.server.processors;
import java.io.IOException;
+import java.util.ArrayList;
+import java.util.List;
import java.util.stream.Collectors;
import envoy.data.Contacts;
import envoy.event.ContactsRequest;
import envoy.server.ObjectProcessor;
+import envoy.server.data.User;
import envoy.server.database.PersistenceManager;
import envoy.server.net.ObjectWriteProxy;
@@ -15,18 +18,36 @@ import envoy.server.net.ObjectWriteProxy;
* Created: 08.02.2020
*
* @author Kai S. K. Engelbart
+ * @author Maximilian Käfer
* @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,
- new Contacts(PersistenceManager.getPersistenceManager()
- .searchUsers(request.get())
- .stream()
- .map(envoy.server.data.User::toCommonUser)
- .collect(Collectors.toList())));
+ // Creating a List containing all searchResults
+ List resultList = PersistenceManager.getPersistenceManager().searchUsers(request.get());
+ // Creating a List containing all contacts of the client this event comes from.
+ List clientContacts = PersistenceManager.getPersistenceManager()
+ .getContacts(PersistenceManager.getPersistenceManager().getUserById(request.getClient().getId()));
+ List returnList = new ArrayList();
+
+ // Checking for already existing users in the contacts of the client an only
+ // adding the ones not included to the returnList.
+ if (clientContacts.size() != 0) {
+ for (int i = 0; i < resultList.size(); i++) {
+ for (int j = 0; j < clientContacts.size(); j++) {
+ if (resultList.get(i).getId() != clientContacts.get(j).getId()) { returnList.add(resultList.get(i)); }
+ }
+ }
+ } else {
+ for (int i = 0; i < resultList.size(); i++) {
+ returnList.add(resultList.get(i));
+ }
+ }
+ // Create new Contacts object from returnList
+ Contacts contacts = new Contacts(returnList.stream().map(envoy.server.data.User::toCommonUser).collect(Collectors.toList()));
+ writeProxy.write(socketId, contacts);
}
@Override
diff --git a/src/main/java/envoy/server/processors/LoginCredentialProcessor.java b/src/main/java/envoy/server/processors/LoginCredentialProcessor.java
index 2bad4e4..1c16070 100644
--- a/src/main/java/envoy/server/processors/LoginCredentialProcessor.java
+++ b/src/main/java/envoy/server/processors/LoginCredentialProcessor.java
@@ -80,8 +80,8 @@ public class LoginCredentialProcessor implements ObjectProcessor