2020-01-03 16:21:35 +01:00
|
|
|
package envoy.server.database;
|
|
|
|
|
2020-01-03 18:17:26 +01:00
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import javax.persistence.EntityManager;
|
|
|
|
import javax.persistence.Persistence;
|
|
|
|
|
|
|
|
import org.hibernate.Session;
|
|
|
|
|
|
|
|
import envoy.server.data.Message;
|
|
|
|
import envoy.server.data.User;
|
2020-01-03 16:21:35 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Project: <strong>envoy-server-standalone</strong><br>
|
|
|
|
* File: <strong>PersistenceManager.java</strong><br>
|
2020-01-03 18:17:26 +01:00
|
|
|
* Created: <strong>1 Jan 2020</strong><br>
|
|
|
|
*
|
2020-01-03 16:21:35 +01:00
|
|
|
* @author Leon Hofmeister
|
|
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
|
|
*/
|
|
|
|
public class PersistenceManager {
|
|
|
|
|
2020-01-11 18:28:48 +01:00
|
|
|
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; }
|
|
|
|
|
2020-01-03 18:17:26 +01:00
|
|
|
private EntityManager entityManager = Persistence.createEntityManagerFactory("envoy").createEntityManager();
|
|
|
|
|
|
|
|
/**
|
2020-01-04 15:39:43 +01:00
|
|
|
* Adds a {@link User} to the database.
|
2020-01-03 18:17:26 +01:00
|
|
|
*
|
|
|
|
* @param User the {@link User} to add to the database
|
|
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
|
|
*/
|
2020-01-21 17:51:50 +01:00
|
|
|
public void addUser(User User) {
|
|
|
|
entityManager.getTransaction().begin();
|
|
|
|
entityManager.persist(User);
|
|
|
|
entityManager.flush();
|
|
|
|
entityManager.getTransaction().commit();
|
|
|
|
}
|
2020-01-03 18:17:26 +01:00
|
|
|
|
|
|
|
/**
|
2020-01-04 15:39:43 +01:00
|
|
|
* Adds a {@link Message} to the database.
|
2020-01-03 18:17:26 +01:00
|
|
|
*
|
|
|
|
* @param message the {@link Message} to add to the database
|
|
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
|
|
*/
|
2020-01-04 15:39:43 +01:00
|
|
|
public void addMessage(Message message) { entityManager.persist(message); }
|
2020-01-03 18:17:26 +01:00
|
|
|
|
|
|
|
/**
|
2020-01-04 15:39:43 +01:00
|
|
|
* Updates a {@link User} in the database
|
2020-01-03 18:17:26 +01:00
|
|
|
*
|
2020-01-04 15:39:43 +01:00
|
|
|
* @param user the {@link User} to add to the database
|
2020-01-03 18:17:26 +01:00
|
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
|
|
*/
|
2020-01-04 15:39:43 +01:00
|
|
|
public void updateUser(User user) { entityManager.unwrap(Session.class).merge(user); }
|
2020-01-03 18:17:26 +01:00
|
|
|
|
|
|
|
/**
|
2020-01-04 15:39:43 +01:00
|
|
|
* Updates a {@link Message} in the database.
|
2020-01-03 18:17:26 +01:00
|
|
|
*
|
2020-01-04 15:39:43 +01:00
|
|
|
* @param message the message to update
|
2020-01-03 18:17:26 +01:00
|
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
|
|
*/
|
2020-01-04 15:39:43 +01:00
|
|
|
public void updateMessage(Message message) { entityManager.unwrap(Session.class).merge(message); }
|
2020-01-03 18:17:26 +01:00
|
|
|
|
|
|
|
/**
|
2020-01-11 18:28:48 +01:00
|
|
|
* Searches for a {@link User} with a specific id.
|
2020-01-03 18:17:26 +01:00
|
|
|
*
|
|
|
|
* @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 (User) entityManager.createNamedQuery("getUserById").setParameter("id", id).getSingleResult(); }
|
|
|
|
|
2020-01-18 23:50:41 +01:00
|
|
|
public User getUserByName(String name) {
|
|
|
|
return (User) entityManager.createNamedQuery("getUserByName").setParameter("name", name).getSingleResult();
|
|
|
|
}
|
2020-01-11 18:28:48 +01:00
|
|
|
/**
|
|
|
|
* 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 (Message) entityManager.createNamedQuery("getMessageById").setParameter("id", id).getSingleResult();
|
|
|
|
}
|
|
|
|
|
2020-01-03 18:17:26 +01:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
@SuppressWarnings("unchecked")
|
2020-01-04 15:50:05 +01:00
|
|
|
public List<Message> getUnreadMessages(User user) {
|
2020-01-03 18:17:26 +01:00
|
|
|
return entityManager.createNamedQuery("getUnreadMessages").setParameter("recipient", user).getResultList();
|
|
|
|
}
|
2020-01-18 13:28:54 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*/
|
|
|
|
@SuppressWarnings("unchecked")
|
|
|
|
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();
|
2020-01-03 18:17:26 +01:00
|
|
|
}
|