diff gnv-artifacts/src/main/java/de/intevation/gnv/statistics/TimeseriesStatistics.java @ 253:07650fc6014c

Put a name to each Statistic group which is similar to the name of the Series in the Charts. issue83 gnv-artifacts/trunk@326 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Tim Englich <tim.englich@intevation.de>
date Thu, 12 Nov 2009 10:15:05 +0000
parents 7fb9441dd8af
children 5403452c06fc
line wrap: on
line diff
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/statistics/TimeseriesStatistics.java	Wed Nov 11 15:54:31 2009 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/statistics/TimeseriesStatistics.java	Thu Nov 12 10:15:05 2009 +0000
@@ -26,6 +26,7 @@
 
 import de.intevation.gnv.geobackend.base.Result;
 import de.intevation.gnv.statistics.exception.StatisticsException;
+import de.intevation.gnv.transition.describedata.KeyValueDescibeData;
 
 /**
  * The class <code>TimeseriesStatistics</code> fulfills the following purposes:
@@ -48,22 +49,25 @@
     private static Logger log = Logger.getLogger(TimeseriesStatistics.class);
     private static boolean sDebug = log.isDebugEnabled();
 
-    private Collection<Statistic> statistics = null;
 
     public TimeseriesStatistics() {
         super();
     }
 
     /**
-     * @see de.intevation.gnv.statistics.Statistics#calculateStatistics(java.util.Collection)
+     * @see de.intevation.gnv.statistics.Statistics#calculateStatistics(java.util.Collection, java.util.Collection, java.util.Collection, java.util.Collection)
      */
-    public Collection<Statistic> calculateStatistics(
-                                                     Collection<Result> resultSet)
-                                                                                  throws StatisticsException {
+    public Collection<StatisticSet> calculateStatistics(
+                                                     Collection<Result> resultSet,
+                                                     Collection<KeyValueDescibeData> parameters,
+                                                     Collection<KeyValueDescibeData> measurements,
+                                                     Collection<KeyValueDescibeData> dates
+                                                     )
+                                                     throws StatisticsException {
         DescriptiveStatistics lStatistics = null;
         SimpleRegression lRegression = null;
-        statistics = new ArrayList<Statistic>();
-        String break1, break2;
+        Collection<StatisticSet> statisticSets = new ArrayList<StatisticSet>();
+        String break1, break2, break3;
         int lSeries = 1;
         try {
 
@@ -72,13 +76,21 @@
                 Result row = resultIterator.next();
                 break1 = row.getString("GROUP1");
                 break2 = row.getString("GROUP2");
+                break3 = row.getString("GROUP3");
                 lRegression = new SimpleRegression();
                 lStatistics = DescriptiveStatistics.newInstance();
                 while (resultIterator.hasNext()) {
 
                     if (!break1.equals(row.getString("GROUP1"))
-                        || !break2.equals(row.getString("GROUP2"))) {
-                        addStatisticsValues(lStatistics, lRegression, lSeries);
+                        || !break2.equals(row.getString("GROUP2"))
+                        || !break3.equals(row.getString("GROUP3"))
+                        ) {
+                        String statisticsName = this.generateStatisticsName(break1, break2, 
+                                                                            break3, parameters, 
+                                                                            measurements, dates);
+                        statisticSets.add(this.generateStatisticsValues(lStatistics, 
+                                                                        lRegression,
+                                                                        statisticsName));
 
                         lStatistics.clear();
                         lRegression.clear();
@@ -89,6 +101,7 @@
 
                         break1 = row.getString("GROUP1");
                         break2 = row.getString("GROUP2");
+                        break3 = row.getString("GROUP3");
                         row = resultIterator.next();
                         lSeries++;
                     } else {
@@ -100,7 +113,13 @@
                     }
 
                 }
-                addStatisticsValues(lStatistics, lRegression, lSeries);
+                String statisticsName = this.generateStatisticsName(break1, break2, 
+                                                                    break3, parameters, 
+                                                                    measurements, dates);
+                
+                statisticSets.add(this.generateStatisticsValues(lStatistics, 
+                                                                lRegression, 
+                                                                statisticsName));
                 lStatistics.clear();
                 lRegression.clear();
             }
@@ -108,9 +127,45 @@
             log.error(e.getMessage(), e);
         }
 
-        return statistics;
+        return statisticSets;
     }
 
+    /**
+     * 
+     * @param break1
+     * @param break2
+     * @param break3
+     * @param parameters
+     * @param measurements
+     * @param dates
+     * @return
+     */
+    protected String generateStatisticsName(String break1, 
+                                            String break2,
+                                            String break3,
+                                            Collection<KeyValueDescibeData> parameters,
+                                            Collection<KeyValueDescibeData> measurements,
+                                            Collection<KeyValueDescibeData> dates){
+        log.debug("TimeseriesStatistics.generateStatisticsName");
+        return this.findValueTitle(parameters,break1)+ " "+
+               this.findValueTitle(measurements,break2) + "m";
+    }
+    
+    
+    protected String findValueTitle(Collection<KeyValueDescibeData> values,
+                                  String id) {
+        log.debug("TimeseriesStatistics.findValueTitle "+ id);
+        if (values != null) {
+            Iterator<KeyValueDescibeData> it = values.iterator();
+            while (it.hasNext()) {
+                KeyValueDescibeData data = it.next();
+                if (id.equals(data.getKey())) {
+                    return data.getValue();
+                }
+            }
+        }
+        return "";
+    }
     protected double calculateXOrdinateValue(Result row) throws SQLException {
         return new Double((row.getDate("XORDINATE")).getTime() / 1000 / 3600);
     }
@@ -121,44 +176,46 @@
      * @param lStats
      * @param lSeries
      */
-    private void addStatisticsValues(DescriptiveStatistics lStatistics,
-                                     SimpleRegression lRegression, int lSeries) {
-        statistics.add(new Statistic("gnviewer.statistics.series",
-                lSeries));
-        statistics.add(new Statistic(
+    private StatisticSet generateStatisticsValues(DescriptiveStatistics lStatistics,
+                                     SimpleRegression lRegression, String  statisticName) {
+        
+        StatisticSet statisticSet = new StatisticSet(statisticName);
+        
+        statisticSet.addStatistic(new Statistic(
                 "gnviewer.statistics.descriptive.arithmeticMean", lStatistics
                         .getMean()));
-        statistics.add(new Statistic(
+        statisticSet.addStatistic(new Statistic(
                 "gnviewer.statistics.descriptive.kurtosis", lStatistics
                         .getKurtosis()));
-        statistics.add(new Statistic("gnviewer.statistics.descriptive.max",
+        statisticSet.addStatistic(new Statistic("gnviewer.statistics.descriptive.max",
                 lStatistics.getMax()));
-        statistics.add(new Statistic("gnviewer.statistics.descriptive.min",
+        statisticSet.addStatistic(new Statistic("gnviewer.statistics.descriptive.min",
                 lStatistics.getMin()));
-        statistics.add(new Statistic("gnviewer.statistics.descriptive.n",
+        statisticSet.addStatistic(new Statistic("gnviewer.statistics.descriptive.n",
                 lStatistics.getN()));
-        statistics.add(new Statistic(
+        statisticSet.addStatistic(new Statistic(
                 "gnviewer.statistics.descriptive.percentile.90", lStatistics
                         .getPercentile(90)));
-        statistics.add(new Statistic(
+        statisticSet.addStatistic(new Statistic(
                 "gnviewer.statistics.descriptive.percentile.75", lStatistics
                         .getPercentile(75)));
-        statistics.add(new Statistic(
+        statisticSet.addStatistic(new Statistic(
                 "gnviewer.statistics.descriptive.percentile.50", lStatistics
                         .getPercentile(50)));
-        statistics.add(new Statistic(
+        statisticSet.addStatistic(new Statistic(
                 "gnviewer.statistics.descriptive.percentile.10", lStatistics
                         .getPercentile(10)));
-        statistics.add(new Statistic(
+        statisticSet.addStatistic(new Statistic(
                 "gnviewer.statistics.descriptive.deviation", lStatistics
                         .getStandardDeviation()));
-        statistics.add(new Statistic(
+        statisticSet.addStatistic(new Statistic(
                 "gnviewer.statistics.descriptive.variance", lStatistics
                         .getVariance()));
-        statistics.add(new Statistic(
+        statisticSet.addStatistic(new Statistic(
                 "gnviewer.statistics.descriptive.intercept", lRegression
                         .getIntercept()));
-        statistics.add(new Statistic("gnviewer.statistics.descriptive.slope",
+        statisticSet.addStatistic(new Statistic("gnviewer.statistics.descriptive.slope",
                 lRegression.getSlope()));
+        return statisticSet;
     }
 }

http://dive4elements.wald.intevation.org