Use constants as query names, joined inheritance for contacts
This commit is contained in:
parent
61d2a8d307
commit
c2775af8cf
@ -17,15 +17,16 @@ import javax.persistence.*;
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
|
@Table(name = "contacts")
|
||||||
|
@Inheritance(strategy = InheritanceType.JOINED)
|
||||||
public abstract class Contact {
|
public abstract class Contact {
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
@GeneratedValue(strategy = GenerationType.TABLE)
|
@GeneratedValue
|
||||||
protected long id;
|
protected long id;
|
||||||
protected String name;
|
protected String name;
|
||||||
|
|
||||||
@ManyToMany(targetEntity = Contact.class, cascade = CascadeType.ALL, fetch = FetchType.EAGER)
|
@ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER)
|
||||||
protected Set<Contact> contacts;
|
protected Set<Contact> contacts;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -7,10 +7,8 @@ import javax.persistence.NamedQuery;
|
|||||||
import javax.persistence.Table;
|
import javax.persistence.Table;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class serves as a way to let Hibernate communicate with the server
|
* Represents a group inside the database. Referred to as "server group" as
|
||||||
* without bringing the dependency of JPA/Hibernate into the client.<br>
|
* opposed to "group" from Envoy Common.<br>
|
||||||
* It will be referenced as "database group" to clarify between the different
|
|
||||||
* group objects.<br>
|
|
||||||
* <br>
|
* <br>
|
||||||
* Project: <strong>envoy-server-standalone</strong><br>
|
* Project: <strong>envoy-server-standalone</strong><br>
|
||||||
* File: <strong>Group.java</strong><br>
|
* File: <strong>Group.java</strong><br>
|
||||||
@ -21,9 +19,19 @@ import javax.persistence.Table;
|
|||||||
*/
|
*/
|
||||||
@Entity
|
@Entity
|
||||||
@Table(name = "groups")
|
@Table(name = "groups")
|
||||||
@NamedQuery(query = "SELECT g FROM Group g WHERE g.name = :name", name = "getGroupByName")
|
@NamedQuery(
|
||||||
|
name = Group.findByName,
|
||||||
|
query = "SELECT g FROM Group g WHERRE g.name = :name"
|
||||||
|
)
|
||||||
public class Group extends Contact {
|
public class Group extends Contact {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Named query retrieving a group by name (parameter {@code :name}.
|
||||||
|
*
|
||||||
|
* @since Envoy Server Standalone v0.1-beta
|
||||||
|
*/
|
||||||
|
public static final String findByName = "Group.findByName";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* {@inheritDoc}
|
* {@inheritDoc}
|
||||||
*/
|
*/
|
||||||
|
@ -3,14 +3,7 @@ package envoy.server.data;
|
|||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import javax.persistence.CascadeType;
|
import javax.persistence.*;
|
||||||
import javax.persistence.ElementCollection;
|
|
||||||
import javax.persistence.Entity;
|
|
||||||
import javax.persistence.Id;
|
|
||||||
import javax.persistence.ManyToOne;
|
|
||||||
import javax.persistence.Table;
|
|
||||||
import javax.persistence.Temporal;
|
|
||||||
import javax.persistence.TemporalType;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Project: <strong>envoy-server-standalone</strong><br>
|
* Project: <strong>envoy-server-standalone</strong><br>
|
||||||
@ -22,9 +15,7 @@ import javax.persistence.TemporalType;
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
@Entity
|
@Entity
|
||||||
@Table(name = "groupMessages")
|
@Table(name = "group_messages")
|
||||||
// @NamedQuery()
|
|
||||||
// TODO Add Queries
|
|
||||||
public class GroupMessage {
|
public class GroupMessage {
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
|
@ -1,15 +1,10 @@
|
|||||||
package envoy.server.data;
|
package envoy.server.data;
|
||||||
|
|
||||||
|
import static javax.persistence.CascadeType.ALL;
|
||||||
|
|
||||||
import java.util.Date;
|
import java.util.Date;
|
||||||
|
|
||||||
import javax.persistence.CascadeType;
|
import javax.persistence.*;
|
||||||
import javax.persistence.Entity;
|
|
||||||
import javax.persistence.Id;
|
|
||||||
import javax.persistence.ManyToOne;
|
|
||||||
import javax.persistence.NamedQuery;
|
|
||||||
import javax.persistence.Table;
|
|
||||||
import javax.persistence.Temporal;
|
|
||||||
import javax.persistence.TemporalType;
|
|
||||||
|
|
||||||
import envoy.data.MessageBuilder;
|
import envoy.data.MessageBuilder;
|
||||||
|
|
||||||
@ -29,20 +24,30 @@ import envoy.data.MessageBuilder;
|
|||||||
@Entity
|
@Entity
|
||||||
@Table(name = "messages")
|
@Table(name = "messages")
|
||||||
@NamedQuery(
|
@NamedQuery(
|
||||||
|
name = Message.getPending,
|
||||||
query = "SELECT m FROM Message m WHERE (m.recipient = :user AND m.status = envoy.data.Message$MessageStatus.SENT) "
|
query = "SELECT m FROM Message m WHERE (m.recipient = :user AND m.status = envoy.data.Message$MessageStatus.SENT) "
|
||||||
+ "OR (m.sender = :user) AND ((m.status = envoy.data.Message$MessageStatus.RECEIVED) AND (m.receivedDate > :lastSeen)"
|
+ "OR (m.sender = :user) AND ((m.status = envoy.data.Message$MessageStatus.RECEIVED) AND (m.receivedDate > :lastSeen)"
|
||||||
+ "OR (m.status = envoy.data.Message$MessageStatus.READ) AND (m.readDate > :lastSeen))",
|
+ "OR (m.status = envoy.data.Message$MessageStatus.READ) AND (m.readDate > :lastSeen))"
|
||||||
name = "getPendingMessages"
|
|
||||||
)
|
)
|
||||||
public class Message {
|
public class Message {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Named query retrieving pending messages for a user (parameter {@code :user})
|
||||||
|
* which was last seen after a specific date (parameter {@code :lastSeen}).
|
||||||
|
*
|
||||||
|
* @since Envoy Server Standalone v0.1-beta
|
||||||
|
*/
|
||||||
|
public static final String getPending = "Message.getPending";
|
||||||
|
|
||||||
@Id
|
@Id
|
||||||
private long id;
|
private long id;
|
||||||
|
|
||||||
@ManyToOne(cascade = CascadeType.PERSIST)
|
@ManyToOne(cascade = ALL)
|
||||||
|
@JoinColumn
|
||||||
private User sender;
|
private User sender;
|
||||||
|
|
||||||
@ManyToOne(cascade = CascadeType.PERSIST)
|
@ManyToOne(cascade = ALL)
|
||||||
|
@JoinColumn
|
||||||
private User recipient;
|
private User recipient;
|
||||||
|
|
||||||
@Temporal(TemporalType.TIMESTAMP)
|
@Temporal(TemporalType.TIMESTAMP)
|
||||||
|
@ -148,7 +148,7 @@ public class PersistenceManager {
|
|||||||
* @since Envoy Server Standalone v0.1-alpha
|
* @since Envoy Server Standalone v0.1-alpha
|
||||||
*/
|
*/
|
||||||
public User getUserByName(String name) {
|
public User getUserByName(String name) {
|
||||||
return (User) entityManager.createNamedQuery("getUserByName").setParameter("name", name).getSingleResult();
|
return (User) entityManager.createNamedQuery(User.findByName).setParameter("name", name).getSingleResult();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -159,18 +159,7 @@ public class PersistenceManager {
|
|||||||
* @since Envoy Server Standalone v0.1-alpha
|
* @since Envoy Server Standalone v0.1-alpha
|
||||||
*/
|
*/
|
||||||
public Group getGroupByName(String name) {
|
public Group getGroupByName(String name) {
|
||||||
return (Group) entityManager.createNamedQuery("getGroupByName").setParameter("name", name).getSingleResult();
|
return (Group) entityManager.createNamedQuery(Group.findByName).setParameter("name", name).getSingleResult();
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Searched for a {@link Contact} with a specific name.
|
|
||||||
*
|
|
||||||
* @param name the name of the contact
|
|
||||||
* @return the contact with the specified name
|
|
||||||
* @since Envoy Server Standalone v0.1-alpha
|
|
||||||
*/
|
|
||||||
public Contact getContactByName(String name) {
|
|
||||||
return (Contact) entityManager.createNamedQuery("getContactByName").setParameter("name", name).getSingleResult();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -198,7 +187,8 @@ public class PersistenceManager {
|
|||||||
* @since Envoy Server Standalone v0.1-alpha
|
* @since Envoy Server Standalone v0.1-alpha
|
||||||
*/
|
*/
|
||||||
public List<Message> getPendingMessages(User user) {
|
public List<Message> getPendingMessages(User user) {
|
||||||
return entityManager.createNamedQuery("getPendingMessages")
|
return entityManager
|
||||||
|
.createNamedQuery(Message.getPending)
|
||||||
.setParameter("user", user)
|
.setParameter("user", user)
|
||||||
.setParameter("lastSeen", user.getLastSeen())
|
.setParameter("lastSeen", user.getLastSeen())
|
||||||
.getResultList();
|
.getResultList();
|
||||||
@ -215,7 +205,8 @@ public class PersistenceManager {
|
|||||||
* @since Envoy Server Standalone v0.1-alpha
|
* @since Envoy Server Standalone v0.1-alpha
|
||||||
*/
|
*/
|
||||||
public List<User> searchUsers(String searchPhrase, long userId) {
|
public List<User> searchUsers(String searchPhrase, long userId) {
|
||||||
return entityManager.createNamedQuery("searchUsers")
|
return entityManager.createNamedQuery(
|
||||||
|
User.searchByName)
|
||||||
.setParameter("searchPhrase", searchPhrase + "%")
|
.setParameter("searchPhrase", searchPhrase + "%")
|
||||||
.setParameter("context", getUserByID(userId))
|
.setParameter("context", getUserByID(userId))
|
||||||
.getResultList();
|
.getResultList();
|
||||||
@ -251,7 +242,7 @@ public class PersistenceManager {
|
|||||||
* @since Envoy Server Standalone v0.1-alpha
|
* @since Envoy Server Standalone v0.1-alpha
|
||||||
*/
|
*/
|
||||||
public List<User> getContacts(User user) {
|
public List<User> getContacts(User user) {
|
||||||
return entityManager.createNamedQuery("getContactsOfUser").setParameter("user", user).getResultList();
|
return entityManager.createNamedQuery(User.findContacts).setParameter("user", user).getResultList();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void persist(Object obj) {
|
private void persist(Object obj) {
|
||||||
|
@ -23,17 +23,45 @@ import envoy.data.User.UserStatus;
|
|||||||
*/
|
*/
|
||||||
@Entity
|
@Entity
|
||||||
@Table(name = "users")
|
@Table(name = "users")
|
||||||
@NamedQueries(
|
@NamedQueries({
|
||||||
{ @NamedQuery(query = "SELECT u FROM User u WHERE u.name = :name", name = "getUserByName"), @NamedQuery(
|
@NamedQuery(
|
||||||
query = "SELECT u.contacts FROM User u WHERE u = :user",
|
name = User.findByName,
|
||||||
name = "getContactsOfUser"
|
query = "SELECT u FROM User u WHERE u.name = :name"
|
||||||
|
),
|
||||||
|
@NamedQuery(
|
||||||
|
name = User.findContacts,
|
||||||
|
query = "SELECT u.contacts FROM User u WHERE u = :user"
|
||||||
), @NamedQuery(
|
), @NamedQuery(
|
||||||
query = "SELECT u FROM User u WHERE (lower(u.name) LIKE lower(:searchPhrase) AND u <> :context AND NOT :context in elements(u.contacts))",
|
name = User.searchByName,
|
||||||
name = "searchUsers"
|
query = "SELECT u FROM User u WHERE (lower(u.name) LIKE lower(:searchPhrase) AND u <> :context AND NOT :context in elements(u.contacts))"
|
||||||
) }
|
)
|
||||||
)
|
})
|
||||||
public class User extends Contact {
|
public class User extends Contact {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Named query retrieving a user by name (parameter {@code :name}).
|
||||||
|
*
|
||||||
|
* @since Envoy Server Standalone v0.1-beta
|
||||||
|
*/
|
||||||
|
public static final String findByName = "User.findByName";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Named query retrieving the contacts of a given user (parameter
|
||||||
|
* {@code :user}).
|
||||||
|
*
|
||||||
|
* @since Envoy Server Standalone v0.1-beta
|
||||||
|
*/
|
||||||
|
public static final String findContacts = "User.findContacts";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Named query searching for users with a name like a search phrase (parameter
|
||||||
|
* {@code :searchPhrase}) that are not in the contact list of a given user
|
||||||
|
* (parameter {@code :context}).
|
||||||
|
*
|
||||||
|
* @since Envoy Server Standalone v0.1-beta
|
||||||
|
*/
|
||||||
|
public static final String searchByName = "User.searchByName";
|
||||||
|
|
||||||
private byte[] passwordHash;
|
private byte[] passwordHash;
|
||||||
|
|
||||||
@Temporal(TemporalType.TIMESTAMP)
|
@Temporal(TemporalType.TIMESTAMP)
|
||||||
|
@ -3,25 +3,25 @@
|
|||||||
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
|
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence
|
||||||
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
|
http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd"
|
||||||
version="2.1">
|
version="2.1">
|
||||||
|
|
||||||
<persistence-unit name="envoy"
|
<persistence-unit name="envoy"
|
||||||
transaction-type="RESOURCE_LOCAL">
|
transaction-type="RESOURCE_LOCAL">
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
<property name="javax.persistence.jdbc.driver"
|
|
||||||
value="org.postgresql.Driver" /> <!-- DB Driver -->
|
|
||||||
<property name="javax.persistence.jdbc.url"
|
|
||||||
value="jdbc:postgresql://localhost/envoy" /> <!-- BD Mane -->
|
|
||||||
<property name="javax.persistence.jdbc.user" value="envoy" /> <!-- DB User -->
|
|
||||||
<property name="javax.persistence.jdbc.password"
|
|
||||||
value="envoy" /> <!-- DB Password -->
|
|
||||||
|
|
||||||
|
<!-- JDBC -->
|
||||||
|
<property name="javax.persistence.jdbc.driver"
|
||||||
|
value="org.postgresql.Driver" />
|
||||||
|
<property name="javax.persistence.jdbc.url"
|
||||||
|
value="jdbc:postgresql://localhost/envoy" />
|
||||||
|
<property name="javax.persistence.jdbc.user" value="envoy" />
|
||||||
|
<property name="javax.persistence.jdbc.password"
|
||||||
|
value="envoy" />
|
||||||
|
|
||||||
|
|
||||||
|
<!-- Hibernate -->
|
||||||
<property name="hibernate.dialect"
|
<property name="hibernate.dialect"
|
||||||
value="org.hibernate.dialect.PostgreSQL95Dialect" /> <!-- DB Dialect -->
|
value="org.hibernate.dialect.PostgreSQL95Dialect" />
|
||||||
<property name="hibernate.hbm2ddl.auto" value="update" /> <!-- create / create-drop / update -->
|
<property name="hibernate.hbm2ddl.auto" value="update" />
|
||||||
|
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</persistence-unit>
|
</persistence-unit>
|
||||||
|
|
||||||
</persistence>
|
</persistence>
|
Reference in New Issue
Block a user