From 34e1b610acd39829e1aa68b09bef0aa1179c84e3 Mon Sep 17 00:00:00 2001 From: delvh Date: Sun, 2 Feb 2020 12:32:10 +0100 Subject: [PATCH] Improved PersistenceManager with new methods to handle incoming entities --- .../server/database/PersistenceManager.java | 61 ++++++++++++++++++- .../processors/UserStatusChangeProcessor.java | 2 +- 2 files changed, 61 insertions(+), 2 deletions(-) diff --git a/src/main/java/envoy/server/database/PersistenceManager.java b/src/main/java/envoy/server/database/PersistenceManager.java index 614a641..e7e2a0e 100644 --- a/src/main/java/envoy/server/database/PersistenceManager.java +++ b/src/main/java/envoy/server/database/PersistenceManager.java @@ -109,6 +109,61 @@ public class PersistenceManager { entityManager.getTransaction().commit(); } + /** + * Deletes a {@link User} in the database. + * + * @param user the {@link User} to delete + * @since Envoy Server Standalone v0.1-alpha + */ + public void deleteUser(User user) { + entityManager.getTransaction().begin(); + entityManager.remove(user); + entityManager.getTransaction().commit(); + } + + /** + * Deletes a {@link Message} in the database. + * + * @param message the {@link Message} to delete + * @since Envoy Server Standalone v0.1-alpha + */ + public void deleteMessage(Message message) { + entityManager.getTransaction().begin(); + entityManager.remove(message); + entityManager.getTransaction().commit(); + } + + /** + * @param action the action that should be applied for this entity. Case does + * not matter.
+ * Currently supported values are:
+ * -"add"/"persist" to add an object to the database
+ * -"update"/"merge" to update this object in the database
+ * -"delete"/"remove" to delete this object in the database + * @param entity the object to apply the action to + * @since Envoy Server Standalone v0.1-alpha + */ + public void applyAction(String action, Object entity) { + entityManager.getTransaction().begin(); + switch (action.trim().toLowerCase()) { + case "add": + case "persist": + entityManager.persist(entity); + break; + case "update": + case "merge": + entityManager.merge(entity); + break; + case "delete": + case "remove": + entityManager.remove(entity); + break; + default: + throw new IllegalStateException("Unknown action: " + action + " was selected to be executed in the database"); + } + entityManager.getTransaction().commit(); + } + /** * Searches for a {@link User} with a specific id. * @@ -173,7 +228,11 @@ public class PersistenceManager { * @param status the new status of that user * @since Envoy Server Standalone v0.1-alpha */ - public void updateUserStatusInDatabase(User user, UserStatus status) { + public void updateUserStatus(User user, UserStatus status) { + if (user.getStatus().equals(status)) { + System.out.println("Received an UserStatus for User" + user.getId() + "to update that this user already has"); + return; + } user.setStatus(status); persistenceManager.updateUser(user); } diff --git a/src/main/java/envoy/server/processors/UserStatusChangeProcessor.java b/src/main/java/envoy/server/processors/UserStatusChangeProcessor.java index 35de77e..8a43556 100644 --- a/src/main/java/envoy/server/processors/UserStatusChangeProcessor.java +++ b/src/main/java/envoy/server/processors/UserStatusChangeProcessor.java @@ -52,7 +52,7 @@ public class UserStatusChangeProcessor implements ObjectProcessor