From cb3db3418390b1579353ac7d054a89839e0b31b5 Mon Sep 17 00:00:00 2001 From: zjayee Date: Tue, 21 Nov 2023 02:44:02 -0500 Subject: [PATCH 1/3] Add session id to message, save websocket messages to mongodb --- .../connect/drivers/MessageWebSocketAccess.java | 13 +++++++++++++ src/main/java/callhub/connect/entities/Message.java | 8 +++++--- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/main/java/callhub/connect/drivers/MessageWebSocketAccess.java b/src/main/java/callhub/connect/drivers/MessageWebSocketAccess.java index c3042e1..374daf6 100644 --- a/src/main/java/callhub/connect/drivers/MessageWebSocketAccess.java +++ b/src/main/java/callhub/connect/drivers/MessageWebSocketAccess.java @@ -1,5 +1,9 @@ package callhub.connect.drivers; +import callhub.connect.data_access.MessageRepository; +import callhub.connect.entities.Message; +import callhub.connect.entities.Sender; import com.google.gson.Gson; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.messaging.handler.annotation.DestinationVariable; import org.springframework.messaging.handler.annotation.MessageMapping; import org.springframework.messaging.handler.annotation.SendTo; @@ -13,6 +17,8 @@ @Controller public class MessageWebSocketAccess { + @Autowired + private MessageRepository messageRepository; private static final Gson gson = new Gson(); final static DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm:ss a", Locale.CANADA); @@ -20,6 +26,7 @@ public class MessageWebSocketAccess { @SendTo("/topic/message-employee/{sessionId}") public String sendMessageCustomer(@DestinationVariable String sessionId, String message) throws Exception { HashMap response = generateResponse(message); + sendResponseToDatabase(message, sessionId, Sender.CUSTOMER); return gson.toJson(response); } @@ -27,6 +34,7 @@ public String sendMessageCustomer(@DestinationVariable String sessionId, String @SendTo("/topic/message-customer/{sessionId}") public String sendMessageEmployee(@DestinationVariable String sessionId, String message) throws Exception { HashMap response = generateResponse(message); + sendResponseToDatabase(message, sessionId, Sender.EMPLOYEE); return gson.toJson(response); } @@ -40,4 +48,9 @@ private HashMap generateResponse(String message){ return response; } + private void sendResponseToDatabase(String content, String sessionId, Sender sender){ + Message message = new Message(content, sessionId, sender); + messageRepository.save(message); + } + } \ No newline at end of file diff --git a/src/main/java/callhub/connect/entities/Message.java b/src/main/java/callhub/connect/entities/Message.java index 4abff96..4961693 100644 --- a/src/main/java/callhub/connect/entities/Message.java +++ b/src/main/java/callhub/connect/entities/Message.java @@ -12,18 +12,20 @@ public class Message { private String content; private LocalDate timeStamp; private String userId; - + private String sessionId; private Sender sender; - public Message(String content, LocalDate timeStamp, Sender sender) { + public Message(String content, LocalDate timeStamp, String sessionId, Sender sender) { this.content = content; this.timeStamp = timeStamp; + this.sessionId = sessionId; this.sender = sender; } - public Message(String content, Sender sender) { + public Message(String content, String sessionId, Sender sender) { this.content = content; this.timeStamp = LocalDate.now(); + this.sessionId = sessionId; this.sender = sender; } From e3d8aded34565566aa1ece3d5d3170de9dd10181 Mon Sep 17 00:00:00 2001 From: Sarina Li <74844953+sarinali@users.noreply.github.com> Date: Thu, 23 Nov 2023 22:58:24 -0500 Subject: [PATCH 2/3] added some helper methods --- src/main/java/callhub/connect/entities/Message.java | 12 ++++++++++++ src/main/java/callhub/connect/entities/Session.java | 4 ++++ 2 files changed, 16 insertions(+) diff --git a/src/main/java/callhub/connect/entities/Message.java b/src/main/java/callhub/connect/entities/Message.java index 4abff96..a16b278 100644 --- a/src/main/java/callhub/connect/entities/Message.java +++ b/src/main/java/callhub/connect/entities/Message.java @@ -2,7 +2,10 @@ import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.Document; +import java.text.DateFormat; +import java.text.SimpleDateFormat; import java.time.LocalDate; +import java.util.Date; @Document("message") public class Message { @@ -34,4 +37,13 @@ public String getContent() { public void setContent(String content) { this.content = content; } + + private String getTimeStampString() { + DateFormat df = new SimpleDateFormat("HH:mm:ss a"); + return df.format(this.timeStamp); + } + + public String formattedMessage() { + return String.format("%s %s", this.getTimeStampString(), this.content); + } } diff --git a/src/main/java/callhub/connect/entities/Session.java b/src/main/java/callhub/connect/entities/Session.java index 7acdf03..26c8d2d 100644 --- a/src/main/java/callhub/connect/entities/Session.java +++ b/src/main/java/callhub/connect/entities/Session.java @@ -33,4 +33,8 @@ public String getCode() { return code; } + public ArrayList getMessages() { + return this.messages; + } + } From 759a73a67a10613c4f3ea0600126e841cb0d9cae Mon Sep 17 00:00:00 2001 From: zjayee Date: Fri, 24 Nov 2023 00:07:12 -0500 Subject: [PATCH 3/3] Moved message saving to data access class --- .../connect/CallhubconnectApplication.java | 7 ++++++ .../data_access/MessageDataAccess.java | 24 +++++++++++++++++++ .../drivers/MessageWebSocketAccess.java | 13 ++++------ 3 files changed, 36 insertions(+), 8 deletions(-) create mode 100644 src/main/java/callhub/connect/data_access/MessageDataAccess.java diff --git a/src/main/java/callhub/connect/CallhubconnectApplication.java b/src/main/java/callhub/connect/CallhubconnectApplication.java index 5edf822..7af2592 100644 --- a/src/main/java/callhub/connect/CallhubconnectApplication.java +++ b/src/main/java/callhub/connect/CallhubconnectApplication.java @@ -1,9 +1,11 @@ package callhub.connect; +import callhub.connect.data_access.MessageDataAccess; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @@ -40,4 +42,9 @@ public void addCorsMappings(CorsRegistry registry) { }; } + @Bean + public MessageDataAccess messageDataAccess(ApplicationContext context) { + return new MessageDataAccess(context); + } + } diff --git a/src/main/java/callhub/connect/data_access/MessageDataAccess.java b/src/main/java/callhub/connect/data_access/MessageDataAccess.java new file mode 100644 index 0000000..df12861 --- /dev/null +++ b/src/main/java/callhub/connect/data_access/MessageDataAccess.java @@ -0,0 +1,24 @@ +package callhub.connect.data_access; + +import callhub.connect.entities.Message; +import callhub.connect.entities.Sender; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.Bean; + +public class MessageDataAccess { + + private ApplicationContext context; + private MessageRepository messageRepository; + + public MessageDataAccess(ApplicationContext context) { + this.context = context; + this.messageRepository = context.getBean(MessageRepository.class); + } + + public void sendResponseToDatabase(String content, String sessionId, Sender sender) { + Message message = new Message(content, sessionId, sender); + messageRepository.save(message); + } +} diff --git a/src/main/java/callhub/connect/drivers/MessageWebSocketAccess.java b/src/main/java/callhub/connect/drivers/MessageWebSocketAccess.java index 6e2b45b..19888c8 100644 --- a/src/main/java/callhub/connect/drivers/MessageWebSocketAccess.java +++ b/src/main/java/callhub/connect/drivers/MessageWebSocketAccess.java @@ -1,4 +1,5 @@ package callhub.connect.drivers; +import callhub.connect.data_access.MessageDataAccess; import callhub.connect.data_access.MessageRepository; import callhub.connect.entities.Message; import callhub.connect.entities.Sender; @@ -17,15 +18,15 @@ @Controller public class MessageWebSocketAccess { - @Autowired - private MessageRepository messageRepository; private static final Gson gson = new Gson(); private MessageInteractor messageInteractor = new MessageInteractor(); + @Autowired + private MessageDataAccess messageDataAccess; @MessageMapping("/message-customer/{sessionId}") @SendTo("/topic/message-employee/{sessionId}") public String sendMessageCustomer(@DestinationVariable String sessionId, String message) throws Exception { - sendResponseToDatabase(message, sessionId, Sender.CUSTOMER); + messageDataAccess.sendResponseToDatabase(message, sessionId, Sender.CUSTOMER); HashMap response = messageInteractor.generateResponse(message); return gson.toJson(response); } @@ -33,15 +34,11 @@ public String sendMessageCustomer(@DestinationVariable String sessionId, String @MessageMapping("/message-employee/{sessionId}") @SendTo("/topic/message-customer/{sessionId}") public String sendMessageEmployee(@DestinationVariable String sessionId, String message) throws Exception { - sendResponseToDatabase(message, sessionId, Sender.EMPLOYEE); + messageDataAccess.sendResponseToDatabase(message, sessionId, Sender.EMPLOYEE); HashMap response = messageInteractor.generateResponse(message); return gson.toJson(response); } - private void sendResponseToDatabase(String content, String sessionId, Sender sender){ - Message message = new Message(content, sessionId, sender); - messageRepository.save(message); - } } \ No newline at end of file