From 64bc34cf61b33c2af6e3d0e0fbf3721eb496c5e3 Mon Sep 17 00:00:00 2001 From: DieGurke <55625494+DieGurke@users.noreply.github.com> Date: Thu, 2 Apr 2020 20:41:05 +0200 Subject: [PATCH] Implemented Groups on the database and implemented MSChange management --- src/main/java/envoy/server/data/Contact.java | 15 ++++++++--- src/main/java/envoy/server/data/Group.java | 6 +++-- src/main/java/envoy/server/data/Message.java | 11 ++++---- .../envoy/server/data/PersistenceManager.java | 12 ++++++--- .../processors/LoginCredentialProcessor.java | 25 +++++++++++++------ 5 files changed, 46 insertions(+), 23 deletions(-) diff --git a/src/main/java/envoy/server/data/Contact.java b/src/main/java/envoy/server/data/Contact.java index d1737d8..81107d4 100644 --- a/src/main/java/envoy/server/data/Contact.java +++ b/src/main/java/envoy/server/data/Contact.java @@ -2,7 +2,14 @@ package envoy.server.data; import java.util.Set; -import javax.persistence.*; +import javax.persistence.CascadeType; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Inheritance; +import javax.persistence.InheritanceType; +import javax.persistence.ManyToMany; /** * This class acts as a superclass for all contacts, being {@link User}s and @@ -16,12 +23,12 @@ import javax.persistence.*; * @since Envoy Server Standalone v0.1-alpha */ -@MappedSuperclass -// TODO add queries +@Entity +@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS) public abstract class Contact { @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) + @GeneratedValue(strategy = GenerationType.TABLE) protected long id; protected String name; diff --git a/src/main/java/envoy/server/data/Group.java b/src/main/java/envoy/server/data/Group.java index 6c76df9..6301624 100644 --- a/src/main/java/envoy/server/data/Group.java +++ b/src/main/java/envoy/server/data/Group.java @@ -2,7 +2,9 @@ package envoy.server.data; import java.util.stream.Collectors; -import javax.persistence.*; +import javax.persistence.Entity; +import javax.persistence.NamedQuery; +import javax.persistence.Table; import envoy.data.User; @@ -21,7 +23,7 @@ import envoy.data.User; */ @Entity @Table(name = "groups") -@NamedQueries({ @NamedQuery(query = "SELECT g FROM Group g WHERE g.name = :name", name = "getGroupByName") }) +@NamedQuery(query = "SELECT g FROM Group g WHERE g.name = :name", name = "getGroupByName") public class Group extends Contact { /** diff --git a/src/main/java/envoy/server/data/Message.java b/src/main/java/envoy/server/data/Message.java index 76c133c..17a12d8 100755 --- a/src/main/java/envoy/server/data/Message.java +++ b/src/main/java/envoy/server/data/Message.java @@ -6,7 +6,6 @@ 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; @@ -29,11 +28,11 @@ import envoy.data.MessageBuilder; */ @Entity @Table(name = "messages") -@NamedQueries( - { @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.recipient = :user AND m.status = envoy.data.Message$MessageStatus.SENT) " + + "OR (m.sender = :user) AND ((m.status = envoy.data.Message$MessageStatus.RECEIVED) AND (m.receivedDate > :lastSeen)" + + "OR (m.status = envoy.data.Message$MessageStatus.READ) AND (m.readDate > :lastSeen))", + name = "getPendingMessages" ) public class Message { diff --git a/src/main/java/envoy/server/data/PersistenceManager.java b/src/main/java/envoy/server/data/PersistenceManager.java index 1dd82a8..53eb670 100755 --- a/src/main/java/envoy/server/data/PersistenceManager.java +++ b/src/main/java/envoy/server/data/PersistenceManager.java @@ -190,14 +190,18 @@ public class PersistenceManager { public ConfigItem getConfigItemById(String key) { return entityManager.find(ConfigItem.class, key); } /** - * Returns all messages received while being offline. + * Returns all messages received while being offline or the ones that have + * changed. * - * @param user - the user who wants to receive his unread messages + * @param user the user who wants to receive his unread messages * @return all messages that the client does not yet have (unread messages) * @since Envoy Server Standalone v0.1-alpha */ - public List getUnreadMessages(User user) { - return entityManager.createNamedQuery("getUnreadMessages").setParameter("recipient", user).getResultList(); + public List getPendingMessages(User user) { + return entityManager.createNamedQuery("getPendingMessages") + .setParameter("user", user) + .setParameter("lastSeen", user.getLastSeen()) + .getResultList(); } /** diff --git a/src/main/java/envoy/server/processors/LoginCredentialProcessor.java b/src/main/java/envoy/server/processors/LoginCredentialProcessor.java index faa9f6c..4051513 100755 --- a/src/main/java/envoy/server/processors/LoginCredentialProcessor.java +++ b/src/main/java/envoy/server/processors/LoginCredentialProcessor.java @@ -1,7 +1,11 @@ package envoy.server.processors; import java.io.IOException; -import java.util.*; +import java.util.Arrays; +import java.util.Date; +import java.util.HashSet; +import java.util.InputMismatchException; +import java.util.List; import java.util.stream.Collectors; import javax.persistence.NoResultException; @@ -11,6 +15,7 @@ import envoy.data.Message.MessageStatus; import envoy.data.User; import envoy.data.User.UserStatus; import envoy.event.HandshakeRejectionEvent; +import envoy.event.MessageStatusChangeEvent; import envoy.server.data.Message; import envoy.server.data.PersistenceManager; import envoy.server.net.ConnectionManager; @@ -61,13 +66,19 @@ public class LoginCredentialProcessor implements ObjectProcessor pendingMessages = PersistenceManager.getInstance().getUnreadMessages(user); + List pendingMessages = PersistenceManager.getInstance().getPendingMessages(user); for (Message msg : pendingMessages) { - System.out.println("Sending message " + msg.toCommonMessage()); - writeProxy.write(socketID, msg.toCommonMessage()); - msg.setReceivedDate(new Date()); - msg.setStatus(MessageStatus.RECEIVED); - PersistenceManager.getInstance().updateMessage(msg); + if (msg.getStatus() == MessageStatus.SENT) { + System.out.println("Sending message " + msg.toCommonMessage()); + writeProxy.write(socketID, msg.toCommonMessage()); + msg.setReceivedDate(new Date()); + msg.setStatus(MessageStatus.RECEIVED); + PersistenceManager.getInstance().updateMessage(msg); + } else { + var evt = new MessageStatusChangeEvent(msg.toCommonMessage()); + System.out.println("Sending messageStatusChangeEvent " + evt); + writeProxy.write(socketID, evt); + } } }