diff --git a/pom.xml b/pom.xml
index da0d59a..db5b8da 100644
--- a/pom.xml
+++ b/pom.xml
@@ -28,7 +28,7 @@
com.github.informatik-ag-ngl
envoy-common
- f~contacts-SNAPSHOT
+ develop-SNAPSHOT
com.github.informatik-ag-ngl
diff --git a/src/main/java/envoy/server/Startup.java b/src/main/java/envoy/server/Startup.java
index 8039f24..e23bf69 100644
--- a/src/main/java/envoy/server/Startup.java
+++ b/src/main/java/envoy/server/Startup.java
@@ -10,13 +10,7 @@ import envoy.server.data.ConfigItem;
import envoy.server.database.PersistenceManager;
import envoy.server.net.ObjectMessageProcessor;
import envoy.server.net.ObjectMessageReader;
-import envoy.server.processors.ContactOperationProcessor;
-import envoy.server.processors.ContactsRequestProcesor;
-import envoy.server.processors.IdGeneratorRequestProcessor;
-import envoy.server.processors.LoginCredentialProcessor;
-import envoy.server.processors.MessageProcessor;
-import envoy.server.processors.MessageStatusChangeProcessor;
-import envoy.server.processors.UserStatusChangeProcessor;
+import envoy.server.processors.*;
/**
* Starts the server.
@@ -44,7 +38,7 @@ public class Startup {
processors.add(new MessageStatusChangeProcessor());
processors.add(new UserStatusChangeProcessor());
processors.add(new IdGeneratorRequestProcessor());
- processors.add(new ContactsRequestProcesor());
+ processors.add(new ContactsRequestEventProcessor());
processors.add(new ContactOperationProcessor());
Server server = new Server(8080, () -> new ObjectMessageReader(), new ObjectMessageProcessor(processors));
diff --git a/src/main/java/envoy/server/data/User.java b/src/main/java/envoy/server/data/User.java
index a7e9bb5..543fec7 100644
--- a/src/main/java/envoy/server/data/User.java
+++ b/src/main/java/envoy/server/data/User.java
@@ -3,17 +3,7 @@ package envoy.server.data;
import java.util.Date;
import java.util.List;
-import javax.persistence.CascadeType;
-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.Table;
-import javax.persistence.Temporal;
-import javax.persistence.TemporalType;
+import javax.persistence.*;
/**
* This class serves as a way to let Hibernate communicate with the server
@@ -32,9 +22,13 @@ import javax.persistence.TemporalType;
@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"),
- @NamedQuery(query = "SELECT u FROM User u WHERE lower(u.name) LIKE lower(:searchPhrase)", name = "searchUsers") }
+ { @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"
+ ), @NamedQuery(
+ query = "SELECT u FROM User u WHERE (lower(u.name) LIKE lower(:searchPhrase) AND u <> :context AND NOT :context in elements(u.contacts))",
+ name = "searchUsers"
+ ) }
)
public class User {
@@ -48,7 +42,7 @@ public class User {
private Date lastSeen;
private envoy.data.User.UserStatus status;
- @ManyToMany(targetEntity = User.class, cascade = CascadeType.ALL) // , orphanRemoval = true
+ @ManyToMany(targetEntity = User.class, cascade = CascadeType.ALL)
private List contacts;
/**
diff --git a/src/main/java/envoy/server/database/PersistenceManager.java b/src/main/java/envoy/server/database/PersistenceManager.java
index a5cb160..8943f52 100644
--- a/src/main/java/envoy/server/database/PersistenceManager.java
+++ b/src/main/java/envoy/server/database/PersistenceManager.java
@@ -172,23 +172,41 @@ 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();
+ /**
+ * Searches for users matching a search phrase. Contacts of the attached user
+ * and the attached user is ignored.
+ *
+ * @param searchPhrase the search phrase
+ * @param userId the ID of the user in whose context the search is
+ * performed
+ * @return a list of all users who matched the criteria
+ * @since Envoy Server Standalone v0.1-alpha
+ */
+ public List searchUsers(String searchPhrase, long userId) {
+ return entityManager.createNamedQuery("searchUsers")
+ .setParameter("searchPhrase", searchPhrase + "%")
+ .setParameter("context", getUserById(userId))
+ .getResultList();
}
- public void addContact(long userId, long contactId) {
- User c1 = getUserById(userId);
- User c2 = getUserById(contactId);
- c1.getContacts().add(c2);
- c2.getContacts().add(c1);
- updateUser(c1);
- updateUser(c2);
+ /**
+ * Adds a user to the contact list of another user and vice versa.
+ *
+ * @param userId1 the ID of the first user
+ * @param userId2 the ID of the second user
+ * @since Envoy Server Standalone v0.1-alpha
+ */
+ public void addContact(long userId1, long userId2) {
+ User u1 = getUserById(userId1);
+ User u2 = getUserById(userId2);
+ u1.getContacts().add(u2);
+ u2.getContacts().add(u1);
+ updateUser(u1);
+ updateUser(u2);
}
/**
@@ -196,7 +214,6 @@ public class PersistenceManager {
* @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();
}
diff --git a/src/main/java/envoy/server/processors/ContactOperationProcessor.java b/src/main/java/envoy/server/processors/ContactOperationProcessor.java
index 577a176..475f8a2 100644
--- a/src/main/java/envoy/server/processors/ContactOperationProcessor.java
+++ b/src/main/java/envoy/server/processors/ContactOperationProcessor.java
@@ -2,8 +2,8 @@ package envoy.server.processors;
import java.io.IOException;
-import envoy.event.ContactOperation;
-import envoy.event.ContactOperation.Operation;
+import envoy.event.ContactOperationEvent;
+import envoy.event.ContactOperationEvent.Operation;
import envoy.server.ConnectionManager;
import envoy.server.ObjectProcessor;
import envoy.server.database.PersistenceManager;
@@ -13,21 +13,21 @@ import envoy.server.net.ObjectWriteProxy;
* Project: envoy-server-standalone
* File: ContactOperationProcessor.java
* Created: 08.02.2020
- *
+ *
* @author Kai S. K. Engelbart
* @since Envoy Server Standalone v0.1-alpha
*/
-public class ContactOperationProcessor implements ObjectProcessor {
+public class ContactOperationProcessor implements ObjectProcessor {
@Override
- public void process(ContactOperation operation, long socketId, ObjectWriteProxy writeProxy) throws IOException {
+ public void process(ContactOperationEvent operation, long socketId, ObjectWriteProxy writeProxy) throws IOException {
if (operation.getOperationType() == Operation.ADD) {
long userId = ConnectionManager.getInstance().getUserIdBySocketId(socketId);
- System.out.printf("Adding user %s to the contact list of user %d", operation.get(), userId);
+ System.out.printf("Adding user %s to the contact list of user %d.%n", operation.get(), userId);
PersistenceManager.getPersistenceManager().addContact(userId, operation.get().getId());
}
}
@Override
- public Class getInputClass() { return ContactOperation.class; }
+ public Class getInputClass() { return ContactOperationEvent.class; }
}
diff --git a/src/main/java/envoy/server/processors/ContactsRequestEventProcessor.java b/src/main/java/envoy/server/processors/ContactsRequestEventProcessor.java
new file mode 100644
index 0000000..06a2d11
--- /dev/null
+++ b/src/main/java/envoy/server/processors/ContactsRequestEventProcessor.java
@@ -0,0 +1,44 @@
+package envoy.server.processors;
+
+import java.io.IOException;
+import java.util.stream.Collectors;
+
+import envoy.data.Contacts;
+import envoy.event.ContactSearchRequest;
+import envoy.server.ConnectionManager;
+import envoy.server.ObjectProcessor;
+import envoy.server.data.User;
+import envoy.server.database.PersistenceManager;
+import envoy.server.net.ObjectWriteProxy;
+
+/**
+ * Project: envoy-server-standalone
+ * File: ContactsRequestEventProcessor.java
+ * Created: 08.02.2020
+ *
+ * @author Kai S. K. Engelbart
+ * @author Maximilian Käfer
+ * @since Envoy Server Standalone v0.1-alpha
+ */
+public class ContactsRequestEventProcessor implements ObjectProcessor {
+
+ /**
+ * Writes a {@link Contacts} list to the client containing all {@link User}s
+ * matching the search phrase contained inside the request. The client and their
+ * contacts are excluded from the result.
+ *
+ * @since Envoy Server Standalone v0.1-alpha
+ */
+ @Override
+ public void process(ContactSearchRequest request, long socketId, ObjectWriteProxy writeProxy) throws IOException {
+ writeProxy.write(socketId,
+ new Contacts(PersistenceManager.getPersistenceManager()
+ .searchUsers(request.get(), ConnectionManager.getInstance().getUserIdBySocketId(socketId))
+ .stream()
+ .map(User::toCommonUser)
+ .collect(Collectors.toList())));
+ }
+
+ @Override
+ public Class getInputClass() { return ContactSearchRequest.class; }
+}
diff --git a/src/main/java/envoy/server/processors/ContactsRequestProcesor.java b/src/main/java/envoy/server/processors/ContactsRequestProcesor.java
deleted file mode 100644
index 32e9714..0000000
--- a/src/main/java/envoy/server/processors/ContactsRequestProcesor.java
+++ /dev/null
@@ -1,55 +0,0 @@
-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;
-
-/**
- * Project: envoy-server-standalone
- * File: ContactsRequestProcesor.java
- * 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 {
- // 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
- 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 1c16070..c7475e9 100644
--- a/src/main/java/envoy/server/processors/LoginCredentialProcessor.java
+++ b/src/main/java/envoy/server/processors/LoginCredentialProcessor.java
@@ -1,9 +1,7 @@
package envoy.server.processors;
import java.io.IOException;
-import java.util.Arrays;
-import java.util.Date;
-import java.util.List;
+import java.util.*;
import java.util.stream.Collectors;
import envoy.data.Contacts;
@@ -81,7 +79,7 @@ public class LoginCredentialProcessor implements ObjectProcessor());
} else {
user = persistenceManager.getUserByName(credentials.getName());
// TODO: Implement error when user does not exist