KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > ui > component > charting > PieDatasetChartComponent


1 /*
2  * Copyright 2006 Pentaho Corporation. All rights reserved.
3  * This software was developed by Pentaho Corporation and is provided under the terms
4  * of the Mozilla Public License, Version 1.1, or any later version. You may not use
5  * this file except in compliance with the license. If you need a copy of the license,
6  * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
7  * BI Platform. The Initial Developer is Pentaho Corporation.
8  *
9  * Software distributed under the Mozilla Public License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
11  * the license for the specific language governing your rights and limitations.
12  *
13  * Created Nov 17, 2005
14  * @author wseyler
15  */

16
17 package org.pentaho.ui.component.charting;
18
19 import java.io.IOException JavaDoc;
20 import java.io.PrintWriter JavaDoc;
21 import java.io.StringWriter JavaDoc;
22 import java.io.UnsupportedEncodingException JavaDoc;
23 import java.net.URLEncoder JavaDoc;
24 import java.util.ArrayList JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27
28 import org.dom4j.Document;
29 import org.dom4j.DocumentHelper;
30 import org.dom4j.Element;
31 import org.dom4j.Node;
32 import org.jfree.chart.ChartRenderingInfo;
33 import org.jfree.chart.entity.CategoryItemEntity;
34 import org.jfree.chart.entity.ChartEntity;
35 import org.jfree.chart.entity.PieSectionEntity;
36 import org.jfree.chart.entity.StandardEntityCollection;
37 import org.jfree.chart.imagemap.ImageMapUtilities;
38 import org.jfree.data.general.Dataset;
39 import org.jfree.data.general.DefaultPieDataset;
40 import org.pentaho.core.connection.IPentahoResultSet;
41 import org.pentaho.core.solution.ActionResource;
42 import org.pentaho.core.solution.IActionResource;
43 import org.pentaho.core.system.PentahoSystem;
44 import org.pentaho.core.ui.IPentahoUrlFactory;
45 import org.pentaho.core.util.TemplateUtil;
46 import org.pentaho.core.util.XmlHelper;
47 import org.pentaho.messages.Messages;
48 import org.pentaho.messages.util.LocaleHelper;
49 import org.pentaho.plugin.jfreechart.JFreeChartEngine;
50 import org.pentaho.plugin.jfreechart.PieDatasetChartDefinition;
51
52 public class PieDatasetChartComponent extends AbstractJFreeChartComponent {
53     private static final long serialVersionUID = -6268840271596447555L;
54
55     protected String JavaDoc paramName2 = null;
56     protected String JavaDoc dataAction = null;
57
58     public PieDatasetChartComponent(int chartType, String JavaDoc definitionPath, int width, int height, IPentahoUrlFactory urlFactory, List JavaDoc messages) {
59         super(chartType, definitionPath, width, height, urlFactory, messages);
60         // Set the XSL file to be used to generate the HTML
61
setXsl("text/html", "Chart.xsl"); //$NON-NLS-1$ //$NON-NLS-2$
62
}
63
64     public PieDatasetChartComponent(String JavaDoc definitionPath, IPentahoUrlFactory urlFactory, ArrayList JavaDoc messages) {
65         super(definitionPath, urlFactory, messages);
66         // Set the XSL file to be used to generate the HTML
67
setXsl("text/html", "Chart.xsl"); //$NON-NLS-1$ //$NON-NLS-2$
68
}
69
70     public PieDatasetChartComponent(IPentahoUrlFactory urlFactory, List JavaDoc messages) {
71         super(urlFactory, messages);
72         // Set the XSL file to be used to generate the HTML
73
setXsl("text/html", "Chart.xsl"); //$NON-NLS-1$ //$NON-NLS-2$
74
}
75
76     public Dataset createChart(Document doc) {
77         if (solution != null) { // if we have a solution then get the values
78
values = getActionData();
79         } else {
80             // TODO support other methods of getting data
81
}
82
83         if( values == null ) {
84             // we could not get any data
85
return null;
86         }
87         
88         // get the chart node from the document
89
Node chartAttributes = doc.selectSingleNode("//" + CHART_NODE_NAME); //$NON-NLS-1$
90
// create the definition
91
PieDatasetChartDefinition chartDefinition = new PieDatasetChartDefinition((IPentahoResultSet) values, byRow, chartAttributes, getSession());
92
93         setTitle(chartDefinition.getTitle());
94
95         // get the URL template
96
Node urlTemplateNode = chartAttributes.selectSingleNode(URLTEMPLE_NODE_NAME);
97         if (urlTemplateNode != null) {
98             setUrlTemplate(urlTemplateNode.getText());
99         }
100
101         // get the additional parameter
102
Node paramName2Node = chartAttributes.selectSingleNode(PARAM2_NODE_NAME);
103         if (paramName2Node != null) {
104             paramName2 = paramName2Node.getText();
105         }
106
107         setWidth(chartDefinition.getWidth());
108         setHeight(chartDefinition.getHeight());
109
110         return chartDefinition;
111     }
112
113     public boolean setDataAction(String JavaDoc chartDefinition) {
114         ActionResource resource = new ActionResource("", IActionResource.SOLUTION_FILE_RESOURCE, "text/xml", //$NON-NLS-1$ //$NON-NLS-2$
115
chartDefinition);
116         try {
117             Document dataActionDocument = PentahoSystem.getSolutionRepository(getSession()).getResourceAsDocument(resource);
118             if (dataActionDocument == null) {
119                 return false;
120             }
121             Node dataNode = dataActionDocument.selectSingleNode("chart/data"); //$NON-NLS-1$
122
chartType = (int) XmlHelper.getNodeText("chart-type", dataNode, -1); //$NON-NLS-1$
123
solution = XmlHelper.getNodeText("data-solution", dataNode); //$NON-NLS-1$
124
actionPath = XmlHelper.getNodeText("data-path", dataNode); //$NON-NLS-1$
125
actionName = XmlHelper.getNodeText("data-action", dataNode); //$NON-NLS-1$
126
actionOutput = XmlHelper.getNodeText("data-output", dataNode); //$NON-NLS-1$
127
byRow = XmlHelper.getNodeText("data-orientation", dataNode, "rows").equals("rows"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
128
width = (int) XmlHelper.getNodeText("chart/width", dataActionDocument, 125); //$NON-NLS-1$
129
height = (int) XmlHelper.getNodeText("chart/height", dataActionDocument, 125); //$NON-NLS-1$
130
} catch (Exception JavaDoc e) {
131             error(Messages.getString("CategoryDatasetChartComponent.ERROR_0001_INVALID_CHART_DEFINITION", chartDefinition), e); //$NON-NLS-1$
132
return false;
133         }
134         return true;
135     }
136
137     public Document getXmlContent() {
138
139         // Create a document that describes the result
140
Document result = DocumentHelper.createDocument();
141         String JavaDoc baseUrl = PentahoSystem.getApplicationContext().getBaseUrl();
142         setXslProperty("baseUrl", baseUrl); //$NON-NLS-1$
143

144         // load the XML document that defines the pie
145
ActionResource resource = new ActionResource(title, IActionResource.SOLUTION_FILE_RESOURCE, "text/xml", //$NON-NLS-1$
146
definitionPath);
147         Document chartDefinition = null;
148         String JavaDoc mapName = "chart" + chartCount++; //$NON-NLS-1$
149
try {
150             chartDefinition = PentahoSystem.getSolutionRepository(getSession()).getResourceAsDocument(resource);
151         } catch (IOException JavaDoc e) {
152         }
153         if( chartDefinition == null ) {
154             Element errorElement = result.addElement("error"); //$NON-NLS-1$
155
errorElement.addElement("title").setText( Messages.getString( "ABSTRACTCHARTEXPRESSION.ERROR_0001_ERROR_GENERATING_CHART" ) ); //$NON-NLS-1$ //$NON-NLS-2$
156
String JavaDoc message = Messages.getString( "CHARTS.ERROR_0001_CHART_DEFINIION_MISSING", definitionPath ); //$NON-NLS-1$
157
errorElement.addElement("message").setText( message ); //$NON-NLS-1$
158
error( message );
159             return result;
160         }
161         // create a pie definition from the XML definition
162
dataDefinition = createChart(chartDefinition);
163         if( dataDefinition == null ) {
164             Element errorElement = result.addElement("error"); //$NON-NLS-1$
165
errorElement.addElement("title").setText( Messages.getString( "ABSTRACTCHARTEXPRESSION.ERROR_0001_ERROR_GENERATING_CHART" ) ); //$NON-NLS-1$ //$NON-NLS-2$
166
String JavaDoc message = Messages.getString( "CHARTS.ERROR_0002_CHART_DATA_MISSING", solution + "/"+ actionPath + "/" + actionName ); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
167
errorElement.addElement("message").setText( message ); //$NON-NLS-1$
168
// System .out.println( result.asXML() );
169
return result;
170         }
171
172         // create an image for the dial using the JFreeChart engine
173
PrintWriter JavaDoc printWriter = new PrintWriter JavaDoc(new StringWriter JavaDoc());
174         // we'll dispay the title in HTML so that the dial image does not have
175
// to
176
// accommodate it
177
String JavaDoc chartTitle = ""; //$NON-NLS-1$
178
try {
179             height = Integer.parseInt(chartDefinition.selectSingleNode("/chart/height").getText()); //$NON-NLS-1$
180
width = Integer.parseInt(chartDefinition.selectSingleNode("/chart/width").getText()); //$NON-NLS-1$
181
} catch (Exception JavaDoc e) {
182             // go with the default
183
}
184         if (chartDefinition.selectSingleNode("/chart/urlTemplate") != null) { //$NON-NLS-1$
185
urlTemplate = chartDefinition.selectSingleNode("/chart/urlTemplate").getText(); //$NON-NLS-1$
186
}
187
188         if (chartDefinition.selectSingleNode("/chart/paramName") != null) { //$NON-NLS-1$
189
paramName = chartDefinition.selectSingleNode("/chart/paramName").getText(); //$NON-NLS-1$
190
}
191
192         Element root = result.addElement("charts"); //$NON-NLS-1$
193
DefaultPieDataset chartDataDefinition = (DefaultPieDataset) dataDefinition;
194         // if (chartDataDefinition.getRowCount() > 0) {
195
// create temporary file names
196
String JavaDoc[] tempFileInfo = createTempFile();
197         String JavaDoc fileName = tempFileInfo[AbstractChartComponent.FILENAME_INDEX];
198         String JavaDoc filePathWithoutExtension = tempFileInfo[AbstractChartComponent.FILENAME_WITHOUT_EXTENSION_INDEX];
199
200         ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
201         JFreeChartEngine.saveChart(chartDataDefinition, chartTitle, "", filePathWithoutExtension, width, height, JFreeChartEngine.OUTPUT_PNG, printWriter, info, this); //$NON-NLS-1$
202
applyOuterURLTemplateParam();
203         popultateInfo(info);
204         Element chartElement = root.addElement("chart"); //$NON-NLS-1$
205
chartElement.addElement("mapName").setText(mapName); //$NON-NLS-1$
206
chartElement.addElement("width").setText(Integer.toString(width)); //$NON-NLS-1$
207
chartElement.addElement("height").setText(Integer.toString(height)); //$NON-NLS-1$
208
// for (int row = 0; row < chartDataDefinition.getRowCount(); row++) {
209
// for (int column = 0; column < chartDataDefinition.getColumnCount();
210
// column++) {
211
// Number value = chartDataDefinition.getValue(row, column);
212
// Comparable rowKey = chartDataDefinition.getRowKey(row);
213
// Comparable columnKey = chartDataDefinition.getColumnKey(column);
214
// Element valueElement = chartElement.addElement("value2D");
215
// //$NON-NLS-1$
216
// valueElement.addElement("value").setText(value.toString());
217
// //$NON-NLS-1$
218
// valueElement.addElement("row-key").setText(rowKey.toString());
219
// //$NON-NLS-1$
220
// valueElement.addElement("column-key").setText(columnKey.toString());
221
// //$NON-NLS-1$
222
// }
223
// }
224
String JavaDoc mapString = ImageMapUtilities.getImageMap(mapName, info);
225         chartElement.addElement("imageMap").setText(mapString); //$NON-NLS-1$
226
chartElement.addElement("image").setText(fileName); //$NON-NLS-1$
227
// }
228
return result;
229     }
230
231     private void popultateInfo(ChartRenderingInfo info) {
232         ArrayList JavaDoc keyListArray = null;
233         int keyListIndex = 0;
234         Iterator JavaDoc iter = info.getEntityCollection().iterator();
235         while (iter.hasNext()) {
236             ChartEntity entity = (ChartEntity) iter.next();
237             if (entity instanceof PieSectionEntity) {
238                 PieSectionEntity pieSectionEntity = (PieSectionEntity) entity;
239                 String JavaDoc value = pieSectionEntity.getSectionKey().toString();
240                 if (paramName == null) {
241                     pieSectionEntity.setURLText(value);
242                 } else {
243                     try {
244                         String JavaDoc encodedVal = URLEncoder.encode(value, LocaleHelper.getSystemEncoding());
245                         String JavaDoc drillURL = TemplateUtil.applyTemplate(urlTemplate, paramName, encodedVal);
246                         pieSectionEntity.setURLText(drillURL);
247
248                     } catch (UnsupportedEncodingException JavaDoc e) {
249                         // TODO Auto-generated catch block
250
e.printStackTrace();
251                     }
252                 }
253             } else if (entity instanceof CategoryItemEntity) {
254                 CategoryItemEntity categoryItemEntity = (CategoryItemEntity) entity;
255                 if (keyListArray == null) {
256                     keyListArray = new ArrayList JavaDoc(categoryItemEntity.getDataset().getRowKeys());
257                 }
258                 String JavaDoc category = categoryItemEntity.getCategory().toString();
259                 if (paramName == null) {
260                     categoryItemEntity.setURLText(category);
261                 } else {
262                     try {
263                         String JavaDoc encodedVal = URLEncoder.encode(category, LocaleHelper.getSystemEncoding());
264                         String JavaDoc drillURL = TemplateUtil.applyTemplate(urlTemplate, paramName, encodedVal);
265                         if (keyListIndex >= keyListArray.size()) {
266                             keyListIndex = 0;
267                         }
268                         encodedVal = URLEncoder.encode(keyListArray.get(keyListIndex).toString(), LocaleHelper.getSystemEncoding());
269                         keyListIndex++;
270                         drillURL = TemplateUtil.applyTemplate(drillURL, paramName2, encodedVal);
271                         categoryItemEntity.setURLText(drillURL);
272
273                     } catch (UnsupportedEncodingException JavaDoc e) {
274                         // TODO Auto-generated catch block
275
e.printStackTrace();
276                     }
277                 }
278             }
279         }
280     }
281
282     public boolean validate() {
283         // TODO Auto-generated method stub
284
return false;
285     }
286 }
287
Popular Tags