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 Oct 15, 2005
014: * @author dmoran
015: */
016:
017: package org.pentaho.core.runtime;
018:
019: import java.util.ArrayList;
020: import java.util.HashMap;
021: import java.util.Iterator;
022: import java.util.List;
023: import java.util.Map;
024:
025: import org.pentaho.commons.connection.IPentahoMetaData;
026: import org.pentaho.commons.connection.IPentahoResultSet;
027:
028: public class SelectionMapper {
029:
030: /**
031: * Creates a SelectionMapper based on an IPentahoResultSet. If the result
032: * set has 1 column, the values and display names will come from that
033: * column. If it has more than 1 column, the first column will be used for
034: * the values and the second will be used for the display names
035: *
036: * @param resultSet
037: * The result set to get the data from
038: * @param displayName
039: * The name used to describe the choice for this selection.
040: * Usually used as a header
041: * @return SelectionMapper if successful or null
042: */
043: public static SelectionMapper create(IPentahoResultSet resultSet,
044: String displayName, String displayStyle) {
045: return (SelectionMapper.create(resultSet, 1, -1, displayName,
046: displayStyle));
047: }
048:
049: /**
050: * Creates a SelectionMapper based on an IPentahoResultSet. The columns to
051: * use for the values and display names are passed in as column names.
052: *
053: * @param resultSet
054: * The result set to get the data from
055: * @param valueColName
056: * The name of the column to use for the values. If null, the
057: * first column will be used
058: * @param dispColName
059: * The name of the column to use for the display names. If null,
060: * the values column will be used
061: * @param displayName
062: * The name used to describe the choice for this selection.
063: * Usually used as a header
064: * @return SelectionMapper if successful or null
065: */
066: public static SelectionMapper create(IPentahoResultSet resultSet,
067: String valueColName, String dispColName,
068: String displayName, String displayStyle) {
069: if (resultSet == null) {
070: return (null);
071: }
072:
073: IPentahoMetaData metaData = resultSet.getMetaData();
074: if ((metaData == null) || (metaData.getColumnCount() < 1)) {
075: // TODO surface an error
076: return (null);
077: }
078:
079: int valueColumnNo = (valueColName == null) ? 0 : metaData
080: .getColumnIndex(valueColName);
081: if (valueColumnNo < 0) {
082: // TODO surface an error
083: return (null);
084: }
085:
086: int dispColumnNo = -1;
087: if (dispColName != null) {
088: dispColumnNo = metaData.getColumnIndex(dispColName);
089: if (dispColumnNo < 0) {
090: // TODO surface an error
091: return (null);
092: }
093: }
094:
095: return (SelectionMapper.create(resultSet, ++valueColumnNo,
096: ++dispColumnNo, displayName, displayStyle));
097: }
098:
099: /**
100: * Creates a SelectionMapper based on an IPentahoResultSet. The index of the
101: * column to use for the values and display names are passed in. The index
102: * is 1 based so the first (left most) column is 1.
103: *
104: * @param resultSet
105: * The result set to get the data from
106: * @param valueColName
107: * The index of the column to use for the values.
108: * @param dispColName
109: * The index of the column to use for the display names. If 0
110: * then the valueColumn will be used.
111: * @param displayName
112: * The name used to describe the choice for this selection.
113: * Usually used as a header
114: * @return SelectionMapper if successful or null
115: */
116: public static SelectionMapper create(IPentahoResultSet resultSet,
117: int valueColIndex, int dispColIndex, String displayName,
118: String displayStyle) {
119: --valueColIndex;
120: --dispColIndex;
121:
122: if ((resultSet == null) || (valueColIndex < 0)) {
123: return (null);
124: }
125:
126: IPentahoMetaData metaData = resultSet.getMetaData();
127: if ((metaData == null)
128: || (metaData.getColumnCount() < valueColIndex)
129: || (metaData.getColumnCount() < dispColIndex)) {
130: return (null);
131: }
132:
133: ArrayList values = new ArrayList();
134:
135: HashMap displayNames = (dispColIndex < 0) ? null
136: : new HashMap();
137: Object row[] = resultSet.next();
138: Object value, name;
139: while (row != null) {
140: value = row[valueColIndex];
141: if (value != null) {
142: value = value.toString();
143: values.add(value);
144: if (displayNames != null) {
145: name = row[dispColIndex];
146: displayNames.put(value, (name != null) ? name
147: .toString() : value);
148: }
149: }
150: row = resultSet.next();
151: }
152: // close the result set so we can loop through it again later if we need to
153: resultSet.close();
154:
155: return (new SelectionMapper(values, displayNames, displayName,
156: displayStyle));
157: }
158:
159: /**
160: * Creates a SelectionMapper based on an IActionParameter. The columns to
161: * use for the values and display names are passed in as column names.
162: *
163: * @param actionParam
164: * The ActionParameter to get the data from
165: * @param valueColName
166: * The name of the column to use for the values. If null, the
167: * first column will be used
168: * @param dispColName
169: * The name of the column to use for the display names. If null,
170: * the values column will be used
171: * @param displayName
172: * The name used to describe the choice for this selection.
173: * Usually used as a header
174: * @return SelectionMapper if successful or null
175: */
176: public static SelectionMapper create(IActionParameter actionParam,
177: String valueColName, String dispColName,
178: String displayName, String displayStyle) {
179: if (actionParam == null) {
180: return (null);
181: }
182:
183: Object value = actionParam.getValue();
184: if (value instanceof IPentahoResultSet) {
185: return (create((IPentahoResultSet) value, valueColName,
186: dispColName, displayName, displayStyle));
187: } else if ("property-map-list".equals(actionParam.getType())) { //$NON-NLS-1$
188: return (createFromPropMapList((List) value, valueColName,
189: dispColName, displayName, displayStyle));
190: } else if (value instanceof List) {
191: return (new SelectionMapper((List) value, null,
192: displayName, displayStyle));
193: }
194:
195: return (null);
196: }
197:
198: /**
199: * Creates a SelectionMapper based on a pentaho property map list. The index
200: * of the column to use for the values and display names are passed in. The
201: * index is 1 based so the first (left most) column is 1.
202: *
203: * @param resultSet
204: * The result set to get the data from
205: * @param valueColName
206: * The index of the column to use for the values.
207: * @param dispColName
208: * The index of the column to use for the display names. If 0
209: * then the valueColumn will be used.
210: * @param displayName
211: * The name used to describe the choice for this selection.
212: * Usually used as a header
213: * @return SelectionMapper if successful or null
214: */
215: public static SelectionMapper createFromPropMapList(List aList,
216: String valueColName, String dispColName,
217: String displayName, String displayStyle) {
218: if (aList == null) {
219: return (null);
220: }
221:
222: ArrayList selValues = new ArrayList();
223: HashMap dispMap = new HashMap();
224: String val, disp;
225: for (Iterator it = aList.iterator(); it.hasNext();) {
226: try {
227: Map hm = (Map) it.next();
228: val = hm.get(valueColName).toString();
229: if (val != null) {
230: selValues.add(val);
231: }
232: disp = hm.get(dispColName).toString();
233: if (disp != null) {
234: dispMap.put(val, disp);
235: }
236: } catch (Exception ignore) {
237: }
238: }
239:
240: return (new SelectionMapper(selValues, dispMap, displayName,
241: displayStyle));
242: }
243:
244: Map selNames;
245:
246: List selValues;
247:
248: String displayName, displayStyle;
249:
250: private SelectionMapper(List selValues, Map selNames,
251: String displayName, String displayStyle) {
252: this .displayName = (displayName != null) ? displayName : ""; //$NON-NLS-1$
253: this .selNames = selNames;
254: this .selValues = (selValues != null) ? selValues
255: : new ArrayList();
256: this .displayStyle = displayStyle;
257: }
258:
259: public String getDisplayStyle() {
260: return displayStyle;
261: }
262:
263: public String getSelectionDisplayName() {
264: return (displayName);
265: }
266:
267: public String getSelectionNameForValue(String val) {
268: Object rtn = null;
269: if (selNames != null) {
270: rtn = selNames.get(val);
271: }
272: return ((rtn == null) ? val : rtn.toString());
273: }
274:
275: public List getSelectionValues() {
276: return (selValues);
277: }
278:
279: public Map getSelectionNameMap() {
280: return (selNames);
281: }
282:
283: public boolean hasValue(String value) {
284: return (selValues.contains(value));
285: }
286:
287: public int selectionCount() {
288: return (selValues.size());
289: }
290:
291: public String getValueAt(int index) {
292: return (selValues.get(index).toString());
293: }
294:
295: public String toString() {
296: StringBuffer sb = new StringBuffer("Display Name: ").append(getSelectionDisplayName()).append(" ["); //$NON-NLS-1$ //$NON-NLS-2$
297: for (Iterator it = selValues.iterator(); it.hasNext();) {
298: String value = it.next().toString();
299: sb
300: .append(" [").append(value).append(" : ").append(getSelectionNameForValue(value)).append("] "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
301: }
302: sb.append("]"); //$NON-NLS-1$
303: return (sb.toString());
304: }
305:
306: }
|