001: /*
002: * Copyright 2006 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
007: * BI Platform. The Initial Developer is Pentaho Corporation.
008: *
009: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
011: * the license for the specific language governing your rights and limitations.
012: *
013: * @created Jun 24, 2005
014: * @author Doug Moran
015: */
016:
017: package org.pentaho.plugin.misc;
018:
019: import java.text.MessageFormat;
020: import java.util.ArrayList;
021: import java.util.HashMap;
022: import java.util.Iterator;
023: import java.util.List;
024: import java.util.Map;
025: import java.util.Set;
026:
027: import org.apache.commons.logging.Log;
028: import org.apache.commons.logging.LogFactory;
029: import org.dom4j.Element;
030: import org.dom4j.Node;
031: import org.pentaho.actionsequence.dom.ActionInput;
032: import org.pentaho.actionsequence.dom.ActionOutput;
033: import org.pentaho.actionsequence.dom.IActionInputValueProvider;
034: import org.pentaho.actionsequence.dom.actions.ActionDefinition;
035: import org.pentaho.actionsequence.dom.actions.CopyParamAction;
036: import org.pentaho.actionsequence.dom.actions.FormatMsgAction;
037: import org.pentaho.actionsequence.dom.actions.PrintMapValsAction;
038: import org.pentaho.actionsequence.dom.actions.PrintParamAction;
039: import org.pentaho.messages.Messages;
040: import org.pentaho.plugin.ComponentBase;
041:
042: /**
043: *
044: * Provides utilities to help manipulate parameters used in action sequences.
045: * <p>
046: * <ul>
047: * <li><i>format</i> - Java style message formatting</li>
048: * <li><i>getvalues</i> - Make the key value pairs from a property map
049: * available as action-outputs</li>
050: * <li><i>copy</i> - Set the action-output with the value of the action input</li>
051: * <li><i>tostring</i> - Sets the action-output to the string value of the
052: * action-input</li>
053: * <li><i></i> - </li>
054: */
055: public class UtilityComponent extends ComponentBase {
056:
057: /**
058: *
059: */
060: private static final long serialVersionUID = -3257037449482351540L;
061:
062: HashMap tmpOutputs = new HashMap();
063:
064: /*
065: * (non-Javadoc)
066: *
067: * @see org.pentaho.component.ComponentBase#validate()
068: */
069:
070: public Log getLogger() {
071: return LogFactory.getLog(UtilityComponent.class);
072: }
073:
074: protected boolean validateSystemSettings() {
075: // This component does not have any system settings to validate
076: return true;
077: }
078:
079: /**
080: * @deprecated
081: */
082: private boolean validateAction(ActionDefinition actionDefinition) {
083: boolean result = true;
084: Element[] elements = actionDefinition
085: .getComponentDefElements("*");
086: for (int i = 0; i < elements.length; i++) {
087: String commandName = elements[i].getName();
088: if ("format".equalsIgnoreCase(commandName)) { //$NON-NLS-1$
089: if (elements[i].selectSingleNode("format-string") == null) {
090: error(Messages
091: .getErrorString(
092: "TestComponent.ERROR_0002_PARAMETER_MISSING", "format-string")); //$NON-NLS-1$
093: result = false;
094: }
095: } else if ("print".equalsIgnoreCase(commandName)) { //$NON-NLS-1$
096: // Do nothing.
097: } else if ("copy".equalsIgnoreCase(commandName)) { //$NON-NLS-1$
098: // Do nothing.
099: } else if ("getmapvalues".equalsIgnoreCase(commandName)) { //$NON-NLS-1$
100: if (elements[i].selectSingleNode("property-map") == null) {
101: error(Messages
102: .getErrorString(
103: "TestComponent.ERROR_0002_PARAMETER_MISSING", "format-string")); //$NON-NLS-1$
104: result = false;
105: }
106: List paramList = elements[i].selectNodes("arg");
107: if (paramList.size() < 1) {
108: error(Messages
109: .getErrorString(
110: "TestComponent.ERROR_0003_PARAMETER_MISSING", "arg", String.valueOf(1), String.valueOf(paramList.size()))); //$NON-NLS-1$
111: result = false;
112: }
113: }
114: }
115: return result;
116: }
117:
118: private boolean validateCopyAction(CopyParamAction copyParamAction) {
119: return true;
120: }
121:
122: private boolean validateFormatAction(FormatMsgAction formatMsgAction) {
123: boolean result = true;
124: if (formatMsgAction.getFormatString() == null) {
125: error(Messages
126: .getErrorString(
127: "TestComponent.ERROR_0002_PARAMETER_MISSING", "format-string")); //$NON-NLS-1$
128: result = false;
129: }
130: return result;
131: }
132:
133: private boolean validatePrintParamAction(
134: PrintParamAction printParamAction) {
135: return true;
136: }
137:
138: private boolean validateGetMapValuesAction(
139: PrintMapValsAction getMapValsAction) {
140: boolean result = true;
141: if (getMapValsAction.getPropertyMap() == IActionInputValueProvider.NULL_INPUT) {
142: error(Messages
143: .getErrorString(
144: "TestComponent.ERROR_0002_PARAMETER_MISSING", "format-string")); //$NON-NLS-1$
145: result = false;
146: }
147: if (getMapValsAction.getKeys().length < 1) {
148: error(Messages
149: .getErrorString(
150: "TestComponent.ERROR_0003_PARAMETER_MISSING", "arg", String.valueOf(1), "0")); //$NON-NLS-1$
151: result = false;
152: }
153: return result;
154: }
155:
156: /**
157: * @deprecated
158: */
159: private boolean executeAction(ActionDefinition actionDefinition) {
160: boolean result = true;
161: Element[] elements = actionDefinition
162: .getComponentDefElements("*");
163: for (int i = 0; i < elements.length; i++) {
164: String commandName = elements[i].getName();
165: if ("format".equalsIgnoreCase(commandName)) { //$NON-NLS-1$
166: result = executeFormatAction(elements[i]);
167: } else if ("print".equalsIgnoreCase(commandName)) { //$NON-NLS-1$
168: result = executePrintParamAction(elements[i]);
169: } else if ("copy".equalsIgnoreCase(commandName)) { //$NON-NLS-1$
170: result = executeCopyAction(elements[i]);
171: } else if ("getmapvalues".equalsIgnoreCase(commandName)) { //$NON-NLS-1$
172: result = executeGetMapValuesAction(elements[i]);
173: }
174: }
175:
176: if (result) {
177: Set outNames = getOutputNames();
178: for (Iterator it = outNames.iterator(); it.hasNext();) {
179: String name = (String) it.next();
180: Object value = tmpOutputs.get(name);
181: if (value != null) {
182: setOutputValue(name, value);
183: }
184: }
185: }
186: return result;
187: }
188:
189: /**
190: * @deprecated
191: */
192: private boolean executeCopyAction(Element componentDefinition) {
193: boolean result = true;
194:
195: String inputName = null;
196: Element element = componentDefinition.element("from");
197: if (element != null) {
198: inputName = element.getText();
199: }
200:
201: String outputName = null;
202: element = componentDefinition.element("return");
203: if (element != null) {
204: outputName = element.getText();
205: }
206:
207: if ((inputName != null) && (outputName != null)) {
208: try {
209: tmpOutputs.put(outputName, getValueOf(inputName));
210: } catch (Exception e) {
211: error(Messages
212: .getString("UtilityComponent.ERROR_0003_ERROR_COPYING_PARAMETER")); //$NON-NLS-1$
213: result = false;
214: }
215: }
216:
217: return result;
218: }
219:
220: /**
221: * @deprecated
222: */
223: private boolean executeFormatAction(Element componentDefinition) {
224: String formatString = componentDefinition.element(
225: "format-string").getText();
226:
227: String outputName = null;
228: Element element = componentDefinition.element("return");
229: if (element != null) {
230: outputName = element.getText();
231: }
232:
233: ArrayList formatArgs = new ArrayList();
234: List paramList = componentDefinition.selectNodes("arg");
235: for (Iterator it = paramList.iterator(); it.hasNext();) {
236: formatArgs.add(((Node) it.next()).getText());
237: }
238:
239: boolean result = true;
240: try {
241: MessageFormat mf = new MessageFormat(formatString);
242: String theResult = mf.format(formatArgs.toArray());
243: tmpOutputs.put(outputName, theResult);
244: } catch (Exception e) {
245: error(Messages
246: .getString("UtilityComponent.ERROR_0001_FORMAT_ERROR")); //$NON-NLS-1$
247: result = false;
248: }
249: return result;
250: }
251:
252: /**
253: * @deprecated
254: */
255: private boolean executePrintParamAction(Element componentDefinition) {
256: String delimiter = "";
257: Element element = componentDefinition.element("delimiter");
258: if (element != null) {
259: delimiter = element.getText();
260: }
261:
262: ArrayList paramNames = new ArrayList();
263: List paramList = componentDefinition.selectNodes("arg");
264: for (Iterator it = paramList.iterator(); it.hasNext();) {
265: paramNames.add(((Node) it.next()).getText());
266: }
267:
268: boolean result = true;
269: try {
270: StringBuffer sb = new StringBuffer(
271: "\n***************************************************************\n"); //$NON-NLS-1$
272: for (Iterator it = paramNames.iterator(); it.hasNext();) {
273: sb.append(getValueOf(it.next().toString())).append(
274: delimiter);
275: }
276: sb
277: .append("\n***************************************************************\n"); //$NON-NLS-1$
278: info(sb.toString());
279: } catch (Exception e) {
280: error(Messages
281: .getString("UtilityComponent.ERROR_0002_MESSAGE_LOG_ERROR")); //$NON-NLS-1$
282: result = false;
283: }
284: return result;
285: }
286:
287: /**
288: * @deprecated
289: */
290: private boolean executeGetMapValuesAction(
291: Element componentDefinition) {
292: String propertyMapName = null;
293: Element element = componentDefinition.element("property-map");
294: if (element != null) {
295: propertyMapName = element.getText();
296: }
297:
298: ArrayList keyNames = new ArrayList();
299: List paramList = componentDefinition.selectNodes("arg");
300: for (Iterator it = paramList.iterator(); it.hasNext();) {
301: keyNames.add(((Node) it.next()).getText());
302: }
303:
304: boolean result = true;
305: try {
306: Object mapObj = getValueOf(propertyMapName); //$NON-NLS-1$
307:
308: if (!(mapObj instanceof Map)) {
309: error(Messages
310: .getErrorString(
311: "UtilityComponent.ERROR_0004_PARAMETER_NOT_MAP", "property-map")); //$NON-NLS-1$ //$NON-NLS-2$
312: result = false;
313: } else {
314: Map srcMap = (Map) mapObj;
315: for (Iterator it = keyNames.iterator(); it.hasNext();) { //$NON-NLS-1$
316: String key = it.next().toString();
317: tmpOutputs.put(key, srcMap.get(key));
318: }
319: }
320: } catch (Exception e) {
321: error(Messages
322: .getString(Messages
323: .getString("UtilityComponent.ERROR_0005_GET_MAP_VALUES_ERROR"))); //$NON-NLS-1$
324: result = false;
325: }
326: return result;
327: }
328:
329: private boolean executeCopyAction(CopyParamAction copyParamAction) {
330: boolean result = true;
331:
332: IActionInputValueProvider actionInput = copyParamAction
333: .getCopyFrom();
334: ActionOutput actionOutput = copyParamAction.getOutputCopy();
335:
336: if ((actionInput instanceof ActionInput)
337: && (actionOutput != null)) {
338: try {
339: actionOutput.setValue(actionInput.getValue());
340: } catch (Exception ex) {
341: result = false;
342: }
343: }
344:
345: return result;
346: }
347:
348: private boolean executeFormatAction(FormatMsgAction formatMsgAction) {
349:
350: boolean result = true;
351: String formatString = formatMsgAction.getFormatString()
352: .getStringValue();
353: ActionOutput actionOutput = formatMsgAction.getOutputString();
354: IActionInputValueProvider[] msgInputs = formatMsgAction
355: .getMsgInputs();
356:
357: ArrayList formatArgs = new ArrayList();
358: for (int i = 0; i < msgInputs.length; i++) {
359: formatArgs.add(msgInputs[i].getStringValue());
360: }
361:
362: try {
363: MessageFormat mf = new MessageFormat(formatString);
364: String theResult = mf.format(formatArgs.toArray());
365: if (actionOutput != null) {
366: actionOutput.setValue(theResult);
367: }
368: } catch (Exception ex) {
369: result = false;
370: }
371: return result;
372: }
373:
374: private boolean executePrintParamAction(
375: PrintParamAction printParamAction) {
376: String delimiter = printParamAction.getDelimiter()
377: .getStringValue("");
378: IActionInputValueProvider[] inputsToPrint = printParamAction
379: .getInputsToPrint();
380: boolean result = true;
381: try {
382: StringBuffer sb = new StringBuffer(
383: "\n***************************************************************\n"); //$NON-NLS-1$
384: for (int i = 0; i < inputsToPrint.length; i++) {
385: sb.append(inputsToPrint[i].getStringValue("")).append(
386: delimiter);
387: }
388: sb
389: .append("\n***************************************************************\n"); //$NON-NLS-1$
390: info(sb.toString());
391: } catch (Exception e) {
392: error(Messages
393: .getString("UtilityComponent.ERROR_0002_MESSAGE_LOG_ERROR")); //$NON-NLS-1$
394: result = false;
395: }
396: return result;
397: }
398:
399: private boolean executeGetMapValuesAction(
400: PrintMapValsAction getMapValsAction) {
401: IActionInputValueProvider propertyMap = getMapValsAction
402: .getPropertyMap();
403: IActionInputValueProvider[] keys = getMapValsAction.getKeys();
404: boolean result = true;
405: try {
406: if (!(propertyMap.getValue() instanceof Map)) {
407: error(Messages
408: .getErrorString(
409: "UtilityComponent.ERROR_0004_PARAMETER_NOT_MAP", "property-map")); //$NON-NLS-1$ //$NON-NLS-2$
410: result = false;
411: } else {
412: Map srcMap = (Map) propertyMap.getValue();
413: for (int i = 0; i < keys.length; i++) {
414: String key = keys[i].getStringValue();
415: getMapValsAction.getOutputParam(key).setValue(
416: srcMap.get(key));
417: }
418: }
419: } catch (Exception e) {
420: error(Messages
421: .getString(Messages
422: .getString("UtilityComponent.ERROR_0005_GET_MAP_VALUES_ERROR"))); //$NON-NLS-1$
423: result = false;
424: }
425: return result;
426: }
427:
428: protected boolean validateAction() {
429: boolean result = true;
430: ActionDefinition actionDefinition = getActionDefinition();
431: if (actionDefinition instanceof CopyParamAction) {
432: result = validateCopyAction((CopyParamAction) actionDefinition);
433: } else if (actionDefinition instanceof FormatMsgAction) {
434: result = validateFormatAction((FormatMsgAction) actionDefinition);
435: } else if (actionDefinition instanceof PrintMapValsAction) {
436: result = validateGetMapValuesAction((PrintMapValsAction) actionDefinition);
437: } else if (actionDefinition instanceof PrintParamAction) {
438: result = validatePrintParamAction((PrintParamAction) actionDefinition);
439: } else {
440: // This component allows multiple actions to be defined in a single action definition.
441: // While this is no longer supported by the design studio, it needs to be supported here
442: // for backwards compatibility with older action sequence documents.
443: result = validateAction(actionDefinition);
444: }
445: return result;
446: }
447:
448: /*
449: * (non-Javadoc)
450: *
451: * @see org.pentaho.component.ComponentBase#done()
452: */
453: public void done() {
454:
455: }
456:
457: /*
458: * (non-Javadoc)
459: *
460: * @see org.pentaho.component.ComponentBase#execute()
461: */
462: protected boolean executeAction() {
463: ActionDefinition actionDefinition = getActionDefinition();
464: tmpOutputs = new HashMap(); // Make sure we start with an empty list in
465: boolean result = true;
466: if (actionDefinition instanceof CopyParamAction) {
467: executeCopyAction((CopyParamAction) actionDefinition);
468: } else if (actionDefinition instanceof FormatMsgAction) {
469: executeFormatAction((FormatMsgAction) actionDefinition);
470: } else if (actionDefinition instanceof PrintMapValsAction) {
471: executeGetMapValuesAction((PrintMapValsAction) actionDefinition);
472: } else if (actionDefinition instanceof PrintParamAction) {
473: executePrintParamAction((PrintParamAction) actionDefinition);
474: } else {
475: // This component allows multiple actions to be defined in a single action definition.
476: // While this is no longer supported by the design studio, it needs to be supported here
477: // for backwards compatibility with older action sequence documents.
478: result = executeAction(actionDefinition);
479: }
480: return result;
481: // this iteration
482: }
483:
484: /*
485: * (non-Javadoc)
486: *
487: * @see org.pentaho.component.ComponentBase#init()
488: */
489: public boolean init() {
490: if (debug)
491: debug(Messages
492: .getString("TestComponent.DEBUG_INITIALIZING_TEST")); //$NON-NLS-1$
493: return true;
494: }
495:
496: protected Object getActionParameterValue(String name) {
497: try {
498: return (getInputValue(name));
499: } catch (Exception e) {
500: } // Return null if it doesn't exist
501:
502: return (null);
503: }
504:
505: Object getValueOf(String paramName) {
506: if (paramName == null) {
507: return (null);
508: }
509:
510: if (paramName.startsWith("\"") && paramName.endsWith("\"")) { //$NON-NLS-1$ //$NON-NLS-2$
511: if (paramName.length() < 3) {
512: return (""); //$NON-NLS-1$
513: }
514: return (paramName.substring(1, paramName.length() - 1));
515: }
516:
517: Object obj = tmpOutputs.get(paramName);
518: if (obj != null) {
519: return (obj);
520: }
521:
522: return (getInputValue(paramName));
523: }
524: }
|