KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > core > runtime > ParameterManager


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 package org.pentaho.core.runtime;
14
15 import java.util.ArrayList JavaDoc;
16 import java.util.Iterator JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.Map JavaDoc;
19 import java.util.Set JavaDoc;
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 JavaDoc[] EMPTY_ARRAY = new String JavaDoc[0];
33     
34     private Map JavaDoc allParams;
35     private Map JavaDoc allResources;
36     
37     private List JavaDoc waitingToDieParams;
38
39     private Map JavaDoc currentInputs;
40     private Map JavaDoc currentOutputs;
41     private Map JavaDoc currentResources;
42     
43     private String JavaDoc[] sequenceInputNames;
44     private String JavaDoc[] sequenceResourceNames;
45     private static final Log logger = LogFactory.getLog(ParameterManager.class);
46     
47     private Map JavaDoc sequenceOutputDefs;
48     
49     ParameterManager() {
50         allParams = new ListOrderedMap();
51         allResources = new ListOrderedMap();
52
53         waitingToDieParams = new ArrayList JavaDoc();
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 JavaDoc[])actionSequence.getInputDefinitions().keySet().toArray( EMPTY_ARRAY );
67
68         allResources.putAll( actionSequence.getResourceDefinitions() );
69         sequenceResourceNames = (String JavaDoc[])actionSequence.getResourceDefinitions().keySet().toArray( EMPTY_ARRAY );
70
71         sequenceOutputDefs.putAll( actionSequence.getOutputDefinitions() );
72     }
73     
74     public Map JavaDoc getAllParameters() {
75         return( allParams );
76     }
77     
78     public IActionParameter getCurrentInput( String JavaDoc inputName ) {
79         return( (IActionParameter) currentInputs.get(inputName) );
80     }
81
82     public IActionParameter getCurrentOutput( String JavaDoc outputName ) {
83         return( (IActionParameter) currentOutputs.get(outputName) );
84     }
85
86     public IActionResource getCurrentResource( String JavaDoc resourceName ) {
87         return( (IActionResource) currentResources.get(resourceName) );
88     }
89
90     public Set JavaDoc getCurrentInputNames() {
91         return currentInputs.keySet();
92     }
93
94     public IActionParameter getLoopParameter( String JavaDoc inputName ) {
95         return( (IActionParameter) allParams.get(inputName) );
96     }
97
98     public Set JavaDoc getCurrentOutputNames() {
99         return currentOutputs.keySet();
100     }
101
102     public Set JavaDoc 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 JavaDoc th) {
113         // Do something here
114
logger.error(Messages.getErrorString("ParameterManager.ERROR_0001_DISPOSE_ERROR", param.getName()), th); //$NON-NLS-1$
115
}
116       return false;
117     }
118     
119     public void dispose() {
120         if (allParams != null) {
121             for (Iterator JavaDoc it = allParams.values().iterator(); it.hasNext();) {
122               disposeParameter((ActionParameter) it.next());
123             }
124             for (Iterator JavaDoc 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 JavaDoc resetMap( String JavaDoc[] names, Map JavaDoc oldMap ) {
141         Map JavaDoc 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 JavaDoc it = sequenceOutputDefs.entrySet().iterator(); it.hasNext(); ) {
158                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc)it.next();
159                 String JavaDoc outputName = (String JavaDoc)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 JavaDoc key;
172         Object JavaDoc value;
173         for (Iterator JavaDoc it = actionDefinition.getActionInputDefinitions().keySet().iterator(); it.hasNext();) {
174             key = (String JavaDoc) 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; // Only use if there is a default value;
180
}
181             }
182
183             if (value != null) {
184                 currentInputs.put(key, value);
185             }
186         }
187
188         currentOutputs.putAll(actionDefinition.getActionOutputDefinitions());
189
190       // This enables the old behavior - It should eventually be removed
191
if ( !actionDefinition.hasActionResources() ) {
192           currentResources.putAll( allResources );
193       }
194       else {
195           for (Iterator JavaDoc it = actionDefinition.getActionResourceDefinitionNames().iterator(); it.hasNext();) {
196               key = (String JavaDoc)it.next();
197               String JavaDoc key2 = actionDefinition.getMappedResourceName(key);
198               value = allResources.get( key2 );
199               currentResources.put( key, value );
200           }
201       }
202     }
203
204     public void addToAllInputs(String JavaDoc key, IActionParameter param) {
205         IActionParameter old = (IActionParameter) allParams.put(key, param);
206         if ((old != null) && !allParams.containsValue(old)) {
207             waitingToDieParams.add(old); // Just in case a parameter gets over written delete it at the final dispose
208
}
209     }
210
211     public void addToCurrentInputs(String JavaDoc key, IActionParameter param) {
212         currentInputs.put(key, param);
213     }
214
215     public boolean addOutputParameters(IActionDefinition actionDefinition) {
216
217         String JavaDoc key;
218         for (Iterator JavaDoc it = actionDefinition.getActionOutputDefinitions().keySet().iterator(); it.hasNext();) {
219             key = (String JavaDoc) it.next();
220             IActionParameter outputParam = (IActionParameter) currentOutputs.get(key);
221             key = actionDefinition.getMappedOutputName(key);
222             
223             // If we already have a parameter with this name, set the value and reuse the definition.
224
IActionParameter param = (IActionParameter)allParams.get( key );
225             if ( param != null ) {
226                 if ( param != outputParam ) { // This is a trap for catching temp params that didn't get deleted at the end of the last loop
227
param.dispose();
228                     param.setValue( outputParam.getValue() );
229                 }
230             }
231             else {
232                 addToAllInputs( key, outputParam );
233             }
234         }
235
236         return (true);
237     }
238     
239     /**
240      * Returns a mapping of output parameters and the value and destination.
241      *
242      * @param actionSequence The Action Sequence definition to use
243      *
244      * @return a map with the param name as the key and a ReturnParameter containing the data.
245      */

246     public Map JavaDoc getReturnParameters() {
247         Map JavaDoc returnMap = new ListOrderedMap();
248         
249         // Iterate for each output defined
250
for ( Iterator JavaDoc it = sequenceOutputDefs.entrySet().iterator(); it.hasNext(); ) {
251             Map.Entry JavaDoc entry = (Map.Entry JavaDoc)it.next();
252             String JavaDoc outputName = (String JavaDoc)entry.getKey();
253             IActionParameter outputParam = (IActionParameter)entry.getValue();
254
255             // The output Action Parameter objects do not have values - they are just the definition
256
// Pull the value from allParams
257
IActionParameter inputParam = (IActionParameter) allParams.get(outputName);
258             if (inputParam == null) {
259                 returnMap.put( outputParam.getName(), null );
260             }
261             else {
262                 for (Iterator JavaDoc 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 JavaDoc destinationName;
273         public String JavaDoc destinationParameter;
274         public Object JavaDoc value;
275         
276         public ReturnParameter( String JavaDoc destinationName, String JavaDoc destinationParameter, Object JavaDoc value ) {
277             this.destinationName = destinationName;
278             this.destinationParameter = destinationParameter;
279             this.value = value;
280         }
281     }
282     
283     public String JavaDoc getActualRequestParameterName(String JavaDoc fieldName) {
284         /*
285          * This method solves the problem that exists when generating an xForm
286          * based on the parameter definition. The parameter definition looks
287          * like this:
288          *
289          * <REGION type="string"> <default-value></default-value> <sources>
290          * <request>regn</request> </sources> </REGION>
291          *
292          * In the above definition, the parameter name is REGION, but we'll be
293          * looking for the variable regn in the request. Before this fix, the
294          * XForm would generate code that puts REGION on the request, not regn.
295          *
296          * MB
297          */

298         // TODO - figure out the actual name used - this is just a guess - maybe store in the ActionParam
299
IActionParameter actionParameter = getCurrentInput(fieldName);
300         if (actionParameter != null) {
301             List JavaDoc 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" )) { //$NON-NLS-1$
306
return source.getValue();
307                 }
308             }
309         }
310         return fieldName;
311     }
312
313     
314 }
315
Popular Tags