Project

General

Profile

1
package eu.dnetlib.uoaadmintools.controllers;
2

    
3
import eu.dnetlib.uoaadmintools.dao.CommunityDAO;
4
import eu.dnetlib.uoaadmintools.dao.NotificationsDAO;
5
import eu.dnetlib.uoaadmintools.entities.Notifications;
6
import eu.dnetlib.uoaadmintools.handlers.NotFoundException;
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 CommunityDAO communityDAO;
25

    
26
    @RequestMapping(value = "/community/{pid}/notifications", method = RequestMethod.GET)
27
    public List<Notifications> getNotifications(@PathVariable(value = "pid") String pid ) throws NotFoundException {
28
        if(communityDAO.findByPid(pid) == null){
29
            throw new NotFoundException("Community not found");
30
        }
31
        List<Notifications> notifications = notificationsDAO.findByCommunityPid(pid);
32
        if(notifications == null || notifications.size() == 0){
33
            throw new NotFoundException("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 NotFoundException {
39
        Notifications notifications = notificationsDAO.findByManagerEmailAndCommunityPid(email,pid);
40
        if(notifications!= null){
41
            notificationsDAO.delete(notifications.getId());
42
        }else{
43
            throw new NotFoundException("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 NotFoundException {
50
        if(communityDAO.findByPid(pid) == null){
51
            throw new NotFoundException("Community not found");
52
        }
53

    
54
        if(notifications.getManagerEmail() != null && !notifications.getManagerEmail().isEmpty()){
55
            Notifications saved = notificationsDAO.findByManagerEmailAndCommunityPid(notifications.getManagerEmail(),pid);
56
            log.debug(saved);
57
            if(saved!= null){
58
                notifications.setId(saved.getId());
59
            }
60

    
61
            notifications.setCommunityPid(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
}
(8-8/15)