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 James Dixon
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: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028: import org.dom4j.Node;
029: import org.pentaho.core.runtime.IActionParameter;
030: import org.pentaho.core.solution.IActionResource;
031: import org.pentaho.core.system.PentahoSystem;
032: import org.pentaho.core.util.XmlHelper;
033: import org.pentaho.messages.Messages;
034: import org.pentaho.plugin.ComponentBase;
035:
036: /**
037: * @author James Dixon
038: *
039: * TODO To change the template for this generated type comment go to Window -
040: * Preferences - Java - Code Style - Code Templates
041: */
042: public class TestComponent extends ComponentBase {
043:
044: private static final long serialVersionUID = -5888733281367666385L;
045:
046: /*
047: * (non-Javadoc)
048: *
049: * @see org.pentaho.component.ComponentBase#validate()
050: */
051:
052: public Log getLogger() {
053: return LogFactory.getLog(TestComponent.class);
054: }
055:
056: private void message(String message) {
057: info(message);
058: System.out.println(message);
059: }
060:
061: protected boolean validateSystemSettings() {
062: // This component does not have any system settings to validate
063: return true;
064: }
065:
066: protected boolean validateAction() {
067:
068: // describe the inputs, outputs and resources available to us
069: Set inputNames = getInputNames();
070: Iterator inputNamesIterator = inputNames.iterator();
071: String inputName;
072: IActionParameter actionParameter;
073: while (inputNamesIterator.hasNext()) {
074: inputName = (String) inputNamesIterator.next();
075: actionParameter = getInputParameter(inputName);
076: message(Messages
077: .getString(
078: "TestComponent.DEBUG_INPUT_DESCRIPTION", inputName, actionParameter.getType())); //$NON-NLS-1$
079: }
080:
081: Set outputNames = getOutputNames();
082: Iterator outputNamesIterator = outputNames.iterator();
083: String outputName;
084: while (outputNamesIterator.hasNext()) {
085: outputName = (String) outputNamesIterator.next();
086: actionParameter = getOutputItem(outputName);
087: message(Messages
088: .getString(
089: "TestComponent.DEBUG_OUTPUT_DESCRIPTION", outputName, actionParameter.getType())); //$NON-NLS-1$
090: }
091:
092: Set resourceNames = getResourceNames();
093: Iterator resourceNamesIterator = resourceNames.iterator();
094: String resourceName;
095: IActionResource actionResource;
096: while (resourceNamesIterator.hasNext()) {
097: resourceName = (String) resourceNamesIterator.next();
098: actionResource = getResource(resourceName);
099: message(Messages
100: .getString(
101: "TestComponent.DEBUG_RESOURCE_DESCRIPTION", resourceName, actionResource.getMimeType(), PentahoSystem.getApplicationContext().getSolutionPath(actionResource.getAddress()))); //$NON-NLS-1$
102: try {
103: String content = getResourceAsString(actionResource);
104: message(Messages
105: .getString(
106: "TestComponent.DEBUG_RESOURCE_CONTENTS", ((content == null) ? "null" : content.substring(0, 100)))); //$NON-NLS-1$ //$NON-NLS-2$
107: } catch (Exception e) {
108: message(Messages
109: .getString(
110: "TestComponent.ERROR_0005_RESOURCE_NOT_LOADED", e.getMessage())); //$NON-NLS-1$
111: }
112: }
113:
114: return true;
115: }
116:
117: /*
118: * (non-Javadoc)
119: *
120: * @see org.pentaho.component.ComponentBase#done()
121: */
122: public void done() {
123: // TODO Auto-generated method stub
124:
125: }
126:
127: /*
128: * (non-Javadoc)
129: *
130: * @see org.pentaho.component.ComponentBase#execute()
131: */
132: protected boolean executeAction() {
133: message(Messages
134: .getString("TestComponent.DEBUG_EXECUTING_TEST")); //$NON-NLS-1$
135: Node componentNode = getComponentDefinition();
136:
137: Set inputNames = getInputNames();
138: Iterator inputNamesIterator = inputNames.iterator();
139: String inputName;
140: IActionParameter actionParameter;
141: while (inputNamesIterator.hasNext()) {
142: inputName = (String) inputNamesIterator.next();
143: actionParameter = getInputParameter(inputName);
144:
145: message(Messages
146: .getString(
147: "TestComponent.DEBUG_INPUT_DESCRIPTION", inputName, actionParameter.getValue().getClass().toString() + "=" + actionParameter.getValue().toString())); //$NON-NLS-1$ //$NON-NLS-2$
148: }
149:
150: String test = XmlHelper.getNodeText("test", componentNode); //$NON-NLS-1$
151: if ((test == null) || (test.length() < 1)) {
152: message(componentNode.asXML());
153: return (true);
154: }
155:
156: String newName = XmlHelper
157: .getNodeText("newname", componentNode); //$NON-NLS-1$
158: Object theResult = null;
159:
160: if ("format".equals(test)) { //$NON-NLS-1$
161: MessageFormat mf = new MessageFormat(XmlHelper.getNodeText(
162: "p1", componentNode, "")); //$NON-NLS-1$ //$NON-NLS-2$
163: Object obj[] = {
164: getParamFromComponentNode("p2", componentNode), getParamFromComponentNode("p3", componentNode) }; //$NON-NLS-1$ //$NON-NLS-2$
165: theResult = mf.format(obj);
166: } else {
167: Object p1 = getParamFromComponentNode("p1", componentNode); //$NON-NLS-1$
168: if (p1 == null) {
169: return (false);
170: } else if ("toupper".equals(test)) { //$NON-NLS-1$
171:
172: theResult = p1.toString().toUpperCase();
173: } else if ("rename".equals(test)) { //$NON-NLS-1$
174: theResult = p1;
175: }
176:
177: else if ("map2params".equals(test)) { //$NON-NLS-1$
178:
179: if (!(p1 instanceof Map)) {
180: error(Messages
181: .getErrorString(
182: "TestComponent.ERROR_0003_PARAMETER_NOT_MAP", "p1")); //$NON-NLS-1$ //$NON-NLS-2$
183: return (false);
184: }
185:
186: Map srcMap = (Map) p1;
187: for (Iterator it = srcMap.keySet().iterator(); it
188: .hasNext();) {
189: String key = it.next().toString();
190: setOutputValue(key, srcMap.get(key));
191: }
192:
193: } else if ("print".equals(test)) { //$NON-NLS-1$
194:
195: String delim = "\r\n***************************************************************\r\n"; //$NON-NLS-1$
196: theResult = delim + p1.toString() + delim;
197: } else if ("getkeys".equals(test)) { //$NON-NLS-1$
198:
199: if (!(p1 instanceof Map)) {
200: error(Messages
201: .getErrorString(
202: "TestComponent.ERROR_0003_PARAMETER_NOT_MAP", "p1")); //$NON-NLS-1$ //$NON-NLS-2$
203: return (false);
204: }
205: theResult = new ArrayList(((Map) p1).keySet());
206: } else {
207:
208: Object p2 = getParamFromComponentNode(
209: "p2", componentNode); //$NON-NLS-1$
210: if (p2 == null) {
211: return (false);
212: }
213:
214: if ("concat".equals(test)) { //$NON-NLS-1$
215: theResult = p1.toString() + p2.toString();
216: } else if ("print2".equals(test)) { //$NON-NLS-1$
217:
218: String delim = Messages
219: .getString("TestComponent.CODE_PRINT_DELIM"); //$NON-NLS-1$
220: theResult = delim + p1.toString()
221: + " - " + p2.toString() + delim; //$NON-NLS-1$
222: } else {
223:
224: Object p3 = getParamFromComponentNode(
225: "p3", componentNode); //$NON-NLS-1$
226: if (p3 == null) {
227: return (false);
228: }
229:
230: if ("merge".equals(test)) { //$NON-NLS-1$
231:
232: // merge cycles through each property map in list p2.
233: // For each map, it cycles through the keys in map p1
234: // and compares the key name
235: // from p1 with a value from p2. p3 specifies the key in
236: // p2 to compare with. When a match is found, an entry
237: // is added to an output
238: // output list that is identical to the map from p2. The
239: // value specified by the key in p1 will be added to the
240: // output under the key "NewKey"
241:
242: if (!(p1 instanceof Map)
243: || !(p2 instanceof List)
244: || !(p3 instanceof String)) {
245: error(Messages
246: .getErrorString("TestComponent.ERROR_0004_P1_P2_WRONG_TYPE")); //$NON-NLS-1$
247: return (false);
248: }
249:
250: theResult = merge((Map) p1, (List) p2,
251: (String) p3);
252: } else {
253: message(Messages
254: .getErrorString("TestComponent.ERROR_0001_TEST_NODE_NOT_FOUND")); //$NON-NLS-1$
255: return false;
256: }
257: }
258: }
259: }
260:
261: if (newName != null) {
262: message(newName + " = " + theResult); //$NON-NLS-1$
263: try {
264: setOutputValue(newName, theResult);
265: } catch (Exception e) {
266: } // setOutputValue logs an error mesage
267: } else {
268: message("The result = " + theResult); //$NON-NLS-1$
269: }
270:
271: return (true);
272: }
273:
274: /*
275: * (non-Javadoc)
276: *
277: * @see org.pentaho.component.ComponentBase#init()
278: */
279: public boolean init() {
280: message(Messages
281: .getString("TestComponent.DEBUG_INITIALIZING_TEST")); //$NON-NLS-1$
282: return true;
283: }
284:
285: protected Object getActionParameterValue(String name) {
286: try {
287: return (getInputValue(name));
288: } catch (Exception e) {
289: } // Return null if it doesn't exist
290:
291: return (null);
292: }
293:
294: private List merge(Map hm, List list, String keyName) {
295: ArrayList al = new ArrayList();
296: for (Iterator it = list.iterator(); it.hasNext();) {
297: Object item = it.next();
298: if (item instanceof Map) {
299: Map resMap = merge(hm, (Map) item, keyName);
300: if (resMap != null) {
301: al.add(resMap);
302: }
303: }
304: }
305: return (al);
306: }
307:
308: private Map merge(Map srcMap, Map destMap, String keyName) {
309: Object keyValue = destMap.get(keyName);
310: Map rtnMap = null;
311: for (Iterator it = srcMap.keySet().iterator(); it.hasNext();) {
312: String key = it.next().toString();
313: if ((keyValue != null)
314: && key.equalsIgnoreCase(keyValue.toString())) {
315: rtnMap = new HashMap(destMap);
316: rtnMap.put("NewKey", srcMap.get(key)); //$NON-NLS-1$
317: return (rtnMap);
318: }
319: }
320:
321: return (rtnMap);
322: }
323:
324: private Object getParamFromComponentNode(String paramName,
325: Node componentNode) {
326: String param = XmlHelper.getNodeText(paramName, componentNode);
327: if ((param == null) || (param.length() < 1)) {
328: error(Messages
329: .getErrorString(
330: "TestComponent.ERROR_0002_PARAMETER_MISSING", paramName)); //$NON-NLS-1$
331: return (null);
332: }
333: return (getActionParameterValue(param));
334: }
335: }
|