1 16 17 package org.pentaho.ui.component.charting; 18 19 import java.io.IOException ; 20 import java.io.PrintWriter ; 21 import java.io.StringWriter ; 22 import java.io.UnsupportedEncodingException ; 23 import java.net.URLEncoder ; 24 import java.util.ArrayList ; 25 import java.util.Iterator ; 26 import java.util.List ; 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 paramName2 = null; 56 protected String dataAction = null; 57 58 public PieDatasetChartComponent(int chartType, String definitionPath, int width, int height, IPentahoUrlFactory urlFactory, List messages) { 59 super(chartType, definitionPath, width, height, urlFactory, messages); 60 setXsl("text/html", "Chart.xsl"); } 63 64 public PieDatasetChartComponent(String definitionPath, IPentahoUrlFactory urlFactory, ArrayList messages) { 65 super(definitionPath, urlFactory, messages); 66 setXsl("text/html", "Chart.xsl"); } 69 70 public PieDatasetChartComponent(IPentahoUrlFactory urlFactory, List messages) { 71 super(urlFactory, messages); 72 setXsl("text/html", "Chart.xsl"); } 75 76 public Dataset createChart(Document doc) { 77 if (solution != null) { values = getActionData(); 79 } else { 80 } 82 83 if( values == null ) { 84 return null; 86 } 87 88 Node chartAttributes = doc.selectSingleNode("//" + CHART_NODE_NAME); PieDatasetChartDefinition chartDefinition = new PieDatasetChartDefinition((IPentahoResultSet) values, byRow, chartAttributes, getSession()); 92 93 setTitle(chartDefinition.getTitle()); 94 95 Node urlTemplateNode = chartAttributes.selectSingleNode(URLTEMPLE_NODE_NAME); 97 if (urlTemplateNode != null) { 98 setUrlTemplate(urlTemplateNode.getText()); 99 } 100 101 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 chartDefinition) { 114 ActionResource resource = new ActionResource("", IActionResource.SOLUTION_FILE_RESOURCE, "text/xml", 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"); chartType = (int) XmlHelper.getNodeText("chart-type", dataNode, -1); solution = XmlHelper.getNodeText("data-solution", dataNode); actionPath = XmlHelper.getNodeText("data-path", dataNode); actionName = XmlHelper.getNodeText("data-action", dataNode); actionOutput = XmlHelper.getNodeText("data-output", dataNode); byRow = XmlHelper.getNodeText("data-orientation", dataNode, "rows").equals("rows"); width = (int) XmlHelper.getNodeText("chart/width", dataActionDocument, 125); height = (int) XmlHelper.getNodeText("chart/height", dataActionDocument, 125); } catch (Exception e) { 131 error(Messages.getString("CategoryDatasetChartComponent.ERROR_0001_INVALID_CHART_DEFINITION", chartDefinition), e); return false; 133 } 134 return true; 135 } 136 137 public Document getXmlContent() { 138 139 Document result = DocumentHelper.createDocument(); 141 String baseUrl = PentahoSystem.getApplicationContext().getBaseUrl(); 142 setXslProperty("baseUrl", baseUrl); 144 ActionResource resource = new ActionResource(title, IActionResource.SOLUTION_FILE_RESOURCE, "text/xml", definitionPath); 147 Document chartDefinition = null; 148 String mapName = "chart" + chartCount++; try { 150 chartDefinition = PentahoSystem.getSolutionRepository(getSession()).getResourceAsDocument(resource); 151 } catch (IOException e) { 152 } 153 if( chartDefinition == null ) { 154 Element errorElement = result.addElement("error"); errorElement.addElement("title").setText( Messages.getString( "ABSTRACTCHARTEXPRESSION.ERROR_0001_ERROR_GENERATING_CHART" ) ); String message = Messages.getString( "CHARTS.ERROR_0001_CHART_DEFINIION_MISSING", definitionPath ); errorElement.addElement("message").setText( message ); error( message ); 159 return result; 160 } 161 dataDefinition = createChart(chartDefinition); 163 if( dataDefinition == null ) { 164 Element errorElement = result.addElement("error"); errorElement.addElement("title").setText( Messages.getString( "ABSTRACTCHARTEXPRESSION.ERROR_0001_ERROR_GENERATING_CHART" ) ); String message = Messages.getString( "CHARTS.ERROR_0002_CHART_DATA_MISSING", solution + "/"+ actionPath + "/" + actionName ); errorElement.addElement("message").setText( message ); return result; 170 } 171 172 PrintWriter printWriter = new PrintWriter (new StringWriter ()); 174 String chartTitle = ""; try { 179 height = Integer.parseInt(chartDefinition.selectSingleNode("/chart/height").getText()); width = Integer.parseInt(chartDefinition.selectSingleNode("/chart/width").getText()); } catch (Exception e) { 182 } 184 if (chartDefinition.selectSingleNode("/chart/urlTemplate") != null) { urlTemplate = chartDefinition.selectSingleNode("/chart/urlTemplate").getText(); } 187 188 if (chartDefinition.selectSingleNode("/chart/paramName") != null) { paramName = chartDefinition.selectSingleNode("/chart/paramName").getText(); } 191 192 Element root = result.addElement("charts"); DefaultPieDataset chartDataDefinition = (DefaultPieDataset) dataDefinition; 194 String [] tempFileInfo = createTempFile(); 197 String fileName = tempFileInfo[AbstractChartComponent.FILENAME_INDEX]; 198 String 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); applyOuterURLTemplateParam(); 203 popultateInfo(info); 204 Element chartElement = root.addElement("chart"); chartElement.addElement("mapName").setText(mapName); chartElement.addElement("width").setText(Integer.toString(width)); chartElement.addElement("height").setText(Integer.toString(height)); String mapString = ImageMapUtilities.getImageMap(mapName, info); 225 chartElement.addElement("imageMap").setText(mapString); chartElement.addElement("image").setText(fileName); return result; 229 } 230 231 private void popultateInfo(ChartRenderingInfo info) { 232 ArrayList keyListArray = null; 233 int keyListIndex = 0; 234 Iterator 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 value = pieSectionEntity.getSectionKey().toString(); 240 if (paramName == null) { 241 pieSectionEntity.setURLText(value); 242 } else { 243 try { 244 String encodedVal = URLEncoder.encode(value, LocaleHelper.getSystemEncoding()); 245 String drillURL = TemplateUtil.applyTemplate(urlTemplate, paramName, encodedVal); 246 pieSectionEntity.setURLText(drillURL); 247 248 } catch (UnsupportedEncodingException e) { 249 e.printStackTrace(); 251 } 252 } 253 } else if (entity instanceof CategoryItemEntity) { 254 CategoryItemEntity categoryItemEntity = (CategoryItemEntity) entity; 255 if (keyListArray == null) { 256 keyListArray = new ArrayList (categoryItemEntity.getDataset().getRowKeys()); 257 } 258 String category = categoryItemEntity.getCategory().toString(); 259 if (paramName == null) { 260 categoryItemEntity.setURLText(category); 261 } else { 262 try { 263 String encodedVal = URLEncoder.encode(category, LocaleHelper.getSystemEncoding()); 264 String 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 e) { 274 e.printStackTrace(); 276 } 277 } 278 } 279 } 280 } 281 282 public boolean validate() { 283 return false; 285 } 286 } 287 | Popular Tags |