MessagesController.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.web;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.uoc.tfg.sel.repository.model.Message;
import org.uoc.tfg.sel.repository.model.MessageRecipient;
import org.uoc.tfg.sel.repository.model.User;
import org.uoc.tfg.sel.service.MessagingService;
import org.uoc.tfg.sel.service.UserService;
/**
* The Class MessagesController.
*
* @author Eduardo Rodriguez Carro
*/
@RestController
@CrossOrigin
@RequestMapping("/messages")
public class MessagesController {
/** The messaging service. */
@Autowired
private MessagingService messagingService;
/** The user service. */
@Autowired
private UserService userService;
/**
* Listado de usuarios por tipo.
*
* @param authentication the authentication
* @return the users all
* @throws Exception the exception
*/
@PreAuthorize("hasRole('ROLE_API_MESSAGES')")
@RequestMapping(value = "/all", method = RequestMethod.GET)
public ResponseEntity<List<Message>> getAllRootMessages(Authentication authentication) throws Exception {
User user = ControllerUtils.getUser(authentication);
List<Message> messages = messagingService.getAllRootMessages(user);
if(messages == null || messages.isEmpty()) {
return ResponseEntity.noContent().build();
}
return ResponseEntity.ok(messages);
}
/**
* Listado de usuarios por tipo.
*
* @param id the id
* @param authentication the authentication
* @return the users all
* @throws Exception the exception
*/
@PreAuthorize("hasRole('ROLE_API_MESSAGES')")
@RequestMapping(value = "/item/{id}/thread", method = RequestMethod.GET)
public ResponseEntity<List<Message>> getAllChildMessages(@PathVariable("id") Integer id,Authentication authentication) throws Exception {
User user = ControllerUtils.getUser(authentication);
List<Message> messages = messagingService.getChildMessages(user, id);
if(messages == null || messages.isEmpty()) {
return ResponseEntity.noContent().build();
}
return ResponseEntity.ok(messages);
}
/**
* Gets the users available to send.
*
* @param authentication the authentication
* @return the users available to send
* @throws Exception the exception
*/
@PreAuthorize("hasRole('ROLE_API_MESSAGES')")
@RequestMapping(value = "/recipients", method = RequestMethod.GET)
public ResponseEntity<List<User>> getUsersAvailableToSend(Authentication authentication) throws Exception {
User user = ControllerUtils.getUser(authentication);
List<User> users = userService.getRecipients(user);
return ResponseEntity.ok(users);
}
/**
* Checks if is user in message to.
*
* @param message the message
* @param id the id
* @return true, if is user in message to
*/
private boolean isUserInMessageTo(Message message,Integer id) {
for (MessageRecipient recipient : message.getTo()) {
if (recipient.getId().getUser().getId() == id) {
return true;
}
}
return false;
}
/**
* Gets the message.
*
* @param id the id
* @param authentication the authentication
* @return the message
* @throws Exception the exception
*/
@PreAuthorize("hasRole('ROLE_API_MESSAGES')")
@RequestMapping(value = "/item/{id}", method = RequestMethod.GET)
public ResponseEntity<Message> getMessage(@PathVariable("id") Integer id,Authentication authentication) throws Exception {
User user = ControllerUtils.getUser(authentication);
Message message = messagingService.getMessage(user, id);
if (message != null && (message.getFrom().getId() == user.getId() || isUserInMessageTo(message,user.getId()))) {
return ResponseEntity.ok(message);
}
return ResponseEntity.notFound().build();
}
/**
* Save message.
*
* @param message the message
* @param authentication the authentication
* @return the response entity
* @throws Exception the exception
*/
@PreAuthorize("hasRole('ROLE_API_MESSAGES')")
@RequestMapping(value = "/send", method = RequestMethod.POST)
public ResponseEntity<Message> saveMessage(@RequestBody Message message,Authentication authentication) throws Exception {
//TODO limitar a quien puede enviar mensajes segun los accesos por clases y alumnos
User user = ControllerUtils.getUser(authentication);
messagingService.save(user, message);
return ResponseEntity.accepted().build();
}
/**
* Sets the readed.
*
* @param id the id
* @param authentication the authentication
* @return the response entity
*/
@PreAuthorize("hasRole('ROLE_API_MESSAGES')")
@RequestMapping(value = "/readed/{id}", method = RequestMethod.POST)
public ResponseEntity<Boolean> setReaded(@PathVariable("id") Integer id, Authentication authentication) {
//TODO limitar a quien puede enviar mensajes segun los accesos por clases y alumnos
User user = ControllerUtils.getUser(authentication);
messagingService.setReaded(id, user);
return ResponseEntity.ok(true);
}
/**
* Sets the readed.
*
* @param id the id
* @param authentication the authentication
* @return the response entity
*/
@PreAuthorize("hasRole('ROLE_API_MESSAGES')")
@RequestMapping(value = "/pending", method = RequestMethod.GET)
public ResponseEntity<Integer> getPendingMessages(Authentication authentication) {
User user = ControllerUtils.getUser(authentication);
Integer messages = messagingService.getPendingMessages(user);
return ResponseEntity.ok(messages);
}
}