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.springframework.beans.factory.annotation.Autowired;
12
import org.springframework.beans.factory.annotation.Value;
13
import org.springframework.security.core.Authentication;
14
import org.springframework.security.core.context.SecurityContextHolder;
15
import org.springframework.stereotype.Component;
16

    
17
import javax.annotation.PostConstruct;
18
import java.io.PrintWriter;
19
import java.io.StringWriter;
20
import java.io.Writer;
21
import java.util.ArrayList;
22
import java.util.List;
23
import java.util.stream.Collectors;
24

    
25

    
26
@Component("emailUtils")
27
public class EmailUtilsImpl implements EmailUtils {
28

    
29
    private static Logger LOGGER = Logger.getLogger(EmailUtilsImpl.class);
30

    
31
    private List<String> specialRecipients = new ArrayList<String>();
32
    private boolean override = false, logonly = false;
33
    private String overrideEmail = null, from = null;
34

    
35
    @Autowired
36
    private MailLibrary mailLibrary;
37

    
38
    @Autowired
39
    private CascadingPropertyLoader pLoader;
40

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

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

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

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

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

    
56
    @Autowired
57
    private RepositoryService repositoryService;
58

    
59

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

    
65

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
211
            String message = "Dear administrator" + ",\n" +
212
                    "\n" +
213
                    "We received a request to register the " + repository.getDatasourceType() + "[" + repository.getEnglishName() + "]" +
214
                    " to the OpenAIRE compliant list of content providers. " +
215
                    "A validation process against the OpenAIRE guidelines compatibility " +
216
                    "has been started. You will be informed in another message once the process is finished." +
217
                    "\n\n" +
218
                    "Please do not reply to this message\n" +
219
                    "This message has been generated automatically.\n\n" +
220
                    "Regards,\n" +
221
                    "the OpenAIRE technical team\n";
222

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

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

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

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

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

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

    
259
    @Override
260
    public void sendUserRegistrationResultsSuccessEmail(String issuerEmail, String jobId, RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) throws Exception {
261
        try {
262
            String subject = "OpenAIRE content provider registration request - results (success) for " +
263
                    repository.getDatasourceType() + "[" + repository.getEnglishName() + "]";
264

    
265
//            String message = "Dear " + ((OIDCAuthenticationToken) authentication).getUserInfo().getName() + ",\n" +
266
            String message = "Dear user,\n" +
267
                    "\n" +
268
                    "the compatibility test on " + "[" + repository.getEnglishName() + "]" +
269
                    " was successful and the datasource type \""+ repository.getDatasourceType()  + "\" will be prepared for aggregation in OpenAIRE."+
270
                    "\n\n" +
271
                    "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" +
272
                    "Registration identifier in OpenAIRE: "+ repository.getNamespacePrefix()+
273
                    "\nOfficial Name:" + repository.getOfficialName() +
274
                    "\n\nBase URL: "+ repositoryInterface.getBaseUrl() +
275
                    "\n\nValidation Set: " + repositoryInterface.getAccessSet() +
276
                    "\n\nGuidelines: "+ repositoryInterface.getDesiredCompatibilityLevel() +
277
                    "\n\nYou can review the validation results here.\n" + valBaseUrl + "" + jobId +
278
                    "\n\n\nPlease do not reply to this email\n"+
279
                    "This message has been generated manually\n\n"+
280
                    "If you have any questions, write to 'helpdesk@openaire.eu'. \n\n" +
281
                    "Regards,\n" +
282
                    "the OpenAIRE technical team\n";
283

    
284
            this.sendMail(repository.getRegisteredBy(), subject, message, false, null);
285

    
286
        } catch (Exception e) {
287
            LOGGER.error("Error while sending registration notification email to user: " + repository.getRegisteredBy(), e);
288
            throw e;
289
        }
290
    }
291

    
292
    @Override
293
    public void sendAdminRegistrationResultsSuccessEmail(String issuerEmail, String jobId,RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) throws Exception {
294
        try {
295
            String subject = "OpenAIRE content provider registration request - results (success) for " +
296
                    repository.getDatasourceType() + "[" + repository.getEnglishName() + "]";
297

    
298
            String message = "Dear admin ,\n" +
299
                    "\n" +
300
                    "the compatibility test on " + "[" + repository.getEnglishName() + "]" +
301
                    " was successful and the datasource type \""+ repository.getDatasourceType()  + "\" will be prepared for aggregation in OpenAIRE."+
302
                    "\n\n" +
303
                    "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" +
304
                    "Registration identifier in OpenAIRE: "+ repository.getNamespacePrefix()+
305
                    "\nOfficial Name:" + repository.getOfficialName() +
306
                    "\n\nBase URL: "+ repositoryInterface.getBaseUrl() +
307
                    "\n\nValidation Set: " + repositoryInterface.getAccessSet() +
308
                    "\n\nGuidelines: "+ repositoryInterface.getDesiredCompatibilityLevel() +
309
                    "\n\nUser Contact:"+ issuerEmail +""+
310
                    "\n\nYou can review the validation results here.\n" + valBaseUrl + "" + jobId +
311
                    "\n\n\nPlease do not reply to this email\n"+
312
                    "This message has been generated manually\n\n"+
313
                    "If you have any questions, write to 'helpdesk@openaire.eu'. \n\n" +
314
                    "Regards,\n" +
315
                    "the OpenAIRE technical team\n";
316

    
317
            this.sendMail(this.provideAdminEmail, subject, message, false, null);
318

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

    
325
    @Override
326
    public void sendUserRegistrationResultsFailureEmail(String jobId, RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) throws Exception {
327
        try {
328
            String subject = "OpenAIRE content provider registration request - results (failure) for " +
329
                    repository.getDatasourceType() + "[" + repository.getEnglishName() + "]";
330
            //            String message = "Dear " + ((OIDCAuthenticationToken) authentication).getUserInfo().getName() + ",\n" +
331
            String message = "Dear user,\n" +
332
                    "\n" +
333
                    "the compatibility test on " + "[" + repository.getEnglishName() + "]" +
334
                    " was not successful and the registration process was interrupted."+
335
                    "\n\n" +
336
                    "We will check what caused the problem and get back to you within a couple of days.\n\n" +
337
                    "Registration identifier in OpenAIRE: "+ repository.getNamespacePrefix()+
338
                    "\nOfficial Name:" + repository.getOfficialName() +
339
                    "\n\nBase URL: "+ repositoryInterface.getBaseUrl() +
340
                    "\n\nValidation Set: " + repositoryInterface.getAccessSet() +
341
                    "\n\nGuidelines: "+ repositoryInterface.getDesiredCompatibilityLevel() +
342
                    "\n\nYou can review the validation results here.\n" + valBaseUrl + "" + jobId +
343
                    "\n\n\nPlease do not reply to this email\n"+
344
                    "This message has been generated manually\n\n"+
345
                    "If you have any questions, write to 'helpdesk@openaire.eu'. \n\n" +
346
                    "Regards,\n" +
347
                    "the OpenAIRE technical team\n";
348

    
349
            this.sendMail(repository.getRegisteredBy(), subject, message, false, null);
350

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

    
357
    @Override
358
    public void sendAdminRegistrationResultsFailureEmail(String issuerEmail, String jobId, RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) throws Exception {
359
        try {
360
            String subject = "OpenAIRE content provider registration request - results (failure) for " +
361
                    repository.getDatasourceType() + "[" + repository.getEnglishName() + "]";
362

    
363
            String message = "Dear admin,\n" +
364
                    "\n" +
365
                    "the compatibility test on " + "[" + repository.getEnglishName() + "]" +
366
                    " was not successful and the registration process was interrupted."+
367
                    "\n\n" +
368
                    "We will check what caused the problem and get back to you within a couple of days.\n\n" +
369
                    "Registration identifier in OpenAIRE: "+ repository.getNamespacePrefix()+
370
                    "\nOfficial Name:" + repository.getOfficialName() +
371
                    "\n\nBase URL: "+ repositoryInterface.getBaseUrl() +
372
                    "\n\nValidation Set: " + repositoryInterface.getAccessSet() +
373
                    "\n\nGuidelines: "+ repositoryInterface.getDesiredCompatibilityLevel() +
374
                    "\n\nUser Contact:"+ issuerEmail +""+
375
                    "\n\nYou can review the validation results here.\n" + valBaseUrl + "" + jobId +
376
                    "\n\n\nPlease do not reply to this email\n"+
377
                    "This message has been generated manually\n\n"+
378
                    "If you have any questions, write to 'helpdesk@openaire.eu'. \n\n" +
379
                    "Regards,\n" +
380
                    "the OpenAIRE technical team\n";
381

    
382
            this.sendMail(this.provideAdminEmail, subject, message, false, null);
383

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

    
390
    @Override
391
    public void sendUserUpdateResultsSuccessEmail(String issuer, String jobId, RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) throws Exception {
392
        try {
393
            String subject = "OpenAIRE content provider registration request - results (success) for " +
394
                    repository.getDatasourceType() + "[" + repository.getEnglishName() + "]";
395

    
396
            //            String message = "Dear " + ((OIDCAuthenticationToken) authentication).getUserInfo().getName() + ",\n" +
397
            String message = "Dear user,\n" +
398
                    "\n" +
399
                    "the compatibility test on [" + repository.getEnglishName()+"] has been successful\n\n" +
400
                    "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" +
401
                    "Registration identifier in OpenAIRE: "+ repository.getNamespacePrefix()+
402
                    "\nOfficial Name:" + repository.getOfficialName() +
403
                    "\n\nBase URL: "+ repositoryInterface.getBaseUrl() +
404
                    "\n\nValidation Set: " + repositoryInterface.getAccessSet() +
405
                    "\n\nGuidelines: "+ repositoryInterface.getDesiredCompatibilityLevel() +
406
                    "\n\nYou can review the validation results here.\n" + valBaseUrl + "" + jobId +
407
                    "\n\n\nPlease do not reply to this email\n"+
408
                    "This message has been generated manually\n\n"+
409
                    "If you have any questions, write to 'helpdesk@openaire.eu'. \n\n" +
410
                    "Regards,\n" +
411
                    "the OpenAIRE technical team\n";
412

    
413
            this.sendMail(issuer, subject, message, false, null);
414

    
415
        } catch (Exception e) {
416
            LOGGER.error("Error while sending registration notification email to the administrator", e);
417
            throw e;
418
        }
419
    }
420

    
421
    @Override
422
    public void sendAdminUpdateResultsSuccessEmail(String issuerEmail, String jobId, RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) throws Exception {
423
            try {
424
                String subject = "OpenAIRE content provider registration request - results (success) for " +
425
                        repository.getDatasourceType() + "[" + repository.getEnglishName() + "]";
426

    
427
                String message = "Dear admin,\n" +
428
                        "\n" +
429
                        "the compatibility test on [" + repository.getEnglishName()+"] has been successful\n\n" +
430
                        "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" +
431
                        "Registration identifier in OpenAIRE: "+ repository.getNamespacePrefix()+
432
                        "\nOfficial Name:" + repository.getOfficialName() +
433
                        "\n\nBase URL: "+ repositoryInterface.getBaseUrl() +
434
                        "\n\nValidation Set: " + repositoryInterface.getAccessSet() +
435
                        "\n\nGuidelines: "+ repositoryInterface.getDesiredCompatibilityLevel() +
436
                        "\n\nUser Contact:"+ issuerEmail +""+
437
                        "\n\nYou can review the validation results here.\n" + valBaseUrl + "" + jobId +
438
                        "\n\n\nPlease do not reply to this email\n"+
439
                        "This message has been generated manually\n\n"+
440
                        "If you have any questions, write to 'helpdesk@openaire.eu'. \n\n" +
441
                        "Regards,\n" +
442
                        "the OpenAIRE technical team\n";
443

    
444
                this.sendMail(this.provideAdminEmail, subject, message, false, null);
445

    
446
            } catch (Exception e) {
447
                LOGGER.error("Error while sending registration notification email to the administrator", e);
448
                throw e;
449
            }
450
    }
451

    
452
    @Override
453
    public void sendUserUpdateResultsFailureEmail(String issuer, String jobId, RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) throws Exception {
454
        try {
455
            String subject = "OpenAIRE content provider update request - results (failure) for " +
456
                    repository.getDatasourceType() + "[" + repository.getEnglishName() + "]";
457

    
458
            //            String message = "Dear " + ((OIDCAuthenticationToken) authentication).getUserInfo().getName() + ",\n" +
459
            String message = "Dear user,\n" +
460
                    "\n" +
461
                    "the compatibility test on " + "[" + repository.getEnglishName() + "]" +
462
                    " was not successful."+
463
                    "\n\n" +
464
                    "WWe will check your transmitted information to see what caused the problem and get back to you within a couple of days.\n\n" +
465
                    "Registration identifier in OpenAIRE: "+ repository.getNamespacePrefix()+
466
                    "\nOfficial Name:" + repository.getOfficialName() +
467
                    "\n\nBase URL: "+ repositoryInterface.getBaseUrl() +
468
                    "\n\nValidation Set: " + repositoryInterface.getAccessSet() +
469
                    "\n\nGuidelines: "+ repositoryInterface.getDesiredCompatibilityLevel() +
470
                    "\n\nYou can review the validation results here.\n" + valBaseUrl + "" + jobId +
471
                    "\n\n\nPlease do not reply to this email\n"+
472
                    "This message has been generated manually\n\n"+
473
                    "If you have any questions, write to 'helpdesk@openaire.eu'. \n\n" +
474
                    "Regards,\n" +
475
                    "the OpenAIRE technical team\n";
476

    
477
            this.sendMail(issuer, subject, message, false, null);
478

    
479
        } catch (Exception e) {
480
            LOGGER.error("Error while sending registration notification email to user: " + repository.getRegisteredBy(), e);
481
            throw e;
482
        }
483
    }
484

    
485
    @Override
486
    public void sendAdminUpdateResultsFailureEmail(String issuerEmail, String jobId, RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) throws Exception {
487
        try {
488
            String subject = "OpenAIRE content provider update request - results (failure) for " +
489
                    repository.getDatasourceType() + "[" + repository.getEnglishName() + "]";
490

    
491
            String message = "Dear admin,\n" +
492
                    "\n" +
493
                    "the compatibility test on " + "[" + repository.getEnglishName() + "]" +
494
                    " was not successful."+
495
                    "\n\n" +
496
                    "WWe will check your transmitted information to see what caused the problem and get back to you within a couple of days.\n\n" +
497
                    "Registration identifier in OpenAIRE: "+ repository.getNamespacePrefix()+
498
                    "\nOfficial Name:" + repository.getOfficialName() +
499
                    "\n\nBase URL: "+ repositoryInterface.getBaseUrl() +
500
                    "\n\nValidation Set: " + repositoryInterface.getAccessSet() +
501
                    "\n\nGuidelines: "+ repositoryInterface.getDesiredCompatibilityLevel() +
502
                    "\n\nUser Contact:"+ issuerEmail +""+
503
                    "\n\nYou can review the validation results here.\n" + valBaseUrl + "" + jobId +
504
                    "\n\n\nPlease do not reply to this email\n"+
505
                    "This message has been generated manually\n\n"+
506
                    "If you have any questions, write to 'helpdesk@openaire.eu'. \n\n" +
507
                    "Regards,\n" +
508
                    "the OpenAIRE technical team\n";
509

    
510
            this.sendMail(this.provideAdminEmail, subject, message, false, null);
511

    
512
        } catch (Exception e) {
513
            LOGGER.error("Error while sending registration notification email to user: " + repository.getRegisteredBy(), e);
514
            throw e;
515
        }
516
    }
517

    
518
    @Override
519
    public void sendUserValidationResults(String issuer, String jobId, RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) throws Exception {
520
        try {
521
            String subject = "OpenAIRE validator - Test results ";
522

    
523
            //            String message = "Dear " + ((OIDCAuthenticationToken) authentication).getUserInfo().getName() + ",\n" +
524
            String message = "Dear user,\n" +
525
                    "\n" +
526
                    "the validation request you have submitted has finished. You can retrieve the results by following this url: "+ valBaseUrl+"" + jobId+" .\n\n" +
527
                    "Please do not reply to this message.\n" +
528
                    "This message has been generated automatically.\n" +
529
                    "If you have any questions, write to 'helpdesk@openaire.eu'. \n\n" +
530
                    "Regards,\n" +
531
                    "the OpenAIRE technical team\n";
532

    
533
            this.sendMail(issuer, subject, message, false, null);
534

    
535
        } catch (Exception e) {
536
            LOGGER.error("Error while sending validation submission notification email to user: " + issuer, e);
537
            throw e;
538
        }
539
    }
540

    
541
    @Override
542
    public void sendAdminValidationResults(String issuer, String jobId, RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) throws Exception {
543
        try {
544
            String subject = "OpenAIRE validator - Test results ";
545

    
546
            String message = "Dear admin,\n" +
547
                    "\n" +
548
                    "the validation request you have submitted has finished. You can retrieve the results by following this url: "+ valBaseUrl+"" + jobId+" .\n\n" +
549
                    "\n\nUser Contact:"+ issuer +""+
550
                    "Please do not reply to this message.\n" +
551
                    "This message has been generated automatically.\n" +
552
                    "If you have any questions, write to 'helpdesk@openaire.eu'. \n\n" +
553
                    "Regards,\n" +
554
                    "the OpenAIRE technical team\n";
555

    
556
            this.sendMail(this.provideAdminEmail, subject, message, false, null);
557

    
558
        } catch (Exception e) {
559
            LOGGER.error("Error while sending validation submission notification email to user: " + issuer, e);
560
            throw e;
561
        }
562
    }
563

    
564
    @Override
565
    public void sendAdminGeneralFailure(String issuer, String jobId, RepositoryInterface repositoryInterface, Repository repository, Authentication authentication) throws Exception {
566
        try {
567
            String subject = "OpenAIRE validator - job failure";
568

    
569
            String message = "Dear admin,\n" +
570
                    "\n" +
571
                    "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." +
572
                    "This message has been generated automatically.\n\n" +
573
                    "Regards,\n" +
574
                    "the OpenAIRE technical team\n";
575

    
576
            this.sendMail(this.provideAdminEmail, subject, message, false, null);
577

    
578
        } catch (Exception e) {
579
            LOGGER.error("Error while sending validation submission notification email to user: " + issuer, e);
580
            throw e;
581
        }
582
    }
583

    
584
    @Override
585
    public void sendAdminUpdateRepositoryEmail(Repository repository, Authentication authentication) throws Exception {
586
        try {
587
            String subject = "OpenAIRE content provider update request started for " +
588
                    repository.getDatasourceType() + "[" + repository.getEnglishName() + "]";
589

    
590
            String message = "Dear administrator" + ",\n" +
591
                    "\n" +
592
                    "We received a request to update the " + repository.getDatasourceType() + "[" + repository.getEnglishName() + "]." +
593
                    "A new iteration process of the validation against the OpenAIRE guidelines compatibility has been started.\n\n" +
594
                    "Please do not reply to this message\n" +
595
                    "This message has been generated automatically.\n\n" +
596
                    "Regards,\n" +
597
                    "the OpenAIRE technical team\n";
598

    
599
            this.sendMail(this.provideAdminEmail, subject, message, false, null);
600

    
601
        } catch (Exception e) {
602
            LOGGER.error("Error while sending registration notification email to the administrator", e);
603
            throw e;
604
        }
605
    }
606

    
607
    @Override
608
    public void sendUserUpdateRepositoryEmail(Repository repository, Authentication authentication) throws Exception {
609
        try {
610
            String subject = "OpenAIRE content provider update request started for " +
611
                    repository.getDatasourceType() + "[" + repository.getEnglishName() + "]";
612

    
613
            //            String message = "Dear " + ((OIDCAuthenticationToken) authentication).getUserInfo().getName() + ",\n" +
614
            String message = "Dear user,\n" +
615
                    "\n" +
616
                    "We received a request to update the " + repository.getDatasourceType() + "[" + repository.getEnglishName() + "]." +
617
                    "A new iteration process of the validation against the OpenAIRE guidelines compatibility has been started.\n\n" +
618
                    "Please do not reply to this message\n" +
619
                    "This message has been generated automatically.\n\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(repository.getRegisteredBy(), subject, message, false, null);
625

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

    
632
    @Override
633
    public void sendSubmitJobForValidationEmail(Authentication authentication, JobForValidation jobForValidation) throws Exception {
634
        try {
635
            String subject = "OpenAIRE validator - Test submission ";
636

    
637
            //            String message = "Dear " + ((OIDCAuthenticationToken) authentication).getUserInfo().getName() + ",\n" +
638
            String message = "Dear user,\n" +
639
                    "\n" +
640
                    "The validation request you have submitted has started.\n" +
641
                    "Please do not reply to this message.\n" +
642
                    "This message has been generated automatically.\n" +
643
                    "If you have any questions, write to 'helpdesk@openaire.eu'. \n\n" +
644
                    "Regards,\n" +
645
                    "the OpenAIRE technical team\n";
646

    
647
            this.sendMail(jobForValidation.getUserEmail(), subject, message, false, null);
648

    
649
        } catch (Exception e) {
650
            LOGGER.error("Error while sending validation submission notification email to user: " + jobForValidation.getUserEmail(), e);
651
            throw e;
652
        }
653
    }
654

    
655
    @Override
656
    public void sendUponJobCompletion(
657
            String repoId,
658
            String repoInterfaceId,
659
            int scoreUsage,
660
            int scoreContent,
661
            boolean isSuccess,
662
            boolean isUpdate,
663
            String issuerEmail,
664
            String jobId) throws Exception {
665
        List<RepositoryInterface> repositoryInterfaces = repositoryService.getRepositoryInterface(repoId);
666
        if(repositoryInterfaces.size()==0)
667
            throw new ValidationServiceException("Repository interface with id \""+repoInterfaceId+"\" not found",ValidationServiceException.ErrorCode.GENERAL_ERROR);
668

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

    
672
        if(!isUpdate){
673
            if(isSuccess){
674
                if(scoreContent>=50 && scoreUsage >= 50){
675
                    this.sendUserRegistrationResultsSuccessEmail(issuerEmail,jobId,repositoryInterface,repository, SecurityContextHolder.getContext().getAuthentication());
676
                    this.sendAdminRegistrationResultsSuccessEmail(issuerEmail,jobId,repositoryInterface,repository,SecurityContextHolder.getContext().getAuthentication());
677
                }else{
678
                    this.sendUserRegistrationResultsFailureEmail(jobId,repositoryInterface,repository, SecurityContextHolder.getContext().getAuthentication());
679
                    this.sendAdminRegistrationResultsFailureEmail(issuerEmail,jobId,repositoryInterface,repository,SecurityContextHolder.getContext().getAuthentication());
680
                }
681
            }else{
682
                this.sendAdminGeneralFailure(issuerEmail,jobId,repositoryInterface,repository,SecurityContextHolder.getContext().getAuthentication());
683
            }
684
        }else{
685
            if(isSuccess){
686
                if(scoreContent>=50 && scoreUsage >= 50){
687
                    this.sendUserUpdateResultsSuccessEmail(issuerEmail,jobId,repositoryInterface,repository, SecurityContextHolder.getContext().getAuthentication());
688
                    this.sendAdminUpdateResultsSuccessEmail(issuerEmail,jobId,repositoryInterface,repository, SecurityContextHolder.getContext().getAuthentication());
689
                }else{
690
                    this.sendUserUpdateResultsFailureEmail(issuerEmail,jobId,repositoryInterface,repository, SecurityContextHolder.getContext().getAuthentication());
691
                    this.sendAdminUpdateResultsFailureEmail(issuerEmail,jobId,repositoryInterface,repository, SecurityContextHolder.getContext().getAuthentication());
692
                }
693
            }else{
694
                this.sendAdminGeneralFailure(issuerEmail,jobId,repositoryInterface,repository,SecurityContextHolder.getContext().getAuthentication());
695
            }
696
        }
697

    
698
    }
699

    
700

    
701
    private void sendMail(String email, String subject, String message, boolean sendToSpecial, List<String> repoAdminMails) throws Exception {
702
        ArrayList<String> to = new ArrayList<String>();
703
        to.add(email);
704
        this.sendMail(to,subject,message,sendToSpecial,repoAdminMails);
705
    }
706

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

    
709
        try {
710
            if (sendToSpecial) {
711
                recipients.addAll(this.specialRecipients);
712
            }
713

    
714
            if (repoAdminMails != null)
715
                recipients.addAll(repoAdminMails);
716

    
717
            if (this.override) {
718
                recipients.clear();
719
                recipients.add(overrideEmail);
720
            }
721
            if (!logonly)
722
                mailLibrary.sendEmail(recipients.toArray(new String[]{}), subject, message);
723
            LOGGER.debug("Sending mail to Recipients: " + recipients + " Subject: " + subject + " Message: " + message);
724
        } catch (Exception e) {
725
            LOGGER.error("Error sending mail to Recipients: " + recipients + " Subject: " + subject + " Message: " + message, e);
726
            throw new Exception(e);
727
        }
728
    }
729

    
730
    private String getEmailProperty(String key) {
731
        return pLoader.getProperties().getProperty(key);
732
    }
733

    
734
    public void setSpecialRecipients(String specialRecipients) {
735
        String[] recps = specialRecipients.split(",");
736

    
737
        for (String recp : recps) {
738
            recp = recp.trim();
739

    
740
            this.specialRecipients.add(recp);
741
        }
742
    }
743

    
744

    
745
    public void setOverride(boolean override) {
746
        this.override = override;
747
    }
748

    
749
    public void setOverrideEmail(String overrideEmail) {
750
        this.overrideEmail = overrideEmail;
751
    }
752

    
753
    public String getFrom() {
754
        return from;
755
    }
756

    
757
    public void setFrom(String from) {
758
        this.from = from;
759
    }
760

    
761
    public boolean isLogonly() {
762
        return logonly;
763
    }
764

    
765
    public void setLogonly(boolean logonly) {
766
        this.logonly = logonly;
767
    }
768

    
769
}
(6-6/20)