Project

General

Profile

« Previous | Next » 

Revision 53916

1. pom.xml: Add commented plugin for surfire (possibly needed in openjdk: bug in their release) | update version of javax.mail.
2. FetchContextHandler.java: bug fix in parsing resultset.
3. QueryGenerator.java: bug fix in query of method 'generateSelectFirstContextByCommunityIdQuery'.
4. TestClass.java: Tests for sending email notifications added.
5. EmailScheduler.java: 'sendEmailNotifications' boolean variable added to control if email scheduler will run or not.
6. EmailSender.java: Fix subject and content of emails | bug fixes | bcc emails to Greece's test mail.
7. springContext-claimsDemo.properties: Add 'services.claims.mail.contactMail', 'services.claims.mail.openaireProjectClaimsPage',
'services.claims.mail.openaireCommunityClaimsPage', 'services.claims.mail.manageCommunityUserNotificationsPage' properties.
8. springContext-claimsDemo.xml: Add missing beans for 'EmailSender' and 'EmailScheduler' classes.

View differences:

modules/uoa-claims/trunk/src/main/java/eu/dnetlib/data/claims/migration/handler/FetchContextHandler.java
38 38
        Context context = null;
39 39
        while(rs.next()) {
40 40
            context = new Context();
41
            context.setOpenaireId(rs.getString(1));
42
            context.setTitle(rs.getString(2));
41
            context.setOpenaireId(rs.getString("openaire_id"));
42
            context.setTitle(rs.getString("context_title"));
43 43
        }
44 44
        return context;
45 45
    }
modules/uoa-claims/trunk/src/main/java/eu/dnetlib/data/emailSender/EmailScheduler.java
23 23
    private int delay = 0;
24 24
    private final Logger logger = Logger.getLogger(EmailScheduler.class);
25 25

  
26
    private static boolean sendEmailNotifications;
27

  
26 28
    @Autowired
27 29
    private String beautySleep;
28 30

  
......
44 46
                .getAutowireCapableBeanFactory()
45 47
                .autowireBean(this);
46 48

  
47
        logger.debug("Initializing EmailScheduler with beautySleep " + beautySleep + " and begin time " + targetHour+":"+targetMinute+":"+targetSecond + " and email sender " + emailSender);
48
        delay = getInitialDelaySeconds(Integer.parseInt(targetHour), Integer.parseInt(targetMinute), Integer.parseInt(targetSecond));
49
        logger.debug("EmailScheduler will wait "+delay + " seconds");
50
        
51
        final ScheduledFuture<?> resetHandle = scheduler.scheduleAtFixedRate(emailSender, delay, Integer.parseInt(beautySleep), SECONDS);
49
        if(sendEmailNotifications) {
50
            logger.debug("Initializing EmailScheduler with beautySleep " + beautySleep + " and begin time " + targetHour + ":" + targetMinute + ":" + targetSecond + " and email sender " + emailSender);
51
            delay = getInitialDelaySeconds(Integer.parseInt(targetHour), Integer.parseInt(targetMinute), Integer.parseInt(targetSecond));
52
            logger.debug("EmailScheduler will wait " + delay + " seconds");
53

  
54
            final ScheduledFuture<?> resetHandle = scheduler.scheduleAtFixedRate(emailSender, delay, Integer.parseInt(beautySleep), SECONDS);
55
        } else {
56
            logger.debug("Shutting down EmailScheduler for claim notifications");
57
            scheduler.shutdown();
58
        }
52 59
    }
53 60

  
54 61
    @Override
55 62
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
56
        logger.info("Shutting down EmailScheduler.");
57
        scheduler.shutdown();
63
        if(!scheduler.isShutdown()) {
64
            logger.info("Shutting down EmailScheduler.");
65
            scheduler.shutdown();
66
        } else {
67
            logger.info("EmailScheduler is already shutdown");
68
        }
58 69
    }
59 70

  
60 71
    private int getInitialDelaySeconds(int targetHour, int targetMinute, int targetSecond) {
......
84 95
        this.emailSender = emailSender;
85 96
    }
86 97

  
98
    public void setSendEmailNotifications(boolean sendEmailNotifications) { this.sendEmailNotifications = sendEmailNotifications; }
99

  
100
    public boolean getSendEmailNotifications() { return sendEmailNotifications; }
101

  
87 102
    public void setBeautySleep(String beautySleep) {
88 103
        this.beautySleep = beautySleep;
89 104
    }
modules/uoa-claims/trunk/src/main/java/eu/dnetlib/data/emailSender/EmailSender.java
36 36
    @Autowired
37 37
    private String defaultFrequencyInHours;
38 38

  
39
    private static String openaireClaimsPage;
39
    private static String manageCommunityUserNotificationsPage;
40
    private static String openaireProjectClaimsPage;
41
    private static String openaireCommunityClaimsPage;
40 42
    private static String username;
41 43
    private static String password;
42 44
    private static String host;
43 45
    private static String port;
44 46
    private static String from;
47
    private static String contactMail;
45 48
    private static String specialRecipients;
46 49

  
47 50
    @Override
......
96 99
                // specialRecipients are used currently for testing purposes
97 100
                List<String> tmpManagers = null;
98 101
                if (specialRecipients != null && !specialRecipients.isEmpty()) {
99
                    tmpManagers = Arrays.asList(specialRecipients.split("\\s*,\\s*"));
102
                    tmpManagers = new ArrayList<>(Arrays.asList(specialRecipients.split("\\s*,\\s*")));
100 103
                    logger.debug("Special recipients: " + specialRecipients);
104

  
105
                    if(tmpManagers != null) {
106
                        Iterator itr = tmpManagers.iterator();
107
                        while (itr.hasNext()) {
108
                            String manager = (String) itr.next();
109
                            Notification notification = null;
110
                            try {
111
                                notification = fetchNotificationHandler.fetchNotification(project.getOpenaireId(), manager);
112
                            } catch (Exception e) {
113
                                e.printStackTrace();
114
                            } catch (SQLStoreException e) {
115
                                e.printStackTrace();
116
                            }
117
                            if (notification != null) {
118
                                itr.remove();
119
                            }
120
                        }
121
                    }
122

  
101 123
                    managersOfProject.put(project.getOpenaireId(), tmpManagers);
102 124
                }
103 125

  
104
                /*
105 126
                // Send emails to actual project managers instead of special recipients
106 127
                List<String> managers = null;
107 128
                try {
108 129
                    managers = fetchProjectHandler.fetchContactEmailsByProjectId(project.getOpenaireId());
130
                    logger.debug("All actual Managers of project " + project.getOpenaireId() + ": "+managers);
131

  
109 132
                    if(managers != null) {
110 133
                        Iterator itr = managers.iterator();
111 134
                        while (itr.hasNext()) {
......
123 146
                    e.printStackTrace();
124 147
                }
125 148

  
126
                logger.debug("Managers of project " + project.getOpenaireId() + ": "+managers);
149
                logger.debug("Managers of project (not in notification table) " + project.getOpenaireId() + ": "+managers);
150
                /*
127 151
                managersOfProject.put(project.getOpenaireId(), managers);
128 152
                */
129 153

  
130 154
                if (managersOfProject.get(project.getOpenaireId()) != null &&
131 155
                        !managersOfProject.get(project.getOpenaireId()).isEmpty()) {
132
                    //send(project.getOpenaireId(), project.getName(), "project", managersOfProject.get(project.getOpenaireId()));
156
                    send(project.getOpenaireId(), project.getName(), "project", managersOfProject.get(project.getOpenaireId()));
133 157
                }
134 158
            }
135 159
        }
......
212 236

  
213 237
                if (managersOfCommunity.get(openaireId) != null &&
214 238
                        !managersOfCommunity.get(openaireId).isEmpty()) {
215
                    //send(openaireId, context.getTitle().split(">")[0], "community", managersOfCommunity.get(openaireId));
239
                    send(openaireId, context.getTitle().split(">")[0], "community", managersOfCommunity.get(openaireId));
216 240
                }
217 241
            }
218 242
        }
......
256 280

  
257 281
                                logger.debug("Sending email for project claims between " + last_interaction_date + " and " + dateTo + " to " + notification.getUserMail());
258 282

  
259
                                //send(project.getOpenaireId(), project.getName(), "project", managersByNotification);
283
                                send(project.getOpenaireId(), project.getName(), "project", managersByNotification);
260 284
                            }
261
                            /*Calendar cal = Calendar.getInstance();
262
                            cal.setTime(_dateTo);
263
                            cal.add(Calendar.SECOND, 1);
264
                            _dateTo = cal.getTime();*/
285

  
265 286
                            notificationHandler.updateNotificationLastInteractionDate(notification.getOpenaireId(), notification.getUserMail(), _dateTo);
266
                            //update last interaction date (db)
267 287
                        }
268 288
                    } else {
269 289
                        logger.debug("managers do not contain "+notification.getUserMail());
......
312 332
                        diff = diff / 1000;
313 333

  
314 334
                        if (diff >= (notification.getFrequency() * 3600)) {
315
                            if (fetchClaimHandler.fetchNumberOfClaimsByDateAndOpenaireId(last_interaction_date, dateTo, notification.getOpenaireId(), null, null, "", "source", true, types, false) > 0) {
335
                            if (fetchClaimHandler.fetchNumberOfClaimsByDateAndOpenaireId(last_interaction_date, dateTo, notification.getOpenaireId(), null, null, "", null, true, types, false) > 0) {
316 336
                                List<String> managersByNotification = new ArrayList<>();
317 337
                                managersByNotification.add(notification.getUserMail());
318 338

  
......
321 341

  
322 342
                                logger.debug("Sending email for community claims between " + last_interaction_date + " and " + dateTo + " to " + notification.getUserMail());
323 343

  
324
                                //send(context.getOpenaireId().split("::")[0], context.getTitle().split(">")[0], "community", managersByNotification);
344
                                send(context.getOpenaireId().split("::")[0], context.getTitle().split(">")[0], "community", managersByNotification);
325 345
                            }
326
                            /*Calendar cal = Calendar.getInstance();
327
                            cal.setTime(_dateTo);
328
                            cal.add(Calendar.SECOND, 1);
329
                            _dateTo = cal.getTime();*/
346

  
330 347
                            notificationHandler.updateNotificationLastInteractionDate(notification.getOpenaireId(), notification.getUserMail(), _dateTo);
331
                            //update last interaction date (db)
332 348
                        }
333 349
                    }
334 350
                }
......
340 356
        }
341 357
    }
342 358

  
343
    public void send(String openaire_id, String openaire_name, String type, /*String token,*/ List<String> managers) {
344
        final String openaireClaimsPageUrl = openaireClaimsPage + openaire_id;//token;
359
    public void send(String openaire_id, String openaire_name, String type, List<String> managers) {
360
        logger.debug("Sending email");
361
        String openaireClaimsPageUrl = "";
362
        String manageUserNotificationsPage = "";
363
        String messageContent = "";
364
        String subject = "";
345 365

  
366
        if(type.equals("project")) {
367
            openaireClaimsPageUrl = openaireProjectClaimsPage + openaire_id;
368

  
369
            subject = "[OpenAIRE] Links notification";
370
            messageContent = "There are new Claims for: '" + openaire_name +"' project for which you seem to be a contact person." +
371
                    "<br>Click <a href=\""+openaireClaimsPageUrl+"\">here</a> to curate these Claims.";
372
        } else if(type.equals("community")) {
373
            openaireClaimsPageUrl = openaireCommunityClaimsPage + openaire_id;
374
            manageUserNotificationsPage = manageCommunityUserNotificationsPage + openaire_id;
375

  
376
            subject = "[OpenAIRE-Connect] "+openaire_name+": Links notification";
377
            messageContent =
378
                    " <div style=\"font-size:14px;\">" +
379
                    "    <p>" +
380
                    "       There are new links for '"+openaire_name+"' community. Click  <a href=\""+openaireClaimsPageUrl+"\">here</a> to view the links.\n" +
381
                    "    </p>" +
382
                    "    <p>OpenAIRE team<br/>" +
383
                    "       <a href=\"https://www.openaire.eu\">www.openaire.eu</a>" +
384
                    "    </p>" +
385
                    "    <p style=\"font-size:11px;\">You are receiving this e-mail as manager of the community  <a href=\"https://beta."+openaire_id+".openaire.eu\">"+openaire_name+"</a>." +
386
                    "    If you are not responsible for this community, please <a href=\"mailto:"+contactMail+"\">contact us</a>." +
387
                    "    <br/>" +
388
                    "    Click  <a href=\""+manageUserNotificationsPage+"\">here</a> to manage your notification settings. </p>" +
389
                    "    </div>"
390
                    ;
391
        }
392

  
393
        //logger.debug(messageContent);
394

  
346 395
        // Get system properties
347 396
        Properties properties = System.getProperties();
348 397
        properties.setProperty("mail.smtp.host", host);
......
369 418
                logger.debug(to);
370 419
                message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
371 420
            }
421
            message.addRecipient(Message.RecipientType.BCC, new InternetAddress("openaire.test@gmail.com"));
372 422

  
373 423
            // Set Subject: header field
374
            message.setSubject("Openaire Claims Notification");
424
            message.setSubject(subject);
375 425

  
376 426
            // For simple text setText() can be used instead of setContent()
377 427

  
378 428
            // Send the actual HTML message, as big as you like
379
            message.setContent("There are new Claims for the "+type+": '" + openaire_name +"' for which you seem to be a contact person." +
380
                                        "<br>Click <a href=\""+openaireClaimsPageUrl+"\">here</a> to curate these Claims.", "text/html");
429
            message.setContent(messageContent, "text/html");
381 430

  
382 431
            // Send message
383 432
            Transport.send(message);
......
403 452
        this.fetchNotificationHandler = fetchNotificationHandler;
404 453
    }
405 454

  
455
    public void setNotificationHandler(NotificationHandler notificationHandler) {
456
        this.notificationHandler = notificationHandler;
457
    }
458

  
406 459
    public void setFetchContextHandler(FetchContextHandler fetchContextHandler) {
407 460
        this.fetchContextHandler = fetchContextHandler;
408 461
    }
409 462

  
410
    public static void setOpenaireClaimsPage(String openaireClaimsPage) {
411
        EmailSender.openaireClaimsPage = openaireClaimsPage;
463
    public static void setOpenaireProjectClaimsPage(String openaireProjectClaimsPage) {
464
        EmailSender.openaireProjectClaimsPage = openaireProjectClaimsPage;
412 465
    }
413 466

  
467
    public static void setOpenaireCommunityClaimsPage(String openaireCommunityClaimsPage) {
468
        EmailSender.openaireCommunityClaimsPage = openaireCommunityClaimsPage;
469
    }
470

  
471
    public static void setManageCommunityUserNotificationsPage(String manageCommunityUserNotificationsPage) {
472
        EmailSender.manageCommunityUserNotificationsPage = manageCommunityUserNotificationsPage;
473
    }
474

  
414 475
    public static void setUsername(String username) {
415 476
        EmailSender.username = username;
416 477
    }
......
431 492
        EmailSender.from = from;
432 493
    }
433 494

  
495
    public static void setContactMail(String contactMail) { EmailSender.contactMail = contactMail; }
496

  
434 497
    public static void setSpecialRecipients(String specialRecipients) {
435 498
        EmailSender.specialRecipients = specialRecipients;
436 499
    }
500

  
501
    public void setDefaultFrequencyInHours(String defaultFrequencyInHours) {
502
        this.defaultFrequencyInHours = defaultFrequencyInHours;
503
    }
437 504
}
modules/uoa-claims/trunk/src/main/java/eu/dnetlib/data/claimsDemo/TestClass.java
21 21
import eu.dnetlib.data.claims.migration.handler.*;
22 22
import eu.dnetlib.data.claims.migration.parser.DMFParser;
23 23
import eu.dnetlib.data.claims.migration.parser.ExternalRecordParser;
24
import eu.dnetlib.data.emailSender.EmailSender;
24 25
import gr.uoa.di.driver.util.ServiceLocator;
25 26
import org.apache.log4j.BasicConfigurator;
26 27
import org.apache.log4j.Logger;
......
79 80
    ClaimHandler claimHandler = null;
80 81
    ClaimValidation claimValidation = null;
81 82

  
83
    EmailSender emailSender = null;
82 84

  
85

  
83 86
    @Before
84 87
	public void init() throws Exception {
85 88
		BasicConfigurator.configure();
......
102 105
        fetchClaimHandler = context.getBean(FetchClaimHandler.class);
103 106
        claimValidation = context.getBean(ClaimValidation.class);
104 107

  
108
        emailSender = context.getBean(EmailSender.class);
109

  
105 110
        assertNotNull(sqlDAO);
106 111

  
107 112
	}
......
475 480
//        System.out.println("\n\nDelete Response is: "+directIndexHandler.deleteRecord("userclaim___::d1e668dc81fa714aa98a558d9ce515fa","openaire____::crossref"));
476 481
    }
477 482

  
483
    // To run following tests about email: claim notifications, set all the properties inside project folder
484
    @Test
485
    public void forceSendEmailNotifications() throws Exception {
486
        //emailSender.run();
487
    }
488

  
489
    @Test
490
    public void testEmail() throws Exception {
491
        ArrayList<String> list = new ArrayList<String>();
492
        list.add("konstantinagalouni@gmail.com");
493
        list.add("argirok@di.uoa.gr");
494
        //emailSender.send("openaire_id_test", "openaire_name_test", "community", list);
495
    }
496

  
478 497
}
modules/uoa-claims/trunk/src/main/java/eu/dnetlib/data/claimsDemo/QueryGenerator.java
1102 1102

  
1103 1103
    public  String generateSelectFirstContextByCommunityIdQuery(String communityId, ArrayList<Object> params) {
1104 1104
        params.add(communityId+"%");
1105
        return " Select " + getContextFields("context") + " from context where openaire_id like '?' LIMIT 1";
1105
        return " Select " + getContextFields("context") + " from context where openaire_id like ? LIMIT 1;";
1106 1106
    }
1107 1107

  
1108 1108
    public  String generateSelectProjectByIdQuery(String projectId, ArrayList<Object> params) {
modules/uoa-claims/trunk/src/main/resources/eu/dnetlib/data/claims/migration/springContext-claimsDemo.xml
117 117
        <property name="fetchClaimHandler" ref="fetchClaimHandler"/>
118 118
        <property name="fetchProjectHandler" ref="fetchProjectHandler"/>
119 119
        <property name="fetchNotificationHandler" ref="fetchNotificationHandler"/>
120
        <property name="notificationHandler" ref="notificationHandler"/>
120 121
        <property name="fetchContextHandler" ref="fetchContextHandler"/>
121
        <property name="openaireClaimsPage" value="${services.claims.mail.angularUrl}"/>
122
        <property name="manageCommunityUserNotificationsPage" value="${services.claims.mail.manageCommunityUserNotificationsPage}"/>
123
        <property name="openaireProjectClaimsPage" value="${services.claims.mail.openaireProjectClaimsPage}"/>
124
        <property name="openaireCommunityClaimsPage" value="${services.claims.mail.openaireCommunityClaimsPage}"/>
122 125
        <property name="username" value="${services.claims.mail.from}"/>
123 126
        <property name="password" value="${services.claims.mail.password}"/>
124 127
        <property name="host" value="${services.claims.mail.host}"/>
125 128
        <property name="port" value="${services.claims.mail.port}"/>
126 129
        <property name="from" value="${services.claims.mail.from}"/>
130
        <property name="contactMail" value="${services.claims.mail.contactMail}"/>
127 131
        <property name="specialRecipients" value="${services.claims.mail.specialRecipients}"/>
132
        <property name="defaultFrequencyInHours" ref="defaultFrequencyInHours"/>
128 133
    </bean>
129 134

  
130 135
    <bean id="CommunityUtils" class="eu.dnetlib.data.claimsDemo.CommunityUtils">
......
147 152
        <property name="updateOnInsert" value="true"/>
148 153
    </bean>
149 154

  
155
    <bean id="emailScheduler" class="eu.dnetlib.data.emailSender.EmailScheduler">
156
        <property name="sendEmailNotifications" value="true"/>
157
        <property name="beautySleep" ref="beautySleep"/>
158
        <property name="targetHour" ref="targetHour"/>
159
        <property name="targetMinute" ref="targetMinute"/>
160
        <property name="targetSecond" ref="targetSecond"/>
161
        <property name="emailSender" ref="emailSender"/>
162
    </bean>
163

  
150 164
    <bean id="beautySleep" class="java.lang.String">
151 165
        <constructor-arg type="String" value="86400"/>
152 166
    </bean>
modules/uoa-claims/trunk/src/main/resources/eu/dnetlib/data/claims/migration/springContext-claimsDemo.properties
13 13
services.claims.mail.port = 587
14 14
services.claims.mail.from = invalid_email@email.com
15 15
services.claims.mail.password = invalid_password
16
services.claims.mail.angularUrl = http://duffy.di.uoa.gr:3000/claims-project-manager?token=
17 16
services.claims.mail.specialRecipients = invalid_recipient@email.com
17
services.claims.mail.contactMail = openaire.test@gmail.com
18
services.claims.mail.openaireProjectClaimsPage = http://duffy.di.uoa.gr:4300/claims-project-manager?openaireId=
19
services.claims.mail.openaireCommunityClaimsPage = http://duffy.di.uoa.gr:4200/claims?communityId=
20
services.claims.mail.manageCommunityUserNotificationsPage = https://beta.admin.connect.openaire.eu/manage-user-notifications?communityId=
18 21

  
19 22
services.claimsDemo.results.pathToSaveRecord = /home/argirok/claims_www/records/
20 23
services.claimsDemo.reports.pathToSaveReport = /home/argirok/claims_www/reports/
modules/uoa-claims/trunk/pom.xml
58 58
		<dependency>
59 59
			<groupId>javax.mail</groupId>
60 60
			<artifactId>mail</artifactId>
61
			<version>1.4</version>
61
			<version>1.5.0-b01</version>
62 62
		</dependency>
63 63
		<dependency>
64 64
			<groupId>com.googlecode.json-simple</groupId>
......
66 66
			<version>1.1</version>
67 67
		</dependency>
68 68
	</dependencies>
69

  
70
	<!--3d answer: https://stackoverflow.com/questions/23260057/the-forked-vm-terminated-without-saying-properly-goodbye-vm-crash-or-system-exi-->
71
	<!--If you use openjdk there might be a problem with surfire plugin - uncomment following lines-->
72
	<!--<build>-->
73
		<!--<plugins>-->
74
			<!--<plugin>-->
75
				<!--<groupId>org.apache.maven.plugins</groupId>-->
76
				<!--<artifactId>maven-surefire-plugin</artifactId>-->
77
				<!--<version>2.19.1</version>-->
78
				<!--<configuration>-->
79
					<!--&lt;!&ndash;<testFailureIgnore>true</testFailureIgnore>&ndash;&gt;-->
80
					<!--<useSystemClassLoader>false</useSystemClassLoader>-->
81
				<!--</configuration>-->
82
			<!--</plugin>-->
83
		<!--</plugins>-->
84
		<!--<finalName>uoa-admin-tools</finalName>-->
85
	<!--</build>-->
69 86
</project>

Also available in: Unified diff