1 16 package org.pentaho.plugin; 17 18 import java.io.InputStream ; 19 import java.io.OutputStream ; 20 import java.util.HashMap ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 import java.util.Map ; 24 import java.util.Set ; 25 import java.util.regex.Matcher ; 26 27 import javax.activation.DataSource ; 28 import org.dom4j.Node; 29 import org.pentaho.core.component.IComponent; 30 import org.pentaho.core.repository.IContentItem; 31 import org.pentaho.core.runtime.ActionParameter; 32 import org.pentaho.core.runtime.IActionParameter; 33 import org.pentaho.core.runtime.IRuntimeContext; 34 import org.pentaho.core.runtime.InvalidParameterException; 35 import org.pentaho.core.runtime.SelectionMapper; 36 import org.pentaho.core.session.IPentahoSession; 37 import org.pentaho.core.solution.IActionResource; 38 import org.pentaho.core.system.PentahoMessenger; 39 import org.pentaho.core.system.PentahoSystem; 40 import org.pentaho.core.util.IParameterResolver; 41 import org.pentaho.messages.Messages; 42 import org.pentaho.plugin.core.StandardSettings; 43 import org.pentaho.util.logging.ILogger; 44 45 47 public abstract class ComponentBase extends PentahoMessenger implements IComponent, IParameterResolver { 48 49 protected static final String UNKNOWN_COMPONENT_ID = "unknown"; 51 public static final String MISSING_SESSION = "session missing"; 53 public static final String COMPONENT_EXECUTE_FAIL = "component failed"; 55 protected static final boolean debug = PentahoSystem.debug; 56 57 private IRuntimeContext runtimeContext; 58 59 private IPentahoSession sessionContext; 60 61 private String processId; 62 63 private String actionName; 64 65 private String instanceId; 66 67 private String id; 68 69 private boolean baseInitOk; 70 71 private boolean componentInitOk; 72 73 private String logId; 75 76 private Node componentDefinition; 77 78 private HashMap settings = new HashMap (); 79 80 public void setInstanceId( String instanceId ) { 81 this.instanceId = instanceId; 82 } 83 84 public String getInstanceId() { 85 return instanceId; 86 } 87 88 public void setActionName( String actionName ) { 89 this.actionName = actionName; 90 } 91 92 public String getActionName( ) { 93 return actionName; 94 } 95 96 public void setProcessId( String processId ) { 97 this.processId = processId; 98 } 99 100 public String getProcessId( ) { 101 return processId; 102 } 103 104 public void setComponentDefinition( Node componentDefinition ) { 105 this.componentDefinition = componentDefinition; 106 } 107 108 public Node getComponentDefinition() { 109 return getComponentDefinition( false ); 110 } 111 112 public Node getComponentDefinition( boolean process ) { 113 if( process ) { 114 List nodes = componentDefinition.selectNodes("//*"); Iterator it = nodes.iterator(); 116 while( it.hasNext() ) { 117 Node node = (Node) it.next(); 118 String txt = node.getText(); 119 if( txt != null && !node.isReadOnly() ) { 120 node.setText( applyInputsToFormat( txt ) ); 121 } 122 } 123 } 124 return componentDefinition; 125 } 126 127 public void setRuntimeContext( IRuntimeContext runtimeContext ) { 128 this.runtimeContext = runtimeContext; 129 } 130 131 public IRuntimeContext getRuntimeContext( ) { 132 return runtimeContext; 133 } 134 135 public void setSession( IPentahoSession session ) { 136 this.sessionContext = session; 137 } 138 139 public IPentahoSession getSession( ) { 140 return sessionContext; 141 } 142 143 protected void saveSetting(String name, Object value) { 144 settings.put(name, value); 145 } 146 147 protected Object getSetting(String name) { 148 return settings.get(name); 149 } 150 151 protected String getStringSetting(String name) { 152 Object value = settings.get(name); 153 if (value == null) { 154 return null; 155 } else if (value instanceof String ) { 156 return (String ) value; 157 } else { 158 return value.toString(); 159 } 160 } 161 162 protected abstract boolean validateAction(); 163 164 protected abstract boolean validateSystemSettings(); 165 166 public abstract void done(); 167 168 protected abstract boolean executeAction() throws Throwable ; 169 170 public abstract boolean init(); 171 172 public String getLogId() { 173 return logId; 174 } 175 176 protected boolean isDefinedInput(String inputName) { 177 178 if (runtimeContext.getInputNames().contains(inputName)) { 179 return true; 180 } else { 181 return getComponentSetting(inputName) != null; 182 } 183 } 184 185 protected boolean isDefinedOutput(String outputName) { 186 return runtimeContext.getOutputNames().contains(outputName); 187 } 188 189 protected boolean isDefinedResource(String resourceName) { 190 return runtimeContext.getResourceNames().contains(resourceName); 191 } 192 193 public final int validate() { 194 195 logId = Messages.getString("Base.CODE_LOG_ID", instanceId, runtimeContext.getHandle(), actionName); if (debug) 197 debug(Messages.getString("Base.DEBUG_VALIDATING_COMPONENT", actionName)); 200 id = Messages.getString("Base.CODE_COMPONENT_ID", processId, actionName); 202 baseInitOk = (instanceId != null && sessionContext != null && processId != null && actionName != null); 204 205 boolean systemSettingsValidate = validateSystemSettings(); 206 207 if (baseInitOk && systemSettingsValidate) { 208 try { 209 componentInitOk = validateAction(); 210 } 211 catch ( Exception e ) { 212 error(Messages.getErrorString("Base.ERROR_0004_VALIDATION_FAILED"), e); } 214 } 215 if (getInitOk()) { 216 return IRuntimeContext.RUNTIME_CONTEXT_VALIDATE_OK; 217 } 218 return IRuntimeContext.RUNTIME_CONTEXT_VALIDATE_FAIL; 219 } 220 221 public int resolveParameter(String template, String parameterName, Matcher parameterMatcher, int copyStart, StringBuffer result) { 222 return -1; 224 } 225 226 public boolean getInitOk() { 227 return baseInitOk && componentInitOk; 228 } 229 230 protected Set getOutputNames() { 231 return runtimeContext.getOutputNames(); 232 } 233 234 protected Set getInputNames() { 235 return runtimeContext.getInputNames(); 236 } 237 238 protected Set getResourceNames() { 239 return runtimeContext.getResourceNames(); 240 } 241 242 protected boolean feedbackAllowed() { 243 return runtimeContext.feedbackAllowed(); 244 } 245 246 protected IActionResource getResource(String resourceName) { 247 return runtimeContext.getResourceDefintion(resourceName); 248 } 249 250 protected InputStream getResourceInputStream(IActionResource resource) { 251 return runtimeContext.getResourceInputStream(resource); 252 } 253 254 protected InputStream getInputStream(String inputName) { 255 return runtimeContext.getInputStream(inputName); 256 } 257 258 protected int getOutputPreference() { 259 return runtimeContext.getOutputPreference(); 260 } 261 262 protected OutputStream getOutputStream(String outputName, String mimeType, String extension) { 263 return runtimeContext.getOutputStream(outputName, mimeType, extension); 264 } 265 266 protected void audit(String messageType, String message, String value, int duration) { 267 runtimeContext.audit(messageType, message, value, duration); 268 } 269 270 protected boolean getInputBooleanValue(String inputName, boolean defaultValue) { 271 String strValue = getInputStringValue(inputName); 272 if (strValue == null) { 273 return defaultValue; 274 } else if ("true".equalsIgnoreCase(strValue)) { return true; 276 } else if ("false".equalsIgnoreCase(strValue)) { return false; 278 } else { 279 return defaultValue; 280 } 281 282 } 283 284 protected long getInputLongValue(String inputName, long defaultValue) { 285 String strValue = getInputStringValue(inputName); 286 if (strValue == null) { 287 return defaultValue; 288 } 289 try { 290 return Long.parseLong(strValue); 291 } catch (Exception e) { 292 return defaultValue; 293 } 294 295 } 296 297 protected String getInputStringValue(String inputName) { 298 String value = null; 301 if (runtimeContext.getInputNames().contains(inputName)) { 302 value = runtimeContext.getInputParameterStringValue(inputName); 303 } else { 304 Node node = componentDefinition.selectSingleNode(inputName); 306 if (node == null) { 307 return null; 308 } 309 value = node.getText(); 310 } 311 if (value != null) { 312 value = this.applyInputsToFormat(value); 314 } 315 return value; 316 } 317 318 protected Object getInputValue(String inputName) { 319 if (runtimeContext.getInputNames().contains(inputName)) { 322 return runtimeContext.getInputParameterValue(inputName); 323 } 324 Node node = componentDefinition.selectSingleNode(inputName); 326 if (node == null) { 327 return null; 328 } 329 return node.getText(); 330 } 331 332 private String getComponentSetting(String path) { 333 if (runtimeContext.getInputNames().contains(path)) { 336 return runtimeContext.getInputParameterStringValue(path); 337 } 338 Node node = componentDefinition.selectSingleNode(path); 340 if (node == null) { 341 return null; 342 } 343 return node.getText(); 344 } 345 346 public void promptNeeded() { 347 runtimeContext.promptNeeded(); 348 } 349 350 public void promptNow() { 351 runtimeContext.promptNow(); 352 } 353 354 public String getResourceAsString(IActionResource resource) { 355 try { 356 return runtimeContext.getResourceAsString(resource); 357 } catch (Exception e) { 358 return null; 359 } 360 } 361 362 365 public String getInitFailMessage() { 366 return null; 368 } 369 370 public String createNewInstance(boolean persisted, Map parameters, boolean forceImmediateWrite) { 371 return runtimeContext.createNewInstance(persisted, parameters, forceImmediateWrite); 372 } 373 374 public void inputMissingError(String paramName) { 375 error(Messages.getErrorString("ComponentBase.ERROR_0003_INPUT_PARAM_MISSING", paramName)); } 377 378 public void outputMissingError(String paramName) { 379 error(Messages.getErrorString("ComponentBase.ERROR_0004_OUTPUT_PARAM_MISSING", paramName)); } 381 382 public void resourceMissingError(String paramName) { 383 error(Messages.getErrorString("ComponentBase.ERROR_0005_RESOURCE_PARAM_MISSING", paramName)); } 385 386 public void resourceComponentSettingError(String paramName) { 387 error(Messages.getErrorString("ComponentBase.ERROR_0006_COMPONENT_SETTING_PARAM_MISSING", paramName)); } 389 390 public int execute() { 391 392 String xsl = getComponentSetting("xsl"); if (xsl != null) { 395 runtimeContext.setParameterXsl(xsl); 396 } 397 398 String target = getComponentSetting("target"); if (target != null) { 401 runtimeContext.setParameterTarget(target); 402 } 403 404 if (loggingLevel == UNKNOWN) { 405 warn(Messages.getString("Base.WARNING_LOGGING_LEVEL_UNKNOWN")); loggingLevel = ILogger.DEBUG; 407 } 408 int result = IRuntimeContext.RUNTIME_STATUS_FAILURE; 409 410 if (sessionContext == null) { 411 error(Messages.getErrorString("Base.ERROR_0001_INVALID_SESSION")); return result; 413 } 414 415 if (debug) 416 debug(Messages.getString("Base.DEBUG_VALIDATION_RESULT") + getInitOk()); if (!getInitOk()) { 418 return result; 419 } 420 421 try { 422 result = (executeAction() ? IRuntimeContext.RUNTIME_STATUS_SUCCESS : IRuntimeContext.RUNTIME_STATUS_FAILURE); 423 if (result == IRuntimeContext.RUNTIME_STATUS_SUCCESS && runtimeContext.isPromptPending()) { 424 if (isDefinedInput(StandardSettings.HANDLE_ALL_PROMPTS)) { 426 runtimeContext.promptNow(); 427 } 428 } 429 } 430 catch (InvalidParameterException e) { 431 error(Messages.getErrorString("Base.ERROR_0002_EXECUTION_FAILED") ); } 434 catch (Throwable e) { 435 error(Messages.getErrorString("Base.ERROR_0002_EXECUTION_FAILED"), e); } 437 return result; 438 } 439 440 public String getObjectName() { 441 return this.getClass().getName(); 442 } 443 444 public String getId() { 445 return id; 446 } 447 448 public String getActionTitle() { 449 return runtimeContext.getActionTitle(); 450 } 451 452 456 protected IContentItem getOutputContentItem() { 457 return runtimeContext.getOutputContentItem(); 458 } 459 460 protected IContentItem getOutputContentItem(String outputName) { 461 return runtimeContext.getOutputContentItem(outputName); 462 } 463 464 protected void setOutputValue(String outputName, Object value) { 465 runtimeContext.setOutputValue(outputName, value); 466 } 467 468 protected void addTempParameter( String name, IActionParameter param ) { 469 runtimeContext.addTempParameter( name, param ); 470 } 471 472 protected void addTempParameterObject( String name, Object paramObject ) { 473 String pType = "object"; IActionParameter actionParameter = new ActionParameter(name, 475 pType, paramObject, null, null); 476 addTempParameter(name, actionParameter); 477 } 478 479 484 protected OutputStream getDefaultOutputStream() { 485 IContentItem contentItem = runtimeContext.getOutputContentItem(); 486 if (contentItem != null) { 487 try { 488 return contentItem.getOutputStream(getActionName()); 489 } catch (Exception e) { 490 } 491 } 492 return null; 493 } 494 495 protected String applyInputsToFormat(String format) { 496 return runtimeContext.applyInputsToFormat(format, this); 497 } 498 499 protected IActionParameter getOutputItem(String outputName) { 500 return runtimeContext.getOutputParameter(outputName); 501 } 502 503 protected String getSolutionName() { 504 return runtimeContext.getSolutionName(); 505 } 506 507 protected String getSolutionPath() { 508 return runtimeContext.getSolutionPath(); 509 } 510 511 protected IActionParameter getInputParameter(String parameterName) { 512 return runtimeContext.getInputParameter(parameterName); 513 } 514 515 protected String getContentUrl(IContentItem contentItem) { 516 return runtimeContext.getContentUrl(contentItem); 517 } 518 519 protected boolean isPromptPending() { 520 return runtimeContext.isPromptPending(); 521 } 522 523 protected void setFeedbackMimeType(String mimeType) { 524 IContentItem feedbackContentItem = runtimeContext.getFeedbackContentItem(); 525 feedbackContentItem.setMimeType(mimeType); 526 } 527 528 533 protected void setOutputMimeType(String mimeType) { 534 IContentItem outputContentItem = runtimeContext.getOutputContentItem(); 535 outputContentItem.setMimeType(mimeType); 536 } 537 538 protected void setOutputMimeType(String outputName, String mimeType) { 539 IContentItem outputContentItem = runtimeContext.getOutputContentItem(outputName); 540 outputContentItem.setMimeType(mimeType); 541 } 542 543 protected OutputStream getFeedbackOutputStream() { 544 IContentItem feedbackContentItem = runtimeContext.getFeedbackContentItem(); 545 if (feedbackContentItem != null) { 546 try { 547 return feedbackContentItem.getOutputStream(getActionName()); 548 } catch (Exception e) { 549 } 550 } 551 return null; 552 } 553 554 558 protected void createFeedbackParameter(IActionParameter actionParam) { 559 runtimeContext.createFeedbackParameter(actionParam); 560 runtimeContext.promptNeeded(); 561 } 562 563 protected void createFeedbackParameter(SelectionMapper selMap, String fieldName, Object defaultValues) { 564 runtimeContext.createFeedbackParameter(selMap, fieldName, defaultValues); 565 runtimeContext.promptNeeded(); 566 } 567 568 protected void createFeedbackParameter(String fieldName, String displayName, String hint, String defaultValue, boolean visible) { 569 runtimeContext.createFeedbackParameter(fieldName, displayName, hint, defaultValue, visible); 570 runtimeContext.promptNeeded(); 571 } 572 573 public void createFeedbackParameter(String fieldName, String displayName, String hint, Object defaultValues, List values, Map dispNames, String displayStyle) { 574 runtimeContext.createFeedbackParameter(fieldName, displayName, hint, defaultValues, values, dispNames, displayStyle); 575 runtimeContext.promptNeeded(); 576 } 577 578 protected DataSource getDataSource(String parameterName) { 579 return runtimeContext.getDataSource(parameterName); 580 } 581 582 protected DataSource getResourceDataSource(IActionResource resource) { 583 return runtimeContext.getResourceDataSource(resource); 584 } 585 } 586 | Popular Tags |