View Javadoc
1   /*
2    * Copyright (c) 2002-2014, Mairie de Paris
3    * All rights reserved.
4    *
5    * Redistribution and use in source and binary forms, with or without
6    * modification, are permitted provided that the following conditions
7    * are met:
8    *
9    *  1. Redistributions of source code must retain the above copyright notice
10   *     and the following disclaimer.
11   *
12   *  2. Redistributions in binary form must reproduce the above copyright notice
13   *     and the following disclaimer in the documentation and/or other materials
14   *     provided with the distribution.
15   *
16   *  3. Neither the name of 'Mairie de Paris' nor 'Lutece' nor the names of its
17   *     contributors may be used to endorse or promote products derived from
18   *     this software without specific prior written permission.
19   *
20   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
24   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30   * POSSIBILITY OF SUCH DAMAGE.
31   *
32   * License 1.0
33   */
34  package fr.paris.lutece.plugins.form.web;
35  
36  import com.keypoint.PngEncoder;
37  
38  import fr.paris.lutece.plugins.form.business.EntryHome;
39  import fr.paris.lutece.plugins.form.business.GraphType;
40  import fr.paris.lutece.plugins.form.business.GraphTypeHome;
41  import fr.paris.lutece.plugins.form.business.IEntry;
42  import fr.paris.lutece.plugins.form.business.ResponseHome;
43  import fr.paris.lutece.plugins.form.business.StatisticEntrySubmit;
44  import fr.paris.lutece.portal.service.plugin.Plugin;
45  import fr.paris.lutece.portal.service.plugin.PluginService;
46  import fr.paris.lutece.portal.service.util.AppLogService;
47  
48  import org.jfree.chart.ChartRenderingInfo;
49  import org.jfree.chart.JFreeChart;
50  import org.jfree.chart.entity.StandardEntityCollection;
51  
52  import java.awt.image.BufferedImage;
53  
54  import java.util.List;
55  
56  import javax.servlet.http.HttpServletRequest;
57  import javax.servlet.http.HttpServletResponse;
58  
59  
60  /**
61   *
62   *  class DoDownloadGraph
63   *
64   */
65  public class DoDownloadGraph
66  {
67      private static final String PARAMETER_ID_ENTRY = "id_entry";
68      private static final String PARAMETER_ID_GRAPH_TYPE = "id_graph_type";
69      private static final String PARAMETER_GRAPH_THREE_DIMENSION = "graph_three_dimension";
70      private static final String PARAMETER_GRAPH_LABEL_VALUE = "graph_label_value";
71      private static final String PARAMETER_PLUGIN_NAME = "plugin_name";
72      private static final String EMPTY_STRING = "";
73  
74      /**
75       * Write in the http response the statistic graph of a question
76       * @param request the http request
77       * @param response The http response
78       *
79       */
80      public void doGenerateGraph( HttpServletRequest request, HttpServletResponse response )
81      {
82          JFreeChart chart = null;
83          Plugin plugin = null;
84          IEntry entry;
85          GraphType graphType = null;
86          int nIdEntry = -1;
87          int nIdGraphType = -1;
88  
89          String strIdEntry = request.getParameter( PARAMETER_ID_ENTRY );
90          String strIdGraphType = request.getParameter( PARAMETER_ID_GRAPH_TYPE );
91          String strPluginName = request.getParameter( PARAMETER_PLUGIN_NAME );
92          String strGraphThreeDimension = request.getParameter( PARAMETER_GRAPH_THREE_DIMENSION );
93          String strGraphLabelValue = request.getParameter( PARAMETER_GRAPH_LABEL_VALUE );
94  
95          boolean nGraphThreeDimension = false;
96          boolean nGraphLabelValue = false;
97  
98          if ( ( strGraphThreeDimension != null ) && strGraphThreeDimension.equals( "1" ) )
99          {
100             nGraphThreeDimension = true;
101         }
102 
103         if ( ( strGraphLabelValue != null ) && strGraphLabelValue.equals( "1" ) )
104         {
105             nGraphLabelValue = true;
106         }
107 
108         if ( ( strIdEntry != null ) && !strIdEntry.equals( EMPTY_STRING ) && ( strIdGraphType != null ) &&
109                 !strIdGraphType.equals( EMPTY_STRING ) && ( strPluginName != null ) &&
110                 !strPluginName.equals( EMPTY_STRING ) )
111         {
112             plugin = PluginService.getPlugin( strPluginName );
113 
114             try
115             {
116                 nIdEntry = Integer.parseInt( strIdEntry );
117                 nIdGraphType = Integer.parseInt( strIdGraphType );
118             }
119             catch ( NumberFormatException ne )
120             {
121                 AppLogService.error( ne );
122             }
123 
124             entry = EntryHome.findByPrimaryKey( nIdEntry, plugin );
125 
126             List<StatisticEntrySubmit> listStatistic = ResponseHome.getStatisticByIdEntry( nIdEntry, plugin );
127             graphType = GraphTypeHome.findByPrimaryKey( nIdGraphType, plugin );
128 
129             if ( graphType != null )
130             {
131                 chart = graphType.createChart( listStatistic, entry.getTitle(  ), nGraphThreeDimension, nGraphLabelValue );
132             }
133 
134             try
135             {
136                 ChartRenderingInfo info = new ChartRenderingInfo( new StandardEntityCollection(  ) );
137                 BufferedImage chartImage = chart.createBufferedImage( 600, 200, info );
138                 response.setContentType( "image/PNG" );
139 
140                 PngEncoder encoder = new PngEncoder( chartImage, false, 0, 9 );
141                 response.getOutputStream(  ).write( encoder.pngEncode(  ) );
142                 response.getOutputStream(  ).close(  );
143             }
144             catch ( Exception e )
145             {
146                 AppLogService.error( e );
147             }
148         }
149     }
150 }