KickJava   Java API By Example, From Geeks To Geeks.

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


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 Oct 15, 2005
14  * @author dmoran
15  */

16
17 package org.pentaho.core.runtime;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import org.pentaho.core.connection.IPentahoMetaData;
26 import org.pentaho.core.connection.IPentahoResultSet;
27
28 public class SelectionMapper {
29
30     /**
31      * Creates a SelectionMapper based on an IPentahoResultSet. If the result
32      * set has 1 column, the values and display names will come from that
33      * column. If it has more than 1 column, the first column will be used for
34      * the values and the second will be used for the display names
35      *
36      * @param resultSet
37      * The result set to get the data from
38      * @param displayName
39      * The name used to describe the choice for this selection.
40      * Usually used as a header
41      * @return SelectionMapper if successful or null
42      */

43     public static SelectionMapper create(IPentahoResultSet resultSet, String JavaDoc displayName, String JavaDoc displayStyle) {
44         return (SelectionMapper.create(resultSet, 1, -1, displayName, displayStyle));
45     }
46
47     /**
48      * Creates a SelectionMapper based on an IPentahoResultSet. The columns to
49      * use for the values and display names are passed in as column names.
50      *
51      * @param resultSet
52      * The result set to get the data from
53      * @param valueColName
54      * The name of the column to use for the values. If null, the
55      * first column will be used
56      * @param dispColName
57      * The name of the column to use for the display names. If null,
58      * the values column will be used
59      * @param displayName
60      * The name used to describe the choice for this selection.
61      * Usually used as a header
62      * @return SelectionMapper if successful or null
63      */

64     public static SelectionMapper create(IPentahoResultSet resultSet, String JavaDoc valueColName, String JavaDoc dispColName, String JavaDoc displayName, String JavaDoc displayStyle) {
65         if (resultSet == null) {
66             return (null);
67         }
68
69         IPentahoMetaData metaData = resultSet.getMetaData();
70         if ((metaData == null) || (metaData.getColumnCount() < 1)) {
71             // TODO surface an error
72
return (null);
73         }
74
75         int valueColumnNo = (valueColName == null) ? 0 : metaData.getColumnIndex(valueColName);
76         if (valueColumnNo < 0) {
77             // TODO surface an error
78
return (null);
79         }
80
81         int dispColumnNo = -1;
82         if (dispColName != null) {
83             dispColumnNo = metaData.getColumnIndex(dispColName);
84             if (dispColumnNo < 0) {
85                 // TODO surface an error
86
return (null);
87             }
88         }
89
90         return (SelectionMapper.create(resultSet, ++valueColumnNo, ++dispColumnNo, displayName, displayStyle));
91     }
92
93     /**
94      * Creates a SelectionMapper based on an IPentahoResultSet. The index of the
95      * column to use for the values and display names are passed in. The index
96      * is 1 based so the first (left most) column is 1.
97      *
98      * @param resultSet
99      * The result set to get the data from
100      * @param valueColName
101      * The index of the column to use for the values.
102      * @param dispColName
103      * The index of the column to use for the display names. If 0
104      * then the valueColumn will be used.
105      * @param displayName
106      * The name used to describe the choice for this selection.
107      * Usually used as a header
108      * @return SelectionMapper if successful or null
109      */

110     public static SelectionMapper create(IPentahoResultSet resultSet, int valueColIndex, int dispColIndex, String JavaDoc displayName, String JavaDoc displayStyle) {
111         --valueColIndex;
112         --dispColIndex;
113
114         if ((resultSet == null) || (valueColIndex < 0)) {
115             return (null);
116         }
117
118         IPentahoMetaData metaData = resultSet.getMetaData();
119         if ((metaData == null) || (metaData.getColumnCount() < valueColIndex) || (metaData.getColumnCount() < dispColIndex)) {
120             return (null);
121         }
122
123         ArrayList JavaDoc values = new ArrayList JavaDoc();
124
125         HashMap JavaDoc displayNames = (dispColIndex < 0) ? null : new HashMap JavaDoc();
126         Object JavaDoc row[] = resultSet.next();
127         Object JavaDoc value, name;
128         while ( row != null ) {
129             value = row[valueColIndex];
130             if ( value != null ) {
131                 value = value.toString();
132                 values.add( value );
133                 if (displayNames != null) {
134                     name = row[dispColIndex];
135                     displayNames.put(value, (name != null) ? name.toString() : value);
136                 }
137             }
138             row = resultSet.next();
139         }
140         // close the result set so we can loop through it again later if we need to
141
resultSet.close();
142
143         return (new SelectionMapper(values, displayNames, displayName, displayStyle));
144     }
145
146     /**
147      * Creates a SelectionMapper based on an IActionParameter. The columns to
148      * use for the values and display names are passed in as column names.
149      *
150      * @param actionParam
151      * The ActionParameter to get the data from
152      * @param valueColName
153      * The name of the column to use for the values. If null, the
154      * first column will be used
155      * @param dispColName
156      * The name of the column to use for the display names. If null,
157      * the values column will be used
158      * @param displayName
159      * The name used to describe the choice for this selection.
160      * Usually used as a header
161      * @return SelectionMapper if successful or null
162      */

163     public static SelectionMapper create(IActionParameter actionParam, String JavaDoc valueColName, String JavaDoc dispColName, String JavaDoc displayName, String JavaDoc displayStyle) {
164         if (actionParam == null) {
165             return (null);
166         }
167
168         Object JavaDoc value = actionParam.getValue();
169         if (value instanceof IPentahoResultSet) {
170             return (create((IPentahoResultSet) value, valueColName, dispColName, displayName, displayStyle));
171         } else if ("property-map-list".equals(actionParam.getType())) { //$NON-NLS-1$
172
return (createFromPropMapList((List JavaDoc) value, valueColName, dispColName, displayName, displayStyle));
173         } else if (value instanceof List JavaDoc) {
174             return (new SelectionMapper((List JavaDoc) value, null, displayName, displayStyle));
175         }
176
177         return (null);
178     }
179
180     /**
181      * Creates a SelectionMapper based on a pentaho property map list. The index
182      * of the column to use for the values and display names are passed in. The
183      * index is 1 based so the first (left most) column is 1.
184      *
185      * @param resultSet
186      * The result set to get the data from
187      * @param valueColName
188      * The index of the column to use for the values.
189      * @param dispColName
190      * The index of the column to use for the display names. If 0
191      * then the valueColumn will be used.
192      * @param displayName
193      * The name used to describe the choice for this selection.
194      * Usually used as a header
195      * @return SelectionMapper if successful or null
196      */

197     public static SelectionMapper createFromPropMapList(List JavaDoc aList, String JavaDoc valueColName, String JavaDoc dispColName, String JavaDoc displayName, String JavaDoc displayStyle) {
198         if (aList == null) {
199             return (null);
200         }
201
202         ArrayList JavaDoc selValues = new ArrayList JavaDoc();
203         HashMap JavaDoc dispMap = new HashMap JavaDoc();
204         String JavaDoc val, disp;
205         for (Iterator JavaDoc it = aList.iterator(); it.hasNext();) {
206             try {
207                 Map JavaDoc hm = (Map JavaDoc) it.next();
208                 val = hm.get(valueColName).toString();
209                 if (val != null) {
210                     selValues.add(val);
211                 }
212                 disp = hm.get(dispColName).toString();
213                 if (disp != null) {
214                     dispMap.put(val, disp);
215                 }
216             } catch (Exception JavaDoc ignore) {
217             }
218         }
219
220         return (new SelectionMapper(selValues, dispMap, displayName, displayStyle));
221     }
222
223     Map JavaDoc selNames;
224
225     List JavaDoc selValues;
226
227     String JavaDoc displayName, displayStyle;
228
229     private SelectionMapper(List JavaDoc selValues, Map JavaDoc selNames, String JavaDoc displayName, String JavaDoc displayStyle) {
230         this.displayName = (displayName != null) ? displayName : ""; //$NON-NLS-1$
231
this.selNames = selNames;
232         this.selValues = (selValues != null) ? selValues : new ArrayList JavaDoc();
233         this.displayStyle = displayStyle;
234     }
235
236     public String JavaDoc getDisplayStyle() {
237         return displayStyle;
238     }
239
240     public String JavaDoc getSelectionDisplayName() {
241         return (displayName);
242     }
243
244     public String JavaDoc getSelectionNameForValue(String JavaDoc val) {
245         Object JavaDoc rtn = null;
246         if (selNames != null) {
247             rtn = selNames.get(val);
248         }
249         return ((rtn == null) ? val : rtn.toString());
250     }
251
252     public List JavaDoc getSelectionValues() {
253         return (selValues);
254     }
255
256     public Map JavaDoc getSelectionNameMap() {
257         return (selNames);
258     }
259
260     public boolean hasValue(String JavaDoc value) {
261         return (selValues.contains(value));
262     }
263
264     public int selectionCount() {
265         return (selValues.size());
266     }
267
268     public String JavaDoc getValueAt(int index) {
269         return (selValues.get(index).toString());
270     }
271
272     public String JavaDoc toString() {
273         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("Display Name: ").append(getSelectionDisplayName()).append(" ["); //$NON-NLS-1$ //$NON-NLS-2$
274
for (Iterator JavaDoc it = selValues.iterator(); it.hasNext();) {
275             String JavaDoc value = it.next().toString();
276             sb.append(" [").append(value).append(" : ").append(getSelectionNameForValue(value)).append("] "); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
277
}
278         sb.append("]"); //$NON-NLS-1$
279
return (sb.toString());
280     }
281
282 }
283
Popular Tags