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
//TODO
112
			this.updateRepositoryInterfaceCompliance(officialName, datasourceId, interfaceId, desiredCompLevel, validationSet, baseUrl, interfaceIdOld);
113
//			this.getOpenAIREValidator().deleteJobForRegistration(activationId);
114
						
115
			// mail to user informing him of the success
116
			List<String> rec = new ArrayList<String>();
117
			rec.add(this.userMail);
118
			if (adminEmails.contains(userMail)) {
119
				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);
120
			}
121
			else {
122
				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);
123
				rec.clear();
124
				rec.addAll(adminEmails);
125
				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);
126
			}
127
				
128
			// mail to specials informing them of the new repository
129
			// registration
130
			rec.clear();
131
			if (this.isUpdateExisting())
132
				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);
133
			else
134
				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);
135
			
136
		} catch (Exception e) {
137
			logger.error("", e);
138
		}
139
	}
140

    
141
	private void criticalFailure() {
142
		try {
143
			String msgGuidelines = "OpenAIRE For Literature Repositories (OpenAIRE 3.0)";
144
			if (this.desiredCompLevel.equalsIgnoreCase("openaire2.0")) {
145
				msgGuidelines = "OpenAIRE For Literature Repositories (OpenAIRE 2.0)";
146
			} else if (this.desiredCompLevel.equalsIgnoreCase("driver")) {
147
				msgGuidelines = "OpenAIRE For Literature Repositories (Basic - ex DRIVER)";
148
			} else if (this.desiredCompLevel.equalsIgnoreCase("openaire2.0_data")) {
149
				msgGuidelines = "OpenAIRE For Data Archives (OpenAIRE 2.0)";
150
			} else if (this.desiredCompLevel.equalsIgnoreCase("openaire3.0")) {
151
				msgGuidelines = "OpenAIRE For Literature Repositories (OpenAIRE 3.0)";
152
			}
153
			logger.debug("Critical Failure of pre-registration validations on repo "+baseUrl);
154
			// mail to inform user of failure
155
			List<String> rec = new ArrayList<String>();
156
			rec.add(this.userMail);
157
			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);
158
			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);
159
			
160
//			openDoarRepositoryFactory.updateRepositoryInterfaceCompliance(officialName, datasourceId, interfaceId, "notCompatible", validationSet, baseUrl, interfaceIdOld);
161
//
162
//			this.getOpenAIREValidator().deleteJobForRegistration(activationId);
163
			
164
		} catch (Exception e) {
165
			logger.error("", e);
166
		}
167
	}
168

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

    
249
	
250
	public boolean isUpdateExisting() {
251
		return updateExisting;
252
	}
253

    
254
	public void setUpdateExisting(boolean updateExisting) {
255
		this.updateExisting = updateExisting;
256
	}
257
	
258
	public Emailer getEmailer() {
259
		return emailer;
260
	}
261

    
262
	public void setEmailer(Emailer emailer) {
263
		this.emailer = emailer;
264
	}
265

    
266
	public String getValBaseUrl() {
267
		return valBaseUrl;
268
	}
269

    
270
	public void setValBaseUrl(String valBaseUrl) {
271
		this.valBaseUrl = valBaseUrl;
272
	}
273

    
274
	public String getActivationId() {
275
		return activationId;
276
	}
277

    
278

    
279
	public void setActivationId(String activationId) {
280
		this.activationId = activationId;
281
	}
282

    
283

    
284
	public String getUserMail() {
285
		return userMail;
286
	}
287

    
288

    
289
	public void setUserMail(String userMail) {
290
		this.userMail = userMail;
291
	}
292

    
293

    
294
	public String getOfficialName() {
295
		return officialName;
296
	}
297

    
298

    
299
	public void setOfficialName(String officialName) {
300
		this.officialName = officialName;
301
	}
302

    
303

    
304
	public String getBaseUrl() {
305
		return baseUrl;
306
	}
307

    
308

    
309
	public void setBaseUrl(String baseUrl) {
310
		this.baseUrl = baseUrl;
311
	}
312

    
313
	public String getDatasourceId() {
314
		return datasourceId;
315
	}
316

    
317
	public void setDatasourceId(String datasourceId) {
318
		this.datasourceId = datasourceId;
319
	}
320

    
321
	public String getInterfaceId() {
322
		return interfaceId;
323
	}
324

    
325
	public void setInterfaceId(String interfaceId) {
326
		this.interfaceId = interfaceId;
327
	}
328

    
329
	public String getValidationSet() {
330
		return validationSet;
331
	}
332

    
333
	public void setValidationSet(String validationSet) {
334
		this.validationSet = validationSet;
335
	}
336

    
337
	public List<String> getAdminEmails() {
338
		return adminEmails;
339
	}
340

    
341

    
342
	public void setAdminEmails(List<String> adminEmails) {
343
		this.adminEmails = adminEmails;
344
	}
345

    
346

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

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

    
361

    
362
	public String getDesiredCompLevel() {
363
		return desiredCompLevel;
364
	}
365

    
366
	public void setDesiredCompLevel(String desiredCompLevel) {
367
		this.desiredCompLevel = desiredCompLevel;
368
	}
369

    
370
	public String getRepoType() {
371
		return repoType;
372
	}
373

    
374
	public void setRepoType(String repoType) {
375
		this.repoType = repoType;
376
	}
377

    
378
	public String getInterfaceIdOld() {
379
		return interfaceIdOld;
380
	}
381

    
382
	public void setInterfaceIdOld(String interfaceIdOld) {
383
		this.interfaceIdOld = interfaceIdOld;
384
	}
385

    
386
	public int getTotalJobs() {
387
		return totalJobs;
388
	}
389

    
390
	public void setTotalJobs(int totalJobs) {
391
		this.totalJobs = totalJobs;
392
	}
393

    
394
	public ServiceLocator<DatasourceManagerService> getDmService() {
395
		return dmService;
396
	}
397

    
398
	public void setDmService(ServiceLocator<DatasourceManagerService> dmService) {
399
		this.dmService = dmService;
400
	}
401

    
402
	public JobsDAO getJobsDao() {
403
		return jobsDao;
404
	}
405

    
406
	public void setJobsDao(JobsDAO jobsDao) {
407
		this.jobsDao = jobsDao;
408
	}
409

    
410
}
(6-6/7)