/
ExporterOccurrenceAggregates.java
50 lines (40 loc) · 2.24 KB
/
ExporterOccurrenceAggregates.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package org.eol.globi.export;
import org.eol.globi.domain.Study;
import org.eol.globi.domain.TaxonNode;
import org.neo4j.cypher.javacompat.ExecutionEngine;
import org.neo4j.cypher.javacompat.ExecutionResult;
import org.neo4j.graphdb.Node;
import scala.collection.convert.Wrappers;
import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
public class ExporterOccurrenceAggregates extends ExporterOccurrencesBase {
@Override
public void doExportStudy(final Study study, Writer writer, boolean includeHeader) throws IOException {
ExecutionEngine engine = new ExecutionEngine(study.getUnderlyingNode().getGraphDatabase());
ExecutionResult results = executeQueryForDistinctTargetTaxaForPreyByStudy(engine, study.getTitle());
HashMap<String, String> properties = new HashMap<String, String>();
for (Map<String, Object> result : results) {
populateRow(study, writer, result, properties);
}
}
private void populateRow(Study study, Writer writer, Map<String, Object> result, Map<String, String> properties) throws IOException {
TaxonNode sourceTaxon = new TaxonNode((Node) result.get(QUERY_PARAM_SOURCE_TAXON));
String relationshipType = (String) result.get(QUERY_PARAM_INTERACTION_TYPE);
String sourceOccurrenceId = study.getUnderlyingNode().getId() + "-" + sourceTaxon.getNodeID() + "-" + relationshipType;
writeRow(writer, properties, sourceTaxon, "globi:occur:source:" + sourceOccurrenceId);
Wrappers.SeqWrapper<Node> targetTaxa = (Wrappers.SeqWrapper<Node>) result.get(QUERY_PARAM_TARGET_TAXA);
for (Node targetTaxon : targetTaxa) {
TaxonNode taxon = new TaxonNode(targetTaxon);
String targetOccurrenceId = sourceOccurrenceId + "-" + taxon.getNodeID();
writeRow(writer, properties, taxon, "globi:occur:target:" + targetOccurrenceId);
}
}
private void writeRow(Writer writer, Map<String, String> properties, TaxonNode taxon, String idPrefix) throws IOException {
properties.put(EOLDictionary.OCCURRENCE_ID, idPrefix);
properties.put(EOLDictionary.TAXON_ID, taxon.getExternalId());
writeProperties(writer, properties);
properties.clear();
}
}