Implemented contact adding mechanism + database update on both clients.

* Added filtering on searchRequest response (temporary: has to be done
in query not manually in ContactsRequestProcessor.)
This commit is contained in:
DieGurke 2020-02-10 20:02:05 +01:00
parent 37f1594989
commit a96199ccd7
4 changed files with 36 additions and 14 deletions

View File

@ -8,9 +8,9 @@ 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.OneToMany;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
@ -26,13 +26,14 @@ import javax.persistence.TemporalType;
* Created: <strong>02.01.2020</strong><br>
*
* @author Kai S. K. Engelbart
* @author Maximilian K&auml;fer
* @since Envoy Server Standalone v0.1-alpha
*/
@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"), // not tested
@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") }
)
public class User {
@ -47,7 +48,7 @@ public class User {
private Date lastSeen;
private envoy.data.User.UserStatus status;
@OneToMany(targetEntity = User.class, cascade = CascadeType.ALL, orphanRemoval = true)
@ManyToMany(targetEntity = User.class, cascade = CascadeType.ALL) // , orphanRemoval = true
private List<User> contacts;
/**

View File

@ -172,10 +172,12 @@ 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();
}
@ -191,13 +193,11 @@ public class PersistenceManager {
/**
* @param user the User whose contacts should be retrieved
* @return the contacts of this User - currently everyone using Envoy
* @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();
}
// TODO current solution gets all users, not just contacts. Should be changed to
// entityManager.createNamedQuery("getContactsOfUser").setParameter("user",
// user).getResultList();
}

View File

@ -1,11 +1,14 @@
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;
@ -15,18 +18,36 @@ import envoy.server.net.ObjectWriteProxy;
* 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 {
writeProxy.write(socketId,
new Contacts(PersistenceManager.getPersistenceManager()
.searchUsers(request.get())
.stream()
.map(envoy.server.data.User::toCommonUser)
.collect(Collectors.toList())));
// 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

View File

@ -80,8 +80,8 @@ public class LoginCredentialProcessor implements ObjectProcessor<LoginCredential
user.setLastSeen(new Date());
user.setStatus(User.UserStatus.ONLINE);
user.setPasswordHash(credentials.getPasswordHash());
user.setContacts(PersistenceManager.getPersistenceManager().getContacts(user));
persistenceManager.addUser(user);
user.setContacts(PersistenceManager.getPersistenceManager().getContacts(user)); // TODO: Maybe delete this line
} else {
user = persistenceManager.getUserByName(credentials.getName());
// TODO: Implement error when user does not exist