Project

General

Profile

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.dao.PortalDAO;
6
import eu.dnetlib.uoaadmintoolslibrary.entities.Portal;
7
import eu.dnetlib.uoaadmintoolslibrary.handlers.ContentNotFoundException;
8
import eu.dnetlib.uoaadmintoolslibrary.handlers.MismatchingContentException;
9
import eu.dnetlib.uoaadmintoolslibrary.handlers.utils.RolesUtils;
10
import org.apache.log4j.Logger;
11
import org.springframework.beans.factory.annotation.Autowired;
12
import org.springframework.security.access.prepost.PreAuthorize;
13
import org.springframework.web.bind.annotation.*;
14

    
15
import java.util.List;
16

    
17
/**
18
 * Created by argirok on 6/7/2018.
19
 */
20
@RestController
21
@CrossOrigin(origins = "*")
22
public class NotificationsController {
23
    private final Logger log = Logger.getLogger(this.getClass());
24

    
25
    @Autowired
26
    private NotificationsDAO notificationsDAO;
27
    @Autowired
28
    private PortalDAO portalDAO;
29
    @Autowired
30
    private RolesUtils rolesUtils;
31

    
32
    @PreAuthorize("hasAnyAuthority(@AuthorizationService.PORTAL_ADMIN)")
33
    @RequestMapping(value = "/community/{pid}/notifications/all", method = RequestMethod.GET)
34
    public List<Notifications> getNotifications(@PathVariable(value = "pid") String pid ) throws ContentNotFoundException {
35
        Portal portal = portalDAO.findByPid(pid);
36
        if(portal == null){
37
            throw new ContentNotFoundException("Portal with pid: "+pid+" not found");
38
        }
39
        if(!portal.getType().equals("community")) {
40
            // EXCEPTION - MismatchingContent
41
            throw new MismatchingContentException("Get Notifications: Portal with id: "+portal.getId()+" has type: "+portal.getType()+" instead of community");
42
        }
43

    
44
        List<Notifications> notifications = notificationsDAO.findByPortalPid(pid);
45
        if(notifications == null || notifications.size() == 0){
46
            throw new ContentNotFoundException("Notifications settings for community with pid: "+pid+" not found");
47
        }
48
        return notifications;
49
    }
50

    
51
    @PreAuthorize("hasAnyAuthority(" +
52
            "@AuthorizationService.PORTAL_ADMIN, " +
53
            "@AuthorizationService.curator('community'), @AuthorizationService.manager('community', #pid))")
54
    @RequestMapping(value = "/community/{pid}/notifications", method = RequestMethod.GET)
55
    public Notifications getNotificationsForUser(@PathVariable(value = "pid") String pid) throws ContentNotFoundException {
56
        Portal portal = portalDAO.findByPid(pid);
57
        if(portal == null){
58
            throw new ContentNotFoundException("Portal with pid: "+pid+" not found");
59
        }
60
        if(!portal.getType().equals("community")) {
61
            // EXCEPTION - MismatchingContent
62
            throw new MismatchingContentException("Get Notifications: Portal with id: "+portal.getId()+" has type: "+portal.getType()+" instead of community");
63
        }
64

    
65
        String email = rolesUtils.getEmail();
66

    
67
        Notifications notifications = notificationsDAO.findByManagerEmailAndPortalPid(email, pid);
68
        if(notifications == null){
69
            throw new ContentNotFoundException("Notifications settings for community with pid: "+pid+" and user email: "+email+" not found");
70
        }
71
        return notifications;
72
    }
73

    
74
    @PreAuthorize("hasAnyAuthority(" +
75
            "@AuthorizationService.PORTAL_ADMIN, " +
76
            "@AuthorizationService.curator('community'), @AuthorizationService.manager('community', #pid))")
77
    @RequestMapping(value = "/community/{pid}/notifications", method = RequestMethod.DELETE)
78
    public void deleteNotification(@PathVariable(value = "pid") String pid) throws ContentNotFoundException {
79
        Portal portal = portalDAO.findByPid(pid);
80
        if(portal == null){
81
            throw new ContentNotFoundException("Portal with pid: "+pid+" not found");
82
        }
83
        if(!portal.getType().equals("community")) {
84
            // EXCEPTION - MismatchingContent
85
            throw new MismatchingContentException("Delete Notifications: Portal with id: "+portal.getId()+" has type: "+portal.getType()+" instead of community");
86
        }
87
        String email = rolesUtils.getEmail();
88
        Notifications notifications = notificationsDAO.findByManagerEmailAndPortalPid(email,pid);
89
        if(notifications!= null){
90
            notificationsDAO.delete(notifications.getId());
91
        }else{
92
            throw new ContentNotFoundException("Notifications settings for community with pid: "+pid+" and user email: "+email+" not found");
93
        }
94

    
95
    }
96

    
97
    @PreAuthorize("hasAnyAuthority(" +
98
            "@AuthorizationService.PORTAL_ADMIN, " +
99
            "@AuthorizationService.curator('community'), @AuthorizationService.manager('community', #pid))")
100
    @RequestMapping(value = "/community/{pid}/notifications", method = RequestMethod.POST)
101
    public Notifications saveNotification(@PathVariable(value = "pid") String pid, @RequestBody Notifications notifications) throws ContentNotFoundException {
102
        Portal portal = portalDAO.findByPid(pid);
103
        if(portal == null){
104
            throw new ContentNotFoundException("Portal with pid: "+pid+" not found");
105
        }
106
        if(!portal.getType().equals("community")) {
107
            // EXCEPTION - MismatchingContent
108
            throw new MismatchingContentException("Save Notifications: Portal with id: "+portal.getId()+" has type: "+portal.getType()+" instead of community");
109
        }
110

    
111
        notifications.setManagerEmail(rolesUtils.getEmail());
112
        notifications.setAaiId(rolesUtils.getAaiId());
113

    
114
//        if(notifications.getManagerEmail() != null && !notifications.getManagerEmail().isEmpty()){
115
            Notifications saved = notificationsDAO.findByManagerEmailAndPortalPid(notifications.getManagerEmail(),pid);
116
            log.debug(saved);
117
            if(saved!= null){
118
                notifications.setId(saved.getId());
119
            }
120

    
121
            notifications.setPortalPid(pid);
122
            log.debug(notifications);
123
            Notifications savedNotifications = notificationsDAO.save(notifications);
124
            return savedNotifications;
125
//        } else{
126
//            log.error("Save notifications: No user e-mail specified");
127
//            return null;
128
//        }
129

    
130

    
131
    }
132

    
133
}
(6-6/9)