/*---
   Copyright 2007 The Scripps Research Institute
   http://www.scripps.edu

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at
   
        http://www.apache.org/licenses/LICENSE-2.0
   
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
---*/

package wicket.contrib.webbeans.databinder;

import java.io.Serializable;
import java.util.Set;
import java.util.HashSet;

import net.databinder.components.hibernate.SearchPanel;
import net.databinder.models.ICriteriaBuilder;

import org.hibernate.Criteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;

/**
 * A Databinder/Hibernate filtering component suitable for adding to a BeanTablePanel in conjunction
 * with a DatabinderProvider. It uses a Databinder Search panel to obtain the search criterion.
 * A typical use case might be:
 * 
 * SearchPanel search = new SearchPanel("search", new Model(null))
 * {
 *      public void onUpdate(AjaxRequestTarget target) {
 *          target.addComponent(getTableBeanPanel());
 *      }
 * };
 * add(search);
 * DataSorter sorter = new DataSorter(metaData.getParameter("orderBy"));
 * IDataProvider provider = new DatabinderProvider(beanClass,
 *                                                  new DataSearchFilter(search,metaData.getParameter("filter")),
 *                                                  sorter);
 * BeanTablePanel panel = new BeanTablePanel("beanTable", provider, sorter, metaData, true, 20);
 * 
 * @author Mark Southern (mrsouthern)
 */
public class DataSearchFilter implements ICriteriaBuilder, Serializable
{
    private SearchPanel searchPanel;
    private String[] properties = null;
    private Set<String> aliases = new HashSet();
    
    /**
     * Construct a new DataSearchFilter.
     *
     * @param searchPanel the Databinder SearchPanel where the user types the search criteria
     * @param property the bean field on which to perform the search
     */
    public DataSearchFilter(SearchPanel searchPanel, String[] properties)
    {
        this.searchPanel = searchPanel;
        setProperties(properties);
    }
    
    public void setProperties(String[] properties)
    {
        this.properties = properties;
        aliases = new HashSet<String>();
        for(String property: properties)
        {
            if( property.contains(".") ) // i.e. property from another bean
            {
                    String[] path = property.split("\\.");
                    for(int ii = 0; ii < path.length - 1; ii++)
                        aliases.add(path[ii]);
            }
        }
    }
    
    public void build(Criteria criteria)
    {
        if( searchPanel.getModelObject() != null && properties != null )
        {
            for(String alias: aliases)
                criteria.createAlias(alias, alias);
            for(String property: properties)
                criteria.add(Restrictions.ilike(property, 
                                                searchPanel.getModelObject().toString(),
                                                MatchMode.ANYWHERE));
        }
    }
}
@@ -2693,24 +2693,73 @@
 String%3E();%0D%0A
+        if( properties != null )%0D%0A        %7B%0D%0A    
         for(
@@ -2788,35 +2788,43 @@
 rties)%0D%0A        
-%7B%0D%0A
+    %7B%0D%0A    
             if( 
@@ -2888,35 +2888,43 @@
 an%0D%0A            
+ 
-%7B%0D%0A
+   %7B%0D%0A    
                 
@@ -2963,24 +2963,28 @@
 it(%22%5C%5C.%22);%0D%0A
+    
             
@@ -3056,24 +3056,28 @@
             
+    
 aliases.add(
@@ -3084,24 +3084,43 @@
 path%5Bii%5D);%0D%0A
+                %7D%0D%0A
             

check for null property array in setProperties