public String insertMessage(MessageRequest messageRequest) throws Exception { MessageDTO messageDTO = new MessageDTO(); messageDTO.setMessageId(MessageUtil.generateUniqueKey()); messageDTO.setUsername(messageRequest.getUsername()); messageDTO.setText(messageRequest.getText()); messageDTO.setExpirationDate(MessageUtil.getExpirationDate(messageRequest.getTimeout())); timer = new Timer(); timer.schedule( new ExpireTask(messageDTO, timer), MessageUtil.getTimeout(messageRequest.getTimeout())); return messageDao.insertUnExpiredMessage(messageDTO); }
public MessageResponse getMessageById(String messageId) { messageResponse = new MessageResponse(); MessageDTO messageDTO = messageDao.getMessage(messageId); if (messageDTO != null) { messageResponse.setMessageId(messageDTO.getMessageId()); messageResponse.setUsername(messageDTO.getUsername()); messageResponse.setText(messageDTO.getText()); messageResponse.setExpirationDate(messageDTO.getExpirationDate()); return messageResponse; } else { return null; } }
public List<MessageContentResponse> getUnExpiredMessagesByUsername(String username) throws Exception { List<MessageContentResponse> messageContentResponses = new ArrayList<MessageContentResponse>(); List<MessageDTO> messages = messageDao.getMessagesByUsername(username); if (messages != null) { expireMessages(messages); for (MessageDTO message : messages) { MessageContentResponse messageContentResponse = new MessageContentResponse(); messageContentResponse.setId(message.getMessageId()); messageContentResponse.setText(message.getText()); messageContentResponses.add(messageContentResponse); } } return messageContentResponses; }