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.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
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
76
77
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 }