Search in sources :

Example 1 with TermLookupServiceException

use of org.eol.globi.service.TermLookupServiceException in project eol-globi-data by jhpoelen.

the class StudyImporterForBlewett method importStudy.

@Override
public void importStudy() throws StudyImporterException {
    String citation = "Blewett DA, Hensley RA, and Stevens PW, Feeding Habits of Common Snook, Centropomus Undecimalis, in Charlotte Harbor, Florida, Gulf and Caribbean Research Vol 18, 1–13, 2006. doi:10.18785/gcr.1801.01 ";
    Study study = nodeFactory.getOrCreateStudy(new StudyImpl("Blewett 2006", StudyImporterForGoMexSI2.GOMEXI_SOURCE_DESCRIPTION, null, citation));
    try {
        Map<String, Location> collectionLocationMap = new HashMap<>();
        Map<String, Date> collectionTimeMap = new HashMap<String, Date>();
        buildLocationTimeMaps(collectionLocationMap, collectionTimeMap, study);
        parsePredatorPreyInteraction(study, collectionLocationMap, collectionTimeMap);
    } catch (IOException e) {
        throw new StudyImporterException("failed to read resource", e);
    } catch (NodeFactoryException e) {
        throw new StudyImporterException("failed to create taxon", e);
    } catch (TermLookupServiceException e) {
        throw new StudyImporterException("failed to map terms", e);
    }
}
Also used : Study(org.eol.globi.domain.Study) TermLookupServiceException(org.eol.globi.service.TermLookupServiceException) HashMap(java.util.HashMap) StudyImpl(org.eol.globi.domain.StudyImpl) IOException(java.io.IOException) Date(java.util.Date) Location(org.eol.globi.domain.Location)

Example 2 with TermLookupServiceException

use of org.eol.globi.service.TermLookupServiceException in project eol-globi-data by jhpoelen.

the class StudyImporterForAkin method addPrey.

private void addPrey(Study study, LabeledCSVParser parser, String[] header, String[] line, Specimen specimen, Location location) throws StudyImporterException {
    int firstPreyIndex = findIndexForColumnWithNameThrowOnMissing("Detritus", header);
    for (int i = firstPreyIndex; i < line.length; i++) {
        String preySpeciesName = header[i];
        if (StringUtils.isNotBlank(preySpeciesName)) {
            String preyVolumeString = line[i];
            try {
                if (StringUtils.isNotBlank(preyVolumeString)) {
                    double volume = Double.parseDouble(preyVolumeString);
                    if (volume > 0) {
                        Specimen prey = nodeFactory.createSpecimen(study, new TaxonImpl(preySpeciesName, null));
                        prey.setLifeStage(parseLifeStage(nodeFactory.getTermLookupService(), preySpeciesName));
                        prey.setVolumeInMilliLiter(volume);
                        prey.caughtIn(location);
                        specimen.ate(prey);
                    }
                }
            } catch (NumberFormatException ex) {
                throw new StudyImporterException("failed to parse volume of prey [" + preySpeciesName + "] in stomach [" + preyVolumeString + "] on line [" + parser.getLastLineNumber() + "]");
            } catch (TermLookupServiceException e) {
                throw new StudyImporterException("failed to parse life stage of prey [" + preySpeciesName + "] in stomach [" + preyVolumeString + "] on line [" + parser.getLastLineNumber() + "]");
            }
        }
    }
}
Also used : Specimen(org.eol.globi.domain.Specimen) TermLookupServiceException(org.eol.globi.service.TermLookupServiceException) TaxonImpl(org.eol.globi.domain.TaxonImpl)

Example 3 with TermLookupServiceException

use of org.eol.globi.service.TermLookupServiceException in project eol-globi-data by jhpoelen.

the class TermLookupServiceImpl method buildMapping.

private void buildMapping(List<URI> uriList) throws TermLookupServiceException {
    mapping = new HashMap<>();
    for (URI uri : uriList) {
        try {
            String response = contentToString(uri);
            CSVParse parser = CSVTSVUtil.createCSVParse(new StringReader(response));
            parser.changeDelimiter(getDelimiter());
            if (hasHeader()) {
                parser = CSVTSVUtil.createLabeledCSVParser(parser);
            }
            String[] line;
            while ((line = parser.getLine()) != null) {
                if (line.length < 4) {
                    LOG.info("line: [" + parser.getLastLineNumber() + "] in [" + uriList + "] contains less than 4 columns");
                } else {
                    String sourceName = line[1];
                    String targetId = line[2];
                    String targetName = line[3];
                    if (StringUtils.isNotBlank(sourceName) && StringUtils.isNotBlank(targetId) && StringUtils.isNotBlank(targetName)) {
                        List<Term> terms = mapping.computeIfAbsent(sourceName, k -> new ArrayList<>());
                        terms.add(new TermImpl(targetId, targetName));
                    }
                }
            }
        } catch (IOException e) {
            throw new TermLookupServiceException("failed to retrieve mapping from [" + uriList + "]", e);
        }
    }
}
Also used : TermLookupServiceException(org.eol.globi.service.TermLookupServiceException) CSVParse(com.Ostermiller.util.CSVParse) StringReader(java.io.StringReader) Term(org.eol.globi.domain.Term) IOException(java.io.IOException) URI(java.net.URI) TermImpl(org.eol.globi.domain.TermImpl)

Example 4 with TermLookupServiceException

use of org.eol.globi.service.TermLookupServiceException in project eol-globi-data by jhpoelen.

the class NodeFactoryNeo4jTest method createAndFindEnvironment.

@Test
public void createAndFindEnvironment() throws NodeFactoryException {
    getNodeFactory().setEnvoLookupService(new TermLookupService() {

        @Override
        public List<Term> lookupTermByName(String name) throws TermLookupServiceException {
            ArrayList<Term> terms = new ArrayList<>();
            terms.add(new TermImpl("NS:" + name, StringUtils.replace(name, " ", "_")));
            return terms;
        }
    });
    Location location = getNodeFactory().getOrCreateLocation(new LocationImpl(0.0, 1.0, 2.0, null));
    List<Environment> first = getNodeFactory().getOrCreateEnvironments(location, "BLA:123", "this and that");
    location = getNodeFactory().getOrCreateLocation(new LocationImpl(0.0, 1.0, 2.0, null));
    List<Environment> second = getNodeFactory().getOrCreateEnvironments(location, "BLA:123", "this and that");
    assertThat(first.size(), is(second.size()));
    assertThat(((NodeBacked) first.get(0)).getNodeID(), is(((NodeBacked) second.get(0)).getNodeID()));
    EnvironmentNode foundEnvironment = getNodeFactory().findEnvironment("this_and_that");
    assertThat(foundEnvironment, is(notNullValue()));
    List<Environment> environments = location.getEnvironments();
    assertThat(environments.size(), is(1));
    Environment environment = environments.get(0);
    NodeBacked environmentNode = (NodeBacked) environment;
    assertThat(environmentNode.getNodeID(), is(foundEnvironment.getNodeID()));
    assertThat(environment.getName(), is("this_and_that"));
    assertThat(environment.getExternalId(), is("NS:this and that"));
    Location anotherLocation = getNodeFactory().getOrCreateLocation(new LocationImpl(48.2, 123.1, null, null));
    LocationNode anotherLocationNode = (LocationNode) anotherLocation;
    assertThat(anotherLocationNode.getEnvironments().size(), is(0));
    anotherLocationNode.addEnvironment((EnvironmentNode) environment);
    assertThat(anotherLocationNode.getEnvironments().size(), is(1));
    // don't add environment that has already been associated
    anotherLocationNode.addEnvironment(environment);
    assertThat(anotherLocationNode.getEnvironments().size(), is(1));
    getNodeFactory().getOrCreateEnvironments(anotherLocation, "BLA:124", "that");
    assertThat(anotherLocationNode.getEnvironments().size(), is(2));
}
Also used : TermLookupServiceException(org.eol.globi.service.TermLookupServiceException) ArrayList(java.util.ArrayList) TermLookupService(org.eol.globi.service.TermLookupService) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Aggregations

TermLookupServiceException (org.eol.globi.service.TermLookupServiceException)4 IOException (java.io.IOException)2 CSVParse (com.Ostermiller.util.CSVParse)1 StringReader (java.io.StringReader)1 URI (java.net.URI)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Location (org.eol.globi.domain.Location)1 Specimen (org.eol.globi.domain.Specimen)1 Study (org.eol.globi.domain.Study)1 StudyImpl (org.eol.globi.domain.StudyImpl)1 TaxonImpl (org.eol.globi.domain.TaxonImpl)1 Term (org.eol.globi.domain.Term)1 TermImpl (org.eol.globi.domain.TermImpl)1 TermLookupService (org.eol.globi.service.TermLookupService)1 Test (org.junit.Test)1