SequenceDefinition.java
| Index Score | ||
|---|---|---|
![]() |
![]() |
org.pentaho.core.solution |
![]() |
![]() |
Pentaho |
View: Reasons, Metrics, Source Code
These are the metrics that contribute to the Enerjy Score for this file, ranked by impact. So the metrics listed at the top influence the score to a greater extent that the metrics listed at the bottom.
/*
* Copyright 2006 Pentaho Corporation. All rights reserved.
* This software was developed by Pentaho Corporation and is provided under the terms
* of the Mozilla Public License, Version 1.1, or any later version. You may not use
* this file except in compliance with the license. If you need a copy of the license,
* please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
* BI Platform. The Initial Developer is Pentaho Corporation.
*
* Software distributed under the Mozilla Public License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
* the license for the specific language governing your rights and limitations.
*
* @created Jul 18, 2005
* @author James Dixon
*
*/
package org.pentaho.core.solution;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.commons.collections.map.ListOrderedMap;
import org.dom4j.Document;
import org.dom4j.Node;
import org.pentaho.commons.connection.memory.MemoryResultSet;
import org.pentaho.core.runtime.ActionParameter;
import org.pentaho.core.runtime.IActionParameter;
import org.pentaho.core.system.IApplicationContext;
import org.pentaho.core.system.PentahoSystem;
import org.pentaho.core.util.XmlHelper;
import org.pentaho.messages.Messages;
import org.pentaho.util.logging.ILogger;
import org.pentaho.util.logging.Logger;
public class SequenceDefinition implements ISequenceDefinition {
private static final boolean debug = PentahoSystem.debug;
private int errorCode;
private String solutionPath;
private String solutionName;
private String name;
private String version;
private String title;
private boolean isWebService;
private String cacheLevel;
private int loggingLevel;
private String description;
private String author;
private String help;
private String resultType;
private String iconPath;
private Map outputDefinitions;
private Map inputDefinitions;
private Map resourceDefinitions;
IApplicationContext applicationContext;
IActionDefinition actionDefinitions[];
public static IActionSequence ActionSequenceFactory(Document document, String actionName, String solutionPath,
String solutionName, ILogger logger, IApplicationContext applicationContext, int loggingLevel) {
// Check for a sequence document
Node sequenceDefinitionNode = document.selectSingleNode("//action-sequence"); //$NON-NLS-1$
if (sequenceDefinitionNode == null) {
logger.error(Messages.getErrorString(
"SequenceDefinition.ERROR_0002_NO_ACTION_SEQUENCE_NODE", solutionName, solutionPath, actionName)); //$NON-NLS-1$
return null;
}
ISequenceDefinition seqDef = new SequenceDefinition(sequenceDefinitionNode, actionName, solutionPath, solutionName,
logger, applicationContext);
Node actionNode = sequenceDefinitionNode.selectSingleNode("actions"); //$NON-NLS-1$
return (getNextLoopGroup(seqDef, actionNode, solutionPath, solutionName, logger, loggingLevel));
}
private static IActionSequence getNextLoopGroup(ISequenceDefinition seqDef, Node actionsNode, String solutionPath,
String solutionName, ILogger logger, int loggingLevel) {
String loopParameterName = XmlHelper.getNodeText("@loop-on", actionsNode); //$NON-NLS-1$
Node actionDefinitionNode;
ActionDefinition actionDefinition;
List actionDefinitionList = new ArrayList();
List nodeList = actionsNode.selectNodes("*"); //$NON-NLS-1$
Iterator actionDefinitionNodes = nodeList.iterator();
while (actionDefinitionNodes.hasNext()) {
actionDefinitionNode = (Node) actionDefinitionNodes.next();
if (actionDefinitionNode.getName().equals("actions")) { //$NON-NLS-1$
actionDefinitionList.add(getNextLoopGroup(seqDef, actionDefinitionNode, solutionPath, solutionName, logger,
loggingLevel));
} else if (actionDefinitionNode.getName().equals("action-definition")) { //$NON-NLS-1$
actionDefinition = new ActionDefinition(actionDefinitionNode, logger);
actionDefinition.setLoggingLevel(loggingLevel);
actionDefinitionList.add(actionDefinition);
}
}
// action sequences with 0 actions are valid, see: JIRA PLATFORM-837
ConditionalExecution conditionalExecution = SequenceDefinition.parseConditionalExecution(actionsNode, logger,
"condition"); //$NON-NLS-1$
ActionSequence sequence = new ActionSequence(loopParameterName, seqDef, actionDefinitionList);
sequence.setConditionalExecution(conditionalExecution);
return sequence;
}
private SequenceDefinition(Node sequenceRootNode, String actionName, String solutionPath, String solutionName,
ILogger logger, IApplicationContext applicationContext) {
// initialize this object from the contents of the xml
this.name = actionName;
this.solutionName = solutionName;
this.solutionPath = solutionPath;
this.applicationContext = applicationContext;
// get the descriptive entries
version = XmlHelper.getNodeText("version", sequenceRootNode); //$NON-NLS-1$
title = XmlHelper.getNodeText("title", sequenceRootNode); //$NON-NLS-1$
isWebService = "true".equals(XmlHelper.getNodeText("web-service", sequenceRootNode)); //$NON-NLS-1$ //$NON-NLS-2$
loggingLevel = Logger.getLogLevel(XmlHelper.getNodeText("logging-level", sequenceRootNode)); //$NON-NLS-1$
description = XmlHelper.getNodeText("documentation/description", sequenceRootNode); //$NON-NLS-1$
help = XmlHelper.getNodeText("documentation/help", sequenceRootNode); //$NON-NLS-1$
author = XmlHelper.getNodeText("documentation/author", sequenceRootNode); //$NON-NLS-1$
resultType = XmlHelper.getNodeText("documentation/result-type", sequenceRootNode); //$NON-NLS-1$
iconPath = XmlHelper.getNodeText("documentation/icon", sequenceRootNode); //$NON-NLS-1$
// get the input parameter definitions
inputDefinitions = new ListOrderedMap();
errorCode = parseParameters(sequenceRootNode, logger, "inputs/*", inputDefinitions, null, true); //$NON-NLS-1$
// get the ouput definitions
outputDefinitions = new ListOrderedMap();
errorCode = parseParameters(sequenceRootNode, logger, "outputs/*", outputDefinitions, null, false); //$NON-NLS-1$
if (errorCode != ISequenceDefinition.ACTION_SEQUENCE_DEFINITION_OK) {
logger.info(Messages.getString("SequenceDefinition.INFO_OUTPUT_PARAMETERS_NOT_DEFINED")); //$NON-NLS-1$
}
// get the resource definitions
errorCode = parseResourceDefinitions(sequenceRootNode, logger);
if (errorCode != ISequenceDefinition.ACTION_SEQUENCE_DEFINITION_OK) {
logger.info(Messages.getString("SequenceDefinition.INFO_RESOURCES_PARAMETERS_NOT_DEFINED")); //$NON-NLS-1$
}
}
public String getVersion() {
return version;
}
public boolean isWebService() {
return isWebService;
}
public String getCacheLevel() {
return cacheLevel;
}
public int getErrorCode() {
return errorCode;
}
static ConditionalExecution parseConditionalExecution(Node actionRootNode, ILogger logger, String nodePath) {
try {
Node condition = actionRootNode.selectSingleNode(nodePath);
if (condition == null) {
return null;
}
String script = condition.getText();
ConditionalExecution ce = new ConditionalExecution();
ce.setScript(script);
return ce;
} catch (Exception ex) {
logger.error(Messages.getErrorString("SequenceDefinition.ERROR_0005_PARSING_CONDITIONAL_EXECUTION"), ex); //$NON-NLS-1$
}
return null;
}
static int parseParameters(Node actionRootNode, ILogger logger, String nodePath, Map parameterMap, Map mapTo,
boolean inputVar) {
try {
List parameters = actionRootNode.selectNodes(nodePath);
// TODO create objects to represent the types
// TODO need source variable list
Iterator parametersIterator = parameters.iterator();
Node parameterNode;
String parameterName;
String parameterType;
ActionParameter parameter;
List variableNodes;
List variables;
Node variableNode;
Iterator variablesIterator;
String variableSource;
String variableName;
int variableIdx;
Object defaultValue = null;
while (parametersIterator.hasNext()) {
parameterNode = (Node) parametersIterator.next();
parameterName = parameterNode.getName();
parameterType = XmlHelper.getNodeText("@type", parameterNode); //$NON-NLS-1$
if (mapTo != null) {
mapTo.put(parameterName, XmlHelper.getNodeText("@mapping", parameterNode, parameterName)); //$NON-NLS-1$
}
defaultValue = getDefaultValue(parameterNode);
// get the list of sources for this parameter
variableNodes = parameterNode.selectNodes((inputVar) ? "sources/*" : "destinations/*"); //$NON-NLS-1$ //$NON-NLS-2$
variablesIterator = variableNodes.iterator();
variableIdx = 1;
variables = new ArrayList();
while (variablesIterator.hasNext()) {
variableNode = (Node) variablesIterator.next();
// try to resolve the parameter value for this
try {
variableSource = variableNode.getName();
variableName = variableNode.getText();
ActionParameterSource variable = new ActionParameterSource(variableSource, variableName);
if (debug)
logger.debug(Messages.getString(
"SequenceDefinition.DEBUG_ADDING_SOURCE_FOR_PARAMETER", variableSource, parameterName)); //$NON-NLS-1$
variables.add(variable);
} catch (Exception e) {
logger
.error(
Messages
.getErrorString(
"SequenceDefinition.ERROR_0004_VARIABLE_SOURCE_NOT_VALID", Integer.toString(variableIdx), parameterName), e); //$NON-NLS-1$
}
variableIdx++;
}
if (defaultValue != null) {
if (debug)
logger.debug(Messages.getString(
"SequenceDefinition.DEBUG_USING_DEFAULT_VALUE", defaultValue.toString(), parameterName)); //$NON-NLS-1$
}
parameter = new ActionParameter(parameterName, parameterType, null, variables, defaultValue);
parameterMap.put(parameterName, parameter);
}
return ISequenceDefinition.ACTION_SEQUENCE_DEFINITION_OK;
} catch (Exception e) {
logger.error(Messages.getErrorString("SequenceDefinition.ERROR_0005_PARSING_PARAMETERS"), e); //$NON-NLS-1$
}
return ISequenceDefinition.ACTION_SEQUENCE_DEFINITION_INVALID_ACTION_DOC;
}
private int parseResourceDefinitions(Node actionRootNode, ILogger logger) {
resourceDefinitions = new ListOrderedMap();
try {
List resources = actionRootNode.selectNodes("resources/*"); //$NON-NLS-1$
// TODO create objects to represent the types
// TODO need source variable list
Iterator resourcesIterator = resources.iterator();
Node resourceNode;
String resourceName;
String resourceTypeName;
String resourceMimeType;
int resourceType;
ActionResource resource;
Node typeNode, mimeNode;
while (resourcesIterator.hasNext()) {
resourceNode = (Node) resourcesIterator.next();
typeNode = resourceNode.selectSingleNode("./*"); //$NON-NLS-1$
if (typeNode != null) {
resourceName = resourceNode.getName();
resourceTypeName = typeNode.getName();
resourceType = ActionResource.getResourceType(resourceTypeName);
String resourceLocation = XmlHelper.getNodeText("location", typeNode); //$NON-NLS-1$
if (resourceType == IActionResource.SOLUTION_FILE_RESOURCE) {
if (resourceLocation == null) {
logger.error(Messages.getErrorString("SequenceDefinition.ERROR_0008_RESOURCE_NO_LOCATION", resourceName)); //$NON-NLS-1$
continue;
}
} else if (resourceType == IActionResource.STRING) {
resourceLocation = XmlHelper.getNodeText("string", resourceNode); //$NON-NLS-1$
} else if (resourceType == IActionResource.XML) {
//resourceLocation = XmlHelper.getNodeText("xml", resourceNode); //$NON-NLS-1$
Node xmlNode = typeNode.selectSingleNode("./location/*"); //$NON-NLS-1$
// Danger, we have now lost the character encoding of the XML in this node
// see BISERVER-895
resourceLocation = (xmlNode == null) ? "" : xmlNode.asXML(); //$NON-NLS-1$
}
mimeNode = typeNode.selectSingleNode("mime-type"); //$NON-NLS-1$
if (mimeNode != null) {
resourceMimeType = mimeNode.getText();
resource = new ActionResource(resourceName, resourceType, resourceMimeType, solutionName, solutionPath,
resourceLocation);
resourceDefinitions.put(resourceName, resource);
} else {
logger.error(Messages.getErrorString("SequenceDefinition.ERROR_0007_RESOURCE_NO_MIME_TYPE", resourceName)); //$NON-NLS-1$
}
}
// input = new ActionParameter( resourceName, resourceType, null
// );
// resourceDefinitions.put( inputName, input );
}
return ISequenceDefinition.ACTION_SEQUENCE_DEFINITION_OK;
} catch (Exception e) {
logger.error(Messages.getErrorString("SequenceDefinition.ERROR_0006_PARSING_RESOURCE"), e); //$NON-NLS-1$
}
return ISequenceDefinition.ACTION_SEQUENCE_DEFINITION_INVALID_ACTION_DOC;
}
/**
* sbarkdull: method appears to never be used anywhere
*
* @param actionRootNode
* @param logger
* @param nodePath
* @param mapTo
* @return
*/
static int parseActionResourceDefinitions(Node actionRootNode, ILogger logger, String nodePath, Map mapTo) {
try {
List resources = actionRootNode.selectNodes(nodePath);
// TODO create objects to represent the types
// TODO need source variable list
Iterator resourcesIterator = resources.iterator();
Node resourceNode;
String resourceName;
while (resourcesIterator.hasNext()) {
resourceNode = (Node) resourcesIterator.next();
resourceName = resourceNode.getName();
if (mapTo != null) {
mapTo.put(resourceName, XmlHelper.getNodeText("@mapping", resourceNode, resourceName)); //$NON-NLS-1$
}
}
return ISequenceDefinition.ACTION_SEQUENCE_DEFINITION_OK;
} catch (Exception e) {
logger.error(Messages.getErrorString("SequenceDefinition.ERROR_0006_PARSING_RESOURCE"), e); //$NON-NLS-1$
}
return ISequenceDefinition.ACTION_SEQUENCE_DEFINITION_INVALID_ACTION_DOC;
}
private static Object getDefaultValue(Node parameterNode) {
Node rootNode = parameterNode.selectSingleNode("default-value"); //$NON-NLS-1$
if (rootNode == null) {
return (null);
}
String dataType = XmlHelper.getNodeText("@type", rootNode); //$NON-NLS-1$
if (dataType == null) {
dataType = XmlHelper.getNodeText("@type", parameterNode); //$NON-NLS-1$
}
if ("string-list".equals(dataType)) { //$NON-NLS-1$
List nodes = rootNode.selectNodes("list-item"); //$NON-NLS-1$
if (nodes == null) {
return (null);
}
ArrayList rtnList = new ArrayList();
for (Iterator it = nodes.iterator(); it.hasNext();) {
rtnList.add(((Node) it.next()).getText());
}
return (rtnList);
} else if ("property-map-list".equals(dataType)) { //$NON-NLS-1$
List nodes = rootNode.selectNodes("property-map"); //$NON-NLS-1$
if (nodes == null) {
return (null);
}
ArrayList rtnList = new ArrayList();
for (Iterator it = nodes.iterator(); it.hasNext();) {
Node mapNode = (Node) it.next();
rtnList.add(getMapFromNode(mapNode));
}
return (rtnList);
} else if ("property-map".equals(dataType)) { //$NON-NLS-1$
return (getMapFromNode(rootNode.selectSingleNode("property-map"))); //$NON-NLS-1$
} else if ("long".equals(dataType)) { //$NON-NLS-1$
try {
return (new Long(rootNode.getText()));
} catch (Exception e) {
}
return (null);
} else if ("result-set".equals(dataType)) { //$NON-NLS-1$
return (MemoryResultSet.createFromActionSequenceInputsNode(parameterNode));
} else { // Assume String
return (rootNode.getText());
}
}
private static Map getMapFromNode(Node mapNode) {
Map rtnMap = new ListOrderedMap();
if (mapNode != null) {
List nodes = mapNode.selectNodes("entry"); //$NON-NLS-1$
if (nodes != null) {
for (Iterator it = nodes.iterator(); it.hasNext();) {
Node entryNode = (Node) it.next();
rtnMap.put(XmlHelper.getNodeText("@key", entryNode), entryNode.getText()); //$NON-NLS-1$
}
}
}
return (rtnMap);
}
/*
* (non-Javadoc)
*
* @see org.pentaho.newcode.IActionDefinition#getParamDefs()
*/
public Map getInputDefinitions() {
return inputDefinitions;
}
public Map getInputDefinitionsForParameterProvider(String parameterProviderName) {
Map rtnMap = new ListOrderedMap();
Map paramList = getInputDefinitions();
for (Iterator it = paramList.values().iterator(); it.hasNext();) {
IActionParameter actionParameter = (IActionParameter) it.next();
List vars = actionParameter.getVariables();
for (int i = 0; i < vars.size(); i++) {
ActionParameterSource source = (ActionParameterSource) (vars.get(i));
if (source.getSourceName().equals(parameterProviderName)) {
rtnMap.put(source.getValue(), actionParameter);
}
}
}
return (rtnMap);
}
/*
* (non-Javadoc)
*
* @see org.pentaho.newcode.IActionDefinition#getOutputDefs()
*/
public Map getOutputDefinitions() {
return outputDefinitions;
}
/*
* (non-Javadoc)
*
* @see org.pentaho.newcode.IActionDefinition#getResourceDefs()
*/
public Map getResourceDefinitions() {
return resourceDefinitions;
}
public String getSequenceName() {
return name;
}
public String getAuthor() {
return author;
}
public String getDescription() {
return description;
}
public String getResultType() {
return resultType;
}
public String getHelp() {
return help;
}
public String getTitle() {
return title;
}
public String getSolutionName() {
return solutionName;
}
public String getSolutionPath() {
return solutionPath;
}
public int getLoggingLevel() {
return (loggingLevel);
}
public String getIcon() {
return iconPath;
}
}
The table below shows all metrics for SequenceDefinition.java.




