1 16 17 package org.pentaho.ui.component; 18 19 import java.io.OutputStream ; 20 import java.util.ArrayList ; 21 import java.util.HashMap ; 22 import java.util.Iterator ; 23 import java.util.Map ; 24 25 import org.dom4j.Element; 26 import org.pentaho.core.connection.IPentahoMetaData; 27 import org.pentaho.core.connection.IPentahoResultSet; 28 import org.pentaho.core.runtime.IActionParameter; 29 import org.pentaho.core.runtime.IRuntimeContext; 30 import org.pentaho.core.session.IPentahoSession; 31 import org.pentaho.core.solution.ISolutionEngine; 32 import org.pentaho.core.solution.SimpleOutputHandler; 33 import org.pentaho.core.system.PentahoSystem; 34 import org.pentaho.core.util.XForm; 35 import org.pentaho.core.util.XmlHelper; 36 import org.pentaho.messages.Messages; 37 import org.pentaho.util.logging.ILogger; 38 39 public class FilterDefinition { 40 41 public static final int ITEM_SOURCE_ACTION = 1; 42 43 public static final int ITEM_SOURCE_SESSION = 2; 44 45 private String title; 46 47 private String elementName; 48 49 private IPentahoResultSet resultSet; 50 51 private int type; 52 53 private int itemsSource; 54 55 private String solution = null; 56 57 private String actionPath = null; 58 59 private String actionName = null; 60 61 private String listSource; 62 63 private String descriptionItem; 64 65 private String valueItem; 66 67 private IPentahoSession session; 68 69 private String [] defaultValue; 70 71 private String formName; 72 73 private ILogger logger; 74 75 private int nameColumnNo = -1; 76 77 private int valueColumnNo = -1; 78 79 public FilterDefinition(Element node, IPentahoSession session, String formName, ILogger logger) { 80 81 this.session = session; 82 this.formName = formName; 83 this.logger = logger; 84 85 if (node != null) { 86 fromXml(node); 87 } 88 89 } 90 91 public void setFormName(String formName) { 92 this.formName = formName; 93 } 94 95 public String getTitle() { 96 return title; 97 } 98 99 public String getName() { 100 return elementName; 101 } 102 103 public void fromXml(Element node) { 104 title = XmlHelper.getNodeText("title", node); elementName = XmlHelper.getNodeText("name", node); String sessionAttribute = XmlHelper.getNodeText("session-attribute", node); if (sessionAttribute != null) { 108 itemsSource = ITEM_SOURCE_SESSION; 109 listSource = sessionAttribute; 110 111 } else { 112 itemsSource = ITEM_SOURCE_ACTION; 113 solution = XmlHelper.getNodeText("data-solution", node); actionPath = XmlHelper.getNodeText("data-path", node); actionName = XmlHelper.getNodeText("data-action", node); listSource = XmlHelper.getNodeText("data-output", node); } 118 descriptionItem = XmlHelper.getNodeText("data-display", node); valueItem = XmlHelper.getNodeText("data-value", node); String typeStr = XmlHelper.getNodeText("type", node); if ("radio".equalsIgnoreCase(typeStr)) { type = XForm.TYPE_RADIO; 123 } else if ("list".equalsIgnoreCase(typeStr)) { type = XForm.TYPE_LIST; 125 } else if ("list-multi".equalsIgnoreCase(typeStr)) { type = XForm.TYPE_LIST_MULTI; 127 } else if ("check-multi".equalsIgnoreCase(typeStr)) { type = XForm.TYPE_CHECK_MULTI; 129 } else if ("check-multi-scroll".equalsIgnoreCase(typeStr)) { type = XForm.TYPE_CHECK_MULTI_SCROLL; 131 } else if ("check-multi-scroll-2-column".equalsIgnoreCase(typeStr)) { type = XForm.TYPE_CHECK_MULTI_SCROLL_2_COLUMN; 133 } else if ("check-multi-scroll-3-column".equalsIgnoreCase(typeStr)) { type = XForm.TYPE_CHECK_MULTI_SCROLL_3_COLUMN; 135 } else if ("check-multi-scroll-4-column".equalsIgnoreCase(typeStr)) { type = XForm.TYPE_CHECK_MULTI_SCROLL_4_COLUMN; 137 } else { 138 type = XForm.TYPE_SELECT; 139 } 140 141 } 142 143 public void setDefaultValue(String [] defaultValue) { 144 this.defaultValue = defaultValue; 145 } 146 147 public boolean isValid(String [] values) { 148 if ( values == null ) { 149 return( false ); 150 } 151 152 for ( int i = 0; i < values.length; ++i ) { 153 if ( ! isValid( values[i]) ) { 154 return( false ); 155 } 156 } 157 return( true ); 158 } 159 160 public boolean isValid(String value) { 161 if (value == null) { 164 return false; 165 } 166 if (resultSet == null) { 167 return false; 169 } 170 Object row[] = resultSet.next(); 171 String rowValue; 172 while (row != null) { 173 rowValue = row[valueColumnNo].toString(); 174 175 if (value.equals(rowValue)) { 176 resultSet.close(); 177 return true; 178 } 179 row = resultSet.next(); 180 } 181 resultSet.close(); 184 return false; 185 } 186 187 public boolean isValid(String [] value, Map parameterProviders) { 188 if (resultSet == null && parameterProviders != null) { 189 populate(parameterProviders, value); 190 } 191 return isValid(value); 192 193 } 194 195 public boolean populate(Map parameterProviders, String [] value) { 196 199 defaultValue = value; 200 if (itemsSource == ITEM_SOURCE_SESSION) { 201 resultSet = getSessionData(); 202 } else { 203 resultSet = getActionData(parameterProviders); 204 } 205 if (resultSet != null) { 206 IPentahoMetaData metaData = resultSet.getMetaData(); 208 nameColumnNo = metaData.getColumnIndex(descriptionItem); 209 valueColumnNo = metaData.getColumnIndex(valueItem); 210 } 211 return (resultSet != null); 212 } 213 214 public void getXForm(StringBuffer xformHeader, StringBuffer xformBody) { 215 216 String value; 218 String name; 219 220 HashMap displayNames = new HashMap (); 221 ArrayList items = new ArrayList (); 222 223 226 if (nameColumnNo == -1) { 227 logger.error(Messages.getErrorString("FilterDefinition.ERROR_0001_NAME_COLUMN_MISSING")); } else if (valueColumnNo == -1) { 230 logger.error(Messages.getErrorString("FilterDefinition.ERROR_0002_VALUE_COLUMN_MISSING")); } else { 233 234 } 235 236 Object row[] = resultSet.next(); 237 while (row != null) { 238 value = row[valueColumnNo].toString(); 239 items.add(value); 240 name = row[nameColumnNo].toString(); 241 if (name != null) { 242 displayNames.put(value, name); 243 } 244 245 row = resultSet.next(); 246 } 247 resultSet.close(); 250 251 if (displayNames.size() == 0) { 253 displayNames = null; 254 } 255 XForm.createXFormControl(type, elementName, defaultValue, items, displayNames, formName, xformHeader, xformBody); 256 } 257 258 protected IPentahoResultSet getSessionData() { 259 260 try { 261 IPentahoResultSet data = (IPentahoResultSet) session.getAttribute(listSource); 264 if (data == null) { 265 data = (IPentahoResultSet) PentahoSystem.getGlobalParameters().getParameter(listSource); 266 } 267 return data; 268 } catch (Exception e) { 269 logger.error(Messages.getString("FilterDefinition.ERROR_0003_NOT_IN_SESSION", listSource), e); } 271 return null; 272 273 } 274 275 protected IPentahoResultSet getActionData(Map parameterProviders) { 276 279 282 ISolutionEngine solutionEngine = PentahoSystem.getSolutionEngineInstance(session); 283 solutionEngine.setLoggingLevel(ILogger.DEBUG); 284 solutionEngine.init(session); 285 286 OutputStream outputStream = null; 287 SimpleOutputHandler outputHandler = null; 288 outputHandler = new SimpleOutputHandler(outputStream, false); 289 290 ArrayList messages = new ArrayList (); 291 String processId = this.getClass().getName(); 292 String instanceId = null; 293 294 IRuntimeContext context = null; 295 try { 296 context = solutionEngine.execute(solution, actionPath, actionName, processId, false, true, instanceId, false, parameterProviders, outputHandler, null, null, messages); 297 298 if (listSource != null) { 299 if (context.getOutputNames().contains(listSource)) { 300 IActionParameter output = context.getOutputParameter(listSource); 301 IPentahoResultSet results = output.getValueAsResultSet(); 302 if (results != null) { 303 results = results.memoryCopy(); 304 } 305 return results; 306 } else { 307 return null; 309 } 310 } else { 311 Iterator it = context.getOutputNames().iterator(); 313 while (it.hasNext()) { 314 IActionParameter output = (IActionParameter) it.next(); 315 if (output.getType().equalsIgnoreCase(IActionParameter.TYPE_RESULT_SET)) { 316 IPentahoResultSet results = output.getValueAsResultSet(); 317 if (results != null) { 318 results = results.memoryCopy(); 319 } 320 return results; 321 } 322 } 323 } 324 return null; 325 } finally { 326 if (context != null) 327 context.dispose(); 328 } 329 } 330 331 } 332 | Popular Tags |