Search in sources :

Example 1 with SpeciesListItemDTO

use of au.org.ala.spatial.dto.SpeciesListItemDTO in project spatial-portal by AtlasOfLivingAustralia.

the class CommonData method initSpeciesListAdditionalColumns.

private static Map<String, Map<String, List<String>>> initSpeciesListAdditionalColumns() {
    Map<String, Map<String, List<String>>> map = new HashMap<String, Map<String, List<String>>>();
    String slac = settings.getProperty("species.list.additional.columns", "");
    // append dynamic columns
    slac += dynamicSpeciesListColumns();
    String[] columns = slac.split("\\|");
    for (String line : columns) {
        String[] parts = line.split(",");
        if (parts[0].equals("Conservation")) {
            speciesListThreatened = "species_list_uid:" + StringUtils.join(Arrays.copyOfRange(parts, 1, parts.length), " OR species_list_uid:");
        }
        if (parts[0].equals("Invasive")) {
            speciesListInvasive = "species_list_uid:" + StringUtils.join(Arrays.copyOfRange(parts, 1, parts.length), " OR species_list_uid:");
        }
        if (parts.length > 1) {
            String columnTitle = parts[0];
            for (int i = 1; i < parts.length; i++) {
                try {
                    JSONParser jp = new JSONParser();
                    InputStream is = new URL(CommonData.getSpeciesListServer() + "/ws/speciesList?druid=" + parts[i]).openStream();
                    String listName = ((JSONObject) jp.parse(IOUtils.toString(is))).get("listName").toString();
                    is.close();
                    Map<String, List<String>> m = map.get(columnTitle);
                    if (m == null)
                        m = new HashMap<String, List<String>>();
                    ArrayList<String> sp = new ArrayList<String>();
                    // fetch species list
                    Collection<SpeciesListItemDTO> list = SpeciesListUtil.getListItems(parts[i]);
                    for (SpeciesListItemDTO item : list) {
                        if (item.getLsid() != null && !item.getLsid().isEmpty())
                            sp.add(item.getLsid());
                    }
                    Collections.sort(sp);
                    m.put(listName, sp);
                    map.put(columnTitle, m);
                } catch (Exception e) {
                    LOGGER.error("error reading list: " + parts[i], e);
                }
            }
        }
    }
    return map;
}
Also used : Point(com.vividsolutions.jts.geom.Point) URL(java.net.URL) SpeciesListItemDTO(au.org.ala.spatial.dto.SpeciesListItemDTO) JSONParser(org.json.simple.parser.JSONParser)

Example 2 with SpeciesListItemDTO

use of au.org.ala.spatial.dto.SpeciesListItemDTO in project spatial-portal by AtlasOfLivingAustralia.

the class SpeciesListListbox method extractQueryFromSelectedLists.

/**
 * Returns a query object that represents a query for all species on the selected lists.
 *
 * @param geospatialKosher
 * @return
 */
public BiocacheQuery extractQueryFromSelectedLists(boolean[] geospatialKosher) {
    StringBuilder sb = new StringBuilder();
    List<String> names = new ArrayList<String>();
    int maxOrTerms = Integer.parseInt(CommonData.getSettings().getProperty("solr.maxOrTerms", "200"));
    int count = 0;
    for (String list : selectedLists) {
        // get the speciesListItems
        Collection<SpeciesListItemDTO> items = SpeciesListUtil.getListItems(list);
        for (SpeciesListItemDTO item : items) {
            count = count + 1;
            if (item.getLsid() != null && count <= maxOrTerms) {
                if (sb.length() > 0) {
                    sb.append(",");
                }
                sb.append(item.getLsid());
            } else {
                names.add(item.getName());
            }
        }
    }
    String[] unmatchedNames = !names.isEmpty() ? names.toArray(new String[names.size()]) : null;
    String lsids = sb.length() > 0 ? sb.toString() : null;
    // ignore unmatchedNames
    return new BiocacheQuery(lsids, null, null, null, null, false, geospatialKosher);
}
Also used : SpeciesListItemDTO(au.org.ala.spatial.dto.SpeciesListItemDTO) ArrayList(java.util.ArrayList) BiocacheQuery(au.org.ala.spatial.util.BiocacheQuery)

Example 3 with SpeciesListItemDTO

use of au.org.ala.spatial.dto.SpeciesListItemDTO in project spatial-portal by AtlasOfLivingAustralia.

the class SpeciesListUtil method getListItems.

/**
 * Retrieves the items from the specified listUid
 *
 * @param listUid
 * @return
 */
public static Collection<SpeciesListItemDTO> getListItems(String listUid) {
    String url = CommonData.getSpeciesListServer() + "/ws/speciesListItems/" + listUid;
    HttpClient client = new HttpClient();
    GetMethod get = new GetMethod(url);
    try {
        get.addRequestHeader(StringConstants.CONTENT_TYPE, StringConstants.TEXT_PLAIN);
        int result = client.executeMethod(get);
        if (result == 200) {
            String rawJSON = get.getResponseBodyAsString();
            JSONParser jp = new JSONParser();
            JSONArray ja = (JSONArray) jp.parse(rawJSON);
            List list = new ArrayList<SpeciesListItemDTO>();
            for (int i = 0; i < ja.size(); i++) {
                JSONObject jo = (JSONObject) ja.get(i);
                SpeciesListItemDTO sli = new SpeciesListItemDTO();
                if (jo.containsKey("lsid") && jo.get("lsid") != null) {
                    sli.setLsid(jo.get("lsid").toString());
                }
                if (jo.containsKey("name") && jo.get("name") != null) {
                    sli.setName(jo.get("name").toString());
                }
                list.add(sli);
            }
            return list;
        } else {
            LOGGER.error("Unable to retrieve species list items for " + listUid + ". " + result + " > " + get.getResponseBodyAsString());
        }
    } catch (Exception e) {
        LOGGER.error("Error retrieving list items.", e);
    } finally {
        get.releaseConnection();
    }
    return new ArrayList();
}
Also used : SpeciesListItemDTO(au.org.ala.spatial.dto.SpeciesListItemDTO) JSONObject(org.json.simple.JSONObject) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod) JSONArray(org.json.simple.JSONArray) ArrayList(java.util.ArrayList) JSONParser(org.json.simple.parser.JSONParser) ArrayList(java.util.ArrayList) List(java.util.List)

Aggregations

SpeciesListItemDTO (au.org.ala.spatial.dto.SpeciesListItemDTO)3 ArrayList (java.util.ArrayList)2 JSONParser (org.json.simple.parser.JSONParser)2 BiocacheQuery (au.org.ala.spatial.util.BiocacheQuery)1 Point (com.vividsolutions.jts.geom.Point)1 URL (java.net.URL)1 List (java.util.List)1 HttpClient (org.apache.commons.httpclient.HttpClient)1 GetMethod (org.apache.commons.httpclient.methods.GetMethod)1 JSONArray (org.json.simple.JSONArray)1 JSONObject (org.json.simple.JSONObject)1