Project

General

Profile

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

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

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

    
11
import eu.dnetlib.api.data.DatasourceManagerService;
12
import eu.dnetlib.validator.commons.dao.jobs.JobsDAO;
13
import eu.dnetlib.validator.commons.email.Emailer;
14
import eu.dnetlib.validator.engine.execution.CompletedTask;
15
import eu.dnetlib.validator.engine.execution.JobListener;
16
import gr.uoa.di.driver.util.ServiceLocator;
17
import org.springframework.util.LinkedMultiValueMap;
18
import org.springframework.util.MultiValueMap;
19
import org.springframework.web.client.RestTemplate;
20

    
21
public class RegistrationListener implements JobListener{
22
	
23
//	private String valBaseUrl = null;
24
	private String providerUrl = null;
25
	private JobsDAO jobsDao;
26

    
27
	private int jobId;
28
//	private String activationId;
29
	private String userMail;
30
//	private String officialName;
31
//	private String baseUrl;
32
	private String datasourceId;
33
	private String interfaceId;
34
//	private String interfaceIdOld;
35
//	private String validationSet;
36
//	private String desiredCompLevel;
37
//	private String repoType;
38

    
39
	private boolean updateExisting;
40
	
41
	private int totalJobs;
42
	private int jobsFinished = 0;
43
	
44
	private int score_content = 0, score_usage = 0;
45

    
46
	private static Logger logger = Logger.getLogger(RegistrationListener.class);
47

    
48
	public RegistrationListener() {
49
		logger.error("Creating new registration listener");
50
	}
51

    
52
	@Override
53
	@Transactional(propagation = Propagation.REQUIRED)
54
	public synchronized void finished(int jobId, Map<String, Object> jobContext) {
55

    
56
		try{
57
			jobsFinished++;
58
			if (jobContext.containsKey("score_content")) {
59
				score_content = (Integer) jobContext.get("score_content");
60
			} else if (jobContext.containsKey("score_usage")) {
61
				score_usage = (Integer) jobContext.get("score_usage");
62
			}
63
			if (jobsFinished == totalJobs) {
64
				logger.debug("all jobs for registration finished");
65
				jobsDao.setTotalJobFinished(jobId, null, false);
66
				logger.debug("id:"+jobId+ "c: " + score_content + " u:" + score_usage);
67
				this.jobSuccess((Integer) jobContext.get("jobSubmittedId"), score_content, score_usage);
68
			} else {
69
				logger.debug("not all jobs finished yet. Waiting "+ (totalJobs-jobsFinished) + " job(s) to finish" );
70
			}
71
		} catch (Exception e) {
72
			logger.error("Error while finalizing successfull registration job");
73
		}
74
	}
75

    
76
	@Override
77
	@Transactional(propagation = Propagation.REQUIRED)
78
	public synchronized void failed(int jobId, Map<String, Object> jobContext, Throwable t) {
79

    
80
		try{
81
			jobsDao.setTotalJobFinished(jobId, t.getMessage(), true);
82
			this.jobFailure((Integer) jobContext.get("jobSubmittedId"),t.getMessage());
83
		} catch (Exception e) {
84
			logger.error("Error while finalizing failed registration job");
85
		}
86
	}
87

    
88
	@Override
89
	public synchronized void currentResults(List<CompletedTask> tasks, int jobId,
90
											Object object, Map<String, Object> recordContext, Throwable t) {
91
		// We don't care about partial results.
92
	}
93

    
94
	@Override
95
	public synchronized void currentResults(List<CompletedTask> tasks, int jobId,
96
											Object object, Map<String, Object> recordContext) {
97
		// We don't care about partial results.
98
	}
99

    
100
	public synchronized void jobSuccess(int jobId, int score_content, int score_usage) {
101
		logger.info("Preregistration job "+jobId+" finished");
102

    
103
		this.score_content = score_content;
104
		this.score_usage = score_usage;
105
		this.jobId=jobId;
106

    
107
		this.success();
108
	}
109

    
110
	public synchronized void jobFailure(int jobId, String error) {
111
		logger.info("Pregistration job "+jobId+" failed with exception: "+error);
112

    
113
		this.jobId=jobId;
114

    
115
		this.error();
116
	}
117
	
118
	private void success() {
119
		try {
120
		    logger.debug("Sending results to provide @ " + providerUrl +"/validator/complete");
121
			HttpHeaders headers = new HttpHeaders();
122
			headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
123

    
124
			MultiValueMap<String, String> map= new LinkedMultiValueMap<String, String>();
125
			map.add("interfaceId", this.interfaceId);
126
			map.add("repoId",this.datasourceId);
127
			map.add("jobId",Integer.toString(this.jobId));
128
			map.add("issuerEmail",this.userMail);
129
			map.add("isUpdate",Boolean.toString(this.updateExisting));
130

    
131
			// isUsccess: the job finished succesfully, regardless of validation scores.
132

    
133
			map.add("isSuccess","true");
134
			map.add("scoreUsage", Integer.toString(score_usage));
135
			map.add("scoreContent",Integer.toString(score_content));
136

    
137
			HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(map, headers);
138
			RestTemplate restTemplate = new RestTemplate();
139

    
140
			ResponseEntity<String> response = restTemplate.postForEntity( providerUrl + "/validator/complete", request , String.class );
141

    
142
//			this.updateRepositoryInterfaceCompliance(officialName, datasourceId, interfaceId, desiredCompLevel, validationSet, baseUrl, interfaceIdOld);
143

    
144
		} catch (Exception e) {
145
			logger.error("", e);
146
		}
147
	}
148

    
149
	private void error() {
150
		try {
151
			logger.debug("Sending results to provide @ " + providerUrl + "/validator/complete");
152
			HttpHeaders headers = new HttpHeaders();
153
			headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
154

    
155
			MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
156
			map.add("interfaceId", this.interfaceId);
157
			map.add("repoId", this.datasourceId);
158
			map.add("jobId", this.jobId + "");
159
			map.add("issuerEmail", this.userMail);
160
			map.add("isUpdate", this.updateExisting + "");
161

    
162
			// The job failed to complete. Validator error or Repository is completely dead and cannot be validated.
163
			map.add("isSuccess", "false");
164
			map.add("scoreUsage", score_usage + "");
165
			map.add("scoreContent", score_content + "");
166

    
167
			HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(map, headers);
168
			RestTemplate restTemplate = new RestTemplate();
169

    
170
			ResponseEntity<String> response = restTemplate.postForEntity(providerUrl + "/validator/complete", request, String.class);
171

    
172
//			this.updateRepositoryInterfaceCompliance(officialName, datasourceId, interfaceId, "notCompatible", validationSet, baseUrl, interfaceIdOld);
173
		} catch (Exception e) {
174
			logger.error("", e);
175
		}
176
	}
177

    
178

    
179
	
180
//	public boolean updateRepositoryInterfaceCompliance(String officialName,
181
//			String datasourceId, String interfaceId, String desiredCompliance, String set, String baseUrl, String oldId)
182
//					throws Exception {
183
//		boolean ret = true;
184
//		try {
185
//			if (desiredCompliance.equalsIgnoreCase("openaire2.0_data"))
186
//				desiredCompliance = "openaire2.0";
187
//			logger.debug("updating repository " + officialName + " compliance to : " + desiredCompliance);
188
//			if (oldId == null) {
189
//				dmService.getService().updateLevelOfCompliance(datasourceId, interfaceId, desiredCompliance);
190
//			}
191
//			else {
192
//				logger.debug("Checking if old interface should be updated");
193
//				if (!desiredCompliance.equalsIgnoreCase("UNKNOWN") && (!desiredCompliance.equalsIgnoreCase("notCompatible"))) {
194
//					logger.debug("updating old interface with new set/url");
195
//					dmService.getService().updateBaseUrl(datasourceId, oldId, baseUrl);
196
//					if(set.equalsIgnoreCase("none"))
197
//						dmService.getService().deleteAccessParamOrExtraField(datasourceId, oldId, "set");
198
//					else
199
//						dmService.getService().updateAccessParam(datasourceId, oldId, "set", set, false);
200
//					logger.debug("deleting new interface");
201
//					dmService.getService().deleteInterface(datasourceId, interfaceId);
202
//
203
//				}
204
//				logger.debug("updating repository " + officialName + " compliance to : " + desiredCompliance);
205
//			}
206
//			java.util.Date utilDate = new java.util.Date();
207
//			java.sql.Timestamp date = new java.sql.Timestamp(utilDate.getTime());
208
//
209
//			String updateQuery = "UPDATE datasources SET activationid = " + null + "," +
210
//					" dateofvalidation = '" + date + "'" +
211
//					" WHERE id = '" + datasourceId + "'";
212
//
213
//			if (dmService.getService().updateSQL(datasourceId, updateQuery, false))
214
//				logger.debug("updated successfully");
215
//			else
216
//				logger.error("error while updating: " + updateQuery);
217
//
218
//		} catch (Exception e) {
219
//			logger.error("error connecting to dms to set a repo interface as openaire compliant " + officialName, e);
220
//			ret = false;
221
//			throw e;
222
//		}
223
//		return ret;
224
//	}
225
//
226

    
227
	public String getProviderUrl() {
228
		return providerUrl;
229
	}
230

    
231
	public void setProviderUrl(String providerUrl) {
232
		this.providerUrl = providerUrl;
233
	}
234

    
235
	public JobsDAO getJobsDao() {
236
		return jobsDao;
237
	}
238

    
239
	public void setJobsDao(JobsDAO jobsDao) {
240
		this.jobsDao = jobsDao;
241
	}
242

    
243
	public int getJobId() {
244
		return jobId;
245
	}
246

    
247
	public void setJobId(int jobId) {
248
		this.jobId = jobId;
249
	}
250

    
251
	public String getUserMail() {
252
		return userMail;
253
	}
254

    
255
	public void setUserMail(String userMail) {
256
		this.userMail = userMail;
257
	}
258

    
259
	public String getDatasourceId() {
260
		return datasourceId;
261
	}
262

    
263
	public void setDatasourceId(String datasourceId) {
264
		this.datasourceId = datasourceId;
265
	}
266

    
267
	public String getInterfaceId() {
268
		return interfaceId;
269
	}
270

    
271
	public void setInterfaceId(String interfaceId) {
272
		this.interfaceId = interfaceId;
273
	}
274

    
275
	public boolean isUpdateExisting() {
276
		return updateExisting;
277
	}
278

    
279
	public void setUpdateExisting(boolean updateExisting) {
280
		this.updateExisting = updateExisting;
281
	}
282

    
283
	public int getTotalJobs() {
284
		return totalJobs;
285
	}
286

    
287
	public void setTotalJobs(int totalJobs) {
288
		this.totalJobs = totalJobs;
289
	}
290
}
(7-7/8)