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, 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
                    "A validation process for this interface against the OpenAIRE guidelines compatibility " +
272
                    "has been started. You will be informed in another message once the process is finished." +
273
                    "\n\n" +
274
                    "User Contact: " + authentication.getName() + " (" + ((OIDCAuthenticationToken) authentication).getUserInfo().getEmail() + ")" +
275
                    "\n\n" +
276
                    "Please do not reply to this message\n" +
277
                    "This message has been generated automatically.\n\n" +
278
                    "Regards,\n" +
279
                    "the OpenAIRE technical team\n";
280

    
281
            this.sendMail(this.provideAdminEmail, subject, message, false, null);
282

    
283
        } catch (Exception e) {
284
            LOGGER.error("Error while sending registration of interface notification email to the administrator", e);
285
            throw e;
286
        }
287
    }
288

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

    
295
//            String message = "Dear " + ((OIDCAuthenticationToken) authentication).getUserInfo().getName() + ",\n" +
296
            String message = "Dear "+SecurityContextHolder.getContext().getAuthentication().getName()+",\n" +
297
                    "\n" +
298
                    "We received a request to add the following interface: \n\n" +
299
                    "Base URL: " + repositoryInterface.getBaseUrl() + "\n" +
300
                    "Set: " + repositoryInterface.getAccessSet() + "\n" +
301
                    "Guidelines: " + repositoryInterface.getDesiredCompatibilityLevel() + "\n\n" +
302
                    "to " + repository.getDatasourceType() + "[" + repository.getEnglishName() + "].\n" +
303
                    "A validation process for this interface against the OpenAIRE guidelines compatibility " +
304
                    "has been started. You will be informed in another message once the process is finished." +
305
                    "\n\n" +
306
                    "Please do not reply to this message\n" +
307
                    "This message has been generated automatically.\n\n" +
308
                    "Regards,\n" +
309
                    "the OpenAIRE technical team\n";
310

    
311
            this.sendMail(repository.getRegisteredBy(), subject, message, false, null);
312

    
313
        } catch (Exception e) {
314
            LOGGER.error("Error while sending registration of interface notification email to user: " + repository.getRegisteredBy(), e);
315
            throw e;
316
        }
317
    }
318

    
319
    @Override
320
    public void sendUserRegistrationResultsSuccessEmail(String issuerEmail, String jobId, RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) throws Exception {
321
        try {
322
            String subject = "OpenAIRE new interface registration request - results (success) for " +
323
                    repository.getDatasourceType() + "[" + repository.getEnglishName() + "]";
324

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

    
344
            this.sendMail(repository.getRegisteredBy(), subject, message, false, null);
345

    
346
        } catch (Exception e) {
347
            LOGGER.error("Error while sending registration notification email to user: " + repository.getRegisteredBy(), e);
348
            throw e;
349
        }
350
    }
351

    
352
    @Override
353
    public void sendAdminRegistrationResultsSuccessEmail(String issuerEmail, String jobId,RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) throws Exception {
354
        try {
355
            String subject = "OpenAIRE new interface registration request - results (success) for " +
356
                    repository.getDatasourceType() + "[" + repository.getEnglishName() + "]";
357

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

    
377
            this.sendMail(this.provideAdminEmail, subject, message, false, null);
378

    
379
        } catch (Exception e) {
380
            LOGGER.error("Error while sending registration notification email to user: " + repository.getRegisteredBy(), e);
381
            throw e;
382
        }
383
    }
384

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

    
409
            this.sendMail(repository.getRegisteredBy(), subject, message, false, null);
410

    
411
        } catch (Exception e) {
412
            LOGGER.error("Error while sending registration notification email to user: " + repository.getRegisteredBy(), e);
413
            throw e;
414
        }
415
    }
416

    
417
    @Override
418
    public void sendAdminRegistrationResultsFailureEmail(String issuerEmail, String jobId, RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) throws Exception {
419
        try {
420
            String subject = "OpenAIRE new interface registration request - results (failure) for " +
421
                    repository.getDatasourceType() + "[" + repository.getEnglishName() + "]";
422

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

    
442
            this.sendMail(this.provideAdminEmail, subject, message, false, null);
443

    
444
        } catch (Exception e) {
445
            LOGGER.error("Error while sending registration notification email to user: " + repository.getRegisteredBy(), e);
446
            throw e;
447
        }
448
    }
449

    
450
    @Override
451
    public void sendUserUpdateResultsSuccessEmail(String issuer, String jobId, RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) throws Exception {
452
        try {
453
            String subject = "OpenAIRE interface update request - results (success) for " +
454
                    repository.getDatasourceType() + "[" + repository.getEnglishName() + "]";
455

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

    
473
            this.sendMail(issuer, subject, message, false, null);
474

    
475
        } catch (Exception e) {
476
            LOGGER.error("Error while sending registration notification email to the administrator", e);
477
            throw e;
478
        }
479
    }
480

    
481
    @Override
482
    public void sendAdminUpdateResultsSuccessEmail(String issuerEmail, String jobId, RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) throws Exception {
483
            try {
484
                String subject = "OpenAIRE interface update request - results (success) for " +
485
                        repository.getDatasourceType() + "[" + repository.getEnglishName() + "]";
486

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

    
504
                this.sendMail(this.provideAdminEmail, subject, message, false, null);
505

    
506
            } catch (Exception e) {
507
                LOGGER.error("Error while sending registration notification email to the administrator", e);
508
                throw e;
509
            }
510
    }
511

    
512
    @Override
513
    public void sendUserUpdateResultsFailureEmail(String issuer, String jobId, RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) throws Exception {
514
        try {
515
            String subject = "OpenAIRE interface update request - results (failure) for " +
516
                    repository.getDatasourceType() + "[" + repository.getEnglishName() + "]";
517

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

    
537
            this.sendMail(issuer, subject, message, false, null);
538

    
539
        } catch (Exception e) {
540
            LOGGER.error("Error while sending registration notification email to user: " + repository.getRegisteredBy(), e);
541
            throw e;
542
        }
543
    }
544

    
545
    @Override
546
    public void sendAdminUpdateResultsFailureEmail(String issuerEmail, String jobId, RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) throws Exception {
547
        try {
548
            String subject = "OpenAIRE interface update request - results (failure) for " +
549
                    repository.getDatasourceType() + "[" + repository.getEnglishName() + "]";
550

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

    
570
            this.sendMail(this.provideAdminEmail, subject, message, false, null);
571

    
572
        } catch (Exception e) {
573
            LOGGER.error("Error while sending registration notification email to user: " + repository.getRegisteredBy(), e);
574
            throw e;
575
        }
576
    }
577

    
578
    @Override
579
    public void sendUserValidationResults(String issuer, String jobId, RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) throws Exception {
580
        try {
581
            String subject = "OpenAIRE validator - Test results ";
582

    
583
            //            String message = "Dear " + ((OIDCAuthenticationToken) authentication).getUserInfo().getName() + ",\n" +
584
            String message = "Dear user,\n" +
585
                    "\n" +
586
                    "the validation request you have submitted has finished. You can retrieve the results by following this url: "+ valBaseUrl+"" + jobId+" .\n\n" +
587
                    "Please do not reply to this message.\n" +
588
                    "This message has been generated automatically.\n" +
589
                    "If you have any questions, write to 'helpdesk@openaire.eu'. \n\n" +
590
                    "Regards,\n" +
591
                    "the OpenAIRE technical team\n";
592

    
593
            this.sendMail(issuer, subject, message, false, null);
594

    
595
        } catch (Exception e) {
596
            LOGGER.error("Error while sending validation submission notification email to user: " + issuer, e);
597
            throw e;
598
        }
599
    }
600

    
601
    @Override
602
    public void sendAdminValidationResults(String issuer, String jobId, RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) throws Exception {
603
        try {
604
            String subject = "OpenAIRE validator - Test results ";
605

    
606
            String message = "Dear admin,\n" +
607
                    "\n" +
608
                    "a validation request has finished. You can retrieve the results by following this url: "+ valBaseUrl+"" + jobId+" .\n\n" +
609
                    "\n\nUser Contact:"+ issuer +""+
610
                    "Please do not reply to this message.\n" +
611
                    "This message has been generated automatically.\n" +
612
                    "If you have any questions, write to 'helpdesk@openaire.eu'. \n\n" +
613
                    "Regards,\n" +
614
                    "the OpenAIRE technical team\n";
615

    
616
            this.sendMail(this.provideAdminEmail, subject, message, false, null);
617

    
618
        } catch (Exception e) {
619
            LOGGER.error("Error while sending validation submission notification email to user: " + issuer, e);
620
            throw e;
621
        }
622
    }
623

    
624
    @Override
625
    public void sendAdminGeneralFailure(String issuer, String jobId, RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) throws Exception {
626
        try {
627
            String subject = "OpenAIRE validator - job failure";
628

    
629
            String message = "Dear admin,\n" +
630
                    "\n" +
631
                    "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." +
632
                    "This message has been generated automatically.\n\n" +
633
                    "Regards,\n" +
634
                    "the OpenAIRE technical team\n";
635

    
636
            this.sendMail(this.provideAdminEmail, subject, message, false, null);
637

    
638
        } catch (Exception e) {
639
            LOGGER.error("Error while sending validation submission notification email to user: " + issuer, e);
640
            throw e;
641
        }
642
    }
643

    
644
    @Override
645
    public void sendAdminUpdateRepositoryInfoEmail(Repository repository, Authentication authentication) throws Exception {
646
        try {
647
            String subject = "OpenAIRE content provider update information for " +
648
                    repository.getDatasourceType() + "[" + repository.getEnglishName() + "]";
649

    
650
            String message = "Dear administrator" + ",\n" +
651
                    "\n" +
652
                    "We received a request to update the basic information for " + repository.getDatasourceType() + "[" + repository.getEnglishName() + "].\n\n" +
653
                    "Please do not reply to this message\n" +
654
                    "This message has been generated automatically.\n\n" +
655
                    "Regards,\n" +
656
                    "the OpenAIRE technical team\n";
657

    
658
            this.sendMail(this.provideAdminEmail, subject, message, false, null);
659

    
660
        } catch (Exception e) {
661
            LOGGER.error("Error while sending registration notification email to the administrator", e);
662
            throw e;
663
        }
664
    }
665

    
666
    @Override
667
    public void sendUserUpdateRepositoryInfoEmail(Repository repository, Authentication authentication) throws Exception {
668
        try {
669
            String subject = "OpenAIRE content provider update information for " +
670
                    repository.getDatasourceType() + "[" + repository.getEnglishName() + "]";
671

    
672
            //            String message = "Dear " + ((OIDCAuthenticationToken) authentication).getUserInfo().getName() + ",\n" +
673
            String message = "Dear user,\n" +
674
                    "\n" +
675
                    "We received a request to update the basic information for " + repository.getDatasourceType() + "[" + repository.getEnglishName() + "].\n\n" +
676
                    "Please do not reply to this message\n" +
677
                    "This message has been generated automatically.\n\n" +
678
                    "If you have any questions, write to 'helpdesk@openaire.eu'. \n\n" +
679
                    "Regards,\n" +
680
                    "the OpenAIRE technical team\n";
681

    
682
            this.sendMail(repository.getRegisteredBy(), subject, message, false, null);
683

    
684
        } catch (Exception e) {
685
            LOGGER.error("Error while sending registration notification email to user: " + repository.getRegisteredBy(), e);
686
            throw e;
687
        }
688
    }
689

    
690
    @Override
691
    public void sendAdminUpdateInterfaceEmail(Repository repository, RepositoryInterface repositoryInterface, Authentication authentication) throws Exception {
692
        try {
693
            String subject = "OpenAIRE interface update request started for " +
694
                    repository.getDatasourceType() + "[" + repository.getEnglishName() + "]";
695

    
696
            String message = "Dear administrator" + ",\n" +
697
                    "\n" +
698
                    "We received a request to update the following interface: \n\n" +
699
                    "Base URL: " + repositoryInterface.getBaseUrl() + "\n" +
700
                    "Set: " + repositoryInterface.getAccessSet() + "\n" +
701
                    "Guidelines: " + repositoryInterface.getDesiredCompatibilityLevel() + "\n\n" +
702
                    "for " + repository.getDatasourceType() + "[" + repository.getEnglishName() + "].\n" +
703
                    "A new iteration process of the validation against the OpenAIRE guidelines compatibility has been started.\n\n" +
704
                    "User Contact: " + authentication.getName() + " (" + ((OIDCAuthenticationToken) authentication).getUserInfo().getEmail() + ")" +
705
                    "\n\n" +
706
                    "Please do not reply to this message\n" +
707
                    "This message has been generated automatically.\n\n" +
708
                    "Regards,\n" +
709
                    "the OpenAIRE technical team\n";
710

    
711
            this.sendMail(this.provideAdminEmail, subject, message, false, null);
712

    
713
        } catch (Exception e) {
714
            LOGGER.error("Error while sending registration notification email to the administrator", e);
715
            throw e;
716
        }
717
    }
718

    
719
    @Override
720
    public void sendUserUpdateInterfaceEmail(Repository repository, RepositoryInterface repositoryInterface, Authentication authentication) throws Exception {
721
        try {
722
            String subject = "OpenAIRE interface update request started for " +
723
                    repository.getDatasourceType() + "[" + repository.getEnglishName() + "]";
724

    
725
            //            String message = "Dear " + ((OIDCAuthenticationToken) authentication).getUserInfo().getName() + ",\n" +
726
            String message = "Dear user,\n" +
727
                    "\n" +
728
                    "We received a request to update the following interface: \n\n" +
729
                    "Base URL: " + repositoryInterface.getBaseUrl() + "\n" +
730
                    "Set: " + repositoryInterface.getAccessSet() + "\n" +
731
                    "Guidelines: " + repositoryInterface.getDesiredCompatibilityLevel() + "\n\n" +
732
                    "for " + repository.getDatasourceType() + "[" + repository.getEnglishName() + "].\n" +
733
                    "A new iteration process of the validation against the OpenAIRE guidelines compatibility has been started.\n\n" +
734
                    "Please do not reply to this message\n" +
735
                    "This message has been generated automatically.\n\n" +
736
                    "If you have any questions, write to 'helpdesk@openaire.eu'. \n\n" +
737
                    "Regards,\n" +
738
                    "the OpenAIRE technical team\n";
739

    
740
            this.sendMail(repository.getRegisteredBy(), subject, message, false, null);
741

    
742
        } catch (Exception e) {
743
            LOGGER.error("Error while sending registration notification email to user: " + repository.getRegisteredBy(), e);
744
            throw e;
745
        }
746
    }
747

    
748
    @Override
749
    public void sendSubmitJobForValidationEmail(Authentication authentication, JobForValidation jobForValidation) throws Exception {
750
        try {
751
            String subject = "OpenAIRE validator - Test submission ";
752

    
753
            //            String message = "Dear " + ((OIDCAuthenticationToken) authentication).getUserInfo().getName() + ",\n" +
754
            String message = "Dear user,\n" +
755
                    "\n" +
756
                    "The validation request you have submitted has started.\n" +
757
                    "Please do not reply to this message.\n" +
758
                    "This message has been generated automatically.\n" +
759
                    "If you have any questions, write to 'helpdesk@openaire.eu'. \n\n" +
760
                    "Regards,\n" +
761
                    "the OpenAIRE technical team\n";
762

    
763
            this.sendMail(jobForValidation.getUserEmail(), subject, message, false, null);
764

    
765
        } catch (Exception e) {
766
            LOGGER.error("Error while sending validation submission notification email to user: " + jobForValidation.getUserEmail(), e);
767
            throw e;
768
        }
769
    }
770

    
771
    @Override
772
    public void sendUponJobCompletion(
773
            String repoId,
774
            String repoInterfaceId,
775
            int scoreUsage,
776
            int scoreContent,
777
            boolean isSuccess,
778
            boolean isUpdate,
779
            String issuerEmail,
780
            String jobId) throws Exception {
781
        List<RepositoryInterface> repositoryInterfaces = repositoryService.getRepositoryInterface(repoId);
782
        if(repositoryInterfaces.size()==0)
783
            throw new ValidationServiceException("Repository interface with id \""+repoInterfaceId+"\" not found",ValidationServiceException.ErrorCode.GENERAL_ERROR);
784

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

    
788
        if(!isUpdate){
789
            if(isSuccess){
790
                if(scoreContent>=50 && scoreUsage >= 50){
791
                    this.sendUserRegistrationResultsSuccessEmail(issuerEmail,jobId,repositoryInterface,repository, SecurityContextHolder.getContext().getAuthentication());
792
                    this.sendAdminRegistrationResultsSuccessEmail(issuerEmail,jobId,repositoryInterface,repository,SecurityContextHolder.getContext().getAuthentication());
793
                }else{
794
                    this.sendUserRegistrationResultsFailureEmail(jobId,repositoryInterface,repository, SecurityContextHolder.getContext().getAuthentication());
795
                    this.sendAdminRegistrationResultsFailureEmail(issuerEmail,jobId,repositoryInterface,repository,SecurityContextHolder.getContext().getAuthentication());
796
                }
797
            }else{
798
                this.sendAdminGeneralFailure(issuerEmail,jobId,repositoryInterface,repository,SecurityContextHolder.getContext().getAuthentication());
799
            }
800
        }else{
801
            if(isSuccess){
802
                if(scoreContent>=50 && scoreUsage >= 50){
803
                    this.sendUserUpdateResultsSuccessEmail(issuerEmail,jobId,repositoryInterface,repository, SecurityContextHolder.getContext().getAuthentication());
804
                    this.sendAdminUpdateResultsSuccessEmail(issuerEmail,jobId,repositoryInterface,repository, SecurityContextHolder.getContext().getAuthentication());
805
                }else{
806
                    this.sendUserUpdateResultsFailureEmail(issuerEmail,jobId,repositoryInterface,repository, SecurityContextHolder.getContext().getAuthentication());
807
                    this.sendAdminUpdateResultsFailureEmail(issuerEmail,jobId,repositoryInterface,repository, SecurityContextHolder.getContext().getAuthentication());
808
                }
809
            }else{
810
                this.sendAdminGeneralFailure(issuerEmail,jobId,repositoryInterface,repository,SecurityContextHolder.getContext().getAuthentication());
811
            }
812
        }
813

    
814
    }
815

    
816

    
817
    private void sendMail(String email, String subject, String message, boolean sendToSpecial, List<String> repoAdminMails) throws Exception {
818
        ArrayList<String> to = new ArrayList<String>();
819
        to.add(email);
820
        this.sendMail(to,subject,message,sendToSpecial,repoAdminMails);
821
    }
822

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

    
825
        try {
826
            if (sendToSpecial) {
827
                recipients.addAll(this.specialRecipients);
828
            }
829

    
830
            if (repoAdminMails != null)
831
                recipients.addAll(repoAdminMails);
832

    
833
            if (this.override) {
834
                recipients.clear();
835
                recipients.add(overrideEmail);
836
            }
837
            if (!logonly)
838
                mailLibrary.sendEmail(recipients.toArray(new String[]{}), subject, message);
839
            LOGGER.debug("Sending mail to Recipients: " + recipients + " Subject: " + subject + " Message: " + message);
840
        } catch (Exception e) {
841
            LOGGER.error("Error sending mail to Recipients: " + recipients + " Subject: " + subject + " Message: " + message, e);
842
            throw new Exception(e);
843
        }
844
    }
845

    
846
    private String getEmailProperty(String key) {
847
        return pLoader.getProperties().getProperty(key);
848
    }
849

    
850
    public void setSpecialRecipients(String specialRecipients) {
851
        String[] recps = specialRecipients.split(",");
852

    
853
        for (String recp : recps) {
854
            recp = recp.trim();
855

    
856
            this.specialRecipients.add(recp);
857
        }
858
    }
859

    
860

    
861
    public void setOverride(boolean override) {
862
        this.override = override;
863
    }
864

    
865
    public void setOverrideEmail(String overrideEmail) {
866
        this.overrideEmail = overrideEmail;
867
    }
868

    
869
    public String getFrom() {
870
        return from;
871
    }
872

    
873
    public void setFrom(String from) {
874
        this.from = from;
875
    }
876

    
877
    public boolean isLogonly() {
878
        return logonly;
879
    }
880

    
881
    public void setLogonly(boolean logonly) {
882
        this.logonly = logonly;
883
    }
884

    
885
}
(6-6/20)