1 16 17 package org.pentaho.ui.component.charting; 18 19 import java.io.File ; 20 import java.io.IOException ; 21 import java.io.OutputStream ; 22 import java.io.UnsupportedEncodingException ; 23 import java.lang.reflect.Array ; 24 import java.net.URLEncoder ; 25 import java.util.ArrayList ; 26 import java.util.HashMap ; 27 import java.util.Iterator ; 28 import java.util.List ; 29 30 import org.apache.commons.logging.Log; 31 import org.apache.commons.logging.LogFactory; 32 import org.pentaho.core.connection.IPentahoResultSet; 33 import org.pentaho.core.runtime.IActionParameter; 34 import org.pentaho.core.runtime.IRuntimeContext; 35 import org.pentaho.core.solution.ISolutionEngine; 36 import org.pentaho.core.solution.SimpleOutputHandler; 37 import org.pentaho.core.system.PentahoSystem; 38 import org.pentaho.core.ui.IPentahoUrlFactory; 39 import org.pentaho.core.util.TemplateUtil; 40 import org.pentaho.messages.util.LocaleHelper; 41 import org.pentaho.ui.XmlComponent; 42 import org.pentaho.util.logging.ILogger; 43 44 public abstract class AbstractChartComponent extends XmlComponent { 45 46 protected static final String CHART_NODE_NAME = "chart"; protected static final String URLTEMPLE_NODE_NAME = "url-template"; protected static final String PARAM2_NODE_NAME = "series-name"; 50 public static final int FILENAME_INDEX = 0; 51 52 public static final int FILENAME_WITHOUT_EXTENSION_INDEX = 1; 53 54 protected String definitionPath; 55 56 protected int width = -1; 57 58 protected int height = -1; 59 60 protected String title; 61 62 protected Object values; 63 64 protected boolean byRow = true; 65 66 protected String solution; 67 68 protected String actionPath; 69 70 protected String actionName; 71 72 protected String actionOutput; 73 74 protected String instanceId = null; 75 76 protected IRuntimeContext context; 77 78 protected String urlTemplate = null; 79 80 protected List outerParamNames = new ArrayList (); 81 82 protected String paramName; 84 protected static int chartCount = 0; 85 86 protected static Log logger = null; 87 88 public AbstractChartComponent(String definitionPath, int width, int height, IPentahoUrlFactory urlFactory, List messages) { 89 this(urlFactory, messages); 90 this.definitionPath = definitionPath; 91 this.width = width; 92 this.height = height; 93 String info[] = PentahoSystem.parseActionString(definitionPath); 94 if (info != null && info.length == 3) { 95 setSourcePath(info[0] + File.separator + info[2]); 96 } 97 } 98 99 104 public AbstractChartComponent(String definitionPath, IPentahoUrlFactory urlFactory, ArrayList messages) { 105 this(urlFactory, messages); 106 this.definitionPath = definitionPath; 107 String info[] = PentahoSystem.parseActionString(definitionPath); 108 if (info != null && info.length == 3) { 109 setSourcePath(info[0] + File.separator + info[2]); 110 } 111 } 112 113 public AbstractChartComponent(IPentahoUrlFactory urlFactory, List messages) { 114 super(urlFactory, messages, null); 115 logger = LogFactory.getLog(this.getClass()); 116 } 117 118 124 public abstract boolean setDataAction(String chartDefinition); 125 126 134 public void setDataAction(String solution, String actionPath, String actionName, String actionOutput) { 135 this.solution = solution; 136 this.actionPath = actionPath; 137 this.actionName = actionName; 138 this.actionOutput = actionOutput; 139 } 140 141 146 public IPentahoResultSet getActionData() { 147 150 ISolutionEngine solutionEngine = PentahoSystem.getSolutionEngineInstance(getSession()); 151 solutionEngine.setLoggingLevel(ILogger.DEBUG); 152 solutionEngine.init(getSession()); 153 154 HashMap parameterProviders = getParameterProviders(); 155 156 OutputStream outputStream = null; 157 SimpleOutputHandler outputHandler = null; 158 outputHandler = new SimpleOutputHandler(outputStream, false); 159 160 ArrayList messages = new ArrayList (); 161 String processId = this.getClass().getName(); 162 context = solutionEngine.execute(solution, actionPath, actionName, processId, false, true, instanceId, false, parameterProviders, outputHandler, null, urlFactory, messages); 163 164 if( context == null ) { 165 return null; 167 } 168 169 if (actionOutput != null) { 170 if (context.getOutputNames().contains(actionOutput)) { 171 IActionParameter output = context.getOutputParameter(actionOutput); 172 IPentahoResultSet results = output.getValueAsResultSet(); 173 if (results != null) { 174 results = results.memoryCopy(); 175 } 176 return results; 177 } else { 178 return null; 180 } 181 } else { 182 try { 183 Iterator it = context.getOutputNames().iterator(); 185 while (it.hasNext()) { 186 IActionParameter output = (IActionParameter) it.next(); 187 if (output.getType().equalsIgnoreCase(IActionParameter.TYPE_RESULT_SET)) { 188 IPentahoResultSet results = output.getValueAsResultSet(); 189 if (results != null) { 190 results = results.memoryCopy(); 191 } 192 return results; 193 } 194 } 195 } catch (Exception e) { 196 197 } 198 } 199 return null; 200 } 201 202 public Log getLogger() { 203 return logger; 204 } 205 206 209 protected String [] createTempFile() { 210 String solutionDir = "system/tmp/"; String fileNamePrefix = "tmp_chart_"; String extension = ".png"; String fileName = null; 215 String filePathWithoutExtension = null; 216 try { 217 File file = File.createTempFile(fileNamePrefix, extension, new File (PentahoSystem.getApplicationContext().getFileOutputPath(solutionDir))); 218 fileName = file.getName(); 220 filePathWithoutExtension = solutionDir + fileName.substring(0, fileName.indexOf('.')); 221 } catch (IOException e) { 222 e.printStackTrace(); 224 } 225 String [] value = new String [2]; 226 value[FILENAME_INDEX] = fileName; 227 value[FILENAME_WITHOUT_EXTENSION_INDEX] = filePathWithoutExtension; 228 229 return value; 230 } 231 232 protected void applyOuterURLTemplateParam() { 233 if (outerParamNames == null) { 234 return; 235 } 236 Iterator iter = outerParamNames.iterator(); 237 while (iter.hasNext()) { 238 String outerParamName = iter.next().toString(); 239 Object value = null; 240 if (context != null && context.getInputNames().contains(outerParamName)) { 241 value = context.getInputParameterValue(outerParamName); 242 } 243 if (value == null) { 244 return; 245 } 246 try { 247 if (value.getClass().isArray()) { 248 if ( Array.getLength( value ) > 0 ) { 249 String [] encodedVals = new String [ Array.getLength( value ) ]; 250 for ( int i = 0; i < Array.getLength( value ); ++i ) { 251 encodedVals[i] = URLEncoder.encode( Array.get( value, i ).toString(), LocaleHelper.getSystemEncoding() ); 252 } 253 urlTemplate = TemplateUtil.applyTemplate(urlTemplate, outerParamName, encodedVals); 261 } 262 } 263 else { 264 String encodedVal = URLEncoder.encode( value.toString(), LocaleHelper.getSystemEncoding() ); 265 urlTemplate = TemplateUtil.applyTemplate(urlTemplate, outerParamName, encodedVal); 266 } 267 268 } catch (UnsupportedEncodingException e) { 270 e.printStackTrace(); 272 } 273 } 274 } 275 276 279 public void dispose() { 280 if (context != null) 281 context.dispose(); 282 } 283 284 287 public String getActionName() { 288 return actionName; 289 } 290 291 295 public void setActionName(String actionName) { 296 this.actionName = actionName; 297 } 298 299 302 public String getActionOutput() { 303 return actionOutput; 304 } 305 306 310 public void setActionOutput(String actionOutput) { 311 this.actionOutput = actionOutput; 312 } 313 314 317 public String getActionPath() { 318 return actionPath; 319 } 320 321 325 public void setActionPath(String actionPath) { 326 this.actionPath = actionPath; 327 } 328 329 332 public IRuntimeContext getContext() { 333 return context; 334 } 335 336 340 public void setContext(IRuntimeContext context) { 341 this.context = context; 342 } 343 344 347 public String getDefinitionPath() { 348 return definitionPath; 349 } 350 351 355 public void setDefinitionPath(String definitionPath) { 356 this.definitionPath = definitionPath; 357 } 358 359 362 public int getHeight() { 363 return height; 364 } 365 366 370 public void setHeight(int height) { 371 this.height = height; 372 } 373 374 377 public String getInstanceId() { 378 return instanceId; 379 } 380 381 385 public void setInstanceId(String instanceId) { 386 this.instanceId = instanceId; 387 } 388 389 392 public String getSolution() { 393 return solution; 394 } 395 396 400 public void setSolution(String solution) { 401 this.solution = solution; 402 } 403 404 407 public String getTitle() { 408 return title; 409 } 410 411 415 public void setTitle(String title) { 416 this.title = title; 417 } 418 419 422 public String getUrlTemplate() { 423 return urlTemplate; 424 } 425 426 430 public void setUrlTemplate(String urlTemplate) { 431 this.urlTemplate = urlTemplate; 432 } 433 434 437 public Object getValues() { 438 return values; 439 } 440 441 445 public void setValues(Object values) { 446 this.values = values; 447 } 448 449 452 public int getWidth() { 453 return width; 454 } 455 456 460 public void setWidth(int width) { 461 this.width = width; 462 } 463 464 468 public void setLogger(Log logger) { 469 AbstractChartComponent.logger = logger; 470 } 471 472 475 public boolean isByRow() { 476 return byRow; 477 } 478 479 483 public void setByRow(boolean byRow) { 484 this.byRow = byRow; 485 } 486 487 490 public String getParamName() { 491 return paramName; 492 } 493 494 498 public void setParamName(String paramName) { 499 this.paramName = paramName; 500 } 501 502 505 public List getOuterParamNames() { 506 return outerParamNames; 507 } 508 509 513 public void addOuterParamName(String outerParamName) { 514 outerParamNames.add(outerParamName); 515 } 516 517 } 518 | Popular Tags |