Use constants as query names, joined inheritance for contacts
This commit is contained in:
		| @@ -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<Contact> contacts; | ||||
|  | ||||
| 	/** | ||||
|   | ||||
| @@ -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.<br> | ||||
|  * It will be referenced as "database group" to clarify between the different | ||||
|  * group objects.<br> | ||||
|  * Represents a group inside the database. Referred to as "server group" as | ||||
|  * opposed to "group" from Envoy Common.<br> | ||||
|  * <br> | ||||
|  * Project: <strong>envoy-server-standalone</strong><br> | ||||
|  * File: <strong>Group.java</strong><br> | ||||
| @@ -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} | ||||
| 	 */ | ||||
|   | ||||
| @@ -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: <strong>envoy-server-standalone</strong><br> | ||||
| @@ -22,9 +15,7 @@ import javax.persistence.TemporalType; | ||||
|  */ | ||||
|  | ||||
| @Entity | ||||
| @Table(name = "groupMessages") | ||||
| // @NamedQuery() | ||||
| // TODO Add Queries | ||||
| @Table(name = "group_messages") | ||||
| public class GroupMessage { | ||||
|  | ||||
| 	@Id | ||||
|   | ||||
| @@ -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) | ||||
|   | ||||
| @@ -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<Message> 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<User> 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<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) { | ||||
|   | ||||
| @@ -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) | ||||
|   | ||||
| @@ -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"> | ||||
|  | ||||
| 	<persistence-unit name="envoy" | ||||
| 		transaction-type="RESOURCE_LOCAL"> | ||||
|  | ||||
| 		<properties> | ||||
| 		 | ||||
| 			<!-- JDBC --> | ||||
| 			<property name="javax.persistence.jdbc.driver" | ||||
| 				value="org.postgresql.Driver" /> <!-- DB Driver --> | ||||
| 				value="org.postgresql.Driver" /> | ||||
| 			<property name="javax.persistence.jdbc.url" | ||||
| 				value="jdbc:postgresql://localhost/envoy" /> <!-- BD Mane --> | ||||
| 			<property name="javax.persistence.jdbc.user" value="envoy" /> <!-- DB User --> | ||||
| 				value="jdbc:postgresql://localhost/envoy" /> | ||||
| 			<property name="javax.persistence.jdbc.user" value="envoy" /> | ||||
| 			<property name="javax.persistence.jdbc.password" | ||||
| 				value="envoy" /> <!-- DB Password --> | ||||
| 				value="envoy" /> | ||||
|  | ||||
|  | ||||
| 			<!-- Hibernate --> | ||||
| 			<property name="hibernate.dialect" | ||||
| 				value="org.hibernate.dialect.PostgreSQL95Dialect" /> <!-- DB Dialect --> | ||||
| 			<property name="hibernate.hbm2ddl.auto" value="update" /> <!-- create / create-drop / update --> | ||||
| 				value="org.hibernate.dialect.PostgreSQL95Dialect" /> | ||||
| 			<property name="hibernate.hbm2ddl.auto" value="update" /> | ||||
|  | ||||
| 		</properties> | ||||
|  | ||||
| 	</persistence-unit> | ||||
|  | ||||
| </persistence> | ||||
		Reference in New Issue
	
	Block a user