1
2
3
4
5
6
7
8
9
10
11
12
13
14
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))
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()))
84 {
85 match = false;
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 }