KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > plugin > misc > TestComponent


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 Jun 24, 2005
14  * @author James Dixon
15  */

16
17 package org.pentaho.plugin.misc;
18
19 import java.text.MessageFormat JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Map JavaDoc;
25 import java.util.Set JavaDoc;
26 import org.apache.commons.logging.Log;
27 import org.apache.commons.logging.LogFactory;
28 import org.dom4j.Node;
29 import org.pentaho.core.runtime.IActionParameter;
30 import org.pentaho.core.solution.IActionResource;
31 import org.pentaho.core.system.PentahoSystem;
32 import org.pentaho.core.util.XmlHelper;
33 import org.pentaho.messages.Messages;
34 import org.pentaho.plugin.ComponentBase;
35
36 /**
37  * @author James Dixon
38  *
39  * TODO To change the template for this generated type comment go to Window -
40  * Preferences - Java - Code Style - Code Templates
41  */

42 public class TestComponent extends ComponentBase {
43
44     private static final long serialVersionUID = -5888733281367666385L;
45
46     /*
47      * (non-Javadoc)
48      *
49      * @see org.pentaho.component.ComponentBase#validate()
50      */

51
52     public Log getLogger() {
53         return LogFactory.getLog(TestComponent.class);
54     }
55
56     private void message(String JavaDoc message) {
57         info(message);
58         System.out.println(message);
59     }
60
61     protected boolean validateSystemSettings() {
62         // This component does not have any system settings to validate
63
return true;
64     }
65
66     protected boolean validateAction() {
67
68         // describe the inputs, outputs and resources available to us
69
Set JavaDoc inputNames = getInputNames();
70         Iterator JavaDoc inputNamesIterator = inputNames.iterator();
71         String JavaDoc inputName;
72         IActionParameter actionParameter;
73         while (inputNamesIterator.hasNext()) {
74             inputName = (String JavaDoc) inputNamesIterator.next();
75             actionParameter = getInputParameter(inputName);
76             message(Messages.getString("TestComponent.DEBUG_INPUT_DESCRIPTION", inputName, actionParameter.getType())); //$NON-NLS-1$
77
}
78
79         Set JavaDoc outputNames = getOutputNames();
80         Iterator JavaDoc outputNamesIterator = outputNames.iterator();
81         String JavaDoc outputName;
82         while (outputNamesIterator.hasNext()) {
83             outputName = (String JavaDoc) outputNamesIterator.next();
84             actionParameter = getOutputItem(outputName);
85             message(Messages.getString("TestComponent.DEBUG_OUTPUT_DESCRIPTION", outputName, actionParameter.getType())); //$NON-NLS-1$
86
}
87
88         Set JavaDoc resourceNames = getResourceNames();
89         Iterator JavaDoc resourceNamesIterator = resourceNames.iterator();
90         String JavaDoc resourceName;
91         IActionResource actionResource;
92         while (resourceNamesIterator.hasNext()) {
93             resourceName = (String JavaDoc) resourceNamesIterator.next();
94             actionResource = getResource(resourceName);
95             message(Messages.getString("TestComponent.DEBUG_RESOURCE_DESCRIPTION", resourceName, actionResource.getMimeType(), PentahoSystem.getApplicationContext().getSolutionPath(actionResource.getAddress()))); //$NON-NLS-1$
96
try {
97                 String JavaDoc content = getResourceAsString(actionResource);
98                 message(Messages.getString("TestComponent.DEBUG_RESOURCE_CONTENTS", ((content == null) ? "null" : content.substring(0, 100)))); //$NON-NLS-1$ //$NON-NLS-2$
99
} catch (Exception JavaDoc e) {
100                 message(Messages.getString("TestComponent.ERROR_0005_RESOURCE_NOT_LOADED", e.getMessage())); //$NON-NLS-1$
101
}
102         }
103
104         return true;
105     }
106
107     /*
108      * (non-Javadoc)
109      *
110      * @see org.pentaho.component.ComponentBase#done()
111      */

112     public void done() {
113         // TODO Auto-generated method stub
114

115     }
116
117     /*
118      * (non-Javadoc)
119      *
120      * @see org.pentaho.component.ComponentBase#execute()
121      */

122     protected boolean executeAction() {
123         message(Messages.getString("TestComponent.DEBUG_EXECUTING_TEST")); //$NON-NLS-1$
124
Node componentNode = getComponentDefinition();
125
126         Set JavaDoc inputNames = getInputNames();
127         Iterator JavaDoc inputNamesIterator = inputNames.iterator();
128         String JavaDoc inputName;
129         IActionParameter actionParameter;
130         while (inputNamesIterator.hasNext()) {
131             inputName = (String JavaDoc) inputNamesIterator.next();
132             actionParameter = getInputParameter(inputName);
133
134             message(Messages.getString("TestComponent.DEBUG_INPUT_DESCRIPTION", inputName, actionParameter.getValue().getClass().toString() + "=" + actionParameter.getValue().toString())); //$NON-NLS-1$ //$NON-NLS-2$
135
}
136
137         String JavaDoc test = XmlHelper.getNodeText("test", componentNode); //$NON-NLS-1$
138
if ((test == null) || (test.length() < 1)) {
139             message(componentNode.asXML());
140             return (true);
141         }
142
143         String JavaDoc newName = XmlHelper.getNodeText("newname", componentNode); //$NON-NLS-1$
144
Object JavaDoc theResult = null;
145
146         if ("format".equals(test)) { //$NON-NLS-1$
147
MessageFormat JavaDoc mf = new MessageFormat JavaDoc(XmlHelper.getNodeText("p1", componentNode, "")); //$NON-NLS-1$ //$NON-NLS-2$
148
Object JavaDoc obj[] = { getParamFromComponentNode("p2", componentNode), getParamFromComponentNode("p3", componentNode) }; //$NON-NLS-1$ //$NON-NLS-2$
149
theResult = mf.format(obj);
150         } else {
151             Object JavaDoc p1 = getParamFromComponentNode("p1", componentNode); //$NON-NLS-1$
152
if (p1 == null) {
153                 return (false);
154             } else if ("toupper".equals(test)) { //$NON-NLS-1$
155

156                 theResult = p1.toString().toUpperCase();
157             } else if ("rename".equals(test)) { //$NON-NLS-1$
158
theResult = p1;
159             }
160
161             else if ("map2params".equals(test)) { //$NON-NLS-1$
162

163                 if (!(p1 instanceof Map JavaDoc)) {
164                     error(Messages.getErrorString("TestComponent.ERROR_0003_PARAMETER_NOT_MAP", "p1")); //$NON-NLS-1$ //$NON-NLS-2$
165
return (false);
166                 }
167
168                 Map JavaDoc srcMap = (Map JavaDoc) p1;
169                 for (Iterator JavaDoc it = srcMap.keySet().iterator(); it.hasNext();) {
170                     String JavaDoc key = it.next().toString();
171                     setOutputValue(key, srcMap.get(key));
172                 }
173
174             } else if ("print".equals(test)) { //$NON-NLS-1$
175

176                 String JavaDoc delim = "\r\n***************************************************************\r\n"; //$NON-NLS-1$
177
theResult = delim + p1.toString() + delim;
178             } else if ("getkeys".equals(test)) { //$NON-NLS-1$
179

180                 if (!(p1 instanceof Map JavaDoc)) {
181                     error(Messages.getErrorString("TestComponent.ERROR_0003_PARAMETER_NOT_MAP", "p1")); //$NON-NLS-1$ //$NON-NLS-2$
182
return (false);
183                 }
184                 theResult = new ArrayList JavaDoc(((Map JavaDoc) p1).keySet());
185             } else {
186
187                 Object JavaDoc p2 = getParamFromComponentNode("p2", componentNode); //$NON-NLS-1$
188
if (p2 == null) {
189                     return (false);
190                 }
191
192                 if ("concat".equals(test)) { //$NON-NLS-1$
193
theResult = p1.toString() + p2.toString();
194                 } else if ("print2".equals(test)) { //$NON-NLS-1$
195

196                     String JavaDoc delim = Messages.getString("TestComponent.CODE_PRINT_DELIM"); //$NON-NLS-1$
197
theResult = delim + p1.toString() + " - " + p2.toString() + delim; //$NON-NLS-1$
198
} else {
199
200                     Object JavaDoc p3 = getParamFromComponentNode("p3", componentNode); //$NON-NLS-1$
201
if (p3 == null) {
202                         return (false);
203                     }
204
205                     if ("merge".equals(test)) { //$NON-NLS-1$
206

207                         // merge cycles through each property map in list p2.
208
// For each map, it cycles through the keys in map p1
209
// and compares the key name
210
// from p1 with a value from p2. p3 specifies the key in
211
// p2 to compare with. When a match is found, an entry
212
// is added to an output
213
// output list that is identical to the map from p2. The
214
// value specified by the key in p1 will be added to the
215
// output under the key "NewKey"
216

217                         if (!(p1 instanceof Map JavaDoc) || !(p2 instanceof List JavaDoc) || !(p3 instanceof String JavaDoc)) {
218                             error(Messages.getErrorString("TestComponent.ERROR_0004_P1_P2_WRONG_TYPE")); //$NON-NLS-1$
219
return (false);
220                         }
221
222                         theResult = merge((Map JavaDoc) p1, (List JavaDoc) p2, (String JavaDoc) p3);
223                     } else {
224                         message(Messages.getErrorString("TestComponent.ERROR_0001_TEST_NODE_NOT_FOUND")); //$NON-NLS-1$
225
return false;
226                     }
227                 }
228             }
229         }
230
231         if (newName != null) {
232             message(newName + " = " + theResult); //$NON-NLS-1$
233
try {
234                 setOutputValue(newName, theResult);
235             } catch (Exception JavaDoc e) {
236             } // setOutputValue logs an error mesage
237
} else {
238             message("The result = " + theResult); //$NON-NLS-1$
239
}
240
241         return (true);
242     }
243
244     /*
245      * (non-Javadoc)
246      *
247      * @see org.pentaho.component.ComponentBase#init()
248      */

249     public boolean init() {
250         message(Messages.getString("TestComponent.DEBUG_INITIALIZING_TEST")); //$NON-NLS-1$
251
return true;
252     }
253
254     protected Object JavaDoc getActionParameterValue(String JavaDoc name) {
255         try {
256             return (getInputValue(name));
257         } catch (Exception JavaDoc e) {
258         } // Return null if it doesn't exist
259

260         return (null);
261     }
262
263     private List JavaDoc merge(Map JavaDoc hm, List JavaDoc list, String JavaDoc keyName) {
264         ArrayList JavaDoc al = new ArrayList JavaDoc();
265         for (Iterator JavaDoc it = list.iterator(); it.hasNext();) {
266             Object JavaDoc item = it.next();
267             if (item instanceof Map JavaDoc) {
268                 Map JavaDoc resMap = merge(hm, (Map JavaDoc) item, keyName);
269                 if (resMap != null) {
270                     al.add(resMap);
271                 }
272             }
273         }
274         return (al);
275     }
276
277     private Map JavaDoc merge(Map JavaDoc srcMap, Map JavaDoc destMap, String JavaDoc keyName) {
278         Object JavaDoc keyValue = destMap.get(keyName);
279         Map JavaDoc rtnMap = null;
280         for (Iterator JavaDoc it = srcMap.keySet().iterator(); it.hasNext();) {
281             String JavaDoc key = it.next().toString();
282             if ((keyValue != null) && key.equalsIgnoreCase(keyValue.toString())) {
283                 rtnMap = new HashMap JavaDoc(destMap);
284                 rtnMap.put("NewKey", srcMap.get(key)); //$NON-NLS-1$
285
return (rtnMap);
286             }
287         }
288
289         return (rtnMap);
290     }
291
292     private Object JavaDoc getParamFromComponentNode(String JavaDoc paramName, Node componentNode) {
293         String JavaDoc param = XmlHelper.getNodeText(paramName, componentNode);
294         if ((param == null) || (param.length() < 1)) {
295             error(Messages.getErrorString("TestComponent.ERROR_0002_PARAMETER_MISSING", paramName)); //$NON-NLS-1$
296
return (null);
297         }
298         return (getActionParameterValue(param));
299     }
300 }
Popular Tags