1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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
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
87
88
89
90
91
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
178
179
180
181
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 }