Search in sources :

Example 1 with InteractionListenerClosable

use of org.eol.globi.process.InteractionListenerClosable in project eol-globi-data by jhpoelen.

the class DatasetImporterForDwCA method createReferenceEnricher.

private static InteractionListenerClosable createReferenceEnricher(Archive archive, final InteractionListener interactionListener) {
    return new InteractionListenerClosable() {

        private BTreeMap<String, Map<String, String>> referenceMap = null;

        @Override
        public void close() {
            if (referenceMap != null) {
                referenceMap.close();
                referenceMap = null;
            }
        }

        private void initIfNeeded() {
            if (referenceMap == null) {
                referenceMap = MapDBUtil.createBigMap();
                ArchiveFile extension = findResourceExtension(archive, EXTENSION_REFERENCE);
                if (extension != null) {
                    for (Record record : extension) {
                        Map<String, String> props = new TreeMap<>();
                        termsToMap(record, props);
                        props.put(REFERENCE_CITATION, CitationUtil.citationFor(props));
                        appendResourceType(props, extension.getRowType());
                        referenceMap.put(record.id(), props);
                    }
                }
            }
        }

        @Override
        public void on(Map<String, String> interaction) throws StudyImporterException {
            initIfNeeded();
            String s = interaction.get(DWC_COREID);
            Map<String, String> enrichedLink = contains(referenceMap, s) ? new TreeMap<String, String>(interaction) {

                {
                    putAll(referenceMap.get(s));
                }
            } : interaction;
            interactionListener.on(enrichedLink);
        }
    };
}
Also used : BTreeMap(org.mapdb.BTreeMap) InteractionListenerClosable(org.eol.globi.process.InteractionListenerClosable) Record(org.gbif.dwc.record.Record) BTreeMap(org.mapdb.BTreeMap) TreeMap(java.util.TreeMap) Map(java.util.Map) HashMap(java.util.HashMap) BTreeMap(org.mapdb.BTreeMap) TreeMap(java.util.TreeMap) ArchiveFile(org.gbif.dwc.ArchiveFile)

Example 2 with InteractionListenerClosable

use of org.eol.globi.process.InteractionListenerClosable in project eol-globi-data by jhpoelen.

the class DatasetImporterForDwCA method importStudy.

@Override
public void importStudy() throws StudyImporterException {
    URI archiveURI = getDataset().getArchiveURI();
    Path tmpDwA = null;
    Thread deleteOnShutdownHook = null;
    try {
        if (getDataset() == null) {
            throw new IllegalArgumentException("no dataset found");
        }
        String archiveURL = getDataset().getOrDefault("url", archiveURI == null ? null : archiveURI.toString());
        getLogger().info(null, "[" + archiveURL + "]: indexing interaction records");
        File dwcaFile = null;
        try {
            URI dwcaURI = URI.create(archiveURL);
            tmpDwA = Files.createTempDirectory("dwca");
            final File tmpDir = tmpDwA.toFile();
            deleteOnShutdownHook = addDeleteOnShutdownHook(tmpDir);
            Archive archive;
            if (CacheUtil.isLocalDir(dwcaURI)) {
                archive = DwCAUtil.archiveFor(dwcaURI, tmpDwA.toString());
            } else {
                dwcaFile = File.createTempFile("dwca", "tmp.zip");
                FileUtils.copyToFile(getDataset().retrieve(dwcaURI), dwcaFile);
                dwcaFile.deleteOnExit();
                archive = DwCAUtil.archiveFor(dwcaFile.toURI(), tmpDwA.toString());
            }
            InteractionListenerWithContext listenerWithContext = new InteractionListenerWithContext();
            try (InteractionListenerClosable referencingListener = createReferenceEnricher(archive, listenerWithContext)) {
                importDescriptionExtension(archive, referencingListener, getLogger());
                importResourceRelationshipExtension(archive, referencingListener);
                importAssociatedTaxaExtension(archive, referencingListener);
                int i = importCore(archive, listenerWithContext);
                getLogger().info(null, "[" + archiveURL + "]: scanned [" + i + "] record(s)");
            }
        } finally {
            removeDeleteOnShutdownHook(deleteOnShutdownHook);
            if (dwcaFile != null && dwcaFile.exists() && dwcaFile.isFile()) {
                FileUtils.deleteQuietly(dwcaFile);
            }
        }
    } catch (IOException | IllegalStateException e) {
        // see https://github.com/globalbioticinteractions/globalbioticinteractions/issues/409
        throw new StudyImporterException("failed to read archive [" + archiveURI + "]", e);
    } finally {
        if (tmpDwA != null) {
            org.apache.commons.io.FileUtils.deleteQuietly(tmpDwA.toFile());
        }
    }
}
Also used : Path(java.nio.file.Path) Archive(org.gbif.dwc.Archive) IOException(java.io.IOException) URI(java.net.URI) InteractionListenerClosable(org.eol.globi.process.InteractionListenerClosable) ArchiveFile(org.gbif.dwc.ArchiveFile) File(java.io.File)

Aggregations

InteractionListenerClosable (org.eol.globi.process.InteractionListenerClosable)2 ArchiveFile (org.gbif.dwc.ArchiveFile)2 File (java.io.File)1 IOException (java.io.IOException)1 URI (java.net.URI)1 Path (java.nio.file.Path)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 Archive (org.gbif.dwc.Archive)1 Record (org.gbif.dwc.record.Record)1 BTreeMap (org.mapdb.BTreeMap)1