All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.integratedmodelling.engine.authorities.GBIFAuthority Maven / Gradle / Ivy

The newest version!
package org.integratedmodelling.engine.authorities;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

import org.integratedmodelling.api.knowledge.IConcept;
import org.integratedmodelling.api.knowledge.IKnowledge;
import org.integratedmodelling.api.metadata.IMetadata;
import org.integratedmodelling.common.beans.authority.Authority;
import org.integratedmodelling.common.beans.authority.AuthorityConcept;
import org.integratedmodelling.common.beans.authority.AuthorityQueryResponse;
import org.integratedmodelling.common.data.lists.Escape;
import org.integratedmodelling.common.metadata.Metadata;
import org.integratedmodelling.common.vocabulary.NS;
import org.integratedmodelling.common.vocabulary.authority.BaseAuthority;
import org.integratedmodelling.exceptions.KlabValidationException;

import us.monoid.json.JSONArray;
import us.monoid.json.JSONObject;
import us.monoid.web.JSONResource;
import us.monoid.web.Resty;

public class GBIFAuthority extends BaseAuthority {

    public GBIFAuthority(Authority definition) {
        super(definition);
        // TODO Auto-generated constructor stub
    }

    @Override
    public IConcept getBaseIdentityFor(String id, String authorityId) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public List search(String query, String authorityId) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public String validateCoreConcept(IKnowledge knowledge, String id) {
        // TODO Auto-generated method stub
        return null;
    }

    public static GBIFAuthority newInstance() {

        Authority ret = new Authority();

        ret.setName("GBIF");
        ret.setOverallDescription( 
                "Global Biodiversity Information Facility (GBIF)\n\n"
                + "GBIF provides uniform codes for taxonomic entities such as species. This authority provides "
                + "access to the suggest service, which will search for a species matching a string, providing at most 20 matches. "
                + "GBIF matches all taxonomic ranks, but we filter the results to return only species names.\n\nWhen the species "
                + "you are searching for is found, the ID code can be used to construct a Thinklab identity for the species, whose definition can be "
                + "copied to the clipboard by selecting the ID and pressing \"Copy selected\". It can then be pasted in the code after the "
                + "observable you are identifying (e.g. im.ecology:Population). Double-clicking the identifier will "
                + "show more details about the species in this window.\n\n"
                + "For more details, head over to http://gbif.org");
        ret.getAuthorities().add("GBIF.SPECIES");
        ret.getAuthorities().add("GBIF.FAMILY");
        ret.getAuthorities().add("GBIF.ORDER");
        ret.getAuthorities().add("GBIF.CLASS");
        ret.getAuthorityDescriptions().add("");
        ret.getAuthorityDescriptions().add("");
        ret.getAuthorityDescriptions().add("");
        ret.getAuthorityDescriptions().add("");
        ret.setOntologyId("gbif");
        ret.setSearchable(true);
        ret.setWorldview("im");
        ret.setVersion("1.0");
        ret.getInitialConcepts().add("biology:Species,Species");
        ret.getInitialConcepts().add("biology:Family,Family");
        
        return new GBIFAuthority(ret);
    }

    /**
     * TODO:
     * 
     * 1. use specific authority 2. use Java clients from GBIF 3. provide paging
     * 
     * @param query
     * @return
     */
    public List searchGBIF(String query) {

        ArrayList ret = new ArrayList<>();

        try {

            JSONResource res = new Resty().json(getSearchURL(query));
            JSONArray ares = res.array();
            // JSONArray ares = (JSONArray) res.get("results");
            for (int i = 0; i < ares.length(); i++) {
                JSONObject ores = (JSONObject) ares.get(i);
                if (ores != null) {
                    Object rank = ores.get("rank");

                    if (!rankOK(rank))
                        continue;

                    Metadata md = new Metadata();
                    md.put("ID", ores.get("speciesKey"));
                    md.put("Label", ores.get("canonicalName"));
                    ret.add(md);
                }
            }

        } catch (Exception e) {
        }

        return ret;
    }

    private boolean rankOK(Object rank) {
        if (rank == null)
            return false;
        String rnk = rank.toString();

        // TODO add search options to select genera, families etc.

        return rnk.equals("SPECIES");
    }

    private URI getSearchURL(String query) throws URISyntaxException {
        // TODO use search instead, once tested - this only returns 20 matches, but search
        // still limits them
        // and returns duplicates
        return new URI("http://api.gbif.org/v0.9/species/suggest?q=" + Escape.forURL(query));
        // return new URI("http://api.gbif.org/v0.9/species/search?q=" +
        // Escape.forURL(query) +
        // "&rank=SPECIES");
    }

    @Override
    public String getDescription() {
        return "Global Biodiversity Information Facility (GBIF)\n\n"
                + "GBIF provides uniform codes for taxonomic entities such as species. This authority provides "
                + "access to the suggest service, which will search for a species matching a string, providing at most 20 matches. "
                + "GBIF matches all taxonomic ranks, but we filter the results to return only species names.\n\nWhen the species "
                + "you are searching for is found, the ID code can be used to construct a Thinklab identity for the species, whose definition can be "
                + "copied to the clipboard by selecting the ID and pressing \"Copy selected\". It can then be pasted in the code after the "
                + "observable you are identifying (e.g. im.ecology:Population). Double-clicking the identifier will "
                + "show more details about the species in this window.\n\n"
                + "For more details, head over to http://gbif.org";
    }

    @Override
    public boolean canSearch() {
        return true;
    }

    protected void checkObservable(IConcept observable) throws KlabValidationException {
        if (!NS.isThing(observable)) {
            throw new KlabValidationException("only subjects, such as individuals or populations, can be identified as a biological species");
        }
    }

    protected String getTraitPropertyName() {
        return "isSpecies";
    }

    protected String getTraitType() {
        return NS.CORE_IDENTITY_TRAIT;
    }

    @Override
    protected String getBaseTraitName() {
        return "Species";
    }

    @Override
    public AuthorityConcept getConcept(String authority, String id) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public AuthorityQueryResponse queryAuthority(String authority, String query) {
        // TODO Auto-generated method stub
        return null;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy