Interface Communication Login Credentials

This commit is contained in:
DieGurke 2020-01-21 17:51:50 +01:00
parent 8161c5fd28
commit 7c7a050c7c
4 changed files with 21 additions and 22 deletions

View File

@ -2,6 +2,7 @@ package envoy.server.data;
import java.util.Date; import java.util.Date;
import javax.persistence.CascadeType;
import javax.persistence.Entity; import javax.persistence.Entity;
import javax.persistence.Id; import javax.persistence.Id;
import javax.persistence.ManyToOne; import javax.persistence.ManyToOne;
@ -33,9 +34,6 @@ import envoy.server.database.PersistenceManager;
{ @NamedQuery( { @NamedQuery(
query = "SELECT m FROM Message m WHERE m.recipient =:recipient AND m.status = envoy.data.Message$MessageStatus.SENT", query = "SELECT m FROM Message m WHERE m.recipient =:recipient AND m.status = envoy.data.Message$MessageStatus.SENT",
name = "getUnreadMessages" name = "getUnreadMessages"
), @NamedQuery(
query = "SELECT m FROM Message m WHERE m.sender =:sender AND m.status = :status",
name = "find read messages"// TODO do we need this namedQuery?
), @NamedQuery(query = "SELECT m FROM Message m WHERE m.id = :messageId", name = "getMessageById") } ), @NamedQuery(query = "SELECT m FROM Message m WHERE m.id = :messageId", name = "getMessageById") }
) )
public class Message { public class Message {
@ -43,10 +41,10 @@ public class Message {
@Id @Id
private long id; private long id;
@ManyToOne @ManyToOne(cascade = { CascadeType.PERSIST })
private User sender; private User sender;
@ManyToOne @ManyToOne(cascade = { CascadeType.PERSIST })
private User recipient; private User recipient;
@Temporal(TemporalType.TIMESTAMP) @Temporal(TemporalType.TIMESTAMP)

View File

@ -49,8 +49,7 @@ public class User {
private Date lastSeen; private Date lastSeen;
private envoy.data.User.UserStatus status; private envoy.data.User.UserStatus status;
// TODO: Fix Error @OneToMany(targetEntity = User.class, cascade = { CascadeType.ALL }, fetch = FetchType.LAZY, orphanRemoval = true)
@OneToMany(targetEntity = User.class, cascade = { CascadeType.ALL }, fetch = FetchType.LAZY, mappedBy = "contacts")
private List<User> contacts; private List<User> contacts;
/** /**

View File

@ -43,7 +43,12 @@ public class PersistenceManager {
* @param User the {@link User} to add to the database * @param User the {@link User} to add to the database
* @since Envoy Server Standalone v0.1-alpha * @since Envoy Server Standalone v0.1-alpha
*/ */
public void addUser(User User) { entityManager.persist(User); } public void addUser(User User) {
entityManager.getTransaction().begin();
entityManager.persist(User);
entityManager.flush();
entityManager.getTransaction().commit();
}
/** /**
* Adds a {@link Message} to the database. * Adds a {@link Message} to the database.

View File

@ -38,21 +38,18 @@ public class LoginCredentialProcessor implements ObjectProcessor<LoginCredential
System.out.println(String.format("Received login credentials %s from socket ID %d", input, socketId)); System.out.println(String.format("Received login credentials %s from socket ID %d", input, socketId));
// Create user // Create user
User user = null; envoy.server.data.User user = new envoy.server.data.User();
if (input.isRegistration()) { if (input.isRegistration()) {
envoy.server.data.User dbUser = new envoy.server.data.User(); user.setName(input.getName());
dbUser.setName(input.getName()); user.setLastSeen(new Date());
dbUser.setLastSeen(new Date()); user.setStatus(User.UserStatus.ONLINE);
dbUser.setStatus(User.UserStatus.ONLINE); user.setPasswordHash(input.getPasswordHash());
dbUser.setPasswordHash(input.getPasswordHash()); persistenceManager.addUser(user);
persistenceManager.addUser(dbUser);
user = dbUser.toCommonUser();
}else { }else {
// TODO: Implement error when user does not exist // TODO: Implement error when user does not exist
envoy.server.data.User requestedUser = persistenceManager.getUserByName(input.getName()); if (input.getPasswordHash() == user.getPasswordHash()) {
if (input.getPasswordHash() == requestedUser.getPasswordHash()) { user = persistenceManager.getUserByName(input.getName());
user = requestedUser.toCommonUser();
} else { } else {
// TODO: Wrong Password Response // TODO: Wrong Password Response
} }
@ -61,7 +58,7 @@ public class LoginCredentialProcessor implements ObjectProcessor<LoginCredential
// Create contacts // Create contacts
List<User> users = PersistenceManager.getPersistenceManager() List<User> users = PersistenceManager.getPersistenceManager()
.getContacts(new envoy.server.data.User(user)) .getContacts(user)
.stream() .stream()
.map(envoy.server.data.User::toCommonUser) .map(envoy.server.data.User::toCommonUser)
.collect(Collectors.toList()); .collect(Collectors.toList());
@ -69,11 +66,11 @@ public class LoginCredentialProcessor implements ObjectProcessor<LoginCredential
// Complete handshake // Complete handshake
System.out.println("Sending user..."); System.out.println("Sending user...");
writeProxy.write(socketId, user); writeProxy.write(socketId, user.toCommonUser());
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...");
List<Message> pendingMessages = PersistenceManager.getPersistenceManager().getUnreadMessages(new envoy.server.data.User(user)); List<Message> pendingMessages = PersistenceManager.getPersistenceManager().getUnreadMessages(user);
for (Message msg : pendingMessages) { for (Message msg : pendingMessages) {
System.out.println("Sending message " + msg.toString()); System.out.println("Sending message " + msg.toString());
writeProxy.write(socketId, msg); writeProxy.write(socketId, msg);