/** * "Reads" a message for a user by returning it and setting the wasRead variabel of the message to * true in the database. If the user isn't the receiver, the message isn't read and null is * returned * * @param messageId a Long defining the message we want to read * @param user the user who wants to read the message * @return the Message given by the id with wasRead = true if the user is the receiver of the * message or null if the user isn't the reciever of the message */ public Message read(Long messageId, User user) { Message opened = messageDao.findOne(messageId); if (opened == null || !opened.getReceiver().equals(user)) return null; opened.setWasRead(true); messageDao.save(opened); return opened; }
/** * "Sends" a message by saving it to the database and setting the send date to now * * @param message SendDate may not be set, will be set to now. * @return the message saved in the database (which is therefore "sent") */ private Message send(Message message) { assert message.getSendDate() == null; if (message.getReceiver().equals(message.getSender())) throw new InvalidUserException("You cannot send yourself a message"); message.setSendDate(new Date()); message = messageDao.save(message); mailService.sendMessageNotificationMail(message); return message; }
/** * Returns all messages the user has received, ordered form newest send date to oldest. * * @param user from which we want to get all messages * @return an ordered list of messages, form newest send date to oldest. */ @Transactional public List<Message> getOrderedMessagesList(User user) { List<Message> messages = (List<Message>) messageDao.findAllByReceiver(user); Collections.sort(messages, MessageDateComparator); return messages; }