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);
}
};
}
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());
}
}
}
Aggregations