1 17 package org.pentaho.core.solution; 18 19 import java.util.ArrayList ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 import java.util.Map ; 23 24 import org.apache.commons.collections.map.ListOrderedMap; 25 import org.dom4j.Document; 26 import org.dom4j.Node; 27 import org.pentaho.core.connection.memory.MemoryResultSet; 28 import org.pentaho.core.runtime.ActionParameter; 29 import org.pentaho.core.runtime.IActionParameter; 30 import org.pentaho.core.system.IApplicationContext; 31 import org.pentaho.core.system.PentahoSystem; 32 import org.pentaho.core.util.XmlHelper; 33 import org.pentaho.messages.Messages; 34 import org.pentaho.util.logging.ILogger; 35 import org.pentaho.util.logging.Logger; 36 37 public class SequenceDefinition implements ISequenceDefinition { 38 39 private static final boolean debug = PentahoSystem.debug; 40 41 private int errorCode; 42 43 private String solutionPath; 44 45 private String solutionName; 46 47 private String name; 48 49 private String version; 50 51 private String title; 52 53 private boolean isWebService; 54 55 private String cacheLevel; 56 57 private int loggingLevel; 58 59 private String description; 60 61 private String author; 62 63 private String help; 64 65 private String resultType; 66 67 private String iconPath; 68 69 private Map outputDefinitions; 70 71 private Map inputDefinitions; 72 73 private Map resourceDefinitions; 74 75 IApplicationContext applicationContext; 76 77 IActionDefinition actionDefinitions[]; 78 79 public static IActionSequence ActionSequenceFactory(Document document, String actionName, String solutionPath, String solutionName, ILogger logger, IApplicationContext applicationContext, int loggingLevel) { 80 81 Node sequenceDefinitionNode = document.selectSingleNode("//action-sequence"); if (sequenceDefinitionNode == null) { 84 logger.error(Messages.getErrorString("SequenceDefinition.ERROR_0002_NO_ACTION_SEQUENCE_NODE", solutionName, solutionPath, actionName)); return null; 86 } 87 88 ISequenceDefinition seqDef = new SequenceDefinition(sequenceDefinitionNode, actionName, solutionPath, solutionName, logger, applicationContext); 89 90 Node actionNode = sequenceDefinitionNode.selectSingleNode("actions"); 92 return (getNextLoopGroup(seqDef, actionNode, solutionPath, solutionName, logger, loggingLevel)); 93 } 94 95 private static IActionSequence getNextLoopGroup(ISequenceDefinition seqDef, Node actionsNode, String solutionPath, String solutionName, ILogger logger, int loggingLevel) { 96 97 String loopParameterName = XmlHelper.getNodeText("@loop-on", actionsNode); 99 Node actionDefinitionNode; 100 ActionDefinition actionDefinition; 101 102 List actionDefinitionList = new ArrayList (); 103 104 List nodeList = actionsNode.selectNodes("*"); Iterator actionDefinitionNodes = nodeList.iterator(); 106 while (actionDefinitionNodes.hasNext()) { 107 actionDefinitionNode = (Node) actionDefinitionNodes.next(); 108 if (actionDefinitionNode.getName().equals("actions")) { actionDefinitionList.add(getNextLoopGroup(seqDef, actionDefinitionNode, solutionPath, solutionName, logger, loggingLevel)); 110 } else if (actionDefinitionNode.getName().equals("action-definition")) { actionDefinition = new ActionDefinition(actionDefinitionNode, logger); 112 actionDefinition.setLoggingLevel(loggingLevel); 113 actionDefinitionList.add(actionDefinition); 114 } 115 } 116 117 if (actionDefinitionList.size() == 0) { 118 logger.error(Messages.getErrorString("SequenceDefinition.ERROR_0003_NO_ACTIONS_IN_FILE")); return null; 120 } 121 122 ConditionalExecution conditionalExecution = SequenceDefinition.parseConditionalExecution(actionsNode, logger, "condition"); 124 ActionSequence sequence = new ActionSequence(loopParameterName, seqDef, actionDefinitionList); 125 126 sequence.setConditionalExecution(conditionalExecution); 127 return sequence; 128 } 129 130 private SequenceDefinition(Node sequenceRootNode, String actionName, String solutionPath, String solutionName, ILogger logger, IApplicationContext applicationContext) { 131 132 134 this.name = actionName; 135 this.solutionName = solutionName; 136 this.solutionPath = solutionPath; 137 this.applicationContext = applicationContext; 138 139 version = XmlHelper.getNodeText("version", sequenceRootNode); title = XmlHelper.getNodeText("title", sequenceRootNode); 143 isWebService = "true".equals(XmlHelper.getNodeText("web-service", sequenceRootNode)); loggingLevel = Logger.getLogLevel(XmlHelper.getNodeText("logging-level", sequenceRootNode)); 146 description = XmlHelper.getNodeText("documentation/description", sequenceRootNode); help = XmlHelper.getNodeText("documentation/help", sequenceRootNode); author = XmlHelper.getNodeText("documentation/author", sequenceRootNode); resultType = XmlHelper.getNodeText("documentation/result-type", sequenceRootNode); iconPath = XmlHelper.getNodeText("documentation/icon", sequenceRootNode); 152 inputDefinitions = new ListOrderedMap(); 154 errorCode = parseParameters(sequenceRootNode, logger, "inputs/*", inputDefinitions, null, true); 156 outputDefinitions = new ListOrderedMap(); 158 errorCode = parseParameters(sequenceRootNode, logger, "outputs/*", outputDefinitions, null, false); 160 errorCode = parseResourceDefintions(sequenceRootNode, logger); 162 } 163 164 public String getVersion() { 165 return version; 166 } 167 168 public boolean isWebService() { 169 return isWebService; 170 } 171 172 public String getCacheLevel() { 173 return cacheLevel; 174 } 175 176 public int getErrorCode() { 177 return errorCode; 178 } 179 180 static ConditionalExecution parseConditionalExecution(Node actionRootNode, ILogger logger, String nodePath) { 181 try { 182 Node condition = actionRootNode.selectSingleNode(nodePath); 183 if (condition == null) { 184 return null; 185 } 186 String script = condition.getText(); 187 ConditionalExecution ce = new ConditionalExecution(); 188 ce.setScript(script); 189 return ce; 190 } catch (Exception ex) { 191 logger.error(Messages.getErrorString("SequenceDefinition.ERROR_0005_PARSING_CONDITIONAL_EXECUTION"), ex); } 193 return null; 194 } 195 196 static int parseParameters(Node actionRootNode, ILogger logger, String nodePath, Map parameterMap, Map mapTo, boolean inputVar) { 197 try { 198 List parameters = actionRootNode.selectNodes(nodePath); 199 200 Iterator parametersIterator = parameters.iterator(); 203 Node parameterNode; 204 String parameterName; 205 String parameterType; 206 ActionParameter parameter; 207 List variableNodes; 208 List variables; 209 Node variableNode; 210 Iterator variablesIterator; 211 String variableSource; 212 String variableName; 213 int variableIdx; 214 Object defaultValue = null; 215 216 while (parametersIterator.hasNext()) { 217 parameterNode = (Node) parametersIterator.next(); 218 parameterName = parameterNode.getName(); 219 parameterType = XmlHelper.getNodeText("@type", parameterNode); 221 if (mapTo != null) { 222 mapTo.put(parameterName, XmlHelper.getNodeText("@mapping", parameterNode, parameterName)); } 224 225 defaultValue = getDefaultValue(parameterNode); 226 variableNodes = parameterNode.selectNodes((inputVar) ? "sources/*" : "destinations/*"); variablesIterator = variableNodes.iterator(); 229 variableIdx = 1; 230 variables = new ArrayList (); 231 while (variablesIterator.hasNext()) { 232 variableNode = (Node) variablesIterator.next(); 233 try { 235 variableSource = variableNode.getName(); 236 variableName = variableNode.getText(); 237 ActionParameterSource variable = new ActionParameterSource(variableSource, variableName); 238 if (debug) 239 logger.debug(Messages.getString("SequenceDefinition.DEBUG_ADDING_SOURCE_FOR_PARAMETER", variableSource, parameterName)); 241 variables.add(variable); 242 } catch (Exception e) { 243 logger.error(Messages.getErrorString("SequenceDefinition.ERROR_0004_VARIABLE_SOURCE_NOT_VALID", Integer.toString(variableIdx), parameterName), e); } 245 variableIdx++; 246 } 247 if (defaultValue != null) { 248 if (debug) 249 logger.debug(Messages.getString("SequenceDefinition.DEBUG_USING_DEFAULT_VALUE", defaultValue.toString(), parameterName)); } 251 parameter = new ActionParameter(parameterName, parameterType, null, variables, defaultValue); 252 parameterMap.put(parameterName, parameter); 253 } 254 return ISequenceDefinition.ACTION_SEQUENCE_DEFINITION_OK; 255 } catch (Exception e) { 256 logger.error(Messages.getErrorString("SequenceDefinition.ERROR_0005_PARSING_PARAMETERS"), e); } 258 return ISequenceDefinition.ACTION_SEQUENCE_DEFINITION_INVALID_ACTION_DOC; 259 } 260 261 private int parseResourceDefintions(Node actionRootNode, ILogger logger) { 262 263 resourceDefinitions = new ListOrderedMap(); 264 265 try { 266 List resources = actionRootNode.selectNodes("resources/*"); 268 Iterator resourcesIterator = resources.iterator(); 271 272 Node resourceNode; 273 String resourceName; 274 String resourceTypeName; 275 String resourceMimeType; 276 int resourceType; 277 ActionResource resource; 278 Node typeNode, mimeNode; 279 while (resourcesIterator.hasNext()) { 280 resourceNode = (Node) resourcesIterator.next(); 281 typeNode = resourceNode.selectSingleNode("./*"); if (typeNode != null) { 283 resourceName = resourceNode.getName(); 284 resourceTypeName = typeNode.getName(); 285 resourceType = ActionResource.getResourceType(resourceTypeName); 286 String resourceLocation = XmlHelper.getNodeText("location", typeNode); if (resourceType == IActionResource.SOLUTION_FILE_RESOURCE) { 288 if (resourceLocation == null) { 289 logger.error(Messages.getErrorString("SequenceDefinition.ERROR_0008_RESOURCE_NO_LOCATION", resourceName)); continue; 291 } 292 } 293 else if(resourceType == IActionResource.STRING) { 294 resourceLocation = XmlHelper.getNodeText("string", resourceNode); } 296 else if(resourceType == IActionResource.XML) { 297 resourceLocation = XmlHelper.getNodeText("xml", resourceNode); } 299 mimeNode = typeNode.selectSingleNode("mime-type"); if (mimeNode != null) { 301 resourceMimeType = mimeNode.getText(); 302 resource = new ActionResource(resourceName, resourceType, resourceMimeType, solutionName, solutionPath, resourceLocation); 303 resourceDefinitions.put(resourceName, resource); 304 } else { 305 logger.error(Messages.getErrorString("SequenceDefinition.ERROR_0007_RESOURCE_NO_MIME_TYPE", resourceName)); } 307 } 308 } 312 return ISequenceDefinition.ACTION_SEQUENCE_DEFINITION_OK; 313 } catch (Exception e) { 314 logger.error(Messages.getErrorString("SequenceDefinition.ERROR_0006_PARSING_RESOURCE"), e); } 316 return ISequenceDefinition.ACTION_SEQUENCE_DEFINITION_INVALID_ACTION_DOC; 317 318 } 319 320 static int parseActionResourceDefintions(Node actionRootNode, ILogger logger, String nodePath, Map mapTo ) { 321 322 try { 323 List resources = actionRootNode.selectNodes( nodePath ); 324 325 Iterator resourcesIterator = resources.iterator(); 328 329 Node resourceNode; 330 String resourceName; 331 while (resourcesIterator.hasNext()) { 332 resourceNode = (Node) resourcesIterator.next(); 333 resourceName = resourceNode.getName(); 334 if (mapTo != null) { 335 mapTo.put(resourceName, XmlHelper.getNodeText("@mapping", resourceNode, resourceName)); } 337 } 338 return ISequenceDefinition.ACTION_SEQUENCE_DEFINITION_OK; 339 } catch (Exception e) { 340 logger.error(Messages.getErrorString("SequenceDefinition.ERROR_0006_PARSING_RESOURCE"), e); } 342 return ISequenceDefinition.ACTION_SEQUENCE_DEFINITION_INVALID_ACTION_DOC; 343 344 } 345 346 private static Object getDefaultValue(Node parameterNode) { 347 Node rootNode = parameterNode.selectSingleNode("default-value"); if (rootNode == null) { 349 return (null); 350 } 351 352 String dataType = XmlHelper.getNodeText("@type", rootNode); if ( dataType == null ) { 354 dataType = XmlHelper.getNodeText("@type", parameterNode); } 356 357 if ("string-list".equals(dataType)) { List nodes = rootNode.selectNodes("list-item"); if (nodes == null) { 360 return (null); 361 } 362 363 ArrayList rtnList = new ArrayList (); 364 for (Iterator it = nodes.iterator(); it.hasNext();) { 365 rtnList.add(((Node) it.next()).getText()); 366 } 367 return (rtnList); 368 } else if ("property-map-list".equals(dataType)) { List nodes = rootNode.selectNodes("property-map"); if (nodes == null) { 371 return (null); 372 } 373 374 ArrayList rtnList = new ArrayList (); 375 for (Iterator it = nodes.iterator(); it.hasNext();) { 376 Node mapNode = (Node) it.next(); 377 rtnList.add(getMapFromNode(mapNode)); 378 } 379 return (rtnList); 380 } else if ("property-map".equals(dataType)) { return (getMapFromNode(rootNode.selectSingleNode("property-map"))); } else if ("long".equals(dataType)) { try { 384 return (new Long (rootNode.getText())); 385 } catch (Exception e) { 386 } 387 return (null); 388 } else if ("result-set".equals(dataType)) { 390 return (MemoryResultSet.createFromActionSequenceInputsNode(parameterNode)); 391 } else { return (rootNode.getText()); 393 } 394 395 } 396 397 private static Map getMapFromNode(Node mapNode) { 398 Map rtnMap = new ListOrderedMap(); 399 400 if (mapNode != null) { 401 List nodes = mapNode.selectNodes("entry"); if (nodes != null) { 403 for (Iterator it = nodes.iterator(); it.hasNext();) { 404 Node entryNode = (Node) it.next(); 405 rtnMap.put(XmlHelper.getNodeText("@key", entryNode), entryNode.getText()); } 407 } 408 } 409 return (rtnMap); 410 } 411 412 417 public Map getInputDefinitions() { 418 return inputDefinitions; 419 } 420 421 public Map getInputDefinitionsForParameterProvider( String parameterProviderName ) { 422 Map rtnMap = new ListOrderedMap(); 423 424 Map paramList = getInputDefinitions(); 425 for( Iterator it = paramList.values().iterator(); it.hasNext(); ) { 426 IActionParameter actionParameter = (IActionParameter)it.next(); 427 List vars = actionParameter.getVariables(); 428 for (int i = 0; i < vars.size(); i++) { 429 ActionParameterSource source = (ActionParameterSource) (vars.get(i)); 430 if (source.getSourceName().equals( parameterProviderName )) { 431 rtnMap.put( source.getValue(), actionParameter ); 432 } 433 } 434 } 435 return( rtnMap ); 436 } 437 438 443 public Map getOutputDefinitions() { 444 return outputDefinitions; 445 } 446 447 452 public Map getResourceDefinitions() { 453 return resourceDefinitions; 454 } 455 456 public String getSequenceName() { 457 return name; 458 } 459 460 public String getAuthor() { 461 return author; 462 } 463 464 public String getDescription() { 465 return description; 466 } 467 468 public String getResultType() { 469 return resultType; 470 } 471 472 public String getHelp() { 473 return help; 474 } 475 476 public String getTitle() { 477 return title; 478 } 479 480 public String getSolutionName() { 481 return solutionName; 482 } 483 484 public String getSolutionPath() { 485 return solutionPath; 486 } 487 488 public int getLoggingLevel() { 489 return (loggingLevel); 490 } 491 492 public String getIcon() { 493 return iconPath; 494 } 495 496 } 497 | Popular Tags |