Search in sources :

Example 1 with CSVPrint

use of com.Ostermiller.util.CSVPrint in project eol-globi-data by jhpoelen.

the class StudyImporterForVertNetTest method parseAssociatedOccurrences2.

@Ignore
@Test
public void parseAssociatedOccurrences2() throws IOException {
    ObjectMapper mapper = new ObjectMapper();
    JsonNode jsonNode1 = mapper.readTree(IOUtils.toString(new GZIPInputStream(getClass().getResourceAsStream("vertnet/response_associated_occurrences.json.gz"))));
    JsonNode recs = jsonNode1.get("recs");
    StringWriter linkOs = new StringWriter();
    CSVPrint linkPrinter = CSVTSVUtil.createCSVPrint(linkOs);
    linkPrinter.print(new String[] { "source", "interaction_type", "target" });
    linkPrinter.setAutoFlush(true);
    StringWriter nodeOs = new StringWriter();
    CSVPrint nodePrinter = CSVTSVUtil.createCSVPrint(nodeOs);
    String[] nodeFields = { "individualid", "decimallongitude", "decimallatitude", "year", "month", "day", "basisofrecord", "scientificname", "dataset_citation" };
    nodePrinter.print(nodeFields);
    nodePrinter.setAutoFlush(true);
    for (JsonNode rec : recs) {
        String specimenId = getOrNull(rec, "individualid");
        String associatedOcc = getOrNull(rec, "associatedoccurrences");
        if (StringUtils.isNotBlank(specimenId) && StringUtils.isNotBlank(associatedOcc)) {
            Map<String, InteractType> associatedoccurrences = parseAssOcc(associatedOcc);
            for (Map.Entry<String, InteractType> entry : associatedoccurrences.entrySet()) {
                linkPrinter.println();
                linkPrinter.print(specimenId);
                linkPrinter.print(entry.getValue().toString());
                linkPrinter.print(entry.getKey());
            }
            nodePrinter.println();
            for (String field : nodeFields) {
                String value = getOrNull(rec, field);
                nodePrinter.print(StringUtils.isBlank(value) ? "" : value);
            }
        }
    }
    linkPrinter.flush();
    linkPrinter.close();
    assertThat(linkOs.toString(), is("source,interaction_type,target" + "\nhttp://arctos.database.museum/guid/CUMV:Amph:16004,EATEN_BY,http://arctos.database.museum/guid/CUMV:Rept:4988" + "\nhttp://arctos.database.museum/guid/DMNS:Bird:34623,ATE,http://arctos.database.museum/guid/DMNS:Mamm:13142" + "\nhttp://arctos.database.museum/guid/DMNS:Bird:34623,ATE,http://arctos.database.museum/guid/DMNS:Mamm:13143" + "\nhttp://arctos.database.museum/guid/DMNS:Bird:34728,ATE,DZTM::Denver:Zoology:Tissue:Mammal:2285" + "\nhttp://arctos.database.museum/guid/DMNS:Bird:34728,ATE,http://arctos.database.museum/guid/DMNS:Mamm:13685"));
    assertThat(nodeOs.toString(), is("individualid,decimallongitude,decimallatitude,year,month,day,basisofrecord,scientificname,dataset_citation" + "\nindividualID,180,-90,2014,10,10,basisOfRecord,scientificName,test citation" + "\nhttp://arctos.database.museum/guid/CUMV:Amph:16004,-76.45442,42.4566,1953,09,27,PreservedSpecimen,Rana sylvatica," + "\nhttp://arctos.database.museum/guid/DMNS:Bird:21686,-106.434158,38.709448,1940,07,27,PreservedSpecimen,Lagopus leucurus,Denver Museum of Nature & Science Bird Collection" + "\nhttp://arctos.database.museum/guid/DMNS:Bird:21688,-106.434158,38.709448,1940,07,27,PreservedSpecimen,Lagopus leucurus,Denver Museum of Nature & Science Bird Collection" + "\nhttp://arctos.database.museum/guid/DMNS:Bird:21689,-106.434158,38.709448,1940,07,27,PreservedSpecimen,Lagopus leucurus,Denver Museum of Nature & Science Bird Collection" + "\nhttp://arctos.database.museum/guid/DMNS:Bird:26722,-105.02222,39.38528,1943,06,24,PreservedSpecimen,Chordeiles minor,Denver Museum of Nature & Science Bird Collection" + "\nhttp://arctos.database.museum/guid/DMNS:Bird:26724,-105.02222,39.38528,1943,06,24,PreservedSpecimen,Chordeiles minor,Denver Museum of Nature & Science Bird Collection" + "\nhttp://arctos.database.museum/guid/DMNS:Bird:34623,-104.738965,39.461082,2012,04,30,PreservedSpecimen,Asio otus,Denver Museum of Nature & Science Bird Collection" + "\nhttp://arctos.database.museum/guid/DMNS:Bird:34728,-105.096498,40.454918,2012,08,10,PreservedSpecimen,Buteo swainsoni,Denver Museum of Nature & Science Bird Collection"));
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) InteractType(org.eol.globi.domain.InteractType) StringWriter(java.io.StringWriter) CSVPrint(com.Ostermiller.util.CSVPrint) JsonNode(org.codehaus.jackson.JsonNode) Map(java.util.Map) TreeMap(java.util.TreeMap) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 2 with CSVPrint

use of com.Ostermiller.util.CSVPrint in project eol-globi-data by jhpoelen.

the class CSVTSVUtilTest method writeQuotes.

@Test
public void writeQuotes() {
    StringWriter writer = new StringWriter();
    CSVPrint csvPrint = CSVTSVUtil.createCSVPrint(writer);
    csvPrint.print("hello \"world\"");
    assertThat(writer.toString(), is("\"hello \"\"world\"\"\""));
}
Also used : StringWriter(java.io.StringWriter) CSVPrint(com.Ostermiller.util.CSVPrint) Test(org.junit.Test)

Aggregations

CSVPrint (com.Ostermiller.util.CSVPrint)2 StringWriter (java.io.StringWriter)2 Test (org.junit.Test)2 Map (java.util.Map)1 TreeMap (java.util.TreeMap)1 GZIPInputStream (java.util.zip.GZIPInputStream)1 JsonNode (org.codehaus.jackson.JsonNode)1 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)1 InteractType (org.eol.globi.domain.InteractType)1 Ignore (org.junit.Ignore)1