1 16 17 package org.pentaho.core.runtime; 18 19 import java.util.ArrayList ; 20 import java.util.HashMap ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 import java.util.Map ; 24 25 import org.pentaho.core.connection.IPentahoMetaData; 26 import org.pentaho.core.connection.IPentahoResultSet; 27 28 public class SelectionMapper { 29 30 43 public static SelectionMapper create(IPentahoResultSet resultSet, String displayName, String displayStyle) { 44 return (SelectionMapper.create(resultSet, 1, -1, displayName, displayStyle)); 45 } 46 47 64 public static SelectionMapper create(IPentahoResultSet resultSet, String valueColName, String dispColName, String displayName, String displayStyle) { 65 if (resultSet == null) { 66 return (null); 67 } 68 69 IPentahoMetaData metaData = resultSet.getMetaData(); 70 if ((metaData == null) || (metaData.getColumnCount() < 1)) { 71 return (null); 73 } 74 75 int valueColumnNo = (valueColName == null) ? 0 : metaData.getColumnIndex(valueColName); 76 if (valueColumnNo < 0) { 77 return (null); 79 } 80 81 int dispColumnNo = -1; 82 if (dispColName != null) { 83 dispColumnNo = metaData.getColumnIndex(dispColName); 84 if (dispColumnNo < 0) { 85 return (null); 87 } 88 } 89 90 return (SelectionMapper.create(resultSet, ++valueColumnNo, ++dispColumnNo, displayName, displayStyle)); 91 } 92 93 110 public static SelectionMapper create(IPentahoResultSet resultSet, int valueColIndex, int dispColIndex, String displayName, String 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 values = new ArrayList (); 124 125 HashMap displayNames = (dispColIndex < 0) ? null : new HashMap (); 126 Object row[] = resultSet.next(); 127 Object 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 resultSet.close(); 142 143 return (new SelectionMapper(values, displayNames, displayName, displayStyle)); 144 } 145 146 163 public static SelectionMapper create(IActionParameter actionParam, String valueColName, String dispColName, String displayName, String displayStyle) { 164 if (actionParam == null) { 165 return (null); 166 } 167 168 Object 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())) { return (createFromPropMapList((List ) value, valueColName, dispColName, displayName, displayStyle)); 173 } else if (value instanceof List ) { 174 return (new SelectionMapper((List ) value, null, displayName, displayStyle)); 175 } 176 177 return (null); 178 } 179 180 197 public static SelectionMapper createFromPropMapList(List aList, String valueColName, String dispColName, String displayName, String displayStyle) { 198 if (aList == null) { 199 return (null); 200 } 201 202 ArrayList selValues = new ArrayList (); 203 HashMap dispMap = new HashMap (); 204 String val, disp; 205 for (Iterator it = aList.iterator(); it.hasNext();) { 206 try { 207 Map hm = (Map ) 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 ignore) { 217 } 218 } 219 220 return (new SelectionMapper(selValues, dispMap, displayName, displayStyle)); 221 } 222 223 Map selNames; 224 225 List selValues; 226 227 String displayName, displayStyle; 228 229 private SelectionMapper(List selValues, Map selNames, String displayName, String displayStyle) { 230 this.displayName = (displayName != null) ? displayName : ""; this.selNames = selNames; 232 this.selValues = (selValues != null) ? selValues : new ArrayList (); 233 this.displayStyle = displayStyle; 234 } 235 236 public String getDisplayStyle() { 237 return displayStyle; 238 } 239 240 public String getSelectionDisplayName() { 241 return (displayName); 242 } 243 244 public String getSelectionNameForValue(String val) { 245 Object rtn = null; 246 if (selNames != null) { 247 rtn = selNames.get(val); 248 } 249 return ((rtn == null) ? val : rtn.toString()); 250 } 251 252 public List getSelectionValues() { 253 return (selValues); 254 } 255 256 public Map getSelectionNameMap() { 257 return (selNames); 258 } 259 260 public boolean hasValue(String value) { 261 return (selValues.contains(value)); 262 } 263 264 public int selectionCount() { 265 return (selValues.size()); 266 } 267 268 public String getValueAt(int index) { 269 return (selValues.get(index).toString()); 270 } 271 272 public String toString() { 273 StringBuffer sb = new StringBuffer ("Display Name: ").append(getSelectionDisplayName()).append(" ["); for (Iterator it = selValues.iterator(); it.hasNext();) { 275 String value = it.next().toString(); 276 sb.append(" [").append(value).append(" : ").append(getSelectionNameForValue(value)).append("] "); } 278 sb.append("]"); return (sb.toString()); 280 } 281 282 } 283
| Popular Tags
|