Updated database implementation
*Added a contact abstract class that serves as a superclass for user and group * Added a group class * Updated persistenceManager to fit the new contact system. * Updated all classes that used methods, that were updated.
This commit is contained in:
		
							
								
								
									
										2
									
								
								pom.xml
									
									
									
									
									
								
							
							
						
						
									
										2
									
								
								pom.xml
									
									
									
									
									
								
							@@ -28,7 +28,7 @@
 | 
				
			|||||||
		<dependency>
 | 
							<dependency>
 | 
				
			||||||
			<groupId>com.github.informatik-ag-ngl</groupId>
 | 
								<groupId>com.github.informatik-ag-ngl</groupId>
 | 
				
			||||||
			<artifactId>envoy-common</artifactId>
 | 
								<artifactId>envoy-common</artifactId>
 | 
				
			||||||
			<version>develop-SNAPSHOT</version>
 | 
								<version>f~groups-SNAPSHOT</version>
 | 
				
			||||||
		</dependency>
 | 
							</dependency>
 | 
				
			||||||
		<dependency>
 | 
							<dependency>
 | 
				
			||||||
			<groupId>com.github.informatik-ag-ngl</groupId>
 | 
								<groupId>com.github.informatik-ag-ngl</groupId>
 | 
				
			||||||
 
 | 
				
			|||||||
							
								
								
									
										62
									
								
								src/main/java/envoy/server/data/Contact.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										62
									
								
								src/main/java/envoy/server/data/Contact.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,62 @@
 | 
				
			|||||||
 | 
					package envoy.server.data;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import javax.persistence.GeneratedValue;
 | 
				
			||||||
 | 
					import javax.persistence.GenerationType;
 | 
				
			||||||
 | 
					import javax.persistence.Id;
 | 
				
			||||||
 | 
					import javax.persistence.MappedSuperclass;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					/**
 | 
				
			||||||
 | 
					 * This class acts as a superclass for all contacts, being {@link User}s and
 | 
				
			||||||
 | 
					 * {@link Group}s. <br>
 | 
				
			||||||
 | 
					 * <br>
 | 
				
			||||||
 | 
					 * Project: <strong>envoy-server-standalone</strong><br>
 | 
				
			||||||
 | 
					 * File: <strong>Contact.java</strong><br>
 | 
				
			||||||
 | 
					 * Created: <strong>24.03.2020</strong><br>
 | 
				
			||||||
 | 
					 * 
 | 
				
			||||||
 | 
					 * @author Maximilian Käfer
 | 
				
			||||||
 | 
					 * @since Envoy Server Standalone v0.1-alpha
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					@MappedSuperclass
 | 
				
			||||||
 | 
					// TODO add queries
 | 
				
			||||||
 | 
					public abstract class Contact {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						@Id
 | 
				
			||||||
 | 
						@GeneratedValue(strategy = GenerationType.IDENTITY)
 | 
				
			||||||
 | 
						protected long		id;
 | 
				
			||||||
 | 
						protected String	name;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * @return a envoy.data.Contact object of this envoy.server.data.Contact object.
 | 
				
			||||||
 | 
						 * @since Envoy Server Standalone v0.1-beta
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						public abstract envoy.data.Contact toCommon();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * @return the ID of this contact.
 | 
				
			||||||
 | 
						 * @since Envoy Server Standalone v0.1-beta
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						public long getID() { return id; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * Sets the ID of this contact.
 | 
				
			||||||
 | 
						 * 
 | 
				
			||||||
 | 
						 * @param id to set for this contact
 | 
				
			||||||
 | 
						 * @since Envoy Server Standalone v0.1-beta
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						public void setID(long id) { this.id = id; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * @return the name of this contact.
 | 
				
			||||||
 | 
						 * @since Envoy Server Standalone v0.1-beta
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						public String getName() { return name; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * Sets the name of this contact.
 | 
				
			||||||
 | 
						 * 
 | 
				
			||||||
 | 
						 * @param name to set for this contact
 | 
				
			||||||
 | 
						 * @since Envoy Server Standalone v0.1-beta
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						public void setName(String name) { this.name = name; }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										54
									
								
								src/main/java/envoy/server/data/Group.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										54
									
								
								src/main/java/envoy/server/data/Group.java
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,54 @@
 | 
				
			|||||||
 | 
					package envoy.server.data;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import java.util.List;
 | 
				
			||||||
 | 
					import java.util.stream.Collectors;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					import javax.persistence.CascadeType;
 | 
				
			||||||
 | 
					import javax.persistence.Entity;
 | 
				
			||||||
 | 
					import javax.persistence.ManyToMany;
 | 
				
			||||||
 | 
					import javax.persistence.NamedQueries;
 | 
				
			||||||
 | 
					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>
 | 
				
			||||||
 | 
					 * <br>
 | 
				
			||||||
 | 
					 * Project: <strong>envoy-server-standalone</strong><br>
 | 
				
			||||||
 | 
					 * File: <strong>Group.java</strong><br>
 | 
				
			||||||
 | 
					 * Created: <strong>24.03.2020</strong><br>
 | 
				
			||||||
 | 
					 * 
 | 
				
			||||||
 | 
					 * @author Maximilian Käfer
 | 
				
			||||||
 | 
					 * @since Envoy Server Standalone v0.1-alpha
 | 
				
			||||||
 | 
					 */
 | 
				
			||||||
 | 
					@Entity
 | 
				
			||||||
 | 
					@Table(name = "groups")
 | 
				
			||||||
 | 
					@NamedQueries({ @NamedQuery(query = "SELECT g FROM Group g WHERE g.name = :name", name = "getGroupByName") })
 | 
				
			||||||
 | 
					public class Group extends Contact {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						@ManyToMany(targetEntity = Group.class, cascade = CascadeType.ALL)
 | 
				
			||||||
 | 
						private List<User> members;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * {@inheritDoc}
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						@Override
 | 
				
			||||||
 | 
						public envoy.data.Group toCommon() { return new envoy.data.Group(id, name, members.stream().map(User::getID).collect(Collectors.toList())); }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * @return a list of all users that are a member of this group.
 | 
				
			||||||
 | 
						 * @since Envoy Server Standalone v0.1-beta
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						public List<User> getMembers() { return members; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * Sets the members of this group.
 | 
				
			||||||
 | 
						 * 
 | 
				
			||||||
 | 
						 * @param Members a list of all users, that should be assigned to this group.
 | 
				
			||||||
 | 
						 * @since Envoy Server Standalone v0.1-beta
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						public void setMembers(List<User> members) { this.members = members; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
@@ -2,7 +2,15 @@ package envoy.server.data;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import java.util.Date;
 | 
					import java.util.Date;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import javax.persistence.*;
 | 
					import javax.persistence.CascadeType;
 | 
				
			||||||
 | 
					import javax.persistence.Entity;
 | 
				
			||||||
 | 
					import javax.persistence.Id;
 | 
				
			||||||
 | 
					import javax.persistence.ManyToOne;
 | 
				
			||||||
 | 
					import javax.persistence.NamedQueries;
 | 
				
			||||||
 | 
					import javax.persistence.NamedQuery;
 | 
				
			||||||
 | 
					import javax.persistence.Table;
 | 
				
			||||||
 | 
					import javax.persistence.Temporal;
 | 
				
			||||||
 | 
					import javax.persistence.TemporalType;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import envoy.data.MessageBuilder;
 | 
					import envoy.data.MessageBuilder;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -89,7 +97,7 @@ public class Message {
 | 
				
			|||||||
	 */
 | 
						 */
 | 
				
			||||||
	public envoy.data.Message toCommonMessage() {
 | 
						public envoy.data.Message toCommonMessage() {
 | 
				
			||||||
		// TODO: Attachment
 | 
							// TODO: Attachment
 | 
				
			||||||
		envoy.data.Message message = new MessageBuilder(sender.getId(), recipient.getId(), id).setText(text)
 | 
							envoy.data.Message message = new MessageBuilder(sender.getID(), recipient.getID(), id).setText(text)
 | 
				
			||||||
			.setDate(creationDate)
 | 
								.setDate(creationDate)
 | 
				
			||||||
			.setStatus(status)
 | 
								.setStatus(status)
 | 
				
			||||||
			.setForwarded(forwarded)
 | 
								.setForwarded(forwarded)
 | 
				
			||||||
@@ -103,12 +111,12 @@ public class Message {
 | 
				
			|||||||
	 * @return the id of a {link envoy.data.Message}
 | 
						 * @return the id of a {link envoy.data.Message}
 | 
				
			||||||
	 * @since Envoy Server Standalone v0.1-alpha
 | 
						 * @since Envoy Server Standalone v0.1-alpha
 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	public long getId() { return id; }
 | 
						public long getID() { return id; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
	 * @param id the id to set
 | 
						 * @param id the id to set
 | 
				
			||||||
	 * @since Envoy Server Standalone v0.1-alpha
 | 
						 * @since Envoy Server Standalone v0.1-alpha
 | 
				
			||||||
	 * @see Message#getId()
 | 
						 * @see Message#getID()
 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	public void setId(long id) { this.id = id; }
 | 
						public void setId(long id) { this.id = id; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -16,6 +16,7 @@ import envoy.server.net.ConnectionManager;
 | 
				
			|||||||
 * Created: <strong>1 Jan 2020</strong><br>
 | 
					 * Created: <strong>1 Jan 2020</strong><br>
 | 
				
			||||||
 *
 | 
					 *
 | 
				
			||||||
 * @author Leon Hofmeister
 | 
					 * @author Leon Hofmeister
 | 
				
			||||||
 | 
					 * @author Maximilian Käfer
 | 
				
			||||||
 * @since Envoy Server Standalone v0.1-alpha
 | 
					 * @since Envoy Server Standalone v0.1-alpha
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
public class PersistenceManager {
 | 
					public class PersistenceManager {
 | 
				
			||||||
@@ -49,12 +50,12 @@ public class PersistenceManager {
 | 
				
			|||||||
	public static PersistenceManager getInstance() { return persistenceManager; }
 | 
						public static PersistenceManager getInstance() { return persistenceManager; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
	 * Adds a {@link User} to the database.
 | 
						 * Adds a {@link Contact} to the database.
 | 
				
			||||||
	 *
 | 
						 *
 | 
				
			||||||
	 * @param user the {@link User} to add to the database
 | 
						 * @param user the {@link Contact} to add to the database
 | 
				
			||||||
	 * @since Envoy Server Standalone v0.1-alpha
 | 
						 * @since Envoy Server Standalone v0.1-alpha
 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	public void addUser(User user) { persist(user); }
 | 
						public void addContact(Contact contact) { persist(contact); }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
	 * Adds a {@link Message} to the database.
 | 
						 * Adds a {@link Message} to the database.
 | 
				
			||||||
@@ -73,12 +74,12 @@ public class PersistenceManager {
 | 
				
			|||||||
	public void addConfigItem(ConfigItem configItem) { persist(configItem); }
 | 
						public void addConfigItem(ConfigItem configItem) { persist(configItem); }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
	 * Updates a {@link User} in the database
 | 
						 * Updates a {@link Contact} in the database
 | 
				
			||||||
	 *
 | 
						 *
 | 
				
			||||||
	 * @param user the {@link User} to add to the database
 | 
						 * @param user the {@link Contact} to add to the database
 | 
				
			||||||
	 * @since Envoy Server Standalone v0.1-alpha
 | 
						 * @since Envoy Server Standalone v0.1-alpha
 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	public void updateUser(User user) { merge(user); }
 | 
						public void updateContact(Contact contact) { merge(contact); }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
	 * Updates a {@link Message} in the database.
 | 
						 * Updates a {@link Message} in the database.
 | 
				
			||||||
@@ -97,12 +98,12 @@ public class PersistenceManager {
 | 
				
			|||||||
	public void updateConfigItem(ConfigItem configItem) { merge(configItem); }
 | 
						public void updateConfigItem(ConfigItem configItem) { merge(configItem); }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
	 * Deletes a {@link User} in the database.
 | 
						 * Deletes a {@link Contact} in the database.
 | 
				
			||||||
	 *
 | 
						 *
 | 
				
			||||||
	 * @param user the {@link User} to delete
 | 
						 * @param user the {@link Contact} to delete
 | 
				
			||||||
	 * @since Envoy Server Standalone v0.1-alpha
 | 
						 * @since Envoy Server Standalone v0.1-alpha
 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	public void deleteUser(User user) { remove(user); }
 | 
						public void deleteContact(Contact contact) { remove(contact); }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
	 * Deletes a {@link Message} in the database.
 | 
						 * Deletes a {@link Message} in the database.
 | 
				
			||||||
@@ -113,7 +114,7 @@ public class PersistenceManager {
 | 
				
			|||||||
	public void deleteMessage(Message message) { remove(message); }
 | 
						public void deleteMessage(Message message) { remove(message); }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
	 * Searches for a {@link User} with a specific id.
 | 
						 * Searches for a {@link User} with a specific ID.
 | 
				
			||||||
	 *
 | 
						 *
 | 
				
			||||||
	 * @param id the id to search for
 | 
						 * @param id the id to search for
 | 
				
			||||||
	 * @return the user with the specified id
 | 
						 * @return the user with the specified id
 | 
				
			||||||
@@ -121,6 +122,24 @@ public class PersistenceManager {
 | 
				
			|||||||
	 */
 | 
						 */
 | 
				
			||||||
	public User getUserById(long id) { return entityManager.find(User.class, id); }
 | 
						public User getUserById(long id) { return entityManager.find(User.class, id); }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * Searches for a {@link Group} with a specific ID.
 | 
				
			||||||
 | 
						 * 
 | 
				
			||||||
 | 
						 * @param id the id to search for
 | 
				
			||||||
 | 
						 * @return the group with the specific id
 | 
				
			||||||
 | 
						 * @since Envoy Server Standalone v0.1-beta
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						public Group getGroupById(long id) { return entityManager.find(Group.class, id); }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * Searches for a {@link Contact} with a specific ID.
 | 
				
			||||||
 | 
						 * 
 | 
				
			||||||
 | 
						 * @param id the id to search for
 | 
				
			||||||
 | 
						 * @return the contact with the specific id
 | 
				
			||||||
 | 
						 * @since Envoy Server Standalone v0.1-beta
 | 
				
			||||||
 | 
						 */
 | 
				
			||||||
 | 
						public Contact getContactById(long id) { return entityManager.find(Contact.class, id); }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
	 * Searched for a {@link User} with a specific name.
 | 
						 * Searched for a {@link User} with a specific name.
 | 
				
			||||||
	 *
 | 
						 *
 | 
				
			||||||
@@ -132,6 +151,28 @@ public class PersistenceManager {
 | 
				
			|||||||
		return (User) entityManager.createNamedQuery("getUserByName").setParameter("name", name).getSingleResult();
 | 
							return (User) entityManager.createNamedQuery("getUserByName").setParameter("name", name).getSingleResult();
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						/**
 | 
				
			||||||
 | 
						 * Searched for a {@link Group} with a specific name.
 | 
				
			||||||
 | 
						 *
 | 
				
			||||||
 | 
						 * @param name the name of the group
 | 
				
			||||||
 | 
						 * @return the group with the specified name
 | 
				
			||||||
 | 
						 * @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();
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
	 * Searches for a {@link Message} with a specific id.
 | 
						 * Searches for a {@link Message} with a specific id.
 | 
				
			||||||
	 *
 | 
						 *
 | 
				
			||||||
@@ -183,7 +224,7 @@ public class PersistenceManager {
 | 
				
			|||||||
	 * @param userId2 the ID of the second user
 | 
						 * @param userId2 the ID of the second user
 | 
				
			||||||
	 * @since Envoy Server Standalone v0.1-alpha
 | 
						 * @since Envoy Server Standalone v0.1-alpha
 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	public void addContact(long userId1, long userId2) {
 | 
						public void addUserContact(long userId1, long userId2) {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// Get users by ID
 | 
							// Get users by ID
 | 
				
			||||||
		User	u1	= getUserById(userId1);
 | 
							User	u1	= getUserById(userId1);
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -31,12 +31,8 @@ import javax.persistence.*;
 | 
				
			|||||||
		name = "searchUsers"
 | 
							name = "searchUsers"
 | 
				
			||||||
	) }
 | 
						) }
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
public class User {
 | 
					public class User extends Contact {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	@Id
 | 
					 | 
				
			||||||
	@GeneratedValue(strategy = GenerationType.IDENTITY)
 | 
					 | 
				
			||||||
	private long	id;
 | 
					 | 
				
			||||||
	private String	name;
 | 
					 | 
				
			||||||
	private byte[]	passwordHash;
 | 
						private byte[]	passwordHash;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	@Temporal(TemporalType.TIMESTAMP)
 | 
						@Temporal(TemporalType.TIMESTAMP)
 | 
				
			||||||
@@ -47,55 +43,10 @@ public class User {
 | 
				
			|||||||
	private List<User> contacts;
 | 
						private List<User> contacts;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
	 * Creates an instance of {@link User}.
 | 
						 * {@inheritDoc}
 | 
				
			||||||
	 *
 | 
					 | 
				
			||||||
	 * @since Envoy Server Standalone v0.1-alpha
 | 
					 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	public User() {}
 | 
						@Override
 | 
				
			||||||
 | 
						public envoy.data.User toCommon() { return new envoy.data.User(id, name, status); }
 | 
				
			||||||
	/**
 | 
					 | 
				
			||||||
	 * Creates an instance of {@link User}.
 | 
					 | 
				
			||||||
	 *
 | 
					 | 
				
			||||||
	 * @param user the {@link envoy.data.User} to convert
 | 
					 | 
				
			||||||
	 * @since Envoy Server Standalone v0.1-alpha
 | 
					 | 
				
			||||||
	 */
 | 
					 | 
				
			||||||
	public User(envoy.data.User user) {
 | 
					 | 
				
			||||||
		id		= user.getId();
 | 
					 | 
				
			||||||
		name	= user.getName();
 | 
					 | 
				
			||||||
		status	= user.getStatus();
 | 
					 | 
				
			||||||
	}
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	/**
 | 
					 | 
				
			||||||
	 * @return a database {@link User} converted into an {@link envoy.data.User}
 | 
					 | 
				
			||||||
	 * @since Envoy Server Standalone v0.1-alpha
 | 
					 | 
				
			||||||
	 */
 | 
					 | 
				
			||||||
	public envoy.data.User toCommonUser() { return new envoy.data.User(id, name, status); }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	/**
 | 
					 | 
				
			||||||
	 * @return the id of a {link envoy.data.User}
 | 
					 | 
				
			||||||
	 * @since Envoy Server Standalone v0.1-alpha
 | 
					 | 
				
			||||||
	 */
 | 
					 | 
				
			||||||
	public long getId() { return id; }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	/**
 | 
					 | 
				
			||||||
	 * @param id the id to set
 | 
					 | 
				
			||||||
	 * @since Envoy Server Standalone v0.1-alpha
 | 
					 | 
				
			||||||
	 * @see User#getId
 | 
					 | 
				
			||||||
	 */
 | 
					 | 
				
			||||||
	public void setId(long id) { this.id = id; }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	/**
 | 
					 | 
				
			||||||
	 * @return the name of a {link envoy.data.User}
 | 
					 | 
				
			||||||
	 * @since Envoy Server Standalone v0.1-alpha
 | 
					 | 
				
			||||||
	 */
 | 
					 | 
				
			||||||
	public String getName() { return name; }
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
	/**
 | 
					 | 
				
			||||||
	 * @param name the username to set
 | 
					 | 
				
			||||||
	 * @since Envoy Server Standalone v0.1-alpha
 | 
					 | 
				
			||||||
	 * @see User#getName()
 | 
					 | 
				
			||||||
	 */
 | 
					 | 
				
			||||||
	public void setName(String name) { this.name = name; }
 | 
					 | 
				
			||||||
 | 
					
 | 
				
			||||||
	/**
 | 
						/**
 | 
				
			||||||
	 * @return the passwordHash of a {link envoy.data.User}
 | 
						 * @return the passwordHash of a {link envoy.data.User}
 | 
				
			||||||
@@ -148,4 +99,5 @@ public class User {
 | 
				
			|||||||
	 * @see User#getContacts()
 | 
						 * @see User#getContacts()
 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	public void setContacts(List<User> contacts) { this.contacts = contacts; }
 | 
						public void setContacts(List<User> contacts) { this.contacts = contacts; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,6 +1,10 @@
 | 
				
			|||||||
package envoy.server.net;
 | 
					package envoy.server.net;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import java.util.*;
 | 
					import java.util.Date;
 | 
				
			||||||
 | 
					import java.util.HashMap;
 | 
				
			||||||
 | 
					import java.util.HashSet;
 | 
				
			||||||
 | 
					import java.util.Map;
 | 
				
			||||||
 | 
					import java.util.Set;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import com.jenkov.nioserver.ISocketIdListener;
 | 
					import com.jenkov.nioserver.ISocketIdListener;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -29,11 +29,11 @@ public class ContactOperationProcessor implements ObjectProcessor<ContactOperati
 | 
				
			|||||||
				final long contactId = evt.get().getId();
 | 
									final long contactId = evt.get().getId();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
				System.out.printf("Adding user %s to the contact list of user %d.%n", evt.get(), userId);
 | 
									System.out.printf("Adding user %s to the contact list of user %d.%n", evt.get(), userId);
 | 
				
			||||||
				PersistenceManager.getInstance().addContact(userId, contactId);
 | 
									PersistenceManager.getInstance().addUserContact(userId, contactId);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
				// Notify the contact if online
 | 
									// Notify the contact if online
 | 
				
			||||||
				if (ConnectionManager.getInstance().isOnline(contactId)) writeProxy.write(connectionManager.getSocketId(contactId),
 | 
									if (ConnectionManager.getInstance().isOnline(contactId)) writeProxy.write(connectionManager.getSocketId(contactId),
 | 
				
			||||||
						new Contacts(Arrays.asList(PersistenceManager.getInstance().getUserById(userId).toCommonUser())));
 | 
											new Contacts(Arrays.asList(PersistenceManager.getInstance().getUserById(userId).toCommon())));
 | 
				
			||||||
				break;
 | 
									break;
 | 
				
			||||||
			default:
 | 
								default:
 | 
				
			||||||
				System.err.printf("Received %s with an unsupported operation.%n", evt);
 | 
									System.err.printf("Received %s with an unsupported operation.%n", evt);
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -35,7 +35,7 @@ public class ContactsRequestEventProcessor implements ObjectProcessor<ContactSea
 | 
				
			|||||||
				new ContactSearchResult(PersistenceManager.getInstance()
 | 
									new ContactSearchResult(PersistenceManager.getInstance()
 | 
				
			||||||
					.searchUsers(request.get(), ConnectionManager.getInstance().getUserIdBySocketId(socketId))
 | 
										.searchUsers(request.get(), ConnectionManager.getInstance().getUserIdBySocketId(socketId))
 | 
				
			||||||
					.stream()
 | 
										.stream()
 | 
				
			||||||
					.map(User::toCommonUser)
 | 
										.map(User::toCommon)
 | 
				
			||||||
					.collect(Collectors.toList())));
 | 
										.collect(Collectors.toList())));
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -2,8 +2,8 @@ package envoy.server.processors;
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import java.io.IOException;
 | 
					import java.io.IOException;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import envoy.data.IdGenerator;
 | 
					import envoy.data.IDGenerator;
 | 
				
			||||||
import envoy.event.IdGeneratorRequest;
 | 
					import envoy.event.IDGeneratorRequest;
 | 
				
			||||||
import envoy.server.data.ConfigItem;
 | 
					import envoy.server.data.ConfigItem;
 | 
				
			||||||
import envoy.server.data.PersistenceManager;
 | 
					import envoy.server.data.PersistenceManager;
 | 
				
			||||||
import envoy.server.net.ObjectWriteProxy;
 | 
					import envoy.server.net.ObjectWriteProxy;
 | 
				
			||||||
@@ -16,19 +16,19 @@ import envoy.server.net.ObjectWriteProxy;
 | 
				
			|||||||
 * @author Kai S. K. Engelbart
 | 
					 * @author Kai S. K. Engelbart
 | 
				
			||||||
 * @since Envoy Server Standalone v0.1-alpha
 | 
					 * @since Envoy Server Standalone v0.1-alpha
 | 
				
			||||||
 */
 | 
					 */
 | 
				
			||||||
public class IDGeneratorRequestProcessor implements ObjectProcessor<IdGeneratorRequest> {
 | 
					public class IDGeneratorRequestProcessor implements ObjectProcessor<IDGeneratorRequest> {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	private static final long ID_RANGE = 200;
 | 
						private static final long ID_RANGE = 200;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	@Override
 | 
						@Override
 | 
				
			||||||
	public Class<IdGeneratorRequest> getInputClass() { return IdGeneratorRequest.class; }
 | 
						public Class<IDGeneratorRequest> getInputClass() { return IDGeneratorRequest.class; }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	@Override
 | 
						@Override
 | 
				
			||||||
	public void process(IdGeneratorRequest input, long socketId, ObjectWriteProxy writeProxy) throws IOException {
 | 
						public void process(IDGeneratorRequest input, long socketId, ObjectWriteProxy writeProxy) throws IOException {
 | 
				
			||||||
		System.out.println("Received id generation request.");
 | 
							System.out.println("Received id generation request.");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		ConfigItem	currentId	= PersistenceManager.getInstance().getConfigItemById("currentMessageId");
 | 
							ConfigItem	currentId	= PersistenceManager.getInstance().getConfigItemById("currentMessageId");
 | 
				
			||||||
		IdGenerator	generator	= new IdGenerator(Integer.parseInt(currentId.getValue()), ID_RANGE);
 | 
							IDGenerator	generator	= new IDGenerator(Integer.parseInt(currentId.getValue()), ID_RANGE);
 | 
				
			||||||
		currentId.setValue(String.valueOf(Integer.parseInt(currentId.getValue()) + ID_RANGE));
 | 
							currentId.setValue(String.valueOf(Integer.parseInt(currentId.getValue()) + ID_RANGE));
 | 
				
			||||||
		PersistenceManager.getInstance().updateConfigItem(currentId);
 | 
							PersistenceManager.getInstance().updateConfigItem(currentId);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,7 +1,11 @@
 | 
				
			|||||||
package envoy.server.processors;
 | 
					package envoy.server.processors;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import java.io.IOException;
 | 
					import java.io.IOException;
 | 
				
			||||||
import java.util.*;
 | 
					import java.util.ArrayList;
 | 
				
			||||||
 | 
					import java.util.Arrays;
 | 
				
			||||||
 | 
					import java.util.Date;
 | 
				
			||||||
 | 
					import java.util.InputMismatchException;
 | 
				
			||||||
 | 
					import java.util.List;
 | 
				
			||||||
import java.util.stream.Collectors;
 | 
					import java.util.stream.Collectors;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import javax.persistence.NoResultException;
 | 
					import javax.persistence.NoResultException;
 | 
				
			||||||
@@ -45,7 +49,7 @@ public class LoginCredentialProcessor implements ObjectProcessor<LoginCredential
 | 
				
			|||||||
			System.out.println("Rejecting handshake on socket " + socketId);
 | 
								System.out.println("Rejecting handshake on socket " + socketId);
 | 
				
			||||||
			return;
 | 
								return;
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		connectionManager.registerUser(user.getId(), socketId);
 | 
							connectionManager.registerUser(user.getID(), socketId);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// Notifies contacts of this users online-going and updates his status in the
 | 
							// Notifies contacts of this users online-going and updates his status in the
 | 
				
			||||||
		// database
 | 
							// database
 | 
				
			||||||
@@ -53,12 +57,12 @@ public class LoginCredentialProcessor implements ObjectProcessor<LoginCredential
 | 
				
			|||||||
		UserStatusChangeProcessor.updateUserStatus(user);
 | 
							UserStatusChangeProcessor.updateUserStatus(user);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// Create contacts
 | 
							// Create contacts
 | 
				
			||||||
		Contacts contacts = new Contacts(user.getContacts().stream().map(envoy.server.data.User::toCommonUser).collect(Collectors.toList()));
 | 
							Contacts contacts = new Contacts(user.getContacts().stream().map(envoy.server.data.User::toCommon).collect(Collectors.toList()));
 | 
				
			||||||
		contacts.getContacts().add(user.toCommonUser());
 | 
							contacts.getContacts().add(user.toCommon());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// Complete handshake
 | 
							// Complete handshake
 | 
				
			||||||
		System.out.println("Sending user...");
 | 
							System.out.println("Sending user...");
 | 
				
			||||||
		writeProxy.write(socketId, user.toCommonUser());
 | 
							writeProxy.write(socketId, user.toCommon());
 | 
				
			||||||
		System.out.println("Sending contacts...");
 | 
							System.out.println("Sending contacts...");
 | 
				
			||||||
		writeProxy.write(socketId, contacts);
 | 
							writeProxy.write(socketId, contacts);
 | 
				
			||||||
		System.out.println("Acquiring pending messages for the client...");
 | 
							System.out.println("Acquiring pending messages for the client...");
 | 
				
			||||||
@@ -94,7 +98,7 @@ public class LoginCredentialProcessor implements ObjectProcessor<LoginCredential
 | 
				
			|||||||
			envoy.server.data.User user = persistenceManager.getUserByName(credentials.getIdentifier());
 | 
								envoy.server.data.User user = persistenceManager.getUserByName(credentials.getIdentifier());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
			// Checking if user is already online
 | 
								// Checking if user is already online
 | 
				
			||||||
			if (connectionManager.isOnline(user.getId())) {
 | 
								if (connectionManager.isOnline(user.getID())) {
 | 
				
			||||||
				writeProxy.write(socketId, new HandshakeRejectionEvent(HandshakeRejectionEvent.ALREADY_ONLINE));
 | 
									writeProxy.write(socketId, new HandshakeRejectionEvent(HandshakeRejectionEvent.ALREADY_ONLINE));
 | 
				
			||||||
				return null;
 | 
									return null;
 | 
				
			||||||
			}
 | 
								}
 | 
				
			||||||
@@ -140,7 +144,7 @@ public class LoginCredentialProcessor implements ObjectProcessor<LoginCredential
 | 
				
			|||||||
			user.setStatus(User.UserStatus.ONLINE);
 | 
								user.setStatus(User.UserStatus.ONLINE);
 | 
				
			||||||
			user.setPasswordHash(credentials.getPasswordHash());
 | 
								user.setPasswordHash(credentials.getPasswordHash());
 | 
				
			||||||
			user.setContacts(new ArrayList<>());
 | 
								user.setContacts(new ArrayList<>());
 | 
				
			||||||
			persistenceManager.addUser(user);
 | 
								persistenceManager.addContact(user);
 | 
				
			||||||
			return user;
 | 
								return user;
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -33,7 +33,7 @@ public class MessageStatusChangeProcessor implements ObjectProcessor<MessageStat
 | 
				
			|||||||
		persistenceManager.updateMessage(msg);
 | 
							persistenceManager.updateMessage(msg);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// Notifies the sender of the message about the status-update to READ
 | 
							// Notifies the sender of the message about the status-update to READ
 | 
				
			||||||
		final long senderId = msg.getSender().getId();
 | 
							final long senderId = msg.getSender().getID();
 | 
				
			||||||
		if (connectionManager.isOnline(senderId)) writeProxy.write(connectionManager.getSocketId(senderId), input);
 | 
							if (connectionManager.isOnline(senderId)) writeProxy.write(connectionManager.getSocketId(senderId), input);
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -48,7 +48,7 @@ public class UserStatusChangeProcessor implements ObjectProcessor<UserStatusChan
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
	public static void updateUserStatus(User user) {
 | 
						public static void updateUserStatus(User user) {
 | 
				
			||||||
		// handling for newly logged in clients
 | 
							// handling for newly logged in clients
 | 
				
			||||||
		persistenceManager.updateUser(user);
 | 
							persistenceManager.updateContact(user);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
		// handling for contacts that are already online
 | 
							// handling for contacts that are already online
 | 
				
			||||||
		notifyContacts(user);
 | 
							notifyContacts(user);
 | 
				
			||||||
@@ -68,11 +68,11 @@ public class UserStatusChangeProcessor implements ObjectProcessor<UserStatusChan
 | 
				
			|||||||
	 * @since Envoy Server Standalone v0.1-alpha
 | 
						 * @since Envoy Server Standalone v0.1-alpha
 | 
				
			||||||
	 */
 | 
						 */
 | 
				
			||||||
	private static void notifyContacts(User user) {
 | 
						private static void notifyContacts(User user) {
 | 
				
			||||||
		UserStatusChangeEvent	evt					= new UserStatusChangeEvent(user.getId(), user.getStatus());
 | 
							UserStatusChangeEvent	evt					= new UserStatusChangeEvent(user.getID(), user.getStatus());
 | 
				
			||||||
		ConnectionManager		connectionManager	= ConnectionManager.getInstance();
 | 
							ConnectionManager		connectionManager	= ConnectionManager.getInstance();
 | 
				
			||||||
		try {
 | 
							try {
 | 
				
			||||||
			for (User contact : user.getContacts())
 | 
								for (User contact : user.getContacts())
 | 
				
			||||||
				if (connectionManager.isOnline(contact.getId())) writeProxy.write(connectionManager.getSocketId(contact.getId()), evt);
 | 
									if (connectionManager.isOnline(contact.getID())) writeProxy.write(connectionManager.getSocketId(contact.getID()), evt);
 | 
				
			||||||
		} catch (IOException e) {
 | 
							} catch (IOException e) {
 | 
				
			||||||
			e.printStackTrace();
 | 
								e.printStackTrace();
 | 
				
			||||||
			System.err.println("Could not notify online contacts of user " + evt.getId() + " that his status has been changed");
 | 
								System.err.println("Could not notify online contacts of user " + evt.getId() + " that his status has been changed");
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user