View Javadoc
1   /*
2    * Copyright (c) 2002-2017, 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.GraphType;
39  import fr.paris.lutece.plugins.form.business.GraphTypeHome;
40  import fr.paris.lutece.plugins.form.service.IResponseService;
41  import fr.paris.lutece.plugins.form.utils.FormUtils;
42  import fr.paris.lutece.plugins.genericattributes.business.Entry;
43  import fr.paris.lutece.plugins.genericattributes.business.EntryHome;
44  import fr.paris.lutece.plugins.genericattributes.business.StatisticEntrySubmit;
45  import fr.paris.lutece.portal.service.plugin.Plugin;
46  import fr.paris.lutece.portal.service.plugin.PluginService;
47  import fr.paris.lutece.portal.service.spring.SpringContextService;
48  import fr.paris.lutece.portal.service.util.AppLogService;
49  import fr.paris.lutece.portal.service.util.AppPathService;
50  
51  import org.apache.commons.collections.CollectionUtils;
52  import org.apache.commons.lang.StringUtils;
53  
54  import org.jfree.chart.ChartRenderingInfo;
55  import org.jfree.chart.JFreeChart;
56  import org.jfree.chart.entity.StandardEntityCollection;
57  
58  import java.awt.image.BufferedImage;
59  
60  import java.io.ByteArrayOutputStream;
61  import java.io.File;
62  
63  import java.util.List;
64  
65  import javax.imageio.ImageIO;
66  
67  import javax.servlet.http.HttpServletRequest;
68  import javax.servlet.http.HttpServletResponse;
69  
70  /**
71   *
72   * class DoDownloadGraph
73   *
74   */
75  public class DoDownloadGraph
76  {
77      private static final String PARAMETER_ID_ENTRY = "id_entry";
78      private static final String PARAMETER_ID_GRAPH_TYPE = "id_graph_type";
79      private static final String PARAMETER_GRAPH_THREE_DIMENSION = "graph_three_dimension";
80      private static final String PARAMETER_GRAPH_LABEL_VALUE = "graph_label_value";
81      private static final String PARAMETER_PLUGIN_NAME = "plugin_name";
82      private static final String EMPTY_STRING = "";
83      private IResponseService _responseService = SpringContextService.getBean( FormUtils.BEAN_FORM_RESPONSE_SERVICE );
84  
85      /**
86       * Write in the http response the statistic graph of a question
87       * 
88       * @param request
89       *            the http request
90       * @param response
91       *            The http response
92       *
93       */
94      public void doGenerateGraph( HttpServletRequest request, HttpServletResponse response )
95      {
96          JFreeChart chart = null;
97          Plugin plugin = null;
98          Entry entry;
99          GraphType graphType = null;
100         int nIdEntry = -1;
101         int nIdGraphType = -1;
102 
103         String strIdEntry = request.getParameter( PARAMETER_ID_ENTRY );
104         String strIdGraphType = request.getParameter( PARAMETER_ID_GRAPH_TYPE );
105         String strPluginName = request.getParameter( PARAMETER_PLUGIN_NAME );
106         String strGraphThreeDimension = request.getParameter( PARAMETER_GRAPH_THREE_DIMENSION );
107         String strGraphLabelValue = request.getParameter( PARAMETER_GRAPH_LABEL_VALUE );
108 
109         boolean nGraphThreeDimension = false;
110         boolean nGraphLabelValue = false;
111 
112         if ( ( strGraphThreeDimension != null ) && strGraphThreeDimension.equals( "1" ) )
113         {
114             nGraphThreeDimension = true;
115         }
116 
117         if ( ( strGraphLabelValue != null ) && strGraphLabelValue.equals( "1" ) )
118         {
119             nGraphLabelValue = true;
120         }
121 
122         if ( ( strIdEntry != null ) && !strIdEntry.equals( EMPTY_STRING ) && ( strIdGraphType != null ) && !strIdGraphType.equals( EMPTY_STRING )
123                 && ( strPluginName != null ) && !strPluginName.equals( EMPTY_STRING ) )
124         {
125             plugin = PluginService.getPlugin( strPluginName );
126 
127             try
128             {
129                 nIdEntry = Integer.parseInt( strIdEntry );
130                 nIdGraphType = Integer.parseInt( strIdGraphType );
131             }
132             catch( NumberFormatException ne )
133             {
134                 AppLogService.error( ne );
135             }
136 
137             entry = EntryHome.findByPrimaryKey( nIdEntry );
138 
139             List<StatisticEntrySubmit> listStatistic = _responseService.getStatisticByIdEntry( nIdEntry );
140             graphType = GraphTypeHome.findByPrimaryKey( nIdGraphType, plugin );
141 
142             if ( graphType != null )
143             {
144                 try
145                 {
146                     if ( isListStatisticValid( listStatistic ) )
147                     {
148                         chart = graphType.createChart( listStatistic, entry.getTitle( ), nGraphThreeDimension, nGraphLabelValue );
149 
150                         ChartRenderingInfo info = new ChartRenderingInfo( new StandardEntityCollection( ) );
151                         BufferedImage chartImage = chart.createBufferedImage( 600, 200, info );
152                         response.setContentType( "image/PNG" );
153 
154                         PngEncoder encoder = new PngEncoder( chartImage, false, 0, 9 );
155                         response.getOutputStream( ).write( encoder.pngEncode( ) );
156                         response.getOutputStream( ).close( );
157                     }
158                     else
159                     {
160                         BufferedImage image = ImageIO.read( new File( AppPathService.getWebAppPath( ) + "/images/none.jpg" ) );
161                         ByteArrayOutputStream baos = new ByteArrayOutputStream( );
162                         ImageIO.write( image, "png", baos );
163 
164                         response.getOutputStream( ).write( baos.toByteArray( ) );
165                         response.getOutputStream( ).close( );
166                     }
167                 }
168                 catch( Exception e )
169                 {
170                     AppLogService.error( e.getMessage( ), e );
171                 }
172             }
173         }
174     }
175 
176     /**
177      * Check if list statistic is valid for display
178      * 
179      * @param listStatistic
180      *            list of entry stats
181      * @return true if list is valid, false otherwise
182      */
183     private boolean isListStatisticValid( List<StatisticEntrySubmit> listStatistic )
184     {
185         if ( CollectionUtils.isEmpty( listStatistic ) )
186         {
187             return false;
188         }
189 
190         for ( StatisticEntrySubmit stat : listStatistic )
191         {
192             if ( StringUtils.isBlank( stat.getFieldLibelle( ) ) )
193             {
194                 return false;
195             }
196         }
197 
198         return true;
199     }
200 }