← Back to team overview

dhis2-devs team mailing list archive

[Branch ~dhis2-devs-core/dhis2/trunk] Rev 2770: Fixed bug, charts are given a meaningful name when saved to local computer

 

------------------------------------------------------------
revno: 2770
committer: Lars Helge Overland <larshelge@xxxxxxxxx>
branch nick: dhis2
timestamp: Thu 2011-01-27 17:15:25 +0100
message:
  Fixed bug, charts are given a meaningful name when saved to local computer
modified:
  dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/result/ChartResult.java
  dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/util/ContextUtils.java
  dhis-2/dhis-web/dhis-web-reporting/src/main/java/org/hisp/dhis/reporting/chart/action/ViewChartAction.java


--
lp:dhis2
https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk

Your team DHIS 2 developers is subscribed to branch lp:dhis2.
To unsubscribe from this branch go to https://code.launchpad.net/~dhis2-devs-core/dhis2/trunk/+edit-subscription
=== modified file 'dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/result/ChartResult.java'
--- dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/result/ChartResult.java	2009-12-08 20:36:33 +0000
+++ dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/result/ChartResult.java	2011-01-27 16:15:25 +0000
@@ -14,9 +14,12 @@
  *
  */
 
+import org.apache.commons.lang.StringUtils;
 import org.apache.struts2.ServletActionContext;
 import com.opensymphony.xwork2.ActionInvocation;
 import com.opensymphony.xwork2.Result;
+
+import org.hisp.dhis.util.ContextUtils;
 import org.jfree.chart.ChartUtilities;
 import org.jfree.chart.JFreeChart;
 
@@ -35,7 +38,7 @@
 public class ChartResult
     implements Result
 {
-    private static final String CONTENT_TYPE = "image/png";
+    private static final String DEFAULT_FILENAME = "chart.png";
     
     private JFreeChart chart = null;
 
@@ -43,6 +46,8 @@
 
     private Integer width;
     
+    private String filename;
+    
     /**
      * Sets the JFreeChart to use.
      * 
@@ -74,6 +79,16 @@
     }
     
     /**
+     * Sets the filename.
+     * 
+     * @param filename the filename.
+     */
+    public void setFilename( String filename )
+    {
+        this.filename = filename;
+    }
+
+    /**
      * Executes the result. Writes the given chart as a PNG to the servlet
      * output stream.
      * 
@@ -96,6 +111,10 @@
         
         width = stackWidth != null && stackWidth > 0 ? stackWidth : width;
         
+        String stackFilename = (String) invocation.getStack().findValue( "filename" );
+        
+        filename = StringUtils.defaultIfEmpty( stackFilename, DEFAULT_FILENAME );
+        
         if ( chart == null )
         {
             throw new NullPointerException( "No chart found" );
@@ -112,7 +131,8 @@
         }
         
         HttpServletResponse response = ServletActionContext.getResponse();
-        response.setContentType( CONTENT_TYPE );
+        ContextUtils.configureResponse( response, ContextUtils.CONTENT_TYPE_PNG, true, filename, false );
+        
         OutputStream os = response.getOutputStream();
         ChartUtilities.writeChartAsPNG( os, chart, width, height );
         os.flush();

=== modified file 'dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/util/ContextUtils.java'
--- dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/util/ContextUtils.java	2011-01-11 15:07:13 +0000
+++ dhis-2/dhis-web/dhis-web-commons/src/main/java/org/hisp/dhis/util/ContextUtils.java	2011-01-27 16:15:25 +0000
@@ -49,6 +49,7 @@
     public static final String CONTENT_TYPE_TEXT = "text/plain";
     public static final String CONTENT_TYPE_XML = "application/xml";
     public static final String CONTENT_TYPE_CSV = "application/csv";
+    public static final String CONTENT_TYPE_PNG = "image/png";
     public static final String CONTENT_TYPE_EXCEL = "application/vnd.ms-excel";
     
     private static final String SEPARATOR = "/";

=== modified file 'dhis-2/dhis-web/dhis-web-reporting/src/main/java/org/hisp/dhis/reporting/chart/action/ViewChartAction.java'
--- dhis-2/dhis-web/dhis-web-reporting/src/main/java/org/hisp/dhis/reporting/chart/action/ViewChartAction.java	2010-08-28 10:44:15 +0000
+++ dhis-2/dhis-web/dhis-web-reporting/src/main/java/org/hisp/dhis/reporting/chart/action/ViewChartAction.java	2011-01-27 16:15:25 +0000
@@ -32,6 +32,7 @@
 import org.hisp.dhis.chart.Chart;
 import org.hisp.dhis.chart.ChartService;
 import org.hisp.dhis.i18n.I18nFormat;
+import org.hisp.dhis.system.util.CodecUtils;
 import org.jfree.chart.JFreeChart;
 
 import com.opensymphony.xwork2.Action;
@@ -87,7 +88,14 @@
     {
         return height;
     }
-    
+
+    private String filename;
+
+    public String getFilename()
+    {
+        return filename;
+    }
+
     // -------------------------------------------------------------------------
     // Input
     // -------------------------------------------------------------------------
@@ -113,6 +121,7 @@
             
             width = temp.getWidth();            
             height = temp.getHeight();
+            filename = CodecUtils.filenameEncode( temp.getName() );
             
             log.info( "Viewing chart: " + temp.getTitle() + ", width: " + width + ", height: " + height );
         }