Project

General

Profile

1
package eu.dnetlib.repo.manager.service;
2

    
3
import eu.dnetlib.domain.data.PiwikInfo;
4
import eu.dnetlib.domain.data.Repository;
5
import eu.dnetlib.domain.data.RepositoryInterface;
6
import eu.dnetlib.domain.functionality.validator.JobForValidation;
7
import eu.dnetlib.repo.manager.config.CascadingPropertyLoader;
8
import eu.dnetlib.repo.manager.domain.ValidationServiceException;
9
import eu.dnetlib.utils.MailLibrary;
10
import org.apache.log4j.Logger;
11
import org.mitre.openid.connect.model.OIDCAuthenticationToken;
12
import org.springframework.beans.factory.annotation.Autowired;
13
import org.springframework.beans.factory.annotation.Value;
14
import org.springframework.security.core.Authentication;
15
import org.springframework.security.core.context.SecurityContextHolder;
16
import org.springframework.stereotype.Component;
17

    
18
import javax.annotation.PostConstruct;
19
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
import java.util.stream.Collectors;
25

    
26

    
27
@Component("emailUtils")
28
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
    @Autowired
37
    private MailLibrary mailLibrary;
38

    
39
    @Autowired
40
    private CascadingPropertyLoader pLoader;
41

    
42
    @Value("${services.repo-manager.baseUrl}")
43
    private String baseUrl;
44

    
45
    @Value("${services.repo-manager.adminEmail}")
46
    private String adminEmail;
47

    
48
    @Value("${services.repomanager.usagestats.adminEmail}")
49
    private String usageStatsAdminEmail;
50

    
51
    @Value("${services.provide.adminEmail}")
52
    private String provideAdminEmail;
53

    
54
    @Value("${validator.results.url}")
55
    private String valBaseUrl;
56

    
57
    @Autowired
58
    private RepositoryService repositoryService;
59

    
60

    
61
    @PostConstruct
62
    public void init(){
63
        System.out.println("url -> " + this.baseUrl);
64
    }
65

    
66

    
67
    @Override
68
    public void reportException(Exception exception) {
69
        Writer writer = new StringWriter();
70
        PrintWriter printWriter = new PrintWriter(writer);
71
        exception.printStackTrace(printWriter);
72

    
73
        List<String> recipients = new ArrayList<String>();
74

    
75
        try {
76
            recipients.add(this.adminEmail);
77
            String message = "An exception has occurred:\n"+writer.toString();
78
            String subject = "Automatic Bug Report";
79
            this.sendMail(recipients, subject, message, false, null);
80
        } catch (Exception e) {
81
            LOGGER.error("Error sending error report", e);
82
        }
83
    }
84

    
85
    @Override
86
    public void sendAdministratorRequestToEnableMetrics(PiwikInfo piwikInfo) throws Exception {
87

    
88
        try {
89
            String subject = "[OpenAIRE-Usage Statistics] New request to enable usage statistics";
90

    
91
            String message = "Dear administrator,\n" +
92
                    "\n" +
93
                    "we have received a request to enable the OpenAIRE usage statistics for the following repository \n" +
94
                    "\n" +
95
                    "Repository - " + piwikInfo.getRepositoryName() + ", " + piwikInfo.getCountry() + " (" + piwikInfo.getRepositoryId() + ")\n" +
96
                    "Requestor - " + piwikInfo.getRequestorName() + ", " + piwikInfo.getRequestorEmail() + "\n" +
97
                    "Matomo ID - " + piwikInfo.getSiteId() + "\n" +
98
                    "Authentication token - " + piwikInfo.getAuthenticationToken() + "\n" +
99
                    "\n" +
100
                    "For more information about this request, go here: \n" +
101
                    this.baseUrl + "/admin/metrics\n" +
102
                    "\n" +
103
                    "Best,\n" +
104
                    "The OpenAIRE team";
105

    
106
            this.sendMail(this.usageStatsAdminEmail, subject, message, false, null);
107

    
108
        } catch (Exception e) {
109
            LOGGER.error("Error while sending request to enable metrics email to administrator: " + this.usageStatsAdminEmail, e);
110
            throw e;
111
        }
112
    }
113

    
114
    @Override
115
    public void sendUserRequestToEnableMetrics(PiwikInfo piwikInfo) throws Exception {
116

    
117
        try {
118
            String subject = "[OpenAIRE-Usage Statistics] Your request to enable usage statistics";
119

    
120
            String message = "Dear " + piwikInfo.getRequestorName() + ",\n" +
121
                    "\n" +
122
                    "we have received your request to enable the OpenAIRE usage statistics for your repository\n" +
123
                    "\n" +
124
                    "Repository - " + piwikInfo.getRepositoryName() + ", " + piwikInfo.getCountry() + " (" + piwikInfo.getRepositoryId() + ")\n" +
125
                    "Matomo ID - " + piwikInfo.getSiteId() + "\n" +
126
                    "Authentication token - " + piwikInfo.getAuthenticationToken() + "\n" +
127
                    "\n" +
128
                    "In order to enable the usage statistics, you must install the OpenAIRE's tracking code in your repository software. " +
129
                    "OpenAIRE's usage statistics service tracking code is maintained on Github as a patch for various versions of DSpace " +
130
                    "(https://github.com/openaire/OpenAIRE-Piwik-DSpace) and as an Eprints plugin for version 3 " +
131
                    "(https://github.com/openaire/EPrints-OAPiwik). In case the platform is different from DSpace or EPrints please contact " +
132
                    "the OpenAIRE team in repositoryusagestats@openaire.eu in order to find a solution.\n" +
133
                    "\n" +
134
                    "For more information about your request and configuration details, go here: \n" +
135
                    this.baseUrl + "/getImpact/instructions/" + piwikInfo.getRepositoryId() + "\n" +
136
                    "\n" +
137
                    "Once you have finished configuring your repository or if you have any questions, please notify the OpenAIRE team by sending \n" +
138
                    "an email to repositoryusagestats@openaire.eu\n" +
139
                    "\n" +
140
                    "Best,\n" +
141
                    "The OpenAIRE team";
142

    
143
            this.sendMail(piwikInfo.getRequestorEmail(), subject, message, false, null);
144

    
145
        } catch (Exception e) {
146
            LOGGER.error("Error while sending request to enable metrics email to user: " + piwikInfo.getRequestorEmail(), e);
147
            throw e;
148
        }
149
    }
150

    
151
    @Override
152
    public void sendAdministratorMetricsEnabled(PiwikInfo piwikInfo) throws Exception {
153

    
154
        try {
155
            String subject = "[OpenAIRE-Usage Statistics] Usage statistics have been enabled";
156

    
157
            String message = "Dear administrator,\n" +
158
                    "\n" +
159
                    "The installation and configuration of OpenAIRE's tracking code for the following repository " +
160
                    "has been completed and validated and the usage statistics have been enabled in OpenAIRE.\n" +
161
                    "\n" +
162
                    "Repository - " + piwikInfo.getRepositoryName() + ", " + piwikInfo.getCountry() + " (" + piwikInfo.getRepositoryId() + ")\n" +
163
                    "Requestor - " + piwikInfo.getRequestorName() + ", " + piwikInfo.getRequestorEmail() + "\n" +
164
                    "Piwik ID - " + piwikInfo.getSiteId() + "\n" +
165
                    "Authentication token - " + piwikInfo.getAuthenticationToken() + "\n" +
166
                    "\n" +
167
                    "Best,\n" +
168
                    "The OpenAIRE team";
169

    
170
            this.sendMail(this.usageStatsAdminEmail, subject, message, false, null);
171

    
172
        } catch (Exception e) {
173
            LOGGER.error("Error while sending metrics enabled notification email to administator: " + this.usageStatsAdminEmail, e);
174
            throw e;
175
        }
176
    }
177

    
178
    @Override
179
    public void sendUserMetricsEnabled(PiwikInfo piwikInfo) throws Exception {
180

    
181
        try {
182
            String subject = "[OpenAIRE-Usage Statistics] Usage statistics have been enabled";
183

    
184
            String message = "Dear " + piwikInfo.getRequestorName() + ",\n" +
185
                    "\n" +
186
                    "The installation and configuration of OpenAIRE's tracking code for your repository \"" + piwikInfo.getRepositoryName() +
187
                    "\" has been completed and validated and the usage statistics have been enabled in OpenAIRE.\n" +
188
                    "\n" +
189
                    "You can preview the statistics in your repository's dashboard: \n" +
190
                    this.baseUrl + "/getImpact/" + piwikInfo.getRepositoryId() + "\n" +
191
                    "\n" +
192
                    " For more information and questions, you can contact the openaire support team by sending an email to " +
193
                    "repositoryusagestats@openaire.eu\n" +
194
                    "\n" +
195
                    "Best,\n" +
196
                    "The OpenAIRE team";
197

    
198
            this.sendMail(piwikInfo.getRequestorEmail(), subject, message, false, null);
199

    
200
        } catch (Exception e) {
201
            LOGGER.error("Error while sending metrics enabled notification email to user: " + piwikInfo.getRequestorEmail(), e);
202
            throw e;
203
        }
204
    }
205

    
206
    @Override
207
    public void sendAdminRegistrationEmail(Repository repository, Authentication authentication) throws Exception {
208
        try {
209
            String subject = "OpenAIRE content provider registration for " +
210
                    repository.getDatasourceType() + "[" + repository.getEnglishName() + "]";
211

    
212
            String message = "Dear administrator" + ",\n" +
213
                    "\n" +
214
                    "We received a request to register the " + repository.getDatasourceType() + "[" + repository.getEnglishName() + "]" +
215
                    " to the OpenAIRE compliant list of content providers. " +
216
                    "\n\n" +
217
                    "User Contact: " + authentication.getName() + " (" + ((OIDCAuthenticationToken) authentication).getUserInfo().getEmail() + ")" +
218
                    "\n\n" +
219
                    "Please do not reply to this message\n" +
220
                    "This message has been generated automatically.\n\n" +
221
                    "Regards,\n" +
222
                    "the OpenAIRE technical team\n";
223

    
224
            this.sendMail(this.provideAdminEmail, subject, message, false, null);
225

    
226
        } catch (Exception e) {
227
            LOGGER.error("Error while sending registration notification email to the administrator", e);
228
            throw e;
229
        }
230
    }
231

    
232
    @Override
233
    public void sendUserRegistrationEmail(Repository repository, Authentication authentication) throws Exception {
234
        try {
235
            String subject = "OpenAIRE content provider registration for " +
236
                    repository.getDatasourceType() + "[" + repository.getEnglishName() + "]";
237

    
238
//            String message = "Dear " + ((OIDCAuthenticationToken) authentication).getUserInfo().getName() + ",\n" +
239
            String message = "Dear "+SecurityContextHolder.getContext().getAuthentication().getName()+",\n" +
240
                    "\n" +
241
                    "We received a request to register the " + repository.getDatasourceType() + "[" + repository.getEnglishName() + "]" +
242
                    " to the OpenAIRE compliant list of content providers. " +
243
                    "\n\n" +
244
                    "Please do not reply to this message\n" +
245
                    "This message has been generated automatically.\n\n" +
246
                    "If you have any questions, write to 'helpdesk@openaire.eu'. \n\n" +
247
                    "Regards,\n" +
248
                    "the OpenAIRE technical team\n";
249

    
250
            this.sendMail(repository.getRegisteredBy(), subject, message, false, null);
251

    
252
        } catch (Exception e) {
253
            LOGGER.error("Error while sending registration notification email to user: " + repository.getRegisteredBy(), e);
254
            throw e;
255
        }
256
    }
257

    
258
    @Override
259
    public void sendAdminRegisterInterfaceEmail(Repository repository, String comment, RepositoryInterface repositoryInterface, Authentication authentication) throws Exception {
260
        try {
261
            String subject = "OpenAIRE new interface registration request started for " +
262
                    repository.getDatasourceType() + "[" + repository.getEnglishName() + "]";
263

    
264
            String message = "Dear administrator" + ",\n" +
265
                    "\n" +
266
                    "We received a request to add the following interface: \n\n" +
267
                    "Base URL: " + repositoryInterface.getBaseUrl() + "\n" +
268
                    "Set: " + repositoryInterface.getAccessSet() + "\n" +
269
                    "Guidelines: " + repositoryInterface.getDesiredCompatibilityLevel() + "\n\n" +
270
                    "to " + repository.getDatasourceType() + "[" + repository.getEnglishName() + "].\n";
271

    
272
            if (comment != null)
273
                message += "\nThe users comment was '" + comment + "'\n";
274

    
275
            message += "A validation process for this interface against the OpenAIRE guidelines compatibility " +
276
                    "has been started. You will be informed in another message once the process is finished." +
277
                    "\n\n" +
278
                    "User Contact: " + authentication.getName() + " (" + ((OIDCAuthenticationToken) authentication).getUserInfo().getEmail() + ")" +
279
                    "\n\n" +
280
                    "Please do not reply to this message\n" +
281
                    "This message has been generated automatically.\n\n" +
282
                    "Regards,\n" +
283
                    "the OpenAIRE technical team\n";
284

    
285
            this.sendMail(this.provideAdminEmail, subject, message, false, null);
286

    
287
        } catch (Exception e) {
288
            LOGGER.error("Error while sending registration of interface notification email to the administrator", e);
289
            throw e;
290
        }
291
    }
292

    
293
    @Override
294
    public void sendUserRegisterInterfaceEmail(Repository repository, String comment, RepositoryInterface repositoryInterface, Authentication authentication) throws Exception {
295
        try {
296
            String subject = "OpenAIRE new interface registration request started for " +
297
                    repository.getDatasourceType() + "[" + repository.getEnglishName() + "]";
298

    
299
            String message = "Dear "+SecurityContextHolder.getContext().getAuthentication().getName()+",\n" +
300
                    "\n" +
301
                    "We received a request to add the following interface: \n\n" +
302
                    "Base URL: " + repositoryInterface.getBaseUrl() + "\n" +
303
                    "Set: " + repositoryInterface.getAccessSet() + "\n" +
304
                    "Guidelines: " + repositoryInterface.getDesiredCompatibilityLevel() + "\n\n" +
305
                    "to " + repository.getDatasourceType() + "[" + repository.getEnglishName() + "].\n";
306

    
307
            if (comment != null) {
308
                message += "\n Your comment was '" + comment + "'\n";
309
            }
310

    
311
            message += "A validation process for this interface against the OpenAIRE guidelines compatibility " +
312
                    "has been started. You will be informed in another message once the process is finished." +
313
                    "\n\n" +
314
                    "Please do not reply to this message\n" +
315
                    "This message has been generated automatically.\n\n" +
316
                    "Regards,\n" +
317
                    "the OpenAIRE technical team\n";
318

    
319
            this.sendMail(repository.getRegisteredBy(), subject, message, false, null);
320

    
321
        } catch (Exception e) {
322
            LOGGER.error("Error while sending registration of interface notification email to user: " + repository.getRegisteredBy(), e);
323
            throw e;
324
        }
325
    }
326

    
327
    @Override
328
    public void sendUserRegistrationResultsSuccessEmail(String issuerEmail, String jobId, RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) throws Exception {
329
        try {
330
            String subject = "OpenAIRE new interface registration request - results (success) for " +
331
                    repository.getDatasourceType() + "[" + repository.getEnglishName() + "]";
332

    
333
//            String message = "Dear " + ((OIDCAuthenticationToken) authentication).getUserInfo().getName() + ",\n" +
334
            String message = "Dear user,\n" +
335
                    "\n" +
336
                    "the compatibility test on " + "[" + repository.getEnglishName() + "]" +
337
                    " was successful and the datasource type \""+ repository.getDatasourceType()  + "\" will be prepared for aggregation in OpenAIRE."+
338
                    "\n\n" +
339
                    "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" +
340
                    "Registration identifier in OpenAIRE: "+ repository.getNamespacePrefix()+
341
                    "\nOfficial Name:" + repository.getOfficialName() +
342
                    "\n\nBase URL: "+ repositoryInterface.getBaseUrl() +
343
                    "\n\nValidation Set: " + repositoryInterface.getAccessSet() +
344
                    "\n\nGuidelines: "+ repositoryInterface.getDesiredCompatibilityLevel() +
345
                    "\n\nYou can review the validation results here.\n" + valBaseUrl + "" + jobId +
346
                    "\n\n\nPlease do not reply to this email\n"+
347
                    "This message has been generated manually\n\n"+
348
                    "If you have any questions, write to 'helpdesk@openaire.eu'. \n\n" +
349
                    "Regards,\n" +
350
                    "the OpenAIRE technical team\n";
351

    
352
            this.sendMail(repository.getRegisteredBy(), subject, message, false, null);
353

    
354
        } catch (Exception e) {
355
            LOGGER.error("Error while sending registration notification email to user: " + repository.getRegisteredBy(), e);
356
            throw e;
357
        }
358
    }
359

    
360
    @Override
361
    public void sendAdminRegistrationResultsSuccessEmail(String issuerEmail, String jobId,RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) throws Exception {
362
        try {
363
            String subject = "OpenAIRE new interface registration request - results (success) for " +
364
                    repository.getDatasourceType() + "[" + repository.getEnglishName() + "]";
365

    
366
            String message = "Dear admin ,\n" +
367
                    "\n" +
368
                    "the compatibility test on " + "[" + repository.getEnglishName() + "]" +
369
                    " was successful and the datasource type \""+ repository.getDatasourceType()  + "\" will be prepared for aggregation in OpenAIRE."+
370
                    "\n\n" +
371
                    "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" +
372
                    "Registration identifier in OpenAIRE: "+ repository.getNamespacePrefix()+
373
                    "\nOfficial Name:" + repository.getOfficialName() +
374
                    "\n\nBase URL: "+ repositoryInterface.getBaseUrl() +
375
                    "\n\nValidation Set: " + repositoryInterface.getAccessSet() +
376
                    "\n\nGuidelines: "+ repositoryInterface.getDesiredCompatibilityLevel() +
377
                    "\n\nUser Contact:"+ issuerEmail +""+
378
                    "\n\nYou can review the validation results here.\n" + valBaseUrl + "" + jobId +
379
                    "\n\n\nPlease do not reply to this email\n"+
380
                    "This message has been generated manually\n\n"+
381
                    "If you have any questions, write to 'helpdesk@openaire.eu'. \n\n" +
382
                    "Regards,\n" +
383
                    "the OpenAIRE technical team\n";
384

    
385
            this.sendMail(this.provideAdminEmail, subject, message, false, null);
386

    
387
        } catch (Exception e) {
388
            LOGGER.error("Error while sending registration notification email to user: " + repository.getRegisteredBy(), e);
389
            throw e;
390
        }
391
    }
392

    
393
    @Override
394
    public void sendUserRegistrationResultsFailureEmail(String jobId, RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) throws Exception {
395
        try {
396
            String subject = "OpenAIRE new interface registration request - results (failure) for " +
397
                    repository.getDatasourceType() + "[" + repository.getEnglishName() + "]";
398
            //            String message = "Dear " + ((OIDCAuthenticationToken) authentication).getUserInfo().getName() + ",\n" +
399
            String message = "Dear user,\n" +
400
                    "\n" +
401
                    "the compatibility test on " + "[" + repository.getEnglishName() + "]" +
402
                    " was not successful and the registration process was interrupted."+
403
                    "\n\n" +
404
                    "We will check what caused the problem and get back to you within a couple of days.\n\n" +
405
                    "Registration identifier in OpenAIRE: "+ repository.getNamespacePrefix()+
406
                    "\nOfficial Name:" + repository.getOfficialName() +
407
                    "\n\nBase URL: "+ repositoryInterface.getBaseUrl() +
408
                    "\n\nValidation Set: " + repositoryInterface.getAccessSet() +
409
                    "\n\nGuidelines: "+ repositoryInterface.getDesiredCompatibilityLevel() +
410
                    "\n\nYou can review the validation results here.\n" + valBaseUrl + "" + jobId +
411
                    "\n\n\nPlease do not reply to this email\n"+
412
                    "This message has been generated manually\n\n"+
413
                    "If you have any questions, write to 'helpdesk@openaire.eu'. \n\n" +
414
                    "Regards,\n" +
415
                    "the OpenAIRE technical team\n";
416

    
417
            this.sendMail(repository.getRegisteredBy(), subject, message, false, null);
418

    
419
        } catch (Exception e) {
420
            LOGGER.error("Error while sending registration notification email to user: " + repository.getRegisteredBy(), e);
421
            throw e;
422
        }
423
    }
424

    
425
    @Override
426
    public void sendAdminRegistrationResultsFailureEmail(String issuerEmail, String jobId, RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) throws Exception {
427
        try {
428
            String subject = "OpenAIRE new interface registration request - results (failure) for " +
429
                    repository.getDatasourceType() + "[" + repository.getEnglishName() + "]";
430

    
431
            String message = "Dear admin,\n" +
432
                    "\n" +
433
                    "the compatibility test on " + "[" + repository.getEnglishName() + "]" +
434
                    " was not successful and the registration process was interrupted."+
435
                    "\n\n" +
436
                    "We will check what caused the problem and get back to you within a couple of days.\n\n" +
437
                    "Registration identifier in OpenAIRE: "+ repository.getNamespacePrefix()+
438
                    "\nOfficial Name:" + repository.getOfficialName() +
439
                    "\n\nBase URL: "+ repositoryInterface.getBaseUrl() +
440
                    "\n\nValidation Set: " + repositoryInterface.getAccessSet() +
441
                    "\n\nGuidelines: "+ repositoryInterface.getDesiredCompatibilityLevel() +
442
                    "\n\nUser Contact:"+ issuerEmail +""+
443
                    "\n\nYou can review the validation results here.\n" + valBaseUrl + "" + jobId +
444
                    "\n\n\nPlease do not reply to this email\n"+
445
                    "This message has been generated manually\n\n"+
446
                    "If you have any questions, write to 'helpdesk@openaire.eu'. \n\n" +
447
                    "Regards,\n" +
448
                    "the OpenAIRE technical team\n";
449

    
450
            this.sendMail(this.provideAdminEmail, subject, message, false, null);
451

    
452
        } catch (Exception e) {
453
            LOGGER.error("Error while sending registration notification email to user: " + repository.getRegisteredBy(), e);
454
            throw e;
455
        }
456
    }
457

    
458
    @Override
459
    public void sendUserUpdateResultsSuccessEmail(String issuer, String jobId, RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) throws Exception {
460
        try {
461
            String subject = "OpenAIRE interface update request - results (success) for " +
462
                    repository.getDatasourceType() + "[" + repository.getEnglishName() + "]";
463

    
464
            //            String message = "Dear " + ((OIDCAuthenticationToken) authentication).getUserInfo().getName() + ",\n" +
465
            String message = "Dear user,\n" +
466
                    "\n" +
467
                    "the compatibility test on [" + repository.getEnglishName()+"] has been successful\n\n" +
468
                    "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" +
469
                    "Registration identifier in OpenAIRE: "+ repository.getNamespacePrefix()+
470
                    "\nOfficial Name:" + repository.getOfficialName() +
471
                    "\n\nBase URL: "+ repositoryInterface.getBaseUrl() +
472
                    "\n\nValidation Set: " + repositoryInterface.getAccessSet() +
473
                    "\n\nGuidelines: "+ repositoryInterface.getDesiredCompatibilityLevel() +
474
                    "\n\nYou can review the validation results here.\n" + valBaseUrl + "" + jobId +
475
                    "\n\n\nPlease do not reply to this email\n"+
476
                    "This message has been generated manually\n\n"+
477
                    "If you have any questions, write to 'helpdesk@openaire.eu'. \n\n" +
478
                    "Regards,\n" +
479
                    "the OpenAIRE technical team\n";
480

    
481
            this.sendMail(issuer, subject, message, false, null);
482

    
483
        } catch (Exception e) {
484
            LOGGER.error("Error while sending registration notification email to the administrator", e);
485
            throw e;
486
        }
487
    }
488

    
489
    @Override
490
    public void sendAdminUpdateResultsSuccessEmail(String issuerEmail, String jobId, RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) throws Exception {
491
            try {
492
                String subject = "OpenAIRE interface update request - results (success) for " +
493
                        repository.getDatasourceType() + "[" + repository.getEnglishName() + "]";
494

    
495
                String message = "Dear admin,\n" +
496
                        "\n" +
497
                        "the compatibility test on [" + repository.getEnglishName()+"] has been successful\n\n" +
498
                        "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" +
499
                        "Registration identifier in OpenAIRE: "+ repository.getNamespacePrefix()+
500
                        "\nOfficial Name:" + repository.getOfficialName() +
501
                        "\n\nBase URL: "+ repositoryInterface.getBaseUrl() +
502
                        "\n\nValidation Set: " + repositoryInterface.getAccessSet() +
503
                        "\n\nGuidelines: "+ repositoryInterface.getDesiredCompatibilityLevel() +
504
                        "\n\nUser Contact:"+ issuerEmail +""+
505
                        "\n\nYou can review the validation results here.\n" + valBaseUrl + "" + jobId +
506
                        "\n\n\nPlease do not reply to this email\n"+
507
                        "This message has been generated manually\n\n"+
508
                        "If you have any questions, write to 'helpdesk@openaire.eu'. \n\n" +
509
                        "Regards,\n" +
510
                        "the OpenAIRE technical team\n";
511

    
512
                this.sendMail(this.provideAdminEmail, subject, message, false, null);
513

    
514
            } catch (Exception e) {
515
                LOGGER.error("Error while sending registration notification email to the administrator", e);
516
                throw e;
517
            }
518
    }
519

    
520
    @Override
521
    public void sendUserUpdateResultsFailureEmail(String issuer, String jobId, RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) throws Exception {
522
        try {
523
            String subject = "OpenAIRE interface update request - results (failure) for " +
524
                    repository.getDatasourceType() + "[" + repository.getEnglishName() + "]";
525

    
526
            //            String message = "Dear " + ((OIDCAuthenticationToken) authentication).getUserInfo().getName() + ",\n" +
527
            String message = "Dear user,\n" +
528
                    "\n" +
529
                    "the compatibility test on " + "[" + repository.getEnglishName() + "]" +
530
                    " was not successful."+
531
                    "\n\n" +
532
                    "WWe will check your transmitted information to see what caused the problem and get back to you within a couple of days.\n\n" +
533
                    "Registration identifier in OpenAIRE: "+ repository.getNamespacePrefix()+
534
                    "\nOfficial Name:" + repository.getOfficialName() +
535
                    "\n\nBase URL: "+ repositoryInterface.getBaseUrl() +
536
                    "\n\nValidation Set: " + repositoryInterface.getAccessSet() +
537
                    "\n\nGuidelines: "+ repositoryInterface.getDesiredCompatibilityLevel() +
538
                    "\n\nYou can review the validation results here.\n" + valBaseUrl + "" + jobId +
539
                    "\n\n\nPlease do not reply to this email\n"+
540
                    "This message has been generated manually\n\n"+
541
                    "If you have any questions, write to 'helpdesk@openaire.eu'. \n\n" +
542
                    "Regards,\n" +
543
                    "the OpenAIRE technical team\n";
544

    
545
            this.sendMail(issuer, subject, message, false, null);
546

    
547
        } catch (Exception e) {
548
            LOGGER.error("Error while sending registration notification email to user: " + repository.getRegisteredBy(), e);
549
            throw e;
550
        }
551
    }
552

    
553
    @Override
554
    public void sendAdminUpdateResultsFailureEmail(String issuerEmail, String jobId, RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) throws Exception {
555
        try {
556
            String subject = "OpenAIRE interface update request - results (failure) for " +
557
                    repository.getDatasourceType() + "[" + repository.getEnglishName() + "]";
558

    
559
            String message = "Dear admin,\n" +
560
                    "\n" +
561
                    "the compatibility test on " + "[" + repository.getEnglishName() + "]" +
562
                    " was not successful."+
563
                    "\n\n" +
564
                    "WWe will check your transmitted information to see what caused the problem and get back to you within a couple of days.\n\n" +
565
                    "Registration identifier in OpenAIRE: "+ repository.getNamespacePrefix()+
566
                    "\nOfficial Name:" + repository.getOfficialName() +
567
                    "\n\nBase URL: "+ repositoryInterface.getBaseUrl() +
568
                    "\n\nValidation Set: " + repositoryInterface.getAccessSet() +
569
                    "\n\nGuidelines: "+ repositoryInterface.getDesiredCompatibilityLevel() +
570
                    "\n\nUser Contact:"+ issuerEmail +""+
571
                    "\n\nYou can review the validation results here.\n" + valBaseUrl + "" + jobId +
572
                    "\n\n\nPlease do not reply to this email\n"+
573
                    "This message has been generated manually\n\n"+
574
                    "If you have any questions, write to 'helpdesk@openaire.eu'. \n\n" +
575
                    "Regards,\n" +
576
                    "the OpenAIRE technical team\n";
577

    
578
            this.sendMail(this.provideAdminEmail, subject, message, false, null);
579

    
580
        } catch (Exception e) {
581
            LOGGER.error("Error while sending registration notification email to user: " + repository.getRegisteredBy(), e);
582
            throw e;
583
        }
584
    }
585

    
586
    @Override
587
    public void sendUserValidationResults(String issuer, String jobId, RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) throws Exception {
588
        try {
589
            String subject = "OpenAIRE validator - Test results ";
590

    
591
            //            String message = "Dear " + ((OIDCAuthenticationToken) authentication).getUserInfo().getName() + ",\n" +
592
            String message = "Dear user,\n" +
593
                    "\n" +
594
                    "the validation request you have submitted has finished. You can retrieve the results by following this url: "+ valBaseUrl+"" + jobId+" .\n\n" +
595
                    "Please do not reply to this message.\n" +
596
                    "This message has been generated automatically.\n" +
597
                    "If you have any questions, write to 'helpdesk@openaire.eu'. \n\n" +
598
                    "Regards,\n" +
599
                    "the OpenAIRE technical team\n";
600

    
601
            this.sendMail(issuer, subject, message, false, null);
602

    
603
        } catch (Exception e) {
604
            LOGGER.error("Error while sending validation submission notification email to user: " + issuer, e);
605
            throw e;
606
        }
607
    }
608

    
609
    @Override
610
    public void sendAdminValidationResults(String issuer, String jobId, RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) throws Exception {
611
        try {
612
            String subject = "OpenAIRE validator - Test results ";
613

    
614
            String message = "Dear admin,\n" +
615
                    "\n" +
616
                    "a validation request has finished. You can retrieve the results by following this url: "+ valBaseUrl+"" + jobId+" .\n\n" +
617
                    "\n\nUser Contact:"+ issuer +""+
618
                    "Please do not reply to this message.\n" +
619
                    "This message has been generated automatically.\n" +
620
                    "If you have any questions, write to 'helpdesk@openaire.eu'. \n\n" +
621
                    "Regards,\n" +
622
                    "the OpenAIRE technical team\n";
623

    
624
            this.sendMail(this.provideAdminEmail, subject, message, false, null);
625

    
626
        } catch (Exception e) {
627
            LOGGER.error("Error while sending validation submission notification email to user: " + issuer, e);
628
            throw e;
629
        }
630
    }
631

    
632
    @Override
633
    public void sendAdminGeneralFailure(String issuer, String jobId, RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) throws Exception {
634
        try {
635
            String subject = "OpenAIRE validator - job failure";
636

    
637
            String message = "Dear admin,\n" +
638
                    "\n" +
639
                    "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." +
640
                    "This message has been generated automatically.\n\n" +
641
                    "Regards,\n" +
642
                    "the OpenAIRE technical team\n";
643

    
644
            this.sendMail(this.provideAdminEmail, subject, message, false, null);
645

    
646
        } catch (Exception e) {
647
            LOGGER.error("Error while sending validation submission notification email to user: " + issuer, e);
648
            throw e;
649
        }
650
    }
651

    
652
    @Override
653
    public void sendAdminUpdateRepositoryInfoEmail(Repository repository, Authentication authentication) throws Exception {
654
        try {
655
            String subject = "OpenAIRE content provider update information for " +
656
                    repository.getDatasourceType() + "[" + repository.getEnglishName() + "]";
657

    
658
            String message = "Dear administrator" + ",\n" +
659
                    "\n" +
660
                    "We received a request to update the basic information for " + repository.getDatasourceType() + "[" + repository.getEnglishName() + "].\n\n" +
661
                    "Please do not reply to this message\n" +
662
                    "This message has been generated automatically.\n\n" +
663
                    "Regards,\n" +
664
                    "the OpenAIRE technical team\n";
665

    
666
            this.sendMail(this.provideAdminEmail, subject, message, false, null);
667

    
668
        } catch (Exception e) {
669
            LOGGER.error("Error while sending registration notification email to the administrator", e);
670
            throw e;
671
        }
672
    }
673

    
674
    @Override
675
    public void sendUserUpdateRepositoryInfoEmail(Repository repository, Authentication authentication) throws Exception {
676
        try {
677
            String subject = "OpenAIRE content provider update information for " +
678
                    repository.getDatasourceType() + "[" + repository.getEnglishName() + "]";
679

    
680
            //            String message = "Dear " + ((OIDCAuthenticationToken) authentication).getUserInfo().getName() + ",\n" +
681
            String message = "Dear user,\n" +
682
                    "\n" +
683
                    "We received a request to update the basic information for " + repository.getDatasourceType() + "[" + repository.getEnglishName() + "].\n\n" +
684
                    "Please do not reply to this message\n" +
685
                    "This message has been generated automatically.\n\n" +
686
                    "If you have any questions, write to 'helpdesk@openaire.eu'. \n\n" +
687
                    "Regards,\n" +
688
                    "the OpenAIRE technical team\n";
689

    
690
            this.sendMail(repository.getRegisteredBy(), subject, message, false, null);
691

    
692
        } catch (Exception e) {
693
            LOGGER.error("Error while sending registration notification email to user: " + repository.getRegisteredBy(), e);
694
            throw e;
695
        }
696
    }
697

    
698
    @Override
699
    public void sendAdminUpdateInterfaceEmail(Repository repository, String comment, RepositoryInterface repositoryInterface, Authentication authentication) throws Exception {
700
        try {
701
            String subject = "OpenAIRE interface update request started for " +
702
                    repository.getDatasourceType() + "[" + repository.getEnglishName() + "]";
703

    
704
            String message = "Dear administrator" + ",\n" +
705
                    "\n" +
706
                    "We received a request to update the following interface: \n\n" +
707
                    "Base URL: " + repositoryInterface.getBaseUrl() + "\n" +
708
                    "Set: " + repositoryInterface.getAccessSet() + "\n" +
709
                    "Guidelines: " + repositoryInterface.getDesiredCompatibilityLevel() + "\n\n" +
710
                    "for " + repository.getDatasourceType() + "[" + repository.getEnglishName() + "].\n";
711

    
712
            if (comment != null)
713
                message += "\nThe users comment was '" + comment + "'\n";
714

    
715
            message += "A new iteration process of the validation against the OpenAIRE guidelines compatibility has been started.\n\n" +
716
                    "User Contact: " + authentication.getName() + " (" + ((OIDCAuthenticationToken) authentication).getUserInfo().getEmail() + ")" +
717
                    "\n\n" +
718
                    "Please do not reply to this message\n" +
719
                    "This message has been generated automatically.\n\n" +
720
                    "Regards,\n" +
721
                    "the OpenAIRE technical team\n";
722

    
723
            this.sendMail(this.provideAdminEmail, subject, message, false, null);
724

    
725
        } catch (Exception e) {
726
            LOGGER.error("Error while sending registration notification email to the administrator", e);
727
            throw e;
728
        }
729
    }
730

    
731
    @Override
732
    public void sendUserUpdateInterfaceEmail(Repository repository, String comment, RepositoryInterface repositoryInterface, Authentication authentication) throws Exception {
733
        try {
734
            String subject = "OpenAIRE interface update request started for " +
735
                    repository.getDatasourceType() + "[" + repository.getEnglishName() + "]";
736

    
737
            //            String message = "Dear " + ((OIDCAuthenticationToken) authentication).getUserInfo().getName() + ",\n" +
738
            String message = "Dear user,\n" +
739
                    "\n" +
740
                    "We received a request to update the following interface: \n\n" +
741
                    "Base URL: " + repositoryInterface.getBaseUrl() + "\n" +
742
                    "Set: " + repositoryInterface.getAccessSet() + "\n" +
743
                    "Guidelines: " + repositoryInterface.getDesiredCompatibilityLevel() + "\n\n" +
744
                    "for " + repository.getDatasourceType() + "[" + repository.getEnglishName() + "].\n";
745

    
746
            if (comment != null) {
747
                message += "\n Your comment was '" + comment + "'\n";
748
            }
749

    
750
            message += "A new iteration process of the validation against the OpenAIRE guidelines compatibility has been started.\n\n" +
751
                    "Please do not reply to this message\n" +
752
                    "This message has been generated automatically.\n\n" +
753
                    "If you have any questions, write to 'helpdesk@openaire.eu'. \n\n" +
754
                    "Regards,\n" +
755
                    "the OpenAIRE technical team\n";
756

    
757
            this.sendMail(repository.getRegisteredBy(), subject, message, false, null);
758

    
759
        } catch (Exception e) {
760
            LOGGER.error("Error while sending registration notification email to user: " + repository.getRegisteredBy(), e);
761
            throw e;
762
        }
763
    }
764

    
765
    @Override
766
    public void sendSubmitJobForValidationEmail(Authentication authentication, JobForValidation jobForValidation) throws Exception {
767
        try {
768
            String subject = "OpenAIRE validator - Test submission ";
769

    
770
            //            String message = "Dear " + ((OIDCAuthenticationToken) authentication).getUserInfo().getName() + ",\n" +
771
            String message = "Dear user,\n" +
772
                    "\n" +
773
                    "The validation request you have submitted has started.\n" +
774
                    "Please do not reply to this message.\n" +
775
                    "This message has been generated automatically.\n" +
776
                    "If you have any questions, write to 'helpdesk@openaire.eu'. \n\n" +
777
                    "Regards,\n" +
778
                    "the OpenAIRE technical team\n";
779

    
780
            this.sendMail(jobForValidation.getUserEmail(), subject, message, false, null);
781

    
782
        } catch (Exception e) {
783
            LOGGER.error("Error while sending validation submission notification email to user: " + jobForValidation.getUserEmail(), e);
784
            throw e;
785
        }
786
    }
787

    
788
    @Override
789
    public void sendUponJobCompletion(
790
            String repoId,
791
            String repoInterfaceId,
792
            int scoreUsage,
793
            int scoreContent,
794
            boolean isSuccess,
795
            boolean isUpdate,
796
            String issuerEmail,
797
            String jobId) throws Exception {
798
        List<RepositoryInterface> repositoryInterfaces = repositoryService.getRepositoryInterface(repoId);
799
        if(repositoryInterfaces.size()==0)
800
            throw new ValidationServiceException("Repository interface with id \""+repoInterfaceId+"\" not found",ValidationServiceException.ErrorCode.GENERAL_ERROR);
801

    
802
        RepositoryInterface repositoryInterface = repositoryInterfaces.stream().filter( repoInterface -> repoInterface.getId().equals(repoInterfaceId)).collect(Collectors.toList()).get(0);
803
        Repository repository = repositoryService.getRepositoryById(repoId);
804

    
805
        if(!isUpdate){
806
            if(isSuccess){
807
                if(scoreContent>=50 && scoreUsage >= 50){
808
                    this.sendUserRegistrationResultsSuccessEmail(issuerEmail,jobId,repositoryInterface,repository, SecurityContextHolder.getContext().getAuthentication());
809
                    this.sendAdminRegistrationResultsSuccessEmail(issuerEmail,jobId,repositoryInterface,repository,SecurityContextHolder.getContext().getAuthentication());
810
                }else{
811
                    this.sendUserRegistrationResultsFailureEmail(jobId,repositoryInterface,repository, SecurityContextHolder.getContext().getAuthentication());
812
                    this.sendAdminRegistrationResultsFailureEmail(issuerEmail,jobId,repositoryInterface,repository,SecurityContextHolder.getContext().getAuthentication());
813
                }
814
            }else{
815
                this.sendAdminGeneralFailure(issuerEmail,jobId,repositoryInterface,repository,SecurityContextHolder.getContext().getAuthentication());
816
            }
817
        }else{
818
            if(isSuccess){
819
                if(scoreContent>=50 && scoreUsage >= 50){
820
                    this.sendUserUpdateResultsSuccessEmail(issuerEmail,jobId,repositoryInterface,repository, SecurityContextHolder.getContext().getAuthentication());
821
                    this.sendAdminUpdateResultsSuccessEmail(issuerEmail,jobId,repositoryInterface,repository, SecurityContextHolder.getContext().getAuthentication());
822
                }else{
823
                    this.sendUserUpdateResultsFailureEmail(issuerEmail,jobId,repositoryInterface,repository, SecurityContextHolder.getContext().getAuthentication());
824
                    this.sendAdminUpdateResultsFailureEmail(issuerEmail,jobId,repositoryInterface,repository, SecurityContextHolder.getContext().getAuthentication());
825
                }
826
            }else{
827
                this.sendAdminGeneralFailure(issuerEmail,jobId,repositoryInterface,repository,SecurityContextHolder.getContext().getAuthentication());
828
            }
829
        }
830

    
831
    }
832

    
833

    
834
    private void sendMail(String email, String subject, String message, boolean sendToSpecial, List<String> repoAdminMails) throws Exception {
835
        ArrayList<String> to = new ArrayList<String>();
836
        to.add(email);
837
        this.sendMail(to,subject,message,sendToSpecial,repoAdminMails);
838
    }
839

    
840
    public void sendMail(List<String> recipients, String subject, String message, boolean sendToSpecial, List<String> repoAdminMails) throws Exception {
841

    
842
        try {
843
            if (sendToSpecial) {
844
                recipients.addAll(this.specialRecipients);
845
            }
846

    
847
            if (repoAdminMails != null)
848
                recipients.addAll(repoAdminMails);
849

    
850
            if (this.override) {
851
                recipients.clear();
852
                recipients.add(overrideEmail);
853
            }
854
            if (!logonly)
855
                mailLibrary.sendEmail(recipients.toArray(new String[]{}), subject, message);
856
            LOGGER.debug("Sending mail to Recipients: " + recipients + " Subject: " + subject + " Message: " + message);
857
        } catch (Exception e) {
858
            LOGGER.error("Error sending mail to Recipients: " + recipients + " Subject: " + subject + " Message: " + message, e);
859
            throw new Exception(e);
860
        }
861
    }
862

    
863
    private String getEmailProperty(String key) {
864
        return pLoader.getProperties().getProperty(key);
865
    }
866

    
867
    public void setSpecialRecipients(String specialRecipients) {
868
        String[] recps = specialRecipients.split(",");
869

    
870
        for (String recp : recps) {
871
            recp = recp.trim();
872

    
873
            this.specialRecipients.add(recp);
874
        }
875
    }
876

    
877

    
878
    public void setOverride(boolean override) {
879
        this.override = override;
880
    }
881

    
882
    public void setOverrideEmail(String overrideEmail) {
883
        this.overrideEmail = overrideEmail;
884
    }
885

    
886
    public String getFrom() {
887
        return from;
888
    }
889

    
890
    public void setFrom(String from) {
891
        this.from = from;
892
    }
893

    
894
    public boolean isLogonly() {
895
        return logonly;
896
    }
897

    
898
    public void setLogonly(boolean logonly) {
899
        this.logonly = logonly;
900
    }
901

    
902
}
(6-6/20)