2020-01-03 16:21:35 +01:00
|
|
|
package envoy.server.database;
|
|
|
|
|
2020-02-09 16:37:53 +01:00
|
|
|
import java.util.Date;
|
2020-01-03 18:17:26 +01:00
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import javax.persistence.EntityManager;
|
2020-02-07 09:34:02 +01:00
|
|
|
import javax.persistence.EntityTransaction;
|
2020-01-03 18:17:26 +01:00
|
|
|
import javax.persistence.Persistence;
|
|
|
|
|
2020-02-09 16:37:53 +01:00
|
|
|
import envoy.data.User.UserStatus;
|
2020-01-28 20:46:27 +01:00
|
|
|
import envoy.server.data.ConfigItem;
|
2020-01-03 18:17:26 +01:00
|
|
|
import envoy.server.data.Message;
|
|
|
|
import envoy.server.data.User;
|
2020-02-12 22:01:32 +01:00
|
|
|
import envoy.server.net.ConnectionManager;
|
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-02-07 09:34:02 +01:00
|
|
|
private final EntityManager entityManager = Persistence.createEntityManagerFactory("envoy").createEntityManager();
|
|
|
|
private final EntityTransaction transaction = entityManager.getTransaction();
|
|
|
|
|
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
|
|
|
|
*/
|
2020-02-07 09:34:02 +01:00
|
|
|
private PersistenceManager() {
|
|
|
|
transaction.begin();
|
2020-02-09 16:37:53 +01:00
|
|
|
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
|
|
|
|
ConnectionManager.getInstance()
|
|
|
|
.getOnlineUsers()
|
|
|
|
.stream()
|
|
|
|
.map(this::getUserById)
|
|
|
|
.forEach(user -> { user.setStatus(UserStatus.OFFLINE); user.setLastSeen(new Date()); updateUser(user); });
|
|
|
|
transaction.commit();
|
|
|
|
}));
|
2020-02-07 09:34:02 +01:00
|
|
|
}
|
2020-01-11 18:28:48 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return the {@link PersistenceManager} singleton
|
|
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
|
|
*/
|
2020-02-12 07:10:33 +01:00
|
|
|
public static PersistenceManager getInstance() { return persistenceManager; }
|
2020-01-11 18:28:48 +01:00
|
|
|
|
2020-01-03 18:17:26 +01:00
|
|
|
/**
|
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.persist(User);
|
2020-02-07 09:34:02 +01:00
|
|
|
entityManager.flush();
|
2020-01-21 17:51:50 +01:00
|
|
|
}
|
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-28 19:25:30 +01:00
|
|
|
public void addMessage(Message message) {
|
|
|
|
entityManager.persist(message);
|
2020-02-07 09:34:02 +01:00
|
|
|
entityManager.flush();
|
2020-01-28 19:25:30 +01:00
|
|
|
}
|
2020-01-03 18:17:26 +01:00
|
|
|
|
2020-01-28 20:46:27 +01:00
|
|
|
/**
|
|
|
|
* 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.persist(configItem);
|
2020-02-07 09:34:02 +01:00
|
|
|
entityManager.flush();
|
2020-01-28 20:46:27 +01:00
|
|
|
}
|
|
|
|
|
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-28 20:46:27 +01:00
|
|
|
public void updateUser(User user) {
|
|
|
|
entityManager.merge(user);
|
2020-02-07 09:34:02 +01:00
|
|
|
entityManager.flush();
|
2020-01-28 20:46:27 +01:00
|
|
|
}
|
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-28 20:46:27 +01:00
|
|
|
public void updateMessage(Message message) {
|
|
|
|
entityManager.merge(message);
|
2020-02-07 09:34:02 +01:00
|
|
|
entityManager.flush();
|
2020-01-28 20:46:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.merge(configItem);
|
2020-02-07 09:34:02 +01:00
|
|
|
entityManager.flush();
|
2020-01-28 20:46:27 +01:00
|
|
|
}
|
2020-01-03 18:17:26 +01:00
|
|
|
|
2020-02-02 12:32:10 +01:00
|
|
|
/**
|
|
|
|
* 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.remove(user);
|
2020-02-07 09:34:02 +01:00
|
|
|
entityManager.flush();
|
2020-02-02 12:32:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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.remove(message);
|
2020-02-07 09:34:02 +01:00
|
|
|
entityManager.flush();
|
2020-02-02 12:32:10 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
*
|
2020-01-28 19:25:30 +01:00
|
|
|
* @param id the id to search for
|
2020-01-03 18:17:26 +01:00
|
|
|
* @return the user with the specified id
|
|
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
|
|
*/
|
2020-01-28 19:25:30 +01:00
|
|
|
public User getUserById(long id) { return entityManager.find(User.class, id); }
|
2020-01-03 18:17:26 +01:00
|
|
|
|
2020-01-28 19:25:30 +01:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
*/
|
2020-01-18 23:50:41 +01:00
|
|
|
public User getUserByName(String name) {
|
|
|
|
return (User) entityManager.createNamedQuery("getUserByName").setParameter("name", name).getSingleResult();
|
|
|
|
}
|
2020-01-28 19:25:30 +01:00
|
|
|
|
2020-01-11 18:28:48 +01:00
|
|
|
/**
|
|
|
|
* Searches for a {@link Message} with a specific id.
|
|
|
|
*
|
2020-01-28 19:25:30 +01:00
|
|
|
* @param id the id to search for
|
2020-01-11 18:28:48 +01:00
|
|
|
* @return the message with the specified id
|
|
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
|
|
*/
|
2020-01-28 19:25:30 +01:00
|
|
|
public Message getMessageById(long id) { return entityManager.find(Message.class, id); }
|
2020-01-11 18:28:48 +01:00
|
|
|
|
2020-01-30 12:52:27 +01:00
|
|
|
/**
|
|
|
|
* @param key the name of this {@link ConfigItem}
|
|
|
|
* @return the {@link ConfigItem} with the given name
|
|
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
|
|
*/
|
2020-01-28 20:46:27 +01:00
|
|
|
public ConfigItem getConfigItemById(String key) { return entityManager.find(ConfigItem.class, key); }
|
|
|
|
|
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
|
|
|
|
*/
|
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
|
|
|
|
2020-02-10 22:29:34 +01:00
|
|
|
/**
|
|
|
|
* Searches for users matching a search phrase. Contacts of the attached user
|
|
|
|
* and the attached user is ignored.
|
|
|
|
*
|
|
|
|
* @param searchPhrase the search phrase
|
|
|
|
* @param userId the ID of the user in whose context the search is
|
|
|
|
* performed
|
|
|
|
* @return a list of all users who matched the criteria
|
|
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
|
|
*/
|
|
|
|
public List<User> searchUsers(String searchPhrase, long userId) {
|
|
|
|
return entityManager.createNamedQuery("searchUsers")
|
|
|
|
.setParameter("searchPhrase", searchPhrase + "%")
|
|
|
|
.setParameter("context", getUserById(userId))
|
|
|
|
.getResultList();
|
2020-02-08 13:53:58 +01:00
|
|
|
}
|
|
|
|
|
2020-02-10 22:29:34 +01:00
|
|
|
/**
|
|
|
|
* Adds a user to the contact list of another user and vice versa.
|
|
|
|
*
|
|
|
|
* @param userId1 the ID of the first user
|
|
|
|
* @param userId2 the ID of the second user
|
|
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
|
|
*/
|
|
|
|
public void addContact(long userId1, long userId2) {
|
|
|
|
User u1 = getUserById(userId1);
|
|
|
|
User u2 = getUserById(userId2);
|
|
|
|
u1.getContacts().add(u2);
|
|
|
|
u2.getContacts().add(u1);
|
|
|
|
updateUser(u1);
|
|
|
|
updateUser(u2);
|
2020-02-08 15:03:03 +01:00
|
|
|
}
|
|
|
|
|
2020-01-18 13:28:54 +01:00
|
|
|
/**
|
|
|
|
* @param user the User whose contacts should be retrieved
|
2020-02-10 20:02:05 +01:00
|
|
|
* @return the contacts of this User
|
2020-01-18 13:28:54 +01:00
|
|
|
* @since Envoy Server Standalone v0.1-alpha
|
|
|
|
*/
|
2020-02-09 22:16:33 +01:00
|
|
|
public List<User> getContacts(User user) {
|
|
|
|
return entityManager.createNamedQuery("getContactsOfUser").setParameter("user", user).getResultList();
|
|
|
|
}
|
2020-01-03 18:17:26 +01:00
|
|
|
}
|