changeset 9106:ffd4e2b0381c

Added metadata subtitle to all chart export.
author gernotbelger
date Tue, 29 May 2018 11:38:13 +0200
parents ae3565385e6a
children 41f4bc83aa7a
files artifacts/src/main/java/org/dive4elements/river/exports/ChartExportHelper.java
diffstat 1 files changed, 46 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/artifacts/src/main/java/org/dive4elements/river/exports/ChartExportHelper.java	Tue May 29 11:36:42 2018 +0200
+++ b/artifacts/src/main/java/org/dive4elements/river/exports/ChartExportHelper.java	Tue May 29 11:38:13 2018 +0200
@@ -26,8 +26,10 @@
 import java.io.OutputStream;
 import java.io.OutputStreamWriter;
 import java.io.UnsupportedEncodingException;
-
+import java.text.DateFormat;
 import java.text.NumberFormat;
+import java.util.Date;
+import java.util.Locale;
 import java.util.Map;
 
 import org.jfree.chart.ChartRenderingInfo;
@@ -44,11 +46,13 @@
 import org.jfree.chart.JFreeChart;
 import org.jfree.chart.plot.XYPlot;
 import org.jfree.data.xy.XYDataset;
-
+import org.dive4elements.artifacts.Artifact;
 import org.dive4elements.artifacts.CallContext;
 
 import org.dive4elements.artifacts.common.utils.XMLUtils;
-
+import org.dive4elements.river.FLYS;
+import org.dive4elements.river.artifacts.resources.Resources;
+import org.dive4elements.river.artifacts.sinfo.util.CalculationUtils;
 import org.dive4elements.river.jfree.XYMetaSeriesCollection;
 import org.dive4elements.river.utils.Formatter;
 
@@ -301,40 +305,42 @@
         CallContext context)
     {
         log.debug("export chart as CSV");
-        CSVWriter writer = null;
+        
         try {
-            writer = new CSVWriter(
+// FIXME: decide: to BOM or not to BOM            
+//            final byte[] BOM = new byte[] {(byte)0xEF, (byte)0xBB, (byte)0xBF};
+//            out.write(BOM);
+            
+            final CSVWriter writer = new CSVWriter(
                 new OutputStreamWriter(
                     out,
                     DEFAULT_ENCODING),
                 DEFAULT_CSV_SEPARATOR, '"', '\\', "\r\n");
+    
+            NumberFormat format = Formatter.getCSVFormatter(context);
+    
+            XYPlot plot = chart.getXYPlot();
+            int count = plot.getDatasetCount();
+            for (int i = 0; i < count; i++) {
+                XYDataset data = plot.getDataset(i);
+                int scount = data.getSeriesCount();
+                for (int j = 0; j < scount; j++) {
+                    Comparable seriesKey = data.getSeriesKey(j);
+                    log.debug("series key: " + seriesKey.toString());
+                    Map<String, String> metaData = null;
+                    if (data instanceof XYMetaSeriesCollection) {
+                        metaData = ((XYMetaSeriesCollection) data).getMetaData();
+                    }
+                    writeCSVHeader(writer, seriesKey.toString(), metaData);
+                    writeCSVData(writer, data, format);
+                }
+            }
+            writer.close();
         }
         catch(UnsupportedEncodingException uee) {
             log.warn("Wrong encoding for CSV export.");
             return;
         }
-
-        NumberFormat format = Formatter.getCSVFormatter(context);
-
-        XYPlot plot = chart.getXYPlot();
-        int count = plot.getDatasetCount();
-        for (int i = 0; i < count; i++) {
-            XYDataset data = plot.getDataset(i);
-            int scount = data.getSeriesCount();
-            for (int j = 0; j < scount; j++) {
-                Comparable seriesKey = data.getSeriesKey(j);
-                log.debug("series key: " + seriesKey.toString());
-                Map<String, String> metaData = null;
-                if (data instanceof XYMetaSeriesCollection) {
-                    metaData = ((XYMetaSeriesCollection) data).getMetaData();
-                }
-                writeCSVHeader(writer, seriesKey.toString(), metaData);
-                writeCSVData(writer, data, format);
-            }
-        }
-        try {
-            writer.close();
-        }
         catch(IOException ioe) {
             log.error("Writing CSV export failed!");
         }
@@ -445,7 +451,7 @@
      * parameters. The first value is the x point, the second value is the y
      * point.
      */
-    public static double[] getCenteredAnchor(
+    private static double[] getCenteredAnchor(
         double mLeft,      double mRight,      double mBottom, double mTop,
         double width,      double height,
         double chartWidth, double chartHeight
@@ -480,5 +486,17 @@
 
         return origin;
     }
+
+
+    public static String createMetadataSubtitle(Artifact artifact, final CallContext context, final String riverName) {
+        
+        final String version = FLYS.VERSION;
+        final String user = CalculationUtils.findArtifactUser(context, artifact);
+        final Locale locale = Resources.getLocale(context.getMeta());
+        final DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT, locale);
+        final String dateText = df.format(new Date());
+        
+        return Resources.getMsg(context.getMeta(), "chart.subtitle.metadata", "default", version, user, dateText, riverName);
+    }
 }
 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf-8 :

http://dive4elements.wald.intevation.org