Project

General

Profile

1
package eu.dnetlib.validator.service.impls.listeners;
2

    
3
import java.util.ArrayList;
4
import java.util.List;
5
import java.util.Map;
6

    
7
import org.apache.log4j.Logger;
8
import org.springframework.transaction.annotation.Propagation;
9
import org.springframework.transaction.annotation.Transactional;
10

    
11
import eu.dnetlib.validator.commons.dao.jobs.JobsDAO;
12
import eu.dnetlib.validator.commons.email.Emailer;
13
import eu.dnetlib.validator.engine.execution.CompletedTask;
14
import eu.dnetlib.validator.engine.execution.JobListener;
15

    
16
public class CompatibilityTestListener implements JobListener{
17
	private static Logger logger = Logger.getLogger(CompatibilityTestListener.class);
18
	
19
	private Emailer emailer = null;
20
	private String valBaseUrl = null;
21
	private String validationSet = null; 
22
	private String guidelines = null;
23
	private JobsDAO jobsDao;
24
	
25
	private int totalJobs;
26
	private int jobsFinished = 0;
27

    
28
	@Override
29
	@Transactional(propagation = Propagation.REQUIRED)
30
	public synchronized void finished(int jobId, Map<String, Object> jobContext) {
31
		jobsFinished++;
32
		if (jobsFinished == totalJobs) {
33
			logger.debug("all jobs finished");
34
			jobsDao.setTotalJobFinished(jobId, null, false);
35
			this.sendMail((Integer) jobContext.get("jobSubmittedId"), (String) jobContext.get("jobSubmittedUser"));
36
		} else {
37
			logger.debug("not all jobs finished yet. Waiting "+ (totalJobs-jobsFinished) + " job(s) to finish" );
38
		}
39
	}
40

    
41
	@Override
42
	@Transactional(propagation = Propagation.REQUIRED)
43
	public synchronized void failed(int jobId, Map<String, Object> jobContext, Throwable t) {
44
		jobsFinished++;
45
		if (jobsFinished == totalJobs) {
46
			jobsDao.setTotalJobFinished(jobId, t.getMessage(), true);
47
			this.sendMail((Integer) jobContext.get("jobSubmittedId"), (String) jobContext.get("jobSubmittedUser"));
48
		} else {
49
			logger.debug("not all jobs finished yet. Waiting "+ (totalJobs-jobsFinished) + " job(s) to finish" );
50
		}
51
	}
52

    
53

    
54
	
55
	private void sendMail(int jobSubmittedId, String jobSubmittedUser) {
56
		logger.debug("JOBID:"+jobSubmittedId+"# Sending email for finished job");
57
		String mailTo = jobSubmittedUser;
58
		List<String> recipients = new ArrayList<String>();
59
		recipients.add(mailTo);
60
		String msgUpgrade = "";
61
//		if (this.validationSet != null && !this.validationSet.equalsIgnoreCase("openaire"))
62
		if (this.guidelines.equalsIgnoreCase("openaire2.0") || this.guidelines.equalsIgnoreCase("driver"))
63
			msgUpgrade = "\n\n Please consider to upgrade to OpenAIRE Guidelines v3. Link: https://guidelines.openaire.eu/wiki/OpenAIRE_Guidelines:_For_Literature_repositories";
64
		String message = "The compatibility test you have submitted has finished. You can retrieve the results by following this url: " + valBaseUrl + "/prepareSummary.action?jobId=" + jobSubmittedId  + msgUpgrade;
65

    
66
		String subject = "OpenAIRE compatibility Test Results";
67
		try {
68
				emailer.sendMail(recipients, subject, message, false, null);
69
		} catch (Exception e) {
70
			logger.error("JOBID:"+jobSubmittedId+"# Error sending email for finished job", e);
71
		}
72
	}
73
	
74
	@Override
75
	public void currentResults(List<CompletedTask> tasks, int jobId,
76
			Object object, Map<String, Object> recordContext, Throwable t) {
77
		// TODO Auto-generated method stub
78
		
79
	}
80

    
81
	@Override
82
	public void currentResults(List<CompletedTask> tasks, int jobId,
83
			Object object, Map<String, Object> recordContext) {
84
		// TODO Auto-generated method stub
85
		
86
	}
87

    
88
	public String getValBaseUrl() {
89
		return valBaseUrl;
90
	}
91

    
92
	public void setValBaseUrl(String valBaseUrl) {
93
		this.valBaseUrl = valBaseUrl;
94
	}
95

    
96
	public String getValidationSet() {
97
		return validationSet;
98
	}
99

    
100
	public void setValidationSet(String validationSet) {
101
		this.validationSet = validationSet;
102
	}
103

    
104
	public Emailer getEmailer() {
105
		return emailer;
106
	}
107

    
108
	public void setEmailer(Emailer emailer) {
109
		this.emailer = emailer;
110
	}
111

    
112
	public String getGuidelines() {
113
		return guidelines;
114
	}
115

    
116
	public void setGuidelines(String guidelines) {
117
		this.guidelines = guidelines;
118
	}
119

    
120
	public int getTotalJobs() {
121
		return totalJobs;
122
	}
123

    
124
	public void setTotalJobs(int totalJobs) {
125
		this.totalJobs = totalJobs;
126
	}
127

    
128
	public JobsDAO getJobsDao() {
129
		return jobsDao;
130
	}
131

    
132
	public void setJobsDao(JobsDAO jobsDao) {
133
		this.jobsDao = jobsDao;
134
	}
135

    
136
	
137
}
(1-1/7)