1 |
1 |
package eu.dnetlib.data.emailSender;
|
2 |
2 |
|
3 |
3 |
import eu.dnetlib.data.claims.migration.entity.Claim;
|
|
4 |
import eu.dnetlib.data.claims.migration.entity.Context;
|
|
5 |
import eu.dnetlib.data.claims.migration.entity.Notification;
|
4 |
6 |
import eu.dnetlib.data.claims.migration.entity.Project;
|
5 |
|
import eu.dnetlib.data.claims.migration.handler.FetchClaimHandler;
|
6 |
|
import eu.dnetlib.data.claims.migration.handler.ProjectHandler;
|
|
7 |
import eu.dnetlib.data.claims.migration.handler.*;
|
7 |
8 |
import eu.dnetlib.data.claimsDemo.ClaimUtils;
|
|
9 |
import eu.dnetlib.data.claimsDemo.CommunityUtils;
|
8 |
10 |
import eu.dnetlib.data.claimsDemo.SQLStoreException;
|
9 |
11 |
import org.apache.log4j.Logger;
|
10 |
12 |
import org.springframework.beans.factory.annotation.Autowired;
|
... | ... | |
22 |
24 |
|
23 |
25 |
@Autowired
|
24 |
26 |
private FetchClaimHandler fetchClaimHandler = null;
|
|
27 |
@Autowired
|
|
28 |
private FetchProjectHandler fetchProjectHandler = null;
|
|
29 |
@Autowired
|
|
30 |
private FetchNotificationHandler fetchNotificationHandler = null;
|
|
31 |
@Autowired
|
|
32 |
private NotificationHandler notificationHandler = null;
|
|
33 |
@Autowired
|
|
34 |
private FetchContextHandler fetchContextHandler = null;
|
25 |
35 |
|
26 |
36 |
@Autowired
|
27 |
|
private ProjectHandler projectHandler = null;
|
|
37 |
private String defaultFrequencyInHours;
|
28 |
38 |
|
29 |
39 |
private static String openaireClaimsPage;
|
30 |
40 |
private static String username;
|
... | ... | |
34 |
44 |
private static String from;
|
35 |
45 |
private static String specialRecipients;
|
36 |
46 |
|
37 |
|
private static String dateFrom;
|
38 |
|
private static String dateTo;
|
39 |
|
|
40 |
47 |
@Override
|
41 |
48 |
public void run() {
|
42 |
49 |
logger.info("EmailSender thread is running. " + host);
|
43 |
50 |
logger.info("Special Recipients " + specialRecipients);
|
44 |
51 |
|
|
52 |
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
|
53 |
Date date = new Date();
|
|
54 |
String dateTo = (format.format(date));
|
45 |
55 |
|
46 |
|
Project project = null;
|
|
56 |
defaultEmails_For_ProjectClaims(dateTo, format); // daily for managers not in notification table
|
|
57 |
defaultEmails_For_CommunityClaims(dateTo, format); // daily for managers not in notification table
|
|
58 |
|
|
59 |
notificationEmails_For_ProjectClaims(dateTo, format);
|
|
60 |
notificationEmails_For_CommunityClaims(dateTo, format);
|
|
61 |
}
|
|
62 |
|
|
63 |
public void defaultEmails_For_ProjectClaims(String dateTo, SimpleDateFormat format) {
|
|
64 |
Map<String, List<String>> managersOfProject = new HashMap<String, List<String>>();
|
|
65 |
Project project;
|
47 |
66 |
List<Claim> claims = null;
|
48 |
67 |
List<String> types = new ArrayList<String>();
|
49 |
68 |
|
50 |
|
Map<String, List<String>> managersOfProject = new HashMap<String, List<String>>();
|
51 |
|
|
52 |
69 |
types.add(ClaimUtils.PROJECT);
|
53 |
70 |
|
54 |
|
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
|
55 |
|
Date date = null;
|
56 |
|
|
57 |
71 |
Calendar calendar = Calendar.getInstance();
|
58 |
|
calendar.add(Calendar.HOUR_OF_DAY, -23);
|
|
72 |
calendar.add(Calendar.HOUR_OF_DAY, -(Integer.parseInt(defaultFrequencyInHours)-1));
|
59 |
73 |
calendar.add(Calendar.MINUTE, -59);
|
60 |
74 |
calendar.add(Calendar.SECOND, -59);
|
61 |
|
date = calendar.getTime();
|
62 |
|
dateFrom=(format.format(date));
|
|
75 |
Date date = calendar.getTime();
|
|
76 |
String dateFrom=(format.format(date));
|
63 |
77 |
|
64 |
|
date = new Date();
|
65 |
|
dateTo = (format.format(date));
|
|
78 |
logger.debug("Sending emails for project claims between " + dateFrom + " and "+dateTo);
|
66 |
79 |
|
67 |
|
logger.debug("Sending emails for claims between " + dateFrom + " and "+dateTo);
|
68 |
|
|
69 |
80 |
try {
|
70 |
81 |
// Get all claims between dateFrom and dateTo which satisfy source_type == "project" or target_type == "project"
|
71 |
82 |
claims = fetchClaimHandler.fetchClaimsByDate(dateFrom, dateTo, null, null, "", "source", true, types, false);
|
... | ... | |
90 |
101 |
managersOfProject.put(project.getOpenaireId(), tmpManagers);
|
91 |
102 |
}
|
92 |
103 |
|
|
104 |
/*
|
93 |
105 |
// Send emails to actual project managers instead of special recipients
|
94 |
|
//List<String> managers = projectFetcher.getFetchProjectHandler().fetchContactEmailsByProjectId(project.getOpenaireId());
|
95 |
|
//logger.debug("Managers of project " + project.getOpenaireId() + ": "+managers);
|
96 |
|
//managersOfProject.put(project.getOpenaireId(), managers);
|
|
106 |
List<String> managers = null;
|
|
107 |
try {
|
|
108 |
managers = fetchProjectHandler.fetchContactEmailsByProjectId(project.getOpenaireId());
|
|
109 |
if(managers != null) {
|
|
110 |
Iterator itr = managers.iterator();
|
|
111 |
while (itr.hasNext()) {
|
|
112 |
String manager = (String) itr.next();
|
|
113 |
Notification notification = fetchNotificationHandler.fetchNotification(project.getOpenaireId(), manager);
|
|
114 |
//if (notification != null && (!notification.isNotify() || notification.getFrequency() != 24)) {
|
|
115 |
if (notification != null) {
|
|
116 |
itr.remove();
|
|
117 |
}
|
|
118 |
}
|
|
119 |
}
|
|
120 |
} catch (Exception e) {
|
|
121 |
e.printStackTrace();
|
|
122 |
} catch (SQLStoreException e) {
|
|
123 |
e.printStackTrace();
|
|
124 |
}
|
97 |
125 |
|
98 |
|
String token = UUID.randomUUID().toString();
|
99 |
|
logger.debug("UUID Token for project " + project.getOpenaireId() + ": " + token);
|
|
126 |
logger.debug("Managers of project " + project.getOpenaireId() + ": "+managers);
|
|
127 |
managersOfProject.put(project.getOpenaireId(), managers);
|
|
128 |
*/
|
|
129 |
|
|
130 |
if (managersOfProject.get(project.getOpenaireId()) != null &&
|
|
131 |
!managersOfProject.get(project.getOpenaireId()).isEmpty()) {
|
|
132 |
//send(project.getOpenaireId(), project.getName(), "project", managersOfProject.get(project.getOpenaireId()));
|
|
133 |
}
|
|
134 |
}
|
|
135 |
}
|
|
136 |
}
|
|
137 |
|
|
138 |
public void defaultEmails_For_CommunityClaims(String dateTo, SimpleDateFormat format) {
|
|
139 |
Map<String, List<String>> managersOfCommunity = new HashMap<String, List<String>>();
|
|
140 |
Context context;
|
|
141 |
List<Claim> claims = null;
|
|
142 |
List<String> types = new ArrayList<String>();
|
|
143 |
|
|
144 |
types.add(ClaimUtils.CONTEXT);
|
|
145 |
|
|
146 |
Calendar calendar = Calendar.getInstance();
|
|
147 |
calendar.add(Calendar.HOUR_OF_DAY, -(Integer.parseInt(defaultFrequencyInHours)-1));
|
|
148 |
calendar.add(Calendar.MINUTE, -59);
|
|
149 |
calendar.add(Calendar.SECOND, -59);
|
|
150 |
Date date = calendar.getTime();
|
|
151 |
String dateFrom=(format.format(date));
|
|
152 |
|
|
153 |
logger.debug("Sending emails for community claims between " + dateFrom + " and "+dateTo);
|
|
154 |
|
|
155 |
try {
|
|
156 |
// Get all claims between dateFrom and dateTo which satisfy source_type == "context" or target_type == "context"
|
|
157 |
claims = fetchClaimHandler.fetchClaimsByDate(dateFrom, dateTo, null, null, "", "source", true, types, false);
|
|
158 |
} catch (SQLStoreException|Exception e) {
|
|
159 |
logger.error("Could not fetch claims by date from "+dateFrom+" to "+dateTo, e);
|
|
160 |
}
|
|
161 |
|
|
162 |
for (Claim claim: claims) {
|
|
163 |
if (claim.getSourceType().equals("context")) {
|
|
164 |
context = (Context)claim.getSource();
|
|
165 |
} else {
|
|
166 |
context = (Context)claim.getTarget();
|
|
167 |
}
|
|
168 |
|
|
169 |
String openaireId = context.getOpenaireId().split("::")[0];
|
|
170 |
if (!managersOfCommunity.containsKey(openaireId)) {
|
|
171 |
|
|
172 |
/*
|
|
173 |
// specialRecipients are used currently for testing purposes
|
|
174 |
List<String> tmpManagers = null;
|
|
175 |
if (specialRecipients != null && !specialRecipients.isEmpty()) {
|
|
176 |
tmpManagers = Arrays.asList(specialRecipients.split("\\s*,\\s*"));
|
|
177 |
logger.debug("Special recipients: " + specialRecipients);
|
|
178 |
managersOfCommunity.put(openaireId, tmpManagers);
|
|
179 |
}
|
|
180 |
*/
|
|
181 |
|
|
182 |
|
|
183 |
// Send emails to actual project managers instead of special recipients
|
|
184 |
List<String> managers = null;
|
100 |
185 |
try {
|
101 |
|
projectHandler.updateTokenByProjectId(project.getOpenaireId(), token);
|
|
186 |
CommunityUtils communityInfo = CommunityUtils.getCommunityInfo(openaireId);
|
|
187 |
managers = communityInfo.getManagers();
|
|
188 |
if(managers != null) {
|
|
189 |
Iterator itr = managers.iterator();
|
|
190 |
while (itr.hasNext()) {
|
|
191 |
String manager = (String) itr.next();
|
|
192 |
Notification notification = fetchNotificationHandler.fetchNotification(openaireId, manager);
|
|
193 |
if (notification != null) {
|
|
194 |
itr.remove();
|
|
195 |
} else {
|
|
196 |
logger.debug("Sending email to community manager: "+ manager);
|
|
197 |
}
|
|
198 |
}
|
|
199 |
}
|
|
200 |
else {
|
|
201 |
logger.debug("Community Managers: null");
|
|
202 |
}
|
|
203 |
} catch (Exception e) {
|
|
204 |
e.printStackTrace();
|
|
205 |
} catch (SQLStoreException e) {
|
|
206 |
e.printStackTrace();
|
|
207 |
}
|
102 |
208 |
|
103 |
|
if (managersOfProject.get(project.getOpenaireId()) != null &&
|
104 |
|
!managersOfProject.get(project.getOpenaireId()).isEmpty()) {
|
105 |
|
send(project, token, managersOfProject.get(project.getOpenaireId()));
|
|
209 |
logger.debug("Managers of community " + openaireId + ": "+managers);
|
|
210 |
managersOfCommunity.put(openaireId, managers);
|
|
211 |
|
|
212 |
|
|
213 |
if (managersOfCommunity.get(openaireId) != null &&
|
|
214 |
!managersOfCommunity.get(openaireId).isEmpty()) {
|
|
215 |
//send(openaireId, context.getTitle().split(">")[0], "community", managersOfCommunity.get(openaireId));
|
|
216 |
}
|
|
217 |
}
|
|
218 |
}
|
|
219 |
}
|
|
220 |
|
|
221 |
public void notificationEmails_For_ProjectClaims(String dateTo, SimpleDateFormat format) {
|
|
222 |
Project project = null;
|
|
223 |
List<String> types = new ArrayList<String>();
|
|
224 |
|
|
225 |
types.add(ClaimUtils.PROJECT);
|
|
226 |
|
|
227 |
logger.debug("Sending email for project claims from notification table");
|
|
228 |
|
|
229 |
try {
|
|
230 |
List<Notification> trueNotifications = fetchNotificationHandler.fetchTrueNotifications();
|
|
231 |
logger.debug(trueNotifications);
|
|
232 |
if(trueNotifications != null) {
|
|
233 |
for(Notification notification : trueNotifications) {
|
|
234 |
List<String> managers = fetchProjectHandler.fetchContactEmailsByProjectId(notification.getOpenaireId());
|
|
235 |
if(managers != null && managers.contains(notification.getUserMail())) {
|
|
236 |
|
|
237 |
Date _dateTo = format.parse(dateTo);
|
|
238 |
|
|
239 |
String last_interaction_date = (format.format(notification.getDate()));
|
|
240 |
|
|
241 |
long diff = _dateTo.getTime() - notification.getDate().getTime();
|
|
242 |
diff = diff / 1000;
|
|
243 |
|
|
244 |
if (diff >= (notification.getFrequency() * 3600)) {
|
|
245 |
if (fetchClaimHandler.fetchNumberOfClaimsByDateAndOpenaireId(last_interaction_date, dateTo, notification.getOpenaireId(), null, null, "", null, true, types, false) > 0) {
|
|
246 |
List<String> managersByNotification = new ArrayList<>();
|
|
247 |
managersByNotification.add(notification.getUserMail());
|
|
248 |
|
|
249 |
project = fetchProjectHandler.fetchProjectById(notification.getOpenaireId());
|
|
250 |
|
|
251 |
logger.debug("Sending email for project claims between " + last_interaction_date + " and " + dateTo + " to " + notification.getUserMail());
|
|
252 |
|
|
253 |
//send(project.getOpenaireId(), project.getName(), "project", managersByNotification);
|
|
254 |
}
|
|
255 |
Calendar cal = Calendar.getInstance();
|
|
256 |
cal.setTime(_dateTo);
|
|
257 |
cal.add(Calendar.SECOND, 1);
|
|
258 |
_dateTo = cal.getTime();
|
|
259 |
notificationHandler.updateNotificationLastInteractionDate(notification.getOpenaireId(), notification.getUserMail(), _dateTo);
|
|
260 |
//update last interaction date (db)
|
|
261 |
}
|
|
262 |
} else {
|
|
263 |
logger.debug("managers do not contain "+notification.getUserMail());
|
106 |
264 |
}
|
107 |
|
} catch (SQLStoreException|Exception e) {
|
108 |
|
logger.error("Could update token for project with id "+ project.getOpenaireId(), e);
|
109 |
265 |
}
|
|
266 |
} else {
|
|
267 |
logger.debug("true notifications: null");
|
110 |
268 |
}
|
|
269 |
} catch (Exception e) {
|
|
270 |
e.printStackTrace();
|
|
271 |
} catch (SQLStoreException e) {
|
|
272 |
e.printStackTrace();
|
111 |
273 |
}
|
112 |
274 |
}
|
113 |
275 |
|
114 |
|
public void send(Project project, String token, List<String> managers) {
|
115 |
|
final String openaireClaimsPageUrl = openaireClaimsPage + token;
|
|
276 |
public void notificationEmails_For_CommunityClaims(String dateTo, SimpleDateFormat format) {
|
|
277 |
Context context = null;
|
|
278 |
List<String> types = new ArrayList<String>();
|
116 |
279 |
|
|
280 |
types.add(ClaimUtils.CONTEXT);
|
|
281 |
|
|
282 |
logger.debug("Sending email for community claims from notification table");
|
|
283 |
|
|
284 |
try {
|
|
285 |
List<Notification> trueNotifications = fetchNotificationHandler.fetchTrueNotifications();
|
|
286 |
|
|
287 |
if(trueNotifications != null) {
|
|
288 |
for(Notification notification : trueNotifications) {
|
|
289 |
CommunityUtils communityInfo = CommunityUtils.getCommunityInfo(notification.getOpenaireId());
|
|
290 |
List<String> managers = null;
|
|
291 |
if(communityInfo != null) {
|
|
292 |
managers = communityInfo.getManagers();
|
|
293 |
}
|
|
294 |
|
|
295 |
if(managers != null && managers.contains(notification.getUserMail())) {
|
|
296 |
Date _dateTo = format.parse(dateTo);
|
|
297 |
|
|
298 |
String last_interaction_date = (format.format(notification.getDate()));
|
|
299 |
long diff = _dateTo.getTime() - notification.getDate().getTime();
|
|
300 |
diff = diff / 1000;
|
|
301 |
//if dateTo - last_interaction_date >= notification.getFrequency//last_interaction_date.compareTo(dateFrom) == -1 &&
|
|
302 |
|
|
303 |
if (diff >= (notification.getFrequency() * 3600)) {
|
|
304 |
if (fetchClaimHandler.fetchNumberOfClaimsByDateAndOpenaireId(last_interaction_date, dateTo, notification.getOpenaireId(), null, null, "", "source", true, types, false) > 0) {
|
|
305 |
List<String> managersByNotification = new ArrayList<>();
|
|
306 |
managersByNotification.add(notification.getUserMail());
|
|
307 |
|
|
308 |
// We need that to get name of community
|
|
309 |
context = fetchContextHandler.fetchFirstContextByCommunityId(notification.getOpenaireId());
|
|
310 |
|
|
311 |
logger.debug("Sending email for community claims between " + last_interaction_date + " and " + dateTo + " to " + notification.getUserMail());
|
|
312 |
|
|
313 |
//send(context.getOpenaireId().split("::")[0], context.getTitle().split(">")[0], "community", managersByNotification);
|
|
314 |
}
|
|
315 |
Calendar cal = Calendar.getInstance();
|
|
316 |
cal.setTime(_dateTo);
|
|
317 |
cal.add(Calendar.SECOND, 1);
|
|
318 |
_dateTo = cal.getTime();
|
|
319 |
notificationHandler.updateNotificationLastInteractionDate(notification.getOpenaireId(), notification.getUserMail(), _dateTo);
|
|
320 |
//update last interaction date (db)
|
|
321 |
}
|
|
322 |
}
|
|
323 |
}
|
|
324 |
}
|
|
325 |
} catch (Exception e) {
|
|
326 |
e.printStackTrace();
|
|
327 |
} catch (SQLStoreException e) {
|
|
328 |
e.printStackTrace();
|
|
329 |
}
|
|
330 |
}
|
|
331 |
|
|
332 |
public void send(String openaire_id, String openaire_name, String type, /*String token,*/ List<String> managers) {
|
|
333 |
final String openaireClaimsPageUrl = openaireClaimsPage + openaire_id;//token;
|
|
334 |
|
117 |
335 |
// Get system properties
|
118 |
336 |
Properties properties = System.getProperties();
|
119 |
337 |
properties.setProperty("mail.smtp.host", host);
|
... | ... | |
137 |
355 |
|
138 |
356 |
// Set To: header field of the header.
|
139 |
357 |
for(String to : managers) {
|
|
358 |
logger.debug(to);
|
140 |
359 |
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
|
141 |
360 |
}
|
142 |
361 |
|
... | ... | |
146 |
365 |
// For simple text setText() can be used instead of setContent()
|
147 |
366 |
|
148 |
367 |
// Send the actual HTML message, as big as you like
|
149 |
|
message.setContent("There are new Claims for the project: '" + project.getName() +"' for which you seem to be a contact person." +
|
|
368 |
message.setContent("There are new Claims for the "+type+": '" + openaire_name +"' for which you seem to be a contact person." +
|
150 |
369 |
"<br>Click <a href=\""+openaireClaimsPageUrl+"\">here</a> to curate these Claims.", "text/html");
|
151 |
370 |
|
152 |
371 |
// Send message
|
... | ... | |
165 |
384 |
this.fetchClaimHandler = fetchClaimHandler;
|
166 |
385 |
}
|
167 |
386 |
|
168 |
|
public void setProjectHandler(ProjectHandler projectHandler) {
|
169 |
|
this.projectHandler = projectHandler;
|
|
387 |
public void setFetchProjectHandler(FetchProjectHandler fetchProjectHandler) {
|
|
388 |
this.fetchProjectHandler = fetchProjectHandler;
|
170 |
389 |
}
|
171 |
390 |
|
|
391 |
public void setFetchNotificationHandler(FetchNotificationHandler fetchNotificationHandler) {
|
|
392 |
this.fetchNotificationHandler = fetchNotificationHandler;
|
|
393 |
}
|
|
394 |
|
|
395 |
public void setFetchContextHandler(FetchContextHandler fetchContextHandler) {
|
|
396 |
this.fetchContextHandler = fetchContextHandler;
|
|
397 |
}
|
|
398 |
|
172 |
399 |
public static void setOpenaireClaimsPage(String openaireClaimsPage) {
|
173 |
400 |
EmailSender.openaireClaimsPage = openaireClaimsPage;
|
174 |
401 |
}
|
1. FetchClaimHandler.java: add method fetchNumberOfClaimsByDateAndOpenaireId.
2. CommunityUtils.java: request datasource API to get community information (including managers).
3. FetchContextHandler.java: Method fetchFirstContextByCommunityId to parse name of community to be used in the above API.
4. EmailSender.java: Completely changed:
No token in project | send emails to project AND community managers | send emails according to notification table - if no preferences (no entry in table), use default settings.
5. springContext-claimsDemo.properties: Add property 'communityAPI'.
6. springContext-claimsDemo.xml: Add beans 'fetchContextHandler', 'CommunityUtils', properties for EmailSender.