001    package net.databinder.models.hib;
002    
003    /*
004     * Databinder: a simple bridge from Wicket to Hibernate
005     * Copyright (C) 2006  Nathan Hamblen nathan@technically.us
006    
007     * This library is free software; you can redistribute it and/or
008     * modify it under the terms of the GNU Lesser General Public
009     * License as published by the Free Software Foundation; either
010     * version 2.1 of the License, or (at your option) any later version.
011     * 
012     * This library is distributed in the hope that it will be useful,
013     * but WITHOUT ANY WARRANTY; without even the implied warranty of
014     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
015     * Lesser General Public License for more details.
016     * 
017     * You should have received a copy of the GNU Lesser General Public
018     * License along with this library; if not, write to the Free Software
019     * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
020     */
021    
022    import org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn;
023    import org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn;
024    import org.apache.wicket.extensions.markup.html.repeater.data.table.filter.IFilterStateLocator;
025    import org.apache.wicket.util.lang.PropertyResolver;
026    import org.hibernate.Criteria;
027    import org.hibernate.criterion.MatchMode;
028    import org.hibernate.criterion.Restrictions;
029    
030    /**
031     * <h1>CriteriaColumnFilter</h1>
032     * <i>Copyright (C) 2008 The Scripps Research Institute</i>
033     * <p>An implementor of CriteriaBuilder and IFilterStateLocator to wire up a HibernateProvider based DataTable with a FilterToolbar<p>
034     * 
035     * <pre>
036     * //...
037     * CriteriaColumnFilter filter = new CriteriaColumnFilter(objectClass.getClass().newInstance(), columns);
038     * FilterForm form = new FilterForm("form", filter);
039     * //...
040     * IDataProvider provider = new DatabinderProvider(objectClass, filter, new DataSorter() );
041     * DataTable table = new DataTable("table", columns, provider, 25);
042     * //...
043     * table.addTopToolbar(new FilterToolbar(table, form, filter));
044     * </pre>
045     * 
046     * @author Mark Southern (southern at scripps dot edu)
047     */
048    public class CriteriaColumnFilter implements CriteriaBuilder, IFilterStateLocator {
049    
050        private IColumn[] columns;
051    
052        private Object bean;
053    
054        public CriteriaColumnFilter(Object bean, IColumn[] columns) {
055            this.bean = bean;
056            this.columns = columns;
057        }
058    
059        public void build(Criteria criteria) {
060            for (IColumn col : columns) {
061                if (col instanceof PropertyColumn) {
062                    PropertyColumn propCol = (PropertyColumn) col;
063                    String property = propCol.getPropertyExpression();
064                    Object value = PropertyResolver.getValue(property, bean);
065                    if (value != null) {
066                        if (value instanceof String)
067                            criteria.add(Restrictions.ilike(property, (String) value, MatchMode.ANYWHERE));
068                        else
069                            criteria.add(Restrictions.eq(property, value));
070                    }
071    
072                }
073            }
074        }
075    
076        public Object getFilterState() {
077            return bean;
078        }
079    
080        public void setFilterState(Object bean) {
081            this.bean = bean;
082        }
083    }