Skip to content

Commit

Permalink
Fix exception handling from schematron validation so that it flags th…
Browse files Browse the repository at this point in the history
…e metadata as invalid if there is an exception (geonetwork#6978)

* Fix exception handling from schematron validation so that it flags the metadata as invalid if there is an exception.
Otherwise, if there is an exception metadata status could be set as valid when it is not because the check failed. It is safer to set the metadata as invalid in this case - once the issue is resolved then re-validation can be performed.

* If validation fails due to exeception then set status to never validated.

* If never validated and required then set status to never validated.
  • Loading branch information
ianwallen authored Oct 13, 2023
1 parent c295956 commit 02a6a77
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
public class AbstractSchematronValidator {

protected void runSchematron(String lang, Path schemaDir, List<MetadataValidation> validations, Element schemaTronXmlOut,
int metadataId, Element md, ApplicableSchematron applicable) {
int metadataId, Element md, ApplicableSchematron applicable) {
final ConfigurableApplicationContext applicationContext = ApplicationContextHolder.get();
ThesaurusManager thesaurusManager = applicationContext.getBean(ThesaurusManager.class);

Expand All @@ -61,6 +61,9 @@ protected void runSchematron(String lang, Path schemaDir, List<MetadataValidatio
report.setAttribute("dbident", String.valueOf(schematron.getId()), Edit.NAMESPACE);
report.setAttribute("required", requirement.toString(), Edit.NAMESPACE);

MetadataValidationStatus metadataValidationStatus = null;
int invalidRules = 0;
int firedRules = 0;
try {
Map<String, Object> params = new HashMap<String, Object>();
params.put("lang", lang);
Expand All @@ -74,28 +77,33 @@ protected void runSchematron(String lang, Path schemaDir, List<MetadataValidatio
// add results to persistent validation information
@SuppressWarnings("unchecked")
Iterator<Element> i = xmlReport.getDescendants(new ElementFilter("fired-rule", Geonet.Namespaces.SVRL));
int firedRules = Iterators.size(i);
firedRules = Iterators.size(i);

i = xmlReport.getDescendants(new ElementFilter("failed-assert", Geonet.Namespaces.SVRL));
int invalidRules = Iterators.size(i);
invalidRules = Iterators.size(i);

if (validations != null) {
validations.add(new MetadataValidation().
setId(new MetadataValidationId(metadataId, ruleId)).
setStatus(invalidRules != 0 ? MetadataValidationStatus.INVALID : MetadataValidationStatus.VALID).
setRequired(requirement == SchematronRequirement.REQUIRED).
setNumTests(firedRules).
setNumFailures(invalidRules));

}
metadataValidationStatus = invalidRules != 0 ? MetadataValidationStatus.INVALID : MetadataValidationStatus.VALID;
}
} catch (Exception e) {
Log.error(Geonet.DATA_MANAGER, "WARNING: schematron xslt " + ruleId + " failed", e);

// If an error occurs that prevents to verify schematron rules, add to show in report
Element errorReport = new Element("schematronVerificationError", Edit.NAMESPACE);
errorReport.addContent("Schematron error ocurred, rules could not be verified: " + e.getMessage());
errorReport.addContent("Schematron error occurred, rules could not be verified: " + e.getMessage());
report.addContent(errorReport);

// As the validation failed due to an exception lets identify the metadata as never validated.
metadataValidationStatus = MetadataValidationStatus.NEVER_CALCULATED;
} finally {
if (metadataValidationStatus != null && validations != null) {
validations.add(new MetadataValidation().
setId(new MetadataValidationId(metadataId, ruleId)).
setStatus(metadataValidationStatus).
setRequired(requirement == SchematronRequirement.REQUIRED).
setNumTests(firedRules).
setNumFailures(invalidRules));

}
}

// -- append report to main XML report.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,11 @@ public void indexMetadata(final String metadataId,

// TODO: Check if ignore INSPIRE validation?
if (!type.equalsIgnoreCase("inspire")) {
if (status == MetadataValidationStatus.INVALID && vi.isRequired()) {
// If never validated and required then set status to never validated.
if (status == MetadataValidationStatus.NEVER_CALCULATED && vi.isRequired()) {
isValid = "-1";
}
if (status == MetadataValidationStatus.INVALID && vi.isRequired() && isValid != "-1") {
isValid = "0";
}
} else {
Expand Down

0 comments on commit 02a6a77

Please sign in to comment.