Project

General

Profile

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

    
3
import eu.dnetlib.validator.commons.dao.jobs.JobsDAO;
4
import eu.dnetlib.validator.engine.execution.CompletedTask;
5
import eu.dnetlib.validator.engine.execution.JobListener;
6
import org.apache.log4j.Logger;
7
import org.springframework.transaction.annotation.Propagation;
8
import org.springframework.transaction.annotation.Transactional;
9

    
10
import java.util.List;
11
import java.util.Map;
12

    
13
public abstract class AbstractValidatorListener implements JobListener {
14

    
15
    private static Logger logger = Logger.getLogger(AbstractValidatorListener.class);
16
    private JobsDAO jobsDao;
17
    protected int jobId;
18
    protected boolean updateExisting;
19
    private int totalJobs;
20
    private int jobsFinished = 0;
21

    
22
    protected int score_content = 0, score_usage = 0;
23

    
24

    
25
    public AbstractValidatorListener() {
26
        logger.error("Creating new validator listener");
27
    }
28

    
29
    protected abstract void jobSuccess(int jobId,  Map<String, Object> jobContext);
30

    
31
    protected abstract void jobFailure(int jobId,  Map<String, Object> jobContext, Throwable t);
32

    
33

    
34
    @Override
35
    @Transactional(propagation = Propagation.REQUIRED)
36
    public synchronized void finished(int jobId, Map<String, Object> jobContext) {
37

    
38
        try{
39
            jobsFinished++;
40
            if (jobContext.containsKey("score_content")) {
41
                score_content = (Integer) jobContext.get("score_content");
42
            } else if (jobContext.containsKey("score_usage")) {
43
                score_usage = (Integer) jobContext.get("score_usage");
44
            }
45
            if (jobsFinished == totalJobs) {
46
                logger.debug("all jobs for registration finished");
47
                jobsDao.setTotalJobFinished(jobId, null, false);
48
                logger.debug("id:"+jobId+ "c: " + score_content + " u:" + score_usage);
49
                jobSuccess((Integer) jobContext.get("jobSubmittedId"), jobContext);
50
            } else {
51
                logger.debug("not all jobs finished yet. Waiting "+ (totalJobs-jobsFinished) + " job(s) to finish" );
52
            }
53
        } catch (Exception e) {
54
            logger.error("Error while finalizing successfull registration job");
55
        }
56
    }
57

    
58
    @Override
59
    @Transactional(propagation = Propagation.REQUIRED)
60
    public synchronized void failed(int jobId, Map<String, Object> jobContext, Throwable t) {
61

    
62
        try{
63
            jobsDao.setTotalJobFinished(jobId, t.getMessage(), true);
64
            this.jobFailure((Integer) jobContext.get("jobSubmittedId"), jobContext,t);
65
        } catch (Exception e) {
66
            logger.error("Error while finalizing failed registration job");
67
        }
68
    }
69

    
70
    @Override
71
    public synchronized void currentResults(List<CompletedTask> tasks, int jobId,
72
                                            Object object, Map<String, Object> recordContext, Throwable t) {
73
        // We don't care about partial results.
74
    }
75

    
76
    @Override
77
    public synchronized void currentResults(List<CompletedTask> tasks, int jobId,
78
                                            Object object, Map<String, Object> recordContext) {
79
        // We don't care about partial results.
80
    }
81

    
82
    public JobsDAO getJobsDao() {
83
        return jobsDao;
84
    }
85

    
86
    public void setJobsDao(JobsDAO jobsDao) {
87
        this.jobsDao = jobsDao;
88
    }
89

    
90
    public int getJobId() {
91
        return jobId;
92
    }
93

    
94
    public void setJobId(int jobId) {
95
        this.jobId = jobId;
96
    }
97

    
98
    public boolean isUpdateExisting() {
99
        return updateExisting;
100
    }
101

    
102
    public void setUpdateExisting(boolean updateExisting) {
103
        this.updateExisting = updateExisting;
104
    }
105

    
106
    public int getTotalJobs() {
107
        return totalJobs;
108
    }
109

    
110
    public void setTotalJobs(int totalJobs) {
111
        this.totalJobs = totalJobs;
112
    }
113

    
114
    public int getJobsFinished() {
115
        return jobsFinished;
116
    }
117

    
118
    public void setJobsFinished(int jobsFinished) {
119
        this.jobsFinished = jobsFinished;
120
    }
121

    
122
    public int getScore_content() {
123
        return score_content;
124
    }
125

    
126
    public void setScore_content(int score_content) {
127
        this.score_content = score_content;
128
    }
129

    
130
    public int getScore_usage() {
131
        return score_usage;
132
    }
133

    
134
    public void setScore_usage(int score_usage) {
135
        this.score_usage = score_usage;
136
    }
137
}
(1-1/9)