KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > plugin > core > SecureFilterComponent


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.plugin.core;
18
19 import java.util.ArrayList JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
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 JavaDoc selList = new ArrayList JavaDoc(); // list of valid selections
38

39     List JavaDoc hiddenList = new ArrayList JavaDoc(); // List of hidden fields that will be
40

41     // added if any prompting happens.
42

43     public Log getLogger() {
44         return LogFactory.getLog(SecureFilterComponent.class);
45     }
46
47     protected boolean validateSystemSettings() {
48         // No System Settings to validate
49
return true;
50     }
51
52     public boolean validateAction() {
53         Node compDef = getComponentDefinition();
54         List JavaDoc selNodes = compDef.selectNodes("selections/*"); //$NON-NLS-1$
55

56         String JavaDoc inputName = null;
57         boolean isOk = true;
58
59         for (Iterator JavaDoc it = selNodes.iterator(); it.hasNext();) {
60             Node node = (Node) it.next();
61             try {
62                 inputName = node.getName(); // Get the Data Node
63
IActionParameter inputParam = getInputParameter(inputName);
64                 String JavaDoc filterType = XmlHelper.getNodeText("@filter", node, null); //$NON-NLS-1$
65
String JavaDoc optionalParm = XmlHelper.getNodeText("@optional", node, null); //$NON-NLS-1$
66
if ("none".equalsIgnoreCase(filterType)) { //$NON-NLS-1$
67
IActionParameter selectParam = getInputParameter(inputName);
68                     String JavaDoc title = XmlHelper.getNodeText("title", node, inputName); //$NON-NLS-1$
69
String JavaDoc valueCol = ""; //$NON-NLS-1$
70
String JavaDoc dispCol = ""; //$NON-NLS-1$
71
String JavaDoc displayStyle = XmlHelper.getNodeText("@style", node, null); //$NON-NLS-1$
72
boolean promptOne = "true".equalsIgnoreCase(XmlHelper.getNodeText("@prompt-if-one-value", node, "false")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
73
if ("hidden".equals(displayStyle)) { //$NON-NLS-1$
74
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); //$NON-NLS-1$
80
Node filterNode = node.selectSingleNode("filter"); //$NON-NLS-1$
81
IActionParameter selectParam = getInputParameter(filterNode.getText().trim());
82
83                     String JavaDoc valueCol = XmlHelper.getNodeText("@value-col-name", filterNode, null); //$NON-NLS-1$
84
String JavaDoc dispCol = XmlHelper.getNodeText("@display-col-name", filterNode, null); //$NON-NLS-1$
85

86                     String JavaDoc title = XmlHelper.getNodeText("title", node, null); //$NON-NLS-1$
87
String JavaDoc displayStyle = XmlHelper.getNodeText("@style", node, null); //$NON-NLS-1$
88
boolean promptOne = "true".equalsIgnoreCase(XmlHelper.getNodeText("@prompt-if-one-value", node, "false")); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
89

90                     selList.add(new SelEntry(inputParam, selectParam, valueCol, dispCol, title, displayStyle, promptOne, isOptional));
91                 }
92
93             } catch (Exception JavaDoc e) { // Catch the exception to let us test all
94
// the params
95
isOk = false;
96                 error(Messages.getErrorString("SecureFilterComponent.ERROR_0001_PARAM_MISSING", inputName)); //$NON-NLS-1$
97
}
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; // Set to true if this securefilter
115
// component actually adds feeback
116
// parameters.
117
for (Iterator JavaDoc 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 we have a value for the input param, verify that it is within
123
// the selections
124
if (entry.inputParam.hasValue() && !parameterUINeeded) {
125                 // TODO support numeric values here
126
Object JavaDoc value = entry.inputParam.getValue();
127                 if (value instanceof String JavaDoc) {
128                     if (selMap == null) {
129                         // this should be ok, we are just checking for its
130
// existance
131
} else if (!selMap.hasValue((String JavaDoc) value)) {
132                         if ((entry.isOptional) && ("".equals(value))) { //$NON-NLS-1$
133
// This is OK
134
} else {
135                             error(Messages.getErrorString("SecureFilterComponent.ERROR_0001_INVALID_SELECTION", entry.inputParam.getValue().toString(), entry.inputParam.getName())); //$NON-NLS-1$
136
isOk = false;
137                         }
138                     }
139                 } else if (value instanceof Object JavaDoc[]) {
140                     // test each item
141
if (selMap == null) {
142                         // this should be ok, we are just checking for its
143
// existance
144
} else {
145                         Object JavaDoc values[] = (Object JavaDoc[]) 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()))) { //$NON-NLS-1$
149
// This is OK
150
} else {
151                                     error(Messages.getErrorString("SecureFilterComponent.ERROR_0001_INVALID_SELECTION", entry.inputParam.getValue().toString(), entry.inputParam.getName())); //$NON-NLS-1$
152
isOk = false;
153                                 }
154                             }
155                         }
156                     }
157                 } else {
158                     // we cannot validate this
159
error(Messages.getErrorString("SecureFilterComponent.ERROR_0001_INVALID_SELECTION", entry.inputParam.getValue().toString(), entry.inputParam.getName())); //$NON-NLS-1$
160
isOk = false;
161                 }
162             } else { // Need to prompt
163
if (selMap == null) {
164                     // TODO support help/hints
165
createFeedbackParameter(entry.inputParam.getName(), entry.title, "", entry.inputParam.getValue().toString(), true); //$NON-NLS-1$
166
promptNeeded();
167                     causingPrompting = true; // If we add a feedback
168
// parameter, then we're causing
169
// prompting
170
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 we add a feedback
182
// parameter, then we're causing
183
// prompting
184
if (stopHere) {
185                         promptNow();
186                     }
187                     entry.inputParam.setPromptStatus(IActionParameter.PROMPT_PENDING);
188                 }
189             }
190         } // Done with the regular selections
191
if (causingPrompting) {
192             // We want the hidden fields to be processed after everything else
193
// is processed because
194
// we only want to add the hidden fields if this component has
195
// caused prompting to occur.
196
for (int i = 0; i < hiddenList.size(); i++) {
197                 entry = (SelEntry) hiddenList.get(i);
198                 Object JavaDoc value = entry.inputParam.getValue();
199                 if (value instanceof String JavaDoc) {
200                     createFeedbackParameter(entry.inputParam.getName(), entry.inputParam.getName(), "", (String JavaDoc) value, false); //$NON-NLS-1$
201
} else {
202                     // Support other types of hidden field parameters (like
203
// Integer/Decimal/Etc.) by using toString.
204
createFeedbackParameter(entry.inputParam.getName(), entry.inputParam.getName(), "", value.toString(), false); //$NON-NLS-1$
205
}
206             }
207         }
208         return isOk;
209
210     }
211
212     /*
213      * (non-Javadoc)
214      *
215      * @see org.pentaho.component.ComponentBase#done()
216      */

217     public void done() {
218         // TODO Auto-generated method stub
219

220     }
221
222     /*
223      * (non-Javadoc)
224      *
225      * @see org.pentaho.component.ComponentBase#init()
226      */

227     public boolean init() {
228         // TODO Auto-generated method stub
229
return true;
230     }
231
232     class SelEntry {
233         IActionParameter inputParam;
234
235         IActionParameter selectionParam;
236
237         String JavaDoc valueCol, dispCol, title, displayStyle;
238
239         boolean promptOne;
240
241         boolean isOptional;
242
243         SelEntry(IActionParameter inputParam, IActionParameter selectionParam, String JavaDoc valueCol, String JavaDoc dispCol, String JavaDoc title, String JavaDoc 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