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.uoaadmintools.handlers.ContentNotFoundException;
6
import eu.dnetlib.uoaadmintoolslibrary.dao.PortalDAO;
7
import eu.dnetlib.uoaadmintoolslibrary.emailSender.EmailSender;
8
import eu.dnetlib.uoaadmintoolslibrary.entities.email.Email;
9
import eu.dnetlib.uoaadmintoolslibrary.entities.email.EmailRecaptcha;
10
import eu.dnetlib.uoaadmintoolslibrary.handlers.InvalidReCaptchaException;
11
import eu.dnetlib.uoaadmintoolslibrary.recaptcha.VerifyRecaptcha;
12
import org.apache.log4j.Logger;
13
import org.springframework.beans.factory.annotation.Autowired;
14
import org.springframework.web.bind.annotation.*;
15

    
16
import java.util.*;
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 PortalDAO portalDAO;
29
    @Autowired
30
    private VerifyRecaptcha verifyRecaptcha;
31

    
32
    @RequestMapping(value = "/contact", method = RequestMethod.POST)
33
    public Boolean contact(@RequestBody EmailRecaptcha form)  throws InvalidReCaptchaException {
34
        if(form.getRecaptcha() != null) {
35
            verifyRecaptcha.processResponse(form.getRecaptcha());
36
        }
37
        Email email = form.getEmail();
38
        ArrayList<String> sendTo = new ArrayList<>(email.getRecipients());
39
        return emailSender.send(sendTo, email.getSubject(), email.getBody(), false);
40
    }
41

    
42
    @RequestMapping(value = "/sendMail", method = RequestMethod.POST)
43
    public Map<String, ArrayList<String>> sendEmail(@RequestBody Email email,
44
                                                    @RequestParam(required = false) Optional<Boolean> optional) {
45
        String successString = "success";
46
        String failureString = "failure";
47
        Map<String, ArrayList<String>> mailResults = new HashMap<>();
48
        boolean bcc = (optional.isPresent())?optional.get():true;
49
        for(String userMail:email.getRecipients()){
50
            ArrayList<String> sendTo = new ArrayList<>();
51
            sendTo.add(userMail);
52
            boolean success =emailSender.send(sendTo,email.getSubject(),email.getBody(), bcc);
53
            if(success){
54
                if(!mailResults.containsKey(successString)) {
55
                    mailResults.put(successString, new ArrayList<>());
56
                }
57
                mailResults.get(successString).add(userMail);
58
            } else {
59
                if(!mailResults.containsKey(failureString)) {
60
                    mailResults.put(failureString, new ArrayList<>());
61
                }
62
                mailResults.get(failureString).add(userMail);
63
            }
64
        }
65
        return mailResults;
66

    
67
    }
68

    
69
    @RequestMapping(value = "/notifyForNewManagers/{pid}", method = RequestMethod.POST)
70
    public Boolean notifyNewManagers(@PathVariable(value = "pid") String pid,@RequestBody Email email ) throws Exception {
71
        List<String> notifyrecipients = new ArrayList<String>();
72
        if(portalDAO.findByPid(pid) == null){
73
            throw new ContentNotFoundException("Portal not found");
74
        }
75
        for(String user:email.getRecipients()){
76
            Notifications userNotifications = notificationsDAO.findByManagerEmailAndPortalPid(user,pid);
77

    
78
            if(userNotifications == null || userNotifications.getNotifyForNewManagers()){
79
                notifyrecipients.add(user);
80
            }
81
        }
82
        if(notifyrecipients.size() > 0){
83
            return emailSender.send(notifyrecipients,email.getSubject(),email.getBody(), false);
84
        }else{
85
            log.debug("There are no users to notify ");
86
        }
87

    
88
        return true;
89
    }
90
    @RequestMapping(value = "/notifyForNewSubscribers/{pid}", method = RequestMethod.POST)
91
    public Boolean notifyNewSubscribers(@PathVariable(value = "pid") String pid,@RequestBody Email email ) throws Exception {
92
        List<String> notifyrecipients = new ArrayList<String>();
93
        if(portalDAO.findByPid(pid) == null){
94
            throw new ContentNotFoundException("Portal not found");
95
        }
96
        for(String user:email.getRecipients()){
97
            Notifications userNotifications = notificationsDAO.findByManagerEmailAndPortalPid(user,pid);
98

    
99
            if(userNotifications == null || userNotifications.getNotifyForNewSubscribers()){
100
                notifyrecipients.add(user);
101
            }
102
        }
103
        if(notifyrecipients.size() > 0){
104
            return emailSender.send(notifyrecipients,email.getSubject(),email.getBody(), false);
105
        }else{
106
            log.debug("There are no users to notify ");
107
        }
108

    
109
        return true;
110
    }
111
    @RequestMapping(value = "/test", method = RequestMethod.GET)
112
    public String test() throws Exception {
113
        log.debug("Test mail");
114
        List<String> mails = new ArrayList<>();
115
        mails.add("argirok@di.uoa.gr");
116
        mails.add("argirokokogiannaki@gmail.com");
117
        log.debug("Recipients"+mails);
118

    
119
        Email email =  new Email();
120
        email.setRecipients(mails);
121
        email.setBody("Test body");
122
        email.setSubject("Test theme");
123
        String response = "";
124
        response+=this.notifyNewManagers("ee", email);
125
        log.debug("Notify managers "+response);
126

    
127
        response+=" ";
128
        response+=this.notifyNewSubscribers("ee", email);
129
        log.debug("Notify for subscr "+response);
130
        return response;
131

    
132
    }
133

    
134
}
(4-4/10)