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.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

    
18
public class RegistrationListener implements JobListener{
19
	
20
	private Emailer emailer = null;
21
	private String valBaseUrl = null;
22
	private JobsDAO jobsDao;
23
	private ServiceLocator<DatasourceManagerService> dmService = null;
24
	
25
	private static final int BASE_SCORE = 50;
26
	
27
	private int jobId;
28
	private String activationId, userMail, officialName, baseUrl, datasourceId, interfaceId, interfaceIdOld, validationSet, desiredCompLevel, repoType;
29

    
30
	private String msgSubject;
31
	
32
	private boolean updateExisting;
33

    
34
	private List<String> adminEmails;
35
	
36
	private boolean job1Success = false;
37
	private boolean job2Success = false; 
38
	
39
	private boolean critFailDone = false;
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
	
49
	public RegistrationListener() {
50
		logger.debug("Creating a pre-registration listener for job "+jobId + " on repo "+baseUrl);
51
	}
52

    
53
	public synchronized void jobSuccess(int jobId, int score_content, int score_usage) {
54
		logger.debug("Preregistration job "+jobId+" finished");
55
		this.score_content = score_content;
56
		this.score_usage = score_usage;
57
		this.jobId=jobId;
58
		if (score_content >= BASE_SCORE)
59
			job1Success = true;
60
		if (score_usage >= BASE_SCORE)
61
			job2Success = true;
62
		if (job1Success == job2Success == true) {
63
			this.criticalSuccess();
64
		} else {
65
			if(!critFailDone)
66
				this.criticalFailure();
67
			critFailDone = true;
68
		}
69
	}
70

    
71
	
72
	public synchronized void jobFailure(int jobId, String error) {
73
		logger.debug("Pregistration job "+jobId+" failed with exception: "+error);
74
		this.jobId=jobId;
75
		if(!critFailDone)
76
			this.criticalFailure();
77
		critFailDone = true;
78
	}
79
	
80
	private void criticalSuccess() {
81
		try {
82
			String msgOpenaireV2 = "OpenAIRE v2, please consider to upgrade to OpenAIRE Guidelines v3. Link: https://guidelines.openaire.eu/wiki/OpenAIRE_Guidelines:_For_Literature_repositories";
83
			String msgDriver = "OpenAIRE Basic (ex DRIVER) Guidelines, please consider to upgrade to OpenAIRE Guidelines v3. Link: https://guidelines.openaire.eu/wiki/OpenAIRE_Guidelines:_For_Literature_repositories";
84
			String msgOpenaireV3 = "OpenAIRE Guidelines v3 (for literature)";
85
			String msgOpenaireV2_data = "OpenAIRE Guidelines v2 (for data archives)";
86
			String msgVersion = msgOpenaireV3;
87
			String msg100 = "\n\nWe also encourage you to display the following logo at your repository's/journal's homepage to indicate that it is 100% OpenAIRE compliant: " + valBaseUrl + "/jsps/images/compliant.png";
88
			String msgInformUser = "An email has been send to the repository's/journal's administrators to inform them about this registration";
89
			String msgInformAdmins = "The " + repoType + " was registered by user: " + userMail + " , who is not listed as repository's/journal's administrator. If you have any concerns, please visit " + valBaseUrl + " to edit your repository's/journal's information. Please Sign in or Register with this email address.";
90
			String msgGuidelines = "OpenAIRE Guidelines v3 (for literature)";
91
			logger.debug("Critical Success of pre-registration validations on repo: "+baseUrl+" and set: " + validationSet + " with scores ("+score_content+", "+score_usage+")");
92
			
93
			if (this.desiredCompLevel.equalsIgnoreCase("openaire2.0")) {
94
				msgVersion = msgOpenaireV2;
95
				msgGuidelines = "OpenAIRE For Literature Repositories (OpenAIRE 2.0)";
96
			} else if (this.desiredCompLevel.equalsIgnoreCase("driver")) {
97
				msgVersion = msgDriver;
98
				msgGuidelines = "OpenAIRE For Literature Repositories (Basic - ex DRIVER)";
99
			} else if (this.desiredCompLevel.equalsIgnoreCase("openaire2.0_data")) {
100
				msgVersion = msgOpenaireV2_data;
101
				msgGuidelines = "OpenAIRE For Data Archives (OpenAIRE 2.0)";
102
			} else if (this.desiredCompLevel.equalsIgnoreCase("openaire3.0")) {
103
				msgVersion = msgOpenaireV3;
104
				msgGuidelines = "OpenAIRE For Literature Repositories (OpenAIRE 3.0)";
105
			}
106
			if (score_content == 100 && score_usage == 100)
107
				emailer.sendMail(this.adminEmails, getMsgSubject() + "- Test Results [" + this.officialName + "]", "Congratulations!! Your " + repoType + " has successfully passed the test to become compliant with " + msgVersion + "." + msg100, false, null);
108
			else
109
				emailer.sendMail(this.adminEmails, getMsgSubject() + "- Test Results [" + this.officialName + "]", "Congratulations!! Your " + repoType + " has successfully passed the test to become compliant with " + msgVersion + ".", false, null);
110
			
111
			this.updateRepositoryInterfaceCompliance(officialName, datasourceId, interfaceId, desiredCompLevel, validationSet, baseUrl, interfaceIdOld);
112
						
113
			// mail to user informing him of the success
114
			List<String> rec = new ArrayList<String>();
115
			rec.add(this.userMail);
116
			if (adminEmails.contains(userMail)) {
117
				emailer.sendMail(rec, getMsgSubject() + "- Results [" + this.officialName + "]", "The compatibility test of your " + repoType + " was successful. Nevertheless, some erros might still exist. We strongly recommend reviewing the results and correcting any errors found:\n" + valBaseUrl + "/prepareSummary.action?jobId=" + this.jobId +  "\n", false, null);
118
			}
119
			else {
120
				emailer.sendMail(rec, getMsgSubject() + "- Results [" + this.officialName + "]", "The compatibility test of your " + repoType + " was successful. Nevertheless, some erros might still exist. We strongly recommend reviewing the results and correcting any errors found:\n" + valBaseUrl + "/prepareSummary.action?jobId=" + this.jobId +  "\n\n" + msgInformUser, false, null);
121
				rec.clear();
122
				rec.addAll(adminEmails);
123
				emailer.sendMail(rec, getMsgSubject() + "- Results [" + this.officialName + "]", "The compatibility test of your " + repoType + " was successful. Nevertheless, some erros might still exist. We strongly recommend reviewing the results and correcting any errors found:\n" + valBaseUrl + "/prepareSummary.action?jobId=" + this.jobId  + "\n\n" + msgInformAdmins, false, null);
124
			}
125
				
126
			// mail to specials informing them of the new repository
127
			// registration
128
			rec.clear();
129
			if (this.isUpdateExisting())
130
				emailer.sendMail(rec, getMsgSubject() + "- Results (success) [" + this.officialName + "]", "An OpenAIRE compliant " + repoType + " has been updated." + "\n\nOfficial Name: " + this.officialName + "\n\nBase URL: " + this.baseUrl + "\n\nValidation Set: " + this.validationSet + "\n\nGuidelines: " + msgGuidelines, true, null);
131
			else
132
				emailer.sendMail(rec, getMsgSubject() + "- Results (success) [" + this.officialName + "]", "A new " + repoType + " is ready to be added in the OpenAIRE compliant list." + "\n\nOfficial Name: " + this.officialName + "\n\nBase URL: " + this.baseUrl + "\n\nValidation Set: " + this.validationSet + "\n\nGuidelines: " + msgGuidelines, true, null);
133
			
134
		} catch (Exception e) {
135
			logger.error("", e);
136
		}
137
	}
138

    
139
	private void criticalFailure() {
140
		try {
141
			String msgGuidelines = "OpenAIRE For Literature Repositories (OpenAIRE 3.0)";
142
			if (this.desiredCompLevel.equalsIgnoreCase("openaire2.0")) {
143
				msgGuidelines = "OpenAIRE For Literature Repositories (OpenAIRE 2.0)";
144
			} else if (this.desiredCompLevel.equalsIgnoreCase("driver")) {
145
				msgGuidelines = "OpenAIRE For Literature Repositories (Basic - ex DRIVER)";
146
			} else if (this.desiredCompLevel.equalsIgnoreCase("openaire2.0_data")) {
147
				msgGuidelines = "OpenAIRE For Data Archives (OpenAIRE 2.0)";
148
			} else if (this.desiredCompLevel.equalsIgnoreCase("openaire3.0")) {
149
				msgGuidelines = "OpenAIRE For Literature Repositories (OpenAIRE 3.0)";
150
			}
151
			logger.debug("Critical Failure of pre-registration validations on repo "+baseUrl);
152
			// mail to inform user of failure
153
			List<String> rec = new ArrayList<String>();
154
			rec.add(this.userMail);
155
			emailer.sendMail(rec, getMsgSubject() + "- Results [" + this.officialName + "]", "The compatibility test of your " + repoType + " on repo: "+baseUrl+" and set: " + validationSet + " was unsuccessful and the join process has failed. \n" + "Your Scores: " + score_content + "/100 and " + score_usage + "/100 . Score required to pass: >50 \n" +" Please review the results and correct any errors: \n" + valBaseUrl + "/prepareSummary.action?jobId=" + this.jobId + "\n", false, null);
156
			emailer.sendMail(new ArrayList<String>(), getMsgSubject() + "- Results (failure) [" + this.officialName + "]", "The compatibility test on "+officialName+" was unsuccessful and the join process has failed. "+ "\n\nOfficial Name: " + this.officialName +  "\n\nBase URL: " + this.baseUrl + "\n\nValidation Set: " + this.validationSet + "\n\nGuidelines: " + msgGuidelines + "\n\nReview the validation results here: \n" + valBaseUrl + "/prepareSummary.action?jobId=" + this.jobId + "\n", true, null);
157
			
158
			this.updateRepositoryInterfaceCompliance(officialName, datasourceId, interfaceId, "notCompatible", validationSet, baseUrl, interfaceIdOld);
159
			
160
		} catch (Exception e) {
161
			logger.error("", e);
162
		}
163
	}
164

    
165
	@Override
166
	@Transactional(propagation = Propagation.REQUIRED)
167
	public synchronized void finished(int jobId, Map<String, Object> jobContext) {
168
		jobsFinished++;
169
		if (jobContext.containsKey("score_content")) {
170
			score_content = (Integer) jobContext.get("score_content");
171
		} else if (jobContext.containsKey("score_usage")) {
172
			score_usage = (Integer) jobContext.get("score_usage");
173
		}
174
		if (jobsFinished == totalJobs) {
175
			logger.debug("all jobs for registration finished");
176
			jobsDao.setTotalJobFinished(jobId, null, false);
177
			logger.debug("id:"+jobId+ "c: " + score_content + " u:" + score_usage);
178
			this.jobSuccess((Integer) jobContext.get("jobSubmittedId"), score_content, score_usage);
179
		} else {
180
			logger.debug("not all jobs finished yet. Waiting "+ (totalJobs-jobsFinished) + " job(s) to finish" );
181
		}
182
	}
183
	
184
	@Override
185
	@Transactional(propagation = Propagation.REQUIRED)
186
	public synchronized void failed(int jobId, Map<String, Object> jobContext, Throwable t) {
187
		jobsDao.setTotalJobFinished(jobId, t.getMessage(), true);
188
		this.jobFailure((Integer) jobContext.get("jobSubmittedId"),t.getMessage());	
189
	}
190
	
191
	public boolean updateRepositoryInterfaceCompliance(String officialName,
192
			String datasourceId, String interfaceId, String desiredCompliance, String set, String baseUrl, String oldId)
193
					throws Exception {
194
		boolean ret = true;
195
		try {
196
			if (desiredCompliance.equalsIgnoreCase("openaire2.0_data"))
197
				desiredCompliance = "openaire2.0";
198
			logger.debug("updating repository " + officialName + " compliance to : " + desiredCompliance);
199
			if (oldId == null) {
200
				dmService.getService().updateLevelOfCompliance(datasourceId, interfaceId, desiredCompliance);
201
			}
202
			else {
203
				logger.debug("Checking if old interface should be updated");
204
				if (!desiredCompliance.equalsIgnoreCase("UNKNOWN") && (!desiredCompliance.equalsIgnoreCase("notCompatible"))) {
205
					logger.debug("updating old interface with new set/url");
206
					dmService.getService().updateBaseUrl(datasourceId, oldId, baseUrl);
207
					if(set.equalsIgnoreCase("none"))
208
						dmService.getService().deleteAccessParamOrExtraField(datasourceId, oldId, "set");
209
					else
210
						dmService.getService().updateAccessParam(datasourceId, oldId, "set", set, false);
211
					logger.debug("deleting new interface");
212
					dmService.getService().deleteInterface(datasourceId, interfaceId);
213
					
214
				}
215
				logger.debug("updating repository " + officialName + " compliance to : " + desiredCompliance);
216
			}
217
			java.util.Date utilDate = new java.util.Date();
218
			java.sql.Timestamp date = new java.sql.Timestamp(utilDate.getTime());	
219
			
220
			String updateQuery = "UPDATE datasources SET activationid = " + null + "," +
221
					" dateofvalidation = '" + date + "'" +
222
					" WHERE id = '" + datasourceId + "'";
223
			
224
			if (dmService.getService().updateSQL(datasourceId, updateQuery, false))
225
				logger.debug("updated successfully");
226
			else
227
				logger.error("error while updating: " + updateQuery);
228
			
229
		} catch (Exception e) {
230
			logger.error("error connecting to dms to set a repo interface as openaire compliant " + officialName, e);
231
			ret = false;
232
			throw e;
233
		}  	
234
		return ret;
235
	}	
236
	
237
	public String getMsgSubject() {
238
		if (isUpdateExisting())
239
			msgSubject = "Request to update an OpenAIRE " + repoType + " ";
240
		else 
241
			msgSubject = "Request to Join OpenAIRE ";
242
		return msgSubject;
243
	}
244

    
245
	
246
	public boolean isUpdateExisting() {
247
		return updateExisting;
248
	}
249

    
250
	public void setUpdateExisting(boolean updateExisting) {
251
		this.updateExisting = updateExisting;
252
	}
253
	
254
	public Emailer getEmailer() {
255
		return emailer;
256
	}
257

    
258
	public void setEmailer(Emailer emailer) {
259
		this.emailer = emailer;
260
	}
261

    
262
	public String getValBaseUrl() {
263
		return valBaseUrl;
264
	}
265

    
266
	public void setValBaseUrl(String valBaseUrl) {
267
		this.valBaseUrl = valBaseUrl;
268
	}
269

    
270
	public String getActivationId() {
271
		return activationId;
272
	}
273

    
274

    
275
	public void setActivationId(String activationId) {
276
		this.activationId = activationId;
277
	}
278

    
279

    
280
	public String getUserMail() {
281
		return userMail;
282
	}
283

    
284

    
285
	public void setUserMail(String userMail) {
286
		this.userMail = userMail;
287
	}
288

    
289

    
290
	public String getOfficialName() {
291
		return officialName;
292
	}
293

    
294

    
295
	public void setOfficialName(String officialName) {
296
		this.officialName = officialName;
297
	}
298

    
299

    
300
	public String getBaseUrl() {
301
		return baseUrl;
302
	}
303

    
304

    
305
	public void setBaseUrl(String baseUrl) {
306
		this.baseUrl = baseUrl;
307
	}
308

    
309
	public String getDatasourceId() {
310
		return datasourceId;
311
	}
312

    
313
	public void setDatasourceId(String datasourceId) {
314
		this.datasourceId = datasourceId;
315
	}
316

    
317
	public String getInterfaceId() {
318
		return interfaceId;
319
	}
320

    
321
	public void setInterfaceId(String interfaceId) {
322
		this.interfaceId = interfaceId;
323
	}
324

    
325
	public String getValidationSet() {
326
		return validationSet;
327
	}
328

    
329
	public void setValidationSet(String validationSet) {
330
		this.validationSet = validationSet;
331
	}
332

    
333
	public List<String> getAdminEmails() {
334
		return adminEmails;
335
	}
336

    
337

    
338
	public void setAdminEmails(List<String> adminEmails) {
339
		this.adminEmails = adminEmails;
340
	}
341

    
342

    
343
	@Override
344
	public synchronized void currentResults(List<CompletedTask> tasks, int jobId,
345
			Object object, Map<String, Object> recordContext, Throwable t) {
346
		// TODO Auto-generated method stub
347
		
348
	}
349

    
350
	@Override
351
	public synchronized void currentResults(List<CompletedTask> tasks, int jobId,
352
			Object object, Map<String, Object> recordContext) {
353
		// TODO Auto-generated method stub
354
		
355
	}
356

    
357

    
358
	public String getDesiredCompLevel() {
359
		return desiredCompLevel;
360
	}
361

    
362
	public void setDesiredCompLevel(String desiredCompLevel) {
363
		this.desiredCompLevel = desiredCompLevel;
364
	}
365

    
366
	public String getRepoType() {
367
		return repoType;
368
	}
369

    
370
	public void setRepoType(String repoType) {
371
		this.repoType = repoType;
372
	}
373

    
374
	public String getInterfaceIdOld() {
375
		return interfaceIdOld;
376
	}
377

    
378
	public void setInterfaceIdOld(String interfaceIdOld) {
379
		this.interfaceIdOld = interfaceIdOld;
380
	}
381

    
382
	public int getTotalJobs() {
383
		return totalJobs;
384
	}
385

    
386
	public void setTotalJobs(int totalJobs) {
387
		this.totalJobs = totalJobs;
388
	}
389

    
390
	public ServiceLocator<DatasourceManagerService> getDmService() {
391
		return dmService;
392
	}
393

    
394
	public void setDmService(ServiceLocator<DatasourceManagerService> dmService) {
395
		this.dmService = dmService;
396
	}
397

    
398
	public JobsDAO getJobsDao() {
399
		return jobsDao;
400
	}
401

    
402
	public void setJobsDao(JobsDAO jobsDao) {
403
		this.jobsDao = jobsDao;
404
	}
405

    
406
}
(6-6/7)