Added PersistenceManager and Namedqueries
In theory, this code is all we need for database integration
This commit is contained in:
parent
8d4996a602
commit
beb98011ca
@ -4,6 +4,9 @@ import java.util.Date;
|
|||||||
|
|
||||||
import javax.persistence.Entity;
|
import javax.persistence.Entity;
|
||||||
import javax.persistence.Id;
|
import javax.persistence.Id;
|
||||||
|
import javax.persistence.NamedQueries;
|
||||||
|
import javax.persistence.NamedQuery;
|
||||||
|
import javax.persistence.Table;
|
||||||
import javax.persistence.Temporal;
|
import javax.persistence.Temporal;
|
||||||
import javax.persistence.TemporalType;
|
import javax.persistence.TemporalType;
|
||||||
|
|
||||||
@ -23,6 +26,13 @@ import envoy.data.MessageBuilder;
|
|||||||
* @since Envoy Server Standalone v0.1-alpha
|
* @since Envoy Server Standalone v0.1-alpha
|
||||||
*/
|
*/
|
||||||
@Entity
|
@Entity
|
||||||
|
@Table(name = "messages")
|
||||||
|
@NamedQueries(
|
||||||
|
{ @NamedQuery(query = "SELECT m FROM Message m WHERE m.recipient =:recipient AND m.state = 1", name = "getUnreadMessages"), @NamedQuery(
|
||||||
|
query = "SELECT m FROM Message m WHERE m.sender =:sender AND m.state = :state",
|
||||||
|
name = "find read messages"//TODO do we need this namedQuery?
|
||||||
|
), @NamedQuery(query = "SELECT m FROM Message m WHERE m.id = :messageId", name = "get message") }//TODO do we need this namedQuery?
|
||||||
|
)
|
||||||
public class Message {
|
public class Message {
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
|
@ -7,6 +7,8 @@ import javax.persistence.Entity;
|
|||||||
import javax.persistence.GeneratedValue;
|
import javax.persistence.GeneratedValue;
|
||||||
import javax.persistence.GenerationType;
|
import javax.persistence.GenerationType;
|
||||||
import javax.persistence.Id;
|
import javax.persistence.Id;
|
||||||
|
import javax.persistence.NamedQuery;
|
||||||
|
import javax.persistence.Table;
|
||||||
import javax.persistence.Temporal;
|
import javax.persistence.Temporal;
|
||||||
import javax.persistence.TemporalType;
|
import javax.persistence.TemporalType;
|
||||||
|
|
||||||
@ -24,6 +26,8 @@ import javax.persistence.TemporalType;
|
|||||||
* @since Envoy Server Standalone v0.1-alpha
|
* @since Envoy Server Standalone v0.1-alpha
|
||||||
*/
|
*/
|
||||||
@Entity
|
@Entity
|
||||||
|
@Table(name = "users")
|
||||||
|
@NamedQuery(query = "SELECT u FROM DBUser u WHERE u.id = :id", name = "getUserById")
|
||||||
public class User {
|
public class User {
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
|
@ -1,21 +1,96 @@
|
|||||||
package envoy.server.database;
|
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;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Project: <strong>envoy-server-standalone</strong><br>
|
* Project: <strong>envoy-server-standalone</strong><br>
|
||||||
* File: <strong>PersistenceManager.java</strong><br>
|
* File: <strong>PersistenceManager.java</strong><br>
|
||||||
* Created: <strong>3 Jan 2020</strong><br>
|
* Created: <strong>1 Jan 2020</strong><br>
|
||||||
*
|
*
|
||||||
* @author Leon Hofmeister
|
* @author Leon Hofmeister
|
||||||
* @since Envoy Server Standalone v0.1-alpha
|
* @since Envoy Server Standalone v0.1-alpha
|
||||||
*/
|
*/
|
||||||
public class PersistenceManager {
|
public class PersistenceManager {
|
||||||
|
|
||||||
|
private EntityManager entityManager = Persistence.createEntityManagerFactory("envoy").createEntityManager();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @since Envoy Server Standalone v0.1-alpha
|
* @since Envoy Server Standalone v0.1-alpha
|
||||||
*/
|
*/
|
||||||
public PersistenceManager() { // TODO Auto-generated constructor stub
|
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
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
/**
|
||||||
|
* 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();
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user