KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: EntityExpr.java 6095 2005-11-09 01:38:54Z 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.util.Collection JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Map JavaDoc;
30
31 import org.ofbiz.base.util.Debug;
32 import org.ofbiz.entity.EntityCryptoException;
33 import org.ofbiz.entity.GenericDelegator;
34 import org.ofbiz.entity.GenericEntity;
35 import org.ofbiz.entity.GenericModelException;
36 import org.ofbiz.entity.model.ModelEntity;
37 import org.ofbiz.entity.model.ModelField;
38
39 /**
40  * Encapsulates simple expressions used for specifying queries
41  *
42  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
43  * @version $Rev: 6095 $
44  * @since 2.0
45  */

46 public class EntityExpr extends EntityCondition {
47     public static final String JavaDoc module = EntityExpr.class.getName();
48
49     private Object JavaDoc lhs;
50     private EntityOperator operator;
51     private Object JavaDoc rhs;
52
53     protected EntityExpr() {}
54
55     public EntityExpr(Object JavaDoc lhs, EntityComparisonOperator operator, Object JavaDoc rhs) {
56         if (lhs == null) {
57             throw new IllegalArgumentException JavaDoc("The field value cannot be null");
58         }
59         if (lhs instanceof String JavaDoc) {
60             Debug.logError(new Exception JavaDoc(), "EntityExpr called with lhs as a String; consider recompiling", module);
61         }
62         if (operator == null) {
63             throw new IllegalArgumentException JavaDoc("The operator argument cannot be null");
64         }
65
66         if (rhs == null || rhs == GenericEntity.NULL_FIELD) {
67             if (!EntityOperator.NOT_EQUAL.equals(operator) && !EntityOperator.EQUALS.equals(operator)) {
68                 throw new IllegalArgumentException JavaDoc("Operator must be EQUALS or NOT_EQUAL when right/rhs argument is NULL ");
69             }
70         }
71
72         if (EntityOperator.BETWEEN.equals(operator)) {
73             if (!(rhs instanceof Collection JavaDoc) || (((Collection JavaDoc) rhs).size() != 2)) {
74                 throw new IllegalArgumentException JavaDoc("BETWEEN Operator requires a Collection with 2 elements for the right/rhs argument");
75             }
76         }
77         
78         this.lhs = lhs;
79         this.operator = operator;
80         this.rhs = rhs;
81
82         //Debug.logInfo("new EntityExpr internal field=" + lhs + ", value=" + rhs + ", value type=" + (rhs == null ? "null object" : rhs.getClass().getName()), module);
83
}
84
85     public EntityExpr(String JavaDoc lhs, EntityComparisonOperator operator, Object JavaDoc rhs) {
86         this(new EntityFieldValue(lhs), operator, rhs);
87         //Debug.logInfo("new EntityExpr field=" + lhs + ", value=" + rhs + ", value type=" + (rhs == null ? "null object" : rhs.getClass().getName()), module);
88
}
89
90     public EntityExpr(String JavaDoc lhs, boolean leftUpper, EntityComparisonOperator operator, Object JavaDoc rhs, boolean rightUpper) {
91         if (lhs == null) {
92             throw new IllegalArgumentException JavaDoc("The field value cannot be null");
93         }
94         if (operator == null) {
95             throw new IllegalArgumentException JavaDoc("The operator argument cannot be null");
96         }
97         this.lhs = new EntityFieldValue(lhs);
98         if (leftUpper) this.lhs = new EntityFunction.UPPER(this.lhs);
99         this.operator = operator;
100         if (rhs instanceof EntityConditionValue) {
101             if (rightUpper) rhs = new EntityFunction.UPPER((EntityConditionValue) rhs);
102             this.rhs = rhs;
103         } else {
104             if (rightUpper) rhs = new EntityFunction.UPPER(rhs);
105             this.rhs = rhs;
106         }
107     }
108
109     public EntityExpr(EntityCondition lhs, EntityJoinOperator operator, EntityCondition rhs) {
110         if (lhs == null) {
111             throw new IllegalArgumentException JavaDoc("The left EntityCondition argument cannot be null");
112         }
113         if (rhs == null) {
114             throw new IllegalArgumentException JavaDoc("The right EntityCondition argument cannot be null");
115         }
116         if (operator == null) {
117             throw new IllegalArgumentException JavaDoc("The operator argument cannot be null");
118         }
119
120         this.lhs = lhs;
121         this.operator = operator;
122         this.rhs = rhs;
123     }
124
125     /** @deprecated */
126     public void setLUpper(boolean upper) {
127     }
128
129     /** @deprecated */
130     public boolean isLUpper() {
131         return lhs instanceof EntityFunction.UPPER;
132     }
133
134     /** @deprecated */
135     public boolean isRUpper() {
136         return rhs instanceof EntityFunction.UPPER;
137     }
138
139     /** @deprecated */
140     public void setRUpper(boolean upper) {
141     }
142
143     public Object JavaDoc getLhs() {
144         return lhs;
145     }
146
147     public EntityOperator getOperator() {
148         return operator;
149     }
150
151     public Object JavaDoc getRhs() {
152         return rhs;
153     }
154
155     public String JavaDoc makeWhereString(ModelEntity modelEntity, List JavaDoc entityConditionParams) {
156         // if (Debug.verboseOn()) Debug.logVerbose("makeWhereString for entity " + modelEntity.getEntityName(), module);
157
StringBuffer JavaDoc sql = new StringBuffer JavaDoc();
158         operator.addSqlValue(sql, modelEntity, entityConditionParams, true, lhs, rhs);
159         return sql.toString();
160     }
161
162     public boolean mapMatches(GenericDelegator delegator, Map JavaDoc map) {
163         return operator.mapMatches(delegator, map, lhs, rhs);
164     }
165
166     public void checkCondition(ModelEntity modelEntity) throws GenericModelException {
167         // if (Debug.verboseOn()) Debug.logVerbose("checkCondition for entity " + modelEntity.getEntityName(), module);
168
if (lhs instanceof EntityCondition) {
169             ((EntityCondition) lhs).checkCondition(modelEntity);
170             ((EntityCondition) rhs).checkCondition(modelEntity);
171         }
172     }
173
174     protected void addValue(StringBuffer JavaDoc buffer, ModelField field, Object JavaDoc value, List JavaDoc params) {
175         if (rhs instanceof EntityFunction.UPPER) {
176             if (value instanceof String JavaDoc) {
177                 value = ((String JavaDoc) value).toUpperCase();
178             }
179         }
180         super.addValue(buffer, field, value, params);
181     }
182
183     public EntityCondition freeze() {
184         return operator.freeze(lhs, rhs);
185     }
186
187     public void encryptConditionFields(ModelEntity modelEntity, GenericDelegator delegator) {
188         if (this.lhs instanceof String JavaDoc) {
189             ModelField modelField = modelEntity.getField((String JavaDoc) this.lhs);
190             if (modelField != null && modelField.getEncrypt()) {
191                 if (!(rhs instanceof EntityConditionValue)) {
192                     try {
193                         this.rhs = delegator.encryptFieldValue(modelEntity.getEntityName(), this.rhs);
194                     } catch (EntityCryptoException e) {
195                         Debug.logWarning(e, "Error encrypting field [" + modelEntity.getEntityName() + "." + modelField.getName() + "] with value: " + this.rhs, module);
196                     }
197                 }
198             }
199         }
200     }
201     
202     public void visit(EntityConditionVisitor visitor) {
203         visitor.acceptEntityOperator(operator, lhs, rhs);
204     }
205
206     public void accept(EntityConditionVisitor visitor) {
207         visitor.acceptEntityExpr(this);
208     }
209
210     public boolean equals(Object JavaDoc obj) {
211         if (!(obj instanceof EntityExpr)) return false;
212         EntityExpr other = (EntityExpr) obj;
213         boolean isEqual = equals(lhs, other.lhs) &&
214                equals(operator, other.operator) &&
215                equals(rhs, other.rhs);
216         //if (!isEqual) {
217
// Debug.logWarning("EntityExpr.equals is false for: \n-this.lhs=" + this.lhs + "; other.lhs=" + other.lhs +
218
// "\nthis.operator=" + this.operator + "; other.operator=" + other.operator +
219
// "\nthis.rhs=" + this.rhs + "other.rhs=" + other.rhs, module);
220
//}
221
return isEqual;
222     }
223
224     public int hashCode() {
225         return hashCode(lhs) +
226                hashCode(operator) +
227                hashCode(rhs);
228     }
229 }
230
Popular Tags