Project

General

Profile

1 54525 panagiotis
package eu.dnetlib.repo.manager.service;
2 40367 nikon.gasp
3 49855 stefania.m
import eu.dnetlib.domain.data.PiwikInfo;
4 53933 panagiotis
import eu.dnetlib.domain.data.Repository;
5 56961 ioannis.di
import eu.dnetlib.domain.data.RepositoryInterface;
6 54149 panagiotis
import eu.dnetlib.domain.functionality.validator.JobForValidation;
7 54525 panagiotis
import eu.dnetlib.repo.manager.config.CascadingPropertyLoader;
8 57741 ioannis.di
import eu.dnetlib.repo.manager.domain.ValidationServiceException;
9 40367 nikon.gasp
import eu.dnetlib.utils.MailLibrary;
10
import org.apache.log4j.Logger;
11 57912 stefania.m
import org.mitre.openid.connect.model.OIDCAuthenticationToken;
12 40367 nikon.gasp
import org.springframework.beans.factory.annotation.Autowired;
13
import org.springframework.beans.factory.annotation.Value;
14 53933 panagiotis
import org.springframework.security.core.Authentication;
15 56961 ioannis.di
import org.springframework.security.core.context.SecurityContextHolder;
16 40367 nikon.gasp
import org.springframework.stereotype.Component;
17
18 52781 panagiotis
import javax.annotation.PostConstruct;
19 40367 nikon.gasp
import java.io.PrintWriter;
20
import java.io.StringWriter;
21
import java.io.Writer;
22
import java.util.ArrayList;
23
import java.util.List;
24 56961 ioannis.di
import java.util.stream.Collectors;
25 40367 nikon.gasp
26
27 51525 panagiotis
@Component("emailUtils")
28 40367 nikon.gasp
public class EmailUtilsImpl implements EmailUtils {
29
30
    private static Logger LOGGER = Logger.getLogger(EmailUtilsImpl.class);
31
32
    private List<String> specialRecipients = new ArrayList<String>();
33
    private boolean override = false, logonly = false;
34
    private String overrideEmail = null, from = null;
35
36 61363 spyroukon
    private final MailLibrary mailLibrary;
37
    private final CascadingPropertyLoader pLoader;
38
    private final RepositoryService repositoryService;
39 40367 nikon.gasp
40
    @Value("${services.repo-manager.baseUrl}")
41
    private String baseUrl;
42
43
    @Value("${services.repo-manager.adminEmail}")
44
    private String adminEmail;
45
46 49855 stefania.m
    @Value("${services.repomanager.usagestats.adminEmail}")
47
    private String usageStatsAdminEmail;
48
49 56636 antonis.le
    @Value("${services.provide.adminEmail}")
50
    private String provideAdminEmail;
51 52781 panagiotis
52 56961 ioannis.di
    @Value("${validator.results.url}")
53
    private String valBaseUrl;
54 56636 antonis.le
55 56961 ioannis.di
    @Autowired
56 61363 spyroukon
    EmailUtilsImpl(MailLibrary mailLibrary, CascadingPropertyLoader pLoader,
57
                   RepositoryService repositoryService) {
58
        this.mailLibrary = mailLibrary;
59
        this.pLoader = pLoader;
60
        this.repositoryService = repositoryService;
61
    }
62 56961 ioannis.di
63
64 52781 panagiotis
    @PostConstruct
65
    public void init(){
66 53933 panagiotis
        System.out.println("url -> " + this.baseUrl);
67 52781 panagiotis
    }
68
69
70 40367 nikon.gasp
    @Override
71
    public void reportException(Exception exception) {
72
        Writer writer = new StringWriter();
73
        PrintWriter printWriter = new PrintWriter(writer);
74
        exception.printStackTrace(printWriter);
75
76
        List<String> recipients = new ArrayList<String>();
77
78
        try {
79
            recipients.add(this.adminEmail);
80
            String message = "An exception has occurred:\n"+writer.toString();
81
            String subject = "Automatic Bug Report";
82
            this.sendMail(recipients, subject, message, false, null);
83
        } catch (Exception e) {
84
            LOGGER.error("Error sending error report", e);
85
        }
86
    }
87
88 49855 stefania.m
    @Override
89
    public void sendAdministratorRequestToEnableMetrics(PiwikInfo piwikInfo) throws Exception {
90
91
        try {
92
            String subject = "[OpenAIRE-Usage Statistics] New request to enable usage statistics";
93
94
            String message = "Dear administrator,\n" +
95
                    "\n" +
96
                    "we have received a request to enable the OpenAIRE usage statistics for the following repository \n" +
97
                    "\n" +
98
                    "Repository - " + piwikInfo.getRepositoryName() + ", " + piwikInfo.getCountry() + " (" + piwikInfo.getRepositoryId() + ")\n" +
99
                    "Requestor - " + piwikInfo.getRequestorName() + ", " + piwikInfo.getRequestorEmail() + "\n" +
100 57892 ioannis.di
                    "Matomo ID - " + piwikInfo.getSiteId() + "\n" +
101 49855 stefania.m
                    "Authentication token - " + piwikInfo.getAuthenticationToken() + "\n" +
102
                    "\n" +
103
                    "For more information about this request, go here: \n" +
104 52465 panagiotis
                    this.baseUrl + "/admin/metrics\n" +
105 49855 stefania.m
                    "\n" +
106
                    "Best,\n" +
107
                    "The OpenAIRE team";
108
109
            this.sendMail(this.usageStatsAdminEmail, subject, message, false, null);
110
111
        } catch (Exception e) {
112
            LOGGER.error("Error while sending request to enable metrics email to administrator: " + this.usageStatsAdminEmail, e);
113
            throw e;
114
        }
115
    }
116
117
    @Override
118
    public void sendUserRequestToEnableMetrics(PiwikInfo piwikInfo) throws Exception {
119
120
        try {
121
            String subject = "[OpenAIRE-Usage Statistics] Your request to enable usage statistics";
122
123
            String message = "Dear " + piwikInfo.getRequestorName() + ",\n" +
124
                    "\n" +
125
                    "we have received your request to enable the OpenAIRE usage statistics for your repository\n" +
126
                    "\n" +
127
                    "Repository - " + piwikInfo.getRepositoryName() + ", " + piwikInfo.getCountry() + " (" + piwikInfo.getRepositoryId() + ")\n" +
128 57892 ioannis.di
                    "Matomo ID - " + piwikInfo.getSiteId() + "\n" +
129 49855 stefania.m
                    "Authentication token - " + piwikInfo.getAuthenticationToken() + "\n" +
130
                    "\n" +
131
                    "In order to enable the usage statistics, you must install the OpenAIRE's tracking code in your repository software. " +
132
                    "OpenAIRE's usage statistics service tracking code is maintained on Github as a patch for various versions of DSpace " +
133
                    "(https://github.com/openaire/OpenAIRE-Piwik-DSpace) and as an Eprints plugin for version 3 " +
134
                    "(https://github.com/openaire/EPrints-OAPiwik). In case the platform is different from DSpace or EPrints please contact " +
135
                    "the OpenAIRE team in repositoryusagestats@openaire.eu in order to find a solution.\n" +
136
                    "\n" +
137
                    "For more information about your request and configuration details, go here: \n" +
138 52465 panagiotis
                    this.baseUrl + "/getImpact/instructions/" + piwikInfo.getRepositoryId() + "\n" +
139 49855 stefania.m
                    "\n" +
140
                    "Once you have finished configuring your repository or if you have any questions, please notify the OpenAIRE team by sending \n" +
141
                    "an email to repositoryusagestats@openaire.eu\n" +
142
                    "\n" +
143
                    "Best,\n" +
144
                    "The OpenAIRE team";
145
146
            this.sendMail(piwikInfo.getRequestorEmail(), subject, message, false, null);
147
148
        } catch (Exception e) {
149
            LOGGER.error("Error while sending request to enable metrics email to user: " + piwikInfo.getRequestorEmail(), e);
150
            throw e;
151
        }
152
    }
153
154
    @Override
155
    public void sendAdministratorMetricsEnabled(PiwikInfo piwikInfo) throws Exception {
156
157
        try {
158
            String subject = "[OpenAIRE-Usage Statistics] Usage statistics have been enabled";
159
160
            String message = "Dear administrator,\n" +
161
                    "\n" +
162
                    "The installation and configuration of OpenAIRE's tracking code for the following repository " +
163
                    "has been completed and validated and the usage statistics have been enabled in OpenAIRE.\n" +
164
                    "\n" +
165
                    "Repository - " + piwikInfo.getRepositoryName() + ", " + piwikInfo.getCountry() + " (" + piwikInfo.getRepositoryId() + ")\n" +
166
                    "Requestor - " + piwikInfo.getRequestorName() + ", " + piwikInfo.getRequestorEmail() + "\n" +
167
                    "Piwik ID - " + piwikInfo.getSiteId() + "\n" +
168
                    "Authentication token - " + piwikInfo.getAuthenticationToken() + "\n" +
169
                    "\n" +
170
                    "Best,\n" +
171
                    "The OpenAIRE team";
172
173 52465 panagiotis
            this.sendMail(this.usageStatsAdminEmail, subject, message, false, null);
174 49855 stefania.m
175
        } catch (Exception e) {
176
            LOGGER.error("Error while sending metrics enabled notification email to administator: " + this.usageStatsAdminEmail, e);
177
            throw e;
178
        }
179
    }
180
181
    @Override
182
    public void sendUserMetricsEnabled(PiwikInfo piwikInfo) throws Exception {
183
184
        try {
185
            String subject = "[OpenAIRE-Usage Statistics] Usage statistics have been enabled";
186
187
            String message = "Dear " + piwikInfo.getRequestorName() + ",\n" +
188
                    "\n" +
189
                    "The installation and configuration of OpenAIRE's tracking code for your repository \"" + piwikInfo.getRepositoryName() +
190
                    "\" has been completed and validated and the usage statistics have been enabled in OpenAIRE.\n" +
191
                    "\n" +
192
                    "You can preview the statistics in your repository's dashboard: \n" +
193 52465 panagiotis
                    this.baseUrl + "/getImpact/" + piwikInfo.getRepositoryId() + "\n" +
194 49855 stefania.m
                    "\n" +
195
                    " For more information and questions, you can contact the openaire support team by sending an email to " +
196
                    "repositoryusagestats@openaire.eu\n" +
197
                    "\n" +
198
                    "Best,\n" +
199
                    "The OpenAIRE team";
200
201
            this.sendMail(piwikInfo.getRequestorEmail(), subject, message, false, null);
202
203
        } catch (Exception e) {
204
            LOGGER.error("Error while sending metrics enabled notification email to user: " + piwikInfo.getRequestorEmail(), e);
205
            throw e;
206
        }
207
    }
208
209 53933 panagiotis
    @Override
210 56636 antonis.le
    public void sendAdminRegistrationEmail(Repository repository, Authentication authentication) throws Exception {
211 54078 panagiotis
        try {
212 57912 stefania.m
            String subject = "OpenAIRE content provider registration for " +
213 53933 panagiotis
                    repository.getDatasourceType() + "[" + repository.getEnglishName() + "]";
214
215 56636 antonis.le
            String message = "Dear administrator" + ",\n" +
216
                    "\n" +
217
                    "We received a request to register the " + repository.getDatasourceType() + "[" + repository.getEnglishName() + "]" +
218
                    " to the OpenAIRE compliant list of content providers. " +
219
                    "\n\n" +
220 57912 stefania.m
                    "User Contact: " + authentication.getName() + " (" + ((OIDCAuthenticationToken) authentication).getUserInfo().getEmail() + ")" +
221
                    "\n\n" +
222 56636 antonis.le
                    "Please do not reply to this message\n" +
223
                    "This message has been generated automatically.\n\n" +
224
                    "Regards,\n" +
225
                    "the OpenAIRE technical team\n";
226
227
            this.sendMail(this.provideAdminEmail, subject, message, false, null);
228
229
        } catch (Exception e) {
230
            LOGGER.error("Error while sending registration notification email to the administrator", e);
231
            throw e;
232
        }
233
    }
234
235
    @Override
236
    public void sendUserRegistrationEmail(Repository repository, Authentication authentication) throws Exception {
237
        try {
238 57912 stefania.m
            String subject = "OpenAIRE content provider registration for " +
239 56636 antonis.le
                    repository.getDatasourceType() + "[" + repository.getEnglishName() + "]";
240
241 56961 ioannis.di
//            String message = "Dear " + ((OIDCAuthenticationToken) authentication).getUserInfo().getName() + ",\n" +
242 57155 ioannis.di
            String message = "Dear "+SecurityContextHolder.getContext().getAuthentication().getName()+",\n" +
243 53933 panagiotis
                    "\n" +
244 54149 panagiotis
                    "We received a request to register the " + repository.getDatasourceType() + "[" + repository.getEnglishName() + "]" +
245 56636 antonis.le
                    " to the OpenAIRE compliant list of content providers. " +
246
                    "\n\n" +
247
                    "Please do not reply to this message\n" +
248
                    "This message has been generated automatically.\n\n" +
249 54149 panagiotis
                    "If you have any questions, write to 'helpdesk@openaire.eu'. \n\n" +
250 53933 panagiotis
                    "Regards,\n" +
251
                    "the OpenAIRE technical team\n";
252
253
            this.sendMail(repository.getRegisteredBy(), subject, message, false, null);
254
255
        } catch (Exception e) {
256
            LOGGER.error("Error while sending registration notification email to user: " + repository.getRegisteredBy(), e);
257
            throw e;
258 54078 panagiotis
        }
259 53933 panagiotis
    }
260
261
    @Override
262 59190 antonis.le
    public void sendAdminRegisterInterfaceEmail(Repository repository, String comment, RepositoryInterface repositoryInterface, Authentication authentication) throws Exception {
263 57912 stefania.m
        try {
264
            String subject = "OpenAIRE new interface registration request started for " +
265
                    repository.getDatasourceType() + "[" + repository.getEnglishName() + "]";
266
267
            String message = "Dear administrator" + ",\n" +
268
                    "\n" +
269
                    "We received a request to add the following interface: \n\n" +
270
                    "Base URL: " + repositoryInterface.getBaseUrl() + "\n" +
271
                    "Set: " + repositoryInterface.getAccessSet() + "\n" +
272
                    "Guidelines: " + repositoryInterface.getDesiredCompatibilityLevel() + "\n\n" +
273 59190 antonis.le
                    "to " + repository.getDatasourceType() + "[" + repository.getEnglishName() + "].\n";
274
275
            if (comment != null)
276 59497 antonis.le
                message += "\nThe users comment was '" + comment + "'\n";
277 59190 antonis.le
278
            message += "A validation process for this interface against the OpenAIRE guidelines compatibility " +
279 57912 stefania.m
                    "has been started. You will be informed in another message once the process is finished." +
280
                    "\n\n" +
281
                    "User Contact: " + authentication.getName() + " (" + ((OIDCAuthenticationToken) authentication).getUserInfo().getEmail() + ")" +
282
                    "\n\n" +
283
                    "Please do not reply to this message\n" +
284
                    "This message has been generated automatically.\n\n" +
285
                    "Regards,\n" +
286
                    "the OpenAIRE technical team\n";
287
288
            this.sendMail(this.provideAdminEmail, subject, message, false, null);
289
290
        } catch (Exception e) {
291
            LOGGER.error("Error while sending registration of interface notification email to the administrator", e);
292
            throw e;
293
        }
294
    }
295
296
    @Override
297 59190 antonis.le
    public void sendUserRegisterInterfaceEmail(Repository repository, String comment, RepositoryInterface repositoryInterface, Authentication authentication) throws Exception {
298 57912 stefania.m
        try {
299
            String subject = "OpenAIRE new interface registration request started for " +
300
                    repository.getDatasourceType() + "[" + repository.getEnglishName() + "]";
301 59198 antonis.le
302 57912 stefania.m
            String message = "Dear "+SecurityContextHolder.getContext().getAuthentication().getName()+",\n" +
303
                    "\n" +
304
                    "We received a request to add the following interface: \n\n" +
305
                    "Base URL: " + repositoryInterface.getBaseUrl() + "\n" +
306
                    "Set: " + repositoryInterface.getAccessSet() + "\n" +
307
                    "Guidelines: " + repositoryInterface.getDesiredCompatibilityLevel() + "\n\n" +
308 59190 antonis.le
                    "to " + repository.getDatasourceType() + "[" + repository.getEnglishName() + "].\n";
309
310
            if (comment != null) {
311 59497 antonis.le
                message += "\n Your comment was '" + comment + "'\n";
312 59190 antonis.le
            }
313
314
            message += "A validation process for this interface against the OpenAIRE guidelines compatibility " +
315 57912 stefania.m
                    "has been started. You will be informed in another message once the process is finished." +
316
                    "\n\n" +
317
                    "Please do not reply to this message\n" +
318
                    "This message has been generated automatically.\n\n" +
319
                    "Regards,\n" +
320
                    "the OpenAIRE technical team\n";
321
322
            this.sendMail(repository.getRegisteredBy(), subject, message, false, null);
323
324
        } catch (Exception e) {
325
            LOGGER.error("Error while sending registration of interface notification email to user: " + repository.getRegisteredBy(), e);
326
            throw e;
327
        }
328
    }
329
330
    @Override
331 56961 ioannis.di
    public void sendUserRegistrationResultsSuccessEmail(String issuerEmail, String jobId, RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) throws Exception {
332
        try {
333 57912 stefania.m
            String subject = "OpenAIRE new interface registration request - results (success) for " +
334 56961 ioannis.di
                    repository.getDatasourceType() + "[" + repository.getEnglishName() + "]";
335
336
//            String message = "Dear " + ((OIDCAuthenticationToken) authentication).getUserInfo().getName() + ",\n" +
337
            String message = "Dear user,\n" +
338
                    "\n" +
339
                    "the compatibility test on " + "[" + repository.getEnglishName() + "]" +
340
                    " was successful and the datasource type \""+ repository.getDatasourceType()  + "\" will be prepared for aggregation in OpenAIRE."+
341
                    "\n\n" +
342
                    "Please note that it usually takes about 3-4 weeks until a data source is indexed and it’s metadata visible on openaire.eu.\n\n" +
343
                    "Registration identifier in OpenAIRE: "+ repository.getNamespacePrefix()+
344
                    "\nOfficial Name:" + repository.getOfficialName() +
345
                    "\n\nBase URL: "+ repositoryInterface.getBaseUrl() +
346
                    "\n\nValidation Set: " + repositoryInterface.getAccessSet() +
347
                    "\n\nGuidelines: "+ repositoryInterface.getDesiredCompatibilityLevel() +
348
                    "\n\nYou can review the validation results here.\n" + valBaseUrl + "" + jobId +
349
                    "\n\n\nPlease do not reply to this email\n"+
350
                    "This message has been generated manually\n\n"+
351
                    "If you have any questions, write to 'helpdesk@openaire.eu'. \n\n" +
352
                    "Regards,\n" +
353
                    "the OpenAIRE technical team\n";
354
355
            this.sendMail(repository.getRegisteredBy(), subject, message, false, null);
356
357
        } catch (Exception e) {
358
            LOGGER.error("Error while sending registration notification email to user: " + repository.getRegisteredBy(), e);
359
            throw e;
360
        }
361
    }
362
363
    @Override
364
    public void sendAdminRegistrationResultsSuccessEmail(String issuerEmail, String jobId,RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) throws Exception {
365
        try {
366 57912 stefania.m
            String subject = "OpenAIRE new interface registration request - results (success) for " +
367 56961 ioannis.di
                    repository.getDatasourceType() + "[" + repository.getEnglishName() + "]";
368
369
            String message = "Dear admin ,\n" +
370
                    "\n" +
371
                    "the compatibility test on " + "[" + repository.getEnglishName() + "]" +
372
                    " was successful and the datasource type \""+ repository.getDatasourceType()  + "\" will be prepared for aggregation in OpenAIRE."+
373
                    "\n\n" +
374
                    "Please note that it usually takes about 3-4 weeks until a data source is indexed and it’s metadata visible on openaire.eu.\n\n" +
375
                    "Registration identifier in OpenAIRE: "+ repository.getNamespacePrefix()+
376
                    "\nOfficial Name:" + repository.getOfficialName() +
377
                    "\n\nBase URL: "+ repositoryInterface.getBaseUrl() +
378
                    "\n\nValidation Set: " + repositoryInterface.getAccessSet() +
379
                    "\n\nGuidelines: "+ repositoryInterface.getDesiredCompatibilityLevel() +
380
                    "\n\nUser Contact:"+ issuerEmail +""+
381
                    "\n\nYou can review the validation results here.\n" + valBaseUrl + "" + jobId +
382
                    "\n\n\nPlease do not reply to this email\n"+
383
                    "This message has been generated manually\n\n"+
384
                    "If you have any questions, write to 'helpdesk@openaire.eu'. \n\n" +
385
                    "Regards,\n" +
386
                    "the OpenAIRE technical team\n";
387
388
            this.sendMail(this.provideAdminEmail, subject, message, false, null);
389
390
        } catch (Exception e) {
391
            LOGGER.error("Error while sending registration notification email to user: " + repository.getRegisteredBy(), e);
392
            throw e;
393
        }
394
    }
395
396
    @Override
397
    public void sendUserRegistrationResultsFailureEmail(String jobId, RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) throws Exception {
398
        try {
399 57912 stefania.m
            String subject = "OpenAIRE new interface registration request - results (failure) for " +
400 56961 ioannis.di
                    repository.getDatasourceType() + "[" + repository.getEnglishName() + "]";
401
            //            String message = "Dear " + ((OIDCAuthenticationToken) authentication).getUserInfo().getName() + ",\n" +
402
            String message = "Dear user,\n" +
403
                    "\n" +
404
                    "the compatibility test on " + "[" + repository.getEnglishName() + "]" +
405
                    " was not successful and the registration process was interrupted."+
406
                    "\n\n" +
407
                    "We will check what caused the problem and get back to you within a couple of days.\n\n" +
408
                    "Registration identifier in OpenAIRE: "+ repository.getNamespacePrefix()+
409
                    "\nOfficial Name:" + repository.getOfficialName() +
410
                    "\n\nBase URL: "+ repositoryInterface.getBaseUrl() +
411
                    "\n\nValidation Set: " + repositoryInterface.getAccessSet() +
412
                    "\n\nGuidelines: "+ repositoryInterface.getDesiredCompatibilityLevel() +
413
                    "\n\nYou can review the validation results here.\n" + valBaseUrl + "" + jobId +
414
                    "\n\n\nPlease do not reply to this email\n"+
415
                    "This message has been generated manually\n\n"+
416
                    "If you have any questions, write to 'helpdesk@openaire.eu'. \n\n" +
417
                    "Regards,\n" +
418
                    "the OpenAIRE technical team\n";
419
420
            this.sendMail(repository.getRegisteredBy(), subject, message, false, null);
421
422
        } catch (Exception e) {
423
            LOGGER.error("Error while sending registration notification email to user: " + repository.getRegisteredBy(), e);
424
            throw e;
425
        }
426
    }
427
428
    @Override
429
    public void sendAdminRegistrationResultsFailureEmail(String issuerEmail, String jobId, RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) throws Exception {
430
        try {
431 57912 stefania.m
            String subject = "OpenAIRE new interface registration request - results (failure) for " +
432 56961 ioannis.di
                    repository.getDatasourceType() + "[" + repository.getEnglishName() + "]";
433
434
            String message = "Dear admin,\n" +
435
                    "\n" +
436
                    "the compatibility test on " + "[" + repository.getEnglishName() + "]" +
437
                    " was not successful and the registration process was interrupted."+
438
                    "\n\n" +
439
                    "We will check what caused the problem and get back to you within a couple of days.\n\n" +
440
                    "Registration identifier in OpenAIRE: "+ repository.getNamespacePrefix()+
441
                    "\nOfficial Name:" + repository.getOfficialName() +
442
                    "\n\nBase URL: "+ repositoryInterface.getBaseUrl() +
443
                    "\n\nValidation Set: " + repositoryInterface.getAccessSet() +
444
                    "\n\nGuidelines: "+ repositoryInterface.getDesiredCompatibilityLevel() +
445
                    "\n\nUser Contact:"+ issuerEmail +""+
446
                    "\n\nYou can review the validation results here.\n" + valBaseUrl + "" + jobId +
447
                    "\n\n\nPlease do not reply to this email\n"+
448
                    "This message has been generated manually\n\n"+
449
                    "If you have any questions, write to 'helpdesk@openaire.eu'. \n\n" +
450
                    "Regards,\n" +
451
                    "the OpenAIRE technical team\n";
452
453
            this.sendMail(this.provideAdminEmail, subject, message, false, null);
454
455
        } catch (Exception e) {
456
            LOGGER.error("Error while sending registration notification email to user: " + repository.getRegisteredBy(), e);
457
            throw e;
458
        }
459
    }
460
461
    @Override
462
    public void sendUserUpdateResultsSuccessEmail(String issuer, String jobId, RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) throws Exception {
463
        try {
464 57912 stefania.m
            String subject = "OpenAIRE interface update request - results (success) for " +
465 56961 ioannis.di
                    repository.getDatasourceType() + "[" + repository.getEnglishName() + "]";
466
467
            //            String message = "Dear " + ((OIDCAuthenticationToken) authentication).getUserInfo().getName() + ",\n" +
468
            String message = "Dear user,\n" +
469
                    "\n" +
470
                    "the compatibility test on [" + repository.getEnglishName()+"] has been successful\n\n" +
471
                    "We will check your transmitted information and adjust the aggregation settings accordingly. Please note that it usually takes about 3-4 weeks until the changes are visible on openaire.eu."+"\n\n" +
472
                    "Registration identifier in OpenAIRE: "+ repository.getNamespacePrefix()+
473
                    "\nOfficial Name:" + repository.getOfficialName() +
474
                    "\n\nBase URL: "+ repositoryInterface.getBaseUrl() +
475
                    "\n\nValidation Set: " + repositoryInterface.getAccessSet() +
476
                    "\n\nGuidelines: "+ repositoryInterface.getDesiredCompatibilityLevel() +
477
                    "\n\nYou can review the validation results here.\n" + valBaseUrl + "" + jobId +
478
                    "\n\n\nPlease do not reply to this email\n"+
479
                    "This message has been generated manually\n\n"+
480
                    "If you have any questions, write to 'helpdesk@openaire.eu'. \n\n" +
481
                    "Regards,\n" +
482
                    "the OpenAIRE technical team\n";
483
484
            this.sendMail(issuer, subject, message, false, null);
485
486
        } catch (Exception e) {
487
            LOGGER.error("Error while sending registration notification email to the administrator", e);
488
            throw e;
489
        }
490
    }
491
492
    @Override
493
    public void sendAdminUpdateResultsSuccessEmail(String issuerEmail, String jobId, RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) throws Exception {
494
            try {
495 57912 stefania.m
                String subject = "OpenAIRE interface update request - results (success) for " +
496 56961 ioannis.di
                        repository.getDatasourceType() + "[" + repository.getEnglishName() + "]";
497
498
                String message = "Dear admin,\n" +
499
                        "\n" +
500
                        "the compatibility test on [" + repository.getEnglishName()+"] has been successful\n\n" +
501
                        "We will check your transmitted information and adjust the aggregation settings accordingly. Please note that it usually takes about 3-4 weeks until the changes are visible on openaire.eu."+"\n\n" +
502
                        "Registration identifier in OpenAIRE: "+ repository.getNamespacePrefix()+
503
                        "\nOfficial Name:" + repository.getOfficialName() +
504
                        "\n\nBase URL: "+ repositoryInterface.getBaseUrl() +
505
                        "\n\nValidation Set: " + repositoryInterface.getAccessSet() +
506
                        "\n\nGuidelines: "+ repositoryInterface.getDesiredCompatibilityLevel() +
507
                        "\n\nUser Contact:"+ issuerEmail +""+
508
                        "\n\nYou can review the validation results here.\n" + valBaseUrl + "" + jobId +
509
                        "\n\n\nPlease do not reply to this email\n"+
510
                        "This message has been generated manually\n\n"+
511
                        "If you have any questions, write to 'helpdesk@openaire.eu'. \n\n" +
512
                        "Regards,\n" +
513
                        "the OpenAIRE technical team\n";
514
515
                this.sendMail(this.provideAdminEmail, subject, message, false, null);
516
517
            } catch (Exception e) {
518
                LOGGER.error("Error while sending registration notification email to the administrator", e);
519
                throw e;
520
            }
521
    }
522
523
    @Override
524
    public void sendUserUpdateResultsFailureEmail(String issuer, String jobId, RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) throws Exception {
525
        try {
526 57912 stefania.m
            String subject = "OpenAIRE interface update request - results (failure) for " +
527 56961 ioannis.di
                    repository.getDatasourceType() + "[" + repository.getEnglishName() + "]";
528
529
            //            String message = "Dear " + ((OIDCAuthenticationToken) authentication).getUserInfo().getName() + ",\n" +
530
            String message = "Dear user,\n" +
531
                    "\n" +
532
                    "the compatibility test on " + "[" + repository.getEnglishName() + "]" +
533
                    " was not successful."+
534
                    "\n\n" +
535
                    "WWe will check your transmitted information to see what caused the problem and get back to you within a couple of days.\n\n" +
536
                    "Registration identifier in OpenAIRE: "+ repository.getNamespacePrefix()+
537
                    "\nOfficial Name:" + repository.getOfficialName() +
538
                    "\n\nBase URL: "+ repositoryInterface.getBaseUrl() +
539
                    "\n\nValidation Set: " + repositoryInterface.getAccessSet() +
540
                    "\n\nGuidelines: "+ repositoryInterface.getDesiredCompatibilityLevel() +
541
                    "\n\nYou can review the validation results here.\n" + valBaseUrl + "" + jobId +
542
                    "\n\n\nPlease do not reply to this email\n"+
543
                    "This message has been generated manually\n\n"+
544
                    "If you have any questions, write to 'helpdesk@openaire.eu'. \n\n" +
545
                    "Regards,\n" +
546
                    "the OpenAIRE technical team\n";
547
548
            this.sendMail(issuer, subject, message, false, null);
549
550
        } catch (Exception e) {
551
            LOGGER.error("Error while sending registration notification email to user: " + repository.getRegisteredBy(), e);
552
            throw e;
553
        }
554
    }
555
556
    @Override
557
    public void sendAdminUpdateResultsFailureEmail(String issuerEmail, String jobId, RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) throws Exception {
558
        try {
559 57912 stefania.m
            String subject = "OpenAIRE interface update request - results (failure) for " +
560 56961 ioannis.di
                    repository.getDatasourceType() + "[" + repository.getEnglishName() + "]";
561
562
            String message = "Dear admin,\n" +
563
                    "\n" +
564
                    "the compatibility test on " + "[" + repository.getEnglishName() + "]" +
565
                    " was not successful."+
566
                    "\n\n" +
567
                    "WWe will check your transmitted information to see what caused the problem and get back to you within a couple of days.\n\n" +
568
                    "Registration identifier in OpenAIRE: "+ repository.getNamespacePrefix()+
569
                    "\nOfficial Name:" + repository.getOfficialName() +
570
                    "\n\nBase URL: "+ repositoryInterface.getBaseUrl() +
571
                    "\n\nValidation Set: " + repositoryInterface.getAccessSet() +
572
                    "\n\nGuidelines: "+ repositoryInterface.getDesiredCompatibilityLevel() +
573
                    "\n\nUser Contact:"+ issuerEmail +""+
574
                    "\n\nYou can review the validation results here.\n" + valBaseUrl + "" + jobId +
575
                    "\n\n\nPlease do not reply to this email\n"+
576
                    "This message has been generated manually\n\n"+
577
                    "If you have any questions, write to 'helpdesk@openaire.eu'. \n\n" +
578
                    "Regards,\n" +
579
                    "the OpenAIRE technical team\n";
580
581
            this.sendMail(this.provideAdminEmail, subject, message, false, null);
582
583
        } catch (Exception e) {
584
            LOGGER.error("Error while sending registration notification email to user: " + repository.getRegisteredBy(), e);
585
            throw e;
586
        }
587
    }
588
589
    @Override
590
    public void sendUserValidationResults(String issuer, String jobId, RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) throws Exception {
591
        try {
592
            String subject = "OpenAIRE validator - Test results ";
593
594
            //            String message = "Dear " + ((OIDCAuthenticationToken) authentication).getUserInfo().getName() + ",\n" +
595
            String message = "Dear user,\n" +
596
                    "\n" +
597
                    "the validation request you have submitted has finished. You can retrieve the results by following this url: "+ valBaseUrl+"" + jobId+" .\n\n" +
598
                    "Please do not reply to this message.\n" +
599
                    "This message has been generated automatically.\n" +
600
                    "If you have any questions, write to 'helpdesk@openaire.eu'. \n\n" +
601
                    "Regards,\n" +
602
                    "the OpenAIRE technical team\n";
603
604
            this.sendMail(issuer, subject, message, false, null);
605
606
        } catch (Exception e) {
607
            LOGGER.error("Error while sending validation submission notification email to user: " + issuer, e);
608
            throw e;
609
        }
610
    }
611
612
    @Override
613
    public void sendAdminValidationResults(String issuer, String jobId, RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) throws Exception {
614
        try {
615
            String subject = "OpenAIRE validator - Test results ";
616
617
            String message = "Dear admin,\n" +
618
                    "\n" +
619 57912 stefania.m
                    "a validation request has finished. You can retrieve the results by following this url: "+ valBaseUrl+"" + jobId+" .\n\n" +
620 56961 ioannis.di
                    "\n\nUser Contact:"+ issuer +""+
621
                    "Please do not reply to this message.\n" +
622
                    "This message has been generated automatically.\n" +
623
                    "If you have any questions, write to 'helpdesk@openaire.eu'. \n\n" +
624
                    "Regards,\n" +
625
                    "the OpenAIRE technical team\n";
626
627
            this.sendMail(this.provideAdminEmail, subject, message, false, null);
628
629
        } catch (Exception e) {
630
            LOGGER.error("Error while sending validation submission notification email to user: " + issuer, e);
631
            throw e;
632
        }
633
    }
634
635
    @Override
636
    public void sendAdminGeneralFailure(String issuer, String jobId, RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) throws Exception {
637
        try {
638
            String subject = "OpenAIRE validator - job failure";
639
640
            String message = "Dear admin,\n" +
641
                    "\n" +
642
                    "the validation job that was automatically submitted for the update/registration of the interface "+repositoryInterface.getId()+" ("+repositoryInterface.getBaseUrl()+", "+repositoryInterface.getAccessSet()+") of the repository "+repository.getId()+" ("+repository.getOfficialName()+") failed to complete." +
643
                    "This message has been generated automatically.\n\n" +
644
                    "Regards,\n" +
645
                    "the OpenAIRE technical team\n";
646
647
            this.sendMail(this.provideAdminEmail, subject, message, false, null);
648
649
        } catch (Exception e) {
650
            LOGGER.error("Error while sending validation submission notification email to user: " + issuer, e);
651
            throw e;
652
        }
653
    }
654
655
    @Override
656 57912 stefania.m
    public void sendAdminUpdateRepositoryInfoEmail(Repository repository, Authentication authentication) throws Exception {
657 56636 antonis.le
        try {
658 57912 stefania.m
            String subject = "OpenAIRE content provider update information for " +
659 56636 antonis.le
                    repository.getDatasourceType() + "[" + repository.getEnglishName() + "]";
660
661
            String message = "Dear administrator" + ",\n" +
662
                    "\n" +
663 57912 stefania.m
                    "We received a request to update the basic information for " + repository.getDatasourceType() + "[" + repository.getEnglishName() + "].\n\n" +
664
                    "Please do not reply to this message\n" +
665
                    "This message has been generated automatically.\n\n" +
666
                    "Regards,\n" +
667
                    "the OpenAIRE technical team\n";
668
669
            this.sendMail(this.provideAdminEmail, subject, message, false, null);
670
671
        } catch (Exception e) {
672
            LOGGER.error("Error while sending registration notification email to the administrator", e);
673
            throw e;
674
        }
675
    }
676
677
    @Override
678
    public void sendUserUpdateRepositoryInfoEmail(Repository repository, Authentication authentication) throws Exception {
679
        try {
680
            String subject = "OpenAIRE content provider update information for " +
681
                    repository.getDatasourceType() + "[" + repository.getEnglishName() + "]";
682
683
            //            String message = "Dear " + ((OIDCAuthenticationToken) authentication).getUserInfo().getName() + ",\n" +
684
            String message = "Dear user,\n" +
685
                    "\n" +
686
                    "We received a request to update the basic information for " + repository.getDatasourceType() + "[" + repository.getEnglishName() + "].\n\n" +
687
                    "Please do not reply to this message\n" +
688
                    "This message has been generated automatically.\n\n" +
689
                    "If you have any questions, write to 'helpdesk@openaire.eu'. \n\n" +
690
                    "Regards,\n" +
691
                    "the OpenAIRE technical team\n";
692
693
            this.sendMail(repository.getRegisteredBy(), subject, message, false, null);
694
695
        } catch (Exception e) {
696
            LOGGER.error("Error while sending registration notification email to user: " + repository.getRegisteredBy(), e);
697
            throw e;
698
        }
699
    }
700
701
    @Override
702 59190 antonis.le
    public void sendAdminUpdateInterfaceEmail(Repository repository, String comment, RepositoryInterface repositoryInterface, Authentication authentication) throws Exception {
703 57912 stefania.m
        try {
704
            String subject = "OpenAIRE interface update request started for " +
705
                    repository.getDatasourceType() + "[" + repository.getEnglishName() + "]";
706
707
            String message = "Dear administrator" + ",\n" +
708
                    "\n" +
709
                    "We received a request to update the following interface: \n\n" +
710
                    "Base URL: " + repositoryInterface.getBaseUrl() + "\n" +
711
                    "Set: " + repositoryInterface.getAccessSet() + "\n" +
712
                    "Guidelines: " + repositoryInterface.getDesiredCompatibilityLevel() + "\n\n" +
713 59190 antonis.le
                    "for " + repository.getDatasourceType() + "[" + repository.getEnglishName() + "].\n";
714
715
            if (comment != null)
716 59497 antonis.le
                message += "\nThe users comment was '" + comment + "'\n";
717 59190 antonis.le
718
            message += "A new iteration process of the validation against the OpenAIRE guidelines compatibility has been started.\n\n" +
719 57912 stefania.m
                    "User Contact: " + authentication.getName() + " (" + ((OIDCAuthenticationToken) authentication).getUserInfo().getEmail() + ")" +
720
                    "\n\n" +
721 56636 antonis.le
                    "Please do not reply to this message\n" +
722
                    "This message has been generated automatically.\n\n" +
723
                    "Regards,\n" +
724
                    "the OpenAIRE technical team\n";
725
726
            this.sendMail(this.provideAdminEmail, subject, message, false, null);
727
728
        } catch (Exception e) {
729
            LOGGER.error("Error while sending registration notification email to the administrator", e);
730
            throw e;
731
        }
732
    }
733
734
    @Override
735 59190 antonis.le
    public void sendUserUpdateInterfaceEmail(Repository repository, String comment, RepositoryInterface repositoryInterface, Authentication authentication) throws Exception {
736 54078 panagiotis
        try {
737 57912 stefania.m
            String subject = "OpenAIRE interface update request started for " +
738 54078 panagiotis
                    repository.getDatasourceType() + "[" + repository.getEnglishName() + "]";
739 53933 panagiotis
740 56961 ioannis.di
            //            String message = "Dear " + ((OIDCAuthenticationToken) authentication).getUserInfo().getName() + ",\n" +
741
            String message = "Dear user,\n" +
742 54078 panagiotis
                    "\n" +
743 57912 stefania.m
                    "We received a request to update the following interface: \n\n" +
744
                    "Base URL: " + repositoryInterface.getBaseUrl() + "\n" +
745
                    "Set: " + repositoryInterface.getAccessSet() + "\n" +
746
                    "Guidelines: " + repositoryInterface.getDesiredCompatibilityLevel() + "\n\n" +
747 59190 antonis.le
                    "for " + repository.getDatasourceType() + "[" + repository.getEnglishName() + "].\n";
748
749
            if (comment != null) {
750 59497 antonis.le
                message += "\n Your comment was '" + comment + "'\n";
751 59190 antonis.le
            }
752
753
            message += "A new iteration process of the validation against the OpenAIRE guidelines compatibility has been started.\n\n" +
754 56636 antonis.le
                    "Please do not reply to this message\n" +
755
                    "This message has been generated automatically.\n\n" +
756 54149 panagiotis
                    "If you have any questions, write to 'helpdesk@openaire.eu'. \n\n" +
757 54078 panagiotis
                    "Regards,\n" +
758
                    "the OpenAIRE technical team\n";
759
760
            this.sendMail(repository.getRegisteredBy(), subject, message, false, null);
761
762
        } catch (Exception e) {
763
            LOGGER.error("Error while sending registration notification email to user: " + repository.getRegisteredBy(), e);
764
            throw e;
765
        }
766 53933 panagiotis
    }
767
768 54149 panagiotis
    @Override
769
    public void sendSubmitJobForValidationEmail(Authentication authentication, JobForValidation jobForValidation) throws Exception {
770
        try {
771
            String subject = "OpenAIRE validator - Test submission ";
772
773 56961 ioannis.di
            //            String message = "Dear " + ((OIDCAuthenticationToken) authentication).getUserInfo().getName() + ",\n" +
774
            String message = "Dear user,\n" +
775 54149 panagiotis
                    "\n" +
776
                    "The validation request you have submitted has started.\n" +
777
                    "Please do not reply to this message.\n" +
778
                    "This message has been generated automatically.\n" +
779
                    "If you have any questions, write to 'helpdesk@openaire.eu'. \n\n" +
780
                    "Regards,\n" +
781
                    "the OpenAIRE technical team\n";
782
783
            this.sendMail(jobForValidation.getUserEmail(), subject, message, false, null);
784
785
        } catch (Exception e) {
786
            LOGGER.error("Error while sending validation submission notification email to user: " + jobForValidation.getUserEmail(), e);
787
            throw e;
788
        }
789
    }
790
791 56961 ioannis.di
    @Override
792
    public void sendUponJobCompletion(
793
            String repoId,
794
            String repoInterfaceId,
795
            int scoreUsage,
796
            int scoreContent,
797
            boolean isSuccess,
798
            boolean isUpdate,
799
            String issuerEmail,
800
            String jobId) throws Exception {
801
        List<RepositoryInterface> repositoryInterfaces = repositoryService.getRepositoryInterface(repoId);
802
        if(repositoryInterfaces.size()==0)
803
            throw new ValidationServiceException("Repository interface with id \""+repoInterfaceId+"\" not found",ValidationServiceException.ErrorCode.GENERAL_ERROR);
804
805
        RepositoryInterface repositoryInterface = repositoryInterfaces.stream().filter( repoInterface -> repoInterface.getId().equals(repoInterfaceId)).collect(Collectors.toList()).get(0);
806
        Repository repository = repositoryService.getRepositoryById(repoId);
807
808
        if(!isUpdate){
809
            if(isSuccess){
810
                if(scoreContent>=50 && scoreUsage >= 50){
811
                    this.sendUserRegistrationResultsSuccessEmail(issuerEmail,jobId,repositoryInterface,repository, SecurityContextHolder.getContext().getAuthentication());
812
                    this.sendAdminRegistrationResultsSuccessEmail(issuerEmail,jobId,repositoryInterface,repository,SecurityContextHolder.getContext().getAuthentication());
813
                }else{
814
                    this.sendUserRegistrationResultsFailureEmail(jobId,repositoryInterface,repository, SecurityContextHolder.getContext().getAuthentication());
815
                    this.sendAdminRegistrationResultsFailureEmail(issuerEmail,jobId,repositoryInterface,repository,SecurityContextHolder.getContext().getAuthentication());
816
                }
817
            }else{
818
                this.sendAdminGeneralFailure(issuerEmail,jobId,repositoryInterface,repository,SecurityContextHolder.getContext().getAuthentication());
819
            }
820
        }else{
821
            if(isSuccess){
822
                if(scoreContent>=50 && scoreUsage >= 50){
823
                    this.sendUserUpdateResultsSuccessEmail(issuerEmail,jobId,repositoryInterface,repository, SecurityContextHolder.getContext().getAuthentication());
824
                    this.sendAdminUpdateResultsSuccessEmail(issuerEmail,jobId,repositoryInterface,repository, SecurityContextHolder.getContext().getAuthentication());
825
                }else{
826
                    this.sendUserUpdateResultsFailureEmail(issuerEmail,jobId,repositoryInterface,repository, SecurityContextHolder.getContext().getAuthentication());
827
                    this.sendAdminUpdateResultsFailureEmail(issuerEmail,jobId,repositoryInterface,repository, SecurityContextHolder.getContext().getAuthentication());
828
                }
829
            }else{
830
                this.sendAdminGeneralFailure(issuerEmail,jobId,repositoryInterface,repository,SecurityContextHolder.getContext().getAuthentication());
831
            }
832
        }
833
834
    }
835
836
837 40367 nikon.gasp
    private void sendMail(String email, String subject, String message, boolean sendToSpecial, List<String> repoAdminMails) throws Exception {
838
        ArrayList<String> to = new ArrayList<String>();
839
        to.add(email);
840
        this.sendMail(to,subject,message,sendToSpecial,repoAdminMails);
841
    }
842
843 56961 ioannis.di
    public void sendMail(List<String> recipients, String subject, String message, boolean sendToSpecial, List<String> repoAdminMails) throws Exception {
844 40367 nikon.gasp
845 51525 panagiotis
        try {
846 40367 nikon.gasp
            if (sendToSpecial) {
847
                recipients.addAll(this.specialRecipients);
848
            }
849
850
            if (repoAdminMails != null)
851
                recipients.addAll(repoAdminMails);
852
853
            if (this.override) {
854
                recipients.clear();
855
                recipients.add(overrideEmail);
856
            }
857
            if (!logonly)
858
                mailLibrary.sendEmail(recipients.toArray(new String[]{}), subject, message);
859
            LOGGER.debug("Sending mail to Recipients: " + recipients + " Subject: " + subject + " Message: " + message);
860
        } catch (Exception e) {
861
            LOGGER.error("Error sending mail to Recipients: " + recipients + " Subject: " + subject + " Message: " + message, e);
862
            throw new Exception(e);
863 51525 panagiotis
        }
864 40367 nikon.gasp
    }
865
866
    private String getEmailProperty(String key) {
867
        return pLoader.getProperties().getProperty(key);
868
    }
869
870
    public void setSpecialRecipients(String specialRecipients) {
871
        String[] recps = specialRecipients.split(",");
872
873
        for (String recp : recps) {
874
            recp = recp.trim();
875
876
            this.specialRecipients.add(recp);
877
        }
878
    }
879
880
881
    public void setOverride(boolean override) {
882
        this.override = override;
883
    }
884
885
    public void setOverrideEmail(String overrideEmail) {
886
        this.overrideEmail = overrideEmail;
887
    }
888
889
    public String getFrom() {
890
        return from;
891
    }
892
893
    public void setFrom(String from) {
894
        this.from = from;
895
    }
896
897
    public boolean isLogonly() {
898
        return logonly;
899
    }
900
901
    public void setLogonly(boolean logonly) {
902
        this.logonly = logonly;
903
    }
904
905
}