KickJava   Java API By Example, From Geeks To Geeks.

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


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

17
18 package org.pentaho.core.runtime;
19
20 import java.util.*;
21
22 import org.pentaho.core.connection.IPentahoResultSet;
23
24 public class ActionParameter implements IActionParameter {
25
26     private String JavaDoc name;
27
28     private String JavaDoc type;
29
30     private Object JavaDoc value; // not persisted
31

32     private List variables;
33
34     private Object JavaDoc defaultValue;
35
36     private ParamSelections paramSelections = null;
37
38     private int promptType = IActionParameter.PROMPT_ALLOWED;
39
40     private List saveLocations = null;
41
42     // should we force this to String for serialization?
43
// TODO: add type checking
44

45     public ActionParameter(String JavaDoc name, String JavaDoc type, Object JavaDoc value, List variables, Object JavaDoc defaultValue) {
46         this.name = name;
47         this.value = value;
48         this.type = type;
49         this.variables = variables;
50
51         // DM - get this working here for now - should be it's own factory - fix
52
// up all the conversions
53
this.defaultValue = null;
54         if (defaultValue != null) {
55             if ("string".equalsIgnoreCase(type)) { //$NON-NLS-1$
56
this.defaultValue = defaultValue.toString();
57             } else if ("string-list".equalsIgnoreCase(type) || "property-map-list".equalsIgnoreCase(type)) { //$NON-NLS-1$ //$NON-NLS-2$
58
if (defaultValue instanceof List)
59                     this.defaultValue = defaultValue;
60             } else if ("property-map".equalsIgnoreCase(type)) { //$NON-NLS-1$
61
if (defaultValue instanceof Map)
62                     this.defaultValue = defaultValue;
63             } else if ("result-set".equalsIgnoreCase(type)) { //$NON-NLS-1$
64
if (defaultValue instanceof IPentahoResultSet)
65                     this.defaultValue = defaultValue;
66             } else if ("long".equalsIgnoreCase(type)) { //$NON-NLS-1$
67
try {
68                     this.defaultValue = Long.valueOf(defaultValue.toString());
69                 } catch (Exception JavaDoc e) {
70                     // @todo: log this throw an exception
71
}
72             }
73         }
74         if ((value == null) && (defaultValue != null)) {
75             promptType = PROMPT_NEEDED;
76         }
77     }
78
79     public List getSaveLocations() {
80         return (saveLocations == null) ? new ArrayList() : saveLocations;
81     }
82
83     public void addSaveLocation(String JavaDoc location) {
84         if (saveLocations == null) {
85             saveLocations = new ArrayList();
86         }
87         saveLocations.add(location);
88         return;
89     }
90
91     public List getVariables() {
92         return (variables == null) ? new ArrayList() : variables;
93     }
94
95     public String JavaDoc getName() {
96         return name;
97     }
98
99     public String JavaDoc getType() {
100         return type;
101     }
102
103     public String JavaDoc getStringValue() {
104         return ((value != null) ? value.toString() : ((defaultValue != null) ? defaultValue.toString() : null));
105     }
106
107     public Object JavaDoc getValue() {
108         return ((value != null) ? value : defaultValue);
109     }
110
111     public List getValueAsList() {
112         Object JavaDoc rtn = (value != null) ? value : defaultValue;
113         if (rtn == null) {
114             return (new ArrayList());
115         }
116
117         if (rtn instanceof List) {
118             return ((List) rtn);
119         }
120
121         ArrayList al = new ArrayList();
122         al.add(rtn);
123         return (al);
124     }
125
126     public IPentahoResultSet getValueAsResultSet() {
127         Object JavaDoc rtn = (value != null) ? value : defaultValue;
128         if (rtn == null) {
129             return null;
130         }
131
132         if (rtn instanceof IPentahoResultSet) {
133             return ((IPentahoResultSet) rtn);
134         }
135
136         return null;
137     }
138
139     public void setValue(Object JavaDoc value) {
140         // TODO need to validate the types here
141
setPromptStatus((value == null) ? PROMPT_NEEDED : PROMPT_ALLOWED);
142         dispose();
143         this.value = value;
144     }
145
146     public boolean hasDefaultValue() {
147         return (defaultValue != null);
148     }
149
150     public boolean hasValue() {
151         return (value != null);
152     }
153
154     public boolean isDefaultValue() {
155         return ((value == null) && (defaultValue != null));
156     }
157
158     public boolean isNull() {
159         return ((value == null) && (defaultValue == null));
160     }
161
162     public void dispose() {
163         if (value != null && value instanceof IDisposable) {
164             ((IDisposable) value).dispose();
165         }
166     }
167
168     // // Selection Support
169

170     public int getPromptType() {
171         return (promptType);
172     }
173
174     public boolean hasSelections() {
175         return (paramSelections != null);
176     }
177
178     public String JavaDoc getSelectionDisplayName() {
179         return ((hasSelections()) ? paramSelections.displayName : ""); //$NON-NLS-1$
180
}
181
182     public String JavaDoc getSelectionNameForValue(String JavaDoc val) {
183         Object JavaDoc rtn = null;
184         if (hasSelections() && paramSelections.selNames != null) {
185             rtn = paramSelections.selNames.get(val);
186         }
187         return ((rtn != null) ? rtn.toString() : val);
188     }
189
190     public List getSelectionValues() {
191         return ((hasSelections()) ? paramSelections.selValues : new ArrayList());
192     }
193
194     /**
195      * Unused
196      * @deprecated
197      */

198     public void setParamSelections(List selValues, Map selNames, String JavaDoc displayname) {
199         paramSelections = new ParamSelections(selValues, selNames, displayname);
200     }
201
202     public Map getSelectionNameMap() {
203         return ((hasSelections()) ? paramSelections.selNames : new HashMap());
204     }
205
206     class ParamSelections {
207         Map selNames;
208
209         List selValues;
210
211         String JavaDoc displayName;
212
213         ParamSelections(List selValues, Map selNames, String JavaDoc displayname) {
214             this.displayName = (displayname != null) ? displayname : ""; //$NON-NLS-1$
215
this.selNames = (selNames != null) ? selNames : new HashMap();
216             this.selValues = (selValues != null) ? selValues : new ArrayList();
217         }
218     }
219
220     public int getPromptStatus() {
221         return (promptType);
222     }
223
224     public boolean setPromptStatus(int status) {
225         if ((promptType == PROMPT_NEVER) || (status < 0) || (status > 3)) {
226             return (false);
227         }
228
229         promptType = status;
230         return (true);
231     }
232
233 }
234
Popular Tags