diff --git a/src/main/java/envoy/server/data/Contact.java b/src/main/java/envoy/server/data/Contact.java index b629c37..c9700d5 100644 --- a/src/main/java/envoy/server/data/Contact.java +++ b/src/main/java/envoy/server/data/Contact.java @@ -17,15 +17,16 @@ import javax.persistence.*; */ @Entity -@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) +@Table(name = "contacts") +@Inheritance(strategy = InheritanceType.JOINED) public abstract class Contact { @Id - @GeneratedValue(strategy = GenerationType.TABLE) + @GeneratedValue protected long id; protected String name; - @ManyToMany(targetEntity = Contact.class, cascade = CascadeType.ALL, fetch = FetchType.EAGER) + @ManyToMany(cascade = CascadeType.ALL, fetch = FetchType.EAGER) protected Set contacts; /** diff --git a/src/main/java/envoy/server/data/Group.java b/src/main/java/envoy/server/data/Group.java index 595b252..9c8ebf4 100644 --- a/src/main/java/envoy/server/data/Group.java +++ b/src/main/java/envoy/server/data/Group.java @@ -7,10 +7,8 @@ import javax.persistence.NamedQuery; import javax.persistence.Table; /** - * This class serves as a way to let Hibernate communicate with the server - * without bringing the dependency of JPA/Hibernate into the client.
- * It will be referenced as "database group" to clarify between the different - * group objects.
+ * Represents a group inside the database. Referred to as "server group" as + * opposed to "group" from Envoy Common.
*
* Project: envoy-server-standalone
* File: Group.java
@@ -21,9 +19,19 @@ import javax.persistence.Table; */ @Entity @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 { + /** + * 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} */ diff --git a/src/main/java/envoy/server/data/GroupMessage.java b/src/main/java/envoy/server/data/GroupMessage.java index 0d1e2bc..f952621 100644 --- a/src/main/java/envoy/server/data/GroupMessage.java +++ b/src/main/java/envoy/server/data/GroupMessage.java @@ -3,14 +3,7 @@ package envoy.server.data; import java.util.Date; import java.util.Map; -import javax.persistence.CascadeType; -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; +import javax.persistence.*; /** * Project: envoy-server-standalone
@@ -22,9 +15,7 @@ import javax.persistence.TemporalType; */ @Entity -@Table(name = "groupMessages") -// @NamedQuery() -// TODO Add Queries +@Table(name = "group_messages") public class GroupMessage { @Id diff --git a/src/main/java/envoy/server/data/Message.java b/src/main/java/envoy/server/data/Message.java index f82658a..470f772 100755 --- a/src/main/java/envoy/server/data/Message.java +++ b/src/main/java/envoy/server/data/Message.java @@ -1,15 +1,10 @@ package envoy.server.data; +import static javax.persistence.CascadeType.ALL; + import java.util.Date; -import javax.persistence.CascadeType; -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 javax.persistence.*; import envoy.data.MessageBuilder; @@ -29,20 +24,30 @@ import envoy.data.MessageBuilder; @Entity @Table(name = "messages") @NamedQuery( + name = Message.getPending, 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.status = envoy.data.Message$MessageStatus.READ) AND (m.readDate > :lastSeen))", - name = "getPendingMessages" + + "OR (m.status = envoy.data.Message$MessageStatus.READ) AND (m.readDate > :lastSeen))" ) 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 private long id; - @ManyToOne(cascade = CascadeType.PERSIST) + @ManyToOne(cascade = ALL) + @JoinColumn private User sender; - @ManyToOne(cascade = CascadeType.PERSIST) + @ManyToOne(cascade = ALL) + @JoinColumn private User recipient; @Temporal(TemporalType.TIMESTAMP) diff --git a/src/main/java/envoy/server/data/PersistenceManager.java b/src/main/java/envoy/server/data/PersistenceManager.java index aeecf77..d4c9929 100755 --- a/src/main/java/envoy/server/data/PersistenceManager.java +++ b/src/main/java/envoy/server/data/PersistenceManager.java @@ -148,7 +148,7 @@ public class PersistenceManager { * @since Envoy Server Standalone v0.1-alpha */ 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 */ public Group getGroupByName(String name) { - return (Group) entityManager.createNamedQuery("getGroupByName").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(); + return (Group) entityManager.createNamedQuery(Group.findByName).setParameter("name", name).getSingleResult(); } /** @@ -198,7 +187,8 @@ public class PersistenceManager { * @since Envoy Server Standalone v0.1-alpha */ public List getPendingMessages(User user) { - return entityManager.createNamedQuery("getPendingMessages") + return entityManager + .createNamedQuery(Message.getPending) .setParameter("user", user) .setParameter("lastSeen", user.getLastSeen()) .getResultList(); @@ -215,7 +205,8 @@ public class PersistenceManager { * @since Envoy Server Standalone v0.1-alpha */ public List searchUsers(String searchPhrase, long userId) { - return entityManager.createNamedQuery("searchUsers") + return entityManager.createNamedQuery( + User.searchByName) .setParameter("searchPhrase", searchPhrase + "%") .setParameter("context", getUserByID(userId)) .getResultList(); @@ -251,7 +242,7 @@ public class PersistenceManager { * @since Envoy Server Standalone v0.1-alpha */ public List 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) { diff --git a/src/main/java/envoy/server/data/User.java b/src/main/java/envoy/server/data/User.java index 3fb167f..fba93ab 100755 --- a/src/main/java/envoy/server/data/User.java +++ b/src/main/java/envoy/server/data/User.java @@ -23,17 +23,45 @@ import envoy.data.User.UserStatus; */ @Entity @Table(name = "users") -@NamedQueries( - { @NamedQuery(query = "SELECT u FROM User u WHERE u.name = :name", name = "getUserByName"), @NamedQuery( - query = "SELECT u.contacts FROM User u WHERE u = :user", - name = "getContactsOfUser" +@NamedQueries({ + @NamedQuery( + name = User.findByName, + 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( - query = "SELECT u FROM User u WHERE (lower(u.name) LIKE lower(:searchPhrase) AND u <> :context AND NOT :context in elements(u.contacts))", - name = "searchUsers" - ) } -) + name = User.searchByName, + 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 { + /** + * 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; @Temporal(TemporalType.TIMESTAMP) diff --git a/src/main/resources/META-INF/persistence.xml b/src/main/resources/META-INF/persistence.xml index 10effe7..e013e04 100755 --- a/src/main/resources/META-INF/persistence.xml +++ b/src/main/resources/META-INF/persistence.xml @@ -3,25 +3,25 @@ xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd" version="2.1"> - - + + + value="org.postgresql.Driver" /> - + value="jdbc:postgresql://localhost/envoy" /> + + value="envoy" /> + + - + value="org.hibernate.dialect.PostgreSQL95Dialect" /> + - - \ No newline at end of file