diff gnv-artifacts/src/main/java/de/intevation/gnv/transition/profile/horizontal/HorizontalProfileOutputTransition.java @ 331:1c427acb6c76

Added subtitles to charts. gnv-artifacts/trunk@397 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Ingo Weinzierl <ingo.weinzierl@intevation.de>
date Fri, 04 Dec 2009 15:30:20 +0000
parents 22a6493e8460
children
line wrap: on
line diff
--- a/gnv-artifacts/src/main/java/de/intevation/gnv/transition/profile/horizontal/HorizontalProfileOutputTransition.java	Fri Dec 04 09:04:10 2009 +0000
+++ b/gnv-artifacts/src/main/java/de/intevation/gnv/transition/profile/horizontal/HorizontalProfileOutputTransition.java	Fri Dec 04 15:30:20 2009 +0000
@@ -3,10 +3,17 @@
  */
 package de.intevation.gnv.transition.profile.horizontal;
 
+import com.vividsolutions.jts.geom.Point;
+import com.vividsolutions.jts.io.WKTReader;
+import com.vividsolutions.jts.io.ParseException;
+
 import java.io.IOException;
 import java.io.OutputStream;
 import java.io.UnsupportedEncodingException;
+import java.text.SimpleDateFormat;
 import java.util.Collection;
+import java.util.Date;
+import java.util.Iterator;
 import java.util.Locale;
 
 import org.apache.log4j.Logger;
@@ -24,6 +31,7 @@
 import de.intevation.gnv.exports.ShapeDataCollector;
 import de.intevation.gnv.exports.DefaultProfile;
 import de.intevation.gnv.exports.Export.Profile;
+import de.intevation.gnv.transition.describedata.KeyValueDescibeData;
 
 /**
  * @author Tim Englich <tim.englich@intevation.de>
@@ -39,6 +47,21 @@
     private static Logger log = Logger
     .getLogger(HorizontalProfileOutputTransition.class);
 
+    public static final String DATE_FORMAT = "yyyy.MM.dd HH:mm:ss";
+
+    public static final String [] CHART_TITLE_META = {
+        "CRUISE",
+        "DEPTH",
+        "SHAPE"
+    };
+
+
+    public static final String [] CHART_TITLE_META_RESSOURCES = {
+        "cruiseid",
+        "depth",
+        "coordinate"
+    };
+
     public static final String [] TIMESERIES_CSV_PROFILE_NAMES = {
         "SHAPE",
         "YORDINATE",
@@ -132,4 +155,131 @@
 
         export.create(TIMESERIES_CSV_PROFILE, outputStream, chartResult);
     }
+
+
+    protected String createChartTitle(Locale locale, String uuid) {
+        String fisName = getFisName(locale);
+        log.debug("created title for horizontal profile chart: " + fisName);
+
+        return fisName;
+    }
+
+
+    protected String createChartSubtitle(Locale locale, String uuid) {
+        log.debug("create chart subtitle.");
+        String subtitle = createTimePeriod(locale, uuid);
+
+        // ODV results contain meta information about cruise, station and so on
+        Collection results = getODVResult(uuid);
+        if (results != null) {
+            Iterator iter   = results.iterator();
+            Result   result = iter.hasNext() ? (Result) iter.next() : null;
+
+            subtitle += subtitle.length() != 0 ? "\n" : "";
+            subtitle += createMetaChartSubtitle(locale, result);
+        }
+
+        return subtitle;
+    }
+
+
+    protected String createMetaChartSubtitle(Locale locale, Result result) {
+        log.debug("Fetch meta information and put it into subtitle.");
+        if (result == null)
+            return "";
+
+        StringBuilder meta      = new StringBuilder();
+        WKTReader     wktReader = new WKTReader();
+
+
+        for (int i = 0; i < CHART_TITLE_META.length; i++) {
+            String qry = CHART_TITLE_META[i];
+
+            if (qry.equals("SHAPE")) {
+                try {
+                    Point p = (Point) wktReader.read(result.getString(qry));
+
+                    meta.append(getMessage(locale,"coordinate","coordinate"));
+                    meta.append(": ");
+
+                    log.debug(
+                        "Add " + qry + " to meta information of subtitle: "
+                        + p.toString()
+                    );
+                    meta.append(p.getX() + ", " + p.getY());
+                }
+                catch (ParseException pe) {
+                    log.warn("Error while parsing point.", pe);
+                }
+            }
+            else {
+                log.debug(
+                    "Add " + qry + " to meta information of subtitle: "
+                    + result.getString(qry)
+                );
+                meta.append(getMessage(
+                    locale,
+                    CHART_TITLE_META_RESSOURCES[i],
+                    CHART_TITLE_META_RESSOURCES[i]
+                ));
+                meta.append(": ");
+                meta.append(result.getString(qry));
+            }
+
+            if (i != CHART_TITLE_META.length-1)
+                meta.append("\n");
+        }
+
+        log.debug("Meta title for chart: " + meta.toString());
+        return meta.toString();
+    }
+
+
+    protected String createTimePeriod(Locale locale, String uuid) {
+        log.debug("create time period for chart subtitle.");
+        String subTitle = null;
+        Date startDate  = null;
+        Date endDate    = null;
+
+        Collection          dates  = getDates(uuid);
+        if (dates == null) {
+            log.debug("No time period for subtitle.");
+            return "";
+        }
+
+        SimpleDateFormat    format = new SimpleDateFormat(DATE_FORMAT);
+        KeyValueDescibeData data   = null;
+
+        Iterator iter = dates.iterator();
+        while (iter.hasNext()) {
+            try {
+                data = (KeyValueDescibeData)iter.next();
+
+                if (!data.isSelected())
+                    continue;
+
+                Date current = format.parse(data.getValue());
+                long time    = current.getTime();
+
+                if (startDate == null) {
+                    startDate = current;
+                    endDate   = current;
+                }
+                else if (time < startDate.getTime()) {
+                    startDate = current;
+                }
+                else if (time > endDate.getTime()) {
+                    endDate = current;
+                }
+            }
+            catch (java.text.ParseException pe) {
+                log.warn("Error while parsing date: " + data.getValue(), pe);
+            }
+        }
+
+        subTitle = format.format(startDate) + " - " + format.format(endDate);
+        log.debug("created title for horizontal profile chart: " + subTitle);
+
+        return subTitle;
+    }
 }

http://dive4elements.wald.intevation.org