1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.seasar.tuigwaa.plugin.database;
17
18 import java.io.BufferedOutputStream;
19 import java.io.IOException;
20 import java.io.OutputStream;
21
22 import javax.servlet.http.HttpServletRequest;
23 import javax.servlet.http.HttpServletResponse;
24
25 import org.apache.commons.io.IOUtils;
26 import org.jfree.chart.ChartUtilities;
27 import org.jfree.chart.JFreeChart;
28 import org.seasar.tuigwaa.cms.core.CmsRequest;
29 import org.seasar.tuigwaa.cms.core.CmsResponse;
30 import org.seasar.tuigwaa.cms.core.wiki.WikiContext;
31 import org.seasar.tuigwaa.database.DataTable;
32 import org.seasar.tuigwaa.database.SpreadSheet;
33 import org.seasar.tuigwaa.database.chart.TCharFactory;
34 import org.seasar.tuigwaa.database.function.DaoMethod;
35 import org.seasar.tuigwaa.model.core.TgwEntity;
36 import org.seasar.tuigwaa.plugin.AbstractTgwPlugin;
37 import org.seasar.tuigwaa.plugin.PluginException;
38 import org.seasar.tuigwaa.plugin.PluginRequest;
39 import org.seasar.tuigwaa.system.Constants;
40 import org.seasar.tuigwaa.util.TgwContext;
41
42 import com.isenshi.util.HtmlBuffer;
43 import com.isenshi.util.HttpUtils;
44 import com.isenshi.util.extlib.StrutsUtil;
45
46 /***
47 * @author nishioka
48 */
49 public class GraphPlugin extends AbstractTgwPlugin {
50
51 private static final String DEFAULT_CHARTTYPE = "bar";
52
53 private static final int DEFAULT_WIDTH = 300;
54
55 private static final int DEFAULT_HEIGHT = 300;
56
57 private static final String PARAM_ENTITY = getParameterName("graph.entity");
58
59 private static final String PARAM_FILTER = getParameterName("graph.filter");
60
61 private static final String PARAM_CHARTTYPE = getParameterName("graph.chartType");
62
63 private static final String PARAM_WIDTH = getParameterName("graph.width");
64
65 private static final String PARAM_HEIGHT = getParameterName("graph.height");
66
67 public String doHTMLView(CmsRequest req, CmsResponse res,
68 PluginRequest prequest) throws PluginException {
69
70 String[] args = prequest.getArgs();
71 if (args == null) {
72 return null;
73 }
74
75 req.setParameter(Constants.PARAM_PLUGINNAME, prequest.getName());
76 req.setParameter(PARAM_ENTITY, args[0]);
77 req.setParameter(PARAM_FILTER, args[1]);
78
79 if (args.length > 2) {
80 req.setParameter(PARAM_CHARTTYPE, args[2]);
81 }
82 if (args.length > 3) {
83 req.setParameter(PARAM_WIDTH, args[3]);
84 if (args.length > 4) {
85 req.setParameter(PARAM_HEIGHT, args[4]);
86 }
87 }
88
89
90 WikiContext ctx = getConfiguration().getWikiContext();
91 String url = ctx.getPluginProxyURL(prequest.getName(), req).toString();
92
93 HtmlBuffer buf = new HtmlBuffer();
94 buf.appendImg(url, getMessage("graph.notsuppor"), -1, null);
95 return buf.toString();
96 }
97
98 public String doAction(HttpServletRequest request,
99 HttpServletResponse response) {
100
101 OutputStream outputStream = null;
102
103 try {
104
105 String siteName = TgwContext.getSiteName();
106 String entityName = StrutsUtil.getURLDecodedParameter(request,
107 PARAM_ENTITY);
108 String filterName = StrutsUtil.getURLDecodedParameter(request,
109 PARAM_FILTER);
110 String chartType = request.getParameter(PARAM_CHARTTYPE);
111
112 if (chartType == null || chartType.equals("")) {
113 chartType = DEFAULT_CHARTTYPE;
114 }
115 int width = 0;
116 int height = 0;
117
118 String widthStr = request.getParameter(PARAM_WIDTH);
119
120 if (widthStr == null || widthStr.length() == 0) {
121 width = DEFAULT_WIDTH;
122 height = DEFAULT_HEIGHT;
123 } else {
124 width = Integer.parseInt(widthStr);
125 height = Integer.parseInt(widthStr);
126 }
127
128 TgwEntity entity = getEntity(siteName, entityName);
129 DaoMethod daoMethod = getEntityDAO(entity).getMethod(filterName);
130 if (daoMethod == null) {
131 return null;
132 }
133
134 DataTable datatalbe = (DataTable) daoMethod.evaluate();
135
136 JFreeChart chart = null;
137 if (chartType.equals("bar")) {
138 chart = TCharFactory.createBarChart(datatalbe);
139 } else if (chartType.equals("pie")) {
140 if (datatalbe instanceof SpreadSheet) {
141 return null;
142 }
143 chart = TCharFactory.createPieChart(datatalbe);
144 } else if (chartType.equals("line")) {
145 chart = TCharFactory.createLineChart(datatalbe);
146 }
147
148 response.setContentType("image/png");
149 HttpUtils.setNocacheResponseHeader(response);
150
151 outputStream = new BufferedOutputStream(response.getOutputStream());
152 ChartUtilities.writeChartAsPNG(outputStream, chart, width, height);
153 } catch (IOException e) {
154
155 } catch (Exception e) {
156 e.printStackTrace();
157 } finally {
158 IOUtils.closeQuietly(outputStream);
159 }
160 return null;
161 }
162
163 }