MessagingService.java
/**
* TFG 75.678 - TFG Desarrollo web 2020 e-Learning for Schools
* Copyright (C) 2020 Eduardo Rodriguez Carro
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.uoc.tfg.sel.service;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.uoc.tfg.sel.repository.MessageRecipientRepository;
import org.uoc.tfg.sel.repository.MessageRepository;
import org.uoc.tfg.sel.repository.model.Message;
import org.uoc.tfg.sel.repository.model.MessageRecipient;
import org.uoc.tfg.sel.repository.model.User;
/**
*
* The Class MessagingService.
* @author Eduardo Rodriguez Carro
*/
@Service
public class MessagingService {
/** The message repository. */
@Autowired
private MessageRepository messageRepository;
/** The message recipient repository. */
@Autowired
private MessageRecipientRepository messageRecipientRepository;
/**
* Walk.
*
* @param messages the messages
* @return the list
*/
private List<Message> walk(List<Message> messages){
for(Message m : messages) {
lazyComplete(m);
}
return messages;
}
/**
* Lazy complete.
*
* @param message the message
*/
private void lazyComplete(Message message) {
message.getFrom();
List<MessageRecipient> recipients = messageRecipientRepository.findByMessageId(message.getId());
message.setTo(recipients);
}
/**
*
*
* @param user the user
* @return the all root messages
*/
@Transactional(readOnly = true)
public List<Message> getAllRootMessages(User user){
return walk(messageRepository.getRootMessagesByUserId(user.getId()));
}
/**
*
*
* @param user the user
* @param parentId the parent id
* @return the child messages
*/
@Transactional(readOnly = true)
public List<Message> getChildMessages(User user,Integer parentId){
return walk(messageRepository.getChildMessagesByUserId(user.getId(), parentId));
}
/**
*
*
* @param id the id
* @return the child messages
*/
@Transactional(readOnly = true)
public Message getMessage(User user,Integer id){
Message message = null;
Optional<Message> optMessage = messageRepository.findById(id);
if(optMessage.isPresent()) {
message = optMessage.get();
lazyComplete(message);
}
return message;
}
@Transactional(readOnly = false)
public void save(User user, Message message) {
List<MessageRecipient> recipients = message.getTo();
message.setTo(null);
messageRepository.save(message);
Integer id = message.getId();
for( MessageRecipient recipient: recipients) {
recipient.getId().setMessageId(id);
messageRecipientRepository.save(recipient);
}
}
@Transactional(readOnly = false)
public void setReaded(Integer id, User user) {
messageRecipientRepository.updateDate(id,user.getId(), new Date());
}
@Transactional(readOnly = false)
public Integer getPendingMessages(User user) {
return messageRecipientRepository.getUnreadedItems(user.getId());
}
}