Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…ackend into end_session
  • Loading branch information
zjayee committed Dec 1, 2023
2 parents 81a97ad + 2193c13 commit f26a6a3
Show file tree
Hide file tree
Showing 18 changed files with 324 additions and 83 deletions.
9 changes: 0 additions & 9 deletions src/main/java/callhub/connect/CallhubconnectApplication.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
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;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
Expand Down Expand Up @@ -42,9 +38,4 @@ public void addCorsMappings(CorsRegistry registry) {
};
}

@Bean
public MessageDataAccess messageDataAccess(ApplicationContext context) {
return new MessageDataAccess(context);
}

}
12 changes: 0 additions & 12 deletions src/main/java/callhub/connect/controllers/ApiController.java

This file was deleted.

39 changes: 35 additions & 4 deletions src/main/java/callhub/connect/controllers/FileController.java
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
package callhub.connect.controllers;
import callhub.connect.data_access.DataAccess;
import callhub.connect.data_access.DocumentRepository;
import callhub.connect.data_access.LocalDataAccess;
import callhub.connect.data_access.NetworkDataAccess;
import callhub.connect.data_access.*;
import callhub.connect.entities.FileDocument;
import callhub.connect.entities.Session;
import callhub.connect.entities.exceptions.FileLimitExceededException;
import org.springframework.http.MediaType;
import org.bson.types.Binary;
Expand All @@ -24,8 +22,11 @@ public class FileController {

@Autowired
public DocumentRepository documentRepository;
@Autowired
public SessionRepository sessionRepository;
HttpHeaders headers = new HttpHeaders();
DataAccess dataAccessObject;
// deprecated only use for testing purposes
@GetMapping("/upload_local")
public ResponseEntity<String> uploadPDFLocal(@RequestParam("name") String name) {
String currentDirectory = System.getProperty("user.dir");
Expand All @@ -45,6 +46,7 @@ public ResponseEntity<String> uploadPDFLocal(@RequestParam("name") String name)
return new ResponseEntity<>("Uploaded! " + result.getId(), headers, HttpStatus.OK);
}

// deprecated, only use for testing purposes
@PostMapping("/upload_network")
public ResponseEntity<String> uploadFile(@RequestParam("file") MultipartFile file, @RequestParam("name") String name) {
dataAccessObject = new NetworkDataAccess(file);
Expand All @@ -54,6 +56,8 @@ public ResponseEntity<String> uploadFile(@RequestParam("file") MultipartFile fil
data = dataAccessObject.serializePDF();
result = documentRepository.save(new FileDocument(name, data, LocalDate.now()));
return new ResponseEntity<>(result.getId(), headers, HttpStatus.OK);
} catch (FileLimitExceededException e) {
return new ResponseEntity<>("File is too large.", headers, HttpStatus.PAYLOAD_TOO_LARGE);
} catch (IOException e) {
return new ResponseEntity<>(e.getMessage(), headers, HttpStatus.BAD_REQUEST);
}
Expand All @@ -72,4 +76,31 @@ public ResponseEntity<Object> findDocumentByID(@PathVariable String id) {
return new ResponseEntity<>(encodedData, headers, HttpStatus.OK);
}
}

// saves PDF to documents collection and links PDF id to session
@PostMapping("/session_add_pdf")
public ResponseEntity<Object> addPDFToSession(@RequestParam("file") MultipartFile file, @RequestParam("name") String name, @RequestParam("session") String code) {
boolean sessionExists = sessionRepository.existsById(code);
if (!sessionExists) {
new ResponseEntity<>("This session is inactive or does not exist.", headers, HttpStatus.NOT_FOUND);
}

dataAccessObject = new NetworkDataAccess(file);
FileDocument result = new FileDocument();
Binary data;
try {
data = dataAccessObject.serializePDF();
result = documentRepository.save(new FileDocument(name, data, LocalDate.now()));
} catch (IOException e) {
new ResponseEntity<>("An issue occurred.", headers, HttpStatus.BAD_REQUEST);
}
try {
Session currentSession = sessionRepository.getSessionsByActiveAndCode(true, code);
currentSession.addDocument(result.getId());
sessionRepository.save(currentSession);
return new ResponseEntity<>(result.getId(), headers, HttpStatus.OK);
} catch (Exception e) {
return new ResponseEntity<>(e.getMessage(), headers, HttpStatus.BAD_REQUEST);
}
}
}
38 changes: 0 additions & 38 deletions src/main/java/callhub/connect/data_access/MessageDataAccess.java

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package callhub.connect.data_access;

import callhub.connect.entities.Message;
import callhub.connect.entities.Sender;
import callhub.connect.entities.Session;
import callhub.connect.use_case.message.MessageDataAccessInterface;
import callhub.connect.use_case.session.SessionDataAccessInterface;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

Expand All @@ -14,10 +18,13 @@ public class MessageDataAccessObject implements MessageDataAccessInterface {

@Autowired
private final MessageRepository messageRepository;
@Autowired
private final SessionDataAccessInterface sessionDataAccessInterface;
final static DateTimeFormatter TIME_FORMATTER = DateTimeFormatter.ofPattern("HH:mm:ss a", Locale.CANADA);

public MessageDataAccessObject(MessageRepository messageRepository) {
public MessageDataAccessObject(MessageRepository messageRepository, SessionDataAccessInterface sessionDataAccessInterface) {
this.messageRepository = messageRepository;
this.sessionDataAccessInterface = sessionDataAccessInterface;
}

public HashMap<String, String> generateResponse(String message){
Expand All @@ -29,4 +36,16 @@ public HashMap<String, String> generateResponse(String message){

return response;
}

public void sendResponseToDatabase(String content, String sessionId, Sender sender) {
Message message = new Message(content, sessionId, sender);
Message result = messageRepository.save(message);
// add message to session
try{
sessionDataAccessInterface.addMessageToSession(sessionId, result);
}catch (Exception e){
System.out.println(e.getMessage());
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,4 @@

import java.util.List;
public interface MessageRepository extends MongoRepository<Message, String>{

@Query("{userId: '?0'}")
public List<Message> findByUserId(String userId);

}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package callhub.connect.data_access;

import callhub.connect.entities.Message;
import callhub.connect.entities.Session;
import callhub.connect.use_case.session.SessionDataAccessInterface;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -44,6 +45,18 @@ public HashMap<String, String> endSession(String code) {
return responseBody;
}

/**
* Adds message to the session with sessionId in the database.
*
* @param sessionId session id
* @param message message to be added
*/
public void addMessageToSession(String sessionId, Message message){
Session currentSession = sessionRepository.findById(sessionId).orElseThrow();
currentSession.addMessage(message);
sessionRepository.save(currentSession);
}

private String generateSessionCode(){
final String ALLOWED_CHAR = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
SecureRandom random = new SecureRandom();
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/callhub/connect/entities/FileDocument.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class FileDocument {
private String id;

public String name;
private final Binary content;
private Binary content = null;
public LocalDate timeStamp;

public FileDocument(String name, Binary content, LocalDate timeStamp) {
Expand All @@ -20,6 +20,8 @@ public FileDocument(String name, Binary content, LocalDate timeStamp) {
this.timeStamp = timeStamp;
}

public FileDocument() {}

public String getId() {
return this.id;
}
Expand Down
32 changes: 22 additions & 10 deletions src/main/java/callhub/connect/entities/Message.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,23 @@
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;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Document("message")
public class Message {

@Id
private String id;
private String content;
private LocalDate timeStamp;
private LocalDateTime timeStamp;
private String userId;
private String sessionId;
private Sender sender;

public Message(){}

public Message(String content, LocalDate timeStamp, String sessionId, Sender sender) {
public Message(String content, LocalDateTime timeStamp, String sessionId, Sender sender) {
this.content = content;
this.timeStamp = timeStamp;
this.sessionId = sessionId;
Expand All @@ -29,7 +27,7 @@ public Message(String content, LocalDate timeStamp, String sessionId, Sender sen

public Message(String content, String sessionId, Sender sender) {
this.content = content;
this.timeStamp = LocalDate.now();
this.timeStamp = LocalDateTime.now();
this.sessionId = sessionId;
this.sender = sender;
}
Expand All @@ -42,12 +40,26 @@ public void setContent(String content) {
this.content = content;
}

/**
* Returns the date the message was sent as a formatted string.
*
* @return A formatted string representing the date of the message in the format "Month dd, yyyy".
*/
public String getDateString() {
DateTimeFormatter dateFormatter = DateTimeFormatter.ofPattern("LLLL dd, yyyy");
return this.timeStamp.format(dateFormatter);
}

private String getTimeStampString() {
DateFormat df = new SimpleDateFormat("HH:mm:ss a");
return df.format(this.timeStamp);
DateTimeFormatter timeFormatter = DateTimeFormatter.ofPattern("HH:mm:ss");
return this.timeStamp.format(timeFormatter);
}

/**
* Returns a string that formats the message with the time it was sent, its sender and its content.
* @return a formatted message.
*/
public String formattedMessage() {
return String.format("%s %s", this.getTimeStampString(), this.content);
return String.format("%s %s: %s", this.getTimeStampString(), this.sender.name(), this.content);
}
}
8 changes: 7 additions & 1 deletion src/main/java/callhub/connect/entities/Session.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ public class Session {

private boolean active;
private ArrayList<Message> messages;
// private ArrayList<> documents;
private ArrayList<String> documents;


public Session(boolean active, String code){
this.active = active;
this.messages = new ArrayList<Message>();
this.startTime = new java.util.Date(System.currentTimeMillis());
this.code = code;
this.documents = new ArrayList<String>();
}

public String getId() {
Expand All @@ -33,6 +35,10 @@ public String getCode() {
return code;
}

public void addDocument(String fileID) {
documents.add(fileID);
}

public ArrayList<Message> getMessages() {
return this.messages;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package callhub.connect.interface_adapter.message;

import callhub.connect.entities.Sender;
import callhub.connect.use_case.message.MessageInputBoundary;
import callhub.connect.use_case.message.MessageInputData;
import callhub.connect.use_case.message.MessageInteractor;
Expand All @@ -22,15 +23,15 @@ public class MessageController {
@MessageMapping("/message-customer/{sessionId}")
@SendTo("/topic/message-employee/{sessionId}")
public String sendMessageCustomer(@DestinationVariable String sessionId, String message) throws Exception {
MessageInputData inputData = new MessageInputData(sessionId, message);
MessageInputData inputData = new MessageInputData(sessionId, message, Sender.CUSTOMER);
HashMap<String, String> response = messageInteractor.newMessage(inputData);
return gson.toJson(response);
}

@MessageMapping("/message-employee/{sessionId}")
@SendTo("/topic/message-customer/{sessionId}")
public String sendMessageEmployee(@DestinationVariable String sessionId, String message) throws Exception {
MessageInputData inputData = new MessageInputData(sessionId, message);
MessageInputData inputData = new MessageInputData(sessionId, message, Sender.EMPLOYEE);
HashMap<String, String> response = messageInteractor.newMessage(inputData);
return gson.toJson(response);
}
Expand Down
Loading

0 comments on commit f26a6a3

Please sign in to comment.