View Javadoc

1   /*
2    * Copyright 2004 Jeff Johnston
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.extremecomponents.table.model;
17  
18  import java.util.ArrayList;
19  import java.util.HashMap;
20  import java.util.Iterator;
21  import java.util.List;
22  import java.util.Map;
23  
24  import org.apache.commons.beanutils.PropertyUtils;
25  import org.apache.commons.collections.CollectionUtils;
26  import org.apache.commons.lang.StringUtils;
27  import org.apache.commons.logging.Log;
28  import org.apache.commons.logging.LogFactory;
29  import org.extremecomponents.table.bean.Column;
30  import org.extremecomponents.table.cell.Cell;
31  import org.extremecomponents.table.cell.FilterCell;
32  
33  
34  
35  /***
36   * Deal with the filtering.
37   *
38   * @author Jeff Johnston
39   */
40  public class FilterHandler
41  {
42  	private static Log logger = LogFactory.getLog(FilterHandler.class);
43  	public final static String DO_CLEAR = "DoClear";
44  	public final static String DO_SEARCH = "DoSearch";
45  	public final static String BUTTON = "button";
46  	public final static String SEARCH_IMAGE = "search";
47  	public final static String CLEAR_IMAGE = "clear";
48  	private String button;
49  	
50  	private TableModel model;
51  	
52  	public FilterHandler(TableModel model)
53  	{
54  		this.model = model;
55  	}
56  
57  	public boolean doFilter()
58  	{
59  		Map parameters = model.getRegistry().getParameters(ParameterRegistry.FILTER) ;
60  		return parameters.size() > 0;
61  	}
62  	
63  	public boolean doClear()
64  	{
65  		if (!StringUtils.isEmpty(getButton()) && getButton().equals(DO_CLEAR)) return true;
66  		return false;
67  	}
68  	
69  	public String getButton()
70  	{
71  		if (button == null) button = model.getRegistry().getParameter(ParameterRegistry.FILTER + BUTTON);
72  		return button;
73  	}
74  	
75  	/***
76  	 * Filter out the table using the Predicate pattern.
77  	 */
78  	public List filterRows(TableModel model, List rows)
79  	{
80  		//we pressed the clear button
81  		if (doClear())
82  		{
83  			model.getRegistry().clearParameters(ParameterRegistry.FILTER);
84  			return rows;
85  		}
86  
87  		if (!doFilter())
88  		{
89  			return rows;
90  		}
91  		else
92  		{
93  			List results = new ArrayList();
94  			FilterPredicate filterPredicate = new FilterPredicate(model);
95  			CollectionUtils.select(rows, filterPredicate, results);
96  
97  			return results;
98  		}
99  	}
100 	
101 	/***
102 	 * Get the Filter
103 	 */
104 	public List getFilter(TableModel model)
105 	{
106 		List filter = new ArrayList();
107 
108 		if (!model.getTable().doFilter()) //if the filter is not turned on
109 		{
110 			return filter;
111 		}
112 
113 		Map droplists = getFilterDropLists(model);
114 
115 		Iterator columns = model.getColumnMetaData().getColumns();
116 
117 		for (Iterator iter = columns; iter.hasNext();)
118 		{
119 			Column column = (Column) iter.next();
120 			String value = model.getRegistry().getParameter(ParameterRegistry.FILTER + column.getProperty());
121 
122 			if ((!StringUtils.isEmpty(button) && button.equals(DO_CLEAR)) || StringUtils.isEmpty(value)) //pressed a button and its to clear value
123 			{
124 				value = "";
125 			}
126 
127 			column.setValue(value);
128 
129 			Object obj = droplists.get(column.getProperty());
130 
131 			if (obj != null)
132 			{
133 				column.setFilterDroplist((List) obj);
134 			}
135 			
136 			Cell cell = model.getCachedCell(FilterCell.class);
137 			cell.init(model, column, null);
138 			filter.add(cell.html());
139 			cell.destroy();
140 		}
141 
142 		return filter;
143 	}
144 
145 	/***
146 	 * Make up the droplists that will be used for the filter. The droplists will be a distinct
147 	 * set of the data in a column.
148 	 * This could be very painful if the list is long so be careful about using for larger sets.
149 	 * Trying to make this faster by running through bean only once.
150 	 */
151 	public Map getFilterDropLists(TableModel model)
152 	{
153 		Map droplists = new HashMap();
154 
155 		Iterator columns = model.getColumnMetaData().getColumns(); //make up the distinct list of droplists
156 
157 		while (columns.hasNext())
158 		{
159 			Column column = (Column) columns.next();
160 
161 			if (column.getFilter().equals("droplist"))
162 			{
163 				List values = new ArrayList();
164 				droplists.put(column.getProperty(), values);
165 			}
166 		}
167 
168 		if (droplists.isEmpty()) //do not need any droplists, so stop right here
169 		{
170 			return droplists;
171 		}
172 
173 		List list = model.getTableCollection(); //iterate though the beans
174 
175 		for (Iterator iter = list.iterator(); iter.hasNext();)
176 		{
177 			Object bean = (Object) iter.next();
178 
179 			Iterator keys = droplists.keySet().iterator();
180 
181 			while (keys.hasNext())
182 			{
183 				String property = (String) keys.next();
184 				List values = (List) droplists.get(property);
185 
186 				try
187 				{
188 					Object obj = PropertyUtils.getProperty(bean, property);
189 
190 					if ((obj != null) && !values.contains(obj))
191 					{
192 						values.add(obj);
193 					}
194 				}
195 				catch (Exception e)
196 				{
197 					logger.debug("FilterHandler.getFilterDropLists()", e);
198 				}
199 			}
200 		}
201 
202 		return droplists;
203 	}
204 	
205 	public void destroy()
206 	{
207 		button = null;
208 	}	
209 }