From 1c8b63901447904d4b67ad00cf812d5c0eeb080c Mon Sep 17 00:00:00 2001 From: CyB3RC0nN0R Date: Sat, 8 Feb 2020 15:03:03 +0100 Subject: [PATCH] Added ContactOperationProcessor for the ADD operation --- .../server/database/PersistenceManager.java | 9 +++++ .../processors/ContactOperationProcessor.java | 33 +++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 src/main/java/envoy/server/processors/ContactOperationProcessor.java diff --git a/src/main/java/envoy/server/database/PersistenceManager.java b/src/main/java/envoy/server/database/PersistenceManager.java index 91c7b19..9815ebe 100644 --- a/src/main/java/envoy/server/database/PersistenceManager.java +++ b/src/main/java/envoy/server/database/PersistenceManager.java @@ -180,6 +180,15 @@ public class PersistenceManager { return entityManager.createNamedQuery("searchUsers").setParameter("searchPhrase", searchPhrase + "%").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); + } + /** * @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/ContactOperationProcessor.java b/src/main/java/envoy/server/processors/ContactOperationProcessor.java new file mode 100644 index 0000000..577a176 --- /dev/null +++ b/src/main/java/envoy/server/processors/ContactOperationProcessor.java @@ -0,0 +1,33 @@ +package envoy.server.processors; + +import java.io.IOException; + +import envoy.event.ContactOperation; +import envoy.event.ContactOperation.Operation; +import envoy.server.ConnectionManager; +import envoy.server.ObjectProcessor; +import envoy.server.database.PersistenceManager; +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 { + + @Override + public void process(ContactOperation 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); + PersistenceManager.getPersistenceManager().addContact(userId, operation.get().getId()); + } + } + + @Override + public Class getInputClass() { return ContactOperation.class; } +}