1
|
package eu.dnetlib.msro.workflows.nodes.is;
|
2
|
|
3
|
import java.io.StringReader;
|
4
|
import java.util.List;
|
5
|
import javax.xml.XMLConstants;
|
6
|
import javax.xml.transform.stream.StreamSource;
|
7
|
import javax.xml.validation.Schema;
|
8
|
import javax.xml.validation.SchemaFactory;
|
9
|
import javax.xml.validation.Validator;
|
10
|
|
11
|
import eu.dnetlib.enabling.locators.UniqueServiceLocator;
|
12
|
import eu.dnetlib.msro.workflows.graph.Arc;
|
13
|
import eu.dnetlib.msro.workflows.nodes.SimpleJobNode;
|
14
|
import eu.dnetlib.msro.workflows.procs.Env;
|
15
|
import eu.dnetlib.msro.workflows.procs.Token;
|
16
|
import eu.dnetlib.msro.workflows.util.ProgressProvider;
|
17
|
import eu.dnetlib.msro.workflows.util.WorkflowsConstants;
|
18
|
import eu.dnetlib.rmi.enabling.ISLookUpService;
|
19
|
import eu.dnetlib.rmi.manager.MSROException;
|
20
|
import org.apache.commons.logging.Log;
|
21
|
import org.apache.commons.logging.LogFactory;
|
22
|
import org.springframework.beans.factory.annotation.Autowired;
|
23
|
|
24
|
public class ValidateProfilesJobNode extends SimpleJobNode {
|
25
|
|
26
|
private static final Log log = LogFactory.getLog(ValidateProfilesJobNode.class);
|
27
|
@Autowired
|
28
|
private UniqueServiceLocator serviceLocator;
|
29
|
private int total = 0;
|
30
|
private int current = 0;
|
31
|
|
32
|
@Override
|
33
|
protected void beforeStart(final Token token) {
|
34
|
token.setProgressProvider(new ProgressProvider() {
|
35
|
|
36
|
@Override
|
37
|
public String getProgressDescription() {
|
38
|
return ValidateProfilesJobNode.this.current + " / " + ValidateProfilesJobNode.this.total;
|
39
|
}
|
40
|
});
|
41
|
}
|
42
|
|
43
|
@Override
|
44
|
protected String execute(final Env env) throws Exception {
|
45
|
|
46
|
final ISLookUpService lookup = this.serviceLocator.getService(ISLookUpService.class);
|
47
|
final SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
|
48
|
|
49
|
int invalidTotals = 0;
|
50
|
|
51
|
final List<String> list = lookup.listResourceTypes();
|
52
|
|
53
|
this.total = list.size();
|
54
|
this.current = 0;
|
55
|
for (final String resourceType : list) {
|
56
|
int valid = 0;
|
57
|
int invalid = 0;
|
58
|
|
59
|
final String schemaSource = lookup.getResourceTypeSchema(resourceType);
|
60
|
final Schema schema = schemaFactory.newSchema(new StreamSource(new StringReader(schemaSource)));
|
61
|
final Validator validator = schema.newValidator();
|
62
|
|
63
|
for (final String profile : lookup.quickSearchProfile("/RESOURCE_PROFILE[./HEADER/RESOURCE_TYPE/@value='" + resourceType + "']")) {
|
64
|
try {
|
65
|
validator.validate(new StreamSource(new StringReader(profile)));
|
66
|
valid++;
|
67
|
} catch (final Exception e) {
|
68
|
invalid++;
|
69
|
}
|
70
|
}
|
71
|
invalidTotals += invalid;
|
72
|
|
73
|
final String message = String.format("Valid: %s, Invalid: %s, Total: %s", valid, invalid, valid + invalid);
|
74
|
env.setAttribute(WorkflowsConstants.MAIN_LOG_PREFIX + resourceType, message);
|
75
|
log.info("Validation of " + resourceType + " profiles: " + message);
|
76
|
}
|
77
|
|
78
|
if (invalidTotals > 0) { throw new MSROException("Validation wf has found " + invalidTotals + " invalid profiles"); }
|
79
|
|
80
|
return Arc.DEFAULT_ARC;
|
81
|
}
|
82
|
|
83
|
}
|