1 16 17 package org.pentaho.plugin.misc; 18 19 import java.text.MessageFormat ; 20 import java.util.ArrayList ; 21 import java.util.HashMap ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 import java.util.Map ; 25 import java.util.Set ; 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 42 public class TestComponent extends ComponentBase { 43 44 private static final long serialVersionUID = -5888733281367666385L; 45 46 51 52 public Log getLogger() { 53 return LogFactory.getLog(TestComponent.class); 54 } 55 56 private void message(String message) { 57 info(message); 58 System.out.println(message); 59 } 60 61 protected boolean validateSystemSettings() { 62 return true; 64 } 65 66 protected boolean validateAction() { 67 68 Set inputNames = getInputNames(); 70 Iterator inputNamesIterator = inputNames.iterator(); 71 String inputName; 72 IActionParameter actionParameter; 73 while (inputNamesIterator.hasNext()) { 74 inputName = (String ) inputNamesIterator.next(); 75 actionParameter = getInputParameter(inputName); 76 message(Messages.getString("TestComponent.DEBUG_INPUT_DESCRIPTION", inputName, actionParameter.getType())); } 78 79 Set outputNames = getOutputNames(); 80 Iterator outputNamesIterator = outputNames.iterator(); 81 String outputName; 82 while (outputNamesIterator.hasNext()) { 83 outputName = (String ) outputNamesIterator.next(); 84 actionParameter = getOutputItem(outputName); 85 message(Messages.getString("TestComponent.DEBUG_OUTPUT_DESCRIPTION", outputName, actionParameter.getType())); } 87 88 Set resourceNames = getResourceNames(); 89 Iterator resourceNamesIterator = resourceNames.iterator(); 90 String resourceName; 91 IActionResource actionResource; 92 while (resourceNamesIterator.hasNext()) { 93 resourceName = (String ) resourceNamesIterator.next(); 94 actionResource = getResource(resourceName); 95 message(Messages.getString("TestComponent.DEBUG_RESOURCE_DESCRIPTION", resourceName, actionResource.getMimeType(), PentahoSystem.getApplicationContext().getSolutionPath(actionResource.getAddress()))); try { 97 String content = getResourceAsString(actionResource); 98 message(Messages.getString("TestComponent.DEBUG_RESOURCE_CONTENTS", ((content == null) ? "null" : content.substring(0, 100)))); } catch (Exception e) { 100 message(Messages.getString("TestComponent.ERROR_0005_RESOURCE_NOT_LOADED", e.getMessage())); } 102 } 103 104 return true; 105 } 106 107 112 public void done() { 113 115 } 116 117 122 protected boolean executeAction() { 123 message(Messages.getString("TestComponent.DEBUG_EXECUTING_TEST")); Node componentNode = getComponentDefinition(); 125 126 Set inputNames = getInputNames(); 127 Iterator inputNamesIterator = inputNames.iterator(); 128 String inputName; 129 IActionParameter actionParameter; 130 while (inputNamesIterator.hasNext()) { 131 inputName = (String ) inputNamesIterator.next(); 132 actionParameter = getInputParameter(inputName); 133 134 message(Messages.getString("TestComponent.DEBUG_INPUT_DESCRIPTION", inputName, actionParameter.getValue().getClass().toString() + "=" + actionParameter.getValue().toString())); } 136 137 String test = XmlHelper.getNodeText("test", componentNode); if ((test == null) || (test.length() < 1)) { 139 message(componentNode.asXML()); 140 return (true); 141 } 142 143 String newName = XmlHelper.getNodeText("newname", componentNode); Object theResult = null; 145 146 if ("format".equals(test)) { MessageFormat mf = new MessageFormat (XmlHelper.getNodeText("p1", componentNode, "")); Object obj[] = { getParamFromComponentNode("p2", componentNode), getParamFromComponentNode("p3", componentNode) }; theResult = mf.format(obj); 150 } else { 151 Object p1 = getParamFromComponentNode("p1", componentNode); if (p1 == null) { 153 return (false); 154 } else if ("toupper".equals(test)) { 156 theResult = p1.toString().toUpperCase(); 157 } else if ("rename".equals(test)) { theResult = p1; 159 } 160 161 else if ("map2params".equals(test)) { 163 if (!(p1 instanceof Map )) { 164 error(Messages.getErrorString("TestComponent.ERROR_0003_PARAMETER_NOT_MAP", "p1")); return (false); 166 } 167 168 Map srcMap = (Map ) p1; 169 for (Iterator it = srcMap.keySet().iterator(); it.hasNext();) { 170 String key = it.next().toString(); 171 setOutputValue(key, srcMap.get(key)); 172 } 173 174 } else if ("print".equals(test)) { 176 String delim = "\r\n***************************************************************\r\n"; theResult = delim + p1.toString() + delim; 178 } else if ("getkeys".equals(test)) { 180 if (!(p1 instanceof Map )) { 181 error(Messages.getErrorString("TestComponent.ERROR_0003_PARAMETER_NOT_MAP", "p1")); return (false); 183 } 184 theResult = new ArrayList (((Map ) p1).keySet()); 185 } else { 186 187 Object p2 = getParamFromComponentNode("p2", componentNode); if (p2 == null) { 189 return (false); 190 } 191 192 if ("concat".equals(test)) { theResult = p1.toString() + p2.toString(); 194 } else if ("print2".equals(test)) { 196 String delim = Messages.getString("TestComponent.CODE_PRINT_DELIM"); theResult = delim + p1.toString() + " - " + p2.toString() + delim; } else { 199 200 Object p3 = getParamFromComponentNode("p3", componentNode); if (p3 == null) { 202 return (false); 203 } 204 205 if ("merge".equals(test)) { 207 217 if (!(p1 instanceof Map ) || !(p2 instanceof List ) || !(p3 instanceof String )) { 218 error(Messages.getErrorString("TestComponent.ERROR_0004_P1_P2_WRONG_TYPE")); return (false); 220 } 221 222 theResult = merge((Map ) p1, (List ) p2, (String ) p3); 223 } else { 224 message(Messages.getErrorString("TestComponent.ERROR_0001_TEST_NODE_NOT_FOUND")); return false; 226 } 227 } 228 } 229 } 230 231 if (newName != null) { 232 message(newName + " = " + theResult); try { 234 setOutputValue(newName, theResult); 235 } catch (Exception e) { 236 } } else { 238 message("The result = " + theResult); } 240 241 return (true); 242 } 243 244 249 public boolean init() { 250 message(Messages.getString("TestComponent.DEBUG_INITIALIZING_TEST")); return true; 252 } 253 254 protected Object getActionParameterValue(String name) { 255 try { 256 return (getInputValue(name)); 257 } catch (Exception e) { 258 } 260 return (null); 261 } 262 263 private List merge(Map hm, List list, String keyName) { 264 ArrayList al = new ArrayList (); 265 for (Iterator it = list.iterator(); it.hasNext();) { 266 Object item = it.next(); 267 if (item instanceof Map ) { 268 Map resMap = merge(hm, (Map ) item, keyName); 269 if (resMap != null) { 270 al.add(resMap); 271 } 272 } 273 } 274 return (al); 275 } 276 277 private Map merge(Map srcMap, Map destMap, String keyName) { 278 Object keyValue = destMap.get(keyName); 279 Map rtnMap = null; 280 for (Iterator it = srcMap.keySet().iterator(); it.hasNext();) { 281 String key = it.next().toString(); 282 if ((keyValue != null) && key.equalsIgnoreCase(keyValue.toString())) { 283 rtnMap = new HashMap (destMap); 284 rtnMap.put("NewKey", srcMap.get(key)); return (rtnMap); 286 } 287 } 288 289 return (rtnMap); 290 } 291 292 private Object getParamFromComponentNode(String paramName, Node componentNode) { 293 String param = XmlHelper.getNodeText(paramName, componentNode); 294 if ((param == null) || (param.length() < 1)) { 295 error(Messages.getErrorString("TestComponent.ERROR_0002_PARAMETER_MISSING", paramName)); return (null); 297 } 298 return (getActionParameterValue(param)); 299 } 300 } | Popular Tags |