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.Iterator;
19  
20  import org.apache.commons.beanutils.PropertyUtils;
21  import org.apache.commons.collections.Predicate;
22  import org.apache.commons.lang.StringUtils;
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  import org.extremecomponents.table.bean.Column;
26  import org.extremecomponents.util.ExtremeUtils;
27  
28  
29  
30  /***
31   * Use the Jakarta Collections predicate pattern to filter out the table.
32   *
33   * @author Jeff Johnston
34   */
35  public class FilterPredicate implements Predicate
36  {
37  	private static Log logger = LogFactory.getLog(FilterPredicate.class);
38  	private TableModel model;
39  
40  	public FilterPredicate(TableModel model)
41  	{
42  		this.model = model;
43  	}
44  
45  	/***
46  	 * Use the filter parameters to filter out the table.
47  	 */
48  	public boolean evaluate(Object bean)
49  	{
50  		boolean match = false;
51  
52  		try
53  		{
54  			for (Iterator iter = model.getColumnMetaData().getColumns(); iter.hasNext();)
55  			{
56  				Column column = (Column) iter.next();
57  				String property = column.getProperty();
58  				String filterValue = model.getRegistry().getParameter(ParameterRegistry.FILTER + property);
59  
60  				if (StringUtils.isEmpty(filterValue)) //only check what filtering on
61  				{
62  					continue;
63  				}
64  
65  				Object obj = PropertyUtils.getProperty(bean, property);
66  				
67  				if (obj == null)
68  				{
69  					continue;
70  				}
71  
72  				String value = obj.toString();			
73  
74  				if (column.isDate())
75  				{
76  					value = ExtremeUtils.formatDate(column.getParse(), column.getFormat(), value);
77  				}
78  				else if (column.isNumber())
79  				{
80  					value = ExtremeUtils.formatNumber(column.getFormat(), value);
81  				}
82  
83  				if (!StringUtils.contains(value.toLowerCase(), filterValue.toLowerCase())) //this is where the fun starts :)
84  				{
85  					match = false; //as soon as fail just short circuit
86  
87  					break;
88  				}
89  				else
90  				{
91  					match = true;
92  				}
93  			}
94  		}
95  		catch (Exception e)
96  		{
97  			logger.error("FilterPredicate.evaluate() had problems", e);
98  		}
99  
100 		return match;
101 	}
102 }