001: /*
002: * Copyright 2006 Pentaho Corporation. All rights reserved.
003: * This software was developed by Pentaho Corporation and is provided under the terms
004: * of the Mozilla Public License, Version 1.1, or any later version. You may not use
005: * this file except in compliance with the license. If you need a copy of the license,
006: * please go to http://www.mozilla.org/MPL/MPL-1.1.txt. The Original Code is the Pentaho
007: * BI Platform. The Initial Developer is Pentaho Corporation.
008: *
009: * Software distributed under the Mozilla Public License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. Please refer to
011: * the license for the specific language governing your rights and limitations.
012: *
013: * @created Apr 14, 2005
014: * @author James Dixon
015: *
016: */
017:
018: package org.pentaho.core.runtime;
019:
020: import java.util.*;
021:
022: import org.pentaho.commons.connection.IPentahoResultSet;
023: import org.pentaho.commons.connection.IDisposable;
024:
025: public class ActionParameter implements IActionParameter {
026:
027: private String name;
028:
029: private String type;
030:
031: private Object value; // not persisted
032:
033: private List variables;
034:
035: private Object defaultValue;
036:
037: private ParamSelections paramSelections = null;
038:
039: private int promptType = IActionParameter.PROMPT_ALLOWED;
040:
041: private List saveLocations = null;
042:
043: // should we force this to String for serialization?
044: // TODO: add type checking
045:
046: public ActionParameter(String name, String type, Object value,
047: List variables, Object defaultValue) {
048: this .name = name;
049: this .value = value;
050: this .type = type;
051: this .variables = variables;
052:
053: // DM - get this working here for now - should be it's own factory - fix
054: // up all the conversions
055: this .defaultValue = null;
056: if (defaultValue != null) {
057: if ("string".equalsIgnoreCase(type)) { //$NON-NLS-1$
058: this .defaultValue = defaultValue.toString();
059: } else if ("string-list".equalsIgnoreCase(type) || "property-map-list".equalsIgnoreCase(type)) { //$NON-NLS-1$ //$NON-NLS-2$
060: if (defaultValue instanceof List)
061: this .defaultValue = defaultValue;
062: } else if ("property-map".equalsIgnoreCase(type)) { //$NON-NLS-1$
063: if (defaultValue instanceof Map)
064: this .defaultValue = defaultValue;
065: } else if ("result-set".equalsIgnoreCase(type)) { //$NON-NLS-1$
066: if (defaultValue instanceof IPentahoResultSet)
067: this .defaultValue = defaultValue;
068: } else if ("long".equalsIgnoreCase(type)) { //$NON-NLS-1$
069: try {
070: this .defaultValue = Long.valueOf(defaultValue
071: .toString());
072: } catch (Exception e) {
073: // @todo: log this throw an exception
074: }
075: }
076: }
077: if ((value == null) && (defaultValue != null)) {
078: promptType = PROMPT_NEEDED;
079: }
080: }
081:
082: public List getSaveLocations() {
083: return (saveLocations == null) ? new ArrayList()
084: : saveLocations;
085: }
086:
087: public void addSaveLocation(String location) {
088: if (saveLocations == null) {
089: saveLocations = new ArrayList();
090: }
091: saveLocations.add(location);
092: return;
093: }
094:
095: public List getVariables() {
096: return (variables == null) ? new ArrayList() : variables;
097: }
098:
099: public String getName() {
100: return name;
101: }
102:
103: public String getType() {
104: return type;
105: }
106:
107: public String getStringValue() {
108: return ((value != null) ? value.toString()
109: : ((defaultValue != null) ? defaultValue.toString()
110: : null));
111: }
112:
113: public Object getValue() {
114: return ((value != null) ? value : defaultValue);
115: }
116:
117: public List getValueAsList() {
118: Object rtn = (value != null) ? value : defaultValue;
119: if (rtn == null) {
120: return (new ArrayList());
121: }
122:
123: if (rtn instanceof List) {
124: return ((List) rtn);
125: }
126:
127: ArrayList al = new ArrayList();
128: al.add(rtn);
129: return (al);
130: }
131:
132: public IPentahoResultSet getValueAsResultSet() {
133: Object rtn = (value != null) ? value : defaultValue;
134: if (rtn == null) {
135: return null;
136: }
137:
138: if (rtn instanceof IPentahoResultSet) {
139: return ((IPentahoResultSet) rtn);
140: }
141:
142: return null;
143: }
144:
145: public void setValue(Object value) {
146: // TODO need to validate the types here
147: setPromptStatus((value == null) ? PROMPT_NEEDED
148: : PROMPT_ALLOWED);
149: if (this .value != value) {
150: dispose();
151: this .value = value;
152: }
153: }
154:
155: public boolean hasDefaultValue() {
156: return (defaultValue != null);
157: }
158:
159: public boolean hasValue() {
160: return (value != null);
161: }
162:
163: public boolean isDefaultValue() {
164: return ((value == null) && (defaultValue != null));
165: }
166:
167: public boolean isNull() {
168: return ((value == null) && (defaultValue == null));
169: }
170:
171: public void dispose() {
172: if (value != null && value instanceof IDisposable) {
173: ((IDisposable) value).dispose();
174: }
175: }
176:
177: // // Selection Support
178:
179: public int getPromptType() {
180: return (promptType);
181: }
182:
183: public boolean hasSelections() {
184: return (paramSelections != null);
185: }
186:
187: public String getSelectionDisplayName() {
188: return ((hasSelections()) ? paramSelections.displayName : ""); //$NON-NLS-1$
189: }
190:
191: public String getSelectionNameForValue(String val) {
192: Object rtn = null;
193: if (hasSelections() && paramSelections.selNames != null) {
194: rtn = paramSelections.selNames.get(val);
195: }
196: return ((rtn != null) ? rtn.toString() : val);
197: }
198:
199: public List getSelectionValues() {
200: return ((hasSelections()) ? paramSelections.selValues
201: : new ArrayList());
202: }
203:
204: /**
205: * Unused
206: * @deprecated
207: */
208: public void setParamSelections(List selValues, Map selNames,
209: String displayname) {
210: paramSelections = new ParamSelections(selValues, selNames,
211: displayname);
212: }
213:
214: public Map getSelectionNameMap() {
215: return ((hasSelections()) ? paramSelections.selNames
216: : new HashMap());
217: }
218:
219: class ParamSelections {
220: Map selNames;
221:
222: List selValues;
223:
224: String displayName;
225:
226: ParamSelections(List selValues, Map selNames, String displayname) {
227: this .displayName = (displayname != null) ? displayname : ""; //$NON-NLS-1$
228: this .selNames = (selNames != null) ? selNames
229: : new HashMap();
230: this .selValues = (selValues != null) ? selValues
231: : new ArrayList();
232: }
233: }
234:
235: public int getPromptStatus() {
236: return (promptType);
237: }
238:
239: public boolean setPromptStatus(int status) {
240: if ((promptType == PROMPT_NEVER) || (status < 0)
241: || (status > 3)) {
242: return (false);
243: }
244:
245: promptType = status;
246: return (true);
247: }
248:
249: }
|