Improved PersistenceManager with new methods to handle incoming entities

This commit is contained in:
delvh 2020-02-02 12:32:10 +01:00
parent 5b28f2f25b
commit 5b482c6815
2 changed files with 61 additions and 2 deletions

View File

@ -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
* <b>not</b> matter.<br>
* Currently supported values are:<br>
* -"add"/"persist" to add an object to the database<br>
* -"update"/"merge" to update this object in the database<br>
* -"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);
}

View File

@ -52,7 +52,7 @@ public class UserStatusChangeProcessor implements ObjectProcessor<UserStatusChan
PersistenceManager perMan = PersistenceManager.getPersistenceManager();
envoy.server.data.User user = perMan.getUserById(evt.getId());
// handling for newly logged in clients
perMan.updateUserStatusInDatabase(user, evt.get());
perMan.updateUserStatus(user, evt.get());
// handling for contacts that are already online
notifyContacts(evt, user, writeProxy);