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

    
253
	
254
	public boolean isUpdateExisting() {
255
		return updateExisting;
256
	}
257

    
258
	public void setUpdateExisting(boolean updateExisting) {
259
		this.updateExisting = updateExisting;
260
	}
261
	
262
	public Emailer getEmailer() {
263
		return emailer;
264
	}
265

    
266
	public void setEmailer(Emailer emailer) {
267
		this.emailer = emailer;
268
	}
269

    
270
	public String getValBaseUrl() {
271
		return valBaseUrl;
272
	}
273

    
274
	public void setValBaseUrl(String valBaseUrl) {
275
		this.valBaseUrl = valBaseUrl;
276
	}
277

    
278
	public String getActivationId() {
279
		return activationId;
280
	}
281

    
282

    
283
	public void setActivationId(String activationId) {
284
		this.activationId = activationId;
285
	}
286

    
287

    
288
	public String getUserMail() {
289
		return userMail;
290
	}
291

    
292

    
293
	public void setUserMail(String userMail) {
294
		this.userMail = userMail;
295
	}
296

    
297

    
298
	public String getOfficialName() {
299
		return officialName;
300
	}
301

    
302

    
303
	public void setOfficialName(String officialName) {
304
		this.officialName = officialName;
305
	}
306

    
307

    
308
	public String getBaseUrl() {
309
		return baseUrl;
310
	}
311

    
312

    
313
	public void setBaseUrl(String baseUrl) {
314
		this.baseUrl = baseUrl;
315
	}
316

    
317
	public String getDatasourceId() {
318
		return datasourceId;
319
	}
320

    
321
	public void setDatasourceId(String datasourceId) {
322
		this.datasourceId = datasourceId;
323
	}
324

    
325
	public String getInterfaceId() {
326
		return interfaceId;
327
	}
328

    
329
	public void setInterfaceId(String interfaceId) {
330
		this.interfaceId = interfaceId;
331
	}
332

    
333
	public String getValidationSet() {
334
		return validationSet;
335
	}
336

    
337
	public void setValidationSet(String validationSet) {
338
		this.validationSet = validationSet;
339
	}
340

    
341
	public List<String> getAdminEmails() {
342
		return adminEmails;
343
	}
344

    
345

    
346
	public void setAdminEmails(List<String> adminEmails) {
347
		this.adminEmails = adminEmails;
348
	}
349

    
350

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

    
358
	@Override
359
	public synchronized void currentResults(List<CompletedTask> tasks, int jobId,
360
			Object object, Map<String, Object> recordContext) {
361
		// TODO Auto-generated method stub
362
		
363
	}
364

    
365

    
366
	public String getDesiredCompLevel() {
367
		return desiredCompLevel;
368
	}
369

    
370
	public void setDesiredCompLevel(String desiredCompLevel) {
371
		this.desiredCompLevel = desiredCompLevel;
372
	}
373

    
374
	public String getRepoType() {
375
		return repoType;
376
	}
377

    
378
	public void setRepoType(String repoType) {
379
		this.repoType = repoType;
380
	}
381

    
382
	public String getInterfaceIdOld() {
383
		return interfaceIdOld;
384
	}
385

    
386
	public void setInterfaceIdOld(String interfaceIdOld) {
387
		this.interfaceIdOld = interfaceIdOld;
388
	}
389

    
390
	public int getTotalJobs() {
391
		return totalJobs;
392
	}
393

    
394
	public void setTotalJobs(int totalJobs) {
395
		this.totalJobs = totalJobs;
396
	}
397

    
398
	public ServiceLocator<DatasourceManagerService> getDmService() {
399
		return dmService;
400
	}
401

    
402
	public void setDmService(ServiceLocator<DatasourceManagerService> dmService) {
403
		this.dmService = dmService;
404
	}
405

    
406
	public JobsDAO getJobsDao() {
407
		return jobsDao;
408
	}
409

    
410
	public void setJobsDao(JobsDAO jobsDao) {
411
		this.jobsDao = jobsDao;
412
	}
413

    
414
}
(7-7/8)