Refactored contact search, added Javadoc

This commit is contained in:
Kai S. K. Engelbart 2020-02-10 22:29:34 +01:00
parent a96199ccd7
commit c5017e71fc
8 changed files with 94 additions and 102 deletions

View File

@ -28,7 +28,7 @@
<dependency>
<groupId>com.github.informatik-ag-ngl</groupId>
<artifactId>envoy-common</artifactId>
<version>f~contacts-SNAPSHOT</version>
<version>develop-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>com.github.informatik-ag-ngl</groupId>

View File

@ -10,13 +10,7 @@ import envoy.server.data.ConfigItem;
import envoy.server.database.PersistenceManager;
import envoy.server.net.ObjectMessageProcessor;
import envoy.server.net.ObjectMessageReader;
import envoy.server.processors.ContactOperationProcessor;
import envoy.server.processors.ContactsRequestProcesor;
import envoy.server.processors.IdGeneratorRequestProcessor;
import envoy.server.processors.LoginCredentialProcessor;
import envoy.server.processors.MessageProcessor;
import envoy.server.processors.MessageStatusChangeProcessor;
import envoy.server.processors.UserStatusChangeProcessor;
import envoy.server.processors.*;
/**
* Starts the server.<br>
@ -44,7 +38,7 @@ public class Startup {
processors.add(new MessageStatusChangeProcessor());
processors.add(new UserStatusChangeProcessor());
processors.add(new IdGeneratorRequestProcessor());
processors.add(new ContactsRequestProcesor());
processors.add(new ContactsRequestEventProcessor());
processors.add(new ContactOperationProcessor());
Server server = new Server(8080, () -> new ObjectMessageReader(), new ObjectMessageProcessor(processors));

View File

@ -3,17 +3,7 @@ package envoy.server.data;
import java.util.Date;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.*;
/**
* This class serves as a way to let Hibernate communicate with the server
@ -32,9 +22,13 @@ import javax.persistence.TemporalType;
@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"),
@NamedQuery(query = "SELECT u FROM User u WHERE lower(u.name) LIKE lower(:searchPhrase)", name = "searchUsers") }
{ @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"
), @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"
) }
)
public class User {
@ -48,7 +42,7 @@ public class User {
private Date lastSeen;
private envoy.data.User.UserStatus status;
@ManyToMany(targetEntity = User.class, cascade = CascadeType.ALL) // , orphanRemoval = true
@ManyToMany(targetEntity = User.class, cascade = CascadeType.ALL)
private List<User> contacts;
/**

View File

@ -172,23 +172,41 @@ public class PersistenceManager {
* @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) {
return entityManager.createNamedQuery("getUnreadMessages").setParameter("recipient", user).getResultList();
}
@SuppressWarnings("unchecked")
public List<User> searchUsers(String searchPhrase) {
return entityManager.createNamedQuery("searchUsers").setParameter("searchPhrase", searchPhrase + "%").getResultList();
/**
* Searches for users matching a search phrase. Contacts of the attached user
* and the attached user is ignored.
*
* @param searchPhrase the search phrase
* @param userId the ID of the user in whose context the search is
* performed
* @return a list of all users who matched the criteria
* @since Envoy Server Standalone v0.1-alpha
*/
public List<User> searchUsers(String searchPhrase, long userId) {
return entityManager.createNamedQuery("searchUsers")
.setParameter("searchPhrase", searchPhrase + "%")
.setParameter("context", getUserById(userId))
.getResultList();
}
public void addContact(long userId, long contactId) {
User c1 = getUserById(userId);
User c2 = getUserById(contactId);
c1.getContacts().add(c2);
c2.getContacts().add(c1);
updateUser(c1);
updateUser(c2);
/**
* Adds a user to the contact list of another user and vice versa.
*
* @param userId1 the ID of the first user
* @param userId2 the ID of the second user
* @since Envoy Server Standalone v0.1-alpha
*/
public void addContact(long userId1, long userId2) {
User u1 = getUserById(userId1);
User u2 = getUserById(userId2);
u1.getContacts().add(u2);
u2.getContacts().add(u1);
updateUser(u1);
updateUser(u2);
}
/**
@ -196,7 +214,6 @@ public class PersistenceManager {
* @return the contacts of this User
* @since Envoy Server Standalone v0.1-alpha
*/
@SuppressWarnings("unchecked")
public List<User> getContacts(User user) {
return entityManager.createNamedQuery("getContactsOfUser").setParameter("user", user).getResultList();
}

View File

@ -2,8 +2,8 @@ package envoy.server.processors;
import java.io.IOException;
import envoy.event.ContactOperation;
import envoy.event.ContactOperation.Operation;
import envoy.event.ContactOperationEvent;
import envoy.event.ContactOperationEvent.Operation;
import envoy.server.ConnectionManager;
import envoy.server.ObjectProcessor;
import envoy.server.database.PersistenceManager;
@ -17,17 +17,17 @@ import envoy.server.net.ObjectWriteProxy;
* @author Kai S. K. Engelbart
* @since Envoy Server Standalone v0.1-alpha
*/
public class ContactOperationProcessor implements ObjectProcessor<ContactOperation> {
public class ContactOperationProcessor implements ObjectProcessor<ContactOperationEvent> {
@Override
public void process(ContactOperation operation, long socketId, ObjectWriteProxy writeProxy) throws IOException {
public void process(ContactOperationEvent operation, long socketId, ObjectWriteProxy writeProxy) throws IOException {
if (operation.getOperationType() == Operation.ADD) {
long userId = ConnectionManager.getInstance().getUserIdBySocketId(socketId);
System.out.printf("Adding user %s to the contact list of user %d", operation.get(), userId);
System.out.printf("Adding user %s to the contact list of user %d.%n", operation.get(), userId);
PersistenceManager.getPersistenceManager().addContact(userId, operation.get().getId());
}
}
@Override
public Class<ContactOperation> getInputClass() { return ContactOperation.class; }
public Class<ContactOperationEvent> getInputClass() { return ContactOperationEvent.class; }
}

View File

@ -0,0 +1,44 @@
package envoy.server.processors;
import java.io.IOException;
import java.util.stream.Collectors;
import envoy.data.Contacts;
import envoy.event.ContactSearchRequest;
import envoy.server.ConnectionManager;
import envoy.server.ObjectProcessor;
import envoy.server.data.User;
import envoy.server.database.PersistenceManager;
import envoy.server.net.ObjectWriteProxy;
/**
* Project: <strong>envoy-server-standalone</strong><br>
* File: <strong>ContactsRequestEventProcessor.java</strong><br>
* Created: <strong>08.02.2020</strong><br>
*
* @author Kai S. K. Engelbart
* @author Maximilian K&auml;fer
* @since Envoy Server Standalone v0.1-alpha
*/
public class ContactsRequestEventProcessor implements ObjectProcessor<ContactSearchRequest> {
/**
* Writes a {@link Contacts} list to the client containing all {@link User}s
* matching the search phrase contained inside the request. The client and their
* contacts are excluded from the result.
*
* @since Envoy Server Standalone v0.1-alpha
*/
@Override
public void process(ContactSearchRequest request, long socketId, ObjectWriteProxy writeProxy) throws IOException {
writeProxy.write(socketId,
new Contacts(PersistenceManager.getPersistenceManager()
.searchUsers(request.get(), ConnectionManager.getInstance().getUserIdBySocketId(socketId))
.stream()
.map(User::toCommonUser)
.collect(Collectors.toList())));
}
@Override
public Class<ContactSearchRequest> getInputClass() { return ContactSearchRequest.class; }
}

View File

@ -1,55 +0,0 @@
package envoy.server.processors;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import envoy.data.Contacts;
import envoy.event.ContactsRequest;
import envoy.server.ObjectProcessor;
import envoy.server.data.User;
import envoy.server.database.PersistenceManager;
import envoy.server.net.ObjectWriteProxy;
/**
* Project: <strong>envoy-server-standalone</strong><br>
* File: <strong>ContactsRequestProcesor.java</strong><br>
* Created: <strong>08.02.2020</strong><br>
*
* @author Kai S. K. Engelbart
* @author Maximilian K&auml;fer
* @since Envoy Server Standalone v0.1-alpha
*/
public class ContactsRequestProcesor implements ObjectProcessor<ContactsRequest> {
@Override
public void process(ContactsRequest request, long socketId, ObjectWriteProxy writeProxy) throws IOException {
// Creating a List containing all searchResults
List<User> resultList = PersistenceManager.getPersistenceManager().searchUsers(request.get());
// Creating a List containing all contacts of the client this event comes from.
List<User> clientContacts = PersistenceManager.getPersistenceManager()
.getContacts(PersistenceManager.getPersistenceManager().getUserById(request.getClient().getId()));
List<User> returnList = new ArrayList<User>();
// Checking for already existing users in the contacts of the client an only
// adding the ones not included to the returnList.
if (clientContacts.size() != 0) {
for (int i = 0; i < resultList.size(); i++) {
for (int j = 0; j < clientContacts.size(); j++) {
if (resultList.get(i).getId() != clientContacts.get(j).getId()) { returnList.add(resultList.get(i)); }
}
}
} else {
for (int i = 0; i < resultList.size(); i++) {
returnList.add(resultList.get(i));
}
}
// Create new Contacts object from returnList
Contacts contacts = new Contacts(returnList.stream().map(envoy.server.data.User::toCommonUser).collect(Collectors.toList()));
writeProxy.write(socketId, contacts);
}
@Override
public Class<ContactsRequest> getInputClass() { return ContactsRequest.class; }
}

View File

@ -1,9 +1,7 @@
package envoy.server.processors;
import java.io.IOException;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;
import envoy.data.Contacts;
@ -81,7 +79,7 @@ public class LoginCredentialProcessor implements ObjectProcessor<LoginCredential
user.setStatus(User.UserStatus.ONLINE);
user.setPasswordHash(credentials.getPasswordHash());
persistenceManager.addUser(user);
user.setContacts(PersistenceManager.getPersistenceManager().getContacts(user)); // TODO: Maybe delete this line
user.setContacts(new ArrayList<>());
} else {
user = persistenceManager.getUserByName(credentials.getName());
// TODO: Implement error when user does not exist