KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > entity > condition > EntityDateFilterCondition


1 /*
2  * $Id: EntityDateFilterCondition.java 5831 2005-09-26 06:52:24Z jonesde $
3  *
4  * Copyright (c) 2001-2005 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.entity.condition;
26
27 import java.sql.Timestamp JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30
31 import org.ofbiz.base.util.UtilDateTime;
32 import org.ofbiz.entity.GenericDelegator;
33 import org.ofbiz.entity.GenericModelException;
34 import org.ofbiz.entity.model.ModelEntity;
35
36 public class EntityDateFilterCondition extends EntityCondition {
37
38     protected String JavaDoc fromDateName;
39     protected String JavaDoc thruDateName;
40
41     public EntityDateFilterCondition(String JavaDoc fromDateName, String JavaDoc thruDateName) {
42         this.fromDateName = fromDateName;
43         this.thruDateName = thruDateName;
44     }
45
46     public String JavaDoc makeWhereString(ModelEntity modelEntity, List JavaDoc entityConditionParams) {
47         EntityCondition condition = makeCondition();
48         return condition.makeWhereString(modelEntity, entityConditionParams);
49     }
50
51     public void checkCondition(ModelEntity modelEntity) throws GenericModelException {
52         EntityCondition condition = makeCondition();
53         condition.checkCondition(modelEntity);
54     }
55
56     public boolean mapMatches(GenericDelegator delegator, Map JavaDoc map) {
57         EntityCondition condition = makeCondition();
58         return condition.mapMatches(delegator, map);
59     }
60
61     public boolean equals(Object JavaDoc obj) {
62         if (!(obj instanceof EntityDateFilterCondition)) return false;
63         EntityDateFilterCondition other = (EntityDateFilterCondition) obj;
64         return equals(fromDateName, other.fromDateName) && equals(thruDateName, other.thruDateName);
65     }
66
67     public int hashCode() {
68         return hashCode(fromDateName) ^ hashCode(thruDateName);
69     }
70
71     public void visit(EntityConditionVisitor visitor) {
72         visitor.acceptEntityDateFilterCondition(this);
73     }
74
75     public void accept(EntityConditionVisitor visitor) {
76         visitor.acceptEntityDateFilterCondition(this);
77     }
78
79     public EntityCondition freeze() {
80         return this;
81     }
82
83     public void encryptConditionFields(ModelEntity modelEntity, GenericDelegator delegator) {
84         // nothing to do here...
85
}
86
87     protected EntityCondition makeCondition() {
88         return makeCondition(UtilDateTime.nowTimestamp(), fromDateName, thruDateName);
89     }
90
91     public static EntityExpr makeCondition(Timestamp JavaDoc moment, String JavaDoc fromDateName, String JavaDoc thruDateName) {
92         return new EntityExpr(
93             new EntityExpr(
94                 new EntityExpr( thruDateName, EntityOperator.EQUALS, null ),
95                 EntityOperator.OR,
96                 new EntityExpr( thruDateName, EntityOperator.GREATER_THAN, moment )
97             ),
98             EntityOperator.AND,
99             new EntityExpr(
100                 new EntityExpr( fromDateName, EntityOperator.EQUALS, null ),
101                 EntityOperator.OR,
102                 new EntityExpr( fromDateName, EntityOperator.LESS_THAN_EQUAL_TO, moment )
103             )
104        );
105     }
106 }
107
Popular Tags