view gnv-artifacts/src/main/java/de/intevation/gnv/histogram/HistogramHelper.java @ 733:18dc6a99f4f4

Fixed NPE will using StateBase without a given Query. gnv-artifacts/trunk@771 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Tim Englich <tim.englich@intevation.de>
date Mon, 15 Mar 2010 12:34:40 +0000
parents 3d13fa281a7e
children d70cac2bafc0
line wrap: on
line source
package de.intevation.gnv.histogram;

import de.intevation.gnv.geobackend.base.Result;
import de.intevation.gnv.geobackend.base.ResultDescriptor;

import de.intevation.gnv.state.describedata.KeyValueDescibeData;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.apache.log4j.Logger;


/**
 * @author Ingo Weinzierl (ingo.weinzierl@intevation.de)
 */
public class HistogramHelper {

    private static Logger logger = Logger.getLogger(HistogramHelper.class);


    private HistogramHelper() {
    }


/*
    public static Object[][] prepareHistogramData(
        Collection input,
        Collection parameters,
        Collection measurements,
        Collection dates
    ) {
        List<Double> values = new ArrayList<Double>();

        ResultDescriptor rd;
        int b1Idx, b2Idx, b3Idx, yIdx;

        Iterator it = parameters.iterator();
        KeyValueDescibeData param = (KeyValueDescibeData) it.hasNext();
        int parameter = param.getValue();

        Iterator iter = input.iterator();
        while (iter.hasNext()) {
            Result row = (Result) iter.next();

            if (rd == null) {
                rd    = row.getResultDescriptor();
                b1Idx = rd.getColumnIndex("GROUP1");
                b2Idx = rd.getColumnIndex("GROUP2");
                b3Idx = rd.getColumnIndex("GROUP3");
            }

            if (row.getInteger(b1Idx) == parameter 
            &&  row.getInteger(b2Idx) == measurement)
            {
                
            }
        }

        return null;
    }
    */

    public static Object[][] prepareHistogramData(
        Collection input,
        Collection parameters,
        Collection measurements,
        Collection dates
    ) {
        List names = new ArrayList<String>();
        List data  = new ArrayList<Double[]>();

        if (logger.isDebugEnabled()) {
            logger.debug("############ prepare histogram data ##########");
            logger.debug("Input data size: " + input.size());
        }

        if (input == null) {
            return new Object[0][0];
        }

        String break1, break2, break3;
        int b1Idx = -1;
        int b2Idx = -1;
        int b3Idx = -1;
        int yIdx  = -1;
        try {
            Iterator iter = input.iterator();

            if (iter.hasNext()) {
                Result row = (Result) iter.next();
                Result previousRow = row;

                if (b1Idx == -1) {
                    ResultDescriptor rd = row.getResultDescriptor();
                    b1Idx = rd.getColumnIndex("GROUP1");
                    b2Idx = rd.getColumnIndex("GROUP2");
                    b3Idx = rd.getColumnIndex("GROUP3");
                    yIdx  = rd.getColumnIndex("YORDINATE");

                    if (b1Idx == -1 || b2Idx == -1 || b3Idx == -1 || yIdx == -1) {
                        return new Object[0][0];
                    }
                }
                break1 = row.getString(b1Idx);
                break2 = row.getString(b2Idx);
                break3 = row.getString(b3Idx);

                List values = new ArrayList<Double>();
                while (iter.hasNext()) {

                    // found new series
                    if (!break1.equals(row.getString(b1Idx))
                        || !break2.equals(row.getString(b2Idx))
                        || !break3.equals(row.getString(b3Idx))
                        ) {

                        // get parameter name
                        String name = generateName(
                            break1, break2, break3,
                            parameters, measurements, dates
                        );

                        // add values and parameter name
                        data.add((Double[]) values.toArray(new Double[values.size()]));
                        names.add(name);

                        if (logger.isDebugEnabled()) {
                            logger.debug(" --- series name: " + name);
                            logger.debug(" --- series items: " + values.size());
                        }

                        values.clear();

                        Double yValue = row.getDouble(yIdx);
                        if (yValue != null)
                            values.add(yValue);

                        // set new conditions to find new series
                        break1 = row.getString(b1Idx);
                        break2 = row.getString(b2Idx);
                        break3 = row.getString(b3Idx);

                        previousRow = row;
                        row         = (Result) iter.next();
                    } else {

                        Double value = row.getDouble(yIdx);
                        if (value != null)
                            values.add(value);

                        row = (Result) iter.next();
                    }
                }

                Double yValue = row.getDouble(yIdx);
                if (yValue != null)
                    values.add(yValue);

                String name = generateName(
                    break1, break2, break3, parameters, measurements, dates);

                if (logger.isDebugEnabled()) {
                    logger.debug(" --- series name: " + name);
                    logger.debug(" --- series items: " + values.size());
                }

                data.add((Double[]) values.toArray(new Double[values.size()]));
                names.add(name);
            }
        }
        catch (Exception e) {
            logger.error(e.getMessage(), e);
        }

        int series = data.size();
        logger.debug(" === Found total: " + series);
        Object[][] obj = new Object[series][2];
        for (int i = 0; i < series; i++) {
            obj[i][0] = names.get(i);
            obj[i][1] = (Double[]) data.get(i);
        }

        return obj;
    }


    protected static String generateName(
        String break1, String break2, String break3,
        Collection parameters, Collection measurements, Collection dates)
    {
        return findValueTitle(parameters,break1) + " " +
               findValueTitle(measurements,break2) + "m";
    }


    protected static String findValueTitle(Collection values, String id) {
        if (values != null) {
            Iterator it = values.iterator();

            while (it.hasNext()) {
                KeyValueDescibeData data = (KeyValueDescibeData) it.next();

                if (id.equals(data.getKey())) {
                    return data.getValue();
                }
            }
        }
        return "";
    }
}
// vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org