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.EmailRecaptcha;
7
import eu.dnetlib.uoaadmintools.entities.Email;
8
import eu.dnetlib.uoaadmintools.entities.Notifications;
9
import eu.dnetlib.uoaadmintools.handlers.ContentNotFoundException;
10
import eu.dnetlib.uoaadmintools.handlers.InvalidReCaptchaException;
11
import eu.dnetlib.uoaadmintools.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 CommunityDAO communityDAO;
29
    @Autowired
30
    private VerifyRecaptcha verifyRecaptcha;
31

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

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

    
68
    }
69

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

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

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

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

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

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

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

    
133
    }
134

    
135
}
(6-6/16)