@Test public void parseLocation() throws IOException { String location = "\"1\": \"{\\\"id\\\":1,\\\"parent\\\":207,\\\"children\\\":[],\\\"locationType\\\":{\\\"id\\\":3,\\\"displayName\\\":\\\"Habitat\\\"},\\\"region\\\":{\\\"id\\\":170," + "\\\"displayName\\\":\\\"South America\\\"},\\\"country\\\":{\\\"id\\\":207,\\\"displayName\\\":\\\"Uruguay\\\"},\\\"geoJsonId\\\":232,\\\"interactions\\\":[3001,3" + "002],\\\"habitatType\\\":{\\\"id\\\":1,\\\"displayName\\\":\\\"Forest\\\"},\\\"displayName\\\":\\\"Uruguay-Forest\\\",\\\"serverUpdatedAt\\\":\\\"2016-11-18T01:" + "38:27-06:00\\\"}\""; String someLocation= "\"2\": \"{\\\"id\\\":2,\\\"parent\\\":190,\\\"children\\\":[],\\\"locationType\\\":{\\\"id\\\":5,\\\"displayName\\\":\\\"Point\\\"},\\\"region\\\":{\\\"id\\\":170,\\\"" + "displayName\\\":\\\"South America\\\"},\\\"country\\\":{\\\"id\\\":190,\\\"displayName\\\":\\\"Brazil\\\"},\\\"geoJsonId\\\":254,\\\"interactions\\\":[3003,3004" + "],\\\"habitatType\\\":{\\\"id\\\":2,\\\"displayName\\\":\\\"Savanna\\\"},\\\"displayName\\\":\\\"Embrapa Cerrados\\\",\\\"elevation\\\":879,\\\"latitude\\\":-15.6" + "283332999999995394091456546448171138763427734375,\\\"longitude\\\":-47.37083333000000351376002072356641292572021484375,\\\"serverUpdated" + "At\\\":\\\"2020-04-28T15:44:11-05:00\\\"}\""; String locationChunk = "{ \"location\": {" + location + "," + someLocation + "}}"; InputStream inputStream = IOUtils.toInputStream(locationChunk, StandardCharsets.UTF_8); DatasetImporterForBatBase.Prefixer prefixer = getFooPrefixer(); Map<String, Map<String, String>> locations = DatasetImporterForBatPlant.parseLocations(inputStream, getFooPrefixer()); assertThat(locations.size(), is(2)); Map<String, String> properties = locations.get("1"); assertThat(properties.get(DatasetImporterForTSV.HABITAT_ID), is("foo:habitat:1")); assertThat(properties.get(DatasetImporterForTSV.HABITAT_NAME), is("forest")); assertThat(properties.get(DatasetImporterForTSV.LOCALITY_NAME), is("Uruguay-Forest")); assertThat(properties.get(DatasetImporterForTSV.LOCALITY_ID), is("foo:location:1")); properties = locations.get("2"); assertThat(properties.get(DatasetImporterForTSV.HABITAT_ID), is("foo:habitat:2")); assertThat(properties.get(DatasetImporterForTSV.HABITAT_NAME), is("savanna")); assertThat(properties.get(DatasetImporterForTSV.DECIMAL_LONGITUDE), is("-47.37083333")); assertThat(properties.get(DatasetImporterForTSV.DECIMAL_LATITUDE), is("-15.6283333")); assertThat(properties.get(DatasetImporterForTSV.LOCALITY_NAME), is("Embrapa Cerrados")); assertThat(properties.get(DatasetImporterForTSV.LOCALITY_ID), is("foo:location:2")); }
static Map<String, Map<String, String>> parseLocations(InputStream inputStream, Prefixer prefixer) throws IOException { Map<String, Map<String, String>> locations = new TreeMap<>(); ObjectMapper objectMapper = new ObjectMapper(); JsonNode jsonNode = objectMapper.readTree(inputStream); if (jsonNode.has("location")) { JsonNode sources = jsonNode.get("location"); Iterator<Map.Entry<String, JsonNode>> taxonEntries = sources.getFields(); while (taxonEntries.hasNext()) { Map.Entry<String, JsonNode> next = taxonEntries.next(); JsonNode sourceValue = next.getValue(); if (sourceValue.isTextual()) { JsonNode locationNode = objectMapper.readTree(sourceValue.getTextValue()); String id = JSONUtil.textValueOrNull(locationNode, "id"); locations.put(id, parseLocationNode(locationNode, prefixer)); } } } return locations; }
DatasetImporterForBatBase extends DatasetImporterWithListener { static Map<String, Map<String, String>> parseLocations(InputStream inputStream, Prefixer prefixer) throws IOException { Map<String, Map<String, String>> locations = new TreeMap<>(); ObjectMapper objectMapper = new ObjectMapper(); JsonNode jsonNode = objectMapper.readTree(inputStream); if (jsonNode.has("location")) { JsonNode sources = jsonNode.get("location"); Iterator<Map.Entry<String, JsonNode>> taxonEntries = sources.getFields(); while (taxonEntries.hasNext()) { Map.Entry<String, JsonNode> next = taxonEntries.next(); JsonNode sourceValue = next.getValue(); if (sourceValue.isTextual()) { JsonNode locationNode = objectMapper.readTree(sourceValue.getTextValue()); String id = JSONUtil.textValueOrNull(locationNode, "id"); locations.put(id, parseLocationNode(locationNode, prefixer)); } } } return locations; } }
DatasetImporterForBatBase extends DatasetImporterWithListener { static Map<String, Map<String, String>> parseLocations(InputStream inputStream, Prefixer prefixer) throws IOException { Map<String, Map<String, String>> locations = new TreeMap<>(); ObjectMapper objectMapper = new ObjectMapper(); JsonNode jsonNode = objectMapper.readTree(inputStream); if (jsonNode.has("location")) { JsonNode sources = jsonNode.get("location"); Iterator<Map.Entry<String, JsonNode>> taxonEntries = sources.getFields(); while (taxonEntries.hasNext()) { Map.Entry<String, JsonNode> next = taxonEntries.next(); JsonNode sourceValue = next.getValue(); if (sourceValue.isTextual()) { JsonNode locationNode = objectMapper.readTree(sourceValue.getTextValue()); String id = JSONUtil.textValueOrNull(locationNode, "id"); locations.put(id, parseLocationNode(locationNode, prefixer)); } } } return locations; } DatasetImporterForBatBase(ParserFactory parserFactory, NodeFactory nodeFactory); }
DatasetImporterForBatBase extends DatasetImporterWithListener { static Map<String, Map<String, String>> parseLocations(InputStream inputStream, Prefixer prefixer) throws IOException { Map<String, Map<String, String>> locations = new TreeMap<>(); ObjectMapper objectMapper = new ObjectMapper(); JsonNode jsonNode = objectMapper.readTree(inputStream); if (jsonNode.has("location")) { JsonNode sources = jsonNode.get("location"); Iterator<Map.Entry<String, JsonNode>> taxonEntries = sources.getFields(); while (taxonEntries.hasNext()) { Map.Entry<String, JsonNode> next = taxonEntries.next(); JsonNode sourceValue = next.getValue(); if (sourceValue.isTextual()) { JsonNode locationNode = objectMapper.readTree(sourceValue.getTextValue()); String id = JSONUtil.textValueOrNull(locationNode, "id"); locations.put(id, parseLocationNode(locationNode, prefixer)); } } } return locations; } DatasetImporterForBatBase(ParserFactory parserFactory, NodeFactory nodeFactory); @Override void importStudy(); static void parseInteractions(Map<String, Taxon> taxa,
                                         Map<String, String> sources,
                                         InteractionListener testListener,
                                         InputStream in,
                                         Map<String, Map<String, String>> locations,
                                         Prefixer prefixer); }
DatasetImporterForBatBase extends DatasetImporterWithListener { static Map<String, Map<String, String>> parseLocations(InputStream inputStream, Prefixer prefixer) throws IOException { Map<String, Map<String, String>> locations = new TreeMap<>(); ObjectMapper objectMapper = new ObjectMapper(); JsonNode jsonNode = objectMapper.readTree(inputStream); if (jsonNode.has("location")) { JsonNode sources = jsonNode.get("location"); Iterator<Map.Entry<String, JsonNode>> taxonEntries = sources.getFields(); while (taxonEntries.hasNext()) { Map.Entry<String, JsonNode> next = taxonEntries.next(); JsonNode sourceValue = next.getValue(); if (sourceValue.isTextual()) { JsonNode locationNode = objectMapper.readTree(sourceValue.getTextValue()); String id = JSONUtil.textValueOrNull(locationNode, "id"); locations.put(id, parseLocationNode(locationNode, prefixer)); } } } return locations; } DatasetImporterForBatBase(ParserFactory parserFactory, NodeFactory nodeFactory); @Override void importStudy(); static void parseInteractions(Map<String, Taxon> taxa,
                                         Map<String, String> sources,
                                         InteractionListener testListener,
                                         InputStream in,
                                         Map<String, Map<String, String>> locations,
                                         Prefixer prefixer);  }