1
|
package eu.dnetlib.msro.workflows.nodes.validation;
|
2
|
|
3
|
import javax.annotation.Resource;
|
4
|
|
5
|
import eu.dnetlib.enabling.datasources.common.Api;
|
6
|
import eu.dnetlib.enabling.datasources.common.Datasource;
|
7
|
import eu.dnetlib.enabling.datasources.common.DsmException;
|
8
|
import eu.dnetlib.enabling.datasources.common.LocalDatasourceManager;
|
9
|
import eu.dnetlib.msro.rmi.MSROException;
|
10
|
import eu.dnetlib.msro.rmi.MSRORuntimeException;
|
11
|
import org.apache.commons.lang.StringUtils;
|
12
|
import org.apache.commons.logging.Log;
|
13
|
import org.apache.commons.logging.LogFactory;
|
14
|
|
15
|
import com.googlecode.sarasvati.Engine;
|
16
|
import com.googlecode.sarasvati.NodeToken;
|
17
|
|
18
|
import eu.dnetlib.data.mdstore.MDStoreService;
|
19
|
import eu.dnetlib.data.mdstore.MDStoreServiceException;
|
20
|
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
|
21
|
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
|
22
|
import eu.dnetlib.enabling.tools.blackboard.BlackboardJob;
|
23
|
import eu.dnetlib.miscutils.datetime.DateUtils;
|
24
|
import eu.dnetlib.msro.workflows.nodes.BlackboardJobNode;
|
25
|
import eu.dnetlib.msro.workflows.nodes.ProgressJobNode;
|
26
|
import eu.dnetlib.msro.workflows.nodes.blackboard.BlackboardWorkflowJobListener;
|
27
|
import eu.dnetlib.msro.workflows.util.ProgressProvider;
|
28
|
import eu.dnetlib.msro.workflows.util.WorkflowsConstants;
|
29
|
import org.springframework.beans.factory.annotation.Autowired;
|
30
|
|
31
|
/**
|
32
|
* This node send the BB message to the ValidatorService to validate records from an mdstore.
|
33
|
* <p>
|
34
|
* See ticket https://issue.openaire.research-infrastructures.eu/issues/538 for specs.
|
35
|
* </p>
|
36
|
*
|
37
|
* @author andrea.mannocci, alessia
|
38
|
*
|
39
|
*/
|
40
|
public class ValidatorServiceBlackboardJobNode extends BlackboardJobNode implements ProgressJobNode, ProgressProvider {
|
41
|
|
42
|
public static final Log log = LogFactory.getLog(ValidatorServiceBlackboardJobNode.class); // NOPMD by marko on 11/24/08 5:02 PM
|
43
|
|
44
|
public static final String VALIDATE_ACTION = "VALIDATE";
|
45
|
public static final String VALIDATION_TYPE = "DNET";
|
46
|
|
47
|
public static final String VALIDATOR_EPR_PARAM_NAME = "outputResultSetEpr";
|
48
|
|
49
|
private String xQueryForValidatorServiceId;
|
50
|
private String outputEprName = VALIDATOR_EPR_PARAM_NAME;
|
51
|
|
52
|
// VALIDATOR JOB PARAM VALUES
|
53
|
private String mdstoreId;
|
54
|
private String guidelines;
|
55
|
private String blacklistRuleName;
|
56
|
private String recordsSampleSize;
|
57
|
/** Json key-value map of other parameters that will be stored in the validator back-end as they are. **/
|
58
|
private String extraParams;
|
59
|
|
60
|
/** True to ask the validator to generate the output EPR. **/
|
61
|
private boolean shouldOutputRecords = false;
|
62
|
/** True to ask the validator to update the blacklist. **/
|
63
|
private boolean shouldUpdateBlacklist = true;
|
64
|
|
65
|
@Resource
|
66
|
private UniqueServiceLocator serviceLocator;
|
67
|
|
68
|
@Autowired
|
69
|
private LocalDatasourceManager<Datasource<?, ?>, Api<?>> dsManager;
|
70
|
|
71
|
/** Number of records after which the Validator must update its progress status in currentlyValidatedParam **/
|
72
|
private int jobStatusUpdateInterval = 1000;
|
73
|
/** Parameter where the validator tells how many records have been processed sofar **/
|
74
|
private String currentlyValidatedParam = "recordsTested";
|
75
|
/** Number of records validated so far. **/
|
76
|
private int currentlyValidated = 0;
|
77
|
private int total = 0;
|
78
|
private boolean accurate;
|
79
|
|
80
|
/**
|
81
|
* <p>
|
82
|
* Parameters to set for the VALIDATE BB job:
|
83
|
*
|
84
|
* <pre>
|
85
|
* <PARAMETER name="type" value="DNET"/>
|
86
|
* <PARAMETER name="guidelines" value="openaire3.0"/> //values: openaire3.0, openaire2.0, openaire2.0_data, driver
|
87
|
* <PARAMETER name="records" value="#records"/> //OPTIONAL
|
88
|
* <PARAMETER name="blacklistedRecords" value="true"/> //enables the feature to keep blacklisted records. Default is true.
|
89
|
* <PARAMETER name="blacklistGuidelines" value="blacklist_ruleset"/> //optional. values: name of the ruleset (as created from the validator admin panel) to use to generate the blacklist. Default value is the value in "guidelines".
|
90
|
* <PARAMETER name="datasourceID" value=""/>
|
91
|
* <PARAMETER name="datasourceName" value=""/> //OPTIONAL
|
92
|
* <PARAMETER name="datasourceNamespacePrefix" value=""/> //OPTIONAL
|
93
|
* <PARAMETER name="interfaceId" value=" "/>
|
94
|
* <PARAMETER name="baseUrl" value=""/> //OPTIONAL
|
95
|
* <PARAMETER name="mdstoreId" value=" "/>
|
96
|
* <PARAMETER name="outputEpr" value="boolean"/> //TRUE to get the EPR of validated records. Default is false.
|
97
|
* <PARAMETER name="extraParams" value="json key-value map"/> //OPTIONAL
|
98
|
* <PARAMETER name="submissionDate" value="YYYY-MM-DD HH24:MI:SS"/>
|
99
|
* </pre>
|
100
|
*
|
101
|
* </p>
|
102
|
* {@inheritDoc}
|
103
|
*
|
104
|
* @see eu.dnetlib.msro.workflows.nodes.BlackboardJobNode#prepareJob(eu.dnetlib.enabling.tools.blackboard.BlackboardJob,
|
105
|
* com.googlecode.sarasvati.NodeToken)
|
106
|
*/
|
107
|
@Override
|
108
|
protected void prepareJob(final BlackboardJob job, final NodeToken token) throws Exception {
|
109
|
|
110
|
job.setAction(VALIDATE_ACTION);
|
111
|
job.getParameters().put("type", VALIDATION_TYPE);
|
112
|
job.getParameters().put("guidelines", guidelines);
|
113
|
job.getParameters().put("records", getRecordsSampleSize());
|
114
|
// params for blacklist
|
115
|
job.getParameters().put("blacklistedRecords", isShouldUpdateBlacklist() + "");
|
116
|
if (isShouldUpdateBlacklist()) {
|
117
|
job.getParameters().put("blacklistGuidelines", blacklistRuleName);
|
118
|
}
|
119
|
|
120
|
// datasource params
|
121
|
final String datasourceId = token.getFullEnv().getAttribute(WorkflowsConstants.DATAPROVIDER_ORIGINALID);
|
122
|
final String api = token.getFullEnv().getAttribute(WorkflowsConstants.DATAPROVIDER_INTERFACE);
|
123
|
job.getParameters().put("datasourceId", datasourceId);
|
124
|
job.getParameters().put("datasourceName", token.getFullEnv().getAttribute(WorkflowsConstants.DATAPROVIDER_NAME));
|
125
|
job.getParameters().put("datasourceNamespacePrefix", token.getFullEnv().getAttribute(WorkflowsConstants.DATAPROVIDER_NAMESPACE_PREFIX));
|
126
|
|
127
|
job.getParameters().put("interfaceId", api);
|
128
|
job.getParameters().put("baseUrl", token.getFullEnv().getAttribute(WorkflowsConstants.DATAPROVIDER_INTERFACE_BASEURL));
|
129
|
job.getParameters().put("mdstoreId", mdstoreId);
|
130
|
|
131
|
// output params
|
132
|
job.getParameters().put("outputEpr", isShouldOutputRecords() + "");
|
133
|
// extra params
|
134
|
if (StringUtils.isNotBlank(extraParams)) {
|
135
|
job.getParameters().put("extraParams", extraParams);
|
136
|
}
|
137
|
|
138
|
// Setting properties for the progress bar
|
139
|
if (StringUtils.isBlank(getRecordsSampleSize()) || getRecordsSampleSize().equalsIgnoreCase("ALL")) {
|
140
|
total = getTotal();
|
141
|
} else {
|
142
|
total = Integer.getInteger(getRecordsSampleSize());
|
143
|
}
|
144
|
if (jobStatusUpdateInterval > 0) {
|
145
|
job.getParameters().put("jobStatusUpdateInterval", "" + jobStatusUpdateInterval);
|
146
|
}
|
147
|
|
148
|
log.debug("*****Launching validation with the following parameters:****");
|
149
|
log.debug("Datasource id: " + datasourceId);
|
150
|
log.debug("API: " + api);
|
151
|
log.debug("Total records: " + total);
|
152
|
log.debug("Number of records to validate: " + getRecordsSampleSize());
|
153
|
log.debug("jobStatusUpdateInterval (set only if > 0, wf default is 1000, validator's default is 100): " + jobStatusUpdateInterval);
|
154
|
|
155
|
job.getParameters().put("submissionDate", DateUtils.now_ISO8601());
|
156
|
}
|
157
|
|
158
|
private int getTotal() {
|
159
|
int size = -1;
|
160
|
accurate = false;
|
161
|
try {
|
162
|
if (StringUtils.isBlank(getRecordsSampleSize()) || getRecordsSampleSize().equalsIgnoreCase("ALL")) {
|
163
|
size = serviceLocator.getService(MDStoreService.class).size(mdstoreId);
|
164
|
accurate = true;
|
165
|
} else {
|
166
|
total = Integer.parseInt(getRecordsSampleSize());
|
167
|
accurate = true;
|
168
|
}
|
169
|
} catch (final MDStoreServiceException e) {
|
170
|
log.warn("Can't get size of mdstore " + mdstoreId + " progress will be INACCURATE");
|
171
|
e.printStackTrace();
|
172
|
} catch (final NumberFormatException e) {
|
173
|
log.warn("The parameter recordSampleSize is invalid: progress will be INACCURATE");
|
174
|
e.printStackTrace();
|
175
|
|
176
|
}
|
177
|
return size;
|
178
|
}
|
179
|
|
180
|
/**
|
181
|
* <p>
|
182
|
* Parameters in the VALIDATE BB job response:
|
183
|
*
|
184
|
* <pre>
|
185
|
* <PARAMETER name="error" value=""/>
|
186
|
* <PARAMETER name="outputResultSetEpr" value=""/> //only if isShouldOutputRecords() == true
|
187
|
* <PARAMETER name="jobId" value=""/>
|
188
|
* </pre>
|
189
|
*
|
190
|
* </p>
|
191
|
*
|
192
|
*
|
193
|
* {@inheritDoc}
|
194
|
*
|
195
|
* @see eu.dnetlib.msro.workflows.nodes.BlackboardJobNode#generateBlackboardListener(com.googlecode.sarasvati.Engine,
|
196
|
* com.googlecode.sarasvati.NodeToken)
|
197
|
*/
|
198
|
@Override
|
199
|
protected BlackboardWorkflowJobListener generateBlackboardListener(final Engine engine, final NodeToken token) {
|
200
|
return new BlackboardWorkflowJobListener(engine, token) {
|
201
|
|
202
|
boolean eprFound = false;
|
203
|
|
204
|
@Override
|
205
|
protected void onDone(final BlackboardJob job) {
|
206
|
|
207
|
log.debug("If you see this log you are not asking to get the validated EPR...");
|
208
|
|
209
|
final String jobId = job.getParameters().get("jobId");
|
210
|
|
211
|
token.getEnv().setAttribute("validatorJobId", jobId);
|
212
|
final String error = job.getParameters().get("error");
|
213
|
if (StringUtils.isNotBlank(error)) {
|
214
|
token.getEnv().setAttribute("validatorError", error);
|
215
|
}
|
216
|
|
217
|
final String dsId = token.getFullEnv().getAttribute(WorkflowsConstants.DATAPROVIDER_ORIGINALID);
|
218
|
final String ifaceId = token.getFullEnv().getAttribute(WorkflowsConstants.DATAPROVIDER_INTERFACE);
|
219
|
|
220
|
try {
|
221
|
dsManager.setLastValidationJob(dsId, ifaceId, jobId);
|
222
|
} catch (final Throwable e) {
|
223
|
final String msg = String.format("Error setting last_validation_job field, BB jobId '%s'", job.getId());
|
224
|
log.error(msg);
|
225
|
job.setError(msg);
|
226
|
super.onFailed(job);
|
227
|
throw new MSRORuntimeException(msg, e);
|
228
|
}
|
229
|
|
230
|
super.onDone(job);
|
231
|
}
|
232
|
|
233
|
@Override
|
234
|
protected void onOngoing(final BlackboardJob job) {
|
235
|
currentlyValidated = Integer.parseInt(job.getParameters().get(currentlyValidatedParam));
|
236
|
if (isShouldOutputRecords() & !eprFound) {
|
237
|
final String epr = job.getParameters().get(VALIDATOR_EPR_PARAM_NAME);
|
238
|
log.fatal(VALIDATOR_EPR_PARAM_NAME + "= " + epr);
|
239
|
token.getEnv().setAttribute(outputEprName, epr);
|
240
|
eprFound = true;
|
241
|
}
|
242
|
super.onOngoing(job);
|
243
|
}
|
244
|
|
245
|
};
|
246
|
|
247
|
}
|
248
|
|
249
|
@Override
|
250
|
protected String obtainServiceId(final NodeToken token) {
|
251
|
try {
|
252
|
return getServiceLocator().getService(ISLookUpService.class).getResourceProfileByQuery(xQueryForValidatorServiceId);
|
253
|
} catch (final Exception e) {
|
254
|
throw new RuntimeException(e);
|
255
|
}
|
256
|
}
|
257
|
|
258
|
@Override
|
259
|
public ProgressProvider getProgressProvider() {
|
260
|
return this;
|
261
|
}
|
262
|
|
263
|
@Override
|
264
|
public int getTotalValue() {
|
265
|
return total;
|
266
|
}
|
267
|
|
268
|
@Override
|
269
|
public int getCurrentValue() {
|
270
|
return currentlyValidated;
|
271
|
}
|
272
|
|
273
|
@Override
|
274
|
public boolean isInaccurate() {
|
275
|
return !accurate;
|
276
|
}
|
277
|
|
278
|
public String getGuidelines() {
|
279
|
return guidelines;
|
280
|
}
|
281
|
|
282
|
public void setGuidelines(final String guidelines) {
|
283
|
this.guidelines = guidelines;
|
284
|
}
|
285
|
|
286
|
public String getRecordsSampleSize() {
|
287
|
return recordsSampleSize;
|
288
|
}
|
289
|
|
290
|
public void setRecordsSampleSize(final String recordsSampleSize) {
|
291
|
this.recordsSampleSize = recordsSampleSize;
|
292
|
}
|
293
|
|
294
|
public String getMdstoreId() {
|
295
|
return mdstoreId;
|
296
|
}
|
297
|
|
298
|
public void setMdstoreId(final String mdstoreId) {
|
299
|
this.mdstoreId = mdstoreId;
|
300
|
}
|
301
|
|
302
|
public String getExtra_param() {
|
303
|
return extraParams;
|
304
|
}
|
305
|
|
306
|
public void setExtra_param(final String extra_param) {
|
307
|
this.extraParams = extra_param;
|
308
|
}
|
309
|
|
310
|
public String getxQueryForValidatorServiceId() {
|
311
|
return xQueryForValidatorServiceId;
|
312
|
}
|
313
|
|
314
|
public void setxQueryForValidatorServiceId(final String xQueryForValidatorServiceId) {
|
315
|
this.xQueryForValidatorServiceId = xQueryForValidatorServiceId;
|
316
|
}
|
317
|
|
318
|
public String getOutputEprName() {
|
319
|
return outputEprName;
|
320
|
}
|
321
|
|
322
|
public void setOutputEprName(final String outputEprName) {
|
323
|
this.outputEprName = outputEprName;
|
324
|
}
|
325
|
|
326
|
public String getExtraParams() {
|
327
|
return extraParams;
|
328
|
}
|
329
|
|
330
|
public void setExtraParams(final String extraParams) {
|
331
|
this.extraParams = extraParams;
|
332
|
}
|
333
|
|
334
|
public String getCurrentlyValidatedParam() {
|
335
|
return currentlyValidatedParam;
|
336
|
}
|
337
|
|
338
|
public void setCurrentlyValidatedParam(final String currentlyValidatedParam) {
|
339
|
this.currentlyValidatedParam = currentlyValidatedParam;
|
340
|
}
|
341
|
|
342
|
public int getJobStatusUpdateInterval() {
|
343
|
return jobStatusUpdateInterval;
|
344
|
}
|
345
|
|
346
|
public void setJobStatusUpdateInterval(final int jobStatusUpdateInterval) {
|
347
|
this.jobStatusUpdateInterval = jobStatusUpdateInterval;
|
348
|
}
|
349
|
|
350
|
public int getCurrentlyValidated() {
|
351
|
return currentlyValidated;
|
352
|
}
|
353
|
|
354
|
public void setCurrentlyValidated(final int currentlyValidated) {
|
355
|
this.currentlyValidated = currentlyValidated;
|
356
|
}
|
357
|
|
358
|
public boolean isShouldOutputRecords() {
|
359
|
return shouldOutputRecords;
|
360
|
}
|
361
|
|
362
|
public void setShouldOutputRecords(final boolean shouldOutputRecords) {
|
363
|
this.shouldOutputRecords = shouldOutputRecords;
|
364
|
}
|
365
|
|
366
|
public boolean isShouldUpdateBlacklist() {
|
367
|
return shouldUpdateBlacklist;
|
368
|
}
|
369
|
|
370
|
public void setShouldUpdateBlacklist(final boolean shouldUpdateBlacklist) {
|
371
|
this.shouldUpdateBlacklist = shouldUpdateBlacklist;
|
372
|
}
|
373
|
|
374
|
public String getBlacklistRuleName() {
|
375
|
return blacklistRuleName;
|
376
|
}
|
377
|
|
378
|
public void setBlacklistRuleName(final String blacklistRuleName) {
|
379
|
this.blacklistRuleName = blacklistRuleName;
|
380
|
}
|
381
|
|
382
|
}
|