1
|
package eu.dnetlib.uoaadmintools.controllers;
|
2
|
|
3
|
import eu.dnetlib.uoaadmintools.dao.NotificationsDAO;
|
4
|
import eu.dnetlib.uoaadmintools.entities.Notifications;
|
5
|
import eu.dnetlib.uoaadmintoolslibrary.handlers.ContentNotFoundException;
|
6
|
import eu.dnetlib.uoaadmintoolslibrary.dao.PortalDAO;
|
7
|
import org.apache.log4j.Logger;
|
8
|
import org.springframework.beans.factory.annotation.Autowired;
|
9
|
import org.springframework.web.bind.annotation.*;
|
10
|
|
11
|
import java.util.List;
|
12
|
|
13
|
/**
|
14
|
* Created by argirok on 6/7/2018.
|
15
|
*/
|
16
|
@RestController
|
17
|
@CrossOrigin(origins = "*")
|
18
|
public class NotificationsController {
|
19
|
private final Logger log = Logger.getLogger(this.getClass());
|
20
|
|
21
|
@Autowired
|
22
|
private NotificationsDAO notificationsDAO;
|
23
|
@Autowired
|
24
|
private PortalDAO portalDAO;
|
25
|
|
26
|
@RequestMapping(value = "/community/{pid}/notifications", method = RequestMethod.GET)
|
27
|
public List<Notifications> getNotifications(@PathVariable(value = "pid") String pid ) throws ContentNotFoundException {
|
28
|
if(portalDAO.findByPid(pid) == null){
|
29
|
throw new ContentNotFoundException("Portal not found");
|
30
|
}
|
31
|
List<Notifications> notifications = notificationsDAO.findByPortalPid(pid);
|
32
|
if(notifications == null || notifications.size() == 0){
|
33
|
throw new ContentNotFoundException("Notifications settings not found");
|
34
|
}
|
35
|
return notifications;
|
36
|
}
|
37
|
@RequestMapping(value = "/community/{pid}/notifications", method = RequestMethod.DELETE)
|
38
|
public void deleteEntity(@PathVariable(value = "pid") String pid, @RequestBody String email) throws ContentNotFoundException {
|
39
|
Notifications notifications = notificationsDAO.findByManagerEmailAndPortalPid(email,pid);
|
40
|
if(notifications!= null){
|
41
|
notificationsDAO.delete(notifications.getId());
|
42
|
}else{
|
43
|
throw new ContentNotFoundException("Notifications not found");
|
44
|
}
|
45
|
|
46
|
}
|
47
|
|
48
|
@RequestMapping(value = "/community/{pid}/notifications", method = RequestMethod.POST)
|
49
|
public Notifications saveEntity(@PathVariable(value = "pid") String pid, @RequestBody Notifications notifications) throws ContentNotFoundException {
|
50
|
if(portalDAO.findByPid(pid) == null){
|
51
|
throw new ContentNotFoundException("Portal not found");
|
52
|
}
|
53
|
|
54
|
if(notifications.getManagerEmail() != null && !notifications.getManagerEmail().isEmpty()){
|
55
|
Notifications saved = notificationsDAO.findByManagerEmailAndPortalPid(notifications.getManagerEmail(),pid);
|
56
|
log.debug(saved);
|
57
|
if(saved!= null){
|
58
|
notifications.setId(saved.getId());
|
59
|
}
|
60
|
|
61
|
notifications.setPortalPid(pid);
|
62
|
log.debug(notifications);
|
63
|
Notifications savedNotifications = notificationsDAO.save(notifications);
|
64
|
return savedNotifications;
|
65
|
}else{
|
66
|
log.error("No user e-mail specified");
|
67
|
return null;
|
68
|
}
|
69
|
|
70
|
|
71
|
}
|
72
|
|
73
|
}
|