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
		try {
32
			jobsFinished++;
33
			if (jobsFinished == totalJobs) {
34
				logger.debug("all jobs finished");
35
				jobsDao.setTotalJobFinished(jobId, null, false);
36
				this.sendMail((Integer) jobContext.get("jobSubmittedId"), (String) jobContext.get("jobSubmittedUser"));
37
			} else {
38
				logger.debug("not all jobs finished yet. Waiting "+ (totalJobs-jobsFinished) + " job(s) to finish" );
39
			}
40
		} catch (Exception e) {
41
			logger.error("Error while finalizing successfull compatibility test job", e);
42
		}
43
	}
44

    
45
	@Override
46
	@Transactional(propagation = Propagation.REQUIRED)
47
	public synchronized void failed(int jobId, Map<String, Object> jobContext, Throwable t) {
48
		try {
49
			jobsFinished++;
50
			if (jobsFinished == totalJobs) {
51
				jobsDao.setTotalJobFinished(jobId, t.getMessage(), true);
52
				this.sendMail((Integer) jobContext.get("jobSubmittedId"), (String) jobContext.get("jobSubmittedUser"));
53
			} else {
54
				logger.debug("not all jobs finished yet. Waiting "+ (totalJobs-jobsFinished) + " job(s) to finish" );
55
			}
56
		} catch (Exception e) {
57
			logger.error("Error while finalizing failed compatibility test job", e);
58
		}
59
	}
60

    
61

    
62
	
63
	private void sendMail(int jobSubmittedId, String jobSubmittedUser) {
64
		logger.debug("JOBID:"+jobSubmittedId+"# Sending email for finished job");
65
		String mailTo = jobSubmittedUser;
66
		List<String> recipients = new ArrayList<String>();
67
		recipients.add(mailTo);
68
		String msgUpgrade = "";
69
//		if (this.validationSet != null && !this.validationSet.equalsIgnoreCase("openaire"))
70
		if (this.guidelines.equalsIgnoreCase("openaire2.0") || this.guidelines.equalsIgnoreCase("driver"))
71
			msgUpgrade = "\n\n Please consider to upgrade to OpenAIRE Guidelines v3. Link: https://guidelines.openaire.eu/wiki/OpenAIRE_Guidelines:_For_Literature_repositories";
72
		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;
73

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

    
89
	@Override
90
	public void currentResults(List<CompletedTask> tasks, int jobId,
91
			Object object, Map<String, Object> recordContext) {
92
		// TODO Auto-generated method stub
93
		
94
	}
95

    
96
	public String getValBaseUrl() {
97
		return valBaseUrl;
98
	}
99

    
100
	public void setValBaseUrl(String valBaseUrl) {
101
		this.valBaseUrl = valBaseUrl;
102
	}
103

    
104
	public String getValidationSet() {
105
		return validationSet;
106
	}
107

    
108
	public void setValidationSet(String validationSet) {
109
		this.validationSet = validationSet;
110
	}
111

    
112
	public Emailer getEmailer() {
113
		return emailer;
114
	}
115

    
116
	public void setEmailer(Emailer emailer) {
117
		this.emailer = emailer;
118
	}
119

    
120
	public String getGuidelines() {
121
		return guidelines;
122
	}
123

    
124
	public void setGuidelines(String guidelines) {
125
		this.guidelines = guidelines;
126
	}
127

    
128
	public int getTotalJobs() {
129
		return totalJobs;
130
	}
131

    
132
	public void setTotalJobs(int totalJobs) {
133
		this.totalJobs = totalJobs;
134
	}
135

    
136
	public JobsDAO getJobsDao() {
137
		return jobsDao;
138
	}
139

    
140
	public void setJobsDao(JobsDAO jobsDao) {
141
		this.jobsDao = jobsDao;
142
	}
143

    
144
	
145
}
(1-1/8)