ingo@617: package de.intevation.gnv.histogram; ingo@617: ingo@617: import de.intevation.gnv.geobackend.base.Result; ingo@617: import de.intevation.gnv.geobackend.base.ResultDescriptor; ingo@617: ingo@617: import de.intevation.gnv.state.describedata.KeyValueDescibeData; ingo@617: ingo@617: import java.util.ArrayList; ingo@617: import java.util.Collection; ingo@617: import java.util.Iterator; ingo@617: import java.util.List; ingo@617: ingo@617: import org.apache.log4j.Logger; ingo@617: ingo@617: ingo@617: /** ingo@617: * @author Ingo Weinzierl (ingo.weinzierl@intevation.de) ingo@617: */ ingo@617: public class HistogramHelper { ingo@617: ingo@617: private static Logger logger = Logger.getLogger(HistogramHelper.class); ingo@617: ingo@617: ingo@617: private HistogramHelper() { ingo@617: } ingo@617: ingo@617: ingo@617: /* ingo@617: public static Object[][] prepareHistogramData( ingo@617: Collection input, ingo@617: Collection parameters, ingo@617: Collection measurements, ingo@617: Collection dates ingo@617: ) { ingo@617: List values = new ArrayList(); ingo@617: ingo@617: ResultDescriptor rd; ingo@617: int b1Idx, b2Idx, b3Idx, yIdx; ingo@617: ingo@617: Iterator it = parameters.iterator(); ingo@617: KeyValueDescibeData param = (KeyValueDescibeData) it.hasNext(); ingo@617: int parameter = param.getValue(); ingo@617: ingo@617: Iterator iter = input.iterator(); ingo@617: while (iter.hasNext()) { ingo@617: Result row = (Result) iter.next(); ingo@617: ingo@617: if (rd == null) { ingo@617: rd = row.getResultDescriptor(); ingo@617: b1Idx = rd.getColumnIndex("GROUP1"); ingo@617: b2Idx = rd.getColumnIndex("GROUP2"); ingo@617: b3Idx = rd.getColumnIndex("GROUP3"); ingo@617: } ingo@617: ingo@617: if (row.getInteger(b1Idx) == parameter ingo@617: && row.getInteger(b2Idx) == measurement) ingo@617: { ingo@617: ingo@617: } ingo@617: } ingo@617: ingo@617: return null; ingo@617: } ingo@617: */ ingo@617: ingo@617: public static Object[][] prepareHistogramData( ingo@617: Collection input, ingo@617: Collection parameters, ingo@617: Collection measurements, ingo@617: Collection dates ingo@617: ) { ingo@617: List names = new ArrayList(); ingo@617: List data = new ArrayList(); ingo@617: ingo@617: if (logger.isDebugEnabled()) { ingo@617: logger.debug("############ prepare histogram data ##########"); ingo@617: logger.debug("Input data size: " + input.size()); ingo@617: } ingo@617: ingo@617: if (input == null) { ingo@617: return new Object[0][0]; ingo@617: } ingo@617: ingo@617: String break1, break2, break3; ingo@617: int b1Idx = -1; ingo@617: int b2Idx = -1; ingo@617: int b3Idx = -1; ingo@617: int yIdx = -1; ingo@617: try { ingo@617: Iterator iter = input.iterator(); ingo@617: ingo@617: if (iter.hasNext()) { ingo@617: Result row = (Result) iter.next(); ingo@617: Result previousRow = row; ingo@617: ingo@617: if (b1Idx == -1) { ingo@617: ResultDescriptor rd = row.getResultDescriptor(); ingo@617: b1Idx = rd.getColumnIndex("GROUP1"); ingo@617: b2Idx = rd.getColumnIndex("GROUP2"); ingo@617: b3Idx = rd.getColumnIndex("GROUP3"); ingo@617: yIdx = rd.getColumnIndex("YORDINATE"); ingo@617: ingo@617: if (b1Idx == -1 || b2Idx == -1 || b3Idx == -1 || yIdx == -1) { ingo@617: return new Object[0][0]; ingo@617: } ingo@617: } ingo@617: break1 = row.getString(b1Idx); ingo@617: break2 = row.getString(b2Idx); ingo@617: break3 = row.getString(b3Idx); ingo@617: ingo@617: List values = new ArrayList(); ingo@617: while (iter.hasNext()) { ingo@617: ingo@617: // found new series ingo@617: if (!break1.equals(row.getString(b1Idx)) ingo@617: || !break2.equals(row.getString(b2Idx)) ingo@617: || !break3.equals(row.getString(b3Idx)) ingo@617: ) { ingo@617: ingo@617: // get parameter name ingo@617: String name = generateName( ingo@617: break1, break2, break3, ingo@617: parameters, measurements, dates ingo@617: ); ingo@617: ingo@617: // add values and parameter name ingo@617: data.add((Double[]) values.toArray(new Double[values.size()])); ingo@617: names.add(name); ingo@617: ingo@617: if (logger.isDebugEnabled()) { ingo@617: logger.debug(" --- series name: " + name); ingo@617: logger.debug(" --- series items: " + values.size()); ingo@617: } ingo@617: ingo@617: values.clear(); ingo@617: ingo@617: Double yValue = row.getDouble(yIdx); ingo@617: if (yValue != null) ingo@617: values.add(yValue); ingo@617: ingo@617: // set new conditions to find new series ingo@617: break1 = row.getString(b1Idx); ingo@617: break2 = row.getString(b2Idx); ingo@617: break3 = row.getString(b3Idx); ingo@617: ingo@617: previousRow = row; ingo@617: row = (Result) iter.next(); ingo@617: } else { ingo@617: ingo@617: Double value = row.getDouble(yIdx); ingo@617: if (value != null) ingo@617: values.add(value); ingo@617: ingo@617: row = (Result) iter.next(); ingo@617: } ingo@617: } ingo@617: ingo@617: Double yValue = row.getDouble(yIdx); ingo@617: if (yValue != null) ingo@617: values.add(yValue); ingo@617: ingo@617: String name = generateName( ingo@617: break1, break2, break3, parameters, measurements, dates); ingo@617: ingo@617: if (logger.isDebugEnabled()) { ingo@617: logger.debug(" --- series name: " + name); ingo@617: logger.debug(" --- series items: " + values.size()); ingo@617: } ingo@617: ingo@617: data.add((Double[]) values.toArray(new Double[values.size()])); ingo@617: names.add(name); ingo@617: } ingo@617: } ingo@617: catch (Exception e) { ingo@617: logger.error(e.getMessage(), e); ingo@617: } ingo@617: ingo@617: int series = data.size(); ingo@617: logger.debug(" === Found total: " + series); ingo@617: Object[][] obj = new Object[series][2]; ingo@617: for (int i = 0; i < series; i++) { ingo@617: obj[i][0] = names.get(i); ingo@617: obj[i][1] = (Double[]) data.get(i); ingo@617: } ingo@617: ingo@617: return obj; ingo@617: } ingo@617: ingo@617: ingo@617: protected static String generateName( ingo@617: String break1, String break2, String break3, ingo@617: Collection parameters, Collection measurements, Collection dates) ingo@617: { ingo@617: return findValueTitle(parameters,break1) + " " + ingo@617: findValueTitle(measurements,break2) + "m"; ingo@617: } ingo@617: ingo@617: ingo@617: protected static String findValueTitle(Collection values, String id) { ingo@617: if (values != null) { ingo@617: Iterator it = values.iterator(); ingo@617: ingo@617: while (it.hasNext()) { ingo@617: KeyValueDescibeData data = (KeyValueDescibeData) it.next(); ingo@617: ingo@617: if (id.equals(data.getKey())) { ingo@617: return data.getValue(); ingo@617: } ingo@617: } ingo@617: } ingo@617: return ""; ingo@617: } ingo@617: } ingo@617: // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :