KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > pentaho > ui > component > FilterDefinition


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 Aug 31, 2005
14  * @author James Dixon
15  */

16
17 package org.pentaho.ui.component;
18
19 import java.io.OutputStream JavaDoc;
20 import java.util.ArrayList JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
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 JavaDoc title;
46
47     private String JavaDoc elementName;
48
49     private IPentahoResultSet resultSet;
50
51     private int type;
52
53     private int itemsSource;
54
55     private String JavaDoc solution = null;
56
57     private String JavaDoc actionPath = null;
58
59     private String JavaDoc actionName = null;
60
61     private String JavaDoc listSource;
62
63     private String JavaDoc descriptionItem;
64
65     private String JavaDoc valueItem;
66
67     private IPentahoSession session;
68
69     private String JavaDoc[] defaultValue;
70
71     private String JavaDoc 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 JavaDoc 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 JavaDoc formName) {
92         this.formName = formName;
93     }
94
95     public String JavaDoc getTitle() {
96         return title;
97     }
98
99     public String JavaDoc getName() {
100         return elementName;
101     }
102
103     public void fromXml(Element node) {
104         title = XmlHelper.getNodeText("title", node); //$NON-NLS-1$
105
elementName = XmlHelper.getNodeText("name", node); //$NON-NLS-1$
106
String JavaDoc sessionAttribute = XmlHelper.getNodeText("session-attribute", node); //$NON-NLS-1$
107
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); //$NON-NLS-1$
114
actionPath = XmlHelper.getNodeText("data-path", node); //$NON-NLS-1$
115
actionName = XmlHelper.getNodeText("data-action", node); //$NON-NLS-1$
116
listSource = XmlHelper.getNodeText("data-output", node); //$NON-NLS-1$
117
}
118         descriptionItem = XmlHelper.getNodeText("data-display", node); //$NON-NLS-1$
119
valueItem = XmlHelper.getNodeText("data-value", node); //$NON-NLS-1$
120
String JavaDoc typeStr = XmlHelper.getNodeText("type", node); //$NON-NLS-1$
121
if ("radio".equalsIgnoreCase(typeStr)) { //$NON-NLS-1$
122
type = XForm.TYPE_RADIO;
123         } else if ("list".equalsIgnoreCase(typeStr)) { //$NON-NLS-1$
124
type = XForm.TYPE_LIST;
125         } else if ("list-multi".equalsIgnoreCase(typeStr)) { //$NON-NLS-1$
126
type = XForm.TYPE_LIST_MULTI;
127         } else if ("check-multi".equalsIgnoreCase(typeStr)) { //$NON-NLS-1$
128
type = XForm.TYPE_CHECK_MULTI;
129         } else if ("check-multi-scroll".equalsIgnoreCase(typeStr)) { //$NON-NLS-1$
130
type = XForm.TYPE_CHECK_MULTI_SCROLL;
131         } else if ("check-multi-scroll-2-column".equalsIgnoreCase(typeStr)) { //$NON-NLS-1$
132
type = XForm.TYPE_CHECK_MULTI_SCROLL_2_COLUMN;
133         } else if ("check-multi-scroll-3-column".equalsIgnoreCase(typeStr)) { //$NON-NLS-1$
134
type = XForm.TYPE_CHECK_MULTI_SCROLL_3_COLUMN;
135         } else if ("check-multi-scroll-4-column".equalsIgnoreCase(typeStr)) { //$NON-NLS-1$
136
type = XForm.TYPE_CHECK_MULTI_SCROLL_4_COLUMN;
137         } else {
138             type = XForm.TYPE_SELECT;
139         }
140
141     }
142
143     public void setDefaultValue(String JavaDoc[] defaultValue) {
144         this.defaultValue = defaultValue;
145     }
146
147     public boolean isValid(String JavaDoc[] 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 JavaDoc value) {
161         // this assume that the list of valid values does not have to be
162
// populated
163
if (value == null) {
164             return false;
165         }
166         if (resultSet == null) {
167             // we cannot determin the values for this filter
168
return false;
169         }
170         Object JavaDoc row[] = resultSet.next();
171         String JavaDoc 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         // close the result set so we can loop through it again later if we need
182
// to
183
resultSet.close();
184         return false;
185     }
186
187     public boolean isValid(String JavaDoc[] value, Map JavaDoc parameterProviders) {
188         if (resultSet == null && parameterProviders != null) {
189             populate(parameterProviders, value);
190         }
191         return isValid(value);
192
193     }
194
195     public boolean populate(Map JavaDoc parameterProviders, String JavaDoc[] value) {
196         // TODO apply session-based security
197
// TODO support static lists of values
198

199         defaultValue = value;
200         if (itemsSource == ITEM_SOURCE_SESSION) {
201             resultSet = getSessionData();
202         } else {
203             resultSet = getActionData(parameterProviders);
204         }
205         if (resultSet != null) {
206             // find the column that we have been told to you
207
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 JavaDoc xformHeader, StringBuffer JavaDoc xformBody) {
215
216         // iterate thru the values to get the items and the display names
217
String JavaDoc value;
218         String JavaDoc name;
219
220         HashMap JavaDoc displayNames = new HashMap JavaDoc();
221         ArrayList JavaDoc items = new ArrayList JavaDoc();
222
223         // TODO support multiple column headers / row headers
224
// TODO support an iteration across columns for a given row
225

226         if (nameColumnNo == -1) {
227             // we did not find the specified name column
228
logger.error(Messages.getErrorString("FilterDefinition.ERROR_0001_NAME_COLUMN_MISSING")); //$NON-NLS-1$
229
} else if (valueColumnNo == -1) {
230             // we did not find the specified name column
231
logger.error(Messages.getErrorString("FilterDefinition.ERROR_0002_VALUE_COLUMN_MISSING")); //$NON-NLS-1$
232
} else {
233
234         }
235
236         Object JavaDoc 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         // close the result set so we can loop through it again later if we need
248
// to
249
resultSet.close();
250
251         // now create the XForm for the item
252
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             // TODO determine the source of the list from the definition
262
// document instead of trying both
263
IPentahoResultSet data = (IPentahoResultSet) session.getAttribute(listSource);
264             if (data == null) {
265                 data = (IPentahoResultSet) PentahoSystem.getGlobalParameters().getParameter(listSource);
266             }
267             return data;
268         } catch (Exception JavaDoc e) {
269             logger.error(Messages.getString("FilterDefinition.ERROR_0003_NOT_IN_SESSION", listSource), e); //$NON-NLS-1$
270
}
271         return null;
272
273     }
274
275     protected IPentahoResultSet getActionData(Map JavaDoc parameterProviders) {
276         // create an instance of the solution engine to execute the specified
277
// action
278

279         // TODO we need to ensure that this data is cached (not live) so that we
280
// can validate selections
281

282         ISolutionEngine solutionEngine = PentahoSystem.getSolutionEngineInstance(session);
283         solutionEngine.setLoggingLevel(ILogger.DEBUG);
284         solutionEngine.init(session);
285
286         OutputStream JavaDoc outputStream = null;
287         SimpleOutputHandler outputHandler = null;
288         outputHandler = new SimpleOutputHandler(outputStream, false);
289
290         ArrayList JavaDoc messages = new ArrayList JavaDoc();
291         String JavaDoc processId = this.getClass().getName();
292         String JavaDoc 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                     // this is an error
308
return null;
309                 }
310             } else {
311                 // return the first list that we find...
312
Iterator JavaDoc 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