1 13 package org.pentaho.core.runtime; 14 15 import java.util.ArrayList ; 16 import java.util.Iterator ; 17 import java.util.List ; 18 import java.util.Map ; 19 import java.util.Set ; 20 21 import org.apache.commons.collections.map.ListOrderedMap; 22 import org.apache.commons.logging.Log; 23 import org.apache.commons.logging.LogFactory; 24 import org.pentaho.core.solution.ActionParameterSource; 25 import org.pentaho.core.solution.IActionDefinition; 26 import org.pentaho.core.solution.IActionResource; 27 import org.pentaho.core.solution.IActionSequence; 28 import org.pentaho.messages.Messages; 29 30 public class ParameterManager { 31 32 private static String [] EMPTY_ARRAY = new String [0]; 33 34 private Map allParams; 35 private Map allResources; 36 37 private List waitingToDieParams; 38 39 private Map currentInputs; 40 private Map currentOutputs; 41 private Map currentResources; 42 43 private String [] sequenceInputNames; 44 private String [] sequenceResourceNames; 45 private static final Log logger = LogFactory.getLog(ParameterManager.class); 46 47 private Map sequenceOutputDefs; 48 49 ParameterManager() { 50 allParams = new ListOrderedMap(); 51 allResources = new ListOrderedMap(); 52 53 waitingToDieParams = new ArrayList (); 54 55 currentInputs = new ListOrderedMap(); 56 currentResources = new ListOrderedMap(); 57 currentOutputs = new ListOrderedMap(); 58 59 sequenceOutputDefs = new ListOrderedMap(); 60 sequenceInputNames = EMPTY_ARRAY; 61 } 62 63 ParameterManager( IActionSequence actionSequence ) { 64 this(); 65 allParams.putAll( actionSequence.getInputDefinitions() ); 66 sequenceInputNames = (String [])actionSequence.getInputDefinitions().keySet().toArray( EMPTY_ARRAY ); 67 68 allResources.putAll( actionSequence.getResourceDefinitions() ); 69 sequenceResourceNames = (String [])actionSequence.getResourceDefinitions().keySet().toArray( EMPTY_ARRAY ); 70 71 sequenceOutputDefs.putAll( actionSequence.getOutputDefinitions() ); 72 } 73 74 public Map getAllParameters() { 75 return( allParams ); 76 } 77 78 public IActionParameter getCurrentInput( String inputName ) { 79 return( (IActionParameter) currentInputs.get(inputName) ); 80 } 81 82 public IActionParameter getCurrentOutput( String outputName ) { 83 return( (IActionParameter) currentOutputs.get(outputName) ); 84 } 85 86 public IActionResource getCurrentResource( String resourceName ) { 87 return( (IActionResource) currentResources.get(resourceName) ); 88 } 89 90 public Set getCurrentInputNames() { 91 return currentInputs.keySet(); 92 } 93 94 public IActionParameter getLoopParameter( String inputName ) { 95 return( (IActionParameter) allParams.get(inputName) ); 96 } 97 98 public Set getCurrentOutputNames() { 99 return currentOutputs.keySet(); 100 } 101 102 public Set getCurrentResourceNames() { 103 return (currentResources.keySet()); 104 } 105 106 protected boolean disposeParameter(ActionParameter param) { 107 try { 108 if ( param != null ) { 109 param.dispose(); 110 return true; 111 } 112 } catch (Throwable th) { 113 logger.error(Messages.getErrorString("ParameterManager.ERROR_0001_DISPOSE_ERROR", param.getName()), th); } 116 return false; 117 } 118 119 public void dispose() { 120 if (allParams != null) { 121 for (Iterator it = allParams.values().iterator(); it.hasNext();) { 122 disposeParameter((ActionParameter) it.next()); 123 } 124 for (Iterator it = waitingToDieParams.iterator(); it.hasNext();) { 125 disposeParameter((ActionParameter) it.next()); 126 } 127 } 128 } 129 130 public void resetParameters() { 131 dispose(); 132 currentInputs.clear(); 133 currentOutputs.clear(); 134 currentResources.clear(); 135 136 allParams = resetMap( sequenceInputNames, allParams ); 137 allResources = resetMap( sequenceResourceNames, allResources ); 138 } 139 140 private Map resetMap( String [] names, Map oldMap ) { 141 Map newMap = new ListOrderedMap(); 142 for ( int i = 0; i < names.length; ++i ) { 143 newMap.put( names[i], oldMap.get( names[i] ) ); 144 } 145 return( newMap ); 146 } 147 148 public void setCurrentParameters(IActionDefinition actionDefinition) { 149 currentInputs.clear(); 150 currentOutputs.clear(); 151 currentResources.clear(); 152 153 if ( actionDefinition == null ) { 154 currentInputs = resetMap( sequenceInputNames, allParams ); 155 currentResources = resetMap( sequenceResourceNames, allParams ); 156 157 for ( Iterator it = sequenceOutputDefs.entrySet().iterator(); it.hasNext(); ) { 158 Map.Entry entry = (Map.Entry )it.next(); 159 String outputName = (String )entry.getKey(); 160 IActionParameter param = (IActionParameter) allParams.get(outputName); 161 if (param == null) { 162 currentOutputs.put( outputName, entry.getValue() ); 163 } 164 else { 165 currentOutputs.put( outputName, param ); 166 } 167 } 168 return; 169 } 170 171 String key; 172 Object value; 173 for (Iterator it = actionDefinition.getActionInputDefinitions().keySet().iterator(); it.hasNext();) { 174 key = (String ) it.next(); 175 value = allParams.get(actionDefinition.getMappedInputName(key)); 176 if (value == null) { 177 value = actionDefinition.getActionInputDefinitions().get(key); 178 if (!((ActionParameter) value).hasDefaultValue()) { 179 value = null; } 181 } 182 183 if (value != null) { 184 currentInputs.put(key, value); 185 } 186 } 187 188 currentOutputs.putAll(actionDefinition.getActionOutputDefinitions()); 189 190 if ( !actionDefinition.hasActionResources() ) { 192 currentResources.putAll( allResources ); 193 } 194 else { 195 for (Iterator it = actionDefinition.getActionResourceDefinitionNames().iterator(); it.hasNext();) { 196 key = (String )it.next(); 197 String key2 = actionDefinition.getMappedResourceName(key); 198 value = allResources.get( key2 ); 199 currentResources.put( key, value ); 200 } 201 } 202 } 203 204 public void addToAllInputs(String key, IActionParameter param) { 205 IActionParameter old = (IActionParameter) allParams.put(key, param); 206 if ((old != null) && !allParams.containsValue(old)) { 207 waitingToDieParams.add(old); } 209 } 210 211 public void addToCurrentInputs(String key, IActionParameter param) { 212 currentInputs.put(key, param); 213 } 214 215 public boolean addOutputParameters(IActionDefinition actionDefinition) { 216 217 String key; 218 for (Iterator it = actionDefinition.getActionOutputDefinitions().keySet().iterator(); it.hasNext();) { 219 key = (String ) it.next(); 220 IActionParameter outputParam = (IActionParameter) currentOutputs.get(key); 221 key = actionDefinition.getMappedOutputName(key); 222 223 IActionParameter param = (IActionParameter)allParams.get( key ); 225 if ( param != null ) { 226 if ( param != outputParam ) { param.dispose(); 228 param.setValue( outputParam.getValue() ); 229 } 230 } 231 else { 232 addToAllInputs( key, outputParam ); 233 } 234 } 235 236 return (true); 237 } 238 239 246 public Map getReturnParameters() { 247 Map returnMap = new ListOrderedMap(); 248 249 for ( Iterator it = sequenceOutputDefs.entrySet().iterator(); it.hasNext(); ) { 251 Map.Entry entry = (Map.Entry )it.next(); 252 String outputName = (String )entry.getKey(); 253 IActionParameter outputParam = (IActionParameter)entry.getValue(); 254 255 IActionParameter inputParam = (IActionParameter) allParams.get(outputName); 258 if (inputParam == null) { 259 returnMap.put( outputParam.getName(), null ); 260 } 261 else { 262 for (Iterator varIt = outputParam.getVariables().iterator(); varIt.hasNext();) { 263 ActionParameterSource src = (ActionParameterSource) varIt.next(); 264 returnMap.put( outputParam.getName(), new ReturnParameter( src.getSourceName(), src.getValue(), inputParam.getValue() ) ); 265 } 266 } 267 } 268 return( returnMap ); 269 } 270 271 public class ReturnParameter { 272 public String destinationName; 273 public String destinationParameter; 274 public Object value; 275 276 public ReturnParameter( String destinationName, String destinationParameter, Object value ) { 277 this.destinationName = destinationName; 278 this.destinationParameter = destinationParameter; 279 this.value = value; 280 } 281 } 282 283 public String getActualRequestParameterName(String fieldName) { 284 298 IActionParameter actionParameter = getCurrentInput(fieldName); 300 if (actionParameter != null) { 301 List vars = actionParameter.getVariables(); 302 303 for (int i = 0; i < vars.size(); i++) { 304 ActionParameterSource source = (ActionParameterSource) (vars.get(i)); 305 if (source.getSourceName().equals( "request" )) { return source.getValue(); 307 } 308 } 309 } 310 return fieldName; 311 } 312 313 314 } 315 | Popular Tags |