1 13 19 package org.pentaho.core.runtime; 20 21 import java.io.File ; 22 import java.io.IOException ; 23 import java.io.InputStream ; 24 import java.io.OutputStream ; 25 import java.io.Reader ; 26 import java.lang.reflect.Array ; 27 import java.math.BigDecimal ; 28 import java.util.ArrayList ; 29 import java.util.Date ; 30 import java.util.HashMap ; 31 import java.util.Iterator ; 32 import java.util.List ; 33 import java.util.Map ; 34 import java.util.Set ; 35 import java.util.regex.Matcher ; 36 import java.util.regex.Pattern ; 37 import javax.activation.DataSource ; 38 import org.apache.commons.logging.Log; 39 import org.apache.commons.logging.LogFactory; 40 import org.dom4j.Document; 41 import org.dom4j.Element; 42 import org.dom4j.Node; 43 import org.pentaho.core.audit.AuditHelper; 44 import org.pentaho.core.audit.MessageTypes; 45 import org.pentaho.core.component.IComponent; 46 import org.pentaho.core.connection.IPentahoResultSet; 47 import org.pentaho.core.repository.IContentItem; 48 import org.pentaho.core.repository.IContentLocation; 49 import org.pentaho.core.repository.IContentRepository; 50 import org.pentaho.core.repository.IRuntimeElement; 51 import org.pentaho.core.repository.IRuntimeRepository; 52 import org.pentaho.core.repository.content.ContentRepositoryOutputHandler; 53 import org.pentaho.core.session.IPentahoSession; 54 import org.pentaho.core.solution.ActionParameterSource; 55 import org.pentaho.core.solution.ConditionalExecution; 56 import org.pentaho.core.solution.IActionCompleteListener; 57 import org.pentaho.core.solution.IActionDefinition; 58 import org.pentaho.core.solution.IActionResource; 59 import org.pentaho.core.solution.IActionSequence; 60 import org.pentaho.core.solution.IOutputHandler; 61 import org.pentaho.core.solution.IParameterProvider; 62 import org.pentaho.core.solution.ISolutionEngine; 63 import org.pentaho.core.system.PentahoMessenger; 64 import org.pentaho.core.system.PentahoSystem; 65 import org.pentaho.core.ui.IPentahoUrlFactory; 66 import org.pentaho.core.util.IParameterResolver; 67 import org.pentaho.core.util.TemplateUtil; 68 import org.pentaho.core.util.XForm; 69 import org.pentaho.core.util.XmlHelper; 70 import org.pentaho.messages.Messages; 71 import org.pentaho.messages.util.LocaleHelper; 72 import org.pentaho.util.logging.Logger; 73 74 80 public class RuntimeContext extends PentahoMessenger implements IRuntimeContext { 81 82 85 private static final long serialVersionUID = -1179016850860938879L; 86 87 public static final String PARAMS_REQUEST = "request"; public static final String PARAMS_SESSION = "session"; 90 private IRuntimeElement runtimeData; 91 92 private static String LOG_NAME = "RUNTIME"; 95 protected static String PARAMETER_FORM = "actionparam"; 97 private String logId; 98 99 private IPentahoSession session; 100 101 protected ISolutionEngine solutionEngine; 102 103 private int errorLevel = RUNTIME_CONTEXT_RESOLVE_OK; 104 105 protected StringBuffer xformHeader; 106 107 protected StringBuffer xformBody; 108 109 protected Map xformFields; 110 111 protected String parameterXsl = "DefaultParameterForm.xsl"; 113 protected String parameterTemplate = null; 114 115 protected String parameterTarget; 116 117 private String instanceId; 118 119 private String processId; 120 121 private String handle; 122 123 private String solutionName; 124 125 protected IPentahoUrlFactory urlFactory; 126 127 protected Map parameterProviders; 128 129 protected Map componentClassMap; 130 131 protected IActionSequence actionSequence; 132 133 public static final boolean debug = PentahoSystem.debug; 134 135 private boolean audit = true; 136 137 private int status; 138 139 protected IOutputHandler outputHandler; 140 141 protected ParameterManager paramManager; 142 143 private String currentComponent; 144 145 private IActionDefinition currentActionDef; 146 147 private int promptStatus = PROMPT_NO; 148 149 private static final byte[] PATTERN_COMPILE_LOCK = new byte[0]; 154 155 private static final Log logger = LogFactory.getLog(RuntimeContext.class); 156 157 public Log getLogger() { 158 return logger; 159 } 160 161 166 public RuntimeContext(String instanceId, ISolutionEngine solutionEngine, String solutionName, IRuntimeElement runtimeData, IPentahoSession session, IOutputHandler outputHandler, String processId, IPentahoUrlFactory urlFactory, Map parameterProviders, 167 List messages) { 168 this.instanceId = instanceId; 169 this.solutionEngine = solutionEngine; 170 this.session = session; 171 this.outputHandler = outputHandler; 172 this.processId = processId; 173 this.solutionName = solutionName; 174 this.urlFactory = urlFactory; 175 this.parameterProviders = parameterProviders; 176 setMessages(messages); 177 xformHeader = new StringBuffer (); 178 xformBody = new StringBuffer (); 179 xformFields = new HashMap (); 180 182 this.currentComponent = ""; this.currentActionDef = null; 184 185 status = RUNTIME_STATUS_NOT_STARTED; 186 187 this.runtimeData = runtimeData; 188 if (runtimeData != null) { 189 this.instanceId = runtimeData.getInstanceId(); 190 } 191 192 handle = "context-" + this.hashCode() + "-" + new Date ().getTime(); 194 logId = ((instanceId != null) ? instanceId : solutionName) + ":" + LOG_NAME + ":" + handle + " "; 196 paramManager = new ParameterManager(); 197 198 parameterXsl = "DefaultParameterForm.xsl"; 201 } 202 203 private IRuntimeElement createChild(boolean persisted) { 204 try { 205 IRuntimeRepository runtimeRepository = PentahoSystem.getRuntimeRepository( session ); 206 runtimeRepository.setLoggingLevel(loggingLevel); 208 IRuntimeElement childRuntimeData = runtimeRepository.newRuntimeElement(instanceId, "instance", !persisted); String childInstanceId = childRuntimeData.getInstanceId(); 210 AuditHelper.audit(instanceId, session.getName(), getActionName(), getObjectName(), processId, MessageTypes.INSTANCE_START, childInstanceId, "", null, this); return childRuntimeData; 213 } catch (Exception e) { 214 error(Messages.getString("RuntimeContext.ERROR_0027_COULD_NOT_CREATE_CHILD"), e); return null; 216 } 217 } 218 219 public String createNewInstance(boolean persisted) { 220 try { 221 IRuntimeElement childRuntimeData = createChild(persisted); 222 String childInstanceId = childRuntimeData.getInstanceId(); 223 return childInstanceId; 224 } catch (Exception e) { 225 error(Messages.getString("RuntimeContext.ERROR_0027_COULD_NOT_CREATE_CHILD"), e); return null; 227 } 228 } 229 230 public String createNewInstance(boolean persisted, Map parameters) { 231 return createNewInstance(persisted, parameters, false); 232 } 233 234 public String createNewInstance(boolean persisted, Map parameters, boolean forceImmediateWrite) { 235 try { 236 IRuntimeElement childRuntimeData = createChild(persisted); 237 if (parameters != null) { 238 Iterator parameterIterator = parameters.keySet().iterator(); 239 while (parameterIterator.hasNext()) { 240 String parameterName = (String ) parameterIterator.next(); 241 Object parameterValue = parameters.get(parameterName); 242 if (parameterValue instanceof String ) { 243 childRuntimeData.setStringProperty(parameterName, (String ) parameterValue); 244 } else if (parameterValue instanceof BigDecimal ) { 245 childRuntimeData.setBigDecimalProperty(parameterName, (BigDecimal ) parameterValue); 246 } else if (parameterValue instanceof Date ) { 247 childRuntimeData.setDateProperty(parameterName, (Date ) parameterValue); 248 } else if (parameterValue instanceof List ) { 249 childRuntimeData.setListProperty(parameterName, (List ) parameterValue); 250 } else if (parameterValue instanceof Long ) { 251 childRuntimeData.setLongProperty(parameterName, (Long ) parameterValue); 252 } 253 } 254 } 255 String childInstanceId = childRuntimeData.getInstanceId(); 256 if (forceImmediateWrite) { 257 childRuntimeData.forceSave(); 258 } 259 return childInstanceId; 260 } catch (Exception e) { 261 error(Messages.getString("RuntimeContext.ERROR_0027_COULD_NOT_CREATE_CHILD"), e); return null; 263 } 264 265 } 266 267 public int getStatus() { 268 return status; 269 } 270 271 public void promptNow() { 272 promptStatus = PROMPT_NOW; 273 } 274 275 276 public void promptNeeded() { 277 if (promptStatus < PROMPT_WAITING) { promptStatus = PROMPT_WAITING; 279 } 280 } 281 282 287 public boolean isPromptPending() { 288 return (promptStatus != PROMPT_NO); 289 } 290 291 public IPentahoUrlFactory getUrlFactory() { 292 return urlFactory; 293 } 294 295 public boolean feedbackAllowed() { 296 return outputHandler != null && outputHandler.allowFeedback(); 297 } 298 299 public IContentItem getFeedbackContentItem() { 300 return outputHandler.getFeedbackContentItem(); 301 } 302 303 public IContentItem getOutputContentItem() { 304 return outputHandler.getOutputContentItem(IOutputHandler.RESPONSE, IOutputHandler.CONTENT, actionSequence.getTitle(), null); 306 } 307 308 public IContentItem getOutputContentItem(String outputName) { 309 310 IActionParameter parameter = (IActionParameter) actionSequence.getOutputDefinitions().get(outputName); 311 if (parameter == null) { 312 error(Messages.getErrorString("RuntimeContext.ERROR_0021_INVALID_OUTPUT_REQUEST", outputName, actionSequence.getSequenceName())); throw new InvalidParameterException(); 314 } 315 316 List destinationsList = parameter.getVariables(); 317 Iterator destinationsIterator = destinationsList.iterator(); 318 if (destinationsIterator.hasNext()) { 320 ActionParameterSource destination = (ActionParameterSource) destinationsIterator.next(); 321 String objectName = destination.getSourceName(); 322 String contentName = destination.getValue(); 323 contentName = TemplateUtil.applyTemplate(contentName, this); 324 return outputHandler.getOutputContentItem(objectName, contentName, actionSequence.getTitle(), null); 325 } 326 return null; 327 } 328 329 public String getHandle() { 330 return handle; 331 } 332 333 public IPentahoSession getSession() { 334 return session; 335 } 336 337 public String getSolutionName() { 338 return (solutionName); 339 } 340 341 public String getSolutionPath() { 342 return ((actionSequence != null) ? actionSequence.getSolutionPath() : null); 343 } 344 345 public String getCurrentComponentName() { 346 if ("".equals(currentComponent)) { return this.getClass().getName(); 348 } 349 return currentComponent; 350 } 351 352 public String getInstanceId() { 353 return instanceId; 354 } 355 356 public int getErrorLevel() { 357 return errorLevel; 358 } 359 360 public void setActionSequence( IActionSequence sequence ) { 361 this.actionSequence = sequence; 362 paramManager = new ParameterManager( sequence ); 363 } 364 365 public int validateSequence(String sequenceName) { 366 paramManager.resetParameters(); 367 368 logId = ((instanceId != null) ? instanceId : solutionName) + ":" + LOG_NAME + ":" + handle + ":" + sequenceName + " "; if (audit) { 370 audit(MessageTypes.ACTION_SEQUENCE_START, MessageTypes.START, "", 0); } 372 373 if (status != RUNTIME_STATUS_NOT_STARTED) { 374 error(Messages.getErrorString("RuntimeContext.ERROR_0001_RUNTIME_RUNNING")); return (status); 376 } 377 378 errorLevel = RUNTIME_CONTEXT_VALIDATE_OK; 379 380 errorLevel = validateHeader(sequenceName); 382 if (errorLevel != RUNTIME_CONTEXT_VALIDATE_OK) { 383 error(Messages.getErrorString("RuntimeContext.ERROR_0002_ACTION_NOT_VALIDATED", sequenceName)); status = RUNTIME_CONTEXT_VALIDATE_FAIL; 385 return errorLevel; 386 } 387 388 390 errorLevel = validateResources(); 392 if (errorLevel != RUNTIME_CONTEXT_VALIDATE_OK) { 393 error(Messages.getErrorString("RuntimeContext.ERROR_0005_ACTION_RESOURCES_NOT_VALID", sequenceName)); status = RUNTIME_CONTEXT_VALIDATE_FAIL; 395 return errorLevel; 396 } 397 398 errorLevel = validateComponents(); 400 if (errorLevel != RUNTIME_CONTEXT_VALIDATE_OK) { 401 error(Messages.getErrorString("RuntimeContext.ERROR_0006_ACTION_COMPONENT_NOT_VALID", sequenceName)); status = RUNTIME_CONTEXT_VALIDATE_FAIL; 403 return errorLevel; 404 } 405 406 status = RUNTIME_CONTEXT_VALIDATE_OK; 407 408 return RUNTIME_CONTEXT_VALIDATE_OK; 409 } 410 411 private int validateHeader(String sequenceName) { 412 413 417 418 errorLevel = initFromActionSequenceDefinition(); 420 if (errorLevel != RUNTIME_CONTEXT_RESOLVE_OK) { 421 error(Messages.getErrorString("RuntimeContext.ERROR_0008_ACTION_INITIALIZATION_FAILED", sequenceName)); return errorLevel; 423 } 424 425 return RUNTIME_CONTEXT_VALIDATE_OK; 426 } 427 428 private int validateResources() { 429 430 return RUNTIME_CONTEXT_VALIDATE_OK; 432 } 433 434 private int validateComponents() { 435 return ( validateComponents(actionSequence) ); 436 } 437 438 private int validateComponents(IActionSequence sequence) { 439 440 List defList = sequence.getActionDefinitionsAndSequences(); 441 442 Object listItem; 443 for (Iterator it = defList.iterator(); it.hasNext();) { 444 listItem = it.next(); 445 446 if (listItem instanceof IActionSequence) { 447 int rtn = validateComponents((IActionSequence) listItem); 448 if (rtn != RUNTIME_CONTEXT_VALIDATE_OK) { 449 return (rtn); 450 } 451 } else if (listItem instanceof IActionDefinition) { 452 IActionDefinition actionDef = (IActionDefinition) listItem; 453 454 if (debug) 455 debug(Messages.getString("RuntimeContext.DEBUG_VALIDATING_COMPONENT", actionDef.getComponentName())); 457 IComponent component = resolveComponent(actionDef); 458 if (component != null) { 459 component.setLoggingLevel(loggingLevel); 460 461 actionDef.setComponent(component); 463 paramManager.setCurrentParameters(actionDef); 464 int stat = component.validate(); 469 if (stat != RUNTIME_CONTEXT_VALIDATE_OK) { 470 return (stat); 471 } 472 paramManager.addOutputParameters(actionDef); 473 } else { 474 return RUNTIME_CONTEXT_VALIDATE_FAIL; 475 } 476 currentComponent = ""; currentActionDef = null; 478 } 479 } 480 return (RUNTIME_CONTEXT_VALIDATE_OK); 481 } 482 483 protected Map getComponentClassMap() { 484 if (componentClassMap == null) { 485 componentClassMap = createComponentClassMap(); 486 } 487 return componentClassMap; 488 } 489 490 protected Map createComponentClassMap() { 491 componentClassMap = new HashMap (); 492 componentClassMap.put("ContentOutputComponent", "org.pentaho.plugin.core.ContentOutputComponent"); componentClassMap.put("ContentRepositoryCleaner", "org.pentaho.plugin.core.ContentRepositoryCleaner"); componentClassMap.put("HelloWorldComponent", "org.pentaho.plugin.core.HelloWorldComponent"); componentClassMap.put("ResultSetCompareComponent", "org.pentaho.plugin.core.ResultSetCompareComponent"); componentClassMap.put("ResultSetExportComponent", "org.pentaho.plugin.core.ResultSetExportComponent"); componentClassMap.put("ResultSetFlattenerComponent", "org.pentaho.plugin.core.ResultSetFlattenerComponent"); componentClassMap.put("SecureFilterComponent", "org.pentaho.plugin.core.SecureFilterComponent"); componentClassMap.put("SubActionComponent", "org.pentaho.plugin.core.SubActionComponent"); componentClassMap.put("TemplateComponent", "org.pentaho.plugin.core.TemplateComponent"); componentClassMap.put("BIRTReportComponent", "org.pentaho.plugin.eclipsebirt.BIRTReportComponent"); componentClassMap.put("EmailComponent", "org.pentaho.plugin.email.EmailComponent"); componentClassMap.put("JasperReportsComponent", "org.pentaho.plugin.jasperreports.JasperReportsComponent"); componentClassMap.put("JavascriptRule", "org.pentaho.plugin.javascript.JavascriptRule"); componentClassMap.put("ChartComponent", "org.pentaho.plugin.jfreechart.ChartComponent"); componentClassMap.put("JFreeReportComponent", "org.pentaho.plugin.jfreereport.JFreeReportComponent"); componentClassMap.put("ReportWizardSpecComponent", "org.pentaho.plugin.jfreereport.ReportWizardSpecComponent"); componentClassMap.put("KettleComponent", "org.pentaho.plugin.kettle.KettleComponent"); componentClassMap.put("MDXDataComponent", "org.pentaho.plugin.mdx.MDXDataComponent"); componentClassMap.put("MDXLookupRule", "org.pentaho.plugin.mdx.MDXLookupRule"); componentClassMap.put("ReceiptAuditComponent", "org.pentaho.plugin.misc.ReceiptAuditComponent"); componentClassMap.put("TestComponent", "org.pentaho.plugin.misc.TestComponent"); componentClassMap.put("UtilityComponent", "org.pentaho.plugin.misc.UtilityComponent"); componentClassMap.put("MondrianModelComponent", "org.pentaho.plugin.olap.MondrianModelComponent"); componentClassMap.put("PivotViewComponent", "org.pentaho.plugin.olap.PivotViewComponent"); componentClassMap.put("PrintComponent", "org.pentaho.plugin.print.PrintComponent"); componentClassMap.put("JobSchedulerComponent", "org.pentaho.plugin.quartz.JobSchedulerComponent"); componentClassMap.put("SchedulerAdminComponent", "org.pentaho.plugin.quartz.SchedulerAdminComponent"); componentClassMap.put("SharkWorkflowComponent", "org.pentaho.plugin.shark.SharkWorkflowComponent"); componentClassMap.put("SQLDataComponent", "org.pentaho.plugin.sql.SQLDataComponent"); componentClassMap.put("SQLLookupRule", "org.pentaho.plugin.sql.SQLLookupRule"); componentClassMap.put("SQLExecute", "org.pentaho.plugin.sql.SQLExecute"); componentClassMap.put("XQueryLookupRule", "org.pentaho.plugin.xquery.XQueryLookupRule"); componentClassMap.put("ResultSetCrosstabComponent", "org.pentaho.plugin.core.ResultSetCrosstabComponent"); 527 componentClassMap.put("org.pentaho.component.ContentOutputComponent", "!org.pentaho.plugin.core.ContentOutputComponent"); componentClassMap.put("org.pentaho.component.ContentRepositoryCleaner", "!org.pentaho.plugin.core.ContentRepositoryCleaner"); componentClassMap.put("org.pentaho.component.HelloWorldComponent", "!org.pentaho.plugin.core.HelloWorldComponent"); componentClassMap.put("org.pentaho.component.ResultSetCompareComponent", "!org.pentaho.plugin.core.ResultSetCompareComponent"); componentClassMap.put("org.pentaho.component.ResultSetExportComponent", "!org.pentaho.plugin.core.ResultSetExportComponent"); componentClassMap.put("org.pentaho.component.ResultSetFlattenerComponent", "!org.pentaho.plugin.core.ResultSetFlattenerComponent"); componentClassMap.put("org.pentaho.component.SecureFilterComponent", "!org.pentaho.plugin.core.SecureFilterComponent"); componentClassMap.put("org.pentaho.component.SubActionComponent", "!org.pentaho.plugin.core.SubActionComponent"); componentClassMap.put("org.pentaho.component.TemplateComponent", "!org.pentaho.plugin.core.TemplateComponent"); componentClassMap.put("org.pentaho.birt.BIRTReportComponent", "!org.pentaho.plugin.eclipsebirt.BIRTReportComponent"); componentClassMap.put("org.pentaho.component.EmailComponent", "!org.pentaho.plugin.email.EmailComponent"); componentClassMap.put("org.pentaho.jasper.JasperReportsComponent", "!org.pentaho.plugin.jasperreports.JasperReportsComponent"); componentClassMap.put("org.pentaho.component.JavascriptRule", "!org.pentaho.plugin.javascript.JavascriptRule"); componentClassMap.put("org.pentaho.component.ChartComponent", "!org.pentaho.plugin.jfreechart.ChartComponent"); componentClassMap.put("org.pentaho.jfree.JFreeReportComponent", "!org.pentaho.plugin.jfreereport.JFreeReportComponent"); componentClassMap.put("org.pentaho.kettle.KettleComponent", "!org.pentaho.plugin.kettle.KettleComponent"); componentClassMap.put("org.pentaho.component.MDXDataComponent", "!org.pentaho.plugin.mdx.MDXDataComponent"); componentClassMap.put("org.pentaho.component.MDXLookupRule", "!org.pentaho.plugin.mdx.MDXLookupRule"); componentClassMap.put("org.pentaho.component.ReceiptAuditComponent", "!org.pentaho.plugin.misc.ReceiptAuditComponent"); componentClassMap.put("org.pentaho.component.TestComponent", "!org.pentaho.plugin.misc.TestComponent"); componentClassMap.put("org.pentaho.component.UtilityComponent", "!org.pentaho.plugin.misc.UtilityComponent"); componentClassMap.put("org.pentaho.component.MondrianModelComponent", "!org.pentaho.plugin.olap.MondrianModelComponent"); componentClassMap.put("org.pentaho.component.PivotViewComponent", "!org.pentaho.plugin.olap.PivotViewComponent"); componentClassMap.put("org.pentaho.component.PrintComponent", "!org.pentaho.plugin.print.PrintComponent"); componentClassMap.put("org.pentaho.component.JobSchedulerComponent", "!org.pentaho.plugin.quartz.JobSchedulerComponent"); componentClassMap.put("org.pentaho.component.SchedulerAdminComponent", "!org.pentaho.plugin.quartz.SchedulerAdminComponent"); componentClassMap.put("org.pentaho.component.SharkWorkflowComponent", "!org.pentaho.plugin.shark.SharkWorkflowComponent"); componentClassMap.put("org.pentaho.component.SQLDataComponent", "!org.pentaho.plugin.sql.SQLDataComponent"); componentClassMap.put("org.pentaho.component.SQLLookupRule", "!org.pentaho.plugin.sql.SQLLookupRule"); componentClassMap.put("org.pentaho.component.XQueryLookupRule", "!org.pentaho.plugin.xquery.XQueryLookupRule"); 559 return componentClassMap; 560 } 561 562 private IComponent resolveComponent(IActionDefinition actionDefinition) { 563 564 String componentClassName = actionDefinition.getComponentName().trim(); 567 568 String mappedClassName = (String ) getComponentClassMap().get(componentClassName); 569 if (mappedClassName != null) { 570 if (mappedClassName.indexOf('!') == 0) { 571 mappedClassName = mappedClassName.substring(1); 573 warn( Messages.getString( "RuntimeContext.WARN_DEPRECATED_COMPONENT_CLASS", componentClassName, mappedClassName ) ); audit(MessageTypes.DEPRECATION_WARNING, componentClassName, mappedClassName, 0); 575 } 576 componentClassName = mappedClassName; 577 } 578 579 Node componentDefinition = actionDefinition.getComponentSection(); 580 currentComponent = componentClassName; 581 currentActionDef = actionDefinition; 582 try { 583 584 589 590 IComponent component = null; 591 Class componentClass; 592 601 602 componentClass = Class.forName(componentClassName); 603 component = (IComponent) componentClass.newInstance(); 604 component.setInstanceId( instanceId ); 605 component.setActionName( getActionName() ); 606 component.setProcessId( processId ); 607 component.setComponentDefinition( componentDefinition ); 608 component.setRuntimeContext( this ); 609 component.setSession( session ); 610 component.setLoggingLevel( getLoggingLevel() ); 611 component.setMessages( getMessages() ); 612 return component; 613 614 } catch (Exception e) { 615 error(Messages.getErrorString("RuntimeContext.ERROR_0009_COULD_NOT_CREATE_COMPONENT", componentClassName), e); } 617 618 return null; 620 } 621 622 public int executeSequence(IActionCompleteListener listener, boolean async) { 623 paramManager.resetParameters(); 624 625 long start = new Date ().getTime(); 626 if (status != RUNTIME_CONTEXT_VALIDATE_OK) { 627 audit(MessageTypes.ACTION_SEQUENCE_FAILED, MessageTypes.VALIDATION, Messages.getErrorString("RuntimeContext.ERROR_0010_RUNTIME_DID_NOT_VALIDATE"), 0); error(Messages.getErrorString("RuntimeContext.ERROR_0010_RUNTIME_DID_NOT_VALIDATE")); return (status); 630 } 631 status = RUNTIME_STATUS_RUNNING; 632 633 List actionDefinitions = actionSequence.getActionDefinitionsAndSequences(); 635 636 if (actionDefinitions == null) { 637 audit(MessageTypes.ACTION_SEQUENCE_FAILED, MessageTypes.VALIDATION, Messages.getErrorString("RuntimeContext.ERROR_0011_NO_VALID_ACTIONS"), 0); error(Messages.getErrorString("RuntimeContext.ERROR_0011_NO_VALID_ACTIONS")); return RUNTIME_CONTEXT_VALIDATE_FAIL; 640 } 641 642 setLoggingLevel(loggingLevel); 643 644 if (debug) 645 debug(Messages.getString("RuntimeContext.DEBUG_EXECUTING_ACTIONS")); 647 paramManager.setCurrentParameters( null ); 648 errorLevel = resolveParameters(); 649 if (errorLevel != RUNTIME_CONTEXT_RESOLVE_OK) { 650 audit(MessageTypes.ACTION_SEQUENCE_FAILED, MessageTypes.VALIDATION, Messages.getErrorString("RuntimeContext.ERROR_0013_BAD_PARAMETERS"), 0); error(Messages.getErrorString("RuntimeContext.ERROR_0013_BAD_PARAMETERS")); return errorLevel; 653 } 654 655 int rtnStat = executeSequence(actionSequence, listener, async); 656 657 if (this.feedbackAllowed() && ( promptStatus!=PROMPT_NO || (xformBody.length() > 0) || (parameterTemplate != null))) { 658 try { 659 sendFeedbackForm(); 660 } catch (Exception e) { 661 error(Messages.getString("RuntimeContext.ERROR_0030_SEND_FEEDBACKFORM"), e); try { 664 outputHandler.getFeedbackContentItem().setMimeType("text/html"); OutputStream os = outputHandler.getFeedbackContentItem().getOutputStream(getActionName()); 666 if (os != null) { 667 os.write(Messages.getString("RuntimeContext.USER_BAD_PARAMETER_PAGE").getBytes(LocaleHelper.getSystemEncoding())); } 669 } catch (Throwable t) { 670 return RUNTIME_STATUS_FAILURE; 671 } 672 } 673 } 674 675 paramManager.setCurrentParameters( null ); 676 677 long end = new Date ().getTime(); 678 if ((rtnStat == RUNTIME_STATUS_SUCCESS) && audit) { 679 audit(MessageTypes.ACTION_SEQUENCE_END, MessageTypes.END, "", (int) (end - start)); } else { 681 audit(MessageTypes.ACTION_SEQUENCE_FAILED, MessageTypes.EXECUTION, "", (int) (end - start)); } 683 684 status = rtnStat; 685 686 if ( (rtnStat == RUNTIME_STATUS_SUCCESS) && !isPromptPending() ) { 687 Map returnParamMap = paramManager.getReturnParameters(); 688 689 for (Iterator it = returnParamMap.entrySet().iterator(); it.hasNext(); ) { 690 Map.Entry mapEntry = (Map.Entry )it.next(); 691 692 String paramName = (String )mapEntry.getKey(); 693 ParameterManager.ReturnParameter returnParam = (ParameterManager.ReturnParameter)mapEntry.getValue(); 694 695 if ( returnParam == null) { 696 error(Messages.getErrorString("RuntimeContext.ERROR_0029_SAVE_PARAM_NOT_FOUND", paramName )); } 698 else { 699 if ( PARAMS_SESSION.equals(returnParam.destinationName) ) { 700 session.setAttribute( returnParam.destinationParameter, returnParam.value ); 701 if (debug) 702 debug( paramName + " - session - " + returnParam.destinationParameter ); } 704 else if ("response".equals(returnParam.destinationName)) { if (outputHandler != null) { 706 outputHandler.setOutput( returnParam.destinationParameter, returnParam.value ); 707 } else { 708 info(Messages.getString("RuntimeContext.INFO_NO_OUTPUT_HANDLER")); } 710 if (debug) 711 debug( paramName + " - response - " + returnParam.destinationParameter ); } 713 } 714 } 715 } 716 717 return (rtnStat); 719 } 720 721 public void setPromptStatus( int status ) { 722 promptStatus = status; 723 } 724 725 private int executeSequence(IActionSequence sequence, IActionCompleteListener listener, boolean async) { 726 String loopParamName = sequence.getLoopParameter(); 727 Object loopList; 728 IActionParameter loopParm = null; 729 730 if (loopParamName == null) { 731 loopList = new ArrayList (); 732 ((ArrayList ) loopList).add(new Integer (0)); 733 } else { 734 loopParm = getLoopParameter(loopParamName); 735 loopList = loopParm.getValue(); 736 } 737 738 if (loopList instanceof List ) { 739 int result = executeLoop(loopParm, (List ) loopList, sequence, listener, async); 740 if (loopParm != null) { 741 addInputParameter(loopParm.getName(), loopParm); } 743 744 if (result != RUNTIME_STATUS_SUCCESS) { 745 return result; 746 } 747 } else if (loopList instanceof IPentahoResultSet) { 748 int result = executeLoop(loopParm, (IPentahoResultSet) loopList, sequence, listener, async); 749 if (result != RUNTIME_STATUS_SUCCESS) { 750 return result; 751 } 752 } 753 754 return (RUNTIME_STATUS_SUCCESS); 755 } 756 757 private int executeLoop(IActionParameter loopParm, IPentahoResultSet loopSet, IActionSequence sequence, IActionCompleteListener listener, boolean async) { 758 759 int loopCount = 0; 761 762 764 Object row[] = loopSet.next(); 765 Object headerSet[][] = loopSet.getMetaData().getColumnHeaders(); 766 Object headers[] = headerSet[0]; 768 while (row != null) { 769 770 if (debug) 771 debug(Messages.getString("RuntimeContext.DEBUG_EXECUTING_ACTION", Integer.toString(loopCount++))); 773 if (loopParm != null) { 774 IActionParameter ap; 775 for (int columnNo = 0; columnNo < headers.length; columnNo++) { 776 String name = headers[columnNo].toString(); 777 Object value = row[columnNo]; 778 String type = null; 779 if (value instanceof String ) { 780 type = IActionParameter.TYPE_STRING; 781 } else if (value instanceof Date ) { 782 type = IActionParameter.TYPE_DATE; 783 } else if (value instanceof Long || value instanceof Integer ) { 784 type = IActionParameter.TYPE_INTEGER; 785 } else if (value instanceof BigDecimal ) { 786 type = IActionParameter.TYPE_DECIMAL; 787 } else if (value instanceof String []) { 788 type = IActionParameter.TYPE_STRING; 789 } else if (value == null) { 790 warn(Messages.getString("RuntimeContext.WARN_VARIABLE_IN_LOOP_IS_NULL", name)); } else { 792 type = IActionParameter.TYPE_OBJECT; 793 warn(Messages.getString("RuntimeContext.WARN_VARIABLE_IN_LOOP_NOT_RECOGNIZED", name, value.getClass().toString())); } 795 if (type != null) { 797 ap = new ActionParameter(name, type, value, null, null); 798 addInputParameter(name, ap); 799 } 800 } 801 } 802 int rtn = performActions(sequence, listener, async); 803 if (rtn != RUNTIME_STATUS_SUCCESS) { 804 return rtn; 805 } 806 row = loopSet.next(); 807 } 808 809 return RUNTIME_STATUS_SUCCESS; 810 } 811 812 private int executeLoop(IActionParameter loopParm, List loopList, IActionSequence sequence, IActionCompleteListener listener, boolean async) { 813 814 int loopCount = 0; 816 for (Iterator it = loopList.iterator(); it.hasNext();) { 817 818 if (debug) 819 debug(Messages.getString("RuntimeContext.DEBUG_EXECUTING_ACTION", Integer.toString(loopCount++))); 821 Object loopVar = it.next(); 822 if (loopParm != null) { 823 IActionParameter ap; 824 if (loopVar instanceof Map ) { 825 ap = new ActionParameter(loopParm.getName(), "property-map", loopVar, null, null); } else { 827 ap = new ActionParameter(loopParm.getName(), "string", loopVar, null, null); } 829 830 addInputParameter(loopParm.getName(), ap); 831 } 832 833 int rtn = performActions(sequence, listener, async); 834 if ( (rtn != RUNTIME_STATUS_SUCCESS) || (promptStatus == PROMPT_NOW) ) { 835 return rtn; 836 } 837 } 838 return RUNTIME_STATUS_SUCCESS; 839 } 840 841 private int performActions(IActionSequence sequence, IActionCompleteListener listener, boolean async) { 842 ConditionalExecution conditional = sequence.getConditionalExecution(); 843 if (conditional != null) { 844 boolean shouldExecute = false; 845 try { 846 shouldExecute = conditional.shouldExecute( paramManager.getAllParameters(), logger); 847 } catch (Exception ex) { 848 error(Messages.getErrorString("RuntimeContext.ERROR_0032_CONDITIONAL_EXECUTION_FAILED"), ex); currentComponent = ""; return RUNTIME_STATUS_FAILURE; 852 } 853 if (!shouldExecute) { 854 if (debug ) this.debug(Messages.getString("RuntimeContext.INFO_ACTION_NOT_EXECUTED")); return RUNTIME_STATUS_SUCCESS; 857 } 858 } 859 860 List defList = sequence.getActionDefinitionsAndSequences(); 861 862 Object listItem; 863 for (Iterator actIt = defList.iterator(); actIt.hasNext();) { 864 listItem = actIt.next(); 865 866 if (listItem instanceof IActionSequence) { 867 int rtn = executeSequence((IActionSequence) listItem, listener, async); 868 if (rtn != RUNTIME_STATUS_SUCCESS) { 869 return (rtn); 870 } 871 } else if (listItem instanceof IActionDefinition) { 872 IActionDefinition actionDef = (IActionDefinition) listItem; 873 currentComponent = actionDef.getComponentName(); 874 currentActionDef = actionDef; 875 paramManager.setCurrentParameters(actionDef); 876 877 int executeResult = executeAction(actionDef, parameterProviders, listener, async); 878 if (executeResult != IRuntimeContext.RUNTIME_STATUS_SUCCESS) { 879 error(Messages.getErrorString("RuntimeContext.ERROR_0012_EXECUTION_FAILED", currentComponent)); currentComponent = ""; return RUNTIME_STATUS_FAILURE; 883 } 884 paramManager.addOutputParameters(actionDef); 885 } 886 if (promptStatus == PROMPT_NOW) { 887 return (RUNTIME_STATUS_SUCCESS); 889 } 890 currentComponent = ""; currentActionDef = null; 892 } 893 return RUNTIME_STATUS_SUCCESS; 894 } 895 896 private int executeAction(IActionDefinition actionDefinition, Map pParameterProviders, IActionCompleteListener listener, boolean async) { 897 898 this.parameterProviders = pParameterProviders; 899 901 long start = new Date ().getTime(); 902 if (audit) { 903 audit(MessageTypes.COMPONENT_EXECUTE_START, MessageTypes.START, "", 0); } 905 906 errorLevel = RUNTIME_CONTEXT_RESOLVE_OK; 907 908 errorLevel = resolveParameters(); 910 if (errorLevel != RUNTIME_CONTEXT_RESOLVE_OK) { 911 error(Messages.getErrorString("RuntimeContext.ERROR_0013_BAD_PARAMETERS")); audit(MessageTypes.COMPONENT_EXECUTE_FAILED, MessageTypes.VALIDATION, Messages.getErrorString("RuntimeContext.ERROR_0013_BAD_PARAMETERS"), 0); if (listener != null) { 914 listener.actionComplete(this); 915 } 916 return errorLevel; 917 } 918 919 if (debug) 920 debug(Messages.getString("RuntimeContext.DEBUG_PRE-EXECUTE_AUDIT")); List auditPre = actionDefinition.getPreExecuteAuditList(); 922 audit(auditPre); 923 924 errorLevel = resolveOutputHandler(); 926 if (errorLevel != RUNTIME_CONTEXT_RESOLVE_OK) { 927 audit(MessageTypes.COMPONENT_EXECUTE_FAILED, MessageTypes.VALIDATION, Messages.getErrorString("RuntimeContext.ERROR_0014_NO_OUTPUT_HANDLER"), 0); if (listener != null) { 929 listener.actionComplete(this); 930 } 931 return errorLevel; 932 } 933 934 937 if (async) { 938 } 941 942 IComponent component = actionDefinition.getComponent(); 944 945 if (debug) 946 debug(Messages.getString("RuntimeContext.DEBUG_SETTING_LOGGING", Logger.getLogLevelName(loggingLevel))); component.setLoggingLevel(loggingLevel); 948 if (debug) 949 debug(Messages.getString("RuntimeContext.DEBUG_INITIALIZING_COMPONENT")); errorLevel = component.init() ? RUNTIME_STATUS_INITIALIZE_OK : RUNTIME_STATUS_INITIALIZE_FAIL; 951 if (errorLevel != RUNTIME_STATUS_INITIALIZE_OK) { 952 audit(MessageTypes.COMPONENT_EXECUTE_FAILED, MessageTypes.VALIDATION, Messages.getErrorString("RuntimeContext.ERROR_0016_COMPONENT_INITIALIZE_FAILED"), 0); error(Messages.getErrorString("RuntimeContext.ERROR_0016_COMPONENT_INITIALIZE_FAILED")); if (listener != null) { 955 listener.actionComplete(this); 956 } 957 return errorLevel; 958 } 959 960 errorLevel = executeComponent(actionDefinition); 962 if (errorLevel != RUNTIME_STATUS_SUCCESS) { 963 if (listener != null) { 964 listener.actionComplete(this); 965 } 966 return errorLevel; 967 } 968 969 if (debug) 970 debug(Messages.getString("RuntimeContext.DEBUG_POST-EXECUTE_AUDIT")); List auditPost = actionDefinition.getPostExecuteAuditList(); 972 audit(auditPost); 973 if (audit) { 974 long end = new Date ().getTime(); 975 audit(MessageTypes.COMPONENT_EXECUTE_END, MessageTypes.END, "", (int) (end - start)); } 977 978 if (listener != null) { 979 listener.actionComplete(this); 980 } 981 return errorLevel; 982 } 983 984 private int executeComponent(IActionDefinition actionDefinition) { 985 if (debug) 986 debug(Messages.getString("RuntimeContext.DEBUG_STARTING_COMPONENT_EXECUTE")); int executeStatus = RUNTIME_STATUS_FAILURE; 988 try { 989 executeStatus = actionDefinition.getComponent().execute(); 990 actionDefinition.getComponent().done(); 991 } catch (Exception e) { 992 audit(MessageTypes.COMPONENT_EXECUTE_FAILED, MessageTypes.FAILED, e.getLocalizedMessage(), 0); 993 error(Messages.getErrorString("RuntimeContext.ERROR_0017_COMPONENT_EXECUTE_FAILED"), e); } 995 if (debug) 996 debug(Messages.getString("RuntimeContext.DEBUG_FINISHED_COMPONENT_EXECUTE")); return executeStatus; 998 } 999 1000 private int initFromActionSequenceDefinition() { 1001 1002 1004 int actionLogLevel = actionSequence.getLoggingLevel(); 1005 int instanceLogLevel = runtimeData.getLoggingLevel(); 1006 int actionSequenceLoggingLevel = (instanceLogLevel != UNKNOWN) ? instanceLogLevel : ((actionLogLevel != UNKNOWN) ? actionLogLevel : solutionEngine.getLoggingLevel()); 1007 1008 setLoggingLevel(actionSequenceLoggingLevel); 1009 1010 return RUNTIME_CONTEXT_RESOLVE_OK; 1011 } 1012 1013 private int resolveParameters() { 1014 1015 Set inputNames = getInputNames(); 1016 Iterator inputNamesIterator = inputNames.iterator(); 1017 IActionParameter actionParameter; 1018 List variables; 1019 Iterator variablesIterator; 1020 ActionParameterSource variable; 1021 String sourceName; 1022 String sourceValue; 1023 Object variableValue = null; 1024 IParameterProvider parameterProvider; 1025 while (inputNamesIterator.hasNext()) { 1026 variableValue = null; 1027 1028 String inputName = (String ) inputNamesIterator.next(); 1029 actionParameter = paramManager.getCurrentInput(inputName); 1030 if (actionParameter == null) { 1031 error(Messages.getErrorString("RuntimeContext.ERROR_0031_INPUT_NOT_FOUND", inputName)); return RUNTIME_CONTEXT_RESOLVE_FAIL; 1033 } 1034 1035 variables = actionParameter.getVariables(); 1036 variablesIterator = variables.iterator(); 1037 while (variablesIterator.hasNext()) { 1038 variable = (ActionParameterSource) variablesIterator.next(); 1039 sourceName = variable.getSourceName(); 1040 sourceValue = variable.getValue(); 1041 variableValue = null; 1042 if ("runtime".equals(sourceName)) { variableValue = getStringParameter(sourceValue, null); 1047 if (variableValue == null) { 1048 variableValue = runtimeData.getStringProperty(sourceValue, null); 1050 } 1051 if (variableValue != null) { 1052 break; 1053 } 1054 } else { 1055 parameterProvider = (IParameterProvider) parameterProviders.get(sourceName); 1056 if (parameterProvider == null) { 1057 warn(Messages.getString("RuntimeContext.WARN_REQUESTED_PARAMETER_SOURCE_NOT_AVAILABLE", sourceName, inputName)); } else { 1059 variableValue = parameterProvider.getParameter(sourceValue); 1060 if (variableValue != null) { 1063 break; 1064 } 1065 } 1066 } 1067 } 1068 1069 if (variableValue == null) { 1070 1071 if (actionParameter.getValue() != null) { 1072 if (actionParameter.hasDefaultValue()) { 1073 trace(Messages.getString("RuntimeContext.TRACE_USING_DEFAULT_PARAMETER_VALUE", inputName)); } else { 1075 trace(Messages.getString("RuntimeContext.TRACE_INFO_USING_CURRENT_PARAMETER_VALUE" + inputName)); } 1077 } else if ("content".equals(actionParameter.getType())) { variableValue = ""; } else { 1081 error(Messages.getErrorString("RuntimeContext.ERROR_0018_PARAMETER_NOT_FULFILLED", inputName)); return RUNTIME_CONTEXT_RESOLVE_FAIL; 1083 } 1084 } else { 1085 actionParameter.setValue(variableValue); 1086 } 1087 } 1088 1089 return RUNTIME_CONTEXT_RESOLVE_OK; 1090 } 1091 1092 public void dispose() { 1093 paramManager.dispose(); 1094 } 1095 1096 private int resolveOutputHandler() { 1097 1098 return RUNTIME_CONTEXT_RESOLVE_OK; 1100 } 1101 1102 public String getStringParameter(String name, String defaultValue) { 1104 if ("instance-id".equals(name)) { return instanceId; 1106 } else if ("solution-id".equals(name)) { return solutionName; 1108 } 1109 return defaultValue; 1110 } 1112 1113 1115 public Object getInputParameterValue(String name) { 1116 IActionParameter actionParameter = paramManager.getCurrentInput(name); 1117 if (actionParameter == null) { 1118 error(Messages.getErrorString("RuntimeContext.ERROR_0019_INVALID_INPUT_REQUEST", name, actionSequence.getSequenceName())); throw new InvalidParameterException(); 1121 } 1122 return actionParameter.getValue(); 1123 } 1124 1125 public String getInputParameterStringValue(String name) { 1126 IActionParameter actionParameter = paramManager.getCurrentInput(name); 1127 if (actionParameter == null) { 1128 error(Messages.getErrorString("RuntimeContext.ERROR_0019_INVALID_INPUT_REQUEST", name, actionSequence.getSequenceName())); throw new InvalidParameterException(); 1131 } 1132 return actionParameter.getStringValue(); 1133 } 1134 1135 private IActionParameter getLoopParameter(String name) { 1137 IActionParameter actionParameter = paramManager.getLoopParameter(name); 1138 if (actionParameter == null) { 1139 error(Messages.getErrorString("RuntimeContext.ERROR_0020_INVALID_LOOP_PARAMETER", name, actionSequence.getSequenceName())); throw new InvalidParameterException(); 1142 } 1143 return actionParameter; 1144 } 1145 1146 public IActionParameter getInputParameter(String name) { 1147 IActionParameter actionParameter = paramManager.getCurrentInput(name); 1148 if (actionParameter == null) { 1149 error(Messages.getErrorString("RuntimeContext.ERROR_0019_INVALID_INPUT_REQUEST", name, actionSequence.getSequenceName())); throw new InvalidParameterException(); 1152 } 1153 return actionParameter; 1154 } 1155 1156 public IActionParameter getOutputParameter(String name) { 1157 IActionParameter actionParameter = paramManager.getCurrentOutput(name); 1158 if (actionParameter == null) { 1159 error(Messages.getErrorString("RuntimeContext.ERROR_0021_INVALID_OUTPUT_REQUEST", name, actionSequence.getSequenceName())); throw new InvalidParameterException(); 1162 } 1163 return actionParameter; 1164 } 1165 1166 public IActionResource getResourceDefintion(String name) { 1167 IActionResource actionResource = paramManager.getCurrentResource( name ); 1168 1169 if (actionResource == null) { 1170 error(Messages.getErrorString("RuntimeContext.ERROR_0022_INVALID_RESOURCE_REQUEST", name, actionSequence.getSequenceName())); throw new InvalidParameterException(); 1173 } 1174 return actionResource; 1175 } 1176 1177 public Set getInputNames() { 1178 return paramManager.getCurrentInputNames(); 1179 } 1180 1181 public void addTempParameter( String name, IActionParameter param ) { 1182 paramManager.addToCurrentInputs( name, param ); 1183 } 1184 1185 public OutputStream getOutputStream(String outputName, String mimeType, String extension) { 1186 1187 1189 IActionParameter outputParameter = getOutputParameter(outputName); 1190 if (outputParameter == null) { 1191 error(Messages.getErrorString("RuntimeContext.ERROR_0021_INVALID_OUTPUT_REQUEST", outputName, actionSequence.getSequenceName())); throw new InvalidParameterException(); 1193 } 1194 1195 String contentName = outputName; 1198 if (currentActionDef != null) { 1199 contentName = currentActionDef.getMappedOutputName(outputName); 1200 contentName = (contentName != null) ? contentName : outputName; 1201 } 1202 1203 if (!IActionParameter.TYPE_CONTENT.equals(outputParameter.getType())) { 1204 error(Messages.getErrorString("RuntimeContext.ERROR_0023_INVALID_OUTPUT_STREAM", outputName)); return null; 1206 } 1207 1208 if (this.outputHandler instanceof ContentRepositoryOutputHandler) { 1209 IContentItem outputItem = this.outputHandler.getOutputContentItem(null, contentName, actionSequence.getTitle(), null); 1213 outputItem.setMimeType(mimeType); 1214 try { 1215 OutputStream stream = outputItem.getOutputStream(actionSequence.getSequenceName()); 1216 if (stream == null) { 1217 error(Messages.getErrorString("RuntimeContext.ERROR_0026_NO_OUTPUT_STREAM")); } 1219 setOutputValue(outputName, outputItem); 1220 return stream; 1221 } catch (IOException e) { 1222 1223 } 1224 return null; 1225 } 1226 1227 IContentRepository contentRepository = PentahoSystem.getContentRepository(session); 1229 if (contentRepository == null) { 1230 error(Messages.getErrorString("RuntimeContext.ERROR_0024_NO_CONTENT_REPOSITORY")); return null; 1232 } 1233 String extensionFolder = extension; 1234 if (extensionFolder.startsWith(".")) { extensionFolder = extensionFolder.substring(1); 1236 } 1237 String outputFolder = actionSequence.getSequenceName().substring(0, actionSequence.getSequenceName().lastIndexOf('.')); 1238 String contentPath = getSolutionName() + "/" + getSolutionPath() + "/" + outputFolder + "/" + contentName + "/" + extensionFolder; IContentLocation contentLocation = null; 1241 try { 1242 contentLocation = contentRepository.getContentLocationByPath(contentPath); 1243 } catch (Exception ex) { 1244 } 1246 if (contentLocation == null) { 1247 contentLocation = contentRepository.newContentLocation(contentPath, contentName, contentName, getSolutionName(), true); 1248 } 1249 if (contentLocation == null) { 1250 error(Messages.getErrorString("RuntimeContext.ERROR_0025_INVALID_CONTENT_LOCATION")); return null; 1252 } 1253 1255 1257 IContentItem contentItem = null; 1259 try { 1260 contentItem = contentLocation.getContentItemByName(instanceId); 1261 } catch (Exception ex) { 1262 } 1264 if (contentItem == null) { contentItem = contentLocation.newContentItem(instanceId, contentName, extension, mimeType, null, IContentItem.WRITEMODE_KEEPVERSIONS); 1267 } 1268 1269 try { 1270 OutputStream stream = contentItem.getOutputStream(actionSequence.getSequenceName()); 1271 if (stream == null) { 1272 error(Messages.getErrorString("RuntimeContext.ERROR_0026_NO_OUTPUT_STREAM")); } 1274 setOutputValue(outputName, contentItem); 1275 return stream; 1276 } catch (IOException e) { 1277 1278 } 1279 return null; 1280 1281 } 1282 1283 public void setOutputValue(String name, Object output) { 1284 IActionParameter actionParameter = paramManager.getCurrentOutput(name); 1285 if (actionParameter == null) { 1286 error(Messages.getErrorString("RuntimeContext.ERROR_0021_INVALID_OUTPUT_REQUEST", name, actionSequence.getSequenceName())); throw new InvalidParameterException(); 1289 } 1290 actionParameter.setValue(output); 1291 1292 if (output instanceof String ) { 1293 runtimeData.setStringProperty(name, (String ) output); 1294 } else if (output instanceof Date ) { 1295 runtimeData.setDateProperty(name, (Date ) output); 1296 } else if (output instanceof Long ) { 1297 runtimeData.setLongProperty(name, (Long ) output); 1298 } else if (output instanceof List ) { 1299 runtimeData.setListProperty(name, (List ) output); 1300 } else if (output instanceof Map ) { 1301 runtimeData.setMapProperty(name, (Map ) output); 1302 } else if (output instanceof IContentItem) { 1303 runtimeData.setStringProperty(name, ((IContentItem) output).getPath()); 1304 } 1305 1306 } 1307 1308 public DataSource getDataSource(String parameterName) { 1309 1310 IActionParameter actionParameter = paramManager.getCurrentInput(parameterName); 1312 if (actionParameter == null) { 1313 error(Messages.getErrorString("RuntimeContext.ERROR_0019_INVALID_INPUT_REQUEST", parameterName, actionSequence.getSequenceName())); throw new InvalidParameterException(); 1315 } 1316 1317 Object locObj = actionParameter.getValue(); 1318 String location = (locObj instanceof IContentItem) ? ((IContentItem) locObj).getPath() : locObj.toString(); 1319 1320 IContentRepository contentRepository = PentahoSystem.getContentRepository(session); 1322 if (contentRepository == null) { 1323 error(Messages.getErrorString("RuntimeContext.ERROR_0024_NO_CONTENT_REPOSITORY")); return null; 1325 } 1326 1327 IContentItem contentItem = contentRepository.getContentItemByPath(location); 1328 if (contentItem == null) { 1329 return null; 1330 } 1331 1332 return contentItem.getDataSource(); 1333 } 1334 1335 public String getContentUrl(IContentItem contentItem) { 1336 if (contentItem == null) { 1337 return (null); 1338 } 1339 String url = PentahoSystem.getApplicationContext().getBaseUrl(); 1340 return (url + "GetContent?id=" + contentItem.getId()); } 1342 1343 public InputStream getInputStream(String parameterName) { 1344 1345 IActionParameter inputParameter = getInputParameter(parameterName); 1346 1347 if (inputParameter == null) { 1348 error(Messages.getErrorString("RuntimeContext.ERROR_0019_INVALID_INPUT_REQUEST", parameterName, actionSequence.getSequenceName())); throw new InvalidParameterException(); 1350 } 1351 Object value = inputParameter.getValue(); 1352 if (value instanceof IContentItem) { 1353 IContentItem contentItem = (IContentItem) value; 1354 return contentItem.getInputStream(); 1355 } else { 1356 return null; 1357 } 1358 1359 1375 } 1376 1377 public Set getOutputNames() { 1378 return paramManager.getCurrentOutputNames(); 1379 } 1380 1381 public Set getResourceNames() { 1382 return paramManager.getCurrentResourceNames(); 1383 } 1384 1385 public InputStream getResourceInputStream(IActionResource actionResource) { 1386 return PentahoSystem.getSolutionRepository(session).getResourceInputStream(actionResource); 1387 } 1388 1389 public Reader getResourceReader(IActionResource actionResource) throws IOException { 1390 return PentahoSystem.getSolutionRepository(session).getResourceReader(actionResource); 1391 } 1392 1393 public String getResourceAsString(IActionResource actionResource) throws IOException { 1394 return PentahoSystem.getSolutionRepository(session).getResourceAsString(actionResource); 1395 } 1396 1397 public Document getResourceAsDocument(IActionResource actionResource) throws IOException { 1398 return PentahoSystem.getSolutionRepository(session).getResourceAsDocument(actionResource); 1399 } 1400 1401 public DataSource getResourceDataSource(IActionResource actionResource) { 1402 return PentahoSystem.getSolutionRepository(session).getResourceDataSource(actionResource); 1403 } 1404 1405 1407 public String getId() { 1408 return handle; 1409 } 1410 1411 public String getProcessId() { 1412 return processId; 1413 } 1414 1415 public String getActionName() { 1416 return ((actionSequence != null) ? actionSequence.getSequenceName() : Messages.getString("RuntimeContext.DEBUG_NO_ACTION")); } 1418 1419 public String getActionTitle() { 1420 return ((actionSequence != null) ? actionSequence.getTitle() : Messages.getString("RuntimeContext.DEBUG_NO_ACTION")); } 1422 1423 1425 public void audit(List auditList) { 1426 1427 if (auditList == null || auditList.size() == 0) { 1428 return; 1429 } 1430 1431 Iterator it = auditList.iterator(); 1433 while (it.hasNext()) { 1434 Element auditNode = (Element) it.next(); 1435 String name = auditNode.getText(); 1436 String value = getStringParameter(name, ""); AuditHelper.audit(this, session, MessageTypes.INSTANCE_ATTRIBUTE, name, value, null, this); 1438 } 1439 1440 } 1441 1442 public void audit(String messageType, String message, String value, long duration) { 1443 if (!audit) { 1444 return; 1445 } 1446 1447 if (debug) 1448 debug(Messages.getString("RuntimeContext.DEBUG_AUDIT", instanceId, getCurrentComponentName(), messageType)); AuditHelper.audit(this, session, messageType, message, value, new BigDecimal (((double) duration / 1000)), this); 1450 } 1451 1452 public void addInputParameter(String name, IActionParameter param) { 1453 paramManager.addToAllInputs(name, param); 1454 } 1455 1456 public String applyInputsToFormat(String format) { 1457 return TemplateUtil.applyTemplate(format, this); 1458 } 1459 1460 public String applyInputsToFormat(String format, IParameterResolver resolver) { 1461 return TemplateUtil.applyTemplate(format, this, resolver); 1462 } 1463 1464 1466 public void sendFeedbackForm() throws IOException { 1467 try { 1468 if (!feedbackAllowed()) { 1469 return; 1470 } 1471 createFeedbackParameter("solution", "solution", "", getSolutionName(), false); createFeedbackParameter("action", "action", "", getActionName(), false); createFeedbackParameter("path", "path", "", getSolutionPath(), false); 1476 IParameterProvider parameterProvider = (IParameterProvider) parameterProviders.get( PARAMS_REQUEST ); 1477 Iterator parameterNameIterator = parameterProvider.getParameterNames(); 1478 while (parameterNameIterator.hasNext()) { 1479 String name = (String ) parameterNameIterator.next(); 1480 if (!"solution".equals(name) && !"action".equals(name) && !"path".equals(name) && (xformFields.get(name) == null)) { Object value = parameterProvider.getParameter(name); 1482 if ( value != null ) { 1483 if ( value.getClass().isArray() ) { 1484 for ( int i = 0; i < Array.getLength( value ); ++i ) { 1485 createFeedbackParameter(name, name, "", String.valueOf( Array.get(value, i) ), false); } 1487 } 1488 else { 1489 createFeedbackParameter(name, name, "", value.toString(), false); } 1491 } 1492 } 1493 } 1494 if (parameterXsl == null) { 1495 xformBody.append("<tr><td>"); XForm.createXFormSubmit(PARAMETER_FORM, xformBody, Messages.getString("RuntimeContext.USER_PARAMETER_FORM_SUBMIT")); xformBody.append("</td></tr></table></body>"); 1501 String html = XForm.completeXForm(XForm.OUTPUT_HTML_PAGE, PARAMETER_FORM, xformHeader, xformBody, getSession()); 1502 1503 if (debug) 1504 debug(Messages.getString("RuntimeContext.DEBUG_PARAMETER_HTML", html)); 1506 outputHandler.getFeedbackContentItem().setMimeType("text/html"); OutputStream os = outputHandler.getFeedbackContentItem().getOutputStream(getActionName()); 1508 os.write(html.getBytes()); 1509 1510 } else if (parameterTemplate != null) { 1511 String html = XForm.completeXForm(XForm.OUTPUT_HTML_PAGE, PARAMETER_FORM, xformHeader, new StringBuffer (parameterTemplate), getSession()); 1512 1513 if (debug) 1514 debug(Messages.getString("RuntimeContext.DEBUG_PARAMETER_HTML", html)); 1516 outputHandler.getFeedbackContentItem().setMimeType("text/html"); OutputStream os = outputHandler.getFeedbackContentItem().getOutputStream(getActionName()); 1518 os.write(html.getBytes(LocaleHelper.getSystemEncoding())); 1519 } else if (parameterXsl.endsWith(".xsl")) { String id = actionSequence.getSequenceName(); 1521 int pos = id.indexOf('.'); 1522 if (pos > -1) { 1523 id = id.substring(0, pos); 1524 } 1525 id = id.replace('-', '_'); 1528 id = id.replace(' ', '_'); 1529 String actionUrl = urlFactory.getActionUrlBuilder().getUrl(); 1530 String displayUrl = urlFactory.getDisplayUrlBuilder().getUrl(); 1531 String target = (parameterTarget == null) ? "" : parameterTarget; XForm.completeXFormHeader(PARAMETER_FORM, xformHeader); 1533 String xml = "<filters xmlns:xf=\"http://www.w3.org/2002/xforms\">" + xformHeader + "<id><![CDATA[" + id + "]]></id><title><![CDATA[" + Messages.getEncodedString(actionSequence.getTitle()) + "]]></title><description><![CDATA[" + Messages.getEncodedString(actionSequence.getDescription()) + "]]></description><icon><![CDATA[" + actionSequence.getIcon() + "]]></icon><help><![CDATA[" + Messages.getEncodedString(actionSequence.getHelp()) + "]]></help>" + "<action><![CDATA[" + actionUrl + "]]></action>" + "<display><![CDATA[" + displayUrl + "]]></display>" + "<target><![CDATA[" + target + "]]></target>" + xformBody.toString() + "</filters>"; StringBuffer content = XmlHelper.transformXml(parameterXsl, getSolutionName() + File.separator + getSolutionPath(), xml, new HashMap (), getSession()); 1545 1546 if (content == null) { 1547 } else { 1549 outputHandler.getFeedbackContentItem().setMimeType("text/html"); OutputStream os = outputHandler.getFeedbackContentItem().getOutputStream(null); 1551 os.write(content.toString().getBytes(LocaleHelper.getSystemEncoding())); 1552 } 1553 } 1554 } catch (Exception e) { 1555 error( Messages.getErrorString( "RuntimeContext.ERROR_0030_SEND_FEEDBACKFORM" ) ); } 1557 1558 } 1559 1560 private void addXFormHeader() { 1561 1562 XForm.createXFormHeader(PARAMETER_FORM, xformHeader); 1563 1564 IActionResource resource = paramManager.getCurrentResource( parameterXsl ); 1565 1566 if (!parameterXsl.endsWith(".xsl") && (resource != null)) { try { 1569 parameterTemplate = getResourceAsString(resource); 1570 } catch (Exception e) { 1571 } 1573 } 1574 1575 } 1576 1577 1581 public void createFeedbackParameter(IActionParameter actionParam) { 1582 if (actionParam.hasSelections()) { 1583 createFeedbackParameter(actionParam.getName(), actionParam.getSelectionDisplayName(), "", actionParam.getStringValue(), actionParam.getSelectionValues(), actionParam.getSelectionNameMap(), null); } 1587 } 1588 1589 public void createFeedbackParameter(SelectionMapper selMap, String fieldName, Object defaultValues) { 1590 if (selMap != null) { 1591 createFeedbackParameter(fieldName, selMap.getSelectionDisplayName(), "", defaultValues, selMap.getSelectionValues(), selMap.getSelectionNameMap(), selMap.getDisplayStyle()); } 1594 } 1595 1596 public void createFeedbackParameter(String fieldName, String displayName, String hint, Object defaultValues, List values, Map dispNames, String displayStyle) { 1597 if (values == null) { 1598 return; 1599 } 1600 if ((xformHeader == null) || (xformHeader.length() == 0)) { 1601 addXFormHeader(); 1603 } 1604 1605 if (checkForFieldInTemplate(fieldName)) { 1608 return; 1609 } 1610 1611 int type = (values.size() < 6) ? XForm.TYPE_RADIO : XForm.TYPE_SELECT; 1612 if (displayStyle != null) { 1613 if ("text-box".equals(displayStyle)) { type = XForm.TYPE_TEXT; 1615 } else if ("radio".equals(displayStyle)) { type = XForm.TYPE_RADIO; 1617 } else if ("select".equals(displayStyle)) { type = XForm.TYPE_SELECT; 1619 } else if ("list".equals(displayStyle)) { type = XForm.TYPE_LIST; 1621 } else if ("list-multi".equals(displayStyle)) { type = XForm.TYPE_LIST_MULTI; 1623 } else if ("check-multi".equals(displayStyle)) { type = XForm.TYPE_CHECK_MULTI; 1625 } else if ("check-multi-scroll".equals(displayStyle)) { type = XForm.TYPE_CHECK_MULTI_SCROLL; 1627 } else if ("check-multi-scroll-2-column".equals(displayStyle)) { type = XForm.TYPE_CHECK_MULTI_SCROLL_2_COLUMN; 1629 } else if ("check-multi-scroll-3-column".equals(displayStyle)) { type = XForm.TYPE_CHECK_MULTI_SCROLL_3_COLUMN; 1631 } else if ("check-multi-scroll-4-column".equals(displayStyle)) { type = XForm.TYPE_CHECK_MULTI_SCROLL_4_COLUMN; 1633 } 1634 1635 } 1636 fieldName = paramManager.getActualRequestParameterName(fieldName); 1637 if (hint == null) { 1638 hint = ""; } 1640 if (parameterXsl == null) { 1641 xformBody.append(Messages.getString("RuntimeContext.CODE_XFORM_CONTROL_LABEL_START", displayName)); XForm.createXFormControl(type, fieldName, defaultValues, values, dispNames, PARAMETER_FORM, xformHeader, xformBody); 1644 xformBody.append(Messages.getString("RuntimeContext.CODE_XFORM_CONTROL_LABEL_END")); } else if (parameterTemplate != null) { 1646 StringBuffer body = new StringBuffer (); 1647 XForm.createXFormControl(type, fieldName, defaultValues, values, dispNames, PARAMETER_FORM, xformHeader, body); 1648 parameterTemplate = parameterTemplate.replaceAll("\\{" + fieldName + "\\}", body.toString()); } else if (parameterXsl.endsWith(".xsl")) { StringBuffer body = new StringBuffer (); 1651 XForm.createXFormControl(type, fieldName, defaultValues, values, dispNames, PARAMETER_FORM, xformHeader, body); 1652 xformBody.append("<filter><id><![CDATA[" + fieldName + "]]></id>") .append("<title><![CDATA[" + displayName + "]]></title>") .append("<help><![CDATA[" + hint + "]]></help><control>") .append(body).append("</control></filter>"); } 1657 1658 xformFields.put(fieldName, fieldName); 1659 1660 } 1661 1662 public boolean checkForFieldInTemplate(String fieldName) { 1663 if ( (parameterTemplate == null) || (parameterTemplate.length()==0) ) { 1680 return false; 1681 } 1682 String regex = "[iI][dD]=[\'\"]" + fieldName + "[\'\"]"; Pattern pattern = null; 1684 synchronized(PATTERN_COMPILE_LOCK) { 1690 pattern = Pattern.compile(regex); 1691 } 1692 Matcher matcher = pattern.matcher(parameterTemplate); 1693 if (matcher.find()) { 1694 return true; 1695 } 1696 return false; 1697 } 1698 1699 public void createFeedbackParameter(String fieldName, String displayName, String hint, String defaultValue, boolean visible) { 1700 1701 if ((xformHeader == null) || (xformHeader.length() == 0)) { 1702 addXFormHeader(); 1704 } 1705 if (parameterTemplate != null) { 1706 if (checkForFieldInTemplate(fieldName)) { 1708 return; 1709 } 1710 } 1711 if (hint == null) { 1712 hint = ""; } 1714 fieldName = paramManager.getActualRequestParameterName(fieldName); 1715 if (parameterXsl == null) { 1716 1718 if (visible) { 1719 xformBody.append(Messages.getString("RuntimeContext.CODE_XFORM_CONTROL_LABEL_START", displayName)); } 1724 XForm.createXFormControl(fieldName, defaultValue, PARAMETER_FORM, xformHeader, xformBody, visible); 1725 if (visible) { 1726 xformBody.append(Messages.getString("RuntimeContext.CODE_XFORM_CONTROL_LABEL_END")); } 1729 } else if (parameterTemplate != null) { 1730 StringBuffer body = new StringBuffer (); 1731 if (visible) { 1732 XForm.createXFormControl(fieldName, defaultValue, PARAMETER_FORM, xformHeader, body, visible); 1733 } else { 1734 try { 1735 String value = defaultValue.replaceAll("&", "&"); value = value.replaceAll("\"", "''"); body.append("<input type=\"hidden\" name=\"" + fieldName + "\" value=\"" + value + "\"></input>"); } catch (Exception e) { 1739 body.append("<input type=\"hidden\" name=\"" + fieldName + "\" value=\"" + defaultValue + "\"></input>"); } 1741 } 1742 parameterTemplate = parameterTemplate.replaceAll("\\{" + fieldName + "\\}", body.toString()); } else { 1744 if (visible) { 1745 StringBuffer body = new StringBuffer (); 1746 XForm.createXFormControl(fieldName, defaultValue, PARAMETER_FORM, xformHeader, body, visible); 1747 xformBody.append("<filter><id><![CDATA[" + fieldName + "]]></id>") .append("<title><![CDATA[" + displayName + "]]></title>") .append("<help><![CDATA[" + hint + "]]></help><control>") .append(body).append("</control></filter>"); 1752 } else { 1753 try { 1754 String value = defaultValue.replaceAll("&", "&"); value = value.replaceAll("\"", "''"); xformBody.append("<input type=\"hidden\" name=\"" + fieldName + "\" value=\"" + value + "\"></input>"); } catch (Exception e) { 1760 xformBody.append("<input type=\"hidden\" name=\"" + fieldName + "\" value=\"" + defaultValue + "\"></input>"); } 1762 } 1763 } 1764 xformFields.put(fieldName, fieldName); 1765 } 1766 1767 public void setParameterXsl(String xsl) { 1768 this.parameterXsl = xsl; 1769 } 1770 1771 public void setParameterTarget(String target) { 1772 this.parameterTarget = target; 1773 } 1774 1775 public String getLogId() { 1776 return logId; 1777 } 1778 1779 1784 public void forceSaveRuntimeData() { 1785 if (runtimeData != null) { 1786 runtimeData.forceSave(); 1787 } 1788 } 1789 1790 1797 public int getOutputPreference() { 1798 return outputHandler.getOutputPreference(); 1799 } 1800 1801 public void setOutputHandler(IOutputHandler outputHandler) { 1802 this.outputHandler = outputHandler; 1803 } 1804 1805} 1806 | Popular Tags |