1
|
package eu.dnetlib.uoaadmintools.controllers;
|
2
|
|
3
|
import eu.dnetlib.uoaadmintools.dao.NotificationsDAO;
|
4
|
import eu.dnetlib.uoaadmintools.entities.Manager;
|
5
|
import eu.dnetlib.uoaadmintools.entities.Notifications;
|
6
|
import eu.dnetlib.uoaadmintools.services.ManagerService;
|
7
|
import eu.dnetlib.uoaadmintoolslibrary.dao.PortalDAO;
|
8
|
import eu.dnetlib.uoaadmintoolslibrary.emailSender.EmailSender;
|
9
|
import eu.dnetlib.uoaadmintoolslibrary.entities.email.Email;
|
10
|
import eu.dnetlib.uoaadmintoolslibrary.entities.email.EmailRecaptcha;
|
11
|
import eu.dnetlib.uoaadmintoolslibrary.handlers.ContentNotFoundException;
|
12
|
import eu.dnetlib.uoaadmintoolslibrary.handlers.InvalidReCaptchaException;
|
13
|
import eu.dnetlib.uoaadmintoolslibrary.handlers.utils.RolesUtils;
|
14
|
import eu.dnetlib.uoaadmintoolslibrary.recaptcha.VerifyRecaptcha;
|
15
|
import org.apache.log4j.Logger;
|
16
|
import org.springframework.beans.factory.annotation.Autowired;
|
17
|
import org.springframework.web.bind.annotation.*;
|
18
|
|
19
|
import java.util.*;
|
20
|
|
21
|
@RestController
|
22
|
@CrossOrigin(origins = "*")
|
23
|
public class EmailController {
|
24
|
private final Logger log = Logger.getLogger(this.getClass());
|
25
|
|
26
|
@Autowired
|
27
|
private EmailSender emailSender;
|
28
|
@Autowired
|
29
|
private NotificationsDAO notificationsDAO;
|
30
|
@Autowired
|
31
|
private PortalDAO portalDAO;
|
32
|
@Autowired
|
33
|
private VerifyRecaptcha verifyRecaptcha;
|
34
|
@Autowired
|
35
|
private ManagerService managerService;
|
36
|
@Autowired
|
37
|
private RolesUtils rolesUtils;
|
38
|
|
39
|
@RequestMapping(value = "/contact", method = RequestMethod.POST)
|
40
|
public Boolean contact(@RequestBody EmailRecaptcha form) throws InvalidReCaptchaException {
|
41
|
if(form.getRecaptcha() != null) {
|
42
|
verifyRecaptcha.processResponse(form.getRecaptcha());
|
43
|
}
|
44
|
Email email = form.getEmail();
|
45
|
ArrayList<String> sendTo = new ArrayList<>(email.getRecipients());
|
46
|
return emailSender.send(sendTo, email.getSubject(), email.getBody(), false);
|
47
|
}
|
48
|
|
49
|
@RequestMapping(value = "/sendMail", method = RequestMethod.POST)
|
50
|
public Map<String, ArrayList<String>> sendEmail(@RequestBody Email email,
|
51
|
@RequestParam(required = false) Optional<Boolean> optional) {
|
52
|
String successString = "success";
|
53
|
String failureString = "failure";
|
54
|
Map<String, ArrayList<String>> mailResults = new HashMap<>();
|
55
|
boolean bcc = (optional.isPresent())?optional.get():true;
|
56
|
for(String userMail:email.getRecipients()){
|
57
|
ArrayList<String> sendTo = new ArrayList<>();
|
58
|
sendTo.add(userMail);
|
59
|
boolean success =emailSender.send(sendTo,email.getSubject(),email.getBody(), bcc);
|
60
|
if(success){
|
61
|
if(!mailResults.containsKey(successString)) {
|
62
|
mailResults.put(successString, new ArrayList<>());
|
63
|
}
|
64
|
mailResults.get(successString).add(userMail);
|
65
|
} else {
|
66
|
if(!mailResults.containsKey(failureString)) {
|
67
|
mailResults.put(failureString, new ArrayList<>());
|
68
|
}
|
69
|
mailResults.get(failureString).add(userMail);
|
70
|
}
|
71
|
}
|
72
|
return mailResults;
|
73
|
|
74
|
}
|
75
|
|
76
|
@RequestMapping(value = "/notifyManagers/{pid}/{newRoleType}", method = RequestMethod.POST)
|
77
|
public Boolean notifyManagers(@PathVariable(value = "pid") String pid,
|
78
|
@PathVariable(value = "newRoleType") String newRoleType,
|
79
|
@RequestBody Email email ) throws Exception {
|
80
|
List<String> notifyrecipients = new ArrayList<String>();
|
81
|
if(portalDAO.findByPid(pid) == null){
|
82
|
throw new ContentNotFoundException("Portal not found");
|
83
|
}
|
84
|
|
85
|
Manager[] managers = managerService.getManagers(pid);
|
86
|
List<String> emails = new ArrayList();
|
87
|
|
88
|
for(Manager manager:managers){
|
89
|
String userEmail = manager.getEmail();
|
90
|
emails.add(userEmail);
|
91
|
|
92
|
// Do not send the notification to the new manager/ subscriber.
|
93
|
if(userEmail.equals(rolesUtils.getEmail())) {
|
94
|
continue;
|
95
|
}
|
96
|
|
97
|
Notifications userNotifications = notificationsDAO.findByManagerEmailAndPortalPid(userEmail,pid);
|
98
|
|
99
|
if(userNotifications == null
|
100
|
|| (newRoleType.equals("manager") && userNotifications.getNotifyForNewManagers())
|
101
|
|| (newRoleType.equals("subscriber") && userNotifications.getNotifyForNewSubscribers())){
|
102
|
notifyrecipients.add(userEmail);
|
103
|
}
|
104
|
}
|
105
|
if(notifyrecipients.size() > 0){
|
106
|
email.setBody(email.getBody().replace("((__managers__))", String.join(", ", emails)));
|
107
|
|
108
|
return emailSender.send(notifyrecipients,email.getSubject(),email.getBody(), false);
|
109
|
}else{
|
110
|
log.debug("There are no users to notify ");
|
111
|
}
|
112
|
|
113
|
return true;
|
114
|
}
|
115
|
// @RequestMapping(value = "/notifyForNewSubscribers/{pid}", method = RequestMethod.POST)
|
116
|
// public Boolean notifyNewSubscribers(@PathVariable(value = "pid") String pid,@RequestBody Email email ) throws Exception {
|
117
|
// List<String> notifyrecipients = new ArrayList<String>();
|
118
|
// if(portalDAO.findByPid(pid) == null){
|
119
|
// throw new ContentNotFoundException("Portal not found");
|
120
|
// }
|
121
|
// for(String user:email.getRecipients()){
|
122
|
// Notifications userNotifications = notificationsDAO.findByManagerEmailAndPortalPid(user,pid);
|
123
|
//
|
124
|
// if(userNotifications == null || userNotifications.getNotifyForNewSubscribers()){
|
125
|
// notifyrecipients.add(user);
|
126
|
// }
|
127
|
// }
|
128
|
// if(notifyrecipients.size() > 0){
|
129
|
// return emailSender.send(notifyrecipients,email.getSubject(),email.getBody(), false);
|
130
|
// }else{
|
131
|
// log.debug("There are no users to notify ");
|
132
|
// }
|
133
|
//
|
134
|
// return true;
|
135
|
// }
|
136
|
@RequestMapping(value = "/test", method = RequestMethod.GET)
|
137
|
public String test() throws Exception {
|
138
|
log.debug("Test mail");
|
139
|
List<String> mails = new ArrayList<>();
|
140
|
mails.add("argirok@di.uoa.gr");
|
141
|
mails.add("argirokokogiannaki@gmail.com");
|
142
|
log.debug("Recipients"+mails);
|
143
|
|
144
|
Email email = new Email();
|
145
|
email.setRecipients(mails);
|
146
|
email.setBody("Test body");
|
147
|
email.setSubject("Test theme");
|
148
|
String response = "";
|
149
|
response+=this.notifyManagers("ee", "manager", email);
|
150
|
log.debug("Notify managers "+response);
|
151
|
|
152
|
response+=" ";
|
153
|
response+=this.notifyManagers("ee", "subscriber", email);
|
154
|
log.debug("Notify for subscr "+response);
|
155
|
return response;
|
156
|
|
157
|
}
|
158
|
|
159
|
}
|