KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > core > solution > SequenceDefinition


1 /*
2  * Copyright 2006 Pentaho Corporation. All rights reserved.
3  * This software was developed by Pentaho Corporation and is provided under the terms
4  * of the Mozilla Public License, Version 1.1, or any later version. You may not use
5  * this file except in compliance with the license. If you need a copy of the license,
6  * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
7  * BI Platform. The Initial Developer is Pentaho Corporation.
8  *
9  * Software distributed under the Mozilla Public License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
11  * the license for the specific language governing your rights and limitations.
12  *
13  * @created Jul 18, 2005
14  * @author James Dixon
15  *
16  */

17 package org.pentaho.core.solution;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
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 JavaDoc solutionPath;
44
45     private String JavaDoc solutionName;
46
47     private String JavaDoc name;
48
49     private String JavaDoc version;
50
51     private String JavaDoc title;
52
53     private boolean isWebService;
54
55     private String JavaDoc cacheLevel;
56
57     private int loggingLevel;
58
59     private String JavaDoc description;
60
61     private String JavaDoc author;
62
63     private String JavaDoc help;
64
65     private String JavaDoc resultType;
66
67     private String JavaDoc iconPath;
68
69     private Map JavaDoc outputDefinitions;
70
71     private Map JavaDoc inputDefinitions;
72
73     private Map JavaDoc resourceDefinitions;
74
75     IApplicationContext applicationContext;
76
77     IActionDefinition actionDefinitions[];
78
79     public static IActionSequence ActionSequenceFactory(Document document, String JavaDoc actionName, String JavaDoc solutionPath, String JavaDoc solutionName, ILogger logger, IApplicationContext applicationContext, int loggingLevel) {
80
81         // Check for a sequence document
82
Node sequenceDefinitionNode = document.selectSingleNode("//action-sequence"); //$NON-NLS-1$
83
if (sequenceDefinitionNode == null) {
84             logger.error(Messages.getErrorString("SequenceDefinition.ERROR_0002_NO_ACTION_SEQUENCE_NODE", solutionName, solutionPath, actionName)); //$NON-NLS-1$
85
return null;
86         }
87
88         ISequenceDefinition seqDef = new SequenceDefinition(sequenceDefinitionNode, actionName, solutionPath, solutionName, logger, applicationContext);
89
90         Node actionNode = sequenceDefinitionNode.selectSingleNode("actions"); //$NON-NLS-1$
91

92         return (getNextLoopGroup(seqDef, actionNode, solutionPath, solutionName, logger, loggingLevel));
93     }
94
95     private static IActionSequence getNextLoopGroup(ISequenceDefinition seqDef, Node actionsNode, String JavaDoc solutionPath, String JavaDoc solutionName, ILogger logger, int loggingLevel) {
96
97         String JavaDoc loopParameterName = XmlHelper.getNodeText("@loop-on", actionsNode); //$NON-NLS-1$
98

99         Node actionDefinitionNode;
100         ActionDefinition actionDefinition;
101
102         List JavaDoc actionDefinitionList = new ArrayList JavaDoc();
103
104         List JavaDoc nodeList = actionsNode.selectNodes("*"); //$NON-NLS-1$
105
Iterator JavaDoc actionDefinitionNodes = nodeList.iterator();
106         while (actionDefinitionNodes.hasNext()) {
107             actionDefinitionNode = (Node) actionDefinitionNodes.next();
108             if (actionDefinitionNode.getName().equals("actions")) { //$NON-NLS-1$
109
actionDefinitionList.add(getNextLoopGroup(seqDef, actionDefinitionNode, solutionPath, solutionName, logger, loggingLevel));
110             } else if (actionDefinitionNode.getName().equals("action-definition")) { //$NON-NLS-1$
111
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")); //$NON-NLS-1$
119
return null;
120         }
121         
122         ConditionalExecution conditionalExecution = SequenceDefinition.parseConditionalExecution(actionsNode, logger, "condition"); //$NON-NLS-1$
123

124         ActionSequence sequence = new ActionSequence(loopParameterName, seqDef, actionDefinitionList);
125         
126         sequence.setConditionalExecution(conditionalExecution);
127         return sequence;
128     }
129
130     private SequenceDefinition(Node sequenceRootNode, String JavaDoc actionName, String JavaDoc solutionPath, String JavaDoc solutionName, ILogger logger, IApplicationContext applicationContext) {
131
132         // initialize this object from the contents of the xml
133

134         this.name = actionName;
135         this.solutionName = solutionName;
136         this.solutionPath = solutionPath;
137         this.applicationContext = applicationContext;
138
139         // get the descriptive entries
140
version = XmlHelper.getNodeText("version", sequenceRootNode); //$NON-NLS-1$
141
title = XmlHelper.getNodeText("title", sequenceRootNode); //$NON-NLS-1$
142

143         isWebService = "true".equals(XmlHelper.getNodeText("web-service", sequenceRootNode)); //$NON-NLS-1$ //$NON-NLS-2$
144
loggingLevel = Logger.getLogLevel(XmlHelper.getNodeText("logging-level", sequenceRootNode)); //$NON-NLS-1$
145

146         description = XmlHelper.getNodeText("documentation/description", sequenceRootNode); //$NON-NLS-1$
147
help = XmlHelper.getNodeText("documentation/help", sequenceRootNode); //$NON-NLS-1$
148
author = XmlHelper.getNodeText("documentation/author", sequenceRootNode); //$NON-NLS-1$
149
resultType = XmlHelper.getNodeText("documentation/result-type", sequenceRootNode); //$NON-NLS-1$
150
iconPath = XmlHelper.getNodeText("documentation/icon", sequenceRootNode); //$NON-NLS-1$
151

152         // get the input parameter definitions
153
inputDefinitions = new ListOrderedMap();
154         errorCode = parseParameters(sequenceRootNode, logger, "inputs/*", inputDefinitions, null, true); //$NON-NLS-1$
155

156         // get the ouput definitions
157
outputDefinitions = new ListOrderedMap();
158         errorCode = parseParameters(sequenceRootNode, logger, "outputs/*", outputDefinitions, null, false); //$NON-NLS-1$
159

160         // get the resource definitions
161
errorCode = parseResourceDefintions(sequenceRootNode, logger);
162     }
163
164     public String JavaDoc getVersion() {
165         return version;
166     }
167
168     public boolean isWebService() {
169         return isWebService;
170     }
171
172     public String JavaDoc getCacheLevel() {
173         return cacheLevel;
174     }
175
176     public int getErrorCode() {
177         return errorCode;
178     }
179
180     static ConditionalExecution parseConditionalExecution(Node actionRootNode, ILogger logger, String JavaDoc nodePath) {
181       try {
182         Node condition = actionRootNode.selectSingleNode(nodePath);
183         if (condition == null) {
184           return null;
185         }
186         String JavaDoc script = condition.getText();
187         ConditionalExecution ce = new ConditionalExecution();
188         ce.setScript(script);
189         return ce;
190       } catch (Exception JavaDoc ex) {
191         logger.error(Messages.getErrorString("SequenceDefinition.ERROR_0005_PARSING_CONDITIONAL_EXECUTION"), ex); //$NON-NLS-1$
192
}
193       return null;
194     }
195     
196     static int parseParameters(Node actionRootNode, ILogger logger, String JavaDoc nodePath, Map JavaDoc parameterMap, Map JavaDoc mapTo, boolean inputVar) {
197         try {
198             List JavaDoc parameters = actionRootNode.selectNodes(nodePath);
199
200             // TODO create objects to represent the types
201
// TODO need source variable list
202
Iterator JavaDoc parametersIterator = parameters.iterator();
203             Node parameterNode;
204             String JavaDoc parameterName;
205             String JavaDoc parameterType;
206             ActionParameter parameter;
207             List JavaDoc variableNodes;
208             List JavaDoc variables;
209             Node variableNode;
210             Iterator JavaDoc variablesIterator;
211             String JavaDoc variableSource;
212             String JavaDoc variableName;
213             int variableIdx;
214             Object JavaDoc defaultValue = null;
215
216             while (parametersIterator.hasNext()) {
217                 parameterNode = (Node) parametersIterator.next();
218                 parameterName = parameterNode.getName();
219                 parameterType = XmlHelper.getNodeText("@type", parameterNode); //$NON-NLS-1$
220

221                 if (mapTo != null) {
222                     mapTo.put(parameterName, XmlHelper.getNodeText("@mapping", parameterNode, parameterName)); //$NON-NLS-1$
223
}
224
225                 defaultValue = getDefaultValue(parameterNode);
226                 // get the list of sources for this parameter
227
variableNodes = parameterNode.selectNodes((inputVar) ? "sources/*" : "destinations/*"); //$NON-NLS-1$ //$NON-NLS-2$
228
variablesIterator = variableNodes.iterator();
229                 variableIdx = 1;
230                 variables = new ArrayList JavaDoc();
231                 while (variablesIterator.hasNext()) {
232                     variableNode = (Node) variablesIterator.next();
233                     // try to resolve the parameter value for this
234
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)); //$NON-NLS-1$
240

241                         variables.add(variable);
242                     } catch (Exception JavaDoc e) {
243                         logger.error(Messages.getErrorString("SequenceDefinition.ERROR_0004_VARIABLE_SOURCE_NOT_VALID", Integer.toString(variableIdx), parameterName), e); //$NON-NLS-1$
244
}
245                     variableIdx++;
246                 }
247                 if (defaultValue != null) {
248                     if (debug)
249                         logger.debug(Messages.getString("SequenceDefinition.DEBUG_USING_DEFAULT_VALUE", defaultValue.toString(), parameterName)); //$NON-NLS-1$
250
}
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 JavaDoc e) {
256             logger.error(Messages.getErrorString("SequenceDefinition.ERROR_0005_PARSING_PARAMETERS"), e); //$NON-NLS-1$
257
}
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 JavaDoc resources = actionRootNode.selectNodes("resources/*"); //$NON-NLS-1$
267

268             // TODO create objects to represent the types
269
// TODO need source variable list
270
Iterator JavaDoc resourcesIterator = resources.iterator();
271
272             Node resourceNode;
273             String JavaDoc resourceName;
274             String JavaDoc resourceTypeName;
275             String JavaDoc resourceMimeType;
276             int resourceType;
277             ActionResource resource;
278             Node typeNode, mimeNode;
279             while (resourcesIterator.hasNext()) {
280                 resourceNode = (Node) resourcesIterator.next();
281                 typeNode = resourceNode.selectSingleNode("./*"); //$NON-NLS-1$
282
if (typeNode != null) {
283                     resourceName = resourceNode.getName();
284                     resourceTypeName = typeNode.getName();
285                     resourceType = ActionResource.getResourceType(resourceTypeName);
286                     String JavaDoc resourceLocation = XmlHelper.getNodeText("location", typeNode); //$NON-NLS-1$
287
if (resourceType == IActionResource.SOLUTION_FILE_RESOURCE) {
288                         if (resourceLocation == null) {
289                             logger.error(Messages.getErrorString("SequenceDefinition.ERROR_0008_RESOURCE_NO_LOCATION", resourceName)); //$NON-NLS-1$
290
continue;
291                         }
292                     }
293                     else if(resourceType == IActionResource.STRING) {
294                         resourceLocation = XmlHelper.getNodeText("string", resourceNode); //$NON-NLS-1$
295
}
296                     else if(resourceType == IActionResource.XML) {
297                         resourceLocation = XmlHelper.getNodeText("xml", resourceNode); //$NON-NLS-1$
298
}
299                     mimeNode = typeNode.selectSingleNode("mime-type"); //$NON-NLS-1$
300
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)); //$NON-NLS-1$
306
}
307                 }
308                 // input = new ActionParameter( resourceName, resourceType, null
309
// );
310
// resourceDefinitions.put( inputName, input );
311
}
312             return ISequenceDefinition.ACTION_SEQUENCE_DEFINITION_OK;
313         } catch (Exception JavaDoc e) {
314             logger.error(Messages.getErrorString("SequenceDefinition.ERROR_0006_PARSING_RESOURCE"), e); //$NON-NLS-1$
315
}
316         return ISequenceDefinition.ACTION_SEQUENCE_DEFINITION_INVALID_ACTION_DOC;
317
318     }
319
320     static int parseActionResourceDefintions(Node actionRootNode, ILogger logger, String JavaDoc nodePath, Map JavaDoc mapTo ) {
321
322         try {
323             List JavaDoc resources = actionRootNode.selectNodes( nodePath );
324
325             // TODO create objects to represent the types
326
// TODO need source variable list
327
Iterator JavaDoc resourcesIterator = resources.iterator();
328
329             Node resourceNode;
330             String JavaDoc 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)); //$NON-NLS-1$
336
}
337             }
338             return ISequenceDefinition.ACTION_SEQUENCE_DEFINITION_OK;
339         } catch (Exception JavaDoc e) {
340             logger.error(Messages.getErrorString("SequenceDefinition.ERROR_0006_PARSING_RESOURCE"), e); //$NON-NLS-1$
341
}
342         return ISequenceDefinition.ACTION_SEQUENCE_DEFINITION_INVALID_ACTION_DOC;
343
344     }
345
346     private static Object JavaDoc getDefaultValue(Node parameterNode) {
347         Node rootNode = parameterNode.selectSingleNode("default-value"); //$NON-NLS-1$
348
if (rootNode == null) {
349             return (null);
350         }
351
352         String JavaDoc dataType = XmlHelper.getNodeText("@type", rootNode); //$NON-NLS-1$
353
if ( dataType == null ) {
354             dataType = XmlHelper.getNodeText("@type", parameterNode); //$NON-NLS-1$
355
}
356
357         if ("string-list".equals(dataType)) { //$NON-NLS-1$
358
List JavaDoc nodes = rootNode.selectNodes("list-item"); //$NON-NLS-1$
359
if (nodes == null) {
360                 return (null);
361             }
362
363             ArrayList JavaDoc rtnList = new ArrayList JavaDoc();
364             for (Iterator JavaDoc it = nodes.iterator(); it.hasNext();) {
365                 rtnList.add(((Node) it.next()).getText());
366             }
367             return (rtnList);
368         } else if ("property-map-list".equals(dataType)) { //$NON-NLS-1$
369
List JavaDoc nodes = rootNode.selectNodes("property-map"); //$NON-NLS-1$
370
if (nodes == null) {
371                 return (null);
372             }
373
374             ArrayList JavaDoc rtnList = new ArrayList JavaDoc();
375             for (Iterator JavaDoc 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)) { //$NON-NLS-1$
381
return (getMapFromNode(rootNode.selectSingleNode("property-map"))); //$NON-NLS-1$
382
} else if ("long".equals(dataType)) { //$NON-NLS-1$
383
try {
384                 return (new Long JavaDoc(rootNode.getText()));
385             } catch (Exception JavaDoc e) {
386             }
387             return (null);
388         } else if ("result-set".equals(dataType)) { //$NON-NLS-1$
389

390             return (MemoryResultSet.createFromActionSequenceInputsNode(parameterNode));
391         } else { // Assume String
392
return (rootNode.getText());
393         }
394
395     }
396
397     private static Map JavaDoc getMapFromNode(Node mapNode) {
398         Map JavaDoc rtnMap = new ListOrderedMap();
399
400         if (mapNode != null) {
401             List JavaDoc nodes = mapNode.selectNodes("entry"); //$NON-NLS-1$
402
if (nodes != null) {
403                 for (Iterator JavaDoc it = nodes.iterator(); it.hasNext();) {
404                     Node entryNode = (Node) it.next();
405                     rtnMap.put(XmlHelper.getNodeText("@key", entryNode), entryNode.getText()); //$NON-NLS-1$
406
}
407             }
408         }
409         return (rtnMap);
410     }
411
412     /*
413      * (non-Javadoc)
414      *
415      * @see org.pentaho.newcode.IActionDefinition#getParamDefs()
416      */

417     public Map JavaDoc getInputDefinitions() {
418         return inputDefinitions;
419     }
420     
421     public Map JavaDoc getInputDefinitionsForParameterProvider( String JavaDoc parameterProviderName ) {
422         Map JavaDoc rtnMap = new ListOrderedMap();
423     
424         Map JavaDoc paramList = getInputDefinitions();
425         for( Iterator JavaDoc it = paramList.values().iterator(); it.hasNext(); ) {
426             IActionParameter actionParameter = (IActionParameter)it.next();
427             List JavaDoc 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     /*
439      * (non-Javadoc)
440      *
441      * @see org.pentaho.newcode.IActionDefinition#getOutputDefs()
442      */

443     public Map JavaDoc getOutputDefinitions() {
444         return outputDefinitions;
445     }
446
447     /*
448      * (non-Javadoc)
449      *
450      * @see org.pentaho.newcode.IActionDefinition#getResourceDefs()
451      */

452     public Map JavaDoc getResourceDefinitions() {
453         return resourceDefinitions;
454     }
455
456     public String JavaDoc getSequenceName() {
457         return name;
458     }
459
460     public String JavaDoc getAuthor() {
461         return author;
462     }
463
464     public String JavaDoc getDescription() {
465         return description;
466     }
467
468     public String JavaDoc getResultType() {
469         return resultType;
470     }
471
472     public String JavaDoc getHelp() {
473         return help;
474     }
475
476     public String JavaDoc getTitle() {
477         return title;
478     }
479
480     public String JavaDoc getSolutionName() {
481         return solutionName;
482     }
483
484     public String JavaDoc getSolutionPath() {
485         return solutionPath;
486     }
487
488     public int getLoggingLevel() {
489         return (loggingLevel);
490     }
491
492     public String JavaDoc getIcon() {
493         return iconPath;
494     }
495
496 }
497
Popular Tags