1 16 package org.pentaho.plugin.jfreechart; 17 18 import java.io.BufferedWriter ; 19 import java.io.File ; 20 import java.io.FileWriter ; 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 import java.io.PrintWriter ; 24 import java.io.StringWriter ; 25 import java.util.Iterator ; 26 import java.util.Set ; 27 28 import org.apache.commons.logging.Log; 29 import org.apache.commons.logging.LogFactory; 30 import org.dom4j.Element; 31 import org.dom4j.Node; 32 import org.jfree.chart.ChartRenderingInfo; 33 import org.jfree.chart.entity.StandardEntityCollection; 34 import org.jfree.chart.imagemap.ImageMapUtilities; 35 import org.jfree.chart.imagemap.StandardToolTipTagFragmentGenerator; 36 import org.jfree.data.general.Dataset; 37 import org.pentaho.core.connection.IPentahoResultSet; 38 import org.pentaho.core.repository.ISolutionRepository; 39 import org.pentaho.core.system.PentahoSystem; 40 import org.pentaho.plugin.ComponentBase; 41 42 public class ChartComponent extends ComponentBase { 43 private static final long serialVersionUID = 9050456842938084174L; 44 45 private static final String CHART_DATA_PROP = "chart-data"; 47 private static final String CHART_ATTRIBUTES_PROP = "chart-attributes"; 49 private static final String BY_ROW_PROP = "by-row"; 51 private static final String URL_TEMPLATE = "url-template"; 53 private static final String PARAMETER_NAME = "paramName"; 55 private static final String OUTER_PARAMETER_NAME = "series-name"; 57 private static final String OUTPUT_TYPE_PROP = "output-type"; 59 private static final String CHART_FILE_NAME_OUTPUT = "chart-filename"; 61 private static final String HTML_MAPPING_OUTPUT = "chart-mapping"; 63 private static final String BASE_URL_OUTPUT = "base-url"; 65 private static final String SVG_TYPE = "SVG"; 67 private static final String TEMP_DIRECTORY = "system/tmp/"; 69 private static final String FILENAME_PREFIX = "tmp_chart_"; 71 private static final String PNG_EXTENSION = ".png"; 73 private static final String SVG_EXTENSION = ".svg"; 75 private static final String MAP_EXTENSION = ".map"; 77 private static final int FILE_NAME = 0; 78 79 private static final int MAP_NAME = 1; 80 81 public Log getLogger() { 82 return LogFactory.getLog(ChartComponent.class); 83 } 84 85 protected boolean validateSystemSettings() { 86 return true; 88 } 89 90 protected boolean validateAction() { 91 if (!isDefinedInput(CHART_DATA_PROP)) { 93 inputMissingError(CHART_DATA_PROP); 94 return false; 95 } 96 if (!isDefinedInput(CHART_ATTRIBUTES_PROP)) { 98 inputMissingError(CHART_ATTRIBUTES_PROP); 99 return false; 100 } 101 102 if (!isDefinedInput(CHART_ATTRIBUTES_PROP)) { 103 inputMissingError(CHART_ATTRIBUTES_PROP); 104 return false; 105 } 106 return true; 108 } 109 110 public void done() { 111 } 112 113 protected boolean executeAction() { 114 int height = -1; 115 int width = -1; 116 117 118 IPentahoResultSet data = (IPentahoResultSet) getInputValue(CHART_DATA_PROP); 119 String urlTemplate = (String ) getInputValue(URL_TEMPLATE); 120 Node chartAttributes = getComponentDefinition( true ).selectSingleNode(CHART_ATTRIBUTES_PROP); 121 122 if (urlTemplate == null || urlTemplate.length() == 0) { 123 if (chartAttributes.selectSingleNode(URL_TEMPLATE) != null) { 124 urlTemplate = chartAttributes.selectSingleNode(URL_TEMPLATE).getText(); 125 } 126 } 127 128 String parameterName = (String ) getInputValue(PARAMETER_NAME); 129 if (parameterName == null || parameterName.length() == 0) { 130 if (chartAttributes.selectSingleNode(PARAMETER_NAME) != null) { 131 parameterName = chartAttributes.selectSingleNode(PARAMETER_NAME).getText(); 132 } 133 } 134 135 String outerParameterName = (String ) getInputValue(OUTER_PARAMETER_NAME); 136 if (outerParameterName == null || outerParameterName.length() == 0) { 137 if (chartAttributes.selectSingleNode(OUTER_PARAMETER_NAME) != null) { 138 outerParameterName = chartAttributes.selectSingleNode(OUTER_PARAMETER_NAME).getText(); 139 } 140 } 141 142 String chartType = chartAttributes.selectSingleNode(ChartDefinition.TYPE_NODE_NAME).getText(); 143 144 Object widthObj = getInputValue(ChartDefinition.WIDTH_NODE_NAME); 147 if (widthObj != null) { 148 width = Integer.parseInt(widthObj.toString()); 149 if (width != -1) { 150 if (chartAttributes.selectSingleNode(ChartDefinition.WIDTH_NODE_NAME) == null) { 151 ((Element)chartAttributes).addElement(ChartDefinition.WIDTH_NODE_NAME); 152 } 153 chartAttributes.selectSingleNode(ChartDefinition.WIDTH_NODE_NAME).setText(Integer.toString(width)); 154 } 155 } 156 Object heightObj = getInputValue(ChartDefinition.HEIGHT_NODE_NAME); 157 if (heightObj != null) { 158 height = Integer.parseInt(heightObj.toString()); 159 if (height != -1) { 160 if (chartAttributes.selectSingleNode(ChartDefinition.HEIGHT_NODE_NAME) == null) { 161 ((Element)chartAttributes).addElement(ChartDefinition.HEIGHT_NODE_NAME); 162 } 163 chartAttributes.selectSingleNode(ChartDefinition.HEIGHT_NODE_NAME).setText(Integer.toString(height)); 164 } 165 } 166 Object titleObj = getInputValue(ChartDefinition.TITLE_NODE_NAME); 167 if (titleObj != null) { 168 if (chartAttributes.selectSingleNode(ChartDefinition.TITLE_NODE_NAME) == null) { 169 ((Element)chartAttributes).addElement(ChartDefinition.TITLE_NODE_NAME); 170 } 171 chartAttributes.selectSingleNode(ChartDefinition.TITLE_NODE_NAME).setText(titleObj.toString()); 172 } 173 175 Node titleNode = chartAttributes.selectSingleNode(ChartDefinition.TITLE_NODE_NAME); 177 if (titleNode != null) { 178 String titleStr = titleNode.getText(); 179 if (titleStr != null) { 180 String newTitle = applyInputsToFormat(titleStr); 181 titleNode.setText(newTitle); 182 } 183 } 184 185 Node subtitleNode = chartAttributes.selectSingleNode(ChartDefinition.SUBTITLE_NODE_NAME); 186 if (subtitleNode != null) { 187 String subtitleStr = subtitleNode.getText(); 188 if (subtitleStr != null) { 189 String newSubtitle = applyInputsToFormat(subtitleStr); 190 subtitleNode.setText(newSubtitle); 191 } 192 } 193 195 197 boolean byRow = false; 198 if (getInputStringValue(BY_ROW_PROP) != null) { 199 byRow = Boolean.valueOf(getInputStringValue(BY_ROW_PROP)).booleanValue(); 200 } 201 int outputType = JFreeChartEngine.OUTPUT_PNG; if (getInputStringValue(OUTPUT_TYPE_PROP) != null) { 203 if (SVG_TYPE.equalsIgnoreCase(getInputStringValue(OUTPUT_TYPE_PROP))) { 204 outputType = JFreeChartEngine.OUTPUT_SVG; 205 } 206 } 207 208 height = (int) getInputLongValue(CHART_ATTRIBUTES_PROP + "/" + ChartDefinition.HEIGHT_NODE_NAME, 50); width = (int) getInputLongValue(CHART_ATTRIBUTES_PROP + "/" + ChartDefinition.WIDTH_NODE_NAME, 100); String title = getInputStringValue(CHART_ATTRIBUTES_PROP + "/" + ChartDefinition.TITLE_NODE_NAME); 212 214 String datasetType = ChartDefinition.CATAGORY_DATASET_STR; Node datasetTypeNode = chartAttributes.selectSingleNode(ChartDefinition.DATASET_TYPE_NODE_NAME); 216 if (datasetTypeNode != null) { 217 datasetType = datasetTypeNode.getText(); 218 } 219 Dataset dataDefinition = null; 220 if (ChartDefinition.XY_SERIES_COLLECTION_STR.equalsIgnoreCase(datasetType)) { 221 dataDefinition = new XYSeriesCollectionChartDefinition(data, byRow, chartAttributes, getSession()); 222 } else if (ChartDefinition.TIME_SERIES_COLLECTION_STR.equalsIgnoreCase(datasetType)) { 223 dataDefinition = new TimeSeriesCollectionChartDefinition(data, byRow, chartAttributes, getSession()); 224 } else if (ChartDefinition.PIE_CHART_STR.equalsIgnoreCase(chartType)) { 225 dataDefinition = new PieDatasetChartDefinition(data, byRow, chartAttributes, getSession()); 226 } else if (ChartDefinition.DIAL_CHART_STR.equalsIgnoreCase(chartType)) { 227 dataDefinition = new DialWidgetDefinition(data, byRow, chartAttributes, width, height, getSession()); 228 } else { 229 dataDefinition = new CategoryDatasetChartDefinition(data, byRow, chartAttributes, getSession()); 230 } 231 232 233 File [] fileResults = createTempFile(outputType); 234 String filePathWithoutExtension = TEMP_DIRECTORY + fileResults[FILE_NAME].getName().substring(0, fileResults[FILE_NAME].getName().indexOf('.')); 235 PrintWriter printWriter = new PrintWriter (new StringWriter ()); 236 ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection()); 237 JFreeChartEngine.saveChart(dataDefinition, title, "", filePathWithoutExtension, width, height, outputType, printWriter, info, this); String mapString = ImageMapUtilities.getImageMap(fileResults[MAP_NAME].getName().substring(0, fileResults[MAP_NAME].getName().indexOf('.')), info, new StandardToolTipTagFragmentGenerator(), new PentahoChartURLTagFragmentGenerator(urlTemplate, dataDefinition, parameterName, outerParameterName)); 239 try { 240 BufferedWriter out = new BufferedWriter (new FileWriter (fileResults[MAP_NAME])); 241 out.write(mapString); 242 out.close(); 243 } catch (IOException e) { 244 e.printStackTrace(); 246 } 247 248 Set outputs = getOutputNames(); 250 if (outputs != null && outputs.size() > 0) { 251 Iterator iter = outputs.iterator(); 252 while (iter.hasNext()) { 253 String outputName = (String ) iter.next(); 254 String outputValue = null; 255 if (outputName.equals(CHART_FILE_NAME_OUTPUT)) { 256 outputValue = fileResults[FILE_NAME].getName(); 257 } else if (outputName.equals(HTML_MAPPING_OUTPUT)) { 258 outputValue = fileResults[MAP_NAME].getName(); 259 } else if (outputName.equals(BASE_URL_OUTPUT)) { 260 outputValue = PentahoSystem.getApplicationContext().getBaseUrl(); 261 } 262 263 if (outputValue != null) { 264 setOutputValue(outputName, outputValue); 265 } 266 } 267 } else { try { 269 getFeedbackOutputStream().write("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">".getBytes()); getFeedbackOutputStream().write("<html xmlns=\"http://www.w3.org/1999/xhtml\">".getBytes()); getFeedbackOutputStream().write("<body><center><br />".getBytes()); 273 ISolutionRepository repository = PentahoSystem.getSolutionRepository(getSession()); 274 InputStream is = repository.getResourceInputStream(getSolutionRelativePath(fileResults[MAP_NAME].getAbsolutePath())); 275 276 byte bytes[] = new byte[1024]; 277 int numRead = 0; 278 while ((numRead=is.read(bytes)) >= 0) { 279 getFeedbackOutputStream().write(bytes, 0, numRead); 280 } 281 is.close(); 282 283 getFeedbackOutputStream().write("<img border=\"0\" ".getBytes()); getFeedbackOutputStream().write(("width=\"" + width +"\" ").getBytes()); getFeedbackOutputStream().write(("height=\"" + height +"\" ").getBytes()); getFeedbackOutputStream().write(("usemap=\"#" + fileResults[MAP_NAME].getName().substring(0, fileResults[MAP_NAME].getName().indexOf('.')) + "\" ").getBytes()); getFeedbackOutputStream().write(("src=\"" + PentahoSystem.getApplicationContext().getBaseUrl() + "getImage?image=" + fileResults[FILE_NAME].getName() + "\"/>").getBytes()); 289 getFeedbackOutputStream().write("</center></body></html>".getBytes()); } catch (Exception e) { 291 e.printStackTrace(); 292 } 293 } 294 295 296 return true; 297 } 298 299 public boolean init() { 300 return true; 302 } 303 304 307 protected File [] createTempFile(int outputType) { 308 File [] results = new File [2]; 309 310 String extension = outputType == JFreeChartEngine.OUTPUT_SVG ? SVG_EXTENSION : PNG_EXTENSION; 311 312 try { 313 File file = File.createTempFile(FILENAME_PREFIX, extension, new File (PentahoSystem.getApplicationContext().getFileOutputPath(TEMP_DIRECTORY))); 314 file.deleteOnExit(); 315 results[FILE_NAME] = file; 316 file = File.createTempFile(FILENAME_PREFIX, MAP_EXTENSION, new File (PentahoSystem.getApplicationContext().getFileOutputPath(TEMP_DIRECTORY))); 317 file.deleteOnExit(); 318 results[MAP_NAME] = file; 319 } catch (IOException e) { 320 e.printStackTrace(); 322 } 323 return results; 324 } 325 326 protected String getSolutionRelativePath(String fullPath) { 327 String prefix = PentahoSystem.getApplicationContext().getSolutionPath(""); String result = fullPath.replaceFirst(prefix, ""); return result; 330 } 331 } 332 | Popular Tags |