Search in sources :

Example 1 with Legend

use of au.org.ala.legend.Legend in project spatial-portal by AtlasOfLivingAustralia.

the class BiocacheQuery method getLegend.

/**
 * Get legend for a facet field.
 *
 * @param colourmode
 * @return
 */
@Override
public LegendObject getLegend(String colourmode) {
    if ("-1".equals(colourmode) || StringConstants.GRID.equals(colourmode)) {
        return null;
    }
    LegendObject lo = legends.get(colourmode);
    if (lo != null && lo.getColourMode() != null && !lo.getColourMode().equals(colourmode)) {
        LOGGER.debug("lo not empty and lo=" + lo);
        lo = legends.get(lo.getColourMode());
    }
    if (lo == null && getOccurrenceCount() > 0) {
        HttpClient client = new HttpClient();
        String facetToColourBy = StringConstants.OCCURRENCE_YEAR_DECADE.equals(colourmode) ? StringConstants.OCCURRENCE_YEAR : translateFieldForSolr(colourmode);
        try {
            String url = biocacheServer + LEGEND_SERVICE_CSV + DEFAULT_ROWS + "&q=" + getQ() + "&cm=" + URLEncoder.encode(facetToColourBy, StringConstants.UTF_8) + getQc();
            LOGGER.debug(url);
            GetMethod get = new GetMethod(url);
            // NQ: Set the header type to JSON so that we can parse JSON instead of CSV (CSV has issue with quoted field where a quote is the escape character)
            get.addRequestHeader(StringConstants.ACCEPT, StringConstants.APPLICATION_JSON);
            client.executeMethod(get);
            String s = get.getResponseBodyAsString();
            // in the first line do field name replacement
            String t = translateFieldForSolr(colourmode);
            if (!colourmode.equals(t)) {
                s = s.replaceFirst(t, colourmode);
            }
            lo = new BiocacheLegendObject(colourmode, s);
            // test for exceptions
            if (!colourmode.contains(",") && (StringConstants.UNCERTAINTY.equals(colourmode) || StringConstants.DECADE.equals(colourmode) || StringConstants.OCCURRENCE_YEAR.equals(colourmode) || StringConstants.COORDINATE_UNCERTAINTY.equals(colourmode))) {
                lo = ((BiocacheLegendObject) lo).getAsIntegerLegend();
                // apply cutpoints to colourMode string
                Legend l = lo.getNumericLegend();
                if (l == null || l.getCutoffFloats() == null) {
                    return null;
                }
                float[] cutpoints = l.getCutoffFloats();
                float[] cutpointmins = l.getCutoffMinFloats();
                StringBuilder sb = new StringBuilder();
                // NQ 20140109: use the translated SOLR field as the colour mode so that Constants.DECADE does not cause an issue
                String newFacet = StringConstants.DECADE.equals(colourmode) ? StringConstants.OCCURRENCE_YEAR : colourmode;
                sb.append(newFacet);
                int i = 0;
                while (i < cutpoints.length) {
                    if (i == cutpoints.length - 1 || cutpoints[i] != cutpoints[i + 1]) {
                        if (i > 0) {
                            sb.append(",").append(cutpointmins[i]);
                            if (StringConstants.OCCURRENCE_YEAR.equals(colourmode) || StringConstants.DECADE.equals(colourmode)) {
                                sb.append(StringConstants.DATE_TIME_BEGINNING_OF_YEAR);
                            }
                        } else {
                            sb.append(",*");
                        }
                        sb.append(",").append(cutpoints[i]);
                        if (StringConstants.OCCURRENCE_YEAR.equals(colourmode) || StringConstants.DECADE.equals(colourmode)) {
                            sb.append(StringConstants.DATE_TIME_END_OF_YEAR);
                        }
                    } else if (i < cutpoints.length - 1 && cutpoints[i] == cutpoints[i + 1]) {
                        cutpointmins[i + 1] = cutpointmins[i];
                    }
                    i++;
                }
                String newColourMode = sb.toString();
                if (StringConstants.OCCURRENCE_YEAR.equals(colourmode) || StringConstants.DECADE.equals(colourmode)) {
                    newColourMode = newColourMode.replace(".0", "");
                }
                lo.setColourMode(newColourMode);
                legends.put(colourmode, lo);
                LegendObject newlo = getLegend(newColourMode);
                newlo.setColourMode(newColourMode);
                newlo.setNumericLegend(lo.getNumericLegend());
                legends.put(newColourMode, newlo);
                lo = newlo;
            } else if (StringConstants.MONTH.equals(colourmode)) {
                String newColourMode = "month,00,00,01,01,02,02,03,03,04,04,05,05,06,06,07,07,08,08,09,09,10,10,11,11,12,12";
                lo.setColourMode(newColourMode);
                legends.put(colourmode, lo);
                LegendObject newlo = getLegend(newColourMode);
                newlo.setColourMode(newColourMode);
                newlo.setNumericLegend(lo.getNumericLegend());
                legends.put(newColourMode, newlo);
                lo = newlo;
            } else if (!colourmode.contains(",") && (StringConstants.OCCURRENCE_YEAR_DECADE.equals(colourmode) || StringConstants.DECADE.equals(colourmode))) {
                Set<Integer> decades = new TreeSet<Integer>();
                for (double d : ((BiocacheLegendObject) lo).categoriesNumeric.keySet()) {
                    decades.add((int) (d / 10));
                }
                List<Integer> d = new ArrayList<Integer>(decades);
                StringBuilder sb = new StringBuilder();
                sb.append(StringConstants.OCCURRENCE_YEAR);
                for (int i = !d.isEmpty() && d.get(0) > 0 ? 0 : 1; i < d.size(); i++) {
                    if (i > 0) {
                        sb.append(",").append(d.get(i));
                        sb.append("0-01-01T00:00:00Z");
                    } else {
                        sb.append(",*");
                    }
                    sb.append(",").append(d.get(i));
                    sb.append("9-12-31T00:00:00Z");
                }
                String newColourMode = sb.toString();
                lo.setColourMode(newColourMode);
                legends.put(colourmode, lo);
                LegendObject newlo = getLegend(newColourMode);
                newlo.setColourMode(newColourMode);
                newlo.setNumericLegend(lo.getNumericLegend());
                legends.put(newColourMode, newlo);
                lo = newlo;
            } else {
                legends.put(colourmode, lo);
            }
        } catch (Exception e) {
            LOGGER.error("error getting legend for : " + colourmode, e);
        }
    }
    return lo;
}
Also used : Legend(au.org.ala.legend.Legend) LegendObject(au.org.ala.legend.LegendObject) HttpClient(org.apache.commons.httpclient.HttpClient) GetMethod(org.apache.commons.httpclient.methods.GetMethod)

Aggregations

Legend (au.org.ala.legend.Legend)1 LegendObject (au.org.ala.legend.LegendObject)1 HttpClient (org.apache.commons.httpclient.HttpClient)1 GetMethod (org.apache.commons.httpclient.methods.GetMethod)1