From cffe9b936f7c095fe03bb3e13150c073a35450ae Mon Sep 17 00:00:00 2001 From: DieGurke <55625494+DieGurke@users.noreply.github.com> Date: Sat, 6 Jun 2020 15:42:10 +0200 Subject: [PATCH] Make GroupMessage extend Message, prepared GroupMessageStatus processing --- src/main/java/envoy/server/Startup.java | 1 + .../java/envoy/server/data/GroupMessage.java | 170 +----------------- src/main/java/envoy/server/data/Message.java | 44 +++-- .../processors/GroupMessageProcessor.java | 31 +++- .../processors/LoginCredentialProcessor.java | 6 +- 5 files changed, 62 insertions(+), 190 deletions(-) diff --git a/src/main/java/envoy/server/Startup.java b/src/main/java/envoy/server/Startup.java index bf30945..0811bbb 100755 --- a/src/main/java/envoy/server/Startup.java +++ b/src/main/java/envoy/server/Startup.java @@ -36,6 +36,7 @@ public class Startup { Set> processors = new HashSet<>(); processors.add(new LoginCredentialProcessor()); processors.add(new MessageProcessor()); + processors.add(new GroupMessageProcessor()); processors.add(new MessageStatusChangeProcessor()); processors.add(new UserStatusChangeProcessor()); processors.add(new IDGeneratorRequestProcessor()); diff --git a/src/main/java/envoy/server/data/GroupMessage.java b/src/main/java/envoy/server/data/GroupMessage.java index 19233d1..e397c8e 100644 --- a/src/main/java/envoy/server/data/GroupMessage.java +++ b/src/main/java/envoy/server/data/GroupMessage.java @@ -1,9 +1,10 @@ package envoy.server.data; -import java.util.Date; import java.util.Map; -import javax.persistence.*; +import javax.persistence.ElementCollection; +import javax.persistence.Entity; +import javax.persistence.Table; import envoy.data.MessageBuilder; @@ -18,34 +19,11 @@ import envoy.data.MessageBuilder; @Entity @Table(name = "group_messages") -public class GroupMessage { - - @Id - private long id; - - @ManyToOne(cascade = CascadeType.PERSIST) - private User sender; - - @ManyToOne(cascade = CascadeType.PERSIST) - private Group group; - - @Temporal(TemporalType.TIMESTAMP) - private Date creationDate; - - @Temporal(TemporalType.TIMESTAMP) - private Date receivedDate; - - @Temporal(TemporalType.TIMESTAMP) - private Date readDate; +public class GroupMessage extends Message { @ElementCollection private Map memberMessageStatus; - private String text; - private envoy.data.Message.MessageStatus status; - private byte[] attachment; - private boolean forwarded; - /** * The constructor for a database object. * @@ -61,19 +39,8 @@ public class GroupMessage { * @since Envoy Server Standalone v0.1-beta */ public GroupMessage(envoy.data.GroupMessage groupMessage) { - PersistenceManager persistenceManager = PersistenceManager.getInstance(); - id = groupMessage.getID(); - status = groupMessage.getStatus(); - text = groupMessage.getText(); - creationDate = groupMessage.getCreationDate(); - receivedDate = groupMessage.getReceivedDate(); - readDate = groupMessage.getReadDate(); - sender = persistenceManager.getUserByID(groupMessage.getSenderID()); - group = persistenceManager.getGroupByID(groupMessage.getRecipientID()); - forwarded = groupMessage.isForwarded(); + super(groupMessage); memberMessageStatus = groupMessage.getMemberStatuses(); - // TODO: attachment = groupMessage.getAttachment().toByteArray();DOES NOT WORK - // YET } /** @@ -84,91 +51,20 @@ public class GroupMessage { * groupMessage * @since Envoy Server Standalone v0.1-beta */ - public envoy.data.GroupMessage toCommonGroupMessage() { + @Override + public envoy.data.GroupMessage toCommon() { // TODO: Attachment - envoy.data.GroupMessage groupMessage = new MessageBuilder(sender.getID(), group.getID(), id).setDate(creationDate) + envoy.data.GroupMessage groupMessage = new MessageBuilder(sender.getID(), recipient.getID(), id).setDate(creationDate) .setForwarded(forwarded) .setStatus(status) .setText(text) // .setAttachment(attachment) TODO make this work - .buildGroupMessage(group.toCommon(), memberMessageStatus); + .buildGroupMessage((envoy.data.Group) recipient.toCommon(), memberMessageStatus); groupMessage.setReceivedDate(receivedDate); groupMessage.setReadDate(readDate); return groupMessage; } - /** - * @return the id - * @since Envoy Server Standalone v0.1-beta - */ - public long getId() { return id; } - - /** - * @param id the id to set - * @since Envoy Server Standalone v0.1-beta - */ - public void setId(long id) { this.id = id; } - - /** - * @return the sender - * @since Envoy Server Standalone v0.1-beta - */ - public User getSender() { return sender; } - - /** - * @param sender the sender to set - * @since Envoy Server Standalone v0.1-beta - */ - public void setSender(User sender) { this.sender = sender; } - - /** - * @return the group - * @since Envoy Server Standalone v0.1-beta - */ - public Group getGroup() { return group; } - - /** - * @param group the group to set - * @since Envoy Server Standalone v0.1-beta - */ - public void setGroup(Group group) { this.group = group; } - - /** - * @return the creationDate - * @since Envoy Server Standalone v0.1-beta - */ - public Date getCreationDate() { return creationDate; } - - /** - * @param creationDate the creationDate to set - * @since Envoy Server Standalone v0.1-beta - */ - public void setCreationDate(Date creationDate) { this.creationDate = creationDate; } - - /** - * @return the receivedDate - * @since Envoy Server Standalone v0.1-beta - */ - public Date getReceivedDate() { return receivedDate; } - - /** - * @param receivedDate the receivedDate to set - * @since Envoy Server Standalone v0.1-beta - */ - public void setReceivedDate(Date receivedDate) { this.receivedDate = receivedDate; } - - /** - * @return the readDate - * @since Envoy Server Standalone v0.1-beta - */ - public Date getReadDate() { return readDate; } - - /** - * @param readDate the readDate to set - * @since Envoy Server Standalone v0.1-beta - */ - public void setReadDate(Date readDate) { this.readDate = readDate; } - /** * @return the memberMessageStatus * @since Envoy Server Standalone v0.1-beta @@ -182,52 +78,4 @@ public class GroupMessage { public void setMemberMessageStatus(Map memberMessageStatus) { this.memberMessageStatus = memberMessageStatus; } - - /** - * @return the text - * @since Envoy Server Standalone v0.1-beta - */ - public String getText() { return text; } - - /** - * @param text the text to set - * @since Envoy Server Standalone v0.1-beta - */ - public void setText(String text) { this.text = text; } - - /** - * @return the status - * @since Envoy Server Standalone v0.1-beta - */ - public envoy.data.Message.MessageStatus getStatus() { return status; } - - /** - * @param status the status to set - * @since Envoy Server Standalone v0.1-beta - */ - public void setStatus(envoy.data.Message.MessageStatus status) { this.status = status; } - - /** - * @return the attachment - * @since Envoy Server Standalone v0.1-beta - */ - public byte[] getAttachment() { return attachment; } - - /** - * @param attachment the attachment to set - * @since Envoy Server Standalone v0.1-beta - */ - public void setAttachment(byte[] attachment) { this.attachment = attachment; } - - /** - * @return the forwarded - * @since Envoy Server Standalone v0.1-beta - */ - public boolean isForwarded() { return forwarded; } - - /** - * @param forwarded the forwarded to set - * @since Envoy Server Standalone v0.1-beta - */ - public void setForwarded(boolean forwarded) { this.forwarded = forwarded; } } diff --git a/src/main/java/envoy/server/data/Message.java b/src/main/java/envoy/server/data/Message.java index a21e6b4..128b122 100755 --- a/src/main/java/envoy/server/data/Message.java +++ b/src/main/java/envoy/server/data/Message.java @@ -1,10 +1,17 @@ package envoy.server.data; -import static javax.persistence.CascadeType.ALL; - import java.util.Date; -import javax.persistence.*; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.Inheritance; +import javax.persistence.InheritanceType; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import javax.persistence.NamedQuery; +import javax.persistence.Table; +import javax.persistence.Temporal; +import javax.persistence.TemporalType; import envoy.data.MessageBuilder; @@ -23,6 +30,7 @@ import envoy.data.MessageBuilder; */ @Entity @Table(name = "messages") +@Inheritance(strategy = InheritanceType.SINGLE_TABLE) @NamedQuery( name = Message.getPending, query = "SELECT m FROM Message m WHERE (m.recipient = :user AND m.status = envoy.data.Message$MessageStatus.SENT) " @@ -40,29 +48,29 @@ public class Message { public static final String getPending = "Message.getPending"; @Id - private long id; + protected long id; - @ManyToOne(cascade = ALL) + @ManyToOne @JoinColumn - private User sender; + protected User sender; - @ManyToOne(cascade = ALL) + @ManyToOne @JoinColumn - private User recipient; + protected Contact recipient; @Temporal(TemporalType.TIMESTAMP) - private Date creationDate; + protected Date creationDate; @Temporal(TemporalType.TIMESTAMP) - private Date receivedDate; + protected Date receivedDate; @Temporal(TemporalType.TIMESTAMP) - private Date readDate; + protected Date readDate; - private String text; - private envoy.data.Message.MessageStatus status; - private byte[] attachment; - private boolean forwarded; + protected String text; + protected envoy.data.Message.MessageStatus status; + protected byte[] attachment; + protected boolean forwarded; /** * The constructor for a database object. @@ -87,7 +95,7 @@ public class Message { receivedDate = message.getReceivedDate(); readDate = message.getReadDate(); sender = persistenceManager.getUserByID(message.getSenderID()); - recipient = persistenceManager.getUserByID(message.getRecipientID()); + recipient = persistenceManager.getContactByID(message.getRecipientID()); forwarded = message.isForwarded(); // TODO: attachment = message.getAttachment().toByteArray();DOES NOT WORK YET } @@ -99,7 +107,7 @@ public class Message { * message * @since Envoy Server Standalone v0.1-alpha */ - public envoy.data.Message toCommonMessage() { + public envoy.data.Message toCommon() { // TODO: Attachment envoy.data.Message message = new MessageBuilder(sender.getID(), recipient.getID(), id).setText(text) .setDate(creationDate) @@ -142,7 +150,7 @@ public class Message { * @return the recipient of a {link envoy.data.Message} * @since Envoy Server Standalone v0.1-alpha */ - public User getRecipient() { return recipient; } + public Contact getRecipient() { return recipient; } /** * @param recipient the recipient to set diff --git a/src/main/java/envoy/server/processors/GroupMessageProcessor.java b/src/main/java/envoy/server/processors/GroupMessageProcessor.java index 2b50e24..e1815b3 100644 --- a/src/main/java/envoy/server/processors/GroupMessageProcessor.java +++ b/src/main/java/envoy/server/processors/GroupMessageProcessor.java @@ -2,6 +2,8 @@ package envoy.server.processors; import java.io.IOException; +import javax.persistence.EntityExistsException; + import envoy.data.GroupMessage; import envoy.data.Message.MessageStatus; import envoy.event.MessageStatusChangeEvent; @@ -31,19 +33,26 @@ public class GroupMessageProcessor implements ObjectProcessor { } final var members = PersistenceManager.getInstance().getGroupByID(groupMessage.getRecipientID()).getContacts(); - members.forEach(user -> { sendToMember(connectionManager, groupMessage, user.getID(), writeProxy); }); - // TODO if all member message statuses are RECEIVED, send - // messageStatusChangeEvent to all members (we need to know if the sender is in - // the member list as well. If so insert continue on the loop one line above. If - // not we need to send the messageStatusChangeEvent separately to the sender). + for (long i = 0; i < groupMessage.getMemberStatuses().keySet().size(); i++) { + groupMessage.getMemberStatuses().replace(i, MessageStatus.SENT); + } + members.forEach(user -> { setMemberStatus(connectionManager, groupMessage, user.getID()); }); - // TODO Implement separate DB entity for groupMesages so we can persist here. + // Checks if all memberMessageStatuses are RECEIVED and if so sets the + // groupMessage Status to RECEIVED. + if (!groupMessage.getMemberStatuses().containsValue(MessageStatus.SENT)) groupMessage.setStatus(MessageStatus.RECEIVED); + + members.forEach(user -> { sendToMember(connectionManager, groupMessage, user.getID(), writeProxy); }); + + try { + PersistenceManager.getInstance().addMessage(new envoy.server.data.Message(groupMessage)); + } catch (EntityExistsException e) { + System.err.println("Received a groupMessage with an id that already exists"); + } } private void sendToMember(ConnectionManager connectionManager, GroupMessage groupMessage, long memberID, ObjectWriteProxy writeProxy) { if (connectionManager.isOnline(memberID)) try { - // Update the message status of the member to RECEIVED - groupMessage.getMemberStatuses().replace(memberID, MessageStatus.RECEIVED); // If recipient is online, send the groupMessage directly writeProxy.write(connectionManager.getSocketId(memberID), groupMessage); } catch (IOException e) { @@ -52,6 +61,12 @@ public class GroupMessageProcessor implements ObjectProcessor { } } + private void setMemberStatus(ConnectionManager connectionManager, GroupMessage groupMessage, long memberID) { + if (connectionManager.isOnline(memberID)) + // Update the message status of the member to RECEIVED + groupMessage.getMemberStatuses().replace(memberID, MessageStatus.RECEIVED); + } + @Override public Class getInputClass() { return GroupMessage.class; } } diff --git a/src/main/java/envoy/server/processors/LoginCredentialProcessor.java b/src/main/java/envoy/server/processors/LoginCredentialProcessor.java index 5bc556f..bae3ca0 100755 --- a/src/main/java/envoy/server/processors/LoginCredentialProcessor.java +++ b/src/main/java/envoy/server/processors/LoginCredentialProcessor.java @@ -58,13 +58,13 @@ public class LoginCredentialProcessor implements ObjectProcessor pendingMessages = PersistenceManager.getInstance().getPendingMessages(user); for (Message msg : pendingMessages) if (msg.getStatus() == MessageStatus.SENT) { - System.out.println("Sending message " + msg.toCommonMessage()); - writeProxy.write(socketID, msg.toCommonMessage()); + System.out.println("Sending message " + msg.toCommon()); + writeProxy.write(socketID, msg.toCommon()); msg.setReceivedDate(new Date()); msg.setStatus(MessageStatus.RECEIVED); PersistenceManager.getInstance().updateMessage(msg); } else { - var evt = new MessageStatusChangeEvent(msg.toCommonMessage()); + var evt = new MessageStatusChangeEvent(msg.toCommon()); System.out.println("Sending messageStatusChangeEvent " + evt); writeProxy.write(socketID, evt); }