KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.ArrayList JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import org.dom4j.Document;
25 import org.dom4j.Element;
26 import org.pentaho.core.session.IPentahoSession;
27 import org.pentaho.core.system.PentahoSystem;
28 import org.pentaho.core.ui.IPentahoUrlFactory;
29 import org.pentaho.core.util.XForm;
30 import org.pentaho.core.util.XmlHelper;
31 import org.pentaho.messages.Messages;
32 import org.pentaho.util.logging.ILogger;
33
34 /**
35  * This class stores the defintion of filters that are used by FilterComponent
36  * to generate content for user interfaces.
37  *
38  * This object supports mulitple filters. It stores information about the
39  * sources of the values for each filters. It generates an XForm snippet to
40  * represent the selections.
41  *
42  * @author James Dixon
43  *
44  */

45 public class FilterPanel {
46
47     public static final boolean debug = PentahoSystem.debug;
48     
49     ArrayList JavaDoc filterList;
50
51     String JavaDoc error;
52
53     String JavaDoc name;
54
55     IPentahoUrlFactory urlFactory;
56
57     ILogger logger;
58
59     public FilterPanel(IPentahoSession session, Document document, IPentahoUrlFactory urlFactory, ILogger logger) {
60         this.urlFactory = urlFactory;
61         this.logger = logger;
62         filterList = new ArrayList JavaDoc();
63         if (document != null) {
64             // create the filter definitions
65
List JavaDoc filterNodes = document.selectNodes("filters/filter"); //$NON-NLS-1$
66
name = document.selectSingleNode("filters/name").getText(); //$NON-NLS-1$
67
Iterator JavaDoc filtersIterator = filterNodes.iterator();
68             while (filtersIterator.hasNext()) {
69                 Element filterNode = (Element) filtersIterator.next();
70                 FilterDefinition filterDefinition = new FilterDefinition(filterNode, session, name, logger);
71                 filterList.add(filterDefinition);
72             }
73             if (filterList.size() == 0) {
74                 error = Messages.getString("FilterPanel.ERROR_0001_NO_FILTERS"); //$NON-NLS-1$
75
return;
76             }
77
78         }
79     }
80
81     public List JavaDoc getFilters() {
82         return filterList;
83     }
84
85     public boolean populate(Map JavaDoc parameterProviders, Map JavaDoc defaultValues) {
86         int ok = 0;
87         int fail = 0;
88         if (filterList != null) {
89             Iterator JavaDoc filtersIterator = filterList.iterator();
90             while (filtersIterator.hasNext()) {
91                 FilterDefinition filterDefinition = (FilterDefinition) filtersIterator.next();
92                 if (filterDefinition.populate(parameterProviders, (String JavaDoc[]) defaultValues.get(filterDefinition.getName()))) {
93                     ok++;
94                 } else {
95                     fail++;
96                 }
97             }
98         }
99         return (fail == 0);
100     }
101
102     public Document getXForm() {
103
104         StringBuffer JavaDoc content = new StringBuffer JavaDoc();
105         String JavaDoc actionUrl = urlFactory.getActionUrlBuilder().getUrl();
106
107         content.append("<filters xmlns:xf=\"http://www.w3.org/2002/xforms\"><id></id><title><![CDATA[" + //$NON-NLS-1$
108
Messages.getEncodedString(name) + "]]></title><description></description><help></help>" + //$NON-NLS-1$
109
"<action><![CDATA[" + actionUrl + "]]></action>"); //$NON-NLS-1$ //$NON-NLS-2$
110

111        if (error != null) {
112             content.append("<error>"); //$NON-NLS-1$
113
content.append(error);
114             content.append("</error>"); //$NON-NLS-1$
115
} else if (filterList == null) {
116             content.append("<error>"); //$NON-NLS-1$
117
content.append(Messages.getString("FilterPanel.ERROR_0003_NO_FILTER_VALUES")); //$NON-NLS-1$
118
content.append("</error>"); //$NON-NLS-1$
119
} else {
120             try {
121                 Iterator JavaDoc filtersIterator = filterList.iterator();
122                 StringBuffer JavaDoc xformHeader = new StringBuffer JavaDoc();
123                 String JavaDoc formName = null;
124                 while (filtersIterator.hasNext()) {
125                     FilterDefinition filterDefinition = (FilterDefinition) filtersIterator.next();
126                     String JavaDoc filterName = filterDefinition.getName();
127                     if (formName == null) {
128                         formName = filterName;
129                     }
130                     String JavaDoc title = filterDefinition.getTitle();
131                     StringBuffer JavaDoc xformBody = new StringBuffer JavaDoc();
132 // XForm.createXFormHeader(name, xformHeader);
133
filterDefinition.getXForm(xformHeader, xformBody);
134 // XForm.completeXForm(XForm.OUTPUT_HTML, filterName, new StringBuffer(), xformBody);
135
// content.append( "<filter name=\""+filterName+"\">"
136
// ).append(filterContent).append( "</filter>" );
137
// //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
138

139                     content.append("<filter><name><![CDATA[" + filterName + "]]></name>") //$NON-NLS-1$ //$NON-NLS-2$
140
.append("<title><![CDATA[" + title + "]]></title><control>") //$NON-NLS-1$ //$NON-NLS-2$
141
.append(xformBody).append("</control></filter>"); //$NON-NLS-1$
142
}
143                 XForm.completeXFormHeader( formName, xformHeader );
144                 content.append( xformHeader );
145             } catch (Exception JavaDoc e) {
146                 logger.error(Messages.getErrorString("FilterPanel.ERROR_0004_COULD_NOT_CREATE_CONTENT"), e); //$NON-NLS-1$
147
}
148
149         }
150         content.append("</filters>"); //$NON-NLS-1$
151
if (debug) logger.debug( content.toString() );
152         return XmlHelper.getDocFromString(content.toString());
153     }
154
155 }
156
Popular Tags