239 lines
7.2 KiB
Java
239 lines
7.2 KiB
Java
package envoy.server.database;
|
|
|
|
import java.util.List;
|
|
|
|
import javax.persistence.EntityManager;
|
|
import javax.persistence.Persistence;
|
|
|
|
import envoy.data.User.UserStatus;
|
|
import envoy.server.data.ConfigItem;
|
|
import envoy.server.data.Message;
|
|
import envoy.server.data.User;
|
|
|
|
/**
|
|
* Project: <strong>envoy-server-standalone</strong><br>
|
|
* File: <strong>PersistenceManager.java</strong><br>
|
|
* Created: <strong>1 Jan 2020</strong><br>
|
|
*
|
|
* @author Leon Hofmeister
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
*/
|
|
public class PersistenceManager {
|
|
|
|
private static final PersistenceManager persistenceManager = new PersistenceManager();
|
|
|
|
/**
|
|
* Creates the singleton instance of the @link{PersistenceManager}.
|
|
*
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
*/
|
|
private PersistenceManager() {}
|
|
|
|
/**
|
|
* @return the {@link PersistenceManager} singleton
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
*/
|
|
public static PersistenceManager getPersistenceManager() { return persistenceManager; }
|
|
|
|
private EntityManager entityManager = Persistence.createEntityManagerFactory("envoy").createEntityManager();
|
|
|
|
/**
|
|
* Adds a {@link User} to the database.
|
|
*
|
|
* @param User the {@link User} to add to the database
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
*/
|
|
public void addUser(User User) {
|
|
entityManager.getTransaction().begin();
|
|
entityManager.persist(User);
|
|
entityManager.getTransaction().commit();
|
|
}
|
|
|
|
/**
|
|
* Adds a {@link Message} to the database.
|
|
*
|
|
* @param message the {@link Message} to add to the database
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
*/
|
|
public void addMessage(Message message) {
|
|
entityManager.getTransaction().begin();
|
|
entityManager.persist(message);
|
|
entityManager.getTransaction().commit();
|
|
}
|
|
|
|
/**
|
|
* Adds a {@link ConfigItem} to the database.
|
|
*
|
|
* @param configItem the {@link ConfigItem} to add to the database
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
*/
|
|
public void addConfigItem(ConfigItem configItem) {
|
|
entityManager.getTransaction().begin();
|
|
entityManager.persist(configItem);
|
|
entityManager.getTransaction().commit();
|
|
}
|
|
|
|
/**
|
|
* Updates a {@link User} in the database
|
|
*
|
|
* @param user the {@link User} to add to the database
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
*/
|
|
public void updateUser(User user) {
|
|
entityManager.getTransaction().begin();
|
|
entityManager.merge(user);
|
|
entityManager.getTransaction().commit();
|
|
}
|
|
|
|
/**
|
|
* Updates a {@link Message} in the database.
|
|
*
|
|
* @param message the message to update
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
*/
|
|
public void updateMessage(Message message) {
|
|
entityManager.getTransaction().begin();
|
|
entityManager.merge(message);
|
|
entityManager.getTransaction().commit();
|
|
}
|
|
|
|
/**
|
|
* Updates a {@link ConfigItem} in the database.
|
|
*
|
|
* @param configItem the configItem to update
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
*/
|
|
public void updateConfigItem(ConfigItem configItem) {
|
|
entityManager.getTransaction().begin();
|
|
entityManager.merge(configItem);
|
|
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.
|
|
*
|
|
* @param id the id to search for
|
|
* @return the user with the specified id
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
*/
|
|
public User getUserById(long id) { return entityManager.find(User.class, id); }
|
|
|
|
/**
|
|
* Searched for a {@link User} with a specific name.
|
|
*
|
|
* @param name the name of the user
|
|
* @return the user with the specified name
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
*/
|
|
public User getUserByName(String name) {
|
|
return (User) entityManager.createNamedQuery("getUserByName").setParameter("name", name).getSingleResult();
|
|
}
|
|
|
|
/**
|
|
* Searches for a {@link Message} with a specific id.
|
|
*
|
|
* @param id the id to search for
|
|
* @return the message with the specified id
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
*/
|
|
public Message getMessageById(long id) { return entityManager.find(Message.class, id); }
|
|
|
|
/**
|
|
* @param key the name of this {@link ConfigItem}
|
|
* @return the {@link ConfigItem} with the given name
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
*/
|
|
public ConfigItem getConfigItemById(String key) { return entityManager.find(ConfigItem.class, key); }
|
|
|
|
/**
|
|
* Returns all messages received while being offline.
|
|
*
|
|
* @param user - the user who wants to receive his unread messages
|
|
* @return all messages that the client does not yet have (unread messages)
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
*/
|
|
public List<Message> getUnreadMessages(User user) {
|
|
return entityManager.createNamedQuery("getUnreadMessages").setParameter("recipient", user).getResultList();
|
|
}
|
|
|
|
/**
|
|
* @param user the User whose contacts should be retrieved
|
|
* @return the contacts of this User - currently everyone using Envoy
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
*/
|
|
public List<User> getContacts(User user) { return entityManager.createQuery("FROM User").getResultList(); }
|
|
// TODO current solution gets all users, not just contacts. Should be changed to
|
|
// entityManager.createNamedQuery("getContactsOfUser").setParameter("user",
|
|
// user).getResultList();
|
|
|
|
/**
|
|
* Updates the {@link UserStatus} in the database.
|
|
*
|
|
* @param user the {@link User} who changes his status
|
|
* @param status the new status of that user
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
*/
|
|
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);
|
|
}
|
|
} |