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.emailSender.EmailSender;
6
import eu.dnetlib.uoaadmintools.entities.Email;
7
import eu.dnetlib.uoaadmintools.entities.Notifications;
8
import eu.dnetlib.uoaadmintools.handlers.ContentNotFoundException;
9
import org.apache.log4j.Logger;
10
import org.springframework.beans.factory.annotation.Autowired;
11
import org.springframework.web.bind.annotation.*;
12

    
13
import java.util.ArrayList;
14
import java.util.HashMap;
15
import java.util.List;
16
import java.util.Map;
17

    
18
@RestController
19
@CrossOrigin(origins = "*")
20
public class EmailController {
21
    private final Logger log = Logger.getLogger(this.getClass());
22

    
23
    @Autowired
24
    private EmailSender emailSender;
25
    @Autowired
26
    private NotificationsDAO notificationsDAO;
27
    @Autowired
28
    private CommunityDAO communityDAO;
29
    //invitation // no check
30
    //welcome new manager - no check
31
    //notify about new manager - check manager option
32
    //notify about new subscribers - check manager option
33
    //notify for claims - check manager option --> claim API
34
    @RequestMapping(value = "/sendMail", method = RequestMethod.POST)
35
    public Map<String, ArrayList<String>> sendEmail(@RequestBody Email email ) {
36
        String successString = "success";
37
        String failureString = "failure";
38
        Map<String, ArrayList<String>> mailResults = new HashMap<>();
39

    
40
        for(String userMail:email.getRecipients()){
41
            ArrayList<String> sendTo = new ArrayList<>();
42
            sendTo.add(userMail);
43
            boolean success =emailSender.send(sendTo,email.getSubject(),email.getBody(), true);
44
            if(success){
45
                if(!mailResults.containsKey(successString)) {
46
                    mailResults.put(successString, new ArrayList<>());
47
                }
48
                mailResults.get(successString).add(userMail);
49
            } else {
50
                if(!mailResults.containsKey(failureString)) {
51
                    mailResults.put(failureString, new ArrayList<>());
52
                }
53
                mailResults.get(failureString).add(userMail);
54
            }
55
        }
56
        return mailResults;
57

    
58
    }
59

    
60
    @RequestMapping(value = "/notifyForNewManagers/{pid}", method = RequestMethod.POST)
61
    public Boolean notifyNewManagers(@PathVariable(value = "pid") String pid,@RequestBody Email email ) throws Exception {
62
        List<String> notifyrecipients = new ArrayList<String>();
63
        if(communityDAO.findByPid(pid) == null){
64
            throw new ContentNotFoundException("Community not found");
65
        }
66
        for(String user:email.getRecipients()){
67
            Notifications userNotifications = notificationsDAO.findByManagerEmailAndCommunityPid(user,pid);
68

    
69
            if(userNotifications == null || userNotifications.getNotifyForNewManagers()){
70
                notifyrecipients.add(user);
71
            }
72
        }
73
        if(notifyrecipients.size() > 0){
74
            return emailSender.send(notifyrecipients,email.getSubject(),email.getBody(), false);
75
        }else{
76
            log.debug("There are no users to notify ");
77
        }
78

    
79
        return true;
80
    }
81
    @RequestMapping(value = "/notifyForNewSubscribers/{pid}", method = RequestMethod.POST)
82
    public Boolean notifyNewSubscribers(@PathVariable(value = "pid") String pid,@RequestBody Email email ) throws Exception {
83
        List<String> notifyrecipients = new ArrayList<String>();
84
        if(communityDAO.findByPid(pid) == null){
85
            throw new ContentNotFoundException("Community not found");
86
        }
87
        for(String user:email.getRecipients()){
88
            Notifications userNotifications = notificationsDAO.findByManagerEmailAndCommunityPid(user,pid);
89

    
90
            if(userNotifications == null || userNotifications.getNotifyForNewSubscribers()){
91
                notifyrecipients.add(user);
92
            }
93
        }
94
        if(notifyrecipients.size() > 0){
95
            return emailSender.send(notifyrecipients,email.getSubject(),email.getBody(), false);
96
        }else{
97
            log.debug("There are no users to notify ");
98
        }
99

    
100
        return true;
101
    }
102
    @RequestMapping(value = "/test", method = RequestMethod.GET)
103
    public String test() throws Exception {
104
        log.debug("Test mail");
105
        List<String> mails = new ArrayList<>();
106
        mails.add("argirok@di.uoa.gr");
107
        mails.add("argirokokogiannaki@gmail.com");
108
        log.debug("Recipients"+mails);
109

    
110
        Email email =  new Email();
111
        email.setRecipients(mails);
112
        email.setBody("Test body");
113
        email.setSubject("Test theme");
114
        String response = "";
115
        response+=this.notifyNewManagers("ee", email);
116
        log.debug("Notify managers "+response);
117

    
118
        response+=" ";
119
        response+=this.notifyNewSubscribers("ee", email);
120
        log.debug("Notify for subscr "+response);
121
        return response;
122

    
123
    }
124

    
125
}
(5-5/15)