This repository has been archived on 2021-12-05. You can view files and clone it, but cannot push or open issues or pull requests.
envoy/src/main/java/envoy/server/database/PersistenceManager.java

96 lines
3.0 KiB
Java
Raw Normal View History

2020-01-03 16:21:35 +01:00
package envoy.server.database;
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>
* 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 {
private EntityManager entityManager = Persistence.createEntityManagerFactory("envoy").createEntityManager();
2020-01-03 16:21:35 +01:00
/**
*
2020-01-03 16:21:35 +01:00
* @since Envoy Server Standalone v0.1-alpha
*/
public PersistenceManager() {
/*
* // TODO TESTING
* User user = new User(3, "t");
* User user2 = new User(2, "w");
* Message msg = new MessageBuilder(user.getId(), user2.getId()).build();
* entityManager.getTransaction().begin();
* entityManager.persist(user);
* entityManager.persist(user2);
* entityManager.persist(msg);
* entityManager.getTransaction().commit();
*/ // TODO delete until here
2020-01-03 16:21:35 +01:00
}
/**
* Adds a 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.persist(User); }
/**
* Adds a 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.persist(message); }// TODO these functions or the one below?
/**
* This is a delegate function for {@link EntityManager#persist} in order to add
* an {@link User} / a {@link Message} into the database
*
* @param obj the object to add to the database
* @since Envoy Server Standalone v0.1-alpha
*/
public void addObject(Object obj) { entityManager.persist(obj); }
/**
* Updates a row of database objects, either a Message or a User with new data.
*
* @param obj the object to update (existing User/Message)
* @since Envoy Server Standalone v0.1-alpha
*/
public void updateObject(Object obj) { entityManager.unwrap(Session.class).merge(obj); }
/**
* Searches for a 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 (User) entityManager.createNamedQuery("getUserById").setParameter("id", id).getSingleResult(); }
/**
* 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")
public List<Message> getUnreadMessages(User user) {// TODO may need to be changed to clientId
return entityManager.createNamedQuery("getUnreadMessages").setParameter("recipient", user).getResultList();
}
}