Commit c3927661 authored by David Fernandez Lobon's avatar David Fernandez Lobon

Notification

parent 12a79968
package adt.mainnode.controller;
import adt.mainnode.entity.Credentials;
import adt.mainnode.entity.Notification;
import adt.mainnode.repository.CredentialsRepository;
import adt.mainnode.service.NotificationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletResponse;
@RestController
@RequestMapping("notifications")
@Api(
value = "/notifications",//"/asset",
produces = MediaType.APPLICATION_JSON_VALUE,
tags = {"Notifications"}
)
@Slf4j
public class NotificationController {
private final CredentialsRepository credentialsRepository;
private final NotificationService notificationService;
@Autowired
public NotificationController(CredentialsRepository credentialsRepository, NotificationService notificationService){
this.credentialsRepository = credentialsRepository;
this.notificationService = notificationService;
}
@CrossOrigin
@ApiOperation(value = "Save a new set of notification", response = Boolean.class)
@ResponseStatus(HttpStatus.CREATED)
@PostMapping(produces = MediaType.APPLICATION_JSON_VALUE)
boolean saveAssets(HttpServletResponse response, @RequestBody Notification notification, @RequestParam String token) {
Credentials credential = credentialsRepository.findByToken(token);
if(credential != null && credential.getStatus().equals("OPERATIVE")) {
if(credential.getAvailableEndpoint().contains("(POST) notifications/")) {
log.info("New notification received");
notificationService.saveNotification(notification, credential);
return true;
}
}
return false;
}
}
......@@ -92,6 +92,7 @@ public class WebController {
arr.add("(POST) nlp/");
arr.add("(GET) virt_query/");
arr.add("(POST) event/");
arr.add("(POST) notifications/");
arr.add("Visualization Service");
user_types = new ArrayList<String>();
......
package adt.mainnode.dto;
import adt.mainnode.entity.Notification;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Data
@Entity
public class NotificationDto {
@Id
@GeneratedValue
private Long id;
@JsonProperty("Node_Id")
private String node_Id;
@JsonProperty("Ip")
private String ip;
@JsonProperty("port")
private String port;
@JsonProperty("timestamp")
private double timestamp;
public static NotificationDto createNotification (Notification notification, String node_Id){
NotificationDto result = new NotificationDto();
result.setIp(notification.getIp());
result.setPort(notification.getPort());
result.setTimestamp(notification.getTimestamp());
result.setNode_Id(node_Id);
return result;
}
}
package adt.mainnode.entity;
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Data
@Entity
public class Notification {
@Id
@GeneratedValue
private Long id;
@JsonProperty("Ip")
private String ip;
@JsonProperty("port")
private String port;
@JsonProperty("timestamp")
private double timestamp;
}
package adt.mainnode.repository;
import adt.mainnode.dto.NotificationDto;
import adt.mainnode.entity.Notification;
import org.springframework.data.jpa.repository.JpaRepository;
public interface NotificationRepository extends JpaRepository<NotificationDto,String> {
}
package adt.mainnode.service;
import adt.mainnode.dto.NotificationDto;
import adt.mainnode.entity.Credentials;
import adt.mainnode.entity.Node;
import adt.mainnode.entity.Notification;
import adt.mainnode.repository.NodeRepository;
import adt.mainnode.repository.NotificationRepository;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
@Slf4j
public class NotificationService {
private final NodeRepository nodeRepository;
private final NotificationRepository notificationRepository;
@Autowired
public NotificationService(NodeRepository nodeRepository, NotificationRepository notificationRepository){
this.nodeRepository = nodeRepository;
this.notificationRepository = notificationRepository;
}
public void saveNotification(Notification notification, Credentials credential){
Node node = nodeRepository.findByNodeId(credential.getName());
NotificationDto notificationDto = NotificationDto.createNotification(notification, node.getNodeId());
//TODO Hacer en la web la visualizacion de la notificacion
notificationRepository.save(notificationDto);
}
}
logging:
file: logs/mainNode.log
debug: true
#debug: true
spring:
datasource:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment