svn commit: r981008 - /ofbiz/trunk/framework/entity/src/org/ofbiz/entity/condition/EntityDateFilterCondition.java

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r981008 - /ofbiz/trunk/framework/entity/src/org/ofbiz/entity/condition/EntityDateFilterCondition.java

lektran
Author: lektran
Date: Sat Jul 31 05:31:17 2010
New Revision: 981008

URL: http://svn.apache.org/viewvc?rev=981008&view=rev
Log:
Add a range condition to EntityDateFilterCondition, useful for finding entities applicable to a given date range.

Modified:
    ofbiz/trunk/framework/entity/src/org/ofbiz/entity/condition/EntityDateFilterCondition.java

Modified: ofbiz/trunk/framework/entity/src/org/ofbiz/entity/condition/EntityDateFilterCondition.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/entity/src/org/ofbiz/entity/condition/EntityDateFilterCondition.java?rev=981008&r1=981007&r2=981008&view=diff
==============================================================================
--- ofbiz/trunk/framework/entity/src/org/ofbiz/entity/condition/EntityDateFilterCondition.java (original)
+++ ofbiz/trunk/framework/entity/src/org/ofbiz/entity/condition/EntityDateFilterCondition.java Sat Jul 31 05:31:17 2010
@@ -23,6 +23,7 @@ import java.util.List;
 import java.util.Map;
 
 import javolution.context.ObjectFactory;
+import javolution.util.FastList;
 
 import org.ofbiz.base.util.UtilDateTime;
 import org.ofbiz.entity.Delegator;
@@ -133,4 +134,65 @@ public class EntityDateFilterCondition e
            )
       );
     }
+
+    /**
+     * Creates an EntityCondition representing a date range filter query to be used against
+     * entities that themselves represent a date range.  When used the resulting entities
+     * will meet at least one of the following criteria:
+     * - fromDate is equal to or after rangeStart but before rangeEnd
+     * - thruDate is equal to or after rangeStart but before rangeEnd
+     * - fromDate is null and thruDate is equal to or after rangeStart
+     * - thruDate is null and fromDate is before rangeEnd
+     * - fromDate is null and thruDate is null
+     * @param rangeStart
+     * @param rangeEnd
+     * @param fromDateName
+     * @param thruDateName
+     * @return
+     */
+    public static EntityCondition makeRangeCondition(Timestamp rangeStart, Timestamp rangeEnd, String fromDateName, String thruDateName) {
+        List<EntityCondition> criteria = FastList.newInstance();
+        // fromDate is equal to or after rangeStart but before rangeEnd
+        criteria.add(
+                EntityCondition.makeCondition(
+                        EntityCondition.makeCondition(fromDateName, EntityOperator.GREATER_THAN_EQUAL_TO, rangeStart),
+                        EntityOperator.AND,
+                        EntityCondition.makeCondition(fromDateName, EntityOperator.LESS_THAN, rangeEnd)
+                )
+        );
+        // thruDate is equal to or after rangeStart but before rangeEnd
+        criteria.add(
+                EntityCondition.makeCondition(
+                        EntityCondition.makeCondition(thruDateName, EntityOperator.GREATER_THAN_EQUAL_TO, rangeStart),
+                        EntityOperator.AND,
+                        EntityCondition.makeCondition(thruDateName, EntityOperator.LESS_THAN, rangeEnd)
+                )
+        );
+        // fromDate is null and thruDate is equal to or after rangeStart
+        criteria.add(
+                EntityCondition.makeCondition(
+                        EntityCondition.makeCondition(fromDateName, EntityOperator.EQUALS, null),
+                        EntityOperator.AND,
+                        EntityCondition.makeCondition(thruDateName, EntityOperator.GREATER_THAN_EQUAL_TO, rangeStart)
+                )
+        );
+        // thruDate is null and fromDate is before rangeEnd
+        criteria.add(
+                EntityCondition.makeCondition(
+                        EntityCondition.makeCondition(thruDateName, EntityOperator.EQUALS, null),
+                        EntityOperator.AND,
+                        EntityCondition.makeCondition(fromDateName, EntityOperator.LESS_THAN, rangeEnd)
+                )
+        );
+        // fromDate is null and thruDate is null
+        criteria.add(
+                EntityCondition.makeCondition(
+                        EntityCondition.makeCondition(thruDateName, EntityOperator.EQUALS, null),
+                        EntityOperator.AND,
+                        EntityCondition.makeCondition(fromDateName, EntityOperator.EQUALS, null)
+                )
+        );
+        // require at least one of the above to be true
+        return EntityCondition.makeCondition(criteria, EntityOperator.OR);
+    }
 }