1 16 17 package org.pentaho.plugin.core; 18 19 import java.util.ArrayList ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 23 import org.apache.commons.logging.Log; 24 import org.apache.commons.logging.LogFactory; 25 import org.dom4j.Node; 26 import org.pentaho.core.runtime.IActionParameter; 27 import org.pentaho.core.runtime.SelectionMapper; 28 import org.pentaho.core.solution.IOutputHandler; 29 import org.pentaho.core.util.XmlHelper; 30 import org.pentaho.messages.Messages; 31 import org.pentaho.plugin.ComponentBase; 32 33 public class SecureFilterComponent extends ComponentBase { 34 35 private static final long serialVersionUID = 7119516440509549539L; 36 37 List selList = new ArrayList (); 39 List hiddenList = new ArrayList (); 41 43 public Log getLogger() { 44 return LogFactory.getLog(SecureFilterComponent.class); 45 } 46 47 protected boolean validateSystemSettings() { 48 return true; 50 } 51 52 public boolean validateAction() { 53 Node compDef = getComponentDefinition(); 54 List selNodes = compDef.selectNodes("selections/*"); 56 String inputName = null; 57 boolean isOk = true; 58 59 for (Iterator it = selNodes.iterator(); it.hasNext();) { 60 Node node = (Node) it.next(); 61 try { 62 inputName = node.getName(); IActionParameter inputParam = getInputParameter(inputName); 64 String filterType = XmlHelper.getNodeText("@filter", node, null); String optionalParm = XmlHelper.getNodeText("@optional", node, null); if ("none".equalsIgnoreCase(filterType)) { IActionParameter selectParam = getInputParameter(inputName); 68 String title = XmlHelper.getNodeText("title", node, inputName); String valueCol = ""; String dispCol = ""; String displayStyle = XmlHelper.getNodeText("@style", node, null); boolean promptOne = "true".equalsIgnoreCase(XmlHelper.getNodeText("@prompt-if-one-value", node, "false")); if ("hidden".equals(displayStyle)) { hiddenList.add(new SelEntry(inputParam, selectParam, valueCol, dispCol, title, displayStyle, promptOne, true)); 75 } else { 76 selList.add(new SelEntry(inputParam, selectParam, valueCol, dispCol, title, displayStyle, promptOne, true)); 77 } 78 } else { 79 boolean isOptional = "true".equals(optionalParm); Node filterNode = node.selectSingleNode("filter"); IActionParameter selectParam = getInputParameter(filterNode.getText().trim()); 82 83 String valueCol = XmlHelper.getNodeText("@value-col-name", filterNode, null); String dispCol = XmlHelper.getNodeText("@display-col-name", filterNode, null); 86 String title = XmlHelper.getNodeText("title", node, null); String displayStyle = XmlHelper.getNodeText("@style", node, null); boolean promptOne = "true".equalsIgnoreCase(XmlHelper.getNodeText("@prompt-if-one-value", node, "false")); 90 selList.add(new SelEntry(inputParam, selectParam, valueCol, dispCol, title, displayStyle, promptOne, isOptional)); 91 } 92 93 } catch (Exception e) { isOk = false; 96 error(Messages.getErrorString("SecureFilterComponent.ERROR_0001_PARAM_MISSING", inputName)); } 98 } 99 100 return (isOk); 101 } 102 103 public boolean executeAction() { 104 105 boolean parameterUINeeded = false; 106 if (getOutputPreference() == IOutputHandler.OUTPUT_TYPE_PARAMETERS) { 107 parameterUINeeded = true; 108 } 109 110 boolean stopHere = getInputBooleanValue(StandardSettings.HANDLE_ALL_PROMPTS, true); 111 112 SelEntry entry; 113 boolean isOk = true; 114 boolean causingPrompting = false; for (Iterator it = selList.iterator(); it.hasNext();) { 118 entry = (SelEntry) it.next(); 119 120 SelectionMapper selMap = SelectionMapper.create(entry.selectionParam, entry.valueCol, entry.dispCol, entry.title, entry.displayStyle); 121 122 if (entry.inputParam.hasValue() && !parameterUINeeded) { 125 Object value = entry.inputParam.getValue(); 127 if (value instanceof String ) { 128 if (selMap == null) { 129 } else if (!selMap.hasValue((String ) value)) { 132 if ((entry.isOptional) && ("".equals(value))) { } else { 135 error(Messages.getErrorString("SecureFilterComponent.ERROR_0001_INVALID_SELECTION", entry.inputParam.getValue().toString(), entry.inputParam.getName())); isOk = false; 137 } 138 } 139 } else if (value instanceof Object []) { 140 if (selMap == null) { 142 } else { 145 Object values[] = (Object []) value; 146 for (int i = 0; i < values.length; i++) { 147 if (!selMap.hasValue(values[i].toString())) { 148 if ((entry.isOptional) && ("".equals(values[i].toString()))) { } else { 151 error(Messages.getErrorString("SecureFilterComponent.ERROR_0001_INVALID_SELECTION", entry.inputParam.getValue().toString(), entry.inputParam.getName())); isOk = false; 153 } 154 } 155 } 156 } 157 } else { 158 error(Messages.getErrorString("SecureFilterComponent.ERROR_0001_INVALID_SELECTION", entry.inputParam.getValue().toString(), entry.inputParam.getName())); isOk = false; 161 } 162 } else { if (selMap == null) { 164 createFeedbackParameter(entry.inputParam.getName(), entry.title, "", entry.inputParam.getValue().toString(), true); promptNeeded(); 167 causingPrompting = true; if (stopHere) { 171 promptNow(); 172 } 173 entry.inputParam.setPromptStatus(IActionParameter.PROMPT_PENDING); 174 } else if (!entry.promptOne && (selMap.selectionCount() == 1)) { 175 entry.inputParam.setValue(selMap.getValueAt(0)); 176 } else if (!feedbackAllowed()) { 177 isOk = false; 178 } else { 179 createFeedbackParameter(selMap, entry.inputParam.getName(), entry.inputParam.getValue()); 180 promptNeeded(); 181 causingPrompting = true; if (stopHere) { 185 promptNow(); 186 } 187 entry.inputParam.setPromptStatus(IActionParameter.PROMPT_PENDING); 188 } 189 } 190 } if (causingPrompting) { 192 for (int i = 0; i < hiddenList.size(); i++) { 197 entry = (SelEntry) hiddenList.get(i); 198 Object value = entry.inputParam.getValue(); 199 if (value instanceof String ) { 200 createFeedbackParameter(entry.inputParam.getName(), entry.inputParam.getName(), "", (String ) value, false); } else { 202 createFeedbackParameter(entry.inputParam.getName(), entry.inputParam.getName(), "", value.toString(), false); } 206 } 207 } 208 return isOk; 209 210 } 211 212 217 public void done() { 218 220 } 221 222 227 public boolean init() { 228 return true; 230 } 231 232 class SelEntry { 233 IActionParameter inputParam; 234 235 IActionParameter selectionParam; 236 237 String valueCol, dispCol, title, displayStyle; 238 239 boolean promptOne; 240 241 boolean isOptional; 242 243 SelEntry(IActionParameter inputParam, IActionParameter selectionParam, String valueCol, String dispCol, String title, String displayStyle, boolean promptOne, boolean optional) { 244 this.inputParam = inputParam; 245 this.selectionParam = selectionParam; 246 this.valueCol = valueCol; 247 this.dispCol = dispCol; 248 this.title = title; 249 this.displayStyle = displayStyle; 250 this.promptOne = promptOne; 251 this.isOptional = optional; 252 } 253 254 } 255 256 } 257 | Popular Tags |