Skip to content

Commit

Permalink
#2487 Validate EML
Browse files Browse the repository at this point in the history
  • Loading branch information
mike-podolskiy90 committed Aug 15, 2024
1 parent 0ebb89d commit c5d7a43
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion src/main/java/org/gbif/ipt/task/GenerateDwca.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.gbif.dwc.ArchiveFile;
import org.gbif.dwc.DwcFiles;
import org.gbif.dwc.MetaDescriptorWriter;
import org.gbif.dwc.MetadataException;
import org.gbif.dwc.terms.DwcTerm;
import org.gbif.dwc.terms.Term;
import org.gbif.dwc.terms.TermFactory;
Expand All @@ -34,6 +35,9 @@
import org.gbif.ipt.service.admin.VocabulariesManager;
import org.gbif.ipt.service.manage.SourceManager;
import org.gbif.ipt.utils.MapUtils;
import org.gbif.metadata.eml.EMLProfileVersion;
import org.gbif.metadata.eml.EmlValidator;
import org.gbif.metadata.eml.InvalidEmlException;
import org.gbif.utils.file.ClosableReportingIterator;
import org.gbif.utils.file.CompressionUtil;
import org.gbif.utils.file.csv.CSVReader;
Expand All @@ -47,6 +51,7 @@
import java.io.StringWriter;
import java.io.Writer;
import java.math.BigDecimal;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -75,6 +80,7 @@

import com.google.inject.Inject;
import com.google.inject.assistedinject.Assisted;
import org.xml.sax.SAXException;

public class GenerateDwca extends ReportingTask implements Callable<Map<String, Integer>> {

Expand Down Expand Up @@ -386,6 +392,28 @@ private void validate() throws GeneratorException, InterruptedException {
try {
// retrieve newly generated archive - decompressed
Archive arch = DwcFiles.fromLocation(dwcaFolder.toPath());

// validate EML
try {
addMessage(Level.INFO, "? Validating EML file");
EmlValidator emlValidator = org.gbif.metadata.eml.EmlValidator.newValidator(EMLProfileVersion.GBIF_1_3);
String emlString = arch.getMetadata();
emlValidator.validate(emlString);
addMessage(Level.INFO, "✓ Validated EML file");
} catch (MetadataException | SAXException e) {
// some error validating this file, report
log.error("Exception caught while validating EML file", e);
addMessage(Level.ERROR, "Failed to validate EML file");
setState(e);
throw new GeneratorException("Problem occurred while validating DwC-A (EML)", e);
} catch (InvalidEmlException e) {
// InvalidEmlException
log.error("Invalid EML", e);
addMessage(Level.ERROR, "Invalid EML file: " + e.getMessage());
setState(e);
throw new GeneratorException("Invalid EML", e);
}

// populate basisOfRecord lookup HashMap
loadBasisOfRecordMapFromVocabulary();
// perform validation on core file (includes core ID and basisOfRecord validation)
Expand All @@ -402,7 +430,7 @@ private void validate() throws GeneratorException, InterruptedException {
throw new GeneratorException("Problem occurred while validating DwC-A", e);
}
// final reporting
addMessage(Level.INFO, "Archive validated");
addMessage(Level.INFO, "Archive validated");
}

/**
Expand Down

0 comments on commit c5d7a43

Please sign in to comment.