1 16 17 package org.pentaho.ui.component; 18 19 import java.io.File ; 20 import java.io.IOException ; 21 import java.io.OutputStream ; 22 import java.io.PrintWriter ; 23 import java.io.StringWriter ; 24 import java.util.ArrayList ; 25 import java.util.HashMap ; 26 import java.util.Iterator ; 27 import java.util.List ; 28 import org.apache.commons.logging.Log; 29 import org.apache.commons.logging.LogFactory; 30 import org.dom4j.Document; 31 import org.dom4j.DocumentHelper; 32 import org.dom4j.Element; 33 import org.dom4j.Node; 34 import org.pentaho.core.connection.IPentahoMetaData; 35 import org.pentaho.core.connection.IPentahoResultSet; 36 import org.pentaho.plugin.jfreechart.DialWidgetDefinition; 37 import org.pentaho.plugin.jfreechart.WidgetDefinition; 38 import org.pentaho.core.runtime.IActionParameter; 39 import org.pentaho.core.runtime.IRuntimeContext; 40 import org.pentaho.core.solution.ActionResource; 41 import org.pentaho.core.solution.IActionResource; 42 import org.pentaho.core.solution.ISolutionEngine; 43 import org.pentaho.core.solution.SimpleOutputHandler; 44 import org.pentaho.core.system.PentahoSystem; 45 import org.pentaho.core.ui.IPentahoUrlFactory; 46 import org.pentaho.plugin.jfreechart.JFreeChartEngine; 47 import org.pentaho.core.util.TemplateUtil; 48 import org.pentaho.core.util.XmlHelper; 49 import org.pentaho.ui.XmlComponent; 50 import org.pentaho.messages.Messages; 51 import org.pentaho.util.logging.ILogger; 52 53 public class WidgetGridComponent extends XmlComponent { 54 55 58 private static final long serialVersionUID = -3952161695550067971L; 59 60 private String definitionPath; 61 62 private int widgetWidth; 63 64 private int widgetHeight; 65 66 private String solution = null; 67 68 private String actionPath = null; 69 70 private String actionName = null; 71 72 private String valueItem = null; 73 74 private String nameItem = null; 75 76 private int columns = 0; 77 78 private String instanceId = null; 79 80 private String actionOutput = null; 81 82 private String urlTemplate = null; 83 84 private String style = null; 85 86 private IRuntimeContext context; 87 88 private static final Log logger = LogFactory.getLog(WidgetGridComponent.class); 89 90 public Log getLogger() { 91 return logger; 92 } 93 94 113 public WidgetGridComponent(String definitionPath, IPentahoUrlFactory urlFactory, List messages) { 114 super(urlFactory, messages, null); 115 this.definitionPath = definitionPath; 116 String info[] = PentahoSystem.parseActionString(definitionPath); 117 if (info != null && info.length == 3) { 118 setSourcePath(info[0] + File.separator + info[2]); 119 } 120 setXsl("text/html", "DialWidget.xsl"); } 122 123 128 public void setWidgetWidth(int widgetWidth) { 129 this.widgetWidth = widgetWidth; 130 } 131 132 137 public void setWidgetHeight(int widgetHeight) { 138 this.widgetHeight = widgetHeight; 139 } 140 141 147 public void setColumns(int columns) { 148 this.columns = columns; 149 } 150 151 157 public void setInstanceId(String instanceId) { 158 this.instanceId = instanceId; 159 } 160 161 public boolean setDataAction(String widgetGridDataDefinition) { 162 ActionResource resource = new ActionResource("", IActionResource.SOLUTION_FILE_RESOURCE, "text/xml", widgetGridDataDefinition); 164 try { 165 Document dataActionDocument = PentahoSystem.getSolutionRepository(getSession()).getResourceAsDocument(resource); 166 if (dataActionDocument == null) { 167 return false; 168 } 169 Node dataNode = dataActionDocument.selectSingleNode("widgetgrid/data"); solution = XmlHelper.getNodeText("data-solution", dataNode); actionPath = XmlHelper.getNodeText("data-path", dataNode); actionName = XmlHelper.getNodeText("data-action", dataNode); actionOutput = XmlHelper.getNodeText("data-output", dataNode); valueItem = XmlHelper.getNodeText("data-value", dataNode); nameItem = XmlHelper.getNodeText("data-name", dataNode); widgetWidth = (int) XmlHelper.getNodeText("widgetgrid/width", dataActionDocument, 125); widgetHeight = (int) XmlHelper.getNodeText("widgetgrid/height", dataActionDocument, 125); columns = (int) XmlHelper.getNodeText("widgetgrid/columns", dataActionDocument, 2); style = XmlHelper.getNodeText("widgetgrid/style", dataActionDocument); } catch (Exception e) { 181 error(Messages.getErrorString("WidgetGrid.ERROR_0003_DEFINITION_NOT_VALID", widgetGridDataDefinition), e); return false; 183 } 184 return true; 185 } 186 187 197 public void setDataAction(String solution, String actionPath, String actionName, String actionOutput, String nameItem, String valueItem) { 198 this.solution = solution; 199 this.actionPath = actionPath; 200 this.actionName = actionName; 201 this.actionOutput = actionOutput; 202 this.valueItem = valueItem; 203 this.nameItem = nameItem; 204 } 205 206 public void setDrillUrlTemplate(String urlTemplate) { 207 this.urlTemplate = urlTemplate; 208 } 209 210 public boolean validate() { 211 return true; 212 } 213 214 public Document getXmlContent() { 215 216 IPentahoResultSet resultSet = null; 218 if (solution != null) { 219 resultSet = getActionData(); 220 } else { 221 } 223 224 ActionResource resource = new ActionResource("", IActionResource.SOLUTION_FILE_RESOURCE, "text/xml", definitionPath); 228 Document dialDefinition = null; 229 try { 230 dialDefinition = PentahoSystem.getSolutionRepository(getSession()).getResourceAsDocument(resource); 231 } catch (IOException e) { 232 } 233 234 WidgetDefinition widgetDefinition = new DialWidgetDefinition(dialDefinition, 0, widgetWidth, widgetHeight, getSession()); 236 237 return createDials(resultSet, widgetDefinition); 238 } 239 240 protected Document createDials(IPentahoResultSet resultSet, WidgetDefinition widgetDefinition) { 241 242 if (resultSet == null) { 243 error(Messages.getErrorString("WidgetGrid.ERROR_0001_NO_RESULTS_FROM_ACTION")); return null; 245 } 246 247 if (valueItem == null) { 248 error(Messages.getErrorString("WidgetGrid.ERROR_0002_NO_VALUE_ITEM")); } 250 251 Document result = DocumentHelper.createDocument(); 253 String baseUrl = PentahoSystem.getApplicationContext().getBaseUrl(); 254 setXslProperty("baseUrl", baseUrl); Element root = result.addElement("widgets"); 257 IPentahoMetaData metaData = resultSet.getMetaData(); 258 261 Object columnHeaders[][] = metaData.getColumnHeaders(); 263 int nameColumnNo = -1; 264 int valueColumnNo = -1; 265 for (int idx = 0; idx < columnHeaders[0].length; idx++) { 266 if (columnHeaders[0][idx].toString().equalsIgnoreCase(nameItem)) { 267 nameColumnNo = idx; 268 } 269 if (columnHeaders[0][idx].toString().equalsIgnoreCase(valueItem)) { 270 valueColumnNo = idx; 271 } 272 } 273 274 if (nameColumnNo == -1) { 275 error(Messages.getErrorString("WidgetGrid.ERROR_0004_NAME_COLUMN_MISSING", nameItem)); return null; 278 } 279 280 if (valueColumnNo == -1) { 281 error(Messages.getErrorString("WidgetGrid.ERROR_0005_VALUE_COLUMN_MISSING", valueItem)); return null; 284 } 285 286 double value; 287 String name; 288 Object row[] = resultSet.next(); 289 while (row != null) { 290 name = row[nameColumnNo].toString(); 291 try { 292 value = Double.parseDouble(row[valueColumnNo].toString()); 293 createDial(value, name, root, widgetDefinition); 294 } catch (Exception e) { 295 } 296 297 row = resultSet.next(); 298 } 299 setXslProperty("urlTarget", "pentaho_popup"); setXslProperty("columns", Integer.toString(columns)); if (style != null) { 302 setXslProperty("style", style); } 304 return result; 305 } 306 307 protected void createDial(double value, String name, Element root, WidgetDefinition widgetDefinition) { 308 309 widgetDefinition.setValue(new Double (value)); 310 311 StringWriter stringWriter = new StringWriter (); 312 PrintWriter printWriter = new PrintWriter (stringWriter); 313 314 String units = ""; String dialName = ""; String solutionDir = "system/tmp/"; String fileNamePrefix = "tmp_pie_"; String extension = ".png"; String fileName = null; 322 String filePathWithoutExtension = null; 323 try { 324 File file = File.createTempFile(fileNamePrefix, extension, new File (PentahoSystem.getApplicationContext().getFileOutputPath(solutionDir))); 325 file.deleteOnExit(); 326 fileName = file.getName(); 327 filePathWithoutExtension = solutionDir + fileName.substring(0, fileName.indexOf('.')); 328 } catch (IOException e) { 329 e.printStackTrace(); 331 } 332 JFreeChartEngine.saveChart(widgetDefinition, dialName, units, filePathWithoutExtension, widgetWidth, widgetHeight, JFreeChartEngine.OUTPUT_PNG, printWriter, this); 333 334 Element widgetNode = root.addElement("widget"); 336 widgetNode.addElement("title").setText(name); widgetNode.addElement("units").setText(units); widgetNode.addElement("width").setText(Integer.toString(widgetWidth)); widgetNode.addElement("height").setText(Integer.toString(widgetHeight)); Element valueNode = widgetNode.addElement("value"); valueNode.setText(Double.toString(value)); 342 valueNode.addAttribute("in-image", Boolean.toString(widgetDefinition.getValueFont() != null)); root.addElement("image").setText(fileName); widgetNode.addElement("image").setText(fileName); 346 String drillUrl = TemplateUtil.applyTemplate(urlTemplate, nameItem, name); 348 349 drillUrl = TemplateUtil.applyTemplate(drillUrl, context); 351 352 widgetNode.addElement("urlDrill").setText(drillUrl); 354 } 355 356 public void dispose() { 357 if (context != null) 358 context.dispose(); 359 } 360 361 protected IPentahoResultSet getActionData() { 362 365 ISolutionEngine solutionEngine = PentahoSystem.getSolutionEngineInstance(getSession()); 366 solutionEngine.setLoggingLevel(ILogger.DEBUG); 367 solutionEngine.init(getSession()); 368 369 HashMap parameterProviders = getParameterProviders(); 370 371 OutputStream outputStream = null; 372 SimpleOutputHandler outputHandler = null; 373 outputHandler = new SimpleOutputHandler(outputStream, false); 374 375 ArrayList messages = new ArrayList (); 376 String processId = this.getClass().getName(); 377 context = solutionEngine.execute(solution, actionPath, actionName, processId, false, true, instanceId, false, parameterProviders, outputHandler, null, urlFactory, messages); 378 379 if (actionOutput != null) { 380 if (context.getOutputNames().contains(actionOutput)) { 381 IActionParameter output = context.getOutputParameter(actionOutput); 382 IPentahoResultSet results = output.getValueAsResultSet(); 383 if (results != null) { 384 results = results.memoryCopy(); 385 } 386 return results; 387 } else { 388 return null; 390 } 391 } else { 392 Iterator it = context.getOutputNames().iterator(); 394 while (it.hasNext()) { 395 IActionParameter output = (IActionParameter) it.next(); 396 if (output.getType().equalsIgnoreCase(IActionParameter.TYPE_RESULT_SET)) { 397 IPentahoResultSet results = output.getValueAsResultSet(); 398 if (results != null) { 399 results = results.memoryCopy(); 400 } 401 return results; 402 } 403 } 404 } 405 return null; 406 } 407 408 } 409 | Popular Tags |