Revision: 18284
Author:   aschrijvers
Date:     2009-05-27 17:30:58 +0200 (Wed, 27 May 2009)
Log Message:
-----------
HSTTWO-384 added support for range queries for String, Long, Double, Boolean 
and Dates, where you can use for the latter java.util.Date or 
java.util.Calendar 

Modified Paths:
--------------
    
ecm/site-toolkit/trunk/api/src/main/java/org/hippoecm/hst/content/beans/query/HstQuery.java
    
ecm/site-toolkit/trunk/applications/site/src/main/java/org/hippoecm/hst/utilities/SimpleSearchExample.java
    
ecm/site-toolkit/trunk/content-beans/src/main/java/org/hippoecm/hst/content/beans/query/HstQueryImpl.java
    
ecm/site-toolkit/trunk/content-beans/src/main/java/org/hippoecm/hst/content/beans/query/filter/FilterImpl.java

Modified: 
ecm/site-toolkit/trunk/api/src/main/java/org/hippoecm/hst/content/beans/query/HstQuery.java
===================================================================
--- 
ecm/site-toolkit/trunk/api/src/main/java/org/hippoecm/hst/content/beans/query/HstQuery.java
 2009-05-27 13:22:23 UTC (rev 18283)
+++ 
ecm/site-toolkit/trunk/api/src/main/java/org/hippoecm/hst/content/beans/query/HstQuery.java
 2009-05-27 15:30:58 UTC (rev 18284)
@@ -17,6 +17,7 @@
 
 import org.hippoecm.hst.content.beans.query.exceptions.QueryException;
 import org.hippoecm.hst.content.beans.query.filter.BaseFilter;
+import org.hippoecm.hst.content.beans.query.filter.Filter;
 
 public interface HstQuery {
 
@@ -25,6 +26,12 @@
     BaseFilter getFilter();
 
     /**
+     * 
+     * @return a new empty Filter
+     */
+    Filter createFilter();
+    
+    /**
      * Sets the limit of search results
      * @param offset
      */

Modified: 
ecm/site-toolkit/trunk/applications/site/src/main/java/org/hippoecm/hst/utilities/SimpleSearchExample.java
===================================================================
--- 
ecm/site-toolkit/trunk/applications/site/src/main/java/org/hippoecm/hst/utilities/SimpleSearchExample.java
  2009-05-27 13:22:23 UTC (rev 18283)
+++ 
ecm/site-toolkit/trunk/applications/site/src/main/java/org/hippoecm/hst/utilities/SimpleSearchExample.java
  2009-05-27 15:30:58 UTC (rev 18284)
@@ -1,18 +1,18 @@
 package org.hippoecm.hst.utilities;
 
+import java.text.ParseException;
+import java.text.SimpleDateFormat;
 import java.util.ArrayList;
+import java.util.Date;
 import java.util.List;
+import java.util.Locale;
 
 import org.hippoecm.hst.beans.NewsPage;
-import org.hippoecm.hst.beans.TextPage;
 import org.hippoecm.hst.component.support.bean.BaseHstComponent;
 import org.hippoecm.hst.content.beans.query.HstQuery;
 import org.hippoecm.hst.content.beans.query.HstQueryResult;
 import org.hippoecm.hst.content.beans.query.exceptions.QueryException;
-import org.hippoecm.hst.content.beans.query.filter.BaseFilter;
 import org.hippoecm.hst.content.beans.query.filter.Filter;
-import org.hippoecm.hst.content.beans.query.filter.FilterImpl;
-import org.hippoecm.hst.content.beans.query.filter.PrimaryNodeTypeFilterImpl;
 import org.hippoecm.hst.content.beans.standard.HippoBean;
 import org.hippoecm.hst.content.beans.standard.HippoBeanIterator;
 import org.hippoecm.hst.core.component.HstComponentException;
@@ -34,8 +34,20 @@
                 hstQuery.addOrderByDescending("testproject:title");
                 hstQuery.addOrderByAscending("testproject:date");
                 
-                Filter filtera = new FilterImpl();
+                Filter filtera = hstQuery.createFilter();
                 filtera.addContains(".",  query);
+  
+                SimpleDateFormat formatter = new 
SimpleDateFormat("dd/MM/yyyy", new Locale("nl"));
+                Date lower = null;
+                Date upper = null;;
+                try {
+                     lower = formatter.parse("22/04/2007");
+                     upper = formatter.parse("22/04/2009");
+                } catch (ParseException e) {
+                    e.printStackTrace();
+                }
+                filtera.addBetween("testproject:date", lower, upper);
+             
                 
                 // example of chaining filters
                 /*

Modified: 
ecm/site-toolkit/trunk/content-beans/src/main/java/org/hippoecm/hst/content/beans/query/HstQueryImpl.java
===================================================================
--- 
ecm/site-toolkit/trunk/content-beans/src/main/java/org/hippoecm/hst/content/beans/query/HstQueryImpl.java
   2009-05-27 13:22:23 UTC (rev 18283)
+++ 
ecm/site-toolkit/trunk/content-beans/src/main/java/org/hippoecm/hst/content/beans/query/HstQueryImpl.java
   2009-05-27 15:30:58 UTC (rev 18284)
@@ -30,6 +30,8 @@
 import org.hippoecm.hst.content.beans.query.exceptions.QueryException;
 import org.hippoecm.hst.content.beans.query.exceptions.ScopeException;
 import org.hippoecm.hst.content.beans.query.filter.BaseFilter;
+import org.hippoecm.hst.content.beans.query.filter.Filter;
+import org.hippoecm.hst.content.beans.query.filter.FilterImpl;
 import org.hippoecm.hst.content.beans.query.filter.HstCtxWhereFilter;
 import org.hippoecm.hst.content.beans.query.filter.NodeTypeFilter;
 import org.hippoecm.hst.core.request.HstRequestContext;
@@ -66,6 +68,13 @@
         orderByList.add("@"+fieldNameAttribute + " descending");
     }
     
+
+    public Filter createFilter() {
+        return new FilterImpl(this.hstRequestContext);
+    }
+
+
+    
     public BaseFilter getFilter() {
         return this.filter;
     }
@@ -160,5 +169,4 @@
         return null;
     }
 
-
 }

Modified: 
ecm/site-toolkit/trunk/content-beans/src/main/java/org/hippoecm/hst/content/beans/query/filter/FilterImpl.java
===================================================================
--- 
ecm/site-toolkit/trunk/content-beans/src/main/java/org/hippoecm/hst/content/beans/query/filter/FilterImpl.java
      2009-05-27 13:22:23 UTC (rev 18283)
+++ 
ecm/site-toolkit/trunk/content-beans/src/main/java/org/hippoecm/hst/content/beans/query/filter/FilterImpl.java
      2009-05-27 15:30:58 UTC (rev 18284)
@@ -1,14 +1,21 @@
 package org.hippoecm.hst.content.beans.query.filter;
 
-import org.hippoecm.hst.content.beans.query.exceptions.FilterException;
+import java.util.ArrayList;
+import java.util.Calendar;
+import java.util.Date;
+import java.util.GregorianCalendar;
+import java.util.List;
 
+import javax.jcr.RepositoryException;
+import javax.jcr.Value;
 
-import java.util.List;
-import java.util.ArrayList;
+import org.hippoecm.hst.content.beans.query.exceptions.FilterException;
+import org.hippoecm.hst.core.request.HstRequestContext;
 
 public class FilterImpl implements Filter{
 
-    StringBuilder jcrExpressionBuilder;
+    private HstRequestContext hstRequestContext; 
+    private StringBuilder jcrExpressionBuilder;
     /**
      * AND and OR filters are evaluated at the end when #getJcrExpression is 
called.
      * This allows us to change those filters even after those are added to 
filter
@@ -17,8 +24,8 @@
      */
     private List<FilterTypeWrapper> childFilters = new 
ArrayList<FilterTypeWrapper>();
     
-    public FilterImpl(){
-        
+    public FilterImpl(HstRequestContext hstRequestContext){
+        this.hstRequestContext = hstRequestContext;
     }
 
     public void addContains(String scope, String fullTextSearch) throws 
FilterException{
@@ -162,15 +169,33 @@
             }
         }
     }
+    
 
-    // TODO support for Long, Int, Boolean, Calendar, etc etc
-    private String getStringValue(String fieldAttributeName, Object value) 
throws FilterException{
-        if(value instanceof String) {
-            return "'" + value + "'";
+
+    public String getStringValue(String fieldAttributeName, Object value) 
throws FilterException{
+        if(value instanceof String || value instanceof Boolean || value 
instanceof Long || value instanceof Double) {
+            return "'" + value.toString() + "'";
+        } else if(value instanceof Calendar){
+            return getCalendarWhereXpath((Calendar)value);
+            
+        } else if(value instanceof Date){
+            Calendar cal = new GregorianCalendar();
+            cal.setTime((Date)value);
+            return getCalendarWhereXpath(cal);
         }
         throw new FilterException("Unsupported Object type 
'"+value.getClass().getName()+"' to query on.");
     }
     
+    public String getCalendarWhereXpath(Calendar value) throws FilterException{
+        try {
+            // TODO : is this a repository roundtrip over rmi? If so, cache 
locally created values?
+            Value val =  
this.hstRequestContext.getSession().getValueFactory().createValue(value);
+            return "xs:dateTime('"+val.getString() + "')";
+         } catch (RepositoryException e) {
+             throw new FilterException("Repository Exception: " , e);
+         }
+    }
+
     /**
      * BaseFilter wrapper so we can distinguish between AND and or filters.
      * 

_______________________________________________
Hippocms-svn mailing list
[email protected]
http://lists.hippo.nl/mailman/listinfo/hippocms-svn

Reply via email to