From 7c7a050c7c95886e6536ffd1ee0b6bc326d9baf6 Mon Sep 17 00:00:00 2001 From: DieGurke <55625494+DieGurke@users.noreply.github.com> Date: Tue, 21 Jan 2020 17:51:50 +0100 Subject: [PATCH] Interface Communication Login Credentials --- src/main/java/envoy/server/data/Message.java | 8 +++--- src/main/java/envoy/server/data/User.java | 3 +-- .../server/database/PersistenceManager.java | 7 +++++- .../processors/LoginCredentialProcessor.java | 25 ++++++++----------- 4 files changed, 21 insertions(+), 22 deletions(-) diff --git a/src/main/java/envoy/server/data/Message.java b/src/main/java/envoy/server/data/Message.java index 575da6a..83d7aeb 100644 --- a/src/main/java/envoy/server/data/Message.java +++ b/src/main/java/envoy/server/data/Message.java @@ -2,6 +2,7 @@ package envoy.server.data; import java.util.Date; +import javax.persistence.CascadeType; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.ManyToOne; @@ -33,9 +34,6 @@ import envoy.server.database.PersistenceManager; { @NamedQuery( query = "SELECT m FROM Message m WHERE m.recipient =:recipient AND m.status = envoy.data.Message$MessageStatus.SENT", 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") } ) public class Message { @@ -43,10 +41,10 @@ public class Message { @Id private long id; - @ManyToOne + @ManyToOne(cascade = { CascadeType.PERSIST }) private User sender; - @ManyToOne + @ManyToOne(cascade = { CascadeType.PERSIST }) private User recipient; @Temporal(TemporalType.TIMESTAMP) diff --git a/src/main/java/envoy/server/data/User.java b/src/main/java/envoy/server/data/User.java index c71e0ac..2f629dc 100644 --- a/src/main/java/envoy/server/data/User.java +++ b/src/main/java/envoy/server/data/User.java @@ -49,8 +49,7 @@ public class User { private Date lastSeen; private envoy.data.User.UserStatus status; - // TODO: Fix Error - @OneToMany(targetEntity = User.class, cascade = { CascadeType.ALL }, fetch = FetchType.LAZY, mappedBy = "contacts") + @OneToMany(targetEntity = User.class, cascade = { CascadeType.ALL }, fetch = FetchType.LAZY, orphanRemoval = true) private List contacts; /** diff --git a/src/main/java/envoy/server/database/PersistenceManager.java b/src/main/java/envoy/server/database/PersistenceManager.java index 0339be6..aa2d008 100644 --- a/src/main/java/envoy/server/database/PersistenceManager.java +++ b/src/main/java/envoy/server/database/PersistenceManager.java @@ -43,7 +43,12 @@ public class PersistenceManager { * @param User the {@link User} to add to the database * @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. diff --git a/src/main/java/envoy/server/processors/LoginCredentialProcessor.java b/src/main/java/envoy/server/processors/LoginCredentialProcessor.java index b658445..71a55dc 100644 --- a/src/main/java/envoy/server/processors/LoginCredentialProcessor.java +++ b/src/main/java/envoy/server/processors/LoginCredentialProcessor.java @@ -38,21 +38,18 @@ public class LoginCredentialProcessor implements ObjectProcessor users = PersistenceManager.getPersistenceManager() - .getContacts(new envoy.server.data.User(user)) + .getContacts(user) .stream() .map(envoy.server.data.User::toCommonUser) .collect(Collectors.toList()); @@ -69,11 +66,11 @@ public class LoginCredentialProcessor implements ObjectProcessor pendingMessages = PersistenceManager.getPersistenceManager().getUnreadMessages(new envoy.server.data.User(user)); + List pendingMessages = PersistenceManager.getPersistenceManager().getUnreadMessages(user); for (Message msg : pendingMessages) { System.out.println("Sending message " + msg.toString()); writeProxy.write(socketId, msg);