Search in sources :

Example 6 with TaxonImage

use of org.eol.globi.domain.TaxonImage in project eol-globi-data by jhpoelen.

the class TaxonUtilTest method toTaxonImageNonEOL.

@Test
public void toTaxonImageNonEOL() {
    TaxonImage image = new TaxonImage();
    Taxon taxon = new TaxonImpl("Donald duckus", "ZZZ:123");
    taxon.setCommonNames("bla @en | boo @de");
    taxon.setPath("one | two | three");
    Map<String, String> taxonMap = new TreeMap<String, String>(TaxonUtil.taxonToMap(taxon));
    taxonMap.put(PropertyAndValueDictionary.THUMBNAIL_URL, "http://foo/bar/thumb");
    taxonMap.put(PropertyAndValueDictionary.EXTERNAL_URL, "http://foo/bar");
    TaxonImage enrichedImage = TaxonUtil.enrichTaxonImageWithTaxon(taxonMap, image);
    assertThat(enrichedImage.getCommonName(), is("bla"));
    assertThat(enrichedImage.getTaxonPath(), is("one | two | three"));
    assertThat(enrichedImage.getInfoURL(), is("http://foo/bar"));
    assertThat(enrichedImage.getThumbnailURL(), is("http://foo/bar/thumb"));
    assertThat(enrichedImage.getPageId(), is(nullValue()));
    assertThat(enrichedImage.getImageURL(), is(nullValue()));
}
Also used : TaxonImage(org.eol.globi.domain.TaxonImage) Taxon(org.eol.globi.domain.Taxon) TaxonImpl(org.eol.globi.domain.TaxonImpl) TreeMap(java.util.TreeMap) Test(org.junit.Test)

Example 7 with TaxonImage

use of org.eol.globi.domain.TaxonImage in project eol-globi-data by jhpoelen.

the class EOLTaxonImageService method lookupImage.

private TaxonImage lookupImage(String externalId) throws PropertyEnricherException {
    TaxonImage image;
    Map<String, String> enrich = new EOLService().enrich(TaxonUtil.taxonToMap(new TaxonImpl(null, externalId)));
    Taxon taxon = TaxonUtil.mapToTaxon(enrich);
    image = taxonToTaxonImage(taxon);
    return image;
}
Also used : TaxonImage(org.eol.globi.domain.TaxonImage) TaxonImpl(org.eol.globi.domain.TaxonImpl) Taxon(org.eol.globi.domain.Taxon) EOLService(org.eol.globi.taxon.EOLService)

Example 8 with TaxonImage

use of org.eol.globi.domain.TaxonImage in project eol-globi-data by jhpoelen.

the class ImageLinker method link.

@Override
public void link() {
    ExecutionEngine engine = new ExecutionEngine(graphDb);
    ExecutionResult executionResult = engine.execute("START taxon = node:taxons('*:*')\n" + "WHERE not(has(taxon.thumbnailUrl)) AND has(taxon.externalId) AND taxon.externalId <> 'no:match'\n" + "RETURN id(taxon) as `id`, taxon.externalId as `externalId`");
    for (Map<String, Object> externalIdMap : executionResult) {
        final String externalId = (String) externalIdMap.get("externalId");
        final Long nodeId = (Long) externalIdMap.get("id");
        TaxonImage taxonImage = null;
        try {
            taxonImage = new EOLTaxonImageService().lookupImageForExternalId(externalId);
        } catch (IOException e) {
            LOG.warn("failed to lookup externalId [" + externalId + "]", e);
        }
        if (taxonImage == null) {
            if (out != null) {
                out.println(StringUtils.join(Arrays.asList("EOL:12345", "", "", ""), "\t"));
            }
        } else {
            final String infoURL = taxonImage.getInfoURL() == null ? "" : taxonImage.getInfoURL();
            final String thumbnailURL = taxonImage.getThumbnailURL() == null ? "" : taxonImage.getThumbnailURL();
            final String imageURL = taxonImage.getImageURL() == null ? "" : taxonImage.getImageURL();
            ExecutionResult execute = engine.execute("START taxon = node({nodeId})\n" + "SET taxon.externalUrl={infoUrl}, taxon.imageUrl={imageUrl}, taxon.thumbnailUrl={thumbnailUrl}\n" + "RETURN taxon.externalId, taxon.externalUrl, taxon.thumbnailUrl, taxon.imageUrl", new HashMap<String, Object>() {

                {
                    put("nodeId", nodeId);
                    put("infoUrl", infoURL);
                    put("imageUrl", imageURL);
                    put("thumbnailUrl", thumbnailURL);
                }
            });
            if (out != null) {
                for (Map<String, Object> stringObjectMap : execute) {
                    out.println(StringUtils.join(CSVTSVUtil.escapeValues(stringObjectMap.values()), "\t"));
                }
            }
            if (execute != null && execute.iterator() != null) {
                execute.iterator().close();
            }
        }
    }
}
Also used : ExecutionEngine(org.neo4j.cypher.javacompat.ExecutionEngine) TaxonImage(org.eol.globi.domain.TaxonImage) ExecutionResult(org.neo4j.cypher.javacompat.ExecutionResult) IOException(java.io.IOException) EOLTaxonImageService(org.eol.globi.service.EOLTaxonImageService)

Example 9 with TaxonImage

use of org.eol.globi.domain.TaxonImage in project eol-globi-data by jhpoelen.

the class EOLTaxonImageService method taxonToTaxonImage.

private static TaxonImage taxonToTaxonImage(Taxon taxon) {
    TaxonImage image = new TaxonImage();
    if (StringUtils.isNotBlank(taxon.getCommonNames())) {
        String[] names = StringUtils.split(taxon.getCommonNames(), CharsetConstant.SEPARATOR_CHAR);
        for (String name : names) {
            String[] nameParts = StringUtils.split(name, CharsetConstant.LANG_SEPARATOR_CHAR);
            if (nameParts.length > 1) {
                String vernacularName = StringUtils.trim(nameParts[0]);
                String language = StringUtils.trim(nameParts[1]);
                if ("en".equals(language)) {
                    String commonName = vernacularName.replaceAll("\\(.*\\)", "");
                    String capitalize = WordUtils.capitalize(commonName);
                    image.setCommonName(capitalize.replaceAll("\\sAnd\\s", " and "));
                    break;
                }
            }
        }
    }
    image.setThumbnailURL(taxon.getThumbnailUrl());
    image.setImageURL(taxon.getThumbnailUrl());
    image.setInfoURL(taxon.getExternalUrl());
    image.setScientificName(taxon.getName());
    image.setTaxonPath(taxon.getPath());
    if (StringUtils.startsWith(taxon.getExternalId(), TaxonomyProvider.ID_PREFIX_EOL)) {
        image.setPageId(StringUtils.replace(taxon.getExternalId(), TaxonomyProvider.ID_PREFIX_EOL, ""));
    }
    return image;
}
Also used : TaxonImage(org.eol.globi.domain.TaxonImage)

Example 10 with TaxonImage

use of org.eol.globi.domain.TaxonImage in project eol-globi-data by jhpoelen.

the class ImageService method findTaxonImagesForTaxonWithName.

@RequestMapping(value = "/imagesForName/{scientificName}", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
@ResponseBody
public TaxonImage findTaxonImagesForTaxonWithName(@PathVariable("scientificName") String scientificName) throws IOException {
    TaxonImage taxonImage = new TaxonImage();
    if (TaxonUtil.isEmptyValue(scientificName)) {
        taxonImage.setScientificName(scientificName);
    } else {
        Map<String, String> taxonWithImage = taxonSearch.findTaxonWithImage(scientificName);
        if (taxonWithImage == null || taxonWithImage.isEmpty()) {
            Map<String, String> taxon = taxonSearch.findTaxon(scientificName, null);
            if (taxon != null) {
                if (taxon.containsKey(PropertyAndValueDictionary.EXTERNAL_ID)) {
                    taxonImage = imageSearch.lookupImageForExternalId(taxon.get(PropertyAndValueDictionary.EXTERNAL_ID));
                    if (taxonImage == null) {
                        taxonImage = new TaxonImage();
                    }
                    TaxonUtil.enrichTaxonImageWithTaxon(taxon, taxonImage);
                }
            }
        } else {
            taxonImage = TaxonUtil.enrichTaxonImageWithTaxon(taxonWithImage, new TaxonImage());
        }
    }
    if (taxonImage == null) {
        throw new ResourceNotFoundException("no image for [" + scientificName + "]");
    }
    return taxonImage;
}
Also used : TaxonImage(org.eol.globi.domain.TaxonImage) RequestMapping(org.springframework.web.bind.annotation.RequestMapping) ResponseBody(org.springframework.web.bind.annotation.ResponseBody)

Aggregations

TaxonImage (org.eol.globi.domain.TaxonImage)12 Test (org.junit.Test)7 IOException (java.io.IOException)4 HashMap (java.util.HashMap)3 Map (java.util.Map)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 Taxon (org.eol.globi.domain.Taxon)3 TaxonImpl (org.eol.globi.domain.TaxonImpl)3 ImageSearch (org.eol.globi.service.ImageSearch)3 TreeMap (java.util.TreeMap)2 EOLTaxonImageService (org.eol.globi.service.EOLTaxonImageService)1 EOLService (org.eol.globi.taxon.EOLService)1 Before (org.junit.Before)1 ExecutionEngine (org.neo4j.cypher.javacompat.ExecutionEngine)1 ExecutionResult (org.neo4j.cypher.javacompat.ExecutionResult)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1